2 * Copyright (C) 2012-2013 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
28 #include <gnutls/gnutls.h>
31 #include "rnet_message.h"
32 #include "rnet_encode.h"
34 static void * get_creds(char *certfile)
36 static gnutls_certificate_credentials_t cred;
37 gnutls_certificate_allocate_credentials(&cred);
38 gnutls_certificate_set_x509_trust_file(cred, certfile,
43 static void session_new(gnutls_session_t *session)
46 cred = get_creds("cert.pem");
47 gnutls_init(session, GNUTLS_CLIENT);
48 gnutls_set_default_priority(*session);
49 gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE, cred);
52 static int deflateRecord(char *buffer, size_t len, char **out, size_t *olen)
56 zstrm.zalloc = Z_NULL;
58 zstrm.opaque = Z_NULL;
59 if ((r = deflateInit(&zstrm, Z_DEFAULT_COMPRESSION)) != Z_OK)
61 *out = malloc(len * 2 + 36);
66 zstrm.next_in = buffer;
68 zstrm.next_out = *out + 6;
69 zstrm.avail_out = len * 2 + 30;
70 while ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END &&
72 if ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END) {
77 *olen = zstrm.avail_out + 6;
79 (*out)[1] = (zstrm.avail_out >> 8);
80 (*out)[2] = (zstrm.avail_out & 0xff);
81 (*out)[3] = (len >> 8);
82 (*out)[4] = (len & 0xff);
88 static int inflateRecord(char *buffer, size_t len, char **out, size_t *olen)
92 zstrm.zalloc = Z_NULL;
94 zstrm.opaque = Z_NULL;
95 if ((r = inflateInit(&zstrm)) != Z_OK)
97 *olen = (buffer[3] << 8 & buffer[4]);
103 zstrm.next_in = buffer + 6;
104 zstrm.avail_in = len - 6;
105 zstrm.next_out = *out;
106 zstrm.avail_out = *olen;
107 while ((r = inflate(&zstrm, Z_FINISH)) != Z_STREAM_END &&
108 zstrm.avail_out > 0);
109 if ((r = inflate(&zstrm, Z_FINISH)) != Z_STREAM_END) {
118 #define RNET_ADDRESS "receitanet.receita.fazenda.gov.br"
120 static int connect_rnet(int *c)
122 struct addrinfo *addresses;
123 struct addrinfo *addr;
124 struct addrinfo hint;
125 struct sockaddr_in saddr;
129 memset(&hint, 0, sizeof(hint));
130 hint.ai_family = AF_UNSPEC;
131 hint.ai_socktype = SOCK_STREAM;
132 hint.ai_protocol = IPPROTO_TCP;
133 hint.ai_flags = AI_ADDRCONFIG;
134 r = getaddrinfo(RNET_ADDRESS, "3456", &hint, &addresses);
138 for (addr = addresses; addr != NULL; addr = addr->ai_next) {
139 fd = socket(addr->ai_family, addr->ai_socktype,
142 if (!(r = connect(fd, addr->ai_addr,
148 freeaddrinfo(addresses);
155 static int handshake(int c)
161 write(c, "00000000000000", 14);
162 r = read(c, buffer, 1);
163 if (r != 1 && buffer[0] != 'E')
165 r = read(c, buffer, 14);
171 static void usage(void)
173 fprintf(stderr, "rnetclient [filename]\n");
177 int main(int argc, char **argv)
184 struct rnet_decfile *decfile;
185 struct rnet_message *message = NULL;
186 gnutls_session_t session;
192 decfile = rnet_decfile_open(argv[1]);
194 fprintf(stderr, "could not parse %s: %s\n", argv[1], strerror(errno));
198 gnutls_global_init();
200 session_new(&session);
201 r = connect_rnet(&c);
203 fprintf(stderr, "error connecting to server: %s\n",
204 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
207 gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) c);
212 if ((r = gnutls_handshake(session)) < 0)
213 fprintf(stderr, "error in handshake: %s\n",
216 rnet_encode(decfile, &message);
217 deflateRecord(message->buffer, message->len, &out, &olen);
218 gnutls_record_send(session, out, olen);
221 while ((r = gnutls_record_recv(session, buffer, sizeof(buffer))) > 0)
225 rnet_decfile_close(decfile);
227 gnutls_global_deinit();