Allow log messages to be captured by tincctl.
[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 #include "poll.h"
22
23 #include "crypto.h"
24 #include "ecdsa.h"
25 #include "sptps.h"
26 #include "utils.h"
27
28 // Symbols necessary to link with logger.o
29 char *logfilename;
30 char *connection_tree;
31 char *send_request;
32 char *send_meta;
33
34 ecdsa_t mykey, hiskey;
35
36 static bool send_data(void *handle, const char *data, size_t len) {
37         char hex[len * 2 + 1];
38         bin2hex(data, hex, len);
39         fprintf(stderr, "Sending %zu bytes of data:\n%s\n", 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
55         if(argc < 3) {
56                 fprintf(stderr, "Usage: %s my_ecdsa_key_file his_ecdsa_key_file [host] port\n", argv[0]);
57                 return 1;
58         }
59
60         if(argc > 4)
61                 initiator = true;
62
63         struct addrinfo *ai, hint;
64         memset(&hint, 0, sizeof hint);
65
66         hint.ai_family = AF_UNSPEC;
67         hint.ai_socktype = SOCK_STREAM;
68         hint.ai_protocol = IPPROTO_TCP;
69         hint.ai_flags = initiator ? 0 : AI_PASSIVE;
70         
71         if(getaddrinfo(initiator ? argv[3] : NULL, initiator ? argv[4] : argv[3], &hint, &ai) || !ai) {
72                 fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
73                 return 1;
74         }
75
76         int sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
77         if(sock < 0) {
78                 fprintf(stderr, "Could not create socket: %s\n", strerror(errno));
79                 return 1;
80         }
81
82         int one = 1;
83         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
84
85         if(initiator) {
86                 if(connect(sock, ai->ai_addr, ai->ai_addrlen)) {
87                         fprintf(stderr, "Could not connect to peer: %s\n", strerror(errno));
88                         return 1;
89                 }
90                 fprintf(stderr, "Connected\n");
91         } else {
92                 if(bind(sock, ai->ai_addr, ai->ai_addrlen)) {
93                         fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
94                         return 1;
95                 }
96                 if(listen(sock, 1)) {
97                         fprintf(stderr, "Could not listen on socket: %s\n", strerror(errno));
98                         return 1;
99                 }
100                 fprintf(stderr, "Listening...\n");
101
102                 sock = accept(sock, NULL, NULL);
103                 if(sock < 0) {
104                         fprintf(stderr, "Could not accept connection: %s\n", strerror(errno));
105                         return 1;
106                 }
107
108                 fprintf(stderr, "Connected\n");
109         }
110
111         crypto_init();
112
113         FILE *fp = fopen(argv[1], "r");
114         if(!ecdsa_read_pem_private_key(&mykey, fp))
115                 return 1;
116         fclose(fp);
117
118         fp = fopen(argv[2], "r");
119         if(!ecdsa_read_pem_public_key(&hiskey, fp))
120                 return 1;
121         fclose(fp);
122
123         fprintf(stderr, "Keys loaded\n");
124
125         sptps_t s;
126         if(!sptps_start(&s, &sock, initiator, mykey, hiskey, "sptps_test", 10, send_data, receive_record))
127                 return 1;
128
129         while(true) {
130                 char buf[65535] = "";
131
132                 struct pollfd fds[2];
133                 fds[0].fd = 0;
134                 fds[0].events = POLLIN;
135                 fds[1].fd = sock;
136                 fds[1].events = POLLIN;
137                 if(poll(fds, 2, -1) < 0)
138                         return 1;
139
140                 if(fds[0].revents) {
141                         ssize_t len = read(0, buf, sizeof buf);
142                         if(len < 0) {
143                                 fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno));
144                                 return 1;
145                         }
146                         if(len == 0)
147                                 break;
148                         if(buf[0] == '^')
149                                 sptps_send_record(&s, SPTPS_HANDSHAKE, NULL, 0);
150                         else if(buf[0] == '$')
151                                 sptps_force_kex(&s);
152                         else
153                         if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, buf[0] == '\n' ? 0 : buf[0] == '*' ? sizeof buf : len))
154                                 return 1;
155                 }
156
157                 if(fds[1].revents) {
158                         ssize_t len = recv(sock, buf, sizeof buf, 0);
159                         if(len < 0) {
160                                 fprintf(stderr, "Could not read from socket: %s\n", strerror(errno));
161                                 return 1;
162                         }
163                         if(len == 0) {
164                                 fprintf(stderr, "Connection terminated by peer.\n");
165                                 break;
166                         }
167                         char hex[len * 2 + 1];
168                         bin2hex(buf, hex, len);
169                         fprintf(stderr, "Received %zd bytes of data:\n%s\n", len, hex);
170                         if(!sptps_receive_data(&s, buf, len))
171                                 return 1;
172                 }
173         }
174
175         if(!sptps_stop(&s))
176                 return 1;
177
178         return 0;
179 }