diff options
author | Andrew Cady <d@jerkface.net> | 2020-08-21 17:52:51 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2020-08-21 17:52:51 -0400 |
commit | 475d51fc1a006503f0a0096c86d29e1253555967 (patch) | |
tree | b9456f2cce10a3bcf74809bcab36f0cdbd2fce24 | |
parent | 893fab60956bf982e0dfd8fcbd80d384dd8deee3 (diff) |
set CONNECTED state in callback
-rw-r--r-- | client.c | 30 | ||||
-rw-r--r-- | client.h | 24 |
2 files changed, 30 insertions, 24 deletions
@@ -12,7 +12,7 @@ | |||
12 | const bool attempt_reconnect = true; | 12 | const bool attempt_reconnect = true; |
13 | 13 | ||
14 | /* The state machine */ | 14 | /* The state machine */ |
15 | int state = CLIENT_STATE_INITIAL; | 15 | int state = CLIENT_STATE_AWAIT_FRIENDSHIP; |
16 | 16 | ||
17 | /* Used in ping mode */ | 17 | /* Used in ping mode */ |
18 | struct timespec ping_sent_time; | 18 | struct timespec ping_sent_time; |
@@ -143,6 +143,10 @@ int handle_acktunnel_frame(protocol_frame *rcvd_frame) | |||
143 | } | 143 | } |
144 | 144 | ||
145 | tun = tunnel_create(client_tunnel.sockfd, rcvd_frame->connid, rcvd_frame->friendnumber); | 145 | tun = tunnel_create(client_tunnel.sockfd, rcvd_frame->connid, rcvd_frame->friendnumber); |
146 | if (!tun) | ||
147 | { | ||
148 | exit(1); | ||
149 | } | ||
146 | 150 | ||
147 | /* Mark that we can accept() another connection */ | 151 | /* Mark that we can accept() another connection */ |
148 | client_tunnel.sockfd = -1; | 152 | client_tunnel.sockfd = -1; |
@@ -153,6 +157,11 @@ int handle_acktunnel_frame(protocol_frame *rcvd_frame) | |||
153 | { | 157 | { |
154 | log_printf(L_INFO, "Accepted a new connection on port %d\n", local_port); | 158 | log_printf(L_INFO, "Accepted a new connection on port %d\n", local_port); |
155 | } | 159 | } |
160 | if(state != CLIENT_STATE_AWAIT_TUNNEL) | ||
161 | { | ||
162 | log_printf(L_WARNING, "Got ACK tunnel frame when state is %d", state); | ||
163 | } | ||
164 | state = CLIENT_STATE_CONNECTED; | ||
156 | return 0; | 165 | return 0; |
157 | } | 166 | } |
158 | 167 | ||
@@ -305,7 +314,7 @@ int do_client_loop(uint8_t *tox_id_str) | |||
305 | /* | 314 | /* |
306 | * Send friend request | 315 | * Send friend request |
307 | */ | 316 | */ |
308 | case CLIENT_STATE_INITIAL: | 317 | case CLIENT_STATE_AWAIT_FRIENDSHIP: |
309 | { | 318 | { |
310 | uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!"; | 319 | uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!"; |
311 | uint16_t length = sizeof(data); | 320 | uint16_t length = sizeof(data); |
@@ -346,11 +355,11 @@ int do_client_loop(uint8_t *tox_id_str) | |||
346 | 355 | ||
347 | invitation_sent_time = time(NULL); | 356 | invitation_sent_time = time(NULL); |
348 | invitations_sent++; | 357 | invitations_sent++; |
349 | state = CLIENT_STATE_REQUEST_SENT; | 358 | state = CLIENT_STATE_AWAIT_FRIEND_CONNECTED; |
350 | log_printf(L_INFO, "Waiting for server to accept friend request...\n"); | 359 | log_printf(L_INFO, "Waiting for server to accept friend request...\n"); |
351 | } | 360 | } |
352 | break; | 361 | break; |
353 | case CLIENT_STATE_REQUEST_SENT: | 362 | case CLIENT_STATE_AWAIT_FRIEND_CONNECTED: |
354 | if(friend_connection_status != TOX_CONNECTION_NONE) | 363 | if(friend_connection_status != TOX_CONNECTION_NONE) |
355 | { | 364 | { |
356 | const char* status = readable_connection_status(friend_connection_status); | 365 | const char* status = readable_connection_status(friend_connection_status); |
@@ -372,7 +381,7 @@ int do_client_loop(uint8_t *tox_id_str) | |||
372 | log_printf(L_WARNING, "When sending ping packet: %u", custom_packet_error); | 381 | log_printf(L_WARNING, "When sending ping packet: %u", custom_packet_error); |
373 | exit(1); | 382 | exit(1); |
374 | } | 383 | } |
375 | state = CLIENT_STATE_PING_SENT; | 384 | state = CLIENT_STATE_AWAIT_PONG; |
376 | break; | 385 | break; |
377 | case Mode_Client_Local_Port_Forward: | 386 | case Mode_Client_Local_Port_Forward: |
378 | if(bind_sockfd < 0) | 387 | if(bind_sockfd < 0) |
@@ -384,7 +393,7 @@ int do_client_loop(uint8_t *tox_id_str) | |||
384 | /* fall through... */ | 393 | /* fall through... */ |
385 | case Mode_Client_Pipe: | 394 | case Mode_Client_Pipe: |
386 | send_tunnel_request_packet(remote_host, remote_port, friendnumber); | 395 | send_tunnel_request_packet(remote_host, remote_port, friendnumber); |
387 | state = CLIENT_STATE_FORWARDING; | 396 | state = CLIENT_STATE_AWAIT_TUNNEL; |
388 | break; | 397 | break; |
389 | default: | 398 | default: |
390 | log_printf(L_ERROR, "BUG: Impossible client mode at %s:%s", __FILE__, __LINE__); | 399 | 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) | |||
406 | exit(-1); | 415 | exit(-1); |
407 | } | 416 | } |
408 | 417 | ||
409 | state = CLIENT_STATE_INITIAL; | 418 | state = CLIENT_STATE_AWAIT_FRIENDSHIP; |
410 | } | 419 | } |
411 | } | 420 | } |
412 | break; | 421 | break; |
413 | case CLIENT_STATE_PING_SENT: | 422 | case CLIENT_STATE_AWAIT_TUNNEL: |
414 | /* Just sit there and wait for pong */ | 423 | break; |
424 | case CLIENT_STATE_AWAIT_PONG: | ||
415 | break; | 425 | break; |
416 | case CLIENT_STATE_FORWARDING: | 426 | case CLIENT_STATE_CONNECTED: |
417 | { | 427 | { |
418 | int accept_fd = 0; | 428 | int accept_fd = 0; |
419 | int select_rv = 0; | 429 | int select_rv = 0; |
@@ -1,19 +1,15 @@ | |||
1 | #include "main.h" | 1 | #include "main.h" |
2 | 2 | ||
3 | #define CLIENT_STATE_INITIAL 1 | 3 | enum CLIENT_STATE { |
4 | #define CLIENT_STATE_REQUEST_SENT 2 | 4 | CLIENT_STATE_AWAIT_FRIENDSHIP, |
5 | #define CLIENT_STATE_REQUEST_ACCEPTED 3 | 5 | CLIENT_STATE_AWAIT_FRIEND_CONNECTED, |
6 | #define CLIENT_STATE_PING_SENT 4 | 6 | CLIENT_STATE_AWAIT_PONG, |
7 | #define CLIENT_STATE_CONNECTED 5 | 7 | CLIENT_STATE_AWAIT_TUNNEL, |
8 | #define CLIENT_STATE_PONG_RECEIVED 6 | 8 | CLIENT_STATE_SEND_PING, |
9 | #define CLIENT_STATE_SEND_PING 7 | 9 | CLIENT_STATE_REQUEST_TUNNEL, |
10 | #define CLIENT_STATE_REQUEST_TUNNEL 8 | 10 | CLIENT_STATE_WAIT_FOR_ACKTUNNEL, |
11 | #define CLIENT_STATE_WAIT_FOR_ACKTUNNEL 9 | 11 | CLIENT_STATE_CONNECTED |
12 | #define CLIENT_STATE_FORWARDING 10 | 12 | }; |
13 | #define CLIENT_STATE_SHUTDOWN 11 | ||
14 | #define CLIENT_STATE_BIND_PORT 12 | ||
15 | #define CLIENT_STATE_SETUP_PIPE 13 | ||
16 | #define CLIENT_STATE_CONNECTION_LOST 14 | ||
17 | 13 | ||
18 | int handle_pong_frame(); | 14 | int handle_pong_frame(); |
19 | int handle_acktunnel_frame(protocol_frame *rcvd_frame); | 15 | int handle_acktunnel_frame(protocol_frame *rcvd_frame); |