From: Thadeu Lima de Souza Cascardo Date: Tue, 11 Jun 2013 22:06:25 +0000 (-0300) Subject: Encode some fields in the first protocol message. X-Git-Tag: v0.1~25 X-Git-Url: http://git.cascardo.info/?p=cascardo%2Flibreceita.git;a=commitdiff_plain;h=5d5cb6b57c38bad07851464bcb3ea6a2ace464c3 Encode some fields in the first protocol message. Read some fields from the decfile and add those to a message built for the ReceitaNet protocol. --- diff --git a/Makefile.am b/Makefile.am index f84ab59..20fe476 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 0000000..6dc6c1e --- /dev/null +++ b/rnet_encode.c @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * 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 +#include +#include +#include +#include +#include +#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 index 0000000..4186642 --- /dev/null +++ b/rnet_encode.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * 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 diff --git a/rnetclient.c b/rnetclient.c index de903cd..4e3f469 100644 --- a/rnetclient.c +++ b/rnetclient.c @@ -28,6 +28,8 @@ #include #include #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);