8e21aae6acfd5561bd703325870ac755e3f0f617
[cascardo/rnetproxy.git] / rnetserver.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 <unistd.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <gnutls/gnutls.h>
26
27 #define DH_BITS 1024
28 static void * get_creds(char *certfile, char *keyfile)
29 {
30         static gnutls_certificate_credentials_t cred;
31         gnutls_dh_params_t dh_params;
32         gnutls_dh_params_init(&dh_params);
33         gnutls_dh_params_generate2(dh_params, DH_BITS);
34         gnutls_certificate_allocate_credentials(&cred);
35         gnutls_certificate_set_x509_key_file(cred, certfile, keyfile,
36                                         GNUTLS_X509_FMT_PEM);
37         gnutls_certificate_set_dh_params(cred, dh_params);
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", "key.pem");
45         gnutls_init(session, GNUTLS_SERVER);
46         gnutls_set_default_priority(*session);
47         gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE, cred);
48         gnutls_dh_set_prime_bits(*session, DH_BITS);
49 }
50 #undef DH_BITS
51
52
53 static char response[65536];
54
55 int main(int argc, char **argv)
56 {
57         int s;
58         struct sockaddr_in saddr;
59         int c;
60         int r;
61         char buffer[256];
62         int resp_size;
63         int count = 0;
64         int val = 1;
65         gnutls_session_t session;
66         gnutls_global_init();
67         session_new(&session);
68         s = socket(PF_INET, SOCK_STREAM, 0);
69         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
70         saddr.sin_family = AF_INET;
71         saddr.sin_port = htons(3456);
72         saddr.sin_addr.s_addr = htonl(INADDR_ANY);
73         bind(s, (struct sockaddr *) &saddr, sizeof(saddr));
74         listen(s, 5);
75         c = accept(s, NULL, NULL);
76         close(s);
77         gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) c);
78         r = read(c, buffer, 1);
79         if (r == 1 && buffer[0] == 1)
80                 write(c, "E", 1);
81         r = read(c, buffer, 14);
82         if (r == 14 && !memcmp(buffer, "00000000000000", 14))
83                 write(c, "08082012225300", 14);
84         if ((r = gnutls_handshake(session)) < 0)
85                 fprintf(stderr, "error in handshake: %s\n",
86                                 gnutls_strerror(r));
87         else
88                 fprintf(stderr, "handshake ok\n");
89         while ((r = gnutls_record_recv(session, buffer, sizeof(buffer))) > 0) {
90                 write(1, buffer, r);
91                 count++;
92                 if (count == 3) {
93                         resp_size = read(0, response, sizeof(response));
94                         gnutls_record_send(session, response, resp_size);
95                 }
96         }
97         close(c);
98         gnutls_global_deinit();
99         return 0;
100 }