From 475d51fc1a006503f0a0096c86d29e1253555967 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Fri, 21 Aug 2020 17:52:51 -0400 Subject: set CONNECTED state in callback --- client.c | 30 ++++++++++++++++++++---------- client.h | 24 ++++++++++-------------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/client.c b/client.c index 687bdcf..4a82bbc 100644 --- a/client.c +++ b/client.c @@ -12,7 +12,7 @@ const bool attempt_reconnect = true; /* The state machine */ -int state = CLIENT_STATE_INITIAL; +int state = CLIENT_STATE_AWAIT_FRIENDSHIP; /* Used in ping mode */ struct timespec ping_sent_time; @@ -143,6 +143,10 @@ int handle_acktunnel_frame(protocol_frame *rcvd_frame) } tun = tunnel_create(client_tunnel.sockfd, rcvd_frame->connid, rcvd_frame->friendnumber); + if (!tun) + { + exit(1); + } /* Mark that we can accept() another connection */ client_tunnel.sockfd = -1; @@ -153,6 +157,11 @@ int handle_acktunnel_frame(protocol_frame *rcvd_frame) { log_printf(L_INFO, "Accepted a new connection on port %d\n", local_port); } + if(state != CLIENT_STATE_AWAIT_TUNNEL) + { + log_printf(L_WARNING, "Got ACK tunnel frame when state is %d", state); + } + state = CLIENT_STATE_CONNECTED; return 0; } @@ -305,7 +314,7 @@ int do_client_loop(uint8_t *tox_id_str) /* * Send friend request */ - case CLIENT_STATE_INITIAL: + case CLIENT_STATE_AWAIT_FRIENDSHIP: { uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!"; uint16_t length = sizeof(data); @@ -346,11 +355,11 @@ int do_client_loop(uint8_t *tox_id_str) invitation_sent_time = time(NULL); invitations_sent++; - state = CLIENT_STATE_REQUEST_SENT; + state = CLIENT_STATE_AWAIT_FRIEND_CONNECTED; log_printf(L_INFO, "Waiting for server to accept friend request...\n"); } break; - case CLIENT_STATE_REQUEST_SENT: + case CLIENT_STATE_AWAIT_FRIEND_CONNECTED: if(friend_connection_status != TOX_CONNECTION_NONE) { const char* status = readable_connection_status(friend_connection_status); @@ -372,7 +381,7 @@ int do_client_loop(uint8_t *tox_id_str) log_printf(L_WARNING, "When sending ping packet: %u", custom_packet_error); exit(1); } - state = CLIENT_STATE_PING_SENT; + state = CLIENT_STATE_AWAIT_PONG; break; case Mode_Client_Local_Port_Forward: if(bind_sockfd < 0) @@ -384,7 +393,7 @@ int do_client_loop(uint8_t *tox_id_str) /* fall through... */ case Mode_Client_Pipe: send_tunnel_request_packet(remote_host, remote_port, friendnumber); - state = CLIENT_STATE_FORWARDING; + state = CLIENT_STATE_AWAIT_TUNNEL; break; default: log_printf(L_ERROR, "BUG: Impossible client mode at %s:%s", __FILE__, __LINE__); @@ -406,14 +415,15 @@ int do_client_loop(uint8_t *tox_id_str) exit(-1); } - state = CLIENT_STATE_INITIAL; + state = CLIENT_STATE_AWAIT_FRIENDSHIP; } } break; - case CLIENT_STATE_PING_SENT: - /* Just sit there and wait for pong */ + case CLIENT_STATE_AWAIT_TUNNEL: + break; + case CLIENT_STATE_AWAIT_PONG: break; - case CLIENT_STATE_FORWARDING: + case CLIENT_STATE_CONNECTED: { int accept_fd = 0; int select_rv = 0; diff --git a/client.h b/client.h index 028e112..d83bb2a 100644 --- a/client.h +++ b/client.h @@ -1,19 +1,15 @@ #include "main.h" -#define CLIENT_STATE_INITIAL 1 -#define CLIENT_STATE_REQUEST_SENT 2 -#define CLIENT_STATE_REQUEST_ACCEPTED 3 -#define CLIENT_STATE_PING_SENT 4 -#define CLIENT_STATE_CONNECTED 5 -#define CLIENT_STATE_PONG_RECEIVED 6 -#define CLIENT_STATE_SEND_PING 7 -#define CLIENT_STATE_REQUEST_TUNNEL 8 -#define CLIENT_STATE_WAIT_FOR_ACKTUNNEL 9 -#define CLIENT_STATE_FORWARDING 10 -#define CLIENT_STATE_SHUTDOWN 11 -#define CLIENT_STATE_BIND_PORT 12 -#define CLIENT_STATE_SETUP_PIPE 13 -#define CLIENT_STATE_CONNECTION_LOST 14 +enum CLIENT_STATE { + CLIENT_STATE_AWAIT_FRIENDSHIP, + CLIENT_STATE_AWAIT_FRIEND_CONNECTED, + CLIENT_STATE_AWAIT_PONG, + CLIENT_STATE_AWAIT_TUNNEL, + CLIENT_STATE_SEND_PING, + CLIENT_STATE_REQUEST_TUNNEL, + CLIENT_STATE_WAIT_FOR_ACKTUNNEL, + CLIENT_STATE_CONNECTED +}; int handle_pong_frame(); int handle_acktunnel_frame(protocol_frame *rcvd_frame); -- cgit v1.2.3