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>
32 static void * get_creds(char *certfile)
34 static gnutls_certificate_credentials_t cred;
35 gnutls_certificate_allocate_credentials(&cred);
36 gnutls_certificate_set_x509_trust_file(cred, certfile,
41 static void session_new(gnutls_session_t *session)
44 cred = get_creds("cert.pem");
45 gnutls_init(session, GNUTLS_CLIENT);
46 gnutls_set_default_priority(*session);
47 gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE, cred);
50 static int deflateRecord(char *buffer, size_t len, char **out, size_t *olen)
54 zstrm.zalloc = Z_NULL;
56 zstrm.opaque = Z_NULL;
57 if ((r = deflateInit(&zstrm, Z_DEFAULT_COMPRESSION)) != Z_OK)
59 *out = malloc(len * 2 + 36);
64 zstrm.next_in = buffer;
66 zstrm.next_out = *out + 6;
67 zstrm.avail_out = len * 2 + 30;
68 while ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END &&
70 if ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END) {
75 *olen = zstrm.avail_out + 6;
77 (*out)[1] = (zstrm.avail_out >> 8);
78 (*out)[2] = (zstrm.avail_out & 0xff);
79 (*out)[3] = (len >> 8);
80 (*out)[4] = (len & 0xff);
86 static int inflateRecord(char *buffer, size_t len, char **out, size_t *olen)
90 zstrm.zalloc = Z_NULL;
92 zstrm.opaque = Z_NULL;
93 if ((r = inflateInit(&zstrm)) != Z_OK)
95 *olen = (buffer[3] << 8 & buffer[4]);
101 zstrm.next_in = buffer + 6;
102 zstrm.avail_in = len - 6;
103 zstrm.next_out = *out;
104 zstrm.avail_out = *olen;
105 while ((r = inflate(&zstrm, Z_FINISH)) != Z_STREAM_END &&
106 zstrm.avail_out > 0);
107 if ((r = inflate(&zstrm, Z_FINISH)) != Z_STREAM_END) {
116 #define RNET_ADDRESS "receitanet.receita.fazenda.gov.br"
118 static int connect_rnet(int *c)
120 struct addrinfo *addresses;
121 struct addrinfo *addr;
122 struct addrinfo hint;
123 struct sockaddr_in saddr;
127 memset(&hint, 0, sizeof(hint));
128 hint.ai_family = AF_UNSPEC;
129 hint.ai_socktype = SOCK_STREAM;
130 hint.ai_protocol = IPPROTO_TCP;
131 hint.ai_flags = AI_ADDRCONFIG;
132 r = getaddrinfo(RNET_ADDRESS, "3456", &hint, &addresses);
136 for (addr = addresses; addr != NULL; addr = addr->ai_next) {
137 fd = socket(addr->ai_family, addr->ai_socktype,
140 if (!(r = connect(fd, addr->ai_addr,
146 freeaddrinfo(addresses);
153 static int handshake(int c)
159 write(c, "00000000000000", 14);
160 r = read(c, buffer, 1);
161 if (r != 1 && buffer[0] != 'E')
163 r = read(c, buffer, 14);
169 static void usage(void)
171 fprintf(stderr, "rnetclient [filename]\n");
175 int main(int argc, char **argv)
182 struct rnet_decfile *decfile;
183 gnutls_session_t session;
189 decfile = rnet_decfile_open(argv[1]);
191 fprintf(stderr, "could not parse %s: %s\n", argv[1], strerror(errno));
195 gnutls_global_init();
197 session_new(&session);
198 r = connect_rnet(&c);
200 fprintf(stderr, "error connecting to server: %s\n",
201 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
204 gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) c);
209 if ((r = gnutls_handshake(session)) < 0)
210 fprintf(stderr, "error in handshake: %s\n",
213 fprintf(stderr, "handshake ok\n");
214 r = read(0, buffer, sizeof(buffer));
215 deflateRecord(buffer, r, &out, &olen);
216 gnutls_record_send(session, out, olen);
218 while ((r = gnutls_record_recv(session, buffer, sizeof(buffer))) > 0)
222 rnet_decfile_close(decfile);
224 gnutls_global_deinit();