2
#include <sys/types.h> // getaddrinfo, gai_strerror, socket, inet_pton
4
#include <sys/socket.h> // getaddrinfo, gai_strerror, socket, inet_pton
6
#include <unistd.h> // close
7
#include <netdb.h> // getaddrinfo, gai_strerror
8
#include <arpa/inet.h> // inet_pton
9
#include <sys/select.h> // select
10
#include <gnutls/gnutls.h>
13
#include <cstdio> // fprintf
14
#include <cerrno> // perror
15
#include <cstring> // memset
17
#define SOCKET_ERR(err,s) if(err<0) {perror(s);return(1);}
19
#define CERTFILE "client-cert.pem"
20
#define KEYFILE "client-key.pem"
21
#define CAFILE "ca.pem"
23
gnutls_certificate_credentials_t x509_cred;
27
gnutls_session_t session;
29
gnutls_global_init ();
32
gnutls_certificate_allocate_credentials (&x509_cred);
33
gnutls_certificate_set_x509_trust_file (x509_cred, CAFILE, GNUTLS_X509_FMT_PEM);
34
gnutls_certificate_set_x509_key_file (x509_cred, CERTFILE, KEYFILE,
38
gnutls_init (&session, GNUTLS_CLIENT);
39
gnutls_set_default_priority (session);
40
gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
48
struct sockaddr_in6 to;
49
struct sockaddr_in6 from;
50
gnutls_session_t session;
52
struct timeval timeout;
54
session = initgnutls ();
56
sd = socket(PF_INET6, SOCK_DGRAM, 0);
57
SOCKET_ERR(sd,"socket");
61
ret = setsockopt(sd, SOL_SOCKET, SO_BROADCAST, & flag, sizeof(flag));
62
SOCKET_ERR(ret,"setsockopt broadcast");
65
setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
66
SOCKET_ERR(ret,"setsockopt bindtodevice");
68
memset (&to, '\0', sizeof (to));
69
to.sin6_family = AF_INET6;
70
ret = inet_pton(AF_INET6, "ff02::1" , &to.sin6_addr);
71
SOCKET_ERR(ret,"setsockopt bindtodevice");
72
to.sin6_port = htons (PORT); // Server Port number
75
FD_SET(sd, &rfds_orig);
82
sendto(sd, "Marco", 5, 0, reinterpret_cast<const sockaddr*>(&to), sizeof(to));
84
fd_set rfds = rfds_orig;
86
ret = select(sd+1, &rfds, 0, 0, & timeout);
87
SOCKET_ERR(sd,"select");
90
socklen_t from_len = sizeof(from);
91
ret = recvfrom(sd,buffer,512,0, reinterpret_cast<sockaddr *>(& from),
93
SOCKET_ERR(ret,"recv");
95
if (strncmp(buffer,"Polo", 4) == 0){
106
sd = socket(PF_INET6, SOCK_STREAM, 0);
107
SOCKET_ERR(sd,"socket");
109
setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
110
SOCKET_ERR(ret,"setsockopt bindtodevice");
112
memset(&to,0,sizeof(to));
113
to.sin6_family = from.sin6_family;
114
to.sin6_port = from.sin6_port;
115
to.sin6_addr = from.sin6_addr;
116
to.sin6_scope_id = from.sin6_scope_id;
118
ret = connect(sd,reinterpret_cast<struct sockaddr *>(&to),sizeof(to));
119
SOCKET_ERR(ret,"connect");
121
gnutls_transport_set_ptr (session, reinterpret_cast<gnutls_transport_ptr_t> (sd));
123
ret = gnutls_handshake (session);
127
fprintf (stderr, "*** Handshake failed\n");
133
ret = gnutls_record_recv (session, buffer, sizeof(buffer));
138
gnutls_bye (session, GNUTLS_SHUT_RDWR);
140
gnutls_deinit (session);
141
gnutls_certificate_free_credentials (x509_cred);
142
gnutls_global_deinit ();