summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorGDR! <gdr@go2.pl>2014-11-26 13:55:12 +0100
committerGDR! <gdr@go2.pl>2014-11-26 13:55:12 +0100
commit7cb0f811c9a573342ec390080ac67e640d0f2e30 (patch)
tree16bf9d1ee4f806b61f56d8e3bb57276ffd8bd42e /main.c
parent16add85eb81629b07dd7bdb04fbe30be1410cc83 (diff)
Server state loading/saving
Diffstat (limited to 'main.c')
-rw-r--r--main.c117
1 files changed, 109 insertions, 8 deletions
diff --git a/main.c b/main.c
index c3444ee..4b7a3c5 100644
--- a/main.c
+++ b/main.c
@@ -22,6 +22,9 @@ int client_pipe_mode = 0;
22/* Remote Tox ID in client mode */ 22/* Remote Tox ID in client mode */
23char *remote_tox_id = NULL; 23char *remote_tox_id = NULL;
24 24
25/* Directory with config and tox save */
26char config_path[500] = "/etc/tuntox/";
27
25/* Ports and hostname for port forwarding */ 28/* Ports and hostname for port forwarding */
26int remote_port = 0; 29int remote_port = 0;
27char *remote_host = NULL; 30char *remote_host = NULL;
@@ -203,7 +206,7 @@ int get_client_socket(char *hostname, int port)
203 206
204 if (p == NULL) { 207 if (p == NULL) {
205 fprintf(stderr, "failed to connect to %s:%d\n", hostname, port); 208 fprintf(stderr, "failed to connect to %s:%d\n", hostname, port);
206 exit(1); 209 return -1;
207 } 210 }
208 211
209 inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), 212 inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
@@ -270,7 +273,7 @@ int send_frame(protocol_frame *frame, uint8_t *data)
270 273
271 if(i > 0 && rv >= 0) 274 if(i > 0 && rv >= 0)
272 { 275 {
273 fprintf(stderr, "Packet succeeded at try %d", i+1); 276 fprintf(stderr, "Packet succeeded at try %d\n", i+1);
274 } 277 }
275 278
276 return rv; 279 return rv;
@@ -559,6 +562,82 @@ int send_tunnel_request_packet(char *remote_host, int remote_port, int friend_nu
559 562
560/* End proto */ 563/* End proto */
561 564
565/* Save tox identity to a file */
566static void write_save(Tox *tox)
567{
568 void *data;
569 uint32_t size;
570 uint8_t path_tmp[512], path_real[512], *p;
571 FILE *file;
572
573 size = tox_size(tox);
574 data = malloc(size);
575 tox_save(tox, data);
576
577 strncpy(path_real, config_path, sizeof(config_path));
578
579 p = path_real + strlen(path_real);
580 memcpy(p, "tox_save", sizeof("tox_save"));
581
582 unsigned int path_len = (p - path_real) + sizeof("tox_save");
583 memcpy(path_tmp, path_real, path_len);
584 memcpy(path_tmp + (path_len - 1), ".tmp", sizeof(".tmp"));
585
586 file = fopen((char*)path_tmp, "wb");
587 if(file) {
588 fwrite(data, size, 1, file);
589 fflush(file);
590 fclose(file);
591 if (rename((char*)path_tmp, (char*)path_real) != 0) {
592 fprintf(stderr, "Failed to rename file. %s to %s deleting and trying again\n", path_tmp, path_real);
593 remove((const char *)path_real);
594 if (rename((char*)path_tmp, (char*)path_real) != 0) {
595 fprintf(stderr, "Saving Failed\n");
596 } else {
597 fprintf(stderr, "Saved data\n");
598 }
599 } else {
600 fprintf(stderr, "Saved data\n");
601 }
602 }
603 else
604 {
605 fprintf(stderr, "Could not open save file\n");
606 }
607
608 free(data);
609}
610
611/* Load tox identity from a file */
612static int load_save(Tox *tox)
613{
614 void *data;
615 uint32_t size;
616 uint8_t path_tmp[512], path_real[512], *p;
617 FILE *file;
618
619 strncpy(path_real, config_path, sizeof(config_path));
620
621 p = path_real + strlen(path_real);
622 memcpy(p, "tox_save", sizeof("tox_save"));
623
624 unsigned int path_len = (p - path_real) + sizeof("tox_save");
625
626 data = file_raw((char *)path_real, &size);
627
628 if(data)
629 {
630 tox_load(tox, data, size);
631 free(data);
632 return 1;
633 }
634 else
635 {
636 fprintf(stderr, "Could not open save file\n");
637 return 0;
638 }
639}
640
562void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) 641void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata)
563{ 642{
564 unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; 643 unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1];
@@ -699,7 +778,7 @@ int main(int argc, char *argv[])
699 unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; 778 unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1];
700 int oc; 779 int oc;
701 780
702 while ((oc = getopt(argc, argv, "L:pi:")) != -1) 781 while ((oc = getopt(argc, argv, "L:pi:C:")) != -1)
703 { 782 {
704 switch(oc) 783 switch(oc)
705 { 784 {
@@ -727,8 +806,20 @@ int main(int argc, char *argv[])
727 ping_mode = 1; 806 ping_mode = 1;
728 break; 807 break;
729 case 'i': 808 case 'i':
809 /* Tox ID */
730 remote_tox_id = optarg; 810 remote_tox_id = optarg;
731 break; 811 break;
812 case 'C':
813 /* Config directory */
814 strncpy(config_path, optarg, sizeof(config_path) - 1);
815 if(optarg[strlen(optarg) - 1] != '/')
816 {
817 int optarg_len = strlen(optarg);
818
819 config_path[optarg_len] = '/';
820 config_path[optarg_len + 1] = '\0';
821 }
822 break;
732 case '?': 823 case '?':
733 default: 824 default:
734 help(); 825 help();
@@ -747,16 +838,16 @@ int main(int argc, char *argv[])
747 838
748 set_tox_username(tox); 839 set_tox_username(tox);
749 840
750 tox_get_address(tox, tox_id);
751 id_to_string(tox_printable_id, tox_id);
752 tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0';
753 printf("Generated Tox ID: %s\n", tox_printable_id);
754
755 do_bootstrap(tox); 841 do_bootstrap(tox);
756 842
757 /* TODO use proper argparse */ 843 /* TODO use proper argparse */
758 if(client_mode) 844 if(client_mode)
759 { 845 {
846 tox_get_address(tox, tox_id);
847 id_to_string(tox_printable_id, tox_id);
848 tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0';
849 printf("Generated Tox ID: %s\n", tox_printable_id);
850
760 if(!remote_tox_id) 851 if(!remote_tox_id)
761 { 852 {
762 fprintf(stderr, "Tox id is required in client mode. Use -i 58435984ABCDEF475...\n"); 853 fprintf(stderr, "Tox id is required in client mode. Use -i 58435984ABCDEF475...\n");
@@ -768,6 +859,16 @@ int main(int argc, char *argv[])
768 { 859 {
769 /* Connect to the forwarded service */ 860 /* Connect to the forwarded service */
770// client_socket = get_client_socket(); 861// client_socket = get_client_socket();
862 if(!load_save(tox))
863 {
864 /* Write generated ID if one is not already present */
865 write_save(tox);
866 }
867
868 tox_get_address(tox, tox_id);
869 id_to_string(tox_printable_id, tox_id);
870 tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0';
871 printf("Using Tox ID: %s\n", tox_printable_id);
771 872
772 tox_callback_friend_request(tox, accept_friend_request, NULL); 873 tox_callback_friend_request(tox, accept_friend_request, NULL);
773 do_server_loop(); 874 do_server_loop();