File main.c changed (mode: 100644) (index c3444ee..4b7a3c5) |
... |
... |
int client_pipe_mode = 0; |
22 |
22 |
/* Remote Tox ID in client mode */ |
/* Remote Tox ID in client mode */ |
23 |
23 |
char *remote_tox_id = NULL; |
char *remote_tox_id = NULL; |
24 |
24 |
|
|
|
25 |
|
/* Directory with config and tox save */ |
|
26 |
|
char config_path[500] = "/etc/tuntox/"; |
|
27 |
|
|
25 |
28 |
/* Ports and hostname for port forwarding */ |
/* Ports and hostname for port forwarding */ |
26 |
29 |
int remote_port = 0; |
int remote_port = 0; |
27 |
30 |
char *remote_host = NULL; |
char *remote_host = NULL; |
|
... |
... |
int get_client_socket(char *hostname, int port) |
203 |
206 |
|
|
204 |
207 |
if (p == NULL) { |
if (p == NULL) { |
205 |
208 |
fprintf(stderr, "failed to connect to %s:%d\n", hostname, port); |
fprintf(stderr, "failed to connect to %s:%d\n", hostname, port); |
206 |
|
exit(1); |
|
|
209 |
|
return -1; |
207 |
210 |
} |
} |
208 |
211 |
|
|
209 |
212 |
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), |
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), |
|
... |
... |
int send_frame(protocol_frame *frame, uint8_t *data) |
270 |
273 |
|
|
271 |
274 |
if(i > 0 && rv >= 0) |
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 |
279 |
return rv; |
return rv; |
|
... |
... |
int send_tunnel_request_packet(char *remote_host, int remote_port, int friend_nu |
559 |
562 |
|
|
560 |
563 |
/* End proto */ |
/* 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 |
641 |
void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) |
void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) |
563 |
642 |
{ |
{ |
564 |
643 |
unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; |
unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; |
|
... |
... |
int main(int argc, char *argv[]) |
699 |
778 |
unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; |
unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; |
700 |
779 |
int oc; |
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 |
783 |
switch(oc) |
switch(oc) |
705 |
784 |
{ |
{ |
|
... |
... |
int main(int argc, char *argv[]) |
727 |
806 |
ping_mode = 1; |
ping_mode = 1; |
728 |
807 |
break; |
break; |
729 |
808 |
case 'i': |
case 'i': |
|
809 |
|
/* Tox ID */ |
730 |
810 |
remote_tox_id = optarg; |
remote_tox_id = optarg; |
731 |
811 |
break; |
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 |
823 |
case '?': |
case '?': |
733 |
824 |
default: |
default: |
734 |
825 |
help(); |
help(); |
|
... |
... |
int main(int argc, char *argv[]) |
747 |
838 |
|
|
748 |
839 |
set_tox_username(tox); |
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 |
841 |
do_bootstrap(tox); |
do_bootstrap(tox); |
756 |
842 |
|
|
757 |
843 |
/* TODO use proper argparse */ |
/* TODO use proper argparse */ |
758 |
844 |
if(client_mode) |
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 |
851 |
if(!remote_tox_id) |
if(!remote_tox_id) |
761 |
852 |
{ |
{ |
762 |
853 |
fprintf(stderr, "Tox id is required in client mode. Use -i 58435984ABCDEF475...\n"); |
fprintf(stderr, "Tox id is required in client mode. Use -i 58435984ABCDEF475...\n"); |
|
... |
... |
int main(int argc, char *argv[]) |
768 |
859 |
{ |
{ |
769 |
860 |
/* Connect to the forwarded service */ |
/* Connect to the forwarded service */ |
770 |
861 |
// client_socket = get_client_socket(); |
// 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 |
873 |
tox_callback_friend_request(tox, accept_friend_request, NULL); |
tox_callback_friend_request(tox, accept_friend_request, NULL); |
773 |
874 |
do_server_loop(); |
do_server_loop(); |