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;
37 static bool writeonly;
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)
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);
52 fwrite(data, len, 1, stdout);
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},
67 const char *program_name;
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"
79 fprintf(stderr, "Report bugs to tinc@tinc-vpn.org.\n");
82 int main(int argc, char *argv[]) {
83 program_name = argv[0];
84 bool initiator = false;
85 bool datagram = false;
89 ecdsa_t *mykey = NULL, *hiskey = NULL;
92 while((r = getopt_long(argc, argv, "dqrwL:W:", long_options, &option_index)) != EOF) {
94 case 0: /* long option */
97 case 'd': /* datagram mode */
101 case 'q': /* close connection on EOF from stdin */
105 case 'r': /* read only */
109 case 'w': /* write only */
113 case 'L': /* packet loss rate */
114 packetloss = atoi(optarg);
117 case 'W': /* replay window size */
118 sptps_replaywin = atoi(optarg);
121 case '?': /* wrong options */
137 if(argc < 4 || argc > 5) {
138 fprintf(stderr, "Wrong number of arguments.\n");
149 static struct WSAData wsa_state;
150 if(WSAStartup(MAKEWORD(2, 2), &wsa_state))
154 struct addrinfo *ai, hint;
155 memset(&hint, 0, sizeof hint);
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;
162 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
163 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
167 int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
169 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
174 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
177 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
178 fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
181 fprintf(stderr, "Connected\n");
183 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
184 fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
189 if(listen(sock, 1)) {
190 fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
193 fprintf(stderr, "Listening...\n");
195 sock = accept(sock, NULL, NULL);
197 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
201 fprintf(stderr, "Listening...\n");
204 struct sockaddr addr;
205 socklen_t addrlen = sizeof addr;
207 if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) {
208 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
212 if(connect(sock, &addr, addrlen)) {
213 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
218 fprintf(stderr, "Connected\n");
223 FILE *fp = fopen(argv[1], "r");
224 if(!(mykey = ecdsa_read_pem_private_key(fp)))
228 fp = fopen(argv[2], "r");
229 if(!(hiskey = ecdsa_read_pem_public_key(fp)))
233 fprintf(stderr, "Keys loaded\n");
236 if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
240 if(writeonly && readonly)
243 char buf[65535] = "";
248 if(!readonly && s.instate)
252 if(select(sock + 1, &fds, NULL, NULL, NULL) <= 0)
255 if(FD_ISSET(0, &fds)) {
256 ssize_t len = read(0, buf, sizeof buf);
257 fprintf(stderr, "%zd\n", len);
259 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
269 s.outseqno = atoi(buf + 1);
271 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
272 else if(buf[0] == '$') {
275 sptps_send_record(&s, 0, buf, len);
277 if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, buf[0] == '\n' ? 0 : buf[0] == '*' ? sizeof buf : len))
281 if(FD_ISSET(sock, &fds)) {
282 ssize_t len = recv(sock, buf, sizeof buf, 0);
284 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
288 fprintf(stderr, "Connection terminated by peer.\n");
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");
298 if(!sptps_receive_data(&s, buf, len) && !datagram)