]> git.meshlink.io Git - meshlink/blob - src/sptps_test.c
Remove old tests for tinc 1.1 and add a basic test for MeshLink.
[meshlink] / src / sptps_test.c
1 /*
2     sptps_test.c -- Simple Peer-to-Peer Security test program
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #ifdef HAVE_LINUX
23 #include <sys/ioctl.h>
24 #include <linux/if.h>
25 #include <linux/if_tun.h>
26 #endif
27
28 #include <getopt.h>
29
30 #include "crypto.h"
31 #include "ecdsa.h"
32 #include "sptps.h"
33 #include "utils.h"
34
35 // Symbols necessary to link with logger.o
36 bool send_request(void *c, const char *msg, ...) { return false; }
37 void *mesh;
38 bool send_meta(void *c, const char *msg , int len) { return false; }
39 char *logfilename = NULL;
40 struct timeval now;
41
42 static bool verbose;
43 static bool readonly;
44 static bool writeonly;
45 static int in = 0;
46 static int out = 1;
47
48 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
49         char hex[len * 2 + 1];
50         bin2hex(data, hex, len);
51         if(verbose)
52                 fprintf(stderr, "Sending %d bytes of data:\n%s\n", (int)len, hex);
53         const int *sock = handle;
54         if(send(*sock, data, len, 0) != len)
55                 return false;
56         return true;
57 }
58
59 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
60         if(verbose)
61                 fprintf(stderr, "Received type %d record of %hu bytes:\n", type, len);
62         if(!writeonly)
63                 write(out, data, len);
64         return true;
65 }
66
67 static struct option const long_options[] = {
68         {"datagram", no_argument, NULL, 'd'},
69         {"quit", no_argument, NULL, 'q'},
70         {"readonly", no_argument, NULL, 'r'},
71         {"writeonly", no_argument, NULL, 'w'},
72         {"packet-loss", required_argument, NULL, 'L'},
73         {"replay-window", required_argument, NULL, 'W'},
74         {"verbose", required_argument, NULL, 'v'},
75         {"help", no_argument, NULL, 1},
76         {NULL, 0, NULL, 0}
77 };
78
79 const char *program_name;
80
81 static void usage() {
82         fprintf(stderr, "Usage: %s [options] my_ecdsa_key_file his_ecdsa_key_file [host] port\n\n", program_name);
83         fprintf(stderr, "Valid options are:\n"
84                         "  -d, --datagram          Enable datagram mode.\n"
85                         "  -q, --quit              Quit when EOF occurs on stdin.\n"
86                         "  -r, --readonly          Only send data from the socket to stdout.\n"
87 #ifdef HAVE_LINUX
88                         "  -t, --tun               Use a tun device instead of stdio.\n"
89 #endif
90                         "  -w, --writeonly         Only send data from stdin to the socket.\n"
91                         "  -L, --packet-loss RATE  Fake packet loss of RATE percent.\n"
92                         "  -R, --replay-window N   Set replay window to N bytes.\n"
93                         "  -v, --verbose           Display debug messages.\n"
94                         "\n");
95         fprintf(stderr, "Report bugs to bugs@meshlink.io.\n");
96 }
97
98 int main(int argc, char *argv[]) {
99         program_name = argv[0];
100         bool initiator = false;
101         bool datagram = false;
102 #ifdef HAVE_LINUX
103         bool tun = false;
104 #endif
105         int packetloss = 0;
106         int r;
107         int option_index = 0;
108         ecdsa_t *mykey = NULL, *hiskey = NULL;
109         bool quit = false;
110
111         while((r = getopt_long(argc, argv, "dqrtwL:W:v", long_options, &option_index)) != EOF) {
112                 switch (r) {
113                         case 0:   /* long option */
114                                 break;
115
116                         case 'd': /* datagram mode */
117                                 datagram = true;
118                                 break;
119
120                         case 'q': /* close connection on EOF from stdin */
121                                 quit = true;
122                                 break;
123
124                         case 'r': /* read only */
125                                 readonly = true;
126                                 break;
127
128                         case 't': /* read only */
129 #ifdef HAVE_LINUX
130                                 tun = true;
131 #else
132                                 fprintf(stderr, "--tun is only supported on Linux.\n");
133                                 usage();
134                                 return 1;
135 #endif
136                                 break;
137
138                         case 'w': /* write only */
139                                 writeonly = true;
140                                 break;
141
142                         case 'L': /* packet loss rate */
143                                 packetloss = atoi(optarg);
144                                 break;
145
146                         case 'W': /* replay window size */
147                                 sptps_replaywin = atoi(optarg);
148                                 break;
149
150                         case 'v': /* be verbose */
151                                 verbose = true;
152                                 break;
153
154                         case '?': /* wrong options */
155                                 usage();
156                                 return 1;
157
158                         case 1: /* help */
159                                 usage();
160                                 return 0;
161
162                         default:
163                                 break;
164                 }
165         }
166
167         argc -= optind - 1;
168         argv += optind - 1;
169
170         if(argc < 4 || argc > 5) {
171                 fprintf(stderr, "Wrong number of arguments.\n");
172                 usage();
173                 return 1;
174         }
175
176         if(argc > 4)
177                 initiator = true;
178
179         srand(time(NULL));
180
181 #ifdef HAVE_LINUX
182         if(tun) {
183                 in = out = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
184                 if(in < 0) {
185                         fprintf(stderr, "Could not open tun device: %s\n", strerror(errno));
186                         return 1;
187                 }
188                 struct ifreq ifr = {
189                         .ifr_flags = IFF_TUN
190                 };
191                 if(ioctl(in, TUNSETIFF, &ifr)) {
192                         fprintf(stderr, "Could not configure tun interface: %s\n", strerror(errno));
193                         return 1;
194                 }
195                 ifr.ifr_name[IFNAMSIZ - 1] = 0;
196                 fprintf(stderr, "Using tun interface %s\n", ifr.ifr_name);
197         }
198 #endif
199
200 #ifdef HAVE_MINGW
201         static struct WSAData wsa_state;
202         if(WSAStartup(MAKEWORD(2, 2), &wsa_state))
203                 return 1;
204 #endif
205
206         struct addrinfo *ai, hint;
207         memset(&hint, 0, sizeof hint);
208
209         hint.ai_family = AF_UNSPEC;
210         hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
211         hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
212         hint.ai_flags = initiator ? 0 : AI_PASSIVE;
213
214         if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
215                 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
216                 return 1;
217         }
218
219         int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
220         if(sock < 0) {
221                 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
222                 return 1;
223         }
224
225         int one = 1;
226         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
227
228         if(initiator) {
229                 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
230                         fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
231                         return 1;
232                 }
233                 fprintf(stderr, "Connected\n");
234         } else {
235                 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
236                         fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
237                         return 1;
238                 }
239
240                 if(!datagram) {
241                         if(listen(sock, 1)) {
242                                 fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
243                                 return 1;
244                         }
245                         fprintf(stderr, "Listening...\n");
246
247                         sock = accept(sock, NULL, NULL);
248                         if(sock < 0) {
249                                 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
250                                 return 1;
251                         }
252                 } else {
253                         fprintf(stderr, "Listening...\n");
254
255                         char buf[65536];
256                         struct sockaddr addr;
257                         socklen_t addrlen = sizeof addr;
258
259                         if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) {
260                                 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
261                                 return 1;
262                         }
263
264                         if(connect(sock, &addr, addrlen)) {
265                                 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
266                                 return 1;
267                         }
268                 }
269
270                 fprintf(stderr, "Connected\n");
271         }
272
273         crypto_init();
274
275         FILE *fp = fopen(argv[1], "r");
276         if(!(mykey = ecdsa_read_pem_private_key(fp)))
277                 return 1;
278         fclose(fp);
279
280         fp = fopen(argv[2], "r");
281         if(!(hiskey = ecdsa_read_pem_public_key(fp)))
282                 return 1;
283         fclose(fp);
284
285         if(verbose)
286                 fprintf(stderr, "Keys loaded\n");
287
288         sptps_t s;
289         if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
290                 return 1;
291
292         while(true) {
293                 if(writeonly && readonly)
294                         break;
295
296                 char buf[65535] = "";
297
298                 fd_set fds;
299                 FD_ZERO(&fds);
300 #ifndef HAVE_MINGW
301                 if(!readonly && s.instate)
302                         FD_SET(in, &fds);
303 #endif
304                 FD_SET(sock, &fds);
305                 if(select(sock + 1, &fds, NULL, NULL, NULL) <= 0)
306                         return 1;
307
308                 if(FD_ISSET(in, &fds)) {
309                         ssize_t len = read(in, buf, sizeof buf);
310                         if(len < 0) {
311                                 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
312                                 return 1;
313                         }
314                         if(len == 0) {
315                                 if(quit)
316                                         break;
317                                 readonly = true;
318                                 continue;
319                         }
320                         if(buf[0] == '#')
321                                 s.outseqno = atoi(buf + 1);
322                         if(buf[0] == '^')
323                                 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
324                         else if(buf[0] == '$') {
325                                 sptps_force_kex(&s);
326                                 if(len > 1)
327                                         sptps_send_record(&s, 0, buf, len);
328                         } else
329                         if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof buf : len))
330                                 return 1;
331                 }
332
333                 if(FD_ISSET(sock, &fds)) {
334                         ssize_t len = recv(sock, buf, sizeof buf, 0);
335                         if(len < 0) {
336                                 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
337                                 return 1;
338                         }
339                         if(len == 0) {
340                                 fprintf(stderr, "Connection terminated by peer.\n");
341                                 break;
342                         }
343                         if(verbose) {
344                                 char hex[len * 2 + 1];
345                                 bin2hex(buf, hex, len);
346                                 fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex);
347                         }
348                         if(packetloss && (rand() % 100) < packetloss) {
349                                 if(verbose)
350                                         fprintf(stderr, "Dropped.\n");
351                                 continue;
352                         }
353                         if(!sptps_receive_data(&s, buf, len) && !datagram)
354                                 return 1;
355                 }
356         }
357
358         if(!sptps_stop(&s))
359                 return 1;
360
361         return 0;
362 }