2 sptps_test.c -- Simple Peer-to-Peer Security test program
3 Copyright (C) 2011-2022 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>
41 #define closesocket(s) close(s)
44 // Symbols necessary to link with logger.o
45 bool send_request(struct connection_t *c, const char *msg, ...) {
51 list_t connection_list;
53 bool send_meta(struct connection_t *c, const void *msg, size_t len) {
60 bool do_detach = false;
66 static bool writeonly;
69 int addressfamily = AF_UNSPEC;
71 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
73 char hex[len * 2 + 1];
74 bin2hex(data, hex, len);
77 fprintf(stderr, "Sending %lu bytes of data:\n%s\n", (unsigned long)len, hex);
80 const int *sock = handle;
84 ssize_t sent = send(*sock, p, len, 0);
87 fprintf(stderr, "Error sending data: %s\n", strerror(errno));
98 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
102 fprintf(stderr, "Received type %d record of %u bytes:\n", type, len);
109 const char *p = data;
112 ssize_t written = write(out, p, len);
115 fprintf(stderr, "Error writing received data: %s\n", strerror(errno));
126 static struct option const long_options[] = {
127 {"datagram", no_argument, NULL, 'd'},
128 {"quit", no_argument, NULL, 'q'},
129 {"readonly", no_argument, NULL, 'r'},
130 {"writeonly", no_argument, NULL, 'w'},
131 {"packet-loss", required_argument, NULL, 'L'},
132 {"replay-window", required_argument, NULL, 'W'},
133 {"special", no_argument, NULL, 's'},
134 {"verbose", required_argument, NULL, 'v'},
135 {"help", no_argument, NULL, 1},
139 static void usage(void) {
140 static const char *message =
141 "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n"
143 "Valid options are:\n"
144 " -d, --datagram Enable datagram mode.\n"
145 " -q, --quit Quit when EOF occurs on stdin.\n"
146 " -r, --readonly Only send data from the socket to stdout.\n"
148 " -t, --tun Use a tun device instead of stdio.\n"
150 " -w, --writeonly Only send data from stdin to the socket.\n"
151 " -L, --packet-loss RATE Fake packet loss of RATE percent.\n"
152 " -R, --replay-window N Set replay window to N bytes.\n"
153 " -s, --special Enable special handling of lines starting with #, ^ and $.\n"
154 " -v, --verbose Display debug messages.\n"
158 "Report bugs to tinc@tinc-vpn.org.\n";
160 fprintf(stderr, message, program_name);
165 int stdin_sock_fd = -1;
167 // Windows does not allow calling select() on anything but sockets. Therefore,
168 // to keep the same code as on other operating systems, we have to put a
169 // separate thread between the stdin and the sptps loop way below. This thread
170 // reads stdin and sends its content to the main thread through a TCP socket,
171 // which can be properly select()'ed.
172 static void *stdin_reader_thread(void *arg) {
173 struct sockaddr_in sa;
174 socklen_t sa_size = sizeof(sa);
177 int peer_fd = accept(stdin_sock_fd, (struct sockaddr *) &sa, &sa_size);
180 fprintf(stderr, "accept() failed: %s\n", strerror(errno));
185 fprintf(stderr, "New connection received from :%d\n", ntohs(sa.sin_port));
191 while((nread = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
193 fprintf(stderr, "Read %lld bytes from input\n", nread);
197 ssize_t nleft = nread;
200 ssize_t nsend = send(peer_fd, start, nleft, 0);
203 if(sockwouldblock(sockerrno)) {
215 fprintf(stderr, "Could not send data: %s\n", strerror(errno));
220 fprintf(stderr, "Sent %lld bytes to peer\n", nread);
224 closesocket(peer_fd);
227 closesocket(stdin_sock_fd);
232 static int start_input_reader(void) {
233 if(stdin_sock_fd != -1) {
234 fprintf(stderr, "stdin thread can only be started once.\n");
238 stdin_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
240 if(stdin_sock_fd < 0) {
241 fprintf(stderr, "Could not create server socket: %s\n", strerror(errno));
245 struct sockaddr_in serv_sa;
247 memset(&serv_sa, 0, sizeof(serv_sa));
249 serv_sa.sin_family = AF_INET;
251 serv_sa.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1
253 int res = bind(stdin_sock_fd, (struct sockaddr *)&serv_sa, sizeof(serv_sa));
256 fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
260 if(listen(stdin_sock_fd, 1) < 0) {
261 fprintf(stderr, "Could not listen: %s\n", strerror(errno));
265 struct sockaddr_in connect_sa;
267 socklen_t addr_len = sizeof(connect_sa);
269 if(getsockname(stdin_sock_fd, (struct sockaddr *)&connect_sa, &addr_len) < 0) {
270 fprintf(stderr, "Could not determine the address of the stdin thread socket\n");
275 fprintf(stderr, "stdin thread is listening on :%d\n", ntohs(connect_sa.sin_port));
279 int err = pthread_create(&th, NULL, stdin_reader_thread, NULL);
282 fprintf(stderr, "Could not start reader thread: %s\n", strerror(err));
286 int client_fd = socket(AF_INET, SOCK_STREAM, 0);
289 fprintf(stderr, "Could not create client socket: %s\n", strerror(errno));
293 if(connect(client_fd, (struct sockaddr *)&connect_sa, sizeof(connect_sa)) < 0) {
294 fprintf(stderr, "Could not connect: %s\n", strerror(errno));
295 closesocket(client_fd);
303 if(stdin_sock_fd != -1) {
304 closesocket(stdin_sock_fd);
313 int main(int argc, char *argv[]) {
314 program_name = argv[0];
315 bool initiator = false;
316 bool datagram = false;
322 int option_index = 0;
325 while((r = getopt_long(argc, argv, "dqrstwL:W:v46", long_options, &option_index)) != EOF) {
327 case 0: /* long option */
330 case 'd': /* datagram mode */
334 case 'q': /* close connection on EOF from stdin */
338 case 'r': /* read only */
342 case 't': /* read only */
346 fprintf(stderr, "--tun is only supported on Linux.\n");
352 case 'w': /* write only */
356 case 'L': /* packet loss rate */
357 packetloss = atoi(optarg);
360 case 'W': /* replay window size */
361 sptps_replaywin = atoi(optarg);
364 case 'v': /* be verbose */
368 case 's': /* special character handling */
372 case '?': /* wrong options */
377 addressfamily = AF_INET;
381 addressfamily = AF_INET6;
396 if(argc < 4 || argc > 5) {
397 fprintf(stderr, "Wrong number of arguments.\n");
409 in = out = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
412 fprintf(stderr, "Could not open tun device: %s\n", strerror(errno));
420 if(ioctl(in, TUNSETIFF, &ifr)) {
421 fprintf(stderr, "Could not configure tun interface: %s\n", strerror(errno));
425 ifr.ifr_name[IFNAMSIZ - 1] = 0;
426 fprintf(stderr, "Using tun interface %s\n", ifr.ifr_name);
432 static struct WSAData wsa_state;
434 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
440 struct addrinfo *ai, hint;
441 memset(&hint, 0, sizeof(hint));
443 hint.ai_family = addressfamily;
444 hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
445 hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
446 hint.ai_flags = initiator ? 0 : AI_PASSIVE;
448 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
449 fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno));
453 int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
456 fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
462 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
465 int res = connect(sock, ai->ai_addr, ai->ai_addrlen);
471 fprintf(stderr, "Could not connect to peer: %s\n", sockstrerror(sockerrno));
475 fprintf(stderr, "Connected\n");
477 int res = bind(sock, ai->ai_addr, ai->ai_addrlen);
483 fprintf(stderr, "Could not bind socket: %s\n", sockstrerror(sockerrno));
488 if(listen(sock, 1)) {
489 fprintf(stderr, "Could not listen on socket: %s\n", sockstrerror(sockerrno));
493 fprintf(stderr, "Listening...\n");
495 sock = accept(sock, NULL, NULL);
498 fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno));
502 fprintf(stderr, "Listening...\n");
505 struct sockaddr addr;
506 socklen_t addrlen = sizeof(addr);
508 if(recvfrom(sock, buf, sizeof(buf), MSG_PEEK, &addr, &addrlen) <= 0) {
509 fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
513 if(connect(sock, &addr, addrlen)) {
514 fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno));
519 fprintf(stderr, "Connected\n");
525 FILE *fp = fopen(argv[1], "r");
528 fprintf(stderr, "Could not open %s: %s\n", argv[1], strerror(errno));
532 ecdsa_t *mykey = NULL;
534 if(!(mykey = ecdsa_read_pem_private_key(fp))) {
540 fp = fopen(argv[2], "r");
543 fprintf(stderr, "Could not open %s: %s\n", argv[2], strerror(errno));
548 ecdsa_t *hiskey = NULL;
550 if(!(hiskey = ecdsa_read_pem_public_key(fp))) {
558 fprintf(stderr, "Keys loaded\n");
563 if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record)) {
572 in = start_input_reader();
575 fprintf(stderr, "Could not init stdin reader thread\n");
584 int max_fd = MAX(sock, in);
587 if(writeonly && readonly) {
591 char buf[65535] = "";
592 size_t readsize = datagram ? 1460u : sizeof(buf);
597 if(!readonly && s.instate) {
603 if(select(max_fd + 1, &fds, NULL, NULL, NULL) <= 0) {
609 if(FD_ISSET(in, &fds)) {
611 ssize_t len = recv(in, buf, readsize, 0);
613 ssize_t len = read(in, buf, readsize);
617 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
625 shutdown(in, SD_SEND);
637 if(special && buf[0] == '#') {
638 s.outseqno = atoi(buf + 1);
641 if(special && buf[0] == '^') {
642 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
643 } else if(special && buf[0] == '$') {
647 sptps_send_record(&s, 0, buf, len);
649 } else if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof(buf) : (size_t)len)) {
656 if(FD_ISSET(sock, &fds)) {
657 ssize_t len = recv(sock, buf, sizeof(buf), 0);
660 fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
667 fprintf(stderr, "Connection terminated by peer.\n");
672 char hex[len * 2 + 1];
673 bin2hex(buf, hex, len);
674 fprintf(stderr, "Received %ld bytes of data:\n%s\n", (long)len, hex);
677 if(packetloss && (int)prng(100) < packetloss) {
679 fprintf(stderr, "Dropped.\n");
688 size_t done = sptps_receive_data(&s, bufp, len);
699 len -= (ssize_t) done;
704 bool stopped = sptps_stop(&s);