2 sptps_test.c -- Simple Peer-to-Peer Security test program
3 Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>,
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.
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;
38 static bool writeonly;
40 static bool send_data(void *handle, uint8_t type, const char *data, size_t len) {
41 char hex[len * 2 + 1];
42 bin2hex(data, hex, len);
44 fprintf(stderr, "Sending %d bytes of data:\n%s\n", (int)len, hex);
45 const int *sock = handle;
46 if(send(*sock, data, len, 0) != len)
51 static bool receive_record(void *handle, uint8_t type, const char *data, uint16_t len) {
53 fprintf(stderr, "Received type %d record of %hu bytes:\n", type, len);
55 fwrite(data, len, 1, stdout);
59 static struct option const long_options[] = {
60 {"datagram", no_argument, NULL, 'd'},
61 {"quit", no_argument, NULL, 'q'},
62 {"readonly", no_argument, NULL, 'r'},
63 {"writeonly", no_argument, NULL, 'w'},
64 {"packet-loss", required_argument, NULL, 'L'},
65 {"replay-window", required_argument, NULL, 'W'},
66 {"verbose", required_argument, NULL, 'v'},
67 {"help", no_argument, NULL, 1},
71 const char *program_name;
74 fprintf(stderr, "Usage: %s [options] my_ecdsa_key_file his_ecdsa_key_file [host] port\n\n", program_name);
75 fprintf(stderr, "Valid options are:\n"
76 " -d, --datagram Enable datagram mode.\n"
77 " -q, --quit Quit when EOF occurs on stdin.\n"
78 " -r, --readonly Only send data from the socket to stdout.\n"
79 " -w, --writeonly Only send data from stdin to the socket.\n"
80 " -L, --packet-loss RATE Fake packet loss of RATE percent.\n"
81 " -R, --replay-window N Set replay window to N bytes.\n"
82 " -v, --verbose Display debug messages.\n"
84 fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
87 int main(int argc, char *argv[]) {
88 program_name = argv[0];
89 bool initiator = false;
90 bool datagram = false;
94 ecdsa_t *mykey = NULL, *hiskey = NULL;
97 while((r = getopt_long(argc, argv, "dqrwL:W:v", long_options, &option_index)) != EOF) {
99 case 0: /* long option */
102 case 'd': /* datagram mode */
106 case 'q': /* close connection on EOF from stdin */
110 case 'r': /* read only */
114 case 'w': /* write only */
118 case 'L': /* packet loss rate */
119 packetloss = atoi(optarg);
122 case 'W': /* replay window size */
123 sptps_replaywin = atoi(optarg);
126 case 'v': /* be verbose */
130 case '?': /* wrong options */
146 if(argc < 4 || argc > 5) {
147 fprintf(stderr, "Wrong number of arguments.\n");
158 static struct WSAData wsa_state;
159 if(WSAStartup(MAKEWORD(2, 2), &wsa_state))
163 struct addrinfo *ai, hint;
164 memset(&hint, 0, sizeof hint);
166 hint.ai_family = AF_UNSPEC;
167 hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
168 hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
169 hint.ai_flags = initiator ? 0 : AI_PASSIVE;
171 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
172 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
176 int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
178 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
183 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
186 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
187 fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
190 fprintf(stderr, "Connected\n");
192 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
193 fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
198 if(listen(sock, 1)) {
199 fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
202 fprintf(stderr, "Listening...\n");
204 sock = accept(sock, NULL, NULL);
206 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
210 fprintf(stderr, "Listening...\n");
213 struct sockaddr addr;
214 socklen_t addrlen = sizeof addr;
216 if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) {
217 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
221 if(connect(sock, &addr, addrlen)) {
222 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
227 fprintf(stderr, "Connected\n");
232 FILE *fp = fopen(argv[1], "r");
233 if(!(mykey = ecdsa_read_pem_private_key(fp)))
237 fp = fopen(argv[2], "r");
238 if(!(hiskey = ecdsa_read_pem_public_key(fp)))
243 fprintf(stderr, "Keys loaded\n");
246 if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
250 if(writeonly && readonly)
253 char buf[65535] = "";
258 if(!readonly && s.instate)
262 if(select(sock + 1, &fds, NULL, NULL, NULL) <= 0)
265 if(FD_ISSET(0, &fds)) {
266 ssize_t len = read(0, buf, sizeof buf);
268 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
278 s.outseqno = atoi(buf + 1);
280 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
281 else if(buf[0] == '$') {
284 sptps_send_record(&s, 0, buf, len);
286 if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof buf : len))
290 if(FD_ISSET(sock, &fds)) {
291 ssize_t len = recv(sock, buf, sizeof buf, 0);
293 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
297 fprintf(stderr, "Connection terminated by peer.\n");
301 char hex[len * 2 + 1];
302 bin2hex(buf, hex, len);
303 fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex);
305 if(packetloss && (rand() % 100) < packetloss) {
307 fprintf(stderr, "Dropped.\n");
310 if(!sptps_receive_data(&s, buf, len) && !datagram)