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.
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;
34 ecdsa_t *mykey, *hiskey;
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)
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);
52 int main(int argc, char *argv[]) {
53 bool initiator = false;
54 bool datagram = false;
56 if(argc > 1 && !strcmp(argv[1], "-d")) {
63 fprintf(stderr, "Usage: %s [-d] my_ecdsa_key_file his_ecdsa_key_file [host] port\n", argv[0]);
71 static struct WSAData wsa_state;
72 if(WSAStartup(MAKEWORD(2, 2), &wsa_state))
76 struct addrinfo *ai, hint;
77 memset(&hint, 0, sizeof hint);
79 hint.ai_family = AF_UNSPEC;
80 hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM;
81 hint.ai_protocol = datagram ? IPPROTO_UDP : IPPROTO_TCP;
82 hint.ai_flags = initiator ? 0 : AI_PASSIVE;
84 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
85 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
89 int sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
91 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
96 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
99 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
100 fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
103 fprintf(stderr, "Connected\n");
105 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
106 fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
111 if(listen(sock, 1)) {
112 fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
115 fprintf(stderr, "Listening...\n");
117 sock = accept(sock, NULL, NULL);
119 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
123 fprintf(stderr, "Listening...\n");
126 struct sockaddr addr;
127 socklen_t addrlen = sizeof addr;
129 if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) {
130 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
134 if(connect(sock, &addr, addrlen)) {
135 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
140 fprintf(stderr, "Connected\n");
145 FILE *fp = fopen(argv[1], "r");
146 if(!(mykey = ecdsa_read_pem_private_key(fp)))
150 fp = fopen(argv[2], "r");
151 if(!(hiskey = ecdsa_read_pem_public_key(fp)))
155 fprintf(stderr, "Keys loaded\n");
158 if(!sptps_start(&s, &sock, initiator, datagram, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
162 char buf[65535] = "";
170 if(select(sock + 1, &fds, NULL, NULL, NULL) <= 0)
173 if(FD_ISSET(0, &fds)) {
174 ssize_t len = read(0, buf, sizeof buf);
176 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
182 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
183 else if(buf[0] == '$') {
186 sptps_send_record(&s, 0, buf, len);
188 if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, buf[0] == '\n' ? 0 : buf[0] == '*' ? sizeof buf : len))
192 if(FD_ISSET(sock, &fds)) {
193 ssize_t len = recv(sock, buf, sizeof buf, 0);
195 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
199 fprintf(stderr, "Connection terminated by peer.\n");
202 char hex[len * 2 + 1];
203 bin2hex(buf, hex, len);
204 fprintf(stderr, "Received %d bytes of data:\n%s\n", (int)len, hex);
205 if(!sptps_receive_data(&s, buf, len))