summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2020-08-21 16:45:25 -0400
committerAndrew Cady <d@jerkface.net>2020-08-21 16:45:25 -0400
commitb9470442a3199a58b1665350ec0d285ca169167c (patch)
tree45bdb7d95e75d6663e3e4a566220e677e032f693
parent8ee3c22920f77a9dc36780c31018310203784d3c (diff)
It's broken.
-rw-r--r--client.c115
1 files changed, 39 insertions, 76 deletions
diff --git a/client.c b/client.c
index 7082b21..2879f40 100644
--- a/client.c
+++ b/client.c
@@ -39,7 +39,8 @@ int handle_pong_frame()
39 39
40 if(program_mode == Mode_Client_Ping) 40 if(program_mode == Mode_Client_Ping)
41 { 41 {
42 state = CLIENT_STATE_SHUTDOWN; 42 log_printf(L_INFO, "Ping mode successful. Exiting.");
43 exit(0);
43 } 44 }
44 return 0; 45 return 0;
45} 46}
@@ -324,7 +325,7 @@ int do_client_loop(uint8_t *tox_id_str)
324 325
325 if(invitations_sent > 0) 326 if(invitations_sent > 0)
326 { 327 {
327 log_printf(L_INFO, "Sending friend request #%d.", invitations_sent); 328 log_printf(L_INFO, "Sending friend request #%d.", invitations_sent+1);
328 } 329 }
329 else 330 else
330 { 331 {
@@ -358,7 +359,40 @@ int do_client_loop(uint8_t *tox_id_str)
358 { 359 {
359 const char* status = readable_connection_status(friend_connection_status); 360 const char* status = readable_connection_status(friend_connection_status);
360 log_printf(L_INFO, "Friend request accepted (%s)!\n", status); 361 log_printf(L_INFO, "Friend request accepted (%s)!\n", status);
361 state = CLIENT_STATE_REQUEST_ACCEPTED; 362
363 switch (program_mode) {
364 case Mode_Client_Ping:
365 /* Send the ping packet */
366 {
367 uint8_t data[] = {
368 0xa2, 0x6a, 0x01, 0x08, 0x00, 0x00, 0x00, 0x05,
369 0x48, 0x65, 0x6c, 0x6c, 0x6f
370 };
371 clock_gettime(CLOCK_MONOTONIC, &ping_sent_time);
372 tox_friend_send_lossless_packet(tox, friendnumber, data, sizeof(data), &custom_packet_error);
373 }
374 if(custom_packet_error != TOX_ERR_FRIEND_CUSTOM_PACKET_OK)
375 {
376 log_printf(L_WARNING, "When sending ping packet: %u", custom_packet_error);
377 exit(1);
378 }
379 state = CLIENT_STATE_PING_SENT;
380 break;
381 case Mode_Client_Local_Port_Forward:
382 if(bind_sockfd < 0)
383 {
384 log_printf(L_ERROR, "Shutting down - could not bind to listening port\n");
385 exit(1);
386 }
387 /* fall through... */
388 case Mode_Client_Pipe:
389 send_tunnel_request_packet(remote_host, remote_port, friendnumber);
390 state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL;
391 break;
392 default:
393 log_printf(L_ERROR, "BUG: Impossible client mode at %s:%s", __FILE__, __LINE__);
394 exit(1);
395 }
362 } 396 }
363 else 397 else
364 { 398 {
@@ -379,51 +413,9 @@ int do_client_loop(uint8_t *tox_id_str)
379 } 413 }
380 } 414 }
381 break; 415 break;
382 case CLIENT_STATE_REQUEST_ACCEPTED:
383 switch (program_mode) {
384 case Mode_Client_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 }
402 break;
403 case Mode_Client_Pipe:
404 send_tunnel_request_packet(remote_host, remote_port, friendnumber);
405 state = CLIENT_STATE_FORWARDING;
406 break;
407 case Mode_Client_Local_Port_Forward:
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;
414 break;
415 default:
416 log_printf(L_ERROR, "BUG: Impossible client mode at %s:%s", __FILE__, __LINE__);
417 exit(1);
418 }
419 break;
420 case CLIENT_STATE_PING_SENT: 416 case CLIENT_STATE_PING_SENT:
421 /* Just sit there and wait for pong */ 417 /* Just sit there and wait for pong */
422 break; 418 break;
423 case CLIENT_STATE_REQUEST_TUNNEL:
424 send_tunnel_request_packet(remote_host, remote_port, friendnumber);
425 state = CLIENT_STATE_WAIT_FOR_ACKTUNNEL;
426 break;
427 case CLIENT_STATE_WAIT_FOR_ACKTUNNEL: 419 case CLIENT_STATE_WAIT_FOR_ACKTUNNEL:
428 /* client_tunnel.sockfd = 0; */ 420 /* client_tunnel.sockfd = 0; */
429 /* send_tunnel_request_packet(remote_host, remote_port, friendnumber); */ 421 /* send_tunnel_request_packet(remote_host, remote_port, friendnumber); */
@@ -536,39 +528,10 @@ int do_client_loop(uint8_t *tox_id_str)
536 /* Check friend connection status changes */ 528 /* Check friend connection status changes */
537 if(friend_connection_status == TOX_CONNECTION_NONE) 529 if(friend_connection_status == TOX_CONNECTION_NONE)
538 { 530 {
539 state = CLIENT_STATE_CONNECTION_LOST; 531 log_printf(L_ERROR, "Lost connection to server. Exiting.");
540 } 532 exit(1);
541 }
542 break;
543 case CLIENT_STATE_CONNECTION_LOST:
544 /* Trying to reconnect here does not seem like a good idea. */
545 if(!attempt_reconnect)
546 {
547 log_printf(L_DEBUG, "Exiting on CLIENT_STATE_CONNECTION_LOST");
548 exit(1);
549 }
550
551 if(friend_connection_status == TOX_CONNECTION_NONE)
552 {
553 /* https://github.com/TokTok/c-toxcore/blob/acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8/toxcore/tox.h#L1267 */
554 TOX_ERR_FRIEND_DELETE tox_delete_error;
555
556 log_printf(L_WARNING, "Lost connection to server, closing all tunnels and re-adding friend\n");
557 client_close_all_connections();
558 tox_friend_delete(tox, friendnumber, &tox_delete_error);
559 if(tox_delete_error)
560 {
561 log_printf(L_ERROR, "Error when deleting server from friend list: %d\n", tox_delete_error);
562 } 533 }
563 state = CLIENT_STATE_INITIAL;
564 } 534 }
565 else
566 {
567 state = CLIENT_STATE_FORWARDING;
568 }
569 break;
570 case CLIENT_STATE_SHUTDOWN:
571 exit(0);
572 break; 535 break;
573 } 536 }
574 537