summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2020-08-21 14:58:37 -0400
committerAndrew Cady <d@jerkface.net>2020-08-21 14:58:37 -0400
commit43f3a5658f8d187a46fa2ecb9074982acce35728 (patch)
tree99b948b2b409f079c0cccba23a3be4312d0f97b9
parentf2153aebaacc67e59311738f74f5b6f3b76347cf (diff)
cleaning up the client state machine
-rw-r--r--client.c197
-rw-r--r--client.h2
-rw-r--r--log.h1
3 files changed, 78 insertions, 122 deletions
diff --git a/client.c b/client.c
index 0ce1cb0..7082b21 100644
--- a/client.c
+++ b/client.c
@@ -9,6 +9,8 @@
9#include "main.h" 9#include "main.h"
10#include "client.h" 10#include "client.h"
11 11
12const bool attempt_reconnect = true;
13
12/* The state machine */ 14/* The state machine */
13int state = CLIENT_STATE_INITIAL; 15int state = CLIENT_STATE_INITIAL;
14 16
@@ -296,8 +298,6 @@ int do_client_loop(uint8_t *tox_id_str)
296 signal(SIGPIPE, SIG_IGN); 298 signal(SIGPIPE, SIG_IGN);
297 } 299 }
298 300
299 log_printf(L_INFO, "Connecting to Tox...\n");
300
301 while(1) 301 while(1)
302 { 302 {
303 /* Let tox do its stuff */ 303 /* Let tox do its stuff */
@@ -309,16 +309,9 @@ int do_client_loop(uint8_t *tox_id_str)
309 * Send friend request 309 * Send friend request
310 */ 310 */
311 case CLIENT_STATE_INITIAL: 311 case CLIENT_STATE_INITIAL:
312 if(connection_status != TOX_CONNECTION_NONE)
313 {
314 state = CLIENT_STATE_CONNECTED;
315 }
316 break;
317 case CLIENT_STATE_CONNECTED:
318 { 312 {
319 uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!"; 313 uint8_t* data = (uint8_t *)"Hi, fellow tuntox instance!";
320 uint16_t length = sizeof(data); 314 uint16_t length = sizeof(data);
321 /* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1168 */
322 TOX_ERR_FRIEND_ADD add_error; 315 TOX_ERR_FRIEND_ADD add_error;
323 316
324 if(use_shared_secret) 317 if(use_shared_secret)
@@ -329,13 +322,13 @@ int do_client_loop(uint8_t *tox_id_str)
329 log_printf(L_DEBUG, "Sent shared secret of length %u\n", length); 322 log_printf(L_DEBUG, "Sent shared secret of length %u\n", length);
330 } 323 }
331 324
332 if(invitations_sent == 0) 325 if(invitations_sent > 0)
333 { 326 {
334 log_printf(L_INFO, "Connected. Sending friend request.\n"); 327 log_printf(L_INFO, "Sending friend request #%d.", invitations_sent);
335 } 328 }
336 else 329 else
337 { 330 {
338 log_printf(L_INFO, "Sending another friend request.\n"); 331 log_printf(L_INFO, "Sending friend request.");
339 } 332 }
340 333
341 friendnumber = tox_friend_add( 334 friendnumber = tox_friend_add(
@@ -356,123 +349,84 @@ int do_client_loop(uint8_t *tox_id_str)
356 349
357 invitation_sent_time = time(NULL); 350 invitation_sent_time = time(NULL);
358 invitations_sent++; 351 invitations_sent++;
359 state = CLIENT_STATE_SENTREQUEST; 352 state = CLIENT_STATE_REQUEST_SENT;
360 log_printf(L_INFO, "Waiting for friend to accept us...\n"); 353 log_printf(L_INFO, "Waiting for server to accept friend request...\n");
361 } 354 }
362 break; 355 break;
363 case CLIENT_STATE_SENTREQUEST: 356 case CLIENT_STATE_REQUEST_SENT:
357 if(friend_connection_status != TOX_CONNECTION_NONE)
364 { 358 {
359 const char* status = readable_connection_status(friend_connection_status);
360 log_printf(L_INFO, "Friend request accepted (%s)!\n", status);
361 state = CLIENT_STATE_REQUEST_ACCEPTED;
362 }
363 else
364 {
365 const int INVITATION_SEND_INTERVAL = 90;
366 if (time(NULL) - invitation_sent_time > INVITATION_SEND_INTERVAL)
365 { 367 {
366 if(friend_connection_status != TOX_CONNECTION_NONE) 368 TOX_ERR_FRIEND_DELETE error = 0;
369
370 log_printf(L_INFO, "Friend request timed out. Sending another...");
371 tox_friend_delete(tox, friendnumber, &error);
372 if(error != TOX_ERR_FRIEND_DELETE_OK)
367 { 373 {
368 const char* status = readable_connection_status(friend_connection_status); 374 log_printf(L_ERROR, "Error %u deleting friend before reconnection\n", error);
369 log_printf(L_INFO, "Friend request accepted (%s)!\n", status); 375 exit(-1);
370 state = CLIENT_STATE_REQUEST_ACCEPTED;
371 } 376 }
372 else
373 {
374 const int INVITATION_SEND_INTERVAL = 90;
375 if (time(NULL) - invitation_sent_time > INVITATION_SEND_INTERVAL)
376 {
377 TOX_ERR_FRIEND_DELETE error = 0;
378
379 log_printf(L_INFO, "Sending another friend request...");
380 tox_friend_delete(
381 tox,
382 friendnumber,
383 &error);
384 if(error != TOX_ERR_FRIEND_DELETE_OK)
385 {
386 log_printf(L_ERROR, "Error %u deleting friend before reconnection\n", error);
387 exit(-1);
388 }
389 377
390 state = CLIENT_STATE_CONNECTED; 378 state = CLIENT_STATE_INITIAL;
391 }
392 }
393 } 379 }
394 break;
395 } 380 }
381 break;
396 case CLIENT_STATE_REQUEST_ACCEPTED: 382 case CLIENT_STATE_REQUEST_ACCEPTED:
397 switch (program_mode) { 383 switch (program_mode) {
398 case Mode_Client_Ping: 384 case Mode_Client_Ping:
399 state = CLIENT_STATE_SEND_PING; 385 /* Send the ping packet */
386 {
387 uint8_t data[] = {
388 0xa2, 0x6a, 0x01, 0x08, 0x00, 0x00, 0x00, 0x05,
389 0x48, 0x65, 0x6c, 0x6c, 0x6f
390 };
391 clock_gettime(CLOCK_MONOTONIC, &ping_sent_time);
392 tox_friend_send_lossless_packet(tox, friendnumber, data, sizeof(data), &custom_packet_error);
393 }
394 if(custom_packet_error == TOX_ERR_FRIEND_CUSTOM_PACKET_OK)
395 {
396 state = CLIENT_STATE_PING_SENT;
397 }
398 else
399 {
400 log_printf(L_WARNING, "When sending ping packet: %u", custom_packet_error);
401 }
400 break; 402 break;
401 case Mode_Client_Pipe: 403 case Mode_Client_Pipe:
402 state = CLIENT_STATE_SETUP_PIPE; 404 send_tunnel_request_packet(remote_host, remote_port, friendnumber);
405 state = CLIENT_STATE_FORWARDING;
403 break; 406 break;
404 case Mode_Client_Local_Port_Forward: 407 case Mode_Client_Local_Port_Forward:
405 state = CLIENT_STATE_BIND_PORT; 408 if(bind_sockfd < 0)
409 {
410 log_printf(L_ERROR, "Shutting down - could not bind to listening port\n");
411 exit(1);
412 }
413 state = CLIENT_STATE_FORWARDING;
406 break; 414 break;
407 default: 415 default:
408 log_printf(L_ERROR, "BUG: Impossible client mode at %s:%s", __FILE__, __LINE__); 416 log_printf(L_ERROR, "BUG: Impossible client mode at %s:%s", __FILE__, __LINE__);
409 exit(1); 417 exit(1);
410 } 418 }
411 break; 419 break;
412 case CLIENT_STATE_SEND_PING:
413 /* Send the ping packet */
414 {
415 uint8_t data[] = {
416 0xa2, 0x6a, 0x01, 0x08, 0x00, 0x00, 0x00, 0x05,
417 0x48, 0x65, 0x6c, 0x6c, 0x6f
418 };
419
420 clock_gettime(CLOCK_MONOTONIC, &ping_sent_time);
421 tox_friend_send_lossless_packet(
422 tox,
423 friendnumber,
424 data,
425 sizeof(data),
426 &custom_packet_error
427 );
428 }
429 if(custom_packet_error == TOX_ERR_FRIEND_CUSTOM_PACKET_OK)
430 {
431 state = CLIENT_STATE_PING_SENT;
432 }
433 else
434 {
435 log_printf(L_WARNING, "When sending ping packet: %u", custom_packet_error);
436 }
437 break;
438 case CLIENT_STATE_PING_SENT: 420 case CLIENT_STATE_PING_SENT:
439 /* Just sit there and wait for pong */ 421 /* Just sit there and wait for pong */
440 break; 422 break;
441
442 case CLIENT_STATE_BIND_PORT:
443 if(bind_sockfd < 0)
444 {
445 log_printf(L_ERROR, "Shutting down - could not bind to listening port\n");
446 state = CLIENT_STATE_SHUTDOWN;
447 }
448 else
449 {
450 state = CLIENT_STATE_FORWARDING;
451 }
452 break;
453 case CLIENT_STATE_SETUP_PIPE:
454 send_tunnel_request_packet(
455 remote_host,
456 remote_port,
457 friendnumber
458 );
459 state = CLIENT_STATE_FORWARDING;
460 break;
461 case CLIENT_STATE_REQUEST_TUNNEL: 423 case CLIENT_STATE_REQUEST_TUNNEL:
462 send_tunnel_request_packet( 424 send_tunnel_request_packet(remote_host, remote_port, friendnumber);
463 remote_host,
464 remote_port,
465 friendnumber
466 );
467 state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL; 425 state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL;
468 break; 426 break;
469 case CLIENT_STATE_WAIT_FOR_ACKTUNNEL: 427 case CLIENT_STATE_WAIT_FOR_ACKTUNNEL:
470 client_tunnel.sockfd = 0; 428 /* client_tunnel.sockfd = 0; */
471 send_tunnel_request_packet( 429 /* send_tunnel_request_packet(remote_host, remote_port, friendnumber); */
472 remote_host,
473 remote_port,
474 friendnumber
475 );
476 break; 430 break;
477 case CLIENT_STATE_FORWARDING: 431 case CLIENT_STATE_FORWARDING:
478 { 432 {
@@ -514,7 +468,7 @@ int do_client_loop(uint8_t *tox_id_str)
514 } 468 }
515 else 469 else
516 { 470 {
517 log_printf(L_DEBUG2, "Nothing to read..."); 471 log_printf(L_DEBUG3, "Nothing to read...");
518 } 472 }
519 } 473 }
520 else 474 else
@@ -587,31 +541,32 @@ int do_client_loop(uint8_t *tox_id_str)
587 } 541 }
588 break; 542 break;
589 case CLIENT_STATE_CONNECTION_LOST: 543 case CLIENT_STATE_CONNECTION_LOST:
544 /* Trying to reconnect here does not seem like a good idea. */
545 if(!attempt_reconnect)
590 { 546 {
591 { 547 log_printf(L_DEBUG, "Exiting on CLIENT_STATE_CONNECTION_LOST");
592 if(friend_connection_status == TOX_CONNECTION_NONE) 548 exit(1);
593 { 549 }
594 /* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1267 */
595 TOX_ERR_FRIEND_DELETE tox_delete_error;
596 550
597 log_printf(L_WARNING, "Lost connection to server, closing all tunnels and re-adding friend\n"); 551 if(friend_connection_status == TOX_CONNECTION_NONE)
598 client_close_all_connections(); 552 {
599 tox_friend_delete(tox, friendnumber, &tox_delete_error); 553 /* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1267 */
600 if(tox_delete_error) 554 TOX_ERR_FRIEND_DELETE tox_delete_error;
601 { 555
602 log_printf(L_ERROR, "Error when deleting server from friend list: %d\n", tox_delete_error); 556 log_printf(L_WARNING, "Lost connection to server, closing all tunnels and re-adding friend\n");
603 } 557 client_close_all_connections();
604 state = CLIENT_STATE_INITIAL; 558 tox_friend_delete(tox, friendnumber, &tox_delete_error);
605 } 559 if(tox_delete_error)
606 else 560 {
607 { 561 log_printf(L_ERROR, "Error when deleting server from friend list: %d\n", tox_delete_error);
608 state = CLIENT_STATE_FORWARDING;
609 }
610 } 562 }
563 state = CLIENT_STATE_INITIAL;
564 }
565 else
566 {
567 state = CLIENT_STATE_FORWARDING;
611 } 568 }
612 break; 569 break;
613 case 0xffffffff:
614 log_printf(L_ERROR, "You forgot a break statement\n");
615 case CLIENT_STATE_SHUTDOWN: 570 case CLIENT_STATE_SHUTDOWN:
616 exit(0); 571 exit(0);
617 break; 572 break;
diff --git a/client.h b/client.h
index be68f25..028e112 100644
--- a/client.h
+++ b/client.h
@@ -1,7 +1,7 @@
1#include "main.h" 1#include "main.h"
2 2
3#define CLIENT_STATE_INITIAL 1 3#define CLIENT_STATE_INITIAL 1
4#define CLIENT_STATE_SENTREQUEST 2 4#define CLIENT_STATE_REQUEST_SENT 2
5#define CLIENT_STATE_REQUEST_ACCEPTED 3 5#define CLIENT_STATE_REQUEST_ACCEPTED 3
6#define CLIENT_STATE_PING_SENT 4 6#define CLIENT_STATE_PING_SENT 4
7#define CLIENT_STATE_CONNECTED 5 7#define CLIENT_STATE_CONNECTED 5
diff --git a/log.h b/log.h
index bcd4c9b..ee25a14 100644
--- a/log.h
+++ b/log.h
@@ -6,6 +6,7 @@
6#define L_INFO 6 6#define L_INFO 6
7#define L_DEBUG 7 7#define L_DEBUG 7
8#define L_DEBUG2 8 8#define L_DEBUG2 8
9#define L_DEBUG3 9
9 10
10#define L_UNSET 0x29a 11#define L_UNSET 0x29a
11 12