-
Notifications
You must be signed in to change notification settings - Fork 58
/
client.c
714 lines (622 loc) · 24.7 KB
/
client.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#include <time.h>
/* MacOS related */
#ifdef __MACH__
#include "mach.h"
#endif
#include "log.h"
#include "main.h"
#include "client.h"
/* The state machine */
int state = CLIENT_STATE_INITIAL;
/* Used in ping mode */
struct timespec ping_sent_time;
fd_set client_master_fdset;
int handle_pong_frame()
{
struct timespec pong_rcvd_time;
double secs1, secs2;
clock_gettime(CLOCK_MONOTONIC, &pong_rcvd_time);
secs1 = (1.0 * ping_sent_time.tv_sec) + (1e-9 * ping_sent_time.tv_nsec);
secs2 = (1.0 * pong_rcvd_time.tv_sec) + (1e-9 * pong_rcvd_time.tv_nsec);
log_printf(L_INFO, "GOT PONG! Time = %.3fs\n", secs2-secs1);
if(ping_mode)
{
state = CLIENT_STATE_SEND_PING;
}
return 0;
}
int local_bind_one(local_port_forward *port_forward)
{
struct addrinfo hints, *res;
char port[6];
int yes = 1;
int flags;
int gai_status;
int setsockopt_status;
snprintf(port, 6, "%d", port_forward->local_port);
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
gai_status = getaddrinfo(NULL, port, &hints, &res);
if(gai_status != 0)
{
log_printf(L_ERROR, "getaddrinfo: %s\n", gai_strerror(gai_status));
exit(1);
}
port_forward->bind_sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(port_forward->bind_sockfd < 0)
{
log_printf(L_ERROR, "Could not create a socket for local listening: %s\n", strerror(errno));
freeaddrinfo(res);
exit(1);
}
setsockopt_status = setsockopt(port_forward->bind_sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if(setsockopt_status < 0)
{
log_printf(L_ERROR, "Could not set socket options: %s\n",
strerror(errno));
freeaddrinfo(res);
exit(1);
}
/* Set O_NONBLOCK to make accept() non-blocking */
if (-1 == (flags = fcntl(port_forward->bind_sockfd, F_GETFL, 0)))
{
flags = 0;
}
if(fcntl(port_forward->bind_sockfd, F_SETFL, flags | O_NONBLOCK) < 0)
{
log_printf(L_ERROR, "Could not make the socket non-blocking: %s\n", strerror(errno));
freeaddrinfo(res);
exit(1);
}
if(bind(port_forward->bind_sockfd, res->ai_addr, res->ai_addrlen) < 0)
{
log_printf(L_ERROR, "Bind to port %d failed: %s\n", port_forward->local_port, strerror(errno));
freeaddrinfo(res);
close(port_forward->bind_sockfd);
exit(1);
}
freeaddrinfo(res);
if(listen(port_forward->bind_sockfd, 1) < 0)
{
log_printf(L_ERROR, "Listening on port %d failed: %s\n", port_forward->local_port, strerror(errno));
close(port_forward->bind_sockfd);
exit(1);
}
log_printf(L_DEBUG, "Bound to local port %d\n", port_forward->local_port);
return 0;
}
void local_bind() {
local_port_forward *port_forward;
LL_FOREACH(local_port_forwards, port_forward)
{
local_bind_one(port_forward);
}
}
/* Bind the client.sockfd to a tunnel */
int handle_acktunnel_frame(protocol_frame *rcvd_frame)
{
uint32_t local_forward_id;
local_port_forward *forward;
tunnel *tun;
if(!client_mode)
{
log_printf(L_WARNING, "Got ACK tunnel frame when not in client mode!?\n");
return -1;
}
if(rcvd_frame->data_length < 3)
{
log_printf(L_WARNING, "Got ACK tunnel frame with not enough data");
return -1;
}
if(rcvd_frame->data_length > PROTOCOL_MAX_PACKET_SIZE) {
log_printf(L_WARNING, "Got ACK tunnel with wrong data length");
return -1;
}
local_forward_id = INT32_AT((rcvd_frame->data), 0);
log_printf(L_DEBUG2, "Got ACK tunnel frame for local forward %ld", local_forward_id);
forward = find_pending_forward_by_id(local_forward_id);
if(!forward)
{
log_printf(L_WARNING, "Got ACK tunnel with wrong forward ID %ld", local_forward_id);
return -1;
}
tun = tunnel_create(
forward->accept_sockfd, /* sockfd */
rcvd_frame->connid,
rcvd_frame->friendnumber
);
/* Mark that we can accept() another connection */
forward->accept_sockfd = -1;
// printf("New tunnel ID: %d\n", tun->connid);
if(client_local_port_mode || client_pipe_mode)
{
update_select_nfds(tun->sockfd);
FD_SET(tun->sockfd, &client_master_fdset);
if(client_local_port_mode)
{
log_printf(L_INFO, "Accepted a new connection on port %d sockfd %d\n", forward->local_port);
}
}
else
{
log_printf(L_ERROR, "This tunnel mode is not supported yet\n");
exit(1);
}
return 0;
}
/* Handle a TCP frame received from server */
int handle_server_tcp_frame(protocol_frame *rcvd_frame)
{
int offset = 0;
tunnel *tun = NULL;
int tun_id = rcvd_frame->connid;
HASH_FIND_INT(by_id, &tun_id, tun);
if(!tun)
{
log_printf(L_WARNING, "Got TCP frame with unknown tunnel ID %d\n", rcvd_frame->connid);
return -1;
}
while(offset < rcvd_frame->data_length)
{
int sent_bytes;
if(client_pipe_mode)
{
sent_bytes = write(
1, /* STDOUT */
rcvd_frame->data + offset,
rcvd_frame->data_length - offset
);
}
else
{
sent_bytes = send(
tun->sockfd,
rcvd_frame->data + offset,
rcvd_frame->data_length - offset,
MSG_NOSIGNAL
);
}
if(sent_bytes < 0)
{
uint8_t data[PROTOCOL_BUFFER_OFFSET];
protocol_frame frame_st, *frame;
log_printf(L_INFO, "Could not write to socket: %s\n", strerror(errno));
frame = &frame_st;
memset(frame, 0, sizeof(protocol_frame));
frame->friendnumber = tun->friendnumber;
frame->packet_type = PACKET_TYPE_TCP_FIN;
frame->connid = tun->connid;
frame->data_length = 0;
send_frame(frame, data);
if(tun->sockfd)
{
FD_CLR(tun->sockfd, &client_master_fdset);
}
tunnel_delete(tun);
return -1;
}
offset += sent_bytes;
}
// printf("Got %d bytes from server - wrote to fd %d\n", rcvd_frame->data_length, tun->sockfd);
return 0;
}
/* Delete tunnel and clear client-side fdset */
void client_close_tunnel(tunnel *tun)
{
if(tun->sockfd)
{
FD_CLR(tun->sockfd, &client_master_fdset);
}
tunnel_delete(tun);
}
/* Handle close-tunnel frame recived from the server */
int handle_server_tcp_fin_frame(protocol_frame *rcvd_frame)
{
tunnel *tun=NULL;
int connid = rcvd_frame->connid;
HASH_FIND_INT(by_id, &connid, tun);
if(!tun)
{
log_printf(L_WARNING, "Got TCP FIN frame with unknown tunnel ID %d\n", rcvd_frame->connid);
return -1;
}
if(tun->friendnumber != rcvd_frame->friendnumber)
{
log_printf(L_WARNING, "Friend #%d tried to close tunnel while server is #%d\n", rcvd_frame->friendnumber, tun->friendnumber);
return -1;
}
client_close_tunnel(tun);
return 0;
}
/* Close and delete all tunnels (when server went offline) */
void client_close_all_connections()
{
tunnel *tmp = NULL;
tunnel *tun = NULL;
HASH_ITER(hh, by_id, tun, tmp)
{
client_close_tunnel(tun);
}
}
void on_friend_connection_status_changed(Tox *tox, uint32_t friend_number, Tox_Connection connection_status,
void *user_data)
{
const char* status = readable_connection_status(connection_status);
log_printf(L_INFO, "Friend connection status changed to: %s (%d)\n", status, connection_status);
if(connection_status == TOX_CONNECTION_NONE)
{
state = CLIENT_STATE_CONNECTION_LOST;
}
}
/* Main loop for the client */
int do_client_loop(uint8_t *tox_id_str)
{
unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE];
unsigned char tox_id[TOX_ADDRESS_SIZE];
uint32_t friendnumber = 0;
struct timeval tv;
fd_set fds;
static time_t invitation_sent_time = 0;
uint32_t invitations_sent = 0;
TOX_ERR_FRIEND_QUERY friend_query_error;
TOX_ERR_FRIEND_CUSTOM_PACKET custom_packet_error;
local_port_forward *port_forward;
FD_ZERO(&client_master_fdset);
tox_callback_friend_lossless_packet(tox, parse_lossless_packet);
tox_callback_friend_connection_status(tox, on_friend_connection_status_changed);
if(!string_to_id(tox_id, tox_id_str))
{
log_printf(L_ERROR, "Invalid Tox ID");
exit(1);
}
if(!ping_mode && !client_pipe_mode)
{
local_bind();
signal(SIGPIPE, SIG_IGN);
}
log_printf(L_INFO, "Connecting to Tox...\n");
while(1)
{
/* Let tox do its stuff */
tox_iterate(tox, NULL);
switch(state)
{
/*
* Send friend request
*/
case CLIENT_STATE_INITIAL:
if(connection_status != TOX_CONNECTION_NONE)
{
state = CLIENT_STATE_CONNECTED;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_CONNECTED");
}
break;
case CLIENT_STATE_CONNECTED:
{
uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!";
uint16_t length = sizeof(data);
/* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1168 */
TOX_ERR_FRIEND_ADD add_error;
if(use_shared_secret)
{
data = (uint8_t *)shared_secret;
data[TOX_MAX_FRIEND_REQUEST_LENGTH-1] = '\0';
length = strlen((char *)data)+1;
log_printf(L_DEBUG, "Sent shared secret of length %u\n", length);
}
if(invitations_sent == 0)
{
log_printf(L_INFO, "Connected. Sending friend request.\n");
}
else
{
log_printf(L_INFO, "Sending another friend request.\n");
}
friendnumber = tox_friend_add(
tox,
tox_id,
data,
length,
&add_error
);
if(add_error != TOX_ERR_FRIEND_ADD_OK)
{
unsigned char tox_printable_id[TOX_ADDRESS_SIZE * 2 + 1];
id_to_string(tox_printable_id, tox_id);
log_printf(L_ERROR, "Error %u adding friend %s\n", add_error, tox_printable_id);
exit(-1);
}
invitation_sent_time = time(NULL);
invitations_sent++;
state = CLIENT_STATE_SENTREQUEST;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_SENTREQUEST");
log_printf(L_INFO, "Waiting for friend to accept us...\n");
}
break;
case CLIENT_STATE_SENTREQUEST:
{
TOX_CONNECTION friend_connection_status;
friend_connection_status = tox_friend_get_connection_status(tox, friendnumber, &friend_query_error);
if(friend_query_error != TOX_ERR_FRIEND_QUERY_OK)
{
log_printf(L_DEBUG, "tox_friend_get_connection_status: error %u", friend_query_error);
}
else
{
if(friend_connection_status != TOX_CONNECTION_NONE)
{
const char* status = readable_connection_status(friend_connection_status);
log_printf(L_INFO, "Friend request accepted (%s)!\n", status);
state = CLIENT_STATE_REQUEST_ACCEPTED;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_REQUEST_ACCEPTED");
}
else
{
if(1 && (time(NULL) - invitation_sent_time > 45))
{
TOX_ERR_FRIEND_DELETE error = 0;
log_printf(L_INFO, "Sending another friend request...");
tox_friend_delete(
tox,
friendnumber,
&error);
if(error != TOX_ERR_FRIEND_DELETE_OK)
{
log_printf(L_ERROR, "Error %u deleting friend before reconnection\n", error);
exit(-1);
}
state = CLIENT_STATE_CONNECTED;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_CONNECTED");
}
}
}
break;
}
case CLIENT_STATE_REQUEST_ACCEPTED:
if(ping_mode)
{
state = CLIENT_STATE_SEND_PING;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_SEND_PING");
}
else if(client_pipe_mode)
{
state = CLIENT_STATE_SETUP_PIPE;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_SETUP_PIPE");
}
else
{
state = CLIENT_STATE_BIND_PORT;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_BIND_PORT");
}
break;
case CLIENT_STATE_SEND_PING:
/* Send the ping packet */
{
uint8_t data[] = {
0xa2, 0x6b, 0x01, 0x08, 0x00, 0x00, 0x00, 0x05,
0x48, 0x65, 0x6c, 0x6c, 0x6f
};
clock_gettime(CLOCK_MONOTONIC, &ping_sent_time);
tox_friend_send_lossless_packet(
tox,
friendnumber,
data,
sizeof(data),
&custom_packet_error
);
}
if(custom_packet_error == TOX_ERR_FRIEND_CUSTOM_PACKET_OK)
{
state = CLIENT_STATE_PING_SENT;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_PING_SENT");
}
else
{
log_printf(L_WARNING, "When sending ping packet: %u", custom_packet_error);
}
break;
case CLIENT_STATE_PING_SENT:
/* Just sit there and wait for pong */
break;
case CLIENT_STATE_BIND_PORT:
LL_FOREACH(local_port_forwards, port_forward)
{
log_printf(L_DEBUG2, "Processing local port %d", port_forward->local_port);
if(port_forward->bind_sockfd < 0)
{
log_printf(L_ERROR, "Shutting down - could not bind to listening port %d\n", port_forward->local_port);
state = CLIENT_STATE_SHUTDOWN;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_SHUTDOWN");
break;
}
else
{
state = CLIENT_STATE_FORWARDING;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_FORWARDING");
}
}
break;
case CLIENT_STATE_SETUP_PIPE:
LL_FOREACH(local_port_forwards, port_forward)
{
send_tunnel_request_packet(
port_forward->remote_host,
port_forward->remote_port,
port_forward->forward_id,
friendnumber
);
}
state = CLIENT_STATE_FORWARDING;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_FORWARDING");
break;
case CLIENT_STATE_REQUEST_TUNNEL:
LL_FOREACH(local_port_forwards, port_forward)
{
send_tunnel_request_packet(
port_forward->remote_host,
port_forward->remote_port,
port_forward->forward_id,
friendnumber
);
}
state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL;
break;
case CLIENT_STATE_WAIT_FOR_ACKTUNNEL:
LL_FOREACH(local_port_forwards, port_forward)
{
port_forward->accept_sockfd = 0;
send_tunnel_request_packet(
port_forward->remote_host,
port_forward->remote_port,
port_forward->forward_id,
friendnumber
);
}
break;
case CLIENT_STATE_FORWARDING:
{
int accept_fd = 0;
int select_rv = 0;
tunnel *tmp = NULL;
tunnel *tun = NULL;
local_port_forward *port_forward;
tv.tv_sec = 0;
tv.tv_usec = 20000;
fds = client_master_fdset;
/* Handle accepting new connections */
LL_FOREACH(local_port_forwards, port_forward)
{
if(!client_pipe_mode &&
port_forward->accept_sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */
{
log_printf(L_DEBUG2, "FORWARDING: checking fd %d for local port %d", port_forward->bind_sockfd, port_forward->local_port);
accept_fd = accept(port_forward->bind_sockfd, NULL, NULL);
if(accept_fd != -1)
{
log_printf(L_INFO, "Accepting a new connection - requesting tunnel...\n");
/* Open a new tunnel for this FD */
port_forward->accept_sockfd = accept_fd;
send_tunnel_request_packet(
port_forward->remote_host,
port_forward->remote_port,
port_forward->forward_id,
friendnumber
);
}
}
}
/* Handle reading from sockets */
select_rv = select(select_nfds, &fds, NULL, NULL, &tv);
if(select_rv == -1 || select_rv == 0)
{
if(select_rv == -1)
{
log_printf(L_DEBUG, "Reading from local socket failed: code=%d (%s)\n",
errno, strerror(errno));
}
else
{
log_printf(L_DEBUG2, "Nothing to read...");
}
}
else
{
HASH_ITER(hh, by_id, tun, tmp)
{
if(FD_ISSET(tun->sockfd, &fds))
{
int nbytes;
if(client_local_port_mode)
{
nbytes = recv(tun->sockfd,
tox_packet_buf + PROTOCOL_BUFFER_OFFSET,
READ_BUFFER_SIZE, 0);
}
else
{
nbytes = read(tun->sockfd,
tox_packet_buf + PROTOCOL_BUFFER_OFFSET,
READ_BUFFER_SIZE
);
}
/* Check if connection closed */
if(nbytes == 0)
{
uint8_t data[PROTOCOL_BUFFER_OFFSET];
protocol_frame frame_st, *frame;
log_printf(L_INFO, "Connection closed\n");
frame = &frame_st;
memset(frame, 0, sizeof(protocol_frame));
frame->friendnumber = tun->friendnumber;
frame->packet_type = PACKET_TYPE_TCP_FIN;
frame->connid = tun->connid;
frame->data_length = 0;
send_frame(frame, data);
if(tun->sockfd)
{
FD_CLR(tun->sockfd, &client_master_fdset);
}
tunnel_delete(tun);
}
else
{
protocol_frame frame_st, *frame;
frame = &frame_st;
memset(frame, 0, sizeof(protocol_frame));
frame->friendnumber = tun->friendnumber;
frame->packet_type = PACKET_TYPE_TCP;
frame->connid = tun->connid;
frame->data_length = nbytes;
send_frame(frame, tox_packet_buf);
// printf("Wrote %d bytes from sock %d to tunnel %d\n", nbytes, tun->sockfd, tun->connid);
}
}
}
}
fds = client_master_fdset;
}
break;
case CLIENT_STATE_CONNECTION_LOST:
{
TOX_CONNECTION friend_connection_status;
friend_connection_status = tox_friend_get_connection_status(tox, friendnumber, &friend_query_error);
if(friend_query_error != TOX_ERR_FRIEND_QUERY_OK)
{
log_printf(L_DEBUG, "tox_friend_get_connection_status: error %u\n", friend_query_error);
}
else
{
if(friend_connection_status == TOX_CONNECTION_NONE)
{
/* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1267 */
TOX_ERR_FRIEND_DELETE tox_delete_error;
log_printf(L_WARNING, "Lost connection to server, closing all tunnels and re-adding friend\n");
client_close_all_connections();
tox_friend_delete(tox, friendnumber, &tox_delete_error);
if(tox_delete_error)
{
log_printf(L_ERROR, "Error when deleting server from friend list: %d\n", tox_delete_error);
}
state = CLIENT_STATE_INITIAL;
}
else
{
state = CLIENT_STATE_FORWARDING;
log_printf(L_DEBUG2, "Entered CLIENT_STATE_FORWARDING");
}
}
}
break;
case 0xffffffff:
log_printf(L_ERROR, "You forgot a break statement\n");
exit(0);
break;
case CLIENT_STATE_SHUTDOWN:
exit(0);
break;
}
usleep(tox_iteration_interval(tox) * 1000);
}
}