summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client.c66
-rw-r--r--client.h1
-rw-r--r--gitversion.c2
-rw-r--r--gitversion.h2
-rw-r--r--main.h1
5 files changed, 67 insertions, 5 deletions
diff --git a/client.c b/client.c
index 20243e5..517b89e 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 */
@@ -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;
diff --git a/client.h b/client.h
index db71d40..be68f25 100644
--- a/client.h
+++ b/client.h
@@ -13,6 +13,7 @@
13#define CLIENT_STATE_SHUTDOWN 11 13#define CLIENT_STATE_SHUTDOWN 11
14#define CLIENT_STATE_BIND_PORT 12 14#define CLIENT_STATE_BIND_PORT 12
15#define CLIENT_STATE_SETUP_PIPE 13 15#define CLIENT_STATE_SETUP_PIPE 13
16#define CLIENT_STATE_CONNECTION_LOST 14
16 17
17int handle_pong_frame(); 18int handle_pong_frame();
18int handle_acktunnel_frame(protocol_frame *rcvd_frame); 19int handle_acktunnel_frame(protocol_frame *rcvd_frame);
diff --git a/gitversion.c b/gitversion.c
index 52f9d71..8d1bb5f 100644
--- a/gitversion.c
+++ b/gitversion.c
@@ -5,5 +5,5 @@ const char *gitversion = GITVERSION;
5 5
6void print_version() 6void print_version()
7{ 7{
8 log_printf(L_INFO, "Tuntox built from git commit %s", gitversion); 8 log_printf(L_INFO, "Tuntox built from git commit %s, toxcore version %d.%d.%d", gitversion, tox_version_major(), tox_version_minor(), tox_version_patch());
9} 9}
diff --git a/gitversion.h b/gitversion.h
index a5dd0be..bc45093 100644
--- a/gitversion.h
+++ b/gitversion.h
@@ -1 +1 @@
#define GITVERSION "0.0.9" #define GITVERSION "121acd73ddba12659713f27abef7524b9a0c6a48"
diff --git a/main.h b/main.h
index 90f393e..17fb9b9 100644
--- a/main.h
+++ b/main.h
@@ -4,6 +4,7 @@
4#include <arpa/inet.h> 4#include <arpa/inet.h>
5#include <errno.h> 5#include <errno.h>
6#include <fcntl.h> 6#include <fcntl.h>
7#include <limits.h>
7#include <netdb.h> 8#include <netdb.h>
8#include <netinet/in.h> 9#include <netinet/in.h>
9#include <pwd.h> 10#include <pwd.h>