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