2 sptps_test.c -- Simple Peer-to-Peer Security test program
3 Copyright (C) 2011 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.
28 ecdsa_t mykey, hiskey;
30 static bool send_data(void *handle, const char *data, size_t len) {
31 char hex[len * 2 + 1];
32 bin2hex(data, hex, len);
33 fprintf(stderr, "Sending %zu bytes of data:\n%s\n", len, hex);
34 const int *sock = handle;
35 if(send(*sock, data, len, 0) != len)
40 static bool receive_record(void *handle, uint8_t type, const char *data, uint16_t len) {
41 fprintf(stderr, "Received type %d record of %hu bytes:\n", type, len);
42 fwrite(data, len, 1, stdout);
46 int main(int argc, char *argv[]) {
47 bool initiator = false;
50 fprintf(stderr, "Usage: %s my_ecdsa_key_file his_ecdsa_key_file [host] port\n", argv[0]);
57 struct addrinfo *ai, hint;
58 memset(&hint, 0, sizeof hint);
60 hint.ai_family = AF_UNSPEC;
61 hint.ai_socktype = SOCK_STREAM;
62 hint.ai_protocol = IPPROTO_TCP;
63 hint.ai_flags = initiator ? 0 : AI_PASSIVE;
65 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
66 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
70 int sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
72 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
77 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
80 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
81 fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
84 fprintf(stderr, "Connected\n");
86 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
87 fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
91 fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
94 fprintf(stderr, "Listening...\n");
96 sock = accept(sock, NULL, NULL);
98 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
102 fprintf(stderr, "Connected\n");
107 FILE *fp = fopen(argv[1], "r");
108 if(!ecdsa_read_pem_private_key(&mykey, fp))
112 fp = fopen(argv[2], "r");
113 if(!ecdsa_read_pem_public_key(&hiskey, fp))
117 fprintf(stderr, "Keys loaded\n");
120 if(!start_sptps(&s, &sock, initiator, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
124 char buf[65535] = "";
126 struct pollfd fds[2];
128 fds[0].events = POLLIN;
130 fds[1].events = POLLIN;
131 if(poll(fds, 2, -1) < 0)
135 ssize_t len = read(0, buf, sizeof buf);
137 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
143 send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
144 else if(buf[0] == '$')
147 if(!send_record(&s, buf[0] == '!' ? 1 : 0, buf, buf[0] == '\n' ? 0 : buf[0] == '*' ? sizeof buf : len))
152 ssize_t len = recv(sock, buf, sizeof buf, 0);
154 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
158 fprintf(stderr, "Connection terminated by peer.\n");
161 char hex[len * 2 + 1];
162 bin2hex(buf, hex, len);
163 fprintf(stderr, "Received %zd bytes of data:\n%s\n", len, hex);
164 if(!receive_data(&s, buf, len))