--- /dev/null
+/*
+ * Copyright (C) 2013 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+#include <netdb.h>
+#include <fcntl.h>
+
+static int add_field(char *buffer, size_t len, char *key, int klen, char *val, int vlen)
+{
+ int n = 0;
+ if (len < (klen + vlen + 3))
+ return -ENOMEM;
+ if (klen > 0x7f || klen < 0)
+ return -EINVAL;
+ if (vlen > 0x7fff || vlen < 0)
+ return -EINVAL;
+ buffer[0] = klen & 0x7f;
+ if (vlen > 0x7f)
+ buffer[0] |= 0x80;
+ buffer++;
+ n++;
+ memcpy(buffer, key, klen);
+ buffer += klen;
+ n += klen;
+ if (vlen > 0x7f) {
+ buffer[0] = (vlen >> 8) & 0x7f;
+ buffer[1] = vlen & 0xff;
+ buffer += 2;
+ n += 2;
+ } else {
+ buffer[0] = vlen & 0x7f;
+ buffer++;
+ n++;
+ }
+ memcpy(buffer, val, vlen);
+ n += vlen;
+ return n;
+}
+
+static int add_u32(char *buffer, int len, char *key, uint32_t val)
+{
+ uint32_t nval = htonl(val);
+ return add_field(buffer, len, key, strlen(key), (char *) &nval, sizeof(val));
+}
+
+static int add_u8(char *buffer, int len, char *key, uint8_t val)
+{
+ return add_field(buffer, len, key, strlen(key), (char *) &val, sizeof(val));
+}
+
+static int add_ascii(char *buffer, int len, char *key, char *val)
+{
+ return add_field(buffer, len, key, strlen(key), val, strlen(val));
+}
+
+int resp(char **buffer)
+{
+ int n;
+ int slen = 2048;
+ char *p;
+ char *end;
+ char file[512];
+ int r = 0;
+ uint64_t pos = 0;
+ //r = read(0, file, sizeof(file) - 1);
+ file[r] = 0;
+ if (*buffer) {
+ return -EINVAL;
+ }
+ *buffer = malloc(slen);
+ if (*buffer == NULL) {
+ return -ENOMEM;
+ }
+ memset(*buffer, 0, slen);
+ end = *buffer + slen;
+ p = *buffer;
+ p[0] = 0x01;
+ p++;
+ n = add_field(p, end - p, "POS_INICIO", strlen("POS_INICIO"), (char *)&pos, 8);
+ p += n;
+/*
+ n = add_u32(p, end - p, "POS_INICIO", 0);
+ p += n;
+ n = add_u8(p, end - p, "SUBTIPO", 0);
+ p += n;
+ n = add_ascii(p, end - p, "TEXTO", "0001.ERRO! Buahahahaha!");
+ p += n;
+ n = add_ascii(p, end - p, "ARQUIVO", file);
+ p += n;
+*/
+ return p - *buffer;
+}
+
+#ifdef MAIN
+int main(int argc, char **argv)
+{
+ size_t n;
+ char *buffer = NULL;
+ n = resp(&buffer);
+ if (n > 0)
+ write(1, buffer, n);
+ free(buffer);
+ return 0;
+}
+#endif
*/
#include <string.h>
+#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <gnutls/gnutls.h>
+#include <zlib.h>
+#include <resp.c>
#define DH_BITS 1024
static void * get_creds(char *certfile, char *keyfile)
}
#undef DH_BITS
+static int buildRecord(char *buffer, size_t len, char **out, size_t *olen)
+{
+ *olen = len + 4;
+ *out = malloc(*olen);
+ (*out)[0] = 0x0;
+ (*out)[1] = (len >> 8);
+ (*out)[2] = (len & 0xff);
+ (*out)[3] = 0x1;
+ memcpy(*out + 4, buffer, len);
+ return 0;
+}
+
+static int deflateRecord(char *buffer, size_t len, char **out, size_t *olen)
+{
+ z_stream zstrm;
+ int r;
+ zstrm.zalloc = Z_NULL;
+ zstrm.zfree = Z_NULL;
+ zstrm.opaque = Z_NULL;
+ if ((r = deflateInit(&zstrm, Z_DEFAULT_COMPRESSION)) != Z_OK)
+ return -1;
+ *out = malloc(len * 2 + 36);
+ if (!out) {
+ deflateEnd(&zstrm);
+ return -1;
+ }
+ zstrm.next_in = buffer;
+ zstrm.avail_in = len;
+ zstrm.next_out = *out + 6;
+ zstrm.avail_out = len * 2 + 30;
+ while ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END &&
+ zstrm.avail_out > 0);
+ if ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END) {
+ deflateEnd(&zstrm);
+ free(*out);
+ return -1;
+ }
+ *olen = zstrm.avail_out + 6;
+ (*out)[0] = 0x1;
+ (*out)[1] = (zstrm.avail_out >> 8);
+ (*out)[2] = (zstrm.avail_out & 0xff);
+ (*out)[3] = (len >> 8);
+ (*out)[4] = (len & 0xff);
+ (*out)[5] = 0x1;
+ deflateEnd(&zstrm);
+ return 0;
+}
+
-static char response[65536];
+static char *response;
+static char *def_response;
int main(int argc, char **argv)
{
struct sockaddr_in saddr;
int c;
int r;
- char buffer[256];
+ char buffer[8192];
int resp_size;
+ int def_resp_size;
int count = 0;
int val = 1;
gnutls_session_t session;
write(c, "E", 1);
r = read(c, buffer, 14);
if (r == 14 && !memcmp(buffer, "00000000000000", 14))
- write(c, "08082012225300", 14);
+ write(c, "08082013225300", 14);
if ((r = gnutls_handshake(session)) < 0)
fprintf(stderr, "error in handshake: %s\n",
gnutls_strerror(r));
else
fprintf(stderr, "handshake ok\n");
while ((r = gnutls_record_recv(session, buffer, sizeof(buffer))) > 0) {
- write(1, buffer, r);
count++;
- if (count == 3) {
- resp_size = read(0, response, sizeof(response));
- gnutls_record_send(session, response, resp_size);
+ if (count >= 2)
+ write(1, buffer, r);
+ if (count > 1) {
+ resp_size = resp(&response);
+ buildRecord(response, resp_size, &def_response, &def_resp_size);
+ gnutls_record_send(session, def_response, def_resp_size);
}
}
close(c);