File client.c changed (mode: 100644) (index bfcab14..9dc1b03) |
... |
... |
int handle_server_tcp_frame(protocol_frame *rcvd_frame) |
210 |
210 |
frame->connid = tun->connid; |
frame->connid = tun->connid; |
211 |
211 |
frame->data_length = 0; |
frame->data_length = 0; |
212 |
212 |
send_frame(frame, data); |
send_frame(frame, data); |
|
213 |
|
if(tun->sockfd) |
|
214 |
|
{ |
|
215 |
|
FD_CLR(tun->sockfd, &client_master_fdset); |
|
216 |
|
} |
213 |
217 |
tunnel_delete(tun); |
tunnel_delete(tun); |
214 |
218 |
|
|
215 |
219 |
return -1; |
return -1; |
|
... |
... |
int handle_server_tcp_fin_frame(protocol_frame *rcvd_frame) |
244 |
248 |
return -1; |
return -1; |
245 |
249 |
} |
} |
246 |
250 |
|
|
|
251 |
|
if(tun->sockfd) |
|
252 |
|
{ |
|
253 |
|
FD_CLR(tun->sockfd, &client_master_fdset); |
|
254 |
|
} |
247 |
255 |
tunnel_delete(tun); |
tunnel_delete(tun); |
248 |
256 |
|
|
249 |
257 |
return 0; |
return 0; |
|
... |
... |
int do_client_loop(char *tox_id_str) |
442 |
450 |
case CLIENT_STATE_FORWARDING: |
case CLIENT_STATE_FORWARDING: |
443 |
451 |
{ |
{ |
444 |
452 |
int accept_fd = 0; |
int accept_fd = 0; |
|
453 |
|
int select_rv = 0; |
445 |
454 |
tunnel *tmp = NULL; |
tunnel *tmp = NULL; |
446 |
455 |
tunnel *tun = NULL; |
tunnel *tun = NULL; |
447 |
456 |
|
|
|
... |
... |
int do_client_loop(char *tox_id_str) |
469 |
478 |
} |
} |
470 |
479 |
|
|
471 |
480 |
/* Handle reading from sockets */ |
/* Handle reading from sockets */ |
472 |
|
select(select_nfds, &fds, NULL, NULL, &tv); |
|
473 |
|
HASH_ITER(hh, by_id, tun, tmp) |
|
|
481 |
|
select_rv = select(select_nfds, &fds, NULL, NULL, &tv); |
|
482 |
|
if(select_rv == -1 || select_rv == 0) |
474 |
483 |
{ |
{ |
475 |
|
if(FD_ISSET(tun->sockfd, &fds)) |
|
|
484 |
|
if(select_rv == -1) |
476 |
485 |
{ |
{ |
477 |
|
int nbytes; |
|
478 |
|
if(client_local_port_mode) |
|
479 |
|
{ |
|
480 |
|
nbytes = recv(tun->sockfd, |
|
481 |
|
tox_packet_buf + PROTOCOL_BUFFER_OFFSET, |
|
482 |
|
READ_BUFFER_SIZE, 0); |
|
483 |
|
} |
|
484 |
|
else |
|
485 |
|
{ |
|
486 |
|
nbytes = read(tun->sockfd, |
|
487 |
|
tox_packet_buf + PROTOCOL_BUFFER_OFFSET, |
|
488 |
|
READ_BUFFER_SIZE |
|
489 |
|
); |
|
490 |
|
} |
|
491 |
|
|
|
492 |
|
/* Check if connection closed */ |
|
493 |
|
if(nbytes == 0) |
|
494 |
|
{ |
|
495 |
|
char data[PROTOCOL_BUFFER_OFFSET]; |
|
496 |
|
protocol_frame frame_st, *frame; |
|
497 |
|
|
|
498 |
|
log_printf(L_INFO, "Connection closed\n"); |
|
499 |
|
|
|
500 |
|
frame = &frame_st; |
|
501 |
|
memset(frame, 0, sizeof(protocol_frame)); |
|
502 |
|
frame->friendnumber = tun->friendnumber; |
|
503 |
|
frame->packet_type = PACKET_TYPE_TCP_FIN; |
|
504 |
|
frame->connid = tun->connid; |
|
505 |
|
frame->data_length = 0; |
|
506 |
|
send_frame(frame, data); |
|
507 |
|
tunnel_delete(tun); |
|
508 |
|
} |
|
509 |
|
else |
|
|
486 |
|
log_printf(L_DEBUG, "Reading from local socket failed: code=%d (%s)\n", |
|
487 |
|
errno, strerror(errno)); |
|
488 |
|
} |
|
489 |
|
else |
|
490 |
|
{ |
|
491 |
|
log_printf(L_DEBUG2, "Nothing to read..."); |
|
492 |
|
} |
|
493 |
|
} |
|
494 |
|
else |
|
495 |
|
{ |
|
496 |
|
HASH_ITER(hh, by_id, tun, tmp) |
|
497 |
|
{ |
|
498 |
|
if(FD_ISSET(tun->sockfd, &fds)) |
510 |
499 |
{ |
{ |
511 |
|
protocol_frame frame_st, *frame; |
|
512 |
|
|
|
513 |
|
frame = &frame_st; |
|
514 |
|
memset(frame, 0, sizeof(protocol_frame)); |
|
515 |
|
frame->friendnumber = tun->friendnumber; |
|
516 |
|
frame->packet_type = PACKET_TYPE_TCP; |
|
517 |
|
frame->connid = tun->connid; |
|
518 |
|
frame->data_length = nbytes; |
|
519 |
|
send_frame(frame, tox_packet_buf); |
|
520 |
|
|
|
521 |
|
// printf("Wrote %d bytes from sock %d to tunnel %d\n", nbytes, tun->sockfd, tun->connid); |
|
|
500 |
|
int nbytes; |
|
501 |
|
if(client_local_port_mode) |
|
502 |
|
{ |
|
503 |
|
nbytes = recv(tun->sockfd, |
|
504 |
|
tox_packet_buf + PROTOCOL_BUFFER_OFFSET, |
|
505 |
|
READ_BUFFER_SIZE, 0); |
|
506 |
|
} |
|
507 |
|
else |
|
508 |
|
{ |
|
509 |
|
nbytes = read(tun->sockfd, |
|
510 |
|
tox_packet_buf + PROTOCOL_BUFFER_OFFSET, |
|
511 |
|
READ_BUFFER_SIZE |
|
512 |
|
); |
|
513 |
|
} |
|
514 |
|
|
|
515 |
|
/* Check if connection closed */ |
|
516 |
|
if(nbytes == 0) |
|
517 |
|
{ |
|
518 |
|
char data[PROTOCOL_BUFFER_OFFSET]; |
|
519 |
|
protocol_frame frame_st, *frame; |
|
520 |
|
|
|
521 |
|
log_printf(L_INFO, "Connection closed\n"); |
|
522 |
|
|
|
523 |
|
frame = &frame_st; |
|
524 |
|
memset(frame, 0, sizeof(protocol_frame)); |
|
525 |
|
frame->friendnumber = tun->friendnumber; |
|
526 |
|
frame->packet_type = PACKET_TYPE_TCP_FIN; |
|
527 |
|
frame->connid = tun->connid; |
|
528 |
|
frame->data_length = 0; |
|
529 |
|
send_frame(frame, data); |
|
530 |
|
if(tun->sockfd) |
|
531 |
|
{ |
|
532 |
|
FD_CLR(tun->sockfd, &client_master_fdset); |
|
533 |
|
} |
|
534 |
|
tunnel_delete(tun); |
|
535 |
|
} |
|
536 |
|
else |
|
537 |
|
{ |
|
538 |
|
protocol_frame frame_st, *frame; |
|
539 |
|
|
|
540 |
|
frame = &frame_st; |
|
541 |
|
memset(frame, 0, sizeof(protocol_frame)); |
|
542 |
|
frame->friendnumber = tun->friendnumber; |
|
543 |
|
frame->packet_type = PACKET_TYPE_TCP; |
|
544 |
|
frame->connid = tun->connid; |
|
545 |
|
frame->data_length = nbytes; |
|
546 |
|
send_frame(frame, tox_packet_buf); |
|
547 |
|
|
|
548 |
|
// printf("Wrote %d bytes from sock %d to tunnel %d\n", nbytes, tun->sockfd, tun->connid); |
|
549 |
|
} |
522 |
550 |
} |
} |
523 |
551 |
} |
} |
524 |
552 |
} |
} |
File main.c changed (mode: 100644) (index 0535e67..b87172c) |
... |
... |
void tunnel_delete(tunnel *t) |
115 |
115 |
if(t->sockfd) |
if(t->sockfd) |
116 |
116 |
{ |
{ |
117 |
117 |
close(t->sockfd); |
close(t->sockfd); |
|
118 |
|
FD_CLR(t->sockfd, &master_server_fds); |
118 |
119 |
} |
} |
119 |
120 |
HASH_DEL( by_id, t ); |
HASH_DEL( by_id, t ); |
120 |
121 |
free(t); |
free(t); |
|
... |
... |
void handle_connection_status_change(Tox *tox, TOX_CONNECTION p_connection_statu |
744 |
745 |
log_printf(L_INFO, "Connection status changed: %s", status); |
log_printf(L_INFO, "Connection status changed: %s", status); |
745 |
746 |
} |
} |
746 |
747 |
|
|
747 |
|
void cleanup(int status, void *tmp) |
|
|
748 |
|
void cleanup() |
748 |
749 |
{ |
{ |
749 |
750 |
log_printf(L_DEBUG, "kthxbye\n"); |
log_printf(L_DEBUG, "kthxbye\n"); |
750 |
751 |
fflush(stdout); |
fflush(stdout); |
|
... |
... |
int do_server_loop() |
778 |
779 |
{ |
{ |
779 |
780 |
TOX_CONNECTION tmp_isconnected = 0; |
TOX_CONNECTION tmp_isconnected = 0; |
780 |
781 |
uint32_t tox_do_interval_ms; |
uint32_t tox_do_interval_ms; |
|
782 |
|
int select_rv = 0; |
781 |
783 |
|
|
782 |
784 |
/* Let tox do its stuff */ |
/* Let tox do its stuff */ |
783 |
785 |
tox_iterate(tox); |
tox_iterate(tox); |
|
... |
... |
int do_server_loop() |
807 |
809 |
fds = master_server_fds; |
fds = master_server_fds; |
808 |
810 |
|
|
809 |
811 |
/* Poll for data from our client connection */ |
/* Poll for data from our client connection */ |
810 |
|
select(select_nfds, &fds, NULL, NULL, &tv); |
|
811 |
|
HASH_ITER(hh, by_id, tun, tmp) |
|
|
812 |
|
select_rv = select(select_nfds, &fds, NULL, NULL, &tv); |
|
813 |
|
if(select_rv == -1 || select_rv == 0) |
812 |
814 |
{ |
{ |
813 |
|
if(FD_ISSET(tun->sockfd, &fds)) |
|
|
815 |
|
if(select_rv == -1) |
814 |
816 |
{ |
{ |
815 |
|
int nbytes = recv(tun->sockfd, |
|
816 |
|
tox_packet_buf+PROTOCOL_BUFFER_OFFSET, |
|
817 |
|
READ_BUFFER_SIZE, 0); |
|
818 |
|
|
|
819 |
|
/* Check if connection closed */ |
|
820 |
|
if(nbytes <= 0) |
|
|
817 |
|
log_printf(L_DEBUG, "Reading from local socket failed: code=%d (%s)\n", |
|
818 |
|
errno, strerror(errno)); |
|
819 |
|
} |
|
820 |
|
else |
|
821 |
|
{ |
|
822 |
|
log_printf(L_DEBUG2, "Nothing to read..."); |
|
823 |
|
} |
|
824 |
|
} |
|
825 |
|
else |
|
826 |
|
{ |
|
827 |
|
HASH_ITER(hh, by_id, tun, tmp) |
|
828 |
|
{ |
|
829 |
|
if(FD_ISSET(tun->sockfd, &fds)) |
821 |
830 |
{ |
{ |
822 |
|
char data[PROTOCOL_BUFFER_OFFSET]; |
|
823 |
|
protocol_frame frame_st, *frame; |
|
|
831 |
|
int nbytes = recv(tun->sockfd, |
|
832 |
|
tox_packet_buf+PROTOCOL_BUFFER_OFFSET, |
|
833 |
|
READ_BUFFER_SIZE, 0); |
824 |
834 |
|
|
825 |
|
if(nbytes == 0) |
|
|
835 |
|
/* Check if connection closed */ |
|
836 |
|
if(nbytes <= 0) |
826 |
837 |
{ |
{ |
827 |
|
log_printf(L_WARNING, "conn closed!\n"); |
|
|
838 |
|
char data[PROTOCOL_BUFFER_OFFSET]; |
|
839 |
|
protocol_frame frame_st, *frame; |
|
840 |
|
|
|
841 |
|
if(nbytes == 0) |
|
842 |
|
{ |
|
843 |
|
log_printf(L_WARNING, "conn closed!\n"); |
|
844 |
|
} |
|
845 |
|
else |
|
846 |
|
{ |
|
847 |
|
log_printf(L_WARNING, "conn closed, code=%d (%s)\n", |
|
848 |
|
errno, strerror(errno)); |
|
849 |
|
} |
|
850 |
|
|
|
851 |
|
frame = &frame_st; |
|
852 |
|
memset(frame, 0, sizeof(protocol_frame)); |
|
853 |
|
frame->friendnumber = tun->friendnumber; |
|
854 |
|
frame->packet_type = PACKET_TYPE_TCP_FIN; |
|
855 |
|
frame->connid = tun->connid; |
|
856 |
|
frame->data_length = 0; |
|
857 |
|
send_frame(frame, data); |
|
858 |
|
|
|
859 |
|
tunnel_delete(tun); |
|
860 |
|
|
|
861 |
|
continue; |
828 |
862 |
} |
} |
829 |
863 |
else |
else |
830 |
864 |
{ |
{ |
831 |
|
log_printf(L_WARNING, "conn closed, code=%d (%s)\n", |
|
832 |
|
errno, strerror(errno)); |
|
|
865 |
|
protocol_frame frame_st, *frame; |
|
866 |
|
|
|
867 |
|
frame = &frame_st; |
|
868 |
|
memset(frame, 0, sizeof(protocol_frame)); |
|
869 |
|
frame->friendnumber = tun->friendnumber; |
|
870 |
|
frame->packet_type = PACKET_TYPE_TCP; |
|
871 |
|
frame->connid = tun->connid; |
|
872 |
|
frame->data_length = nbytes; |
|
873 |
|
send_frame(frame, tox_packet_buf); |
833 |
874 |
} |
} |
834 |
|
|
|
835 |
|
frame = &frame_st; |
|
836 |
|
memset(frame, 0, sizeof(protocol_frame)); |
|
837 |
|
frame->friendnumber = tun->friendnumber; |
|
838 |
|
frame->packet_type = PACKET_TYPE_TCP_FIN; |
|
839 |
|
frame->connid = tun->connid; |
|
840 |
|
frame->data_length = 0; |
|
841 |
|
send_frame(frame, data); |
|
842 |
|
|
|
843 |
|
tunnel_delete(tun); |
|
844 |
|
|
|
845 |
|
continue; |
|
846 |
|
} |
|
847 |
|
else |
|
848 |
|
{ |
|
849 |
|
protocol_frame frame_st, *frame; |
|
850 |
|
|
|
851 |
|
frame = &frame_st; |
|
852 |
|
memset(frame, 0, sizeof(protocol_frame)); |
|
853 |
|
frame->friendnumber = tun->friendnumber; |
|
854 |
|
frame->packet_type = PACKET_TYPE_TCP; |
|
855 |
|
frame->connid = tun->connid; |
|
856 |
|
frame->data_length = nbytes; |
|
857 |
|
send_frame(frame, tox_packet_buf); |
|
858 |
875 |
} |
} |
859 |
876 |
} |
} |
860 |
877 |
} |
} |
|
... |
... |
int do_server_loop() |
865 |
882 |
|
|
866 |
883 |
if(ms_end - ms_start < tox_do_interval_ms) |
if(ms_end - ms_start < tox_do_interval_ms) |
867 |
884 |
{ |
{ |
868 |
|
log_printf(L_DEBUG, "Sleeping for %d ms extra to prevent high CPU usage\n", (tox_do_interval_ms - (ms_end - ms_start))); |
|
|
885 |
|
/*log_printf(L_DEBUG, "Sleeping for %d ms extra to prevent high CPU usage\n", (tox_do_interval_ms - (ms_end - ms_start)));*/ |
869 |
886 |
usleep((tox_do_interval_ms - (ms_end - ms_start)) * 1000); |
usleep((tox_do_interval_ms - (ms_end - ms_start)) * 1000); |
870 |
887 |
} |
} |
871 |
888 |
} |
} |