Allow testing the replay window with sptps_test.
[tinc] / src / sptps_test.c
1 /*
2     sptps_test.c -- Simple Peer-to-Peer Security test program
3     Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>,
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 #include <getopt.h>
23
24 #include "crypto.h"
25 #include "ecdsa.h"
26 #include "sptps.h"
27 #include "utils.h"
28
29 // Symbols necessary to link with logger.o
30 bool send_request(void *c, const char *msg, ...) { return false; }
31 struct list_t *connection_list = NULL;
32 bool send_meta(void *c, const char *msg , int len) { return false; }
33 char *logfilename = NULL;
34 struct timeval now;
35
36 ecdsa_t *mykey, *hiskey;
37
38 static bool send_data(void *handle, uint8_t type, const char *data, size_t len) {
39         char hex[len * 2 + 1];
40         bin2hex(data, hex, len);
41         fprintf(stderr, "Sending %d bytes of data:\n%s\n", (int)len, hex);
42         const int *sock = handle;
43         if(send(*sock, data, len, 0) != len)
44                 return false;
45         return true;
46 }
47
48 static bool receive_record(void *handle, uint8_t type, const char *data, uint16_t len) {
49         fprintf(stderr, "Received type %d record of %hu bytes:\n", type, len);
50         fwrite(data, len, 1, stdout);
51         return true;
52 }
53
54 static struct option const long_options[] = {
55         {"datagram", no_argument, NULL, 'd'},
56         {"packet-loss", required_argument, NULL, 'l'},
57         {"replay-window", required_argument, NULL, 'r'},
58         {"help", no_argument, NULL, 1},
59         {NULL, 0, NULL, 0}
60 };
61
62 const char *program_name;
63
64 static void usage() {
65         fprintf(stderr, "Usage: %s [options] my_ecdsa_key_file his_ecdsa_key_file [host] port\n\n", program_name);
66         fprintf(stderr, "Valid options are:\n"
67                         "  -d, --datagram          Enable datagram mode.\n"
68                         "  -l, --packet-loss RATE  Fake packet loss of RATE percent.\n"
69                         "  -r, --replay-window N   Set replay window to N bytes.\n"
70                         "\n");
71         fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
72 }
73
74 int main(int argc, char *argv[]) {
75         program_name = argv[0];
76         bool initiator = false;
77         bool datagram = false;
78         int packetloss = 0;
79         int r;
80         int option_index = 0;
81
82         while((r = getopt_long(argc, argv, "dl:r:", long_options, &option_index)) != EOF) {
83                 switch (r) {
84                         case 0:   /* long option */
85                                 break;
86
87                         case 'd': /* datagram mode */
88                                 datagram = true;
89                                 break;
90
91                         case 'l': /* packet loss rate */
92                                 packetloss = atoi(optarg);
93                                 break;
94
95                         case 'r': /* replay window size */
96                                 sptps_replaywin = atoi(optarg);
97                                 break;
98
99                         case '?': /* wrong options */
100                                 usage();
101                                 return 1;
102
103                         case 1: /* help */
104                                 usage();
105                                 return 0;
106
107                         default:
108                                 break;
109                 }
110         }
111
112         argc -= optind - 1;
113         argv += optind - 1;
114
115         if(argc < 4 || argc > 5) {
116                 fprintf(stderr, "Wrong number of arguments.\n");
117                 usage();
118                 return 1;
119         }
120
121         if(argc > 4)
122                 initiator = true;
123
124         srand(time(NULL));
125
126 #ifdef HAVE_MINGW
127         static struct WSAData wsa_state;
128         if(WSAStartup(MAKEWORD(2, 2), &wsa_state))
129                 return 1;
130 #endif
131
132         struct addrinfo *ai, hint;
133         memset(&hint, 0, sizeof hint);
134
135         hint.ai_family = AF_UNSPEC;
136         hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
137         hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
138         hint.ai_flags = initiator ? 0 : AI_PASSIVE;
139
140         if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
141                 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
142                 return 1;
143         }
144
145         int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
146         if(sock < 0) {
147                 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
148                 return 1;
149         }
150
151         int one = 1;
152         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
153
154         if(initiator) {
155                 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
156                         fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
157                         return 1;
158                 }
159                 fprintf(stderr, "Connected\n");
160         } else {
161                 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
162                         fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
163                         return 1;
164                 }
165
166                 if(!datagram) {
167                         if(listen(sock, 1)) {
168                                 fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
169                                 return 1;
170                         }
171                         fprintf(stderr, "Listening...\n");
172
173                         sock = accept(sock, NULL, NULL);
174                         if(sock < 0) {
175                                 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
176                                 return 1;
177                         }
178                 } else {
179                         fprintf(stderr, "Listening...\n");
180
181                         char buf[65536];
182                         struct sockaddr addr;
183                         socklen_t addrlen = sizeof addr;
184
185                         if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) {
186                                 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
187                                 return 1;
188                         }
189
190                         if(connect(sock, &addr, addrlen)) {
191                                 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
192                                 return 1;
193                         }
194                 }
195
196                 fprintf(stderr, "Connected\n");
197         }
198
199         crypto_init();
200
201         FILE *fp = fopen(argv[1], "r");
202         if(!(mykey = ecdsa_read_pem_private_key(fp)))
203                 return 1;
204         fclose(fp);
205
206         fp = fopen(argv[2], "r");
207         if(!(hiskey = ecdsa_read_pem_public_key(fp)))
208                 return 1;
209         fclose(fp);
210
211         fprintf(stderr, "Keys loaded\n");
212
213         sptps_t s;
214         if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
215                 return 1;
216
217         while(true) {
218                 char buf[65535] = "";
219
220                 fd_set fds;
221                 FD_ZERO(&fds);
222 #ifndef HAVE_MINGW
223                 FD_SET(0, &fds);
224 #endif
225                 FD_SET(sock, &fds);
226                 if(select(sock + 1, &fds, NULL, NULL, NULL) <= 0)
227                         return 1;
228
229                 if(FD_ISSET(0, &fds)) {
230                         ssize_t len = read(0, buf, sizeof buf);
231                         if(len < 0) {
232                                 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
233                                 return 1;
234                         }
235                         if(len == 0)
236                                 break;
237                         if(buf[0] == '#')
238                                 s.outseqno = atoi(buf + 1);
239                         if(buf[0] == '^')
240                                 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
241                         else if(buf[0] == '$') {
242                                 sptps_force_kex(&s);
243                                 if(len > 1)
244                                         sptps_send_record(&s, 0, buf, len);
245                         } else
246                         if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, buf[0] == '\n' ? 0 : buf[0] == '*' ? sizeof buf : len))
247                                 return 1;
248                 }
249
250                 if(FD_ISSET(sock, &fds)) {
251                         ssize_t len = recv(sock, buf, sizeof buf, 0);
252                         if(len < 0) {
253                                 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
254                                 return 1;
255                         }
256                         if(len == 0) {
257                                 fprintf(stderr, "Connection terminated by peer.\n");
258                                 break;
259                         }
260                         char hex[len * 2 + 1];
261                         bin2hex(buf, hex, len);
262                         fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex);
263                         if((rand() % 100) < packetloss) {
264                                 fprintf(stderr, "Dropped.\n");
265                                 continue;
266                         }
267                         if(!sptps_receive_data(&s, buf, len) && !datagram)
268                                 return 1;
269                 }
270         }
271
272         if(!sptps_stop(&s))
273                 return 1;
274
275         return 0;
276 }