diff options
Diffstat (limited to 'client.c')
-rw-r--r-- | client.c | 66 |
1 files changed, 63 insertions, 3 deletions
@@ -244,14 +244,33 @@ int handle_server_tcp_fin_frame(protocol_frame *rcvd_frame) | |||
244 | log_printf(L_WARNING, "Friend #%d tried to close tunnel while server is #%d\n", rcvd_frame->friendnumber, tun->friendnumber); | 244 | log_printf(L_WARNING, "Friend #%d tried to close tunnel while server is #%d\n", rcvd_frame->friendnumber, tun->friendnumber); |
245 | return -1; | 245 | return -1; |
246 | } | 246 | } |
247 | 247 | ||
248 | client_close_tunnel(tun); | ||
249 | |||
250 | return 0; | ||
251 | } | ||
252 | |||
253 | /* Delete tunnel and clear client-side fdset */ | ||
254 | int client_close_tunnel(tunnel *tun) | ||
255 | { | ||
248 | if(tun->sockfd) | 256 | if(tun->sockfd) |
249 | { | 257 | { |
250 | FD_CLR(tun->sockfd, &client_master_fdset); | 258 | FD_CLR(tun->sockfd, &client_master_fdset); |
251 | } | 259 | } |
260 | |||
252 | tunnel_delete(tun); | 261 | tunnel_delete(tun); |
262 | } | ||
253 | 263 | ||
254 | return 0; | 264 | /* Close and delete all tunnels (when server went offline) */ |
265 | int client_close_all_connections() | ||
266 | { | ||
267 | tunnel *tmp = NULL; | ||
268 | tunnel *tun = NULL; | ||
269 | |||
270 | HASH_ITER(hh, by_id, tun, tmp) | ||
271 | { | ||
272 | client_close_tunnel(tun); | ||
273 | } | ||
255 | } | 274 | } |
256 | 275 | ||
257 | /* Main loop for the client */ | 276 | /* Main loop for the client */ |
@@ -262,6 +281,7 @@ int do_client_loop(uint8_t *tox_id_str) | |||
262 | uint32_t friendnumber = 0; | 281 | uint32_t friendnumber = 0; |
263 | TOX_CONNECTION last_friend_connection_status = TOX_CONNECTION_NONE; | 282 | TOX_CONNECTION last_friend_connection_status = TOX_CONNECTION_NONE; |
264 | time_t last_friend_connection_status_received = 0; | 283 | time_t last_friend_connection_status_received = 0; |
284 | time_t connection_lost_timestamp = 0; | ||
265 | struct timeval tv; | 285 | struct timeval tv; |
266 | fd_set fds; | 286 | fd_set fds; |
267 | static time_t invitation_sent_time = 0; | 287 | static time_t invitation_sent_time = 0; |
@@ -308,6 +328,7 @@ int do_client_loop(uint8_t *tox_id_str) | |||
308 | { | 328 | { |
309 | uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!"; | 329 | uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!"; |
310 | uint16_t length = sizeof(data); | 330 | uint16_t length = sizeof(data); |
331 | /* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1168 */ | ||
311 | TOX_ERR_FRIEND_ADD add_error; | 332 | TOX_ERR_FRIEND_ADD add_error; |
312 | 333 | ||
313 | if(use_shared_secret) | 334 | if(use_shared_secret) |
@@ -592,7 +613,13 @@ int do_client_loop(uint8_t *tox_id_str) | |||
592 | if(friend_connection_status != last_friend_connection_status) | 613 | if(friend_connection_status != last_friend_connection_status) |
593 | { | 614 | { |
594 | const char* status = readable_connection_status(friend_connection_status); | 615 | const char* status = readable_connection_status(friend_connection_status); |
595 | log_printf(L_INFO, "Friend connection status changed to: %s\n", status); | 616 | log_printf(L_INFO, "Friend connection status changed to: %s (%d)\n", status, friend_connection_status); |
617 | |||
618 | if(friend_connection_status == TOX_CONNECTION_NONE) | ||
619 | { | ||
620 | state = CLIENT_STATE_CONNECTION_LOST; | ||
621 | connection_lost_timestamp = time(NULL); | ||
622 | } | ||
596 | } | 623 | } |
597 | 624 | ||
598 | last_friend_connection_status_received = time(NULL); | 625 | last_friend_connection_status_received = time(NULL); |
@@ -601,6 +628,39 @@ int do_client_loop(uint8_t *tox_id_str) | |||
601 | } | 628 | } |
602 | } | 629 | } |
603 | break; | 630 | break; |
631 | case CLIENT_STATE_CONNECTION_LOST: | ||
632 | { | ||
633 | TOX_CONNECTION friend_connection_status; | ||
634 | friend_connection_status = tox_friend_get_connection_status(tox, friendnumber, &friend_query_error); | ||
635 | if(friend_query_error != TOX_ERR_FRIEND_QUERY_OK) | ||
636 | { | ||
637 | log_printf(L_DEBUG, "tox_friend_get_connection_status: error %u\n", friend_query_error); | ||
638 | } | ||
639 | else | ||
640 | { | ||
641 | if(friend_connection_status == TOX_CONNECTION_NONE) | ||
642 | { | ||
643 | /* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1267 */ | ||
644 | TOX_ERR_FRIEND_DELETE tox_delete_error; | ||
645 | |||
646 | log_printf(L_WARNING, "Lost connection to server, closing all tunnels and re-adding friend\n"); | ||
647 | client_close_all_connections(); | ||
648 | tox_friend_delete(tox, friendnumber, &tox_delete_error); | ||
649 | if(tox_delete_error) | ||
650 | { | ||
651 | log_printf(L_ERROR, "Error when deleting server from friend list: %d\n", tox_delete_error); | ||
652 | } | ||
653 | state = CLIENT_STATE_INITIAL; | ||
654 | } | ||
655 | else | ||
656 | { | ||
657 | state = CLIENT_STATE_FORWARDING; | ||
658 | } | ||
659 | } | ||
660 | } | ||
661 | break; | ||
662 | case 0xffffffff: | ||
663 | log_printf(L_ERROR, "You forgot a break statement\n"); | ||
604 | case CLIENT_STATE_SHUTDOWN: | 664 | case CLIENT_STATE_SHUTDOWN: |
605 | exit(0); | 665 | exit(0); |
606 | break; | 666 | break; |