2
#include <sys/types.h> //socket, setsockopt, bind, listen, accept,
4
#include <sys/socket.h> //socket, setsockopt, bind, listen, accept,
6
#include <sys/ioctl.h> //ioctl, sockaddr_ll, ifreq
7
#include <unistd.h> //write, close
8
#include <netinet/ip.h> // sockaddr_in
9
#include <gnutls/gnutls.h>
10
#include <gnutls/x509.h> // gnutls_x509_crt_init, gnutls_x509_crt_import, gnutls_x509_crt_get_dn
11
#include <arpa/inet.h> // inet_ntop, htons
12
#include <net/if.h> //ifreq
18
#include <algorithm> // std::max
19
#include <cstdlib> // exit()
21
#define SOCKET_ERR(err,s) if(err<0) {perror(s);exit(1);}
24
#define KEYFILE "key.pem"
25
#define CERTFILE "cert.pem"
26
#define CAFILE "ca.pem"
27
#define CRLFILE "crl.pem"
30
/* These are global */
31
gnutls_certificate_credentials_t x509_cred;
33
static gnutls_dh_params_t dh_params;
39
/* Generate Diffie Hellman parameters - for use with DHE
40
* kx algorithms. These should be discarded and regenerated
41
* once a day, once a week or once a month. Depending on the
42
* security requirements.
44
gnutls_dh_params_init (&dh_params);
45
gnutls_dh_params_generate2 (dh_params, DH_BITS);
51
initialize_tls_session ()
53
gnutls_session_t session;
55
gnutls_global_init ();
57
gnutls_certificate_allocate_credentials (&x509_cred);
58
gnutls_certificate_set_x509_trust_file (x509_cred, CAFILE,
60
gnutls_certificate_set_x509_crl_file (x509_cred, CRLFILE,
62
gnutls_certificate_set_x509_key_file (x509_cred, CERTFILE, KEYFILE,
65
generate_dh_params ();
66
gnutls_certificate_set_dh_params (x509_cred, dh_params);
68
gnutls_init (&session, GNUTLS_SERVER);
69
gnutls_set_default_priority (session);
70
gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
72
// request client certificate if any.
74
gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
75
gnutls_dh_set_prime_bits (session, DH_BITS);
81
void udpreply(int &sd){
82
struct sockaddr_in6 sa_cli;
87
socklen_t sa_cli_len = sizeof(sa_cli);
88
ret = recvfrom(sd, buffer, 512,0,
89
reinterpret_cast<sockaddr *>(& sa_cli), & sa_cli_len);
90
SOCKET_ERR (ret, "recvfrom");
93
if (strncmp(buffer,"Marco", 5) == 0){
94
ret = sendto(sd, "Polo", 4, 0, reinterpret_cast<sockaddr *>(& sa_cli),
96
SOCKET_ERR (ret, "sendto");
101
void tcpreply(int sd, struct sockaddr_in6 sa_cli, gnutls_session_t session){
106
printf ("- connection from %s, port %d\n",
107
inet_ntop (AF_INET6, &sa_cli.sin6_addr, buffer,
108
sizeof (buffer)), ntohs (sa_cli.sin6_port));
111
gnutls_transport_set_ptr (session, reinterpret_cast<gnutls_transport_ptr_t> (sd));
114
ret = gnutls_handshake (session);
118
gnutls_deinit (session);
119
fprintf (stderr, "*** Handshake has failed (%s)\n\n",
120
gnutls_strerror (ret));
123
printf ("- Handshake was completed\n");
127
ret = gnutls_certificate_verify_peers2 (session, &status);
131
printf ("Verify failed\n");
135
if (status & GNUTLS_CERT_INVALID)
136
printf ("The certificate is not trusted.\n");
138
if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
139
printf ("The certificate hasn't got a known issuer.\n");
141
if (status & GNUTLS_CERT_REVOKED)
142
printf ("The certificate has been revoked.\n");
144
if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509){
145
printf("Recived certificate not X.509\n");
149
const gnutls_datum_t *cert_list;
150
unsigned int cert_list_size = 0;
151
gnutls_x509_crt_t cert;
155
cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
157
printf ("Peer provided %d certificates.\n", cert_list_size);
159
if (cert_list_size == 0){
160
printf("No certificates recived\n"); //should never happen because verify_peers2 should fail if so
164
gnutls_x509_crt_init (&cert);
166
// XXX -Checking only first cert, might want to check them all
167
gnutls_x509_crt_import (cert, &cert_list[0], GNUTLS_X509_FMT_DER);
170
gnutls_x509_crt_get_dn (cert, dn, &size);
172
printf ("DN: %s\n", dn);
175
ret = gnutls_record_recv (session, buffer, sizeof(buffer));
179
write(1, buffer, ret);
182
fprintf (stderr, "\n*** Received corrupted "
183
"data(%d). Closing the connection.\n\n", ret);
186
gnutls_bye (session, GNUTLS_SHUT_WR);
188
gnutls_deinit (session);
189
gnutls_certificate_free_credentials (x509_cred);
190
gnutls_global_deinit ();
195
int ret, err, udp_listen_sd, tcp_listen_sd;
196
struct sockaddr_in6 sa_serv;
197
struct sockaddr_in6 sa_cli;
200
socklen_t client_len;
202
gnutls_session_t session;
206
session = initialize_tls_session ();
208
//UDP socket creation
209
udp_listen_sd = socket (PF_INET6, SOCK_DGRAM, 0);
210
SOCKET_ERR (udp_listen_sd, "socket");
212
memset (&sa_serv, '\0', sizeof (sa_serv));
213
sa_serv.sin6_family = AF_INET6;
214
sa_serv.sin6_addr = in6addr_any; //XXX only listen to link local?
215
sa_serv.sin6_port = htons (PORT); /* Server Port number */
217
ret = setsockopt (udp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
218
SOCKET_ERR(ret,"setsockopt reuseaddr");
220
ret = setsockopt(udp_listen_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
221
SOCKET_ERR(ret,"setsockopt bindtodevice");
225
ret = setsockopt(udp_listen_sd, SOL_SOCKET, SO_BROADCAST, & flag, sizeof(flag));
226
SOCKET_ERR(ret,"setsockopt broadcast");
229
err = bind (udp_listen_sd, reinterpret_cast<const sockaddr *> (& sa_serv),
231
SOCKET_ERR (err, "bind");
233
//UDP socket creation done
236
//TCP socket creation
238
tcp_listen_sd = socket(PF_INET6, SOCK_STREAM, 0);
239
SOCKET_ERR(tcp_listen_sd,"socket");
241
setsockopt(tcp_listen_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
242
SOCKET_ERR(ret,"setsockopt bindtodevice");
244
ret = setsockopt (tcp_listen_sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
245
SOCKET_ERR(ret,"setsockopt reuseaddr");
247
err = bind (tcp_listen_sd, reinterpret_cast<const sockaddr *> (& sa_serv),
249
SOCKET_ERR (err, "bind");
251
err = listen (tcp_listen_sd, 1024);
252
SOCKET_ERR (err, "listen");
254
//TCP sockets creation done
257
FD_SET(udp_listen_sd, &rfds_orig);
258
FD_SET(tcp_listen_sd, &rfds_orig);
260
printf ("Server ready. Listening to port '%d' on UDP and TCP.\n\n", PORT);
263
fd_set rfds = rfds_orig;
265
ret = select(std::max(udp_listen_sd, tcp_listen_sd)+1, &rfds, 0, 0, 0);
266
SOCKET_ERR(ret,"select");
268
if (FD_ISSET(udp_listen_sd, &rfds)){
269
udpreply(udp_listen_sd);
272
if (FD_ISSET(tcp_listen_sd, &rfds)){
274
client_len = sizeof(sa_cli);
276
int sd = accept (tcp_listen_sd,
277
reinterpret_cast<struct sockaddr *> (& sa_cli),
279
SOCKET_ERR(sd,"accept"); //xxx not dieing when just connection abort
282
tcpreply(sd, sa_cli, session);
287
close(tcp_listen_sd);
288
close(udp_listen_sd);
297
close(tcp_listen_sd);
298
close(udp_listen_sd);