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>
33 // Symbols necessary to link with logger.o
34 bool send_request(void *c, const char *msg, ...) {
40 struct list_t *connection_list = NULL;
42 bool send_meta(void *c, const char *msg, int len) {
49 char *logfilename = NULL;
50 bool do_detach = false;
56 static bool writeonly;
59 static int addressfamily = AF_UNSPEC;
61 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
63 char hex[len * 2 + 1];
64 bin2hex(data, hex, len);
67 fprintf(stderr, "Sending %d bytes of data:\n%s\n", (int)len, hex);
70 const int *sock = handle;
72 if((size_t)send(*sock, data, len, 0) != len) {
79 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
83 fprintf(stderr, "Received type %d record of %u bytes:\n", type, len);
87 write(out, data, len);
93 static struct option const long_options[] = {
94 {"datagram", no_argument, NULL, 'd'},
95 {"quit", no_argument, NULL, 'q'},
96 {"readonly", no_argument, NULL, 'r'},
97 {"writeonly", no_argument, NULL, 'w'},
98 {"packet-loss", required_argument, NULL, 'L'},
99 {"replay-window", required_argument, NULL, 'W'},
100 {"special", no_argument, NULL, 's'},
101 {"verbose", required_argument, NULL, 'v'},
102 {"help", no_argument, NULL, 1},
106 const char *program_name;
108 static void usage() {
109 fprintf(stderr, "Usage: %s [options] my_ed25519_key_file his_ed25519_key_file [host] port\n\n", program_name);
110 fprintf(stderr, "Valid options are:\n"
111 " -d, --datagram Enable datagram mode.\n"
112 " -q, --quit Quit when EOF occurs on stdin.\n"
113 " -r, --readonly Only send data from the socket to stdout.\n"
115 " -t, --tun Use a tun device instead of stdio.\n"
117 " -w, --writeonly Only send data from stdin to the socket.\n"
118 " -L, --packet-loss RATE Fake packet loss of RATE percent.\n"
119 " -R, --replay-window N Set replay window to N bytes.\n"
120 " -s, --special Enable special handling of lines starting with #, ^ and $.\n"
121 " -v, --verbose Display debug messages.\n"
125 fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
128 int main(int argc, char *argv[]) {
129 program_name = argv[0];
130 bool initiator = false;
131 bool datagram = false;
137 int option_index = 0;
138 ecdsa_t *mykey = NULL, *hiskey = NULL;
141 while((r = getopt_long(argc, argv, "dqrstwL:W:v46", long_options, &option_index)) != EOF) {
143 case 0: /* long option */
146 case 'd': /* datagram mode */
150 case 'q': /* close connection on EOF from stdin */
154 case 'r': /* read only */
158 case 't': /* read only */
162 fprintf(stderr, "--tun is only supported on Linux.\n");
168 case 'w': /* write only */
172 case 'L': /* packet loss rate */
173 packetloss = atoi(optarg);
176 case 'W': /* replay window size */
177 sptps_replaywin = atoi(optarg);
180 case 'v': /* be verbose */
184 case 's': /* special character handling */
188 case '?': /* wrong options */
193 addressfamily = AF_INET;
197 addressfamily = AF_INET6;
212 if(argc < 4 || argc > 5) {
213 fprintf(stderr, "Wrong number of arguments.\n");
227 in = out = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
230 fprintf(stderr, "Could not open tun device: %s\n", strerror(errno));
238 if(ioctl(in, TUNSETIFF, &ifr)) {
239 fprintf(stderr, "Could not configure tun interface: %s\n", strerror(errno));
243 ifr.ifr_name[IFNAMSIZ - 1] = 0;
244 fprintf(stderr, "Using tun interface %s\n", ifr.ifr_name);
250 static struct WSAData wsa_state;
252 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
258 struct addrinfo *ai, hint;
259 memset(&hint, 0, sizeof(hint));
261 hint.ai_family = addressfamily;
262 hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
263 hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
264 hint.ai_flags = initiator ? 0 : AI_PASSIVE;
266 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
267 fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno));
271 int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
274 fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
279 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
282 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
283 fprintf(stderr, "Could not connect to peer: %s\n", sockstrerror(sockerrno));
287 fprintf(stderr, "Connected\n");
289 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
290 fprintf(stderr, "Could not bind socket: %s\n", sockstrerror(sockerrno));
295 if(listen(sock, 1)) {
296 fprintf(stderr, "Could not listen on socket: %s\n", sockstrerror(sockerrno));
300 fprintf(stderr, "Listening...\n");
302 sock = accept(sock, NULL, NULL);
305 fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno));
309 fprintf(stderr, "Listening...\n");
312 struct sockaddr addr;
313 socklen_t addrlen = sizeof(addr);
315 if(recvfrom(sock, buf, sizeof(buf), MSG_PEEK, &addr, &addrlen) <= 0) {
316 fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
320 if(connect(sock, &addr, addrlen)) {
321 fprintf(stderr, "Could not accept connection: %s\n", sockstrerror(sockerrno));
326 fprintf(stderr, "Connected\n");
331 FILE *fp = fopen(argv[1], "r");
334 fprintf(stderr, "Could not open %s: %s\n", argv[1], strerror(errno));
338 if(!(mykey = ecdsa_read_pem_private_key(fp))) {
344 fp = fopen(argv[2], "r");
347 fprintf(stderr, "Could not open %s: %s\n", argv[2], strerror(errno));
351 if(!(hiskey = ecdsa_read_pem_public_key(fp))) {
358 fprintf(stderr, "Keys loaded\n");
363 if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record)) {
368 if(writeonly && readonly) {
372 char buf[65535] = "";
373 size_t readsize = datagram ? 1460u : sizeof(buf);
379 if(!readonly && s.instate) {
386 if(select(sock + 1, &fds, NULL, NULL, NULL) <= 0) {
390 if(FD_ISSET(in, &fds)) {
391 ssize_t len = read(in, buf, readsize);
394 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
407 if(special && buf[0] == '#') {
408 s.outseqno = atoi(buf + 1);
411 if(special && buf[0] == '^') {
412 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
413 } else if(special && buf[0] == '$') {
417 sptps_send_record(&s, 0, buf, len);
419 } else if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof(buf) : (size_t)len)) {
424 if(FD_ISSET(sock, &fds)) {
425 ssize_t len = recv(sock, buf, sizeof(buf), 0);
428 fprintf(stderr, "Could not read from socket: %s\n", sockstrerror(sockerrno));
433 fprintf(stderr, "Connection terminated by peer.\n");
438 char hex[len * 2 + 1];
439 bin2hex(buf, hex, len);
440 fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex);
443 if(packetloss && (rand() % 100) < packetloss) {
445 fprintf(stderr, "Dropped.\n");
454 size_t done = sptps_receive_data(&s, bufp, len);
468 if(!sptps_stop(&s)) {