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>
36 #define closesocket(s) close(s)
39 // Symbols necessary to link with logger.o
40 bool send_request(struct connection_t *c, const char *msg, ...) {
46 list_t connection_list;
48 bool send_meta(struct connection_t *c, const void *msg, size_t len) {
55 bool do_detach = false;
61 static bool writeonly;
64 int addressfamily = AF_UNSPEC;
66 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
68 char *hex = alloca(len * 2 + 1);
69 bin2hex(data, hex, len);
72 fprintf(stderr, "Sending %lu bytes of data:\n%s\n", (unsigned long)len, hex);
75 const int *sock = handle;
79 ssize_t sent = send(*sock, p, len, 0);
82 fprintf(stderr, "Error sending data: %s\n", strerror(errno));
93 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
97 fprintf(stderr, "Received type %d record of %u bytes:\n", type, len);
104 const char *p = data;
107 ssize_t written = write(out, p, len);
110 fprintf(stderr, "Error writing received data: %s\n", strerror(errno));
121 static struct option const long_options[] = {
122 {"datagram", no_argument, NULL, 'd'},
123 {"quit", no_argument, NULL, 'q'},
124 {"readonly", no_argument, NULL, 'r'},
125 {"writeonly", no_argument, NULL, 'w'},
126 {"packet-loss", required_argument, NULL, 'L'},
127 {"replay-window", required_argument, NULL, 'W'},
128 {"special", no_argument, NULL, 's'},
129 {"verbose", required_argument, NULL, 'v'},
130 {"help", no_argument, NULL, 1},
134 static void usage(void) {
135 static const char *message =
136 "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n"
138 "Valid options are:\n"
139 " -d, --datagram Enable datagram mode.\n"
140 " -q, --quit Quit when EOF occurs on stdin.\n"
141 " -r, --readonly Only send data from the socket to stdout.\n"
143 " -t, --tun Use a tun device instead of stdio.\n"
145 " -w, --writeonly Only send data from stdin to the socket.\n"
146 " -L, --packet-loss RATE Fake packet loss of RATE percent.\n"
147 " -R, --replay-window N Set replay window to N bytes.\n"
148 " -s, --special Enable special handling of lines starting with #, ^ and $.\n"
149 " -v, --verbose Display debug messages.\n"
153 "Report bugs to tinc@tinc-vpn.org.\n";
155 fprintf(stderr, message, program_name);
160 int stdin_sock_fd = -1;
162 // Windows does not allow calling select() on anything but sockets. Therefore,
163 // to keep the same code as on other operating systems, we have to put a
164 // separate thread between the stdin and the sptps loop way below. This thread
165 // reads stdin and sends its content to the main thread through a TCP socket,
166 // which can be properly select()'ed.
167 static DWORD WINAPI stdin_reader_thread(LPVOID arg) {
168 struct sockaddr_in sa;
169 socklen_t sa_size = sizeof(sa);
172 int peer_fd = accept(stdin_sock_fd, (struct sockaddr *) &sa, &sa_size);
175 fprintf(stderr, "accept() failed: %s\n", strerror(errno));
180 fprintf(stderr, "New connection received from :%d\n", ntohs(sa.sin_port));
186 while((nread = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
188 fprintf(stderr, "Read %lld bytes from input\n", nread);
192 ssize_t nleft = nread;
195 ssize_t nsend = send(peer_fd, start, nleft, 0);
198 if(sockwouldblock(sockerrno)) {
210 fprintf(stderr, "Could not send data: %s\n", strerror(errno));
215 fprintf(stderr, "Sent %lld bytes to peer\n", nread);
219 closesocket(peer_fd);
222 closesocket(stdin_sock_fd);
227 static int start_input_reader(void) {
228 if(stdin_sock_fd != -1) {
229 fprintf(stderr, "stdin thread can only be started once.\n");
233 stdin_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
235 if(stdin_sock_fd < 0) {
236 fprintf(stderr, "Could not create server socket: %s\n", strerror(errno));
240 struct sockaddr_in serv_sa;
242 memset(&serv_sa, 0, sizeof(serv_sa));
244 serv_sa.sin_family = AF_INET;
246 serv_sa.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1
248 int res = bind(stdin_sock_fd, (struct sockaddr *)&serv_sa, sizeof(serv_sa));
251 fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
255 if(listen(stdin_sock_fd, 1) < 0) {
256 fprintf(stderr, "Could not listen: %s\n", strerror(errno));
260 struct sockaddr_in connect_sa;
262 socklen_t addr_len = sizeof(connect_sa);
264 if(getsockname(stdin_sock_fd, (struct sockaddr *)&connect_sa, &addr_len) < 0) {
265 fprintf(stderr, "Could not determine the address of the stdin thread socket\n");
270 fprintf(stderr, "stdin thread is listening on :%d\n", ntohs(connect_sa.sin_port));
273 if(!CreateThread(NULL, 0, stdin_reader_thread, NULL, 0, NULL)) {
274 fprintf(stderr, "Could not start reader thread: %d\n", GetLastError());
278 int client_fd = socket(AF_INET, SOCK_STREAM, 0);
281 fprintf(stderr, "Could not create client socket: %s\n", strerror(errno));
285 if(connect(client_fd, (struct sockaddr *)&connect_sa, sizeof(connect_sa)) < 0) {
286 fprintf(stderr, "Could not connect: %s\n", strerror(errno));
287 closesocket(client_fd);
295 if(stdin_sock_fd != -1) {
296 closesocket(stdin_sock_fd);
303 #endif // HAVE_WINDOWS
305 static void print_listening_msg(int sock) {
307 socklen_t salen = sizeof(sa);
310 if(!getsockname(sock, &sa.sa, &salen)) {
311 port = ntohs(sa.in.sin_port);
314 fprintf(stderr, "Listening on %d...\n", port);
318 static int run_test(int argc, char *argv[]) {
319 program_name = argv[0];
320 bool initiator = false;
321 bool datagram = false;
327 int option_index = 0;
330 while((r = getopt_long(argc, argv, "dqrstwL:W:v46", long_options, &option_index)) != EOF) {
332 case 0: /* long option */
335 case 'd': /* datagram mode */
339 case 'q': /* close connection on EOF from stdin */
343 case 'r': /* read only */
347 case 't': /* read only */
351 fprintf(stderr, "--tun is only supported on Linux.\n");
357 case 'w': /* write only */
361 case 'L': /* packet loss rate */
362 packetloss = atoi(optarg);
365 case 'W': /* replay window size */
366 sptps_replaywin = atoi(optarg);
369 case 'v': /* be verbose */
373 case 's': /* special character handling */
377 case '?': /* wrong options */
382 addressfamily = AF_INET;
386 addressfamily = AF_INET6;
401 if(argc < 4 || argc > 5) {
402 fprintf(stderr, "Wrong number of arguments.\n");
414 in = out = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
417 fprintf(stderr, "Could not open tun device: %s\n", strerror(errno));
425 if(ioctl(in, TUNSETIFF, &ifr)) {
426 fprintf(stderr, "Could not configure tun interface: %s\n", strerror(errno));
430 ifr.ifr_name[IFNAMSIZ - 1] = 0;
431 fprintf(stderr, "Using tun interface %s\n", ifr.ifr_name);
437 static struct WSAData wsa_state;
439 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
445 struct addrinfo *ai, hint;
446 memset(&hint, 0, sizeof(hint));
448 hint.ai_family = addressfamily;
449 hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
450 hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
451 hint.ai_flags = initiator ? 0 : AI_PASSIVE;
453 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
454 fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno));
458 int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
461 fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
467 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
470 int res = connect(sock, ai->ai_addr, ai->ai_addrlen);
476 fprintf(stderr, "Could not connect to peer: %s\n", sockstrerror(sockerrno));
480 fprintf(stderr, "Connected\n");
482 int res = bind(sock, ai->ai_addr, ai->ai_addrlen);
488 fprintf(stderr, "Could not bind socket: %s\n", sockstrerror(sockerrno));
493 if(listen(sock, 1)) {
494 fprintf(stderr, "Could not listen on socket: %s\n", sockstrerror(sockerrno));
498 print_listening_msg(sock);
500 sock = accept(sock, NULL, NULL);
503 fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno));
507 print_listening_msg(sock);
510 struct sockaddr addr;
511 socklen_t addrlen = sizeof(addr);
513 if(recvfrom(sock, buf, sizeof(buf), MSG_PEEK, &addr, &addrlen) <= 0) {
514 fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
518 if(connect(sock, &addr, addrlen)) {
519 fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno));
524 fprintf(stderr, "Connected\n");
527 FILE *fp = fopen(argv[1], "r");
530 fprintf(stderr, "Could not open %s: %s\n", argv[1], strerror(errno));
534 ecdsa_t *mykey = NULL;
536 if(!(mykey = ecdsa_read_pem_private_key(fp))) {
542 fp = fopen(argv[2], "r");
545 fprintf(stderr, "Could not open %s: %s\n", argv[2], strerror(errno));
550 ecdsa_t *hiskey = NULL;
552 if(!(hiskey = ecdsa_read_pem_public_key(fp))) {
560 fprintf(stderr, "Keys loaded\n");
565 if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record)) {
574 in = start_input_reader();
577 fprintf(stderr, "Could not init stdin reader thread\n");
586 int max_fd = MAX(sock, in);
589 if(writeonly && readonly) {
593 char buf[65535] = "";
594 size_t readsize = datagram ? 1460u : sizeof(buf);
599 if(!readonly && s.instate) {
605 if(select(max_fd + 1, &fds, NULL, NULL, NULL) <= 0) {
611 if(FD_ISSET(in, &fds)) {
613 ssize_t len = recv(in, buf, readsize, 0);
615 ssize_t len = read(in, buf, readsize);
619 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
627 shutdown(in, SD_SEND);
639 if(special && buf[0] == '#') {
640 s.outseqno = atoi(buf + 1);
643 if(special && buf[0] == '^') {
644 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
645 } else if(special && buf[0] == '$') {
649 sptps_send_record(&s, 0, buf, len);
651 } else if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof(buf) : (size_t)len)) {
658 if(FD_ISSET(sock, &fds)) {
659 ssize_t len = recv(sock, buf, sizeof(buf), 0);
662 fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
669 fprintf(stderr, "Connection terminated by peer.\n");
674 char *hex = alloca(len * 2 + 1);
675 bin2hex(buf, hex, len);
676 fprintf(stderr, "Received %ld bytes of data:\n%s\n", (long)len, hex);
679 if(packetloss && (int)prng(100) < packetloss) {
681 fprintf(stderr, "Dropped.\n");
690 size_t done = sptps_receive_data(&s, bufp, len);
701 len -= (ssize_t) done;
706 bool stopped = sptps_stop(&s);
715 int main(int argc, char *argv[]) {
720 int result = run_test(argc, argv);