From 64ca4b5db23dbb68d1b01e307837c9fb89283256 Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Tue, 10 Sep 2013 22:59:33 +0200 Subject: tox.*, DHT.*: - return to the caller if the string could be resolved into an IP other/DHT_bootstrap.c, testing/*_test.c, testing/nTox.c: - parse cmdline for --ipv4/--ipv6 switch to allow user a choice util.h: - proper old-style C-comment --- testing/DHT_test.c | 52 +++++++++++++++++++++++----- testing/Lossless_UDP_testclient.c | 48 ++++++++++++++++++++----- testing/Lossless_UDP_testserver.c | 41 ++++++++++++++++++---- testing/Messenger_test.c | 55 ++++++++++++++++++++++++----- testing/nTox.c | 73 +++++++++++++++++++++++++++------------ 5 files changed, 214 insertions(+), 55 deletions(-) (limited to 'testing') diff --git a/testing/DHT_test.c b/testing/DHT_test.c index caa0cde2..61762162 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -133,8 +133,40 @@ void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port) int main(int argc, char *argv[]) { - /* let use decide by cmdline: TODO */ - uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; + if (argc < 4) { + printf("Usage: %s [--ipv4|--ipv6] ip port public_key\n", argv[0]); + exit(0); + } + + /* let user override default by cmdline */ + uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ + + int argvoffset = 0, argi; + for(argi = 1; argi < argc; argi++) + if (!strncasecmp(argv[argi], "--ipv", 5)) { + if (argv[argi][5] && !argv[argi][6]) { + char c = argv[argi][5]; + if (c == '4') + ipv6enabled = 0; + else if (c == '6') + ipv6enabled = 1; + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + exit(1); + } + } + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + exit(1); + } + + if (argvoffset != argi - 1) { + printf("Argument must come first: %s.\n", argv[argi]); + exit(1); + } + + argvoffset++; + } //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); /* initialize networking */ @@ -144,11 +176,6 @@ int main(int argc, char *argv[]) DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); - if (argc < 4) { - printf("usage %s ip port public_key\n", argv[0]); - exit(0); - } - new_keys(dht->c); printf("OUR ID: "); uint32_t i; @@ -168,9 +195,16 @@ int main(int argc, char *argv[]) DHT_addfriend(dht, hex_string_to_bin(temp_id)); - perror("Initialization"); - DHT_bootstrap_ex(dht, argv[1], ipv6enabled, htons(atoi(argv[2])), hex_string_to_bin(argv[3])); + + uint16_t port = htons(atoi(argv[argvoffset + 2])); + unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); + int res = DHT_bootstrap_ex(dht, argv[argvoffset + 1], ipv6enabled, port, binary_string); + free(binary_string); + if (!res) { + printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); + return 1; + } /* IP_Port ip_port; diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c index 564b2ecf..5d4c8547 100644 --- a/testing/Lossless_UDP_testclient.c +++ b/testing/Lossless_UDP_testclient.c @@ -151,21 +151,50 @@ void printconnection(int connection_id) int main(int argc, char *argv[]) { - /* let use decide by cmdline: TODO */ - uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; + /* let user override default by cmdline */ + uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ + + int argvoffset = 0, argi; + for(argi = 1; argi < argc; argi++) + if (!strncasecmp(argv[argi], "--ipv", 5)) { + if (argv[argi][5] && !argv[argi][6]) { + char c = argv[argi][5]; + if (c == '4') + ipv6enabled = 0; + else if (c == '6') + ipv6enabled = 1; + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + exit(1); + } + } + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + exit(1); + } + + if (argvoffset != argi - 1) { + printf("Argument must come first: %s.\n", argv[argi]); + exit(1); + } - if (argc < 4) { - printf("usage: %s ip port filename\n", argv[0]); + argvoffset++; + } + + if (argc < argvoffset + 4) { + printf("Usage: %s [--ipv4|--ipv6] ip port filename\n", argv[0]); exit(0); } uint8_t buffer[512]; int read; - FILE *file = fopen(argv[3], "rb"); + FILE *file = fopen(argv[argvoffset + 3], "rb"); - if (file == NULL) + if (file == NULL) { + printf("Failed to open file \"%s\".\n", argv[argvoffset + 3]); return 1; + } /* initialize networking */ @@ -178,8 +207,11 @@ int main(int argc, char *argv[]) IP_Port serverip; ip_init(&serverip.ip, ipv6enabled); - addr_resolve(argv[1], &serverip.ip); - serverip.port = htons(atoi(argv[2])); + if (!addr_resolve(argv[argvoffset + 1], &serverip.ip)) { + printf("Failed to convert \"%s\" into an IP address.\n", argv[argvoffset + 1]); + return 1; + } + serverip.port = htons(atoi(argv[argvoffset + 2])); printip(serverip); int connection = new_connection(ludp, serverip); diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index 3e54e9be..52dbcc80 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c @@ -147,21 +147,50 @@ void printconnection(int connection_id) int main(int argc, char *argv[]) { - /* let use decide by cmdline: TODO */ - uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; + /* let user override default by cmdline */ + uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ + + int argvoffset = 0, argi; + for(argi = 1; argi < argc; argi++) + if (!strncasecmp(argv[argi], "--ipv", 5)) { + if (argv[argi][5] && !argv[argi][6]) { + char c = argv[argi][5]; + if (c == '4') + ipv6enabled = 0; + else if (c == '6') + ipv6enabled = 1; + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + exit(1); + } + } + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + exit(1); + } + + if (argvoffset != argi - 1) { + printf("Argument must come first: %s.\n", argv[argi]); + exit(1); + } - if (argc < 2) { - printf("usage: %s filename\n", argv[0]); + argvoffset++; + } + + if (argc < argvoffset + 2) { + printf("Usage: %s [--ipv4|--ipv6] filename\n", argv[0]); exit(0); } uint8_t buffer[512]; int read; - FILE *file = fopen(argv[1], "wb"); + FILE *file = fopen(argv[argvoffset + 1], "wb"); - if (file == NULL) + if (file == NULL) { + printf("Failed to open file \"%s\".\n", argv[argvoffset + 1]); return 1; + } //initialize networking diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 73d44efb..e7a75c8c 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -95,13 +95,44 @@ void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t len int main(int argc, char *argv[]) { - if (argc < 4 && argc != 2) { - printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]); + /* let user override default by cmdline */ + uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ + + int argvoffset = 0, argi; + for(argi = 1; argi < argc; argi++) + if (!strncasecmp(argv[argi], "--ipv", 5)) { + if (argv[argi][5] && !argv[argi][6]) { + char c = argv[argi][5]; + if (c == '4') + ipv6enabled = 0; + else if (c == '6') + ipv6enabled = 1; + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + exit(1); + } + } + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + exit(1); + } + + if (argvoffset != argi - 1) { + printf("Argument must come first: %s.\n", argv[argi]); + exit(1); + } + + argvoffset++; + } + + /* with optional --ipvx, now it can be 1-4 arguments... */ + if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) { + printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]); + printf("or\n"); + printf(" %s [--ipv4|--ipv6] Save.bak (to read Save.bak as state file)\n", argv[0], argv[0]); exit(0); } - /* IPv6: maybe allow from cmdline --ipv6? sticking to IPv4 for now */ - uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; m = initMessenger(ipv6enabled); if ( !m ) { @@ -109,13 +140,19 @@ int main(int argc, char *argv[]) exit(0); } - if (argc > 3) { - uint16_t port = htons(atoi(argv[2])); - DHT_bootstrap_ex(m->dht, argv[1], ipv6enabled, port, hex_string_to_bin(argv[3])); + if (argc == argvoffset + 4) { + uint16_t port = htons(atoi(argv[argvoffset + 2])); + uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); + int res = DHT_bootstrap_ex(m->dht, argv[argvoffset + 1], ipv6enabled, port, bootstrap_key); + free(bootstrap_key); + if (!res) { + printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); + exit(1); + } } else { - FILE *file = fopen(argv[1], "rb"); - + FILE *file = fopen(argv[argvoffset + 1], "rb"); if ( file == NULL ) { + printf("Failed to open \"%s\" - does it exist?\n", argv[argvoffset + 1]); return 1; } diff --git a/testing/nTox.c b/testing/nTox.c index 6084aeda..6eeec5d3 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -537,8 +537,40 @@ void print_help(void) int main(int argc, char *argv[]) { - /* let use decide by cmdline: TODO */ - uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; + if (argc < 4) { + printf("Usage: %s [--ipv4|--ipv6] IP PORT KEY [-f keyfile]\n", argv[0]); + exit(0); + } + + /* let user override default by cmdline */ + uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ + + int argvoffset = 0, argi; + for(argi = 1; argi < argc; argi++) + if (!strncasecmp(argv[argi], "--ipv", 5)) { + if (argv[argi][5] && !argv[argi][6]) { + char c = argv[argi][5]; + if (c == '4') + ipv6enabled = 0; + else if (c == '6') + ipv6enabled = 1; + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + exit(1); + } + } + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + exit(1); + } + + if (argvoffset != argi - 1) { + printf("Argument must come first: %s.\n", argv[argi]); + exit(1); + } + + argvoffset++; + } int on = 0; int c = 0; @@ -547,27 +579,16 @@ int main(int argc, char *argv[]) char idstring[200] = {0}; Tox *m; - if (argc < 4) { - printf("[!] Usage: %s [IP] [port] [public_key] \n", argv[0]); + if ((argc == 2) && !strcmp(argv[1], "-h")) { + print_help(); exit(0); } - for (i = 0; i < argc; i++) { - if (argv[i] == NULL) { - break; - } else if (argv[i][0] == '-') { - if (argv[i][1] == 'h') { - print_help(); - exit(0); - } else if (argv[i][1] == 'f') { - if (argv[i + 1] != NULL) - filename = argv[i + 1]; - else { - fputs("[!] you passed '-f' without giving an argument!\n", stderr); - } - } - } - } + /* [-f keyfile] MUST be last two arguments, no point in walking over the list + * especially not a good idea to accept it anywhere in the middle */ + if (argc > argvoffset + 3) + if (!strcmp(argv[argc - 2], "-f")) + filename = argv[argc - 1]; m = tox_new_ex(ipv6enabled); @@ -593,11 +614,17 @@ int main(int argc, char *argv[]) new_lines(idstring); strcpy(line, ""); - uint16_t port = htons(atoi(argv[2])); - unsigned char *binary_string = hex_string_to_bin(argv[3]); - tox_bootstrap_ex(m, argv[1], ipv6enabled, port, binary_string); + uint16_t port = htons(atoi(argv[argvoffset + 2])); + unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); + int res = tox_bootstrap_ex(m, argv[argvoffset + 1], ipv6enabled, port, binary_string); free(binary_string); + if (!res) { + printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); + endwin(); + exit(1); + } + nodelay(stdscr, TRUE); while (1) { if (on == 0 && tox_isconnected(m)) { -- cgit v1.2.3