2 sptps_test.c -- Simple Peer-to-Peer Security test program
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
23 #include <sys/ioctl.h>
25 #include <linux/if_tun.h>
35 // Symbols necessary to link with logger.o
36 bool send_request(void *c, const char *msg, ...) { return false; }
38 bool send_meta(void *c, const char *msg , int len) { return false; }
39 char *logfilename = NULL;
44 static bool writeonly;
48 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
49 char hex[len * 2 + 1];
50 bin2hex(data, hex, len);
52 fprintf(stderr, "Sending %d bytes of data:\n%s\n", (int)len, hex);
53 const int *sock = handle;
54 if(send(*sock, data, len, 0) != len)
59 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
61 fprintf(stderr, "Received type %d record of %hu bytes:\n", type, len);
63 write(out, data, len);
67 static struct option const long_options[] = {
68 {"datagram", no_argument, NULL, 'd'},
69 {"quit", no_argument, NULL, 'q'},
70 {"readonly", no_argument, NULL, 'r'},
71 {"writeonly", no_argument, NULL, 'w'},
72 {"packet-loss", required_argument, NULL, 'L'},
73 {"replay-window", required_argument, NULL, 'W'},
74 {"verbose", required_argument, NULL, 'v'},
75 {"help", no_argument, NULL, 1},
79 const char *program_name;
82 fprintf(stderr, "Usage: %s [options] my_ecdsa_key_file his_ecdsa_key_file [host] port\n\n", program_name);
83 fprintf(stderr, "Valid options are:\n"
84 " -d, --datagram Enable datagram mode.\n"
85 " -q, --quit Quit when EOF occurs on stdin.\n"
86 " -r, --readonly Only send data from the socket to stdout.\n"
88 " -t, --tun Use a tun device instead of stdio.\n"
90 " -w, --writeonly Only send data from stdin to the socket.\n"
91 " -L, --packet-loss RATE Fake packet loss of RATE percent.\n"
92 " -R, --replay-window N Set replay window to N bytes.\n"
93 " -v, --verbose Display debug messages.\n"
95 fprintf(stderr, "Report bugs to bugs@meshlink.io.\n");
98 int main(int argc, char *argv[]) {
99 program_name = argv[0];
100 bool initiator = false;
101 bool datagram = false;
107 int option_index = 0;
108 ecdsa_t *mykey = NULL, *hiskey = NULL;
111 while((r = getopt_long(argc, argv, "dqrtwL:W:v", long_options, &option_index)) != EOF) {
113 case 0: /* long option */
116 case 'd': /* datagram mode */
120 case 'q': /* close connection on EOF from stdin */
124 case 'r': /* read only */
128 case 't': /* read only */
132 fprintf(stderr, "--tun is only supported on Linux.\n");
138 case 'w': /* write only */
142 case 'L': /* packet loss rate */
143 packetloss = atoi(optarg);
146 case 'W': /* replay window size */
147 sptps_replaywin = atoi(optarg);
150 case 'v': /* be verbose */
154 case '?': /* wrong options */
170 if(argc < 4 || argc > 5) {
171 fprintf(stderr, "Wrong number of arguments.\n");
183 in = out = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
185 fprintf(stderr, "Could not open tun device: %s\n", strerror(errno));
191 if(ioctl(in, TUNSETIFF, &ifr)) {
192 fprintf(stderr, "Could not configure tun interface: %s\n", strerror(errno));
195 ifr.ifr_name[IFNAMSIZ - 1] = 0;
196 fprintf(stderr, "Using tun interface %s\n", ifr.ifr_name);
201 static struct WSAData wsa_state;
202 if(WSAStartup(MAKEWORD(2, 2), &wsa_state))
206 struct addrinfo *ai, hint;
207 memset(&hint, 0, sizeof hint);
209 hint.ai_family = AF_UNSPEC;
210 hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
211 hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
212 hint.ai_flags = initiator ? 0 : AI_PASSIVE;
214 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
215 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
219 int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
221 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
226 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
229 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
230 fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
233 fprintf(stderr, "Connected\n");
235 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
236 fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
241 if(listen(sock, 1)) {
242 fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
245 fprintf(stderr, "Listening...\n");
247 sock = accept(sock, NULL, NULL);
249 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
253 fprintf(stderr, "Listening...\n");
256 struct sockaddr addr;
257 socklen_t addrlen = sizeof addr;
259 if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) {
260 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
264 if(connect(sock, &addr, addrlen)) {
265 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
270 fprintf(stderr, "Connected\n");
275 FILE *fp = fopen(argv[1], "r");
276 if(!(mykey = ecdsa_read_pem_private_key(fp)))
280 fp = fopen(argv[2], "r");
281 if(!(hiskey = ecdsa_read_pem_public_key(fp)))
286 fprintf(stderr, "Keys loaded\n");
289 if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
293 if(writeonly && readonly)
296 char buf[65535] = "";
301 if(!readonly && s.instate)
305 if(select(sock + 1, &fds, NULL, NULL, NULL) <= 0)
308 if(FD_ISSET(in, &fds)) {
309 ssize_t len = read(in, buf, sizeof buf);
311 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
321 s.outseqno = atoi(buf + 1);
323 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
324 else if(buf[0] == '$') {
327 sptps_send_record(&s, 0, buf, len);
329 if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof buf : len))
333 if(FD_ISSET(sock, &fds)) {
334 ssize_t len = recv(sock, buf, sizeof buf, 0);
336 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
340 fprintf(stderr, "Connection terminated by peer.\n");
344 char hex[len * 2 + 1];
345 bin2hex(buf, hex, len);
346 fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex);
348 if(packetloss && (rand() % 100) < packetloss) {
350 fprintf(stderr, "Dropped.\n");
353 if(!sptps_receive_data(&s, buf, len) && !datagram)