summaryrefslogtreecommitdiff
path: root/client.c
diff options
context:
space:
mode:
authorGDR! <gdr@gdr.name>2018-11-17 11:42:55 +0100
committerGDR! <gdr@gdr.name>2018-11-17 11:42:55 +0100
commit03d83602d3a328d89b806e23c35644ec3908dc1f (patch)
tree6546454a0cd64b1a86b31cb7d7bea82cf46e7334 /client.c
parent3adc626eb1646ee3456ca282e1855142f9f935e0 (diff)
Retry on lost friend connection
As described in Github issue #49 https://github.com/gjedeer/tuntox/issues/49 tuntox should try to reconnect to the server when friend connection is lost. This commit fixes that.
Diffstat (limited to 'client.c')
-rw-r--r--client.c41
1 files changed, 38 insertions, 3 deletions
diff --git a/client.c b/client.c
index 20243e5..fa326ea 100644
--- a/client.c
+++ b/client.c
@@ -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 */
254int 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) */
265int 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 */
@@ -308,6 +327,7 @@ int do_client_loop(uint8_t *tox_id_str)
308 { 327 {
309 uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!"; 328 uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!";
310 uint16_t length = sizeof(data); 329 uint16_t length = sizeof(data);
330 /* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1168 */
311 TOX_ERR_FRIEND_ADD add_error; 331 TOX_ERR_FRIEND_ADD add_error;
312 332
313 if(use_shared_secret) 333 if(use_shared_secret)
@@ -592,7 +612,22 @@ int do_client_loop(uint8_t *tox_id_str)
592 if(friend_connection_status != last_friend_connection_status) 612 if(friend_connection_status != last_friend_connection_status)
593 { 613 {
594 const char* status = readable_connection_status(friend_connection_status); 614 const char* status = readable_connection_status(friend_connection_status);
595 log_printf(L_INFO, "Friend connection status changed to: %s\n", status); 615 log_printf(L_INFO, "Friend connection status changed to: %s (%d)\n", status, friend_connection_status);
616
617 if(friend_connection_status == TOX_CONNECTION_NONE)
618 {
619 /* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1267 */
620 TOX_ERR_FRIEND_DELETE tox_delete_error;
621
622 log_printf(L_WARNING, "Lost connection to server, closing all tunnels and re-adding friend\n");
623 client_close_all_connections();
624 tox_friend_delete(tox, friendnumber, &tox_delete_error);
625 if(tox_delete_error)
626 {
627 log_printf(L_ERROR, "Error when deleting server from friend list: %d\n", tox_delete_error);
628 }
629 state = CLIENT_STATE_INITIAL;
630 }
596 } 631 }
597 632
598 last_friend_connection_status_received = time(NULL); 633 last_friend_connection_status_received = time(NULL);