Inclui pequeno cliente de teste.
[cascardo/rnetproxy.git] / rnetclient.c
1 /*
2  *  Copyright (C) 2011  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
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 2 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 <unistd.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <gnutls/gnutls.h>
27
28 static void session_new(gnutls_session_t *session)
29 {
30         static void *cred;
31         gnutls_init(session, GNUTLS_CLIENT);
32         gnutls_set_default_priority(*session);
33 }
34
35 int main(int argc, char **argv)
36 {
37         struct sockaddr_in saddr;
38         int c;
39         int r;
40         char buffer[256];
41         gnutls_session_t session;
42         gnutls_global_init();
43         session_new(&session);
44         c = socket(PF_INET, SOCK_STREAM, 0);
45         saddr.sin_family = AF_INET;
46         saddr.sin_port = htons(3456);
47         saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
48         connect(c, (struct sockaddr *) &saddr, sizeof(saddr));
49         gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) c);
50         buffer[0] = 1;
51         write(c, buffer, 1);
52         r = read(c, buffer, 1);
53         if (r != 1 && buffer[0] != 'E')
54                 exit(1);
55         write(c, "00000000000000", 14);
56         r = read(c, buffer, 14);
57         if (r != 14)
58                 exit(1);
59         if ((r = gnutls_handshake(session)) < 0)
60                 fprintf(stderr, "error in handshake: %s\n",
61                                 gnutls_strerror(r));
62         else
63                 fprintf(stderr, "handshake ok\n");
64         buffer[0] = 0x40;
65         gnutls_record_send(session, buffer, 1);
66         while ((r = gnutls_record_recv(session, buffer, sizeof(buffer))) > 0)
67                 write(1, buffer, r);
68         close(c);
69         gnutls_global_deinit();
70         return 0;
71 }