diff options
author | GDR! <gdr@gdr.name> | 2016-06-07 20:42:45 +0200 |
---|---|---|
committer | GDR! <gdr@gdr.name> | 2016-06-07 20:42:45 +0200 |
commit | 816245f429145052b8908d500c64d372fc8c2972 (patch) | |
tree | 6bd800e5a6cc7f5cf47efa0d1f8fa22cd687d74c /client.c | |
parent | c2a78f48bd44613162a6e3c1d02cb53266611936 (diff) |
Remove FDs of closed tunnels from fdset
For #19 and #17
Diffstat (limited to 'client.c')
-rw-r--r-- | client.c | 122 |
1 files changed, 75 insertions, 47 deletions
@@ -210,6 +210,10 @@ int handle_server_tcp_frame(protocol_frame *rcvd_frame) | |||
210 | frame->connid = tun->connid; | 210 | frame->connid = tun->connid; |
211 | frame->data_length = 0; | 211 | frame->data_length = 0; |
212 | send_frame(frame, data); | 212 | send_frame(frame, data); |
213 | if(tun->sockfd) | ||
214 | { | ||
215 | FD_CLR(tun->sockfd, &client_master_fdset); | ||
216 | } | ||
213 | tunnel_delete(tun); | 217 | tunnel_delete(tun); |
214 | 218 | ||
215 | return -1; | 219 | return -1; |
@@ -244,6 +248,10 @@ int handle_server_tcp_fin_frame(protocol_frame *rcvd_frame) | |||
244 | return -1; | 248 | return -1; |
245 | } | 249 | } |
246 | 250 | ||
251 | if(tun->sockfd) | ||
252 | { | ||
253 | FD_CLR(tun->sockfd, &client_master_fdset); | ||
254 | } | ||
247 | tunnel_delete(tun); | 255 | tunnel_delete(tun); |
248 | 256 | ||
249 | return 0; | 257 | return 0; |
@@ -442,6 +450,7 @@ int do_client_loop(char *tox_id_str) | |||
442 | case CLIENT_STATE_FORWARDING: | 450 | case CLIENT_STATE_FORWARDING: |
443 | { | 451 | { |
444 | int accept_fd = 0; | 452 | int accept_fd = 0; |
453 | int select_rv = 0; | ||
445 | tunnel *tmp = NULL; | 454 | tunnel *tmp = NULL; |
446 | tunnel *tun = NULL; | 455 | tunnel *tun = NULL; |
447 | 456 | ||
@@ -469,56 +478,75 @@ int do_client_loop(char *tox_id_str) | |||
469 | } | 478 | } |
470 | 479 | ||
471 | /* Handle reading from sockets */ | 480 | /* Handle reading from sockets */ |
472 | select(select_nfds, &fds, NULL, NULL, &tv); | 481 | select_rv = select(select_nfds, &fds, NULL, NULL, &tv); |
473 | HASH_ITER(hh, by_id, tun, tmp) | 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; | 486 | log_printf(L_DEBUG, "Reading from local socket failed: code=%d (%s)\n", |
478 | if(client_local_port_mode) | 487 | errno, strerror(errno)); |
479 | { | 488 | } |
480 | nbytes = recv(tun->sockfd, | 489 | else |
481 | tox_packet_buf + PROTOCOL_BUFFER_OFFSET, | 490 | { |
482 | READ_BUFFER_SIZE, 0); | 491 | log_printf(L_DEBUG2, "Nothing to read..."); |
483 | } | 492 | } |
484 | else | 493 | } |
485 | { | 494 | else |
486 | nbytes = read(tun->sockfd, | 495 | { |
487 | tox_packet_buf + PROTOCOL_BUFFER_OFFSET, | 496 | HASH_ITER(hh, by_id, tun, tmp) |
488 | READ_BUFFER_SIZE | 497 | { |
489 | ); | 498 | if(FD_ISSET(tun->sockfd, &fds)) |
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 | ||
510 | { | 499 | { |
511 | protocol_frame frame_st, *frame; | 500 | int nbytes; |
512 | 501 | if(client_local_port_mode) | |
513 | frame = &frame_st; | 502 | { |
514 | memset(frame, 0, sizeof(protocol_frame)); | 503 | nbytes = recv(tun->sockfd, |
515 | frame->friendnumber = tun->friendnumber; | 504 | tox_packet_buf + PROTOCOL_BUFFER_OFFSET, |
516 | frame->packet_type = PACKET_TYPE_TCP; | 505 | READ_BUFFER_SIZE, 0); |
517 | frame->connid = tun->connid; | 506 | } |
518 | frame->data_length = nbytes; | 507 | else |
519 | send_frame(frame, tox_packet_buf); | 508 | { |
520 | 509 | nbytes = read(tun->sockfd, | |
521 | // printf("Wrote %d bytes from sock %d to tunnel %d\n", nbytes, tun->sockfd, tun->connid); | 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 | } |