4e3f4696a28c0a26051f9d1f244f2f0f7660e371
[cascardo/libreceita.git] / rnetclient.c
1 /*
2  *  Copyright (C) 2012-2013  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netdb.h>
28 #include <gnutls/gnutls.h>
29 #include <zlib.h>
30 #include "decfile.h"
31 #include "rnet_message.h"
32 #include "rnet_encode.h"
33
34 static void * get_creds(char *certfile)
35 {
36         static gnutls_certificate_credentials_t cred;
37         gnutls_certificate_allocate_credentials(&cred);
38         gnutls_certificate_set_x509_trust_file(cred, certfile,
39                                         GNUTLS_X509_FMT_PEM);
40         return cred;
41 }
42
43 static void session_new(gnutls_session_t *session)
44 {
45         static void *cred;
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);
50 }
51
52 static int deflateRecord(char *buffer, size_t len, char **out, size_t *olen)
53 {
54         z_stream zstrm;
55         int r;
56         zstrm.zalloc = Z_NULL;
57         zstrm.zfree = Z_NULL;
58         zstrm.opaque = Z_NULL;
59         if ((r = deflateInit(&zstrm, Z_DEFAULT_COMPRESSION)) != Z_OK)
60                 return -1;
61         *out = malloc(len * 2 + 36);
62         if (!out) {
63                 deflateEnd(&zstrm);
64                 return -1;
65         }
66         zstrm.next_in = buffer;
67         zstrm.avail_in = len;
68         zstrm.next_out = *out + 6;
69         zstrm.avail_out = len * 2 + 30;
70         while ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END &&
71                 zstrm.avail_out > 0);
72         if ((r = deflate(&zstrm, Z_FINISH)) != Z_STREAM_END) {
73                 deflateEnd(&zstrm);
74                 free(*out);
75                 return -1;
76         }
77         *olen = zstrm.avail_out + 6;
78         (*out)[0] = 0x1;
79         (*out)[1] = (zstrm.avail_out >> 8);
80         (*out)[2] = (zstrm.avail_out & 0xff);
81         (*out)[3] = (len >> 8);
82         (*out)[4] = (len & 0xff);
83         (*out)[5] = 0x1;
84         deflateEnd(&zstrm);
85         return 0;
86 }
87
88 static int inflateRecord(char *buffer, size_t len, char **out, size_t *olen)
89 {
90         z_stream zstrm;
91         int r;
92         zstrm.zalloc = Z_NULL;
93         zstrm.zfree = Z_NULL;
94         zstrm.opaque = Z_NULL;
95         if ((r = inflateInit(&zstrm)) != Z_OK)
96                 return -1;
97         *olen = (buffer[3] << 8 & buffer[4]);
98         *out = malloc(*olen);
99         if (!out) {
100                 inflateEnd(&zstrm);
101                 return -1;
102         }
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) {
110                 inflateEnd(&zstrm);
111                 free(*out);
112                 return -1;
113         }
114         inflateEnd(&zstrm);
115         return 0;
116 }
117
118 #define RNET_ADDRESS "receitanet.receita.fazenda.gov.br"
119
120 static int connect_rnet(int *c)
121 {
122         struct addrinfo *addresses;
123         struct addrinfo *addr;
124         struct addrinfo hint;
125         struct sockaddr_in saddr;
126         int r;
127         int fd = *c = -1;
128         int i;
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);
135         if (r) {
136                 return r;
137         }
138         for (addr = addresses; addr != NULL; addr = addr->ai_next) {
139                 fd = socket(addr->ai_family, addr->ai_socktype,
140                                 addr->ai_protocol);
141                 if (fd >= 0)
142                         if (!(r = connect(fd, addr->ai_addr,
143                                                 addr->ai_addrlen)))
144                                 break;
145                 close(fd);
146                 fd = -1;
147         }
148         freeaddrinfo(addresses);
149         *c = fd;
150         if (fd == -1)
151                 return EAI_SYSTEM;
152         return 0;
153 }
154
155 static int handshake(int c)
156 {
157         char buffer[16];
158         int r;
159         buffer[0] = 1;
160         write(c, buffer, 1);
161         write(c, "00000000000000", 14);
162         r = read(c, buffer, 1);
163         if (r != 1 && buffer[0] != 'E')
164                 return -1;
165         r = read(c, buffer, 14);
166         if (r != 14)
167                 return -1;
168         return 0;
169 }
170
171 static void usage(void)
172 {
173         fprintf(stderr, "rnetclient [filename]\n");
174         exit(1);
175 }
176
177 int main(int argc, char **argv)
178 {
179         int c;
180         int r;
181         char buffer[2048];
182         char *out;
183         size_t olen;
184         struct rnet_decfile *decfile;
185         struct rnet_message *message = NULL;
186         gnutls_session_t session;
187         
188         if (argc < 2) {
189                 usage();
190         }
191
192         decfile = rnet_decfile_open(argv[1]);
193         if (!decfile) {
194                 fprintf(stderr, "could not parse %s: %s\n", argv[1], strerror(errno));
195                 exit(1);
196         }
197
198         gnutls_global_init();
199
200         session_new(&session);
201         r = connect_rnet(&c);
202         if (r) {
203                 fprintf(stderr, "error connecting to server: %s\n",
204                         r == EAI_SYSTEM ? strerror(errno) : gai_strerror(r));
205                 exit(1);
206         }
207         gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) c);
208         r = handshake(c);
209         if (r < 0) {
210                 exit(1);
211         }
212         if ((r = gnutls_handshake(session)) < 0)
213                 fprintf(stderr, "error in handshake: %s\n",
214                                 gnutls_strerror(r));
215
216         rnet_encode(decfile, &message);
217         deflateRecord(message->buffer, message->len, &out, &olen);
218         gnutls_record_send(session, out, olen);
219         free(out);
220
221         while ((r = gnutls_record_recv(session, buffer, sizeof(buffer))) > 0)
222                 write(1, buffer, r);
223         close(c);
224
225         rnet_decfile_close(decfile);
226
227         gnutls_global_deinit();
228
229         return 0;
230 }