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