diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 795e24ac..099aad80 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -89,33 +89,9 @@ int main(int argc, char *argv[]) /* 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 argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) + exit(1); /* Initialize networking - Bind to ip 0.0.0.0 / [::] : PORT */ diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 61762162..ba8c2f23 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -140,33 +140,9 @@ int main(int argc, char *argv[]) /* 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 argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) + exit(1); //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); /* initialize networking */ diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c index 5d4c8547..70349204 100644 --- a/testing/Lossless_UDP_testclient.c +++ b/testing/Lossless_UDP_testclient.c @@ -153,33 +153,9 @@ int main(int argc, char *argv[]) { /* 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 argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) + exit(1); if (argc < argvoffset + 4) { printf("Usage: %s [--ipv4|--ipv6] ip port filename\n", argv[0]); diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index 52dbcc80..343a662a 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c @@ -149,33 +149,9 @@ int main(int argc, char *argv[]) { /* 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 argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) + exit(1); if (argc < argvoffset + 2) { printf("Usage: %s [--ipv4|--ipv6] filename\n", argv[0]); diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index e7a75c8c..14d9ca20 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -97,33 +97,9 @@ int main(int argc, char *argv[]) { /* 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 argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) + exit(1); /* with optional --ipvx, now it can be 1-4 arguments... */ if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) { diff --git a/testing/nTox.c b/testing/nTox.c index 6eeec5d3..750970f9 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -544,33 +544,9 @@ int main(int argc, char *argv[]) /* 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 argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) + exit(1); int on = 0; int c = 0; diff --git a/toxcore/util.c b/toxcore/util.c index 19d464d4..7b5ddc49 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -43,6 +43,38 @@ void id_cpy(uint8_t *dest, uint8_t *src) memcpy(dest, src, CLIENT_ID_SIZE); } +int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) +{ + 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]); + return -1; + } + } + else { + printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); + return -1; + } + + if (argvoffset != argi - 1) { + printf("Argument must come first: %s.\n", argv[argi]); + return -1; + } + + argvoffset++; + } + + return argvoffset; +}; + #ifdef LOGGING char logbuffer[512]; static FILE *logfile = NULL; diff --git a/toxcore/util.h b/toxcore/util.h index 71be84ca..e7be2d51 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -16,6 +16,8 @@ uint64_t random_64b(); bool id_eq(uint8_t *dest, uint8_t *src); void id_cpy(uint8_t *dest, uint8_t *src); +int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled); + #undef LOGGING /* #define LOGGING */ #ifdef LOGGING