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