File client.c changed (mode: 100644) (index e56cd90..5ef2980) |
... |
... |
int handle_acktunnel_frame(protocol_frame *rcvd_frame) |
124 |
124 |
{ |
{ |
125 |
125 |
uint32_t local_forward_id; |
uint32_t local_forward_id; |
126 |
126 |
local_port_forward *forward; |
local_port_forward *forward; |
|
127 |
|
tunnel *tun; |
127 |
128 |
|
|
128 |
129 |
if(!client_mode) |
if(!client_mode) |
129 |
130 |
{ |
{ |
|
... |
... |
int handle_acktunnel_frame(protocol_frame *rcvd_frame) |
144 |
145 |
|
|
145 |
146 |
local_forward_id = INT32_AT((rcvd_frame->data), 0); |
local_forward_id = INT32_AT((rcvd_frame->data), 0); |
146 |
147 |
|
|
|
148 |
|
log_printf(L_DEBUG2, "Got ACK tunnel frame for local forward %ld", local_forward_id); |
|
149 |
|
|
147 |
150 |
forward = find_pending_forward_by_id(local_forward_id); |
forward = find_pending_forward_by_id(local_forward_id); |
148 |
151 |
if(!forward) |
if(!forward) |
149 |
152 |
{ |
{ |
150 |
153 |
log_printf(L_WARNING, "Got ACK tunnel with wrong forward ID %ld", local_forward_id); |
log_printf(L_WARNING, "Got ACK tunnel with wrong forward ID %ld", local_forward_id); |
151 |
154 |
return -1; |
return -1; |
152 |
155 |
} |
} |
153 |
|
LL_DELETE(pending_port_forwards, forward); |
|
154 |
|
LL_APPEND(local_port_forwards, forward); |
|
155 |
156 |
|
|
156 |
|
forward->tun = tunnel_create( |
|
157 |
|
0, /* sockfd */ |
|
|
157 |
|
tun = tunnel_create( |
|
158 |
|
forward->accept_sockfd, /* sockfd */ |
158 |
159 |
rcvd_frame->connid, |
rcvd_frame->connid, |
159 |
160 |
rcvd_frame->friendnumber |
rcvd_frame->friendnumber |
160 |
161 |
); |
); |
161 |
162 |
|
|
162 |
163 |
/* Mark that we can accept() another connection */ |
/* Mark that we can accept() another connection */ |
163 |
|
forward->tun->sockfd = -1; |
|
|
164 |
|
forward->accept_sockfd = -1; |
164 |
165 |
|
|
165 |
166 |
// printf("New tunnel ID: %d\n", tun->connid); |
// printf("New tunnel ID: %d\n", tun->connid); |
166 |
167 |
|
|
167 |
168 |
if(client_local_port_mode || client_pipe_mode) |
if(client_local_port_mode || client_pipe_mode) |
168 |
169 |
{ |
{ |
169 |
|
update_select_nfds(forward->tun->sockfd); |
|
170 |
|
FD_SET(forward->tun->sockfd, &client_master_fdset); |
|
|
170 |
|
update_select_nfds(tun->sockfd); |
|
171 |
|
FD_SET(tun->sockfd, &client_master_fdset); |
171 |
172 |
if(client_local_port_mode) |
if(client_local_port_mode) |
172 |
173 |
{ |
{ |
173 |
|
log_printf(L_INFO, "Accepted a new connection on port %d\n", forward->local_port); |
|
|
174 |
|
log_printf(L_INFO, "Accepted a new connection on port %d sockfd %d\n", forward->local_port); |
174 |
175 |
} |
} |
175 |
176 |
} |
} |
176 |
177 |
else |
else |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
348 |
349 |
if(connection_status != TOX_CONNECTION_NONE) |
if(connection_status != TOX_CONNECTION_NONE) |
349 |
350 |
{ |
{ |
350 |
351 |
state = CLIENT_STATE_CONNECTED; |
state = CLIENT_STATE_CONNECTED; |
|
352 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_CONNECTED"); |
351 |
353 |
} |
} |
352 |
354 |
break; |
break; |
353 |
355 |
case CLIENT_STATE_CONNECTED: |
case CLIENT_STATE_CONNECTED: |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
393 |
395 |
invitation_sent_time = time(NULL); |
invitation_sent_time = time(NULL); |
394 |
396 |
invitations_sent++; |
invitations_sent++; |
395 |
397 |
state = CLIENT_STATE_SENTREQUEST; |
state = CLIENT_STATE_SENTREQUEST; |
|
398 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_SENTREQUEST"); |
396 |
399 |
log_printf(L_INFO, "Waiting for friend to accept us...\n"); |
log_printf(L_INFO, "Waiting for friend to accept us...\n"); |
397 |
400 |
} |
} |
398 |
401 |
break; |
break; |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
414 |
417 |
const char* status = readable_connection_status(friend_connection_status); |
const char* status = readable_connection_status(friend_connection_status); |
415 |
418 |
log_printf(L_INFO, "Friend request accepted (%s)!\n", status); |
log_printf(L_INFO, "Friend request accepted (%s)!\n", status); |
416 |
419 |
state = CLIENT_STATE_REQUEST_ACCEPTED; |
state = CLIENT_STATE_REQUEST_ACCEPTED; |
|
420 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_REQUEST_ACCEPTED"); |
417 |
421 |
} |
} |
418 |
422 |
else |
else |
419 |
423 |
{ |
{ |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
433 |
437 |
} |
} |
434 |
438 |
|
|
435 |
439 |
state = CLIENT_STATE_CONNECTED; |
state = CLIENT_STATE_CONNECTED; |
|
440 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_CONNECTED"); |
436 |
441 |
} |
} |
437 |
442 |
} |
} |
438 |
443 |
} |
} |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
442 |
447 |
if(ping_mode) |
if(ping_mode) |
443 |
448 |
{ |
{ |
444 |
449 |
state = CLIENT_STATE_SEND_PING; |
state = CLIENT_STATE_SEND_PING; |
|
450 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_SEND_PING"); |
445 |
451 |
} |
} |
446 |
452 |
else if(client_pipe_mode) |
else if(client_pipe_mode) |
447 |
453 |
{ |
{ |
448 |
454 |
state = CLIENT_STATE_SETUP_PIPE; |
state = CLIENT_STATE_SETUP_PIPE; |
|
455 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_SETUP_PIPE"); |
449 |
456 |
} |
} |
450 |
457 |
else |
else |
451 |
458 |
{ |
{ |
452 |
459 |
state = CLIENT_STATE_BIND_PORT; |
state = CLIENT_STATE_BIND_PORT; |
|
460 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_BIND_PORT"); |
453 |
461 |
} |
} |
454 |
462 |
break; |
break; |
455 |
463 |
case CLIENT_STATE_SEND_PING: |
case CLIENT_STATE_SEND_PING: |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
472 |
480 |
if(custom_packet_error == TOX_ERR_FRIEND_CUSTOM_PACKET_OK) |
if(custom_packet_error == TOX_ERR_FRIEND_CUSTOM_PACKET_OK) |
473 |
481 |
{ |
{ |
474 |
482 |
state = CLIENT_STATE_PING_SENT; |
state = CLIENT_STATE_PING_SENT; |
|
483 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_PING_SENT"); |
475 |
484 |
} |
} |
476 |
485 |
else |
else |
477 |
486 |
{ |
{ |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
485 |
494 |
case CLIENT_STATE_BIND_PORT: |
case CLIENT_STATE_BIND_PORT: |
486 |
495 |
LL_FOREACH(local_port_forwards, port_forward) |
LL_FOREACH(local_port_forwards, port_forward) |
487 |
496 |
{ |
{ |
|
497 |
|
log_printf(L_DEBUG2, "Processing local port %d", port_forward->local_port); |
488 |
498 |
if(port_forward->bind_sockfd < 0) |
if(port_forward->bind_sockfd < 0) |
489 |
499 |
{ |
{ |
490 |
500 |
log_printf(L_ERROR, "Shutting down - could not bind to listening port %d\n", port_forward->local_port); |
log_printf(L_ERROR, "Shutting down - could not bind to listening port %d\n", port_forward->local_port); |
491 |
501 |
state = CLIENT_STATE_SHUTDOWN; |
state = CLIENT_STATE_SHUTDOWN; |
|
502 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_SHUTDOWN"); |
492 |
503 |
break; |
break; |
493 |
504 |
} |
} |
494 |
505 |
else |
else |
495 |
506 |
{ |
{ |
496 |
507 |
state = CLIENT_STATE_FORWARDING; |
state = CLIENT_STATE_FORWARDING; |
|
508 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_FORWARDING"); |
497 |
509 |
} |
} |
498 |
510 |
} |
} |
499 |
511 |
break; |
break; |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
508 |
520 |
); |
); |
509 |
521 |
} |
} |
510 |
522 |
state = CLIENT_STATE_FORWARDING; |
state = CLIENT_STATE_FORWARDING; |
|
523 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_FORWARDING"); |
511 |
524 |
break; |
break; |
512 |
525 |
case CLIENT_STATE_REQUEST_TUNNEL: |
case CLIENT_STATE_REQUEST_TUNNEL: |
513 |
526 |
LL_FOREACH(local_port_forwards, port_forward) |
LL_FOREACH(local_port_forwards, port_forward) |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
522 |
535 |
state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL; |
state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL; |
523 |
536 |
break; |
break; |
524 |
537 |
case CLIENT_STATE_WAIT_FOR_ACKTUNNEL: |
case CLIENT_STATE_WAIT_FOR_ACKTUNNEL: |
525 |
|
/* TODO REMOTE_L */ |
|
526 |
538 |
LL_FOREACH(local_port_forwards, port_forward) |
LL_FOREACH(local_port_forwards, port_forward) |
527 |
539 |
{ |
{ |
528 |
|
port_forward->tun->sockfd = 0; |
|
|
540 |
|
port_forward->accept_sockfd = 0; |
529 |
541 |
send_tunnel_request_packet( |
send_tunnel_request_packet( |
530 |
542 |
port_forward->remote_host, |
port_forward->remote_host, |
531 |
543 |
port_forward->remote_port, |
port_forward->remote_port, |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
545 |
557 |
tv.tv_sec = 0; |
tv.tv_sec = 0; |
546 |
558 |
tv.tv_usec = 20000; |
tv.tv_usec = 20000; |
547 |
559 |
fds = client_master_fdset; |
fds = client_master_fdset; |
548 |
|
|
|
549 |
|
/* TODO MULTIPLE_L loop over tunnels and sockfds */ |
|
|
560 |
|
|
550 |
561 |
/* Handle accepting new connections */ |
/* Handle accepting new connections */ |
551 |
562 |
LL_FOREACH(local_port_forwards, port_forward) |
LL_FOREACH(local_port_forwards, port_forward) |
552 |
563 |
{ |
{ |
553 |
564 |
if(!client_pipe_mode && |
if(!client_pipe_mode && |
554 |
|
port_forward->tun->sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */ |
|
|
565 |
|
port_forward->accept_sockfd <= 0) /* Don't accept if we're already waiting to establish a tunnel */ |
555 |
566 |
{ |
{ |
|
567 |
|
log_printf(L_DEBUG2, "FORWARDING: checking fd %d for local port %d", port_forward->bind_sockfd, port_forward->local_port); |
556 |
568 |
accept_fd = accept(port_forward->bind_sockfd, NULL, NULL); |
accept_fd = accept(port_forward->bind_sockfd, NULL, NULL); |
557 |
569 |
if(accept_fd != -1) |
if(accept_fd != -1) |
558 |
570 |
{ |
{ |
559 |
571 |
log_printf(L_INFO, "Accepting a new connection - requesting tunnel...\n"); |
log_printf(L_INFO, "Accepting a new connection - requesting tunnel...\n"); |
560 |
572 |
|
|
561 |
573 |
/* Open a new tunnel for this FD */ |
/* Open a new tunnel for this FD */ |
562 |
|
port_forward->tun->sockfd = accept_fd; |
|
|
574 |
|
port_forward->accept_sockfd = accept_fd; |
563 |
575 |
send_tunnel_request_packet( |
send_tunnel_request_packet( |
564 |
576 |
port_forward->remote_host, |
port_forward->remote_host, |
565 |
577 |
port_forward->remote_port, |
port_forward->remote_port, |
|
... |
... |
int do_client_loop(uint8_t *tox_id_str) |
704 |
716 |
else |
else |
705 |
717 |
{ |
{ |
706 |
718 |
state = CLIENT_STATE_FORWARDING; |
state = CLIENT_STATE_FORWARDING; |
|
719 |
|
log_printf(L_DEBUG2, "Entered CLIENT_STATE_FORWARDING"); |
707 |
720 |
} |
} |
708 |
721 |
} |
} |
709 |
722 |
} |
} |
File main.c changed (mode: 100644) (index 6fccb63..4df4ab4) |
... |
... |
rule *rules = NULL; |
46 |
46 |
|
|
47 |
47 |
/* Ports and hostname for port forwarding */ |
/* Ports and hostname for port forwarding */ |
48 |
48 |
local_port_forward *local_port_forwards = NULL; |
local_port_forward *local_port_forwards = NULL; |
49 |
|
/* Non-acknowledged local port forwards */ |
|
50 |
|
local_port_forward *pending_port_forwards = NULL; |
|
51 |
49 |
uint32_t last_forward_id; |
uint32_t last_forward_id; |
52 |
50 |
|
|
53 |
51 |
/* Whether to daemonize/fork after startup */ |
/* Whether to daemonize/fork after startup */ |
|
... |
... |
local_port_forward *find_pending_forward_by_id(uint32_t local_forward_id) |
213 |
211 |
{ |
{ |
214 |
212 |
local_port_forward *forward; |
local_port_forward *forward; |
215 |
213 |
|
|
216 |
|
LL_FOREACH(pending_port_forwards, forward) |
|
|
214 |
|
LL_FOREACH(local_port_forwards, forward) |
217 |
215 |
{ |
{ |
218 |
216 |
if(forward->forward_id == local_forward_id) |
if(forward->forward_id == local_forward_id) |
219 |
217 |
{ |
{ |
|
... |
... |
void parse_lossless_packet(Tox *tox, uint32_t friendnumber, const uint8_t *data, |
689 |
687 |
if(data[0] != PROTOCOL_MAGIC_HIGH || data[1] != PROTOCOL_MAGIC_LOW) |
if(data[0] != PROTOCOL_MAGIC_HIGH || data[1] != PROTOCOL_MAGIC_LOW) |
690 |
688 |
{ |
{ |
691 |
689 |
log_printf(L_WARNING, "Received data frame with invalid protocol magic number 0x%x%x\n", data[0], data[1]); |
log_printf(L_WARNING, "Received data frame with invalid protocol magic number 0x%x%x\n", data[0], data[1]); |
|
690 |
|
if(data[0] == (PROTOCOL_MAGIC_V1 >> 8) && data[1] == (PROTOCOL_MAGIC_V1 & 0xff)) |
|
691 |
|
{ |
|
692 |
|
log_printf(L_ERROR, "Tuntox on the other end uses old protocol version 1. Please upgrade it."); |
|
693 |
|
} |
692 |
694 |
return; |
return; |
693 |
695 |
} |
} |
694 |
696 |
|
|
|
... |
... |
int main(int argc, char *argv[]) |
1347 |
1349 |
exit(1); |
exit(1); |
1348 |
1350 |
} |
} |
1349 |
1351 |
|
|
1350 |
|
LL_APPEND(pending_port_forwards, port_forward); |
|
|
1352 |
|
LL_APPEND(local_port_forwards, port_forward); |
1351 |
1353 |
|
|
1352 |
1354 |
if(min_log_level == L_UNSET) |
if(min_log_level == L_UNSET) |
1353 |
1355 |
{ |
{ |
|
... |
... |
int main(int argc, char *argv[]) |
1369 |
1371 |
{ |
{ |
1370 |
1372 |
min_log_level = L_ERROR; |
min_log_level = L_ERROR; |
1371 |
1373 |
} |
} |
1372 |
|
LL_APPEND(pending_port_forwards, port_forward); |
|
|
1374 |
|
LL_APPEND(local_port_forwards, port_forward); |
1373 |
1375 |
log_printf(L_INFO, "Forwarding remote port %d to stdin/out\n", port_forward->remote_port); |
log_printf(L_INFO, "Forwarding remote port %d to stdin/out\n", port_forward->remote_port); |
1374 |
1376 |
break; |
break; |
1375 |
1377 |
case 'p': |
case 'p': |