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.
29 ecdsa_t mykey, hiskey;
31 static bool send_data(void *handle, const char *data, size_t len) {
32 char hex[len * 2 + 1];
33 bin2hex(data, hex, len);
34 fprintf(stderr, "Sending %zu bytes of data:\n%s\n", len, hex);
35 const int *sock = handle;
36 if(send(*sock, data, len, 0) != len)
41 static bool receive_record(void *handle, uint8_t type, const char *data, uint16_t len) {
42 fprintf(stderr, "Received type %d record of %hu bytes:\n", type, len);
43 fwrite(data, len, 1, stdout);
47 int main(int argc, char *argv[]) {
48 bool initiator = false;
51 fprintf(stderr, "Usage: %s my_ecdsa_key_file his_ecdsa_key_file [host] port\n", argv[0]);
58 struct addrinfo *ai, hint;
59 memset(&hint, 0, sizeof hint);
61 hint.ai_family = AF_UNSPEC;
62 hint.ai_socktype = SOCK_STREAM;
63 hint.ai_protocol = IPPROTO_TCP;
64 hint.ai_flags = initiator ? 0 : AI_PASSIVE;
66 if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
67 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
71 int sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
73 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
78 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
81 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
82 fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
85 fprintf(stderr, "Connected\n");
87 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
88 fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
92 fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
95 fprintf(stderr, "Listening...\n");
97 sock = accept(sock, NULL, NULL);
99 fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
103 fprintf(stderr, "Connected\n");
108 FILE *fp = fopen(argv[1], "r");
109 if(!ecdsa_read_pem_private_key(&mykey, fp))
113 fp = fopen(argv[2], "r");
114 if(!ecdsa_read_pem_public_key(&hiskey, fp))
118 fprintf(stderr, "Keys loaded\n");
121 if(!sptps_start(&s, &sock, initiator, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
125 char buf[65535] = "";
127 struct pollfd fds[2];
129 fds[0].events = POLLIN;
131 fds[1].events = POLLIN;
132 if(poll(fds, 2, -1) < 0)
136 ssize_t len = read(0, buf, sizeof buf);
138 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
144 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
145 else if(buf[0] == '$')
148 if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, buf[0] == '\n' ? 0 : buf[0] == '*' ? sizeof buf : len))
153 ssize_t len = recv(sock, buf, sizeof buf, 0);
155 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
159 fprintf(stderr, "Connection terminated by peer.\n");
162 char hex[len * 2 + 1];
163 bin2hex(buf, hex, len);
164 fprintf(stderr, "Received %zd bytes of data:\n%s\n", len, hex);
165 if(!sptps_receive_data(&s, buf, len))