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