Encode some fields in the first protocol message.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Tue, 11 Jun 2013 22:06:25 +0000 (19:06 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Wed, 12 Jun 2013 11:19:44 +0000 (08:19 -0300)
Read some fields from the decfile and add those to a message built for
the ReceitaNet protocol.

Makefile.am
rnet_encode.c [new file with mode: 0644]
rnet_encode.h [new file with mode: 0644]
rnetclient.c

index f84ab59..20fe476 100644 (file)
@@ -1,3 +1,3 @@
 bin_PROGRAMS = rnetclient
 rnetclient_SOURCES = rnetclient.c decfile.c decfile.h pmhash.c pmhash.h \
-       rnet_message.c rnet_message.h
+       rnet_message.c rnet_message.h rnet_encode.c rnet_encode.h
diff --git a/rnet_encode.c b/rnet_encode.c
new file mode 100644 (file)
index 0000000..6dc6c1e
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ *  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 "rnet_encode.h"
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include "rnet_message.h"
+#include "decfile.h"
+
+int rnet_encode(struct rnet_decfile *decfile, struct rnet_message **msg)
+{
+       int r;
+       uint32_t tp_arq;
+       uint32_t id_dec;
+       char *cpf;
+       char *codigo_recnet;
+       char *ano;
+       char *exerc;
+       *msg = rnet_message_new();
+       if (*msg == NULL) {
+               return -ENOMEM;
+       }
+
+       codigo_recnet = rnet_decfile_get_header_field(decfile, "codigo_recnet");
+       tp_arq = strtoul(codigo_recnet, NULL, 10);
+       id_dec = strtoul(rnet_decfile_get_header_field(decfile, "hash"), NULL, 10);
+       cpf = rnet_decfile_get_header_field(decfile, "cpf");
+       ano = rnet_decfile_get_header_field(decfile, "ano");
+       exerc = rnet_decfile_get_header_field(decfile, "exerc");
+
+       (*msg)->buffer[0] = 0x40;
+       (*msg)->len = 1;
+       r = rnet_message_add_u32(*msg, "a_comp", 0);
+       r = rnet_message_add_u32(*msg, "tp_arq", tp_arq);
+       r = rnet_message_add_u32(*msg, "id_dec", id_dec);
+       r = rnet_message_add_ascii(*msg, "exercicio", ano);
+       r = rnet_message_add_ascii(*msg, "exercicio_pgd", exerc);
+       r = rnet_message_add_ascii(*msg, "ni", cpf);
+       r = rnet_message_add_ascii(*msg, "tipo_ni", "CPF");
+       if (r < 0)
+               return r;
+       return 0;
+}
diff --git a/rnet_encode.h b/rnet_encode.h
new file mode 100644 (file)
index 0000000..4186642
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ *  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.
+ */
+
+#ifndef _RNET_ENCODE_H
+#define _RNET_ENCODE_H
+
+#include "rnet_message.h"
+#include "decfile.h"
+
+int rnet_encode(struct rnet_decfile *decfile, struct rnet_message **msg);
+
+#endif
index de903cd..4e3f469 100644 (file)
@@ -28,6 +28,8 @@
 #include <gnutls/gnutls.h>
 #include <zlib.h>
 #include "decfile.h"
+#include "rnet_message.h"
+#include "rnet_encode.h"
 
 static void * get_creds(char *certfile)
 {
@@ -180,6 +182,7 @@ int main(int argc, char **argv)
        char *out;
        size_t olen;
        struct rnet_decfile *decfile;
+       struct rnet_message *message = NULL;
        gnutls_session_t session;
        
        if (argc < 2) {
@@ -209,10 +212,12 @@ int main(int argc, char **argv)
        if ((r = gnutls_handshake(session)) < 0)
                fprintf(stderr, "error in handshake: %s\n",
                                gnutls_strerror(r));
-       r = read(0, buffer, sizeof(buffer));
-       deflateRecord(buffer, r, &out, &olen);
+
+       rnet_encode(decfile, &message);
+       deflateRecord(message->buffer, message->len, &out, &olen);
        gnutls_record_send(session, out, olen);
        free(out);
+
        while ((r = gnutls_record_recv(session, buffer, sizeof(buffer))) > 0)
                write(1, buffer, r);
        close(c);