2 sptps_test.c -- Simple Peer-to-Peer Security test program
3 Copyright (C) 2011-2014 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <linux/if_tun.h>
38 #define MAX(a, b) ((a) > (b) ? (a) : (b))
42 #define closesocket(s) close(s)
45 // Symbols necessary to link with logger.o
46 bool send_request(void *c, const char *msg, ...) {
52 struct list_t *connection_list = NULL;
54 bool send_meta(void *c, const char *msg, int len) {
61 char *logfilename = NULL;
62 bool do_detach = false;
68 static bool writeonly;
71 static int addressfamily = AF_UNSPEC;
73 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
75 char hex[len * 2 + 1];
76 bin2hex(data, hex, len);
79 fprintf(stderr, "Sending %d bytes of data:\n%s\n", (int)len, hex);
82 const int *sock = handle;
84 if((size_t)send(*sock, data, len, 0) != len) {
91 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
95 fprintf(stderr, "Received type %d record of %u bytes:\n", type, len);
99 write(out, data, len);
105 static struct option const long_options[] = {
106 {"datagram", no_argument, NULL, 'd'},
107 {"quit", no_argument, NULL, 'q'},
108 {"readonly", no_argument, NULL, 'r'},
109 {"writeonly", no_argument, NULL, 'w'},
110 {"packet-loss", required_argument, NULL, 'L'},
111 {"replay-window", required_argument, NULL, 'W'},
112 {"special", no_argument, NULL, 's'},
113 {"verbose", required_argument, NULL, 'v'},
114 {"help", no_argument, NULL, 1},
118 const char *program_name;
120 static void usage() {
121 fprintf(stderr, "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n\n", program_name);
122 fprintf(stderr, "Valid options are:\n"
123 " -d, --datagram Enable datagram mode.\n"
124 " -q, --quit Quit when EOF occurs on stdin.\n"
125 " -r, --readonly Only send data from the socket to stdout.\n"
127 " -t, --tun Use a tun device instead of stdio.\n"
129 " -w, --writeonly Only send data from stdin to the socket.\n"
130 " -L, --packet-loss RATE Fake packet loss of RATE percent.\n"
131 " -R, --replay-window N Set replay window to N bytes.\n"
132 " -s, --special Enable special handling of lines starting with #, ^ and $.\n"
133 " -v, --verbose Display debug messages.\n"
137 fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
142 int stdin_sock_fd = -1;
144 // Windows does not allow calling select() on anything but sockets. Therefore,
145 // to keep the same code as on other operating systems, we have to put a
146 // separate thread between the stdin and the sptps loop way below. This thread
147 // reads stdin and sends its content to the main thread through a TCP socket,
148 // which can be properly select()'ed.
149 void *stdin_reader_thread(void *arg) {
150 struct sockaddr_in sa;
151 socklen_t sa_size = sizeof(sa);
154 int peer_fd = accept(stdin_sock_fd, (struct sockaddr *) &sa, &sa_size);
157 fprintf(stderr, "accept() failed: %s\n", strerror(errno));
162 fprintf(stderr, "New connection received from :%d\n", ntohs(sa.sin_port));
168 while((nread = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
170 fprintf(stderr, "Read %lld bytes from input\n", nread);
174 ssize_t nleft = nread;
177 ssize_t nsend = send(peer_fd, start, nleft, 0);
180 if(sockwouldblock(sockerrno)) {
192 fprintf(stderr, "Could not send data: %s\n", strerror(errno));
197 fprintf(stderr, "Sent %lld bytes to peer\n", nread);
201 closesocket(peer_fd);
204 closesocket(stdin_sock_fd);
208 int start_input_reader() {
209 if(stdin_sock_fd != -1) {
210 fprintf(stderr, "stdin thread can only be started once.\n");
214 stdin_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
216 if(stdin_sock_fd < 0) {
217 fprintf(stderr, "Could not create server socket: %s\n", strerror(errno));
221 struct sockaddr_in serv_sa;
223 memset(&serv_sa, 0, sizeof(serv_sa));
225 serv_sa.sin_family = AF_INET;
227 serv_sa.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1
229 int res = bind(stdin_sock_fd, (struct sockaddr *)&serv_sa, sizeof(serv_sa));
232 fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
236 if(listen(stdin_sock_fd, 1) < 0) {
237 fprintf(stderr, "Could not listen: %s\n", strerror(errno));
241 struct sockaddr_in connect_sa;
243 socklen_t addr_len = sizeof(connect_sa);
245 if(getsockname(stdin_sock_fd, (struct sockaddr *)&connect_sa, &addr_len) < 0) {
246 fprintf(stderr, "Could not determine the address of the stdin thread socket\n");
251 fprintf(stderr, "stdin thread is listening on :%d\n", ntohs(connect_sa.sin_port));
255 int err = pthread_create(&th, NULL, stdin_reader_thread, NULL);
258 fprintf(stderr, "Could not start reader thread: %s\n", strerror(err));
262 int client_fd = socket(AF_INET, SOCK_STREAM, 0);
265 fprintf(stderr, "Could not create client socket: %s\n", strerror(errno));
269 if(connect(client_fd, (struct sockaddr *)&connect_sa, sizeof(connect_sa)) < 0) {
270 fprintf(stderr, "Could not connect: %s\n", strerror(errno));
271 closesocket(client_fd);
279 if(stdin_sock_fd != -1) {
280 closesocket(stdin_sock_fd);
289 int main(int argc, char *argv[]) {
290 program_name = argv[0];
291 bool initiator = false;
292 bool datagram = false;
298 int option_index = 0;
299 ecdsa_t *mykey = NULL, *hiskey = NULL;
302 while((r = getopt_long(argc, argv, "dqrstwL:W:v46", long_options, &option_index)) != EOF) {
304 case 0: /* long option */
307 case 'd': /* datagram mode */
311 case 'q': /* close connection on EOF from stdin */
315 case 'r': /* read only */
319 case 't': /* read only */
323 fprintf(stderr, "--tun is only supported on Linux.\n");
329 case 'w': /* write only */
333 case 'L': /* packet loss rate */
334 packetloss = atoi(optarg);
337 case 'W': /* replay window size */
338 sptps_replaywin = atoi(optarg);
341 case 'v': /* be verbose */
345 case 's': /* special character handling */
349 case '?': /* wrong options */
354 addressfamily = AF_INET;
358 addressfamily = AF_INET6;
373 if(argc < 4 || argc > 5) {
374 fprintf(stderr, "Wrong number of arguments.\n");
388 in = out = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
391 fprintf(stderr, "Could not open tun device: %s\n", strerror(errno));
399 if(ioctl(in, TUNSETIFF, &ifr)) {
400 fprintf(stderr, "Could not configure tun interface: %s\n", strerror(errno));
404 ifr.ifr_name[IFNAMSIZ - 1] = 0;
405 fprintf(stderr, "Using tun interface %s\n", ifr.ifr_name);
411 static struct WSAData wsa_state;
413 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
419 struct addrinfo *ai, hint;
420 memset(&hint, 0, sizeof(hint));
422 hint.ai_family = addressfamily;
423 hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
424 hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
425 hint.ai_flags = initiator ? 0 : AI_PASSIVE;
427 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
428 fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno));
432 int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
435 fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
440 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
443 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
444 fprintf(stderr, "Could not connect to peer: %s\n", sockstrerror(sockerrno));
448 fprintf(stderr, "Connected\n");
450 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
451 fprintf(stderr, "Could not bind socket: %s\n", sockstrerror(sockerrno));
456 if(listen(sock, 1)) {
457 fprintf(stderr, "Could not listen on socket: %s\n", sockstrerror(sockerrno));
461 fprintf(stderr, "Listening...\n");
463 sock = accept(sock, NULL, NULL);
466 fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno));
470 fprintf(stderr, "Listening...\n");
473 struct sockaddr addr;
474 socklen_t addrlen = sizeof(addr);
476 if(recvfrom(sock, buf, sizeof(buf), MSG_PEEK, &addr, &addrlen) <= 0) {
477 fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
481 if(connect(sock, &addr, addrlen)) {
482 fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno));
487 fprintf(stderr, "Connected\n");
492 FILE *fp = fopen(argv[1], "r");
495 fprintf(stderr, "Could not open %s: %s\n", argv[1], strerror(errno));
499 if(!(mykey = ecdsa_read_pem_private_key(fp))) {
505 fp = fopen(argv[2], "r");
508 fprintf(stderr, "Could not open %s: %s\n", argv[2], strerror(errno));
512 if(!(hiskey = ecdsa_read_pem_public_key(fp))) {
519 fprintf(stderr, "Keys loaded\n");
524 if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record)) {
531 in = start_input_reader();
534 fprintf(stderr, "Could not init stdin reader thread\n");
541 int max_fd = MAX(sock, in);
544 if(writeonly && readonly) {
548 char buf[65535] = "";
549 size_t readsize = datagram ? 1460u : sizeof(buf);
554 if(!readonly && s.instate) {
560 if(select(max_fd + 1, &fds, NULL, NULL, NULL) <= 0) {
564 if(FD_ISSET(in, &fds)) {
566 ssize_t len = recv(in, buf, readsize, 0);
568 ssize_t len = read(in, buf, readsize);
572 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
578 shutdown(in, SD_SEND);
590 if(special && buf[0] == '#') {
591 s.outseqno = atoi(buf + 1);
594 if(special && buf[0] == '^') {
595 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
596 } else if(special && buf[0] == '$') {
600 sptps_send_record(&s, 0, buf, len);
602 } else if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof(buf) : (size_t)len)) {
607 if(FD_ISSET(sock, &fds)) {
608 ssize_t len = recv(sock, buf, sizeof(buf), 0);
611 fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
616 fprintf(stderr, "Connection terminated by peer.\n");
621 char hex[len * 2 + 1];
622 bin2hex(buf, hex, len);
623 fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex);
626 if(packetloss && (rand() % 100) < packetloss) {
628 fprintf(stderr, "Dropped.\n");
637 size_t done = sptps_receive_data(&s, bufp, len);
651 if(!sptps_stop(&s)) {