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