diff options
-rw-r--r-- | main.c | 117 | ||||
-rw-r--r-- | util.c | 40 | ||||
-rw-r--r-- | util.h | 1 |
3 files changed, 150 insertions, 8 deletions
@@ -22,6 +22,9 @@ int client_pipe_mode = 0; | |||
22 | /* Remote Tox ID in client mode */ | 22 | /* Remote Tox ID in client mode */ |
23 | char *remote_tox_id = NULL; | 23 | char *remote_tox_id = NULL; |
24 | 24 | ||
25 | /* Directory with config and tox save */ | ||
26 | char config_path[500] = "/etc/tuntox/"; | ||
27 | |||
25 | /* Ports and hostname for port forwarding */ | 28 | /* Ports and hostname for port forwarding */ |
26 | int remote_port = 0; | 29 | int remote_port = 0; |
27 | char *remote_host = NULL; | 30 | char *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 */ | ||
566 | static 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 */ | ||
612 | static 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 | |||
562 | void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) | 641 | void 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(); |
@@ -1,6 +1,7 @@ | |||
1 | #include "util.h" | 1 | #include "util.h" |
2 | #include <string.h> | 2 | #include <string.h> |
3 | #include <tox/tox.h> | 3 | #include <tox/tox.h> |
4 | #include <stdio.h> | ||
4 | 5 | ||
5 | void writechecksum(uint8_t *address) | 6 | void writechecksum(uint8_t *address) |
6 | { | 7 | { |
@@ -100,3 +101,42 @@ int parse_local_port_forward(char *string, int *local_port, char **hostname, int | |||
100 | 101 | ||
101 | return 0; | 102 | return 0; |
102 | } | 103 | } |
104 | |||
105 | void* file_raw(char *path, uint32_t *size) | ||
106 | { | ||
107 | FILE *file; | ||
108 | char *data; | ||
109 | int len; | ||
110 | |||
111 | file = fopen(path, "rb"); | ||
112 | if(!file) { | ||
113 | fprintf(stderr, "File not found (%s)\n", path); | ||
114 | return NULL; | ||
115 | } | ||
116 | |||
117 | fseek(file, 0, SEEK_END); | ||
118 | len = ftell(file); | ||
119 | data = malloc(len); | ||
120 | if(!data) { | ||
121 | fclose(file); | ||
122 | return NULL; | ||
123 | } | ||
124 | |||
125 | fseek(file, 0, SEEK_SET); | ||
126 | |||
127 | if(fread(data, len, 1, file) != 1) { | ||
128 | fprintf(stderr, "Read error (%s)\n", path); | ||
129 | fclose(file); | ||
130 | free(data); | ||
131 | return NULL; | ||
132 | } | ||
133 | |||
134 | fclose(file); | ||
135 | |||
136 | fprintf(stderr, "Read %u bytes (%s)\n", len, path); | ||
137 | |||
138 | if(size) { | ||
139 | *size = len; | ||
140 | } | ||
141 | return data; | ||
142 | } | ||
@@ -10,5 +10,6 @@ void writechecksum(uint8_t *address); | |||
10 | void to_hex(char_t *a, const char_t *p, int size); | 10 | void to_hex(char_t *a, const char_t *p, int size); |
11 | void id_to_string(char_t *dest, const char_t *src); | 11 | void id_to_string(char_t *dest, const char_t *src); |
12 | int string_to_id(char_t *w, char_t *a); | 12 | int string_to_id(char_t *w, char_t *a); |
13 | void* file_raw(char *path, uint32_t *size); | ||
13 | 14 | ||
14 | #endif | 15 | #endif |