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