From bcb283cf4512f36b189ce2d49eb040c205cbc778 Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Tue, 10 Sep 2013 16:36:20 +0200 Subject: big push, putting all the infrastructure in place behind TOX_ENABLE_IPV6 --- toxcore/util.h | 1 - 1 file changed, 1 deletion(-) (limited to 'toxcore/util.h') diff --git a/toxcore/util.h b/toxcore/util.h index 90a3c8e4..7dea3eac 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -7,6 +7,5 @@ uint64_t now(); uint64_t random_64b(); -bool ipp_eq(IP_Port a, IP_Port b); bool id_eq(uint8_t *dest, uint8_t *src); void id_cpy(uint8_t *dest, uint8_t *src); -- cgit v1.2.3 From 3ae7460853b6777e18f213b316d43cef6c5a603e Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Tue, 10 Sep 2013 20:55:05 +0200 Subject: util.*: - added logging functions, default off tox.h: - added includes for sockaddr_in/6 network.c: - added logging functions, default off (#define in util.h) - IPv6: activating site-local all-nodes multicast address (i.e. IPv6 equivalent of broadcast) --- toxcore/Messenger.c | 65 +++++++++++++++++++++++++-------------------- toxcore/network.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++------ toxcore/network.h | 1 + toxcore/tox.h | 17 ++++++++++++ toxcore/util.c | 32 ++++++++++++++++++++-- toxcore/util.h | 17 ++++++++++++ 6 files changed, 170 insertions(+), 38 deletions(-) (limited to 'toxcore/util.h') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index cf744e23..b955ad5a 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -965,19 +965,23 @@ void Messenger_save(Messenger *m, uint8_t *data) { save_keys(m->net_crypto, data); data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; + uint32_t nospam = get_nospam(&(m->fr)); memcpy(data, &nospam, sizeof(nospam)); data += sizeof(nospam); + uint32_t size = DHT_size(m->dht); memcpy(data, &size, sizeof(size)); data += sizeof(size); DHT_save(m->dht, data); data += size; + size = sizeof(Friend) * m->numfriends; memcpy(data, &size, sizeof(size)); data += sizeof(size); memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); data += size; + uint16_t small_size = m->name_length; memcpy(data, &small_size, sizeof(small_size)); data += sizeof(small_size); @@ -990,59 +994,64 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) if (length == ~((uint32_t)0)) return -1; - if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) + /* BLOCK1: PUBKEY, SECKEY, NOSPAM, SIZE */ + if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) return -1; - length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3; load_keys(m->net_crypto, data); data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; + length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; + uint32_t nospam; memcpy(&nospam, data, sizeof(nospam)); set_nospam(&(m->fr), nospam); data += sizeof(nospam); + length -= sizeof(nospam); + uint32_t size; memcpy(&size, data, sizeof(size)); data += sizeof(size); + length -= sizeof(size); if (length < size) return -1; - length -= size; - if (DHT_load(m->dht, data, size) == -1) - return -1; + fprintf(stderr, "Data file: Something wicked happened to the stored connections.\n"); + + /* go on, friends still might be intact */ data += size; + length -= size; + memcpy(&size, data, sizeof(size)); data += sizeof(size); - - if (length < size || size % sizeof(Friend) != 0) + if (length < size) return -1; - Friend *temp = malloc(size); - memcpy(temp, data, size); - - uint16_t num = size / sizeof(Friend); - - uint32_t i; - - for (i = 0; i < num; ++i) { - if (temp[i].status >= 3) { - int fnum = m_addfriend_norequest(m, temp[i].client_id); - setfriendname(m, fnum, temp[i].name, temp[i].name_length); - /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ - } else if (temp[i].status != 0) { - /* TODO: This is not a good way to do this. */ - uint8_t address[FRIEND_ADDRESS_SIZE]; - memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); - memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); - uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); - memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); - m_addfriend(m, address, temp[i].info, temp[i].info_size); + if (!(size % sizeof(Friend))) { + uint16_t num = size / sizeof(Friend); + Friend temp[num]; + memcpy(temp, data, size); + + uint32_t i; + for (i = 0; i < num; ++i) { + if (temp[i].status >= 3) { + int fnum = m_addfriend_norequest(m, temp[i].client_id); + setfriendname(m, fnum, temp[i].name, temp[i].name_length); + /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ + } else if (temp[i].status != 0) { + /* TODO: This is not a good way to do this. */ + uint8_t address[FRIEND_ADDRESS_SIZE]; + memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); + memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); + uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); + memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); + m_addfriend(m, address, temp[i].info, temp[i].info_size); + } } } - free(temp); data += size; length -= size; diff --git a/toxcore/network.c b/toxcore/network.c index 2969f3ac..0b5eba61 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -26,6 +26,7 @@ #endif #include "network.h" +#include "util.h" /* return current UNIX time in microseconds (us). */ uint64_t current_time(void) @@ -61,6 +62,10 @@ uint32_t random_int(void) #endif } +#ifdef LOGGING +static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res); +#endif + /* Basic network functions: * Function to send packet(data) of length length to ip_port. */ @@ -122,7 +127,11 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le } #endif - return sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); + int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); +#ifdef LOGGING + loglogdata("O=>", data, length, &ip_port, res); +#endif + return res; } /* Function to receive data @@ -131,11 +140,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le * Packet length is put into length. * Dump all empty packets. */ -#ifdef WIN32 -static int receivepacket(unsigned int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) -#else -static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) -#endif +static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) { struct sockaddr_storage addr; #ifdef WIN32 @@ -145,8 +150,13 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le #endif (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); - if (*(int32_t *)length <= 0) + if (*(int32_t *)length <= 0) { +#ifdef LOGGING + if ((length < 0) && (errno != EWOULDBLOCK)) + sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno)); +#endif return -1; /* Nothing received or empty packet. */ + } #ifdef TOX_ENABLE_IPV6 if (addr.ss_family == AF_INET) { @@ -173,6 +183,10 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le return -1; #endif +#ifdef LOGGING + loglogdata("=>O", data, *length, ip_port, 0); +#endif + return 0; } @@ -307,6 +321,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); #endif +#ifdef LOGGING + loginit(ntohs(port)); +#endif + /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ uint16_t *portptr = NULL; struct sockaddr_storage addr; @@ -346,9 +364,33 @@ Networking_Core *new_networking(IP ip, uint16_t port) if (ip.family == AF_INET6) { char ipv6only = 0; int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only)); +#ifdef LOGGING + if (res < 0) { + sprintf(logbuffer, "Failed to enable dual-stack on IPv6 socket, won't be able to receive from/send to IPv4 addresses. (%u, %s)\n", + errno, strerror(errno)); + loglog(logbuffer); + } + else + loglog("Embedded IPv4 addresses enabled successfully.\n"); +#endif + + /* multicast local nodes */ + struct ipv6_mreq mreq; + memset(&mreq, 0, sizeof(mreq)); + mreq.ipv6mr_multiaddr.s6_addr[ 0] = 0xFF; + mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02; + mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; + mreq.ipv6mr_interface = 0; + res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); +#ifdef LOGGING if (res < 0) { - /* add log message*/ + sprintf(logbuffer, "Failed to activate local multicast membership. (%u, %s)\n", + errno, strerror(errno)); + loglog(logbuffer); } + else + loglog("Local multicast group FF02::1 joined successfully.\n"); +#endif } #endif @@ -363,6 +405,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) if (!res) { temp->port = *portptr; +#ifdef LOGGING + sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); + loglog(logbuffer); +#endif return temp; } @@ -733,3 +779,17 @@ int addr_resolve_or_parse_ip(const char *address, IP *to) return 1; }; + +#ifdef LOGGING +static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res) +{ + snprintf(logbuffer, sizeof(logbuffer), "[%2u] %3u%c %s %s:%u (%u: %s) | %04x%04x\n", + buffer[0], res < 0 ? (buflen & 0xFF) : res, + res < 0 ? '-' : (res == buflen ? '=' : '+'), + message, ip_ntoa(&ip_port->ip), ntohs(ip_port->port), res < 0 ? errno : 0, + res < 0 ? strerror(errno) : "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, + buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); + logbuffer[sizeof(logbuffer) - 1] = 0; + loglog(logbuffer); +} +#endif diff --git a/toxcore/network.h b/toxcore/network.h index 0d31ffac..e804379d 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -119,6 +119,7 @@ typedef struct { } IPAny_Port; #undef TOX_ENABLE_IPV6 +/* #define TOX_ENABLE_IPV6 */ #ifdef TOX_ENABLE_IPV6 #define TOX_ENABLE_IPV6_DEFAULT 1 typedef IPAny IP; diff --git a/toxcore/tox.h b/toxcore/tox.h index 44eba081..15bef94c 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -26,6 +26,22 @@ #include +#ifdef WIN32 +#ifndef WINVER +//Windows XP +#define WINVER 0x0501 +#endif + +#include +#include +#include + +#else + +#include + +#endif + #ifdef __cplusplus extern "C" { #endif @@ -71,6 +87,7 @@ typedef struct { } tox_IPAny_Port; #undef TOX_ENABLE_IPV6 +/* #define TOX_ENABLE_IPV6 */ #ifdef TOX_ENABLE_IPV6 #define TOX_ENABLE_IPV6_DEFAULT 1 typedef tox_IPAny tox_IP; diff --git a/toxcore/util.c b/toxcore/util.c index ff366a07..19d464d4 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -10,11 +10,12 @@ #endif #include -#include -#include +/* for CLIENT_ID_SIZE */ #include "DHT.h" +#include "util.h" + uint64_t now() { return time(NULL); @@ -41,3 +42,30 @@ void id_cpy(uint8_t *dest, uint8_t *src) { memcpy(dest, src, CLIENT_ID_SIZE); } + +#ifdef LOGGING +char logbuffer[512]; +static FILE *logfile = NULL; +void loginit(uint16_t port) +{ + if (logfile) + fclose(logfile); + + sprintf(logbuffer, "%u-%u.log", ntohs(port), now); + logfile = fopen(logbuffer, "w"); +}; +void loglog(char *text) +{ + if (logfile) { + fprintf(logfile, text); + fflush(logfile); + } +}; +void logexit() +{ + if (logfile) { + fclose(logfile); + logfile = NULL; + } +}; +#endif diff --git a/toxcore/util.h b/toxcore/util.h index 7dea3eac..20731a05 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -5,7 +5,24 @@ * Copyright 2013 plutooo */ +#ifndef __UTIL_H__ +#define __UTIL_H__ + +#include +#include + uint64_t now(); uint64_t random_64b(); bool id_eq(uint8_t *dest, uint8_t *src); void id_cpy(uint8_t *dest, uint8_t *src); + +#undef LOGGING +// #define LOGGING +#ifdef LOGGING +extern char logbuffer[512]; +void loginit(uint16_t port); +void loglog(char *text); +void logexit(); +#endif + +#endif /* __UTIL_H__ */ -- cgit v1.2.3 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 --- other/DHT_bootstrap.c | 51 +++++++++++++++++++++++---- 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 +++++++++++++++++++++++++++------------ toxcore/DHT.c | 5 ++- toxcore/DHT.h | 3 +- toxcore/tox.c | 4 +-- toxcore/tox.h | 4 +-- toxcore/util.h | 2 +- 11 files changed, 270 insertions(+), 68 deletions(-) (limited to 'toxcore/util.h') diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 7355ca10..795e24ac 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -81,11 +81,44 @@ void manage_keys(DHT *dht) int main(int argc, char *argv[]) { - /* let use decide by cmdline: TODO */ - uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; + if (argc == 2 && !strncasecmp(argv[1], "-h", 3)) { + printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]); + printf("Usage (unconnected): %s [--ipv4|--ipv6]\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++; + } /* Initialize networking - - Bind to ip 0.0.0.0:PORT */ + Bind to ip 0.0.0.0 / [::] : PORT */ IP ip; ip_init(&ip, ipv6enabled); @@ -112,11 +145,17 @@ int main(int argc, char *argv[]) perror("Initialization."); - if (argc > 3) { + if (argc > argvoffset + 3) { printf("Trying to bootstrap into the network...\n"); - uint8_t *bootstrap_key = hex_string_to_bin(argv[3]); - DHT_bootstrap_ex(dht, argv[1], ipv6enabled, htons(atoi(argv[2])), bootstrap_key); + uint16_t port = htons(atoi(argv[argvoffset + 2])); + uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); + int res = DHT_bootstrap_ex(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); + } } int is_waiting_for_dht_connection = 1; 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)) { diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 7663b1cc..1798ea1a 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -978,14 +978,17 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) getnodes(dht, ip_port, public_key, dht->c->self_public_key); send_ping_request(dht->ping, dht->c, ip_port, public_key); } -void DHT_bootstrap_ex(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) +int DHT_bootstrap_ex(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) { IP_Port ip_port; ip_init(&ip_port.ip, ipv6enabled); if (addr_resolve_or_parse_ip(address, &ip_port.ip)) { ip_port.port = port; DHT_bootstrap(dht, ip_port, public_key); + return 1; } + else + return 0; } /* Send the given packet to node with client_id diff --git a/toxcore/DHT.h b/toxcore/DHT.h index d980f269..90b76a2f 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -157,9 +157,10 @@ void do_DHT(DHT *dht); /* Use this function to bootstrap the client. * Sends a get nodes request to the given node with ip port and public_key. + * DHT_bootstrap_ex() returns 1 if the address could be converted, 0 otherwise */ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); -void DHT_bootstrap_ex(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key); +int DHT_bootstrap_ex(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key); /* Add nodes to the toping list. * All nodes in this list are pinged every TIME_TOPING seconds diff --git a/toxcore/tox.c b/toxcore/tox.c index 6417b16a..5e3893ec 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -374,11 +374,11 @@ void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) Messenger *m = tox; DHT_bootstrap(m->dht, ip_port, public_key); } -void tox_bootstrap_ex(void *tox, const char *address, uint8_t ipv6enabled, +int tox_bootstrap_ex(void *tox, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) { Messenger *m = tox; - DHT_bootstrap_ex(m->dht, address, ipv6enabled, port, public_key); + return DHT_bootstrap_ex(m->dht, address, ipv6enabled, port, public_key); }; /* return 0 if we are not connected to the DHT. diff --git a/toxcore/tox.h b/toxcore/tox.h index 15bef94c..cf5d6b2a 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -340,10 +340,10 @@ void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uin /* Use this function to bootstrap the client. * Sends a get nodes request to the given node with ip port and public_key. - * tox_bootstrap_ex converts the address into an IP_Port structure internally + * tox_bootstrap_ex() returns 1 if the address could be converted, 0 otherwise */ void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); -void tox_bootstrap_ex(Tox *tox, const char *address, uint8_t ipv6enabled, +int tox_bootstrap_ex(Tox *tox, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key); /* return 0 if we are not connected to the DHT. diff --git a/toxcore/util.h b/toxcore/util.h index 20731a05..71be84ca 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -17,7 +17,7 @@ bool id_eq(uint8_t *dest, uint8_t *src); void id_cpy(uint8_t *dest, uint8_t *src); #undef LOGGING -// #define LOGGING +/* #define LOGGING */ #ifdef LOGGING extern char logbuffer[512]; void loginit(uint16_t port); -- cgit v1.2.3 From 4cf0d857bcf87ba271178e7b1734958fb93b018a Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Wed, 11 Sep 2013 00:14:20 +0200 Subject: cmdline parsing of --ipv4/6 plucked into util --- other/DHT_bootstrap.c | 30 +++--------------------------- testing/DHT_test.c | 30 +++--------------------------- testing/Lossless_UDP_testclient.c | 30 +++--------------------------- testing/Lossless_UDP_testserver.c | 30 +++--------------------------- testing/Messenger_test.c | 30 +++--------------------------- testing/nTox.c | 30 +++--------------------------- toxcore/util.c | 32 ++++++++++++++++++++++++++++++++ toxcore/util.h | 2 ++ 8 files changed, 52 insertions(+), 162 deletions(-) (limited to 'toxcore/util.h') 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 -- cgit v1.2.3 From d35fee43ba7fe131e9c3dcda5167a46eacbf315a Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Wed, 11 Sep 2013 15:19:39 +0200 Subject: toxcore/util.h: - moved cmdline_parsefor_ipv46() to testing/misc_tools.c --- other/DHT_bootstrap.c | 1 - testing/DHT_test.c | 1 - testing/Lossless_UDP_testclient.c | 2 +- testing/Lossless_UDP_testserver.c | 2 +- testing/Messenger_test.c | 1 - testing/misc_tools.c | 33 +++++++++++++++++++++++++++++++++ toxcore/util.c | 32 -------------------------------- toxcore/util.h | 2 -- 8 files changed, 35 insertions(+), 39 deletions(-) (limited to 'toxcore/util.h') diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index f2e792ac..f5fa9818 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -33,7 +33,6 @@ #include "../toxcore/DHT.h" #include "../toxcore/friend_requests.h" #include "../testing/misc_tools.c" -#include "../toxcore/util.h" /* Sleep function (x = milliseconds) */ #ifdef WIN32 diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 5451c467..ba8c2f23 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -34,7 +34,6 @@ //#include "../core/network.h" #include "../toxcore/DHT.h" #include "../toxcore/friend_requests.h" -#include "../toxcore/util.h" #include "misc_tools.c" #include diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c index e36ac6eb..7891ce64 100644 --- a/testing/Lossless_UDP_testclient.c +++ b/testing/Lossless_UDP_testclient.c @@ -34,7 +34,7 @@ #include "../toxcore/network.h" #include "../toxcore/Lossless_UDP.h" -#include "../toxcore/util.h" +#include "misc_tools.c" #ifdef WIN32 diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index 97c3eb46..0fd4edd9 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c @@ -34,7 +34,7 @@ #include "../toxcore/network.h" #include "../toxcore/Lossless_UDP.h" -#include "../toxcore/util.h" +#include "misc_tools.c" //Sleep function (x = milliseconds) #ifdef WIN32 diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 93069226..14d9ca20 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -43,7 +43,6 @@ #include "../toxcore/Messenger.h" #include "misc_tools.c" -#include "../toxcore/util.h" #ifdef WIN32 diff --git a/testing/misc_tools.c b/testing/misc_tools.c index c4dce1bb..824200d8 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c @@ -46,3 +46,36 @@ unsigned char *hex_string_to_bin(char hex_string[]) return val; } + + +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; +}; diff --git a/toxcore/util.c b/toxcore/util.c index 7b5ddc49..19d464d4 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -43,38 +43,6 @@ 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 e7be2d51..71be84ca 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -16,8 +16,6 @@ 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 -- cgit v1.2.3