-
Notifications
You must be signed in to change notification settings - Fork 2
/
tconn_connect_one.c
387 lines (313 loc) · 8.95 KB
/
tconn_connect_one.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* dvpn, a multipoint vpn implementation
* Copyright (C) 2015, 2016 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <iv.h>
#include <netinet/tcp.h>
#include <string.h>
#include "conf.h"
#include "tconn.h"
#include "tconn_connect_one.h"
#include "util.h"
#include "x509.h"
#define STATE_CONNECT 1
#define STATE_TLS_HANDSHAKE 2
#define STATE_CONNECTED 3
#define STATE_FAILED 4
#define CONNECT_TIMEOUT 10
#define HANDSHAKE_TIMEOUT 15
#define KEEPALIVE_INTERVAL 15
#define KEEPALIVE_TIMEOUT 20
static void state_destroy(struct tconn_connect_one *tco)
{
if (iv_timer_registered(&tco->rx_timeout))
iv_timer_unregister(&tco->rx_timeout);
if (tco->state == STATE_TLS_HANDSHAKE ||
tco->state == STATE_CONNECTED) {
tconn_destroy(&tco->tconn);
}
if (tco->state == STATE_CONNECT ||
tco->state == STATE_TLS_HANDSHAKE ||
tco->state == STATE_CONNECTED) {
iv_fd_unregister(&tco->fd);
close(tco->fd.fd);
}
if (tco->state == STATE_CONNECTED)
iv_timer_unregister(&tco->keepalive_timer);
}
static void connection_failed(struct tconn_connect_one *tco, int notify_err)
{
state_destroy(tco);
tco->state = STATE_FAILED;
if (notify_err)
tco->connection_failed(tco->cookie);
}
static void rx_timeout_expired(void *_tco)
{
struct tconn_connect_one *tco = _tco;
switch (tco->state) {
case STATE_CONNECT:
fprintf(stderr, "%s: connect timed out\n", tco->name);
break;
case STATE_TLS_HANDSHAKE:
fprintf(stderr, "%s: TLS handshake timed out\n", tco->name);
break;
case STATE_CONNECTED:
fprintf(stderr, "%s: receive timeout\n", tco->name);
break;
}
connection_failed(tco, 1);
}
static int verify_key_ids(void *_tco, const uint8_t *ids, int num)
{
struct tconn_connect_one *tco = _tco;
int i;
fprintf(stderr, "%s: peer key ID ", tco->name);
print_fingerprint(stderr, ids);
if (tco->cnameid != NULL) {
for (i = 0; i < num; i++) {
const uint8_t *id;
id = ids + (i * NODE_ID_LEN);
if (!memcmp(tco->cnameid, id, NODE_ID_LEN))
break;
}
if (i == num) {
fprintf(stderr, " - does not match its CNAME\n");
return 1;
}
if (tco->fp_type == CONF_FP_TYPE_ANY ||
tco->fp_type == CONF_FP_TYPE_CNAME) {
fprintf(stderr, " - have CNAME match\n");
return 0;
}
}
if (tco->fp_type == CONF_FP_TYPE_ANY) {
fprintf(stderr, " - matches 'any'\n");
return 0;
}
if (tco->fp_type == CONF_FP_TYPE_CNAME) {
fprintf(stderr, " - don't have CNAME match\n");
return 1;
}
for (i = 0; i < num; i++) {
const uint8_t *id;
id = ids + (i * NODE_ID_LEN);
if (!memcmp(tco->fingerprint, id, NODE_ID_LEN)) {
fprintf(stderr, " - OK%s\n",
(i != 0) ? " (via role certificate)" : "");
memcpy(tco->id, ids, NODE_ID_LEN);
return 0;
}
}
fprintf(stderr, " - no matches\n");
return 1;
}
static void send_keepalive(void *_tco)
{
static uint8_t keepalive[] = { 0x00, 0x00, 0x00 };
struct tconn_connect_one *tco = _tco;
if (tco->state != STATE_CONNECTED)
abort();
timespec_add_ms(&tco->keepalive_timer.expires,
900 * KEEPALIVE_INTERVAL, 1100 * KEEPALIVE_INTERVAL);
iv_timer_register(&tco->keepalive_timer);
if (tconn_record_send(&tco->tconn, keepalive, 3)) {
fprintf(stderr, "%s: error sending keepalive, disconnecting\n",
tco->name);
connection_failed(tco, 1);
}
}
static void handshake_done(void *_tco, char *desc)
{
struct tconn_connect_one *tco = _tco;
fprintf(stderr, "%s: handshake done, using %s\n", tco->name, desc);
tco->state = STATE_CONNECTED;
iv_validate_now();
iv_timer_unregister(&tco->rx_timeout);
tco->rx_timeout.expires = iv_now;
timespec_add_ms(&tco->rx_timeout.expires,
1000 * KEEPALIVE_TIMEOUT, 1000 * KEEPALIVE_TIMEOUT);
iv_timer_register(&tco->rx_timeout);
IV_TIMER_INIT(&tco->keepalive_timer);
tco->keepalive_timer.expires = iv_now;
timespec_add_ms(&tco->keepalive_timer.expires,
900 * KEEPALIVE_INTERVAL, 1100 * KEEPALIVE_INTERVAL);
tco->keepalive_timer.cookie = tco;
tco->keepalive_timer.handler = send_keepalive;
iv_timer_register(&tco->keepalive_timer);
tco->connected(tco->cookie, tco->id);
}
static void record_received(void *_tco, const uint8_t *rec, int len)
{
struct tconn_connect_one *tco = _tco;
iv_timer_unregister(&tco->rx_timeout);
iv_validate_now();
tco->rx_timeout.expires = iv_now;
timespec_add_ms(&tco->rx_timeout.expires,
1000 * KEEPALIVE_TIMEOUT, 1000 * KEEPALIVE_TIMEOUT);
iv_timer_register(&tco->rx_timeout);
tco->record_received(tco->cookie, rec, len);
}
static void connection_lost(void *_tco)
{
struct tconn_connect_one *tco = _tco;
connection_failed(tco, 1);
}
static int start_handshake(struct tconn_connect_one *tco)
{
fprintf(stderr, "%s: connection established, starting TLS handshake\n",
tco->name);
tco->tconn.name = tco->name;
tco->tconn.fd = &tco->fd;
tco->tconn.role = TCONN_ROLE_CLIENT;
tco->tconn.mykey = tco->mykey;
tco->tconn.numcrts = tco->numcrts;
tco->tconn.mycrts = tco->mycrts;
tco->tconn.cookie = tco;
tco->tconn.verify_key_ids = verify_key_ids;
tco->tconn.handshake_done = handshake_done;
tco->tconn.record_received = record_received;
tco->tconn.connection_lost = connection_lost;
if (tconn_start(&tco->tconn) < 0)
return -1;
tco->state = STATE_TLS_HANDSHAKE;
iv_validate_now();
tco->rx_timeout.expires = iv_now;
timespec_add_ms(&tco->rx_timeout.expires,
1000 * HANDSHAKE_TIMEOUT, 1000 * HANDSHAKE_TIMEOUT);
iv_timer_register(&tco->rx_timeout);
return 0;
}
static void connect_pollout(void *_tco)
{
struct tconn_connect_one *tco = _tco;
socklen_t len;
int ret;
len = sizeof(ret);
if (getsockopt(tco->fd.fd, SOL_SOCKET, SO_ERROR, &ret, &len) < 0) {
fprintf(stderr, "%s: getsockopt error '%s'\n",
tco->name, strerror(errno));
return;
}
if (ret == EINPROGRESS)
return;
iv_timer_unregister(&tco->rx_timeout);
if (ret) {
fprintf(stderr, "%s: connect error '%s'\n",
tco->name, strerror(ret));
connection_failed(tco, 1);
return;
}
if (start_handshake(tco))
connection_failed(tco, 1);
}
int tconn_connect_one_connect(struct tconn_connect_one *tco)
{
int fd;
int ret;
tco->state = STATE_CONNECT;
IV_TIMER_INIT(&tco->rx_timeout);
tco->rx_timeout.cookie = tco;
tco->rx_timeout.handler = rx_timeout_expired;
fd = socket(tco->addr->sa_family, SOCK_STREAM, 0);
if (fd < 0)
return -1;
IV_FD_INIT(&tco->fd);
tco->fd.cookie = tco;
tco->fd.handler_out = connect_pollout;
tco->fd.fd = fd;
iv_fd_register(&tco->fd);
ret = connect(fd, tco->addr, tco->addrlen);
if (ret < 0 && errno != EINPROGRESS) {
fprintf(stderr, "%s: connect error '%s'\n",
tco->name, strerror(errno));
iv_fd_unregister(&tco->fd);
close(fd);
return -1;
}
if (ret == 0) {
if (start_handshake(tco)) {
iv_fd_unregister(&tco->fd);
close(fd);
return -1;
}
return 0;
}
iv_validate_now();
tco->rx_timeout.expires = iv_now;
timespec_add_ms(&tco->rx_timeout.expires,
1000 * CONNECT_TIMEOUT, 1000 * CONNECT_TIMEOUT);
iv_timer_register(&tco->rx_timeout);
return 0;
}
void tconn_connect_one_disconnect(struct tconn_connect_one *tco)
{
state_destroy(tco);
}
int tconn_connect_one_get_rtt(struct tconn_connect_one *tco)
{
struct tcp_info info;
socklen_t len;
if (tco->state != STATE_CONNECTED)
return -1;
len = sizeof(info);
if (getsockopt(tco->fd.fd, SOL_TCP, TCP_INFO, &info, &len) < 0) {
perror("getsockopt(SOL_TCP, TCP_INFO)");
return -1;
}
return (info.tcpi_rtt + 999) / 1000;
}
int tconn_connect_one_get_maxseg(struct tconn_connect_one *tco)
{
int mseg;
socklen_t len;
if (tco->state != STATE_CONNECTED)
return -1;
len = sizeof(mseg);
if (getsockopt(tco->fd.fd, SOL_TCP, TCP_MAXSEG, &mseg, &len) < 0) {
perror("getsockopt(SOL_TCP, TCP_MAXSEG)");
return -1;
}
return mseg;
}
int tconn_connect_one_record_send(struct tconn_connect_one *tco,
const uint8_t *rec, int len)
{
if (tco->state != STATE_CONNECTED)
return 0;
iv_timer_unregister(&tco->keepalive_timer);
iv_validate_now();
tco->keepalive_timer.expires = iv_now;
timespec_add_ms(&tco->keepalive_timer.expires,
900 * KEEPALIVE_INTERVAL, 1100 * KEEPALIVE_INTERVAL);
iv_timer_register(&tco->keepalive_timer);
if (tconn_record_send(&tco->tconn, rec, len)) {
fprintf(stderr, "%s: error sending TLS record, disconnecting\n",
tco->name);
connection_failed(tco, 0);
return -1;
}
return 0;
}