Drop libevent and use our own event handling again.
[tinc] / src / sptps_test.c
1 /*
2     sptps_test.c -- Simple Peer-to-Peer Security test program
3     Copyright (C) 2011-2012 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 "crypto.h"
23 #include "ecdsa.h"
24 #include "sptps.h"
25 #include "utils.h"
26
27 // Symbols necessary to link with logger.o
28 bool send_request(void *c, const char *msg, ...) { return false; }
29 struct list_t *connection_list = NULL;
30 bool send_meta(void *c, const char *msg , int len) { return false; }
31 char *logfilename = NULL;
32 struct timeval now;
33
34 ecdsa_t mykey, hiskey;
35
36 static bool send_data(void *handle, uint8_t type, const char *data, size_t len) {
37         char hex[len * 2 + 1];
38         bin2hex(data, hex, len);
39         fprintf(stderr, "Sending %d bytes of data:\n%s\n", (int)len, hex);
40         const int *sock = handle;
41         if(send(*sock, data, len, 0) != len)
42                 return false;
43         return true;
44 }
45
46 static bool receive_record(void *handle, uint8_t type, const char *data, uint16_t len) {
47         fprintf(stderr, "Received type %d record of %hu bytes:\n", type, len);
48         fwrite(data, len, 1, stdout);
49         return true;
50 }
51
52 int main(int argc, char *argv[]) {
53         bool initiator = false;
54         bool datagram = false;
55
56         if(argc > 1 && !strcmp(argv[1], "-d")) {
57                 datagram = true;
58                 argc--;
59                 argv++;
60         }
61
62         if(argc < 4) {
63                 fprintf(stderr, "Usage: %s [-d] my_ecdsa_key_file his_ecdsa_key_file [host] port\n", argv[0]);
64                 return 1;
65         }
66
67         if(argc > 4)
68                 initiator = true;
69
70 #ifdef HAVE_MINGW
71         static struct WSAData wsa_state;
72         if(WSAStartup(MAKEWORD(2, 2), &wsa_state))
73                 return 1;
74 #endif
75
76         struct addrinfo *ai, hint;
77         memset(&hint, 0, sizeof hint);
78
79         hint.ai_family = AF_UNSPEC;
80         hint.ai_socktype = SOCK_STREAM;
81         hint.ai_protocol = IPPROTO_TCP;
82         hint.ai_flags = initiator ? 0 : AI_PASSIVE;
83
84         if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
85                 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
86                 return 1;
87         }
88
89         int sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
90         if(sock < 0) {
91                 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
92                 return 1;
93         }
94
95         int one = 1;
96         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
97
98         if(initiator) {
99                 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
100                         fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
101                         return 1;
102                 }
103                 fprintf(stderr, "Connected\n");
104         } else {
105                 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
106                         fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
107                         return 1;
108                 }
109                 if(listen(sock, 1)) {
110                         fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
111                         return 1;
112                 }
113                 fprintf(stderr, "Listening...\n");
114
115                 sock = accept(sock, NULL, NULL);
116                 if(sock < 0) {
117                         fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
118                         return 1;
119                 }
120
121                 fprintf(stderr, "Connected\n");
122         }
123
124         crypto_init();
125
126         FILE *fp = fopen(argv[1], "r");
127         if(!ecdsa_read_pem_private_key(&mykey, fp))
128                 return 1;
129         fclose(fp);
130
131         fp = fopen(argv[2], "r");
132         if(!ecdsa_read_pem_public_key(&hiskey, fp))
133                 return 1;
134         fclose(fp);
135
136         fprintf(stderr, "Keys loaded\n");
137
138         sptps_t s;
139         if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
140                 return 1;
141
142         while(true) {
143                 char buf[65535] = "";
144
145                 fd_set fds;
146                 FD_ZERO(&fds);
147 #ifndef HAVE_MINGW
148                 FD_SET(0, &fds);
149 #endif
150                 FD_SET(sock, &fds);
151                 if(select(sock + 1, &fds, NULL, NULL, NULL) <= 0)
152                         return 1;
153
154                 if(FD_ISSET(0, &fds)) {
155                         ssize_t len = read(0, buf, sizeof buf);
156                         if(len < 0) {
157                                 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
158                                 return 1;
159                         }
160                         if(len == 0)
161                                 break;
162                         if(buf[0] == '^')
163                                 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
164                         else if(buf[0] == '$') {
165                                 sptps_force_kex(&s);
166                                 if(len > 1)
167                                         sptps_send_record(&s, 0, buf, len);
168                         } else
169                         if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, buf[0] == '\n' ? 0 : buf[0] == '*' ? sizeof buf : len))
170                                 return 1;
171                 }
172
173                 if(FD_ISSET(sock, &fds)) {
174                         ssize_t len = recv(sock, buf, sizeof buf, 0);
175                         if(len < 0) {
176                                 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
177                                 return 1;
178                         }
179                         if(len == 0) {
180                                 fprintf(stderr, "Connection terminated by peer.\n");
181                                 break;
182                         }
183                         char hex[len * 2 + 1];
184                         bin2hex(buf, hex, len);
185                         fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex);
186                         if(!sptps_receive_data(&s, buf, len))
187                                 return 1;
188                 }
189         }
190
191         if(!sptps_stop(&s))
192                 return 1;
193
194         return 0;
195 }