4
4
#include <sys/socket.h> // getaddrinfo, gai_strerror, socket, inet_pton
6
#include <unistd.h> // close
6
#include <unistd.h> // close, STDIN_FILENO, STDOUT_FILENO
7
7
#include <netdb.h> // getaddrinfo, gai_strerror
8
8
#include <arpa/inet.h> // inet_pton
9
9
#include <sys/select.h> // select
10
10
#include <gnutls/gnutls.h>
11
#include <sys/ioctl.h> // ioctl, ifreq, SIOCGIFFLAGS, IFF_UP, SIOCSIFFLAGS
12
#include <net/if.h> // ioctl, ifreq, SIOCGIFFLAGS, IFF_UP, SIOCSIFFLAGS
13
#include <termios.h> // struct termios, tcsetattr, tcgetattr, TCSAFLUSH, ECHO
13
#include <cstdio> // fprintf
14
16
#include <cerrno> // perror
15
17
#include <cstring> // memset
18
#include <string> // std::string, std::getline
19
#include <iostream> // cin, cout, cerr
20
#include <ostream> // <<
17
#define SOCKET_ERR(err,s) if(err<0) {perror(s);return(1);}
22
#define SOCKET_ERR(err,s) if(err<0) {perror(s); status = 1; goto quit;}
19
#define CERTFILE "client-cert.pem"
20
#define KEYFILE "client-key.pem"
21
#define CAFILE "ca.pem"
24
#define CERTFILE "/conf/conf.d/cryptkeyreq/client-cert.pem"
25
#define KEYFILE "/conf/conf.d/cryptkeyreq/client-key.pem"
26
#define CAFILE "/conf/conf.d/cryptkeyreq/ca.pem"
23
28
gnutls_certificate_credentials_t x509_cred;
52
61
struct timeval timeout;
63
struct termios t_old, t_new;
54
66
session = initgnutls ();
56
sd = socket(PF_INET6, SOCK_DGRAM, 0);
57
SOCKET_ERR(sd,"socket");
69
std::cerr << "Open ipv6 UDP\n";
72
udp_sd = socket(PF_INET6, SOCK_DGRAM, 0);
73
SOCKET_ERR(udp_sd,"socket");
76
std::cerr << "Open socket with socket nr: " << udp_sd << '\n';
61
ret = setsockopt(sd, SOL_SOCKET, SO_BROADCAST, & flag, sizeof(flag));
81
ret = setsockopt(udp_sd, SOL_SOCKET, SO_BROADCAST, & flag, sizeof(flag));
62
82
SOCKET_ERR(ret,"setsockopt broadcast");
65
setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
85
ret = setsockopt(udp_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
66
86
SOCKET_ERR(ret,"setsockopt bindtodevice");
68
88
memset (&to, '\0', sizeof (to));
69
89
to.sin6_family = AF_INET6;
70
90
ret = inet_pton(AF_INET6, "ff02::1" , &to.sin6_addr);
71
SOCKET_ERR(ret,"setsockopt bindtodevice");
91
SOCKET_ERR(ret,"inet_pton");
72
92
to.sin6_port = htons (PORT); // Server Port number
96
strcpy(network.ifr_name, "eth0");
98
ret = ioctl(udp_sd, SIOCGIFFLAGS, &network);
99
SOCKET_ERR(ret,"ioctl SIOCGIFFLAGS");
101
network.ifr_flags |= IFF_UP;
103
ret = ioctl(udp_sd, SIOCSIFFLAGS, &network);
104
SOCKET_ERR(ret,"ioctl SIOCSIFFLAGS");
74
106
FD_ZERO(&rfds_orig);
75
FD_SET(sd, &rfds_orig);
107
FD_SET(udp_sd, &rfds_orig);
108
FD_SET(STDIN_FILENO, &rfds_orig);
111
if (tcgetattr (STDIN_FILENO, &t_old) != 0){
115
t_new.c_lflag &= ~ECHO;
116
if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
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){
124
std::cerr << "Sending Marco on UDP\n";
126
ret = sendto(udp_sd, "Marco", 5, 0, reinterpret_cast<const sockaddr*>(&to), sizeof(to));
131
fd_set rfds = rfds_orig;
135
std::cerr << "Password: ";
137
ret = select(udp_sd+1, &rfds, 0, 0, & timeout);
138
SOCKET_ERR(udp_sd,"select");
141
if (FD_ISSET(STDIN_FILENO, &rfds)){
143
std::getline(std::cin, buffer);
149
socklen_t from_len = sizeof(from);
150
ret = recvfrom(udp_sd,buffer,512,0, reinterpret_cast<sockaddr *>(& from),
152
SOCKET_ERR(ret,"recv");
154
if (strncmp(buffer,"Polo", 4) == 0){
162
tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
163
SOCKET_ERR(tcp_sd,"socket");
165
setsockopt(tcp_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
166
SOCKET_ERR(ret,"setsockopt bindtodevice");
168
memset(&to,0,sizeof(to));
169
to.sin6_family = from.sin6_family;
170
to.sin6_port = from.sin6_port;
171
to.sin6_addr = from.sin6_addr;
172
to.sin6_scope_id = from.sin6_scope_id;
174
ret = connect(tcp_sd,reinterpret_cast<struct sockaddr *>(&to),sizeof(to));
180
gnutls_transport_set_ptr (session, reinterpret_cast<gnutls_transport_ptr_t> (tcp_sd));
182
ret = gnutls_handshake (session);
186
std::cerr << "\n*** Handshake failed ***\n";
192
ret = gnutls_record_recv (session, buffer, sizeof(buffer));
194
write(STDOUT_FILENO,buffer,ret);
197
gnutls_bye (session, GNUTLS_SHUT_RDWR);
199
gnutls_deinit (session);
200
gnutls_certificate_free_credentials (x509_cred);
201
gnutls_global_deinit ();
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 ();
207
tcsetattr (STDIN_FILENO, TCSAFLUSH, &t_old);