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