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>
35 #define closesocket(s) close(s)
38 // Symbols necessary to link with logger.o
39 bool send_request(struct connection_t *c, const char *msg, ...) {
45 list_t connection_list;
47 bool send_meta(struct connection_t *c, const void *msg, size_t len) {
54 bool do_detach = false;
60 static bool writeonly;
63 int addressfamily = AF_UNSPEC;
65 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
67 char *hex = alloca(len * 2 + 1);
68 bin2hex(data, hex, len);
71 fprintf(stderr, "Sending %lu bytes of data:\n%s\n", (unsigned long)len, hex);
74 const int *sock = handle;
78 ssize_t sent = send(*sock, p, len, 0);
81 fprintf(stderr, "Error sending data: %s\n", strerror(errno));
92 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
96 fprintf(stderr, "Received type %d record of %u bytes:\n", type, len);
103 const char *p = data;
106 ssize_t written = write(out, p, len);
109 fprintf(stderr, "Error writing received data: %s\n", strerror(errno));
120 static struct option const long_options[] = {
121 {"datagram", no_argument, NULL, 'd'},
122 {"quit", no_argument, NULL, 'q'},
123 {"readonly", no_argument, NULL, 'r'},
124 {"writeonly", no_argument, NULL, 'w'},
125 {"packet-loss", required_argument, NULL, 'L'},
126 {"replay-window", required_argument, NULL, 'W'},
127 {"special", no_argument, NULL, 's'},
128 {"verbose", required_argument, NULL, 'v'},
129 {"help", no_argument, NULL, 1},
133 static void usage(void) {
134 static const char *message =
135 "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n"
137 "Valid options are:\n"
138 " -d, --datagram Enable datagram mode.\n"
139 " -q, --quit Quit when EOF occurs on stdin.\n"
140 " -r, --readonly Only send data from the socket to stdout.\n"
142 " -t, --tun Use a tun device instead of stdio.\n"
144 " -w, --writeonly Only send data from stdin to the socket.\n"
145 " -L, --packet-loss RATE Fake packet loss of RATE percent.\n"
146 " -R, --replay-window N Set replay window to N bytes.\n"
147 " -s, --special Enable special handling of lines starting with #, ^ and $.\n"
148 " -v, --verbose Display debug messages.\n"
152 "Report bugs to tinc@tinc-vpn.org.\n";
154 fprintf(stderr, message, program_name);
159 int stdin_sock_fd = -1;
161 // Windows does not allow calling select() on anything but sockets. Therefore,
162 // to keep the same code as on other operating systems, we have to put a
163 // separate thread between the stdin and the sptps loop way below. This thread
164 // reads stdin and sends its content to the main thread through a TCP socket,
165 // which can be properly select()'ed.
166 static DWORD WINAPI stdin_reader_thread(LPVOID arg) {
167 struct sockaddr_in sa;
168 socklen_t sa_size = sizeof(sa);
171 int peer_fd = accept(stdin_sock_fd, (struct sockaddr *) &sa, &sa_size);
174 fprintf(stderr, "accept() failed: %s\n", strerror(errno));
179 fprintf(stderr, "New connection received from :%d\n", ntohs(sa.sin_port));
185 while((nread = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
187 fprintf(stderr, "Read %lld bytes from input\n", nread);
191 ssize_t nleft = nread;
194 ssize_t nsend = send(peer_fd, start, nleft, 0);
197 if(sockwouldblock(sockerrno)) {
209 fprintf(stderr, "Could not send data: %s\n", strerror(errno));
214 fprintf(stderr, "Sent %lld bytes to peer\n", nread);
218 closesocket(peer_fd);
221 closesocket(stdin_sock_fd);
226 static int start_input_reader(void) {
227 if(stdin_sock_fd != -1) {
228 fprintf(stderr, "stdin thread can only be started once.\n");
232 stdin_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
234 if(stdin_sock_fd < 0) {
235 fprintf(stderr, "Could not create server socket: %s\n", strerror(errno));
239 struct sockaddr_in serv_sa;
241 memset(&serv_sa, 0, sizeof(serv_sa));
243 serv_sa.sin_family = AF_INET;
245 serv_sa.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1
247 int res = bind(stdin_sock_fd, (struct sockaddr *)&serv_sa, sizeof(serv_sa));
250 fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
254 if(listen(stdin_sock_fd, 1) < 0) {
255 fprintf(stderr, "Could not listen: %s\n", strerror(errno));
259 struct sockaddr_in connect_sa;
261 socklen_t addr_len = sizeof(connect_sa);
263 if(getsockname(stdin_sock_fd, (struct sockaddr *)&connect_sa, &addr_len) < 0) {
264 fprintf(stderr, "Could not determine the address of the stdin thread socket\n");
269 fprintf(stderr, "stdin thread is listening on :%d\n", ntohs(connect_sa.sin_port));
272 if(!CreateThread(NULL, 0, stdin_reader_thread, NULL, 0, NULL)) {
273 fprintf(stderr, "Could not start reader thread: %d\n", GetLastError());
277 int client_fd = socket(AF_INET, SOCK_STREAM, 0);
280 fprintf(stderr, "Could not create client socket: %s\n", strerror(errno));
284 if(connect(client_fd, (struct sockaddr *)&connect_sa, sizeof(connect_sa)) < 0) {
285 fprintf(stderr, "Could not connect: %s\n", strerror(errno));
286 closesocket(client_fd);
294 if(stdin_sock_fd != -1) {
295 closesocket(stdin_sock_fd);
302 #endif // HAVE_WINDOWS
304 int main(int argc, char *argv[]) {
305 program_name = argv[0];
306 bool initiator = false;
307 bool datagram = false;
313 int option_index = 0;
316 while((r = getopt_long(argc, argv, "dqrstwL:W:v46", long_options, &option_index)) != EOF) {
318 case 0: /* long option */
321 case 'd': /* datagram mode */
325 case 'q': /* close connection on EOF from stdin */
329 case 'r': /* read only */
333 case 't': /* read only */
337 fprintf(stderr, "--tun is only supported on Linux.\n");
343 case 'w': /* write only */
347 case 'L': /* packet loss rate */
348 packetloss = atoi(optarg);
351 case 'W': /* replay window size */
352 sptps_replaywin = atoi(optarg);
355 case 'v': /* be verbose */
359 case 's': /* special character handling */
363 case '?': /* wrong options */
368 addressfamily = AF_INET;
372 addressfamily = AF_INET6;
387 if(argc < 4 || argc > 5) {
388 fprintf(stderr, "Wrong number of arguments.\n");
400 in = out = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
403 fprintf(stderr, "Could not open tun device: %s\n", strerror(errno));
411 if(ioctl(in, TUNSETIFF, &ifr)) {
412 fprintf(stderr, "Could not configure tun interface: %s\n", strerror(errno));
416 ifr.ifr_name[IFNAMSIZ - 1] = 0;
417 fprintf(stderr, "Using tun interface %s\n", ifr.ifr_name);
423 static struct WSAData wsa_state;
425 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
431 struct addrinfo *ai, hint;
432 memset(&hint, 0, sizeof(hint));
434 hint.ai_family = addressfamily;
435 hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
436 hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
437 hint.ai_flags = initiator ? 0 : AI_PASSIVE;
439 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
440 fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno));
444 int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
447 fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
453 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
456 int res = connect(sock, ai->ai_addr, ai->ai_addrlen);
462 fprintf(stderr, "Could not connect to peer: %s\n", sockstrerror(sockerrno));
466 fprintf(stderr, "Connected\n");
468 int res = bind(sock, ai->ai_addr, ai->ai_addrlen);
474 fprintf(stderr, "Could not bind socket: %s\n", sockstrerror(sockerrno));
479 if(listen(sock, 1)) {
480 fprintf(stderr, "Could not listen on socket: %s\n", sockstrerror(sockerrno));
484 fprintf(stderr, "Listening...\n");
486 sock = accept(sock, NULL, NULL);
489 fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno));
493 fprintf(stderr, "Listening...\n");
496 struct sockaddr addr;
497 socklen_t addrlen = sizeof(addr);
499 if(recvfrom(sock, buf, sizeof(buf), MSG_PEEK, &addr, &addrlen) <= 0) {
500 fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
504 if(connect(sock, &addr, addrlen)) {
505 fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno));
510 fprintf(stderr, "Connected\n");
516 FILE *fp = fopen(argv[1], "r");
519 fprintf(stderr, "Could not open %s: %s\n", argv[1], strerror(errno));
523 ecdsa_t *mykey = NULL;
525 if(!(mykey = ecdsa_read_pem_private_key(fp))) {
531 fp = fopen(argv[2], "r");
534 fprintf(stderr, "Could not open %s: %s\n", argv[2], strerror(errno));
539 ecdsa_t *hiskey = NULL;
541 if(!(hiskey = ecdsa_read_pem_public_key(fp))) {
549 fprintf(stderr, "Keys loaded\n");
554 if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record)) {
563 in = start_input_reader();
566 fprintf(stderr, "Could not init stdin reader thread\n");
575 int max_fd = MAX(sock, in);
578 if(writeonly && readonly) {
582 char buf[65535] = "";
583 size_t readsize = datagram ? 1460u : sizeof(buf);
588 if(!readonly && s.instate) {
594 if(select(max_fd + 1, &fds, NULL, NULL, NULL) <= 0) {
600 if(FD_ISSET(in, &fds)) {
602 ssize_t len = recv(in, buf, readsize, 0);
604 ssize_t len = read(in, buf, readsize);
608 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
616 shutdown(in, SD_SEND);
628 if(special && buf[0] == '#') {
629 s.outseqno = atoi(buf + 1);
632 if(special && buf[0] == '^') {
633 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
634 } else if(special && buf[0] == '$') {
638 sptps_send_record(&s, 0, buf, len);
640 } else if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof(buf) : (size_t)len)) {
647 if(FD_ISSET(sock, &fds)) {
648 ssize_t len = recv(sock, buf, sizeof(buf), 0);
651 fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
658 fprintf(stderr, "Connection terminated by peer.\n");
663 char *hex = alloca(len * 2 + 1);
664 bin2hex(buf, hex, len);
665 fprintf(stderr, "Received %ld bytes of data:\n%s\n", (long)len, hex);
668 if(packetloss && (int)prng(100) < packetloss) {
670 fprintf(stderr, "Dropped.\n");
679 size_t done = sptps_receive_data(&s, bufp, len);
690 len -= (ssize_t) done;
695 bool stopped = sptps_stop(&s);