2 * Copyright (C) 2012 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 static void * get_creds(char *certfile)
33 static gnutls_certificate_credentials_t cred;
34 gnutls_certificate_allocate_credentials(&cred);
35 gnutls_certificate_set_x509_trust_file(cred, certfile,
40 static void session_new(gnutls_session_t *session)
43 cred = get_creds("cert.pem");
44 gnutls_init(session, GNUTLS_CLIENT);
45 gnutls_set_default_priority(*session);
46 gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE, cred);
49 static int deflateRecord(char *buffer, size_t len, char **out, size_t *olen)
53 zstrm.zalloc = Z_NULL;
55 zstrm.opaque = Z_NULL;
56 if ((r = deflateInit(&zstrm, Z_DEFAULT_COMPRESSION)) != Z_OK)
58 *out = malloc(len * 2 + 36);
63 zstrm.next_in = buffer;
65 zstrm.next_out = *out + 6;
66 zstrm.avail_out = len * 2 + 30;
67 while ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END &&
69 if ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END) {
74 *olen = zstrm.avail_out + 6;
76 (*out)[1] = (zstrm.avail_out >> 8);
77 (*out)[2] = (zstrm.avail_out & 0xff);
78 (*out)[3] = (len >> 8);
79 (*out)[4] = (len & 0xff);
85 static int inflateRecord(char *buffer, size_t len, char **out, size_t *olen)
89 zstrm.zalloc = Z_NULL;
91 zstrm.opaque = Z_NULL;
92 if ((r = inflateInit(&zstrm)) != Z_OK)
94 *olen = (buffer[3] << 8 & buffer[4]);
100 zstrm.next_in = buffer + 6;
101 zstrm.avail_in = len - 6;
102 zstrm.next_out = *out;
103 zstrm.avail_out = *olen;
104 while ((r = inflate(&zstrm, Z_FINISH)) != Z_STREAM_END &&
105 zstrm.avail_out > 0);
106 if ((r = inflate(&zstrm, Z_FINISH)) != Z_STREAM_END) {
115 #define RNET_ADDRESS "receitanet.receita.fazenda.gov.br"
117 static int connect_rnet(int *c)
119 struct addrinfo *addresses;
120 struct addrinfo *addr;
121 struct addrinfo hint;
122 struct sockaddr_in saddr;
126 memset(&hint, 0, sizeof(hint));
127 hint.ai_family = AF_UNSPEC;
128 hint.ai_socktype = SOCK_STREAM;
129 hint.ai_protocol = IPPROTO_TCP;
130 hint.ai_flags = AI_ADDRCONFIG;
131 r = getaddrinfo(RNET_ADDRESS, "3456", &hint, &addresses);
135 for (addr = addresses; addr != NULL; addr = addr->ai_next) {
136 fd = socket(addr->ai_family, addr->ai_socktype,
139 if (!(r = connect(fd, addr->ai_addr,
145 freeaddrinfo(addresses);
152 static int handshake(int c)
158 write(c, "00000000000000", 14);
159 r = read(c, buffer, 1);
160 if (r != 1 && buffer[0] != 'E')
162 r = read(c, buffer, 14);
168 int main(int argc, char **argv)
175 gnutls_session_t session;
176 gnutls_global_init();
177 session_new(&session);
178 r = connect_rnet(&c);
180 fprintf(stderr, "error connecting to server: %s\n",
181 r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
184 gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) c);
189 if ((r = gnutls_handshake(session)) < 0)
190 fprintf(stderr, "error in handshake: %s\n",
193 fprintf(stderr, "handshake ok\n");
194 r = read(0, buffer, sizeof(buffer));
195 deflateRecord(buffer, r, &out, &olen);
196 gnutls_record_send(session, out, olen);
198 while ((r = gnutls_record_recv(session, buffer, sizeof(buffer))) > 0)
201 gnutls_global_deinit();