From 513e37815db8319dd015bf03b588380a4e3c61d3 Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Wed, 11 Sep 2013 20:50:15 +0200 Subject: [PATCH] tox.h, DHT.h: - tox_bootstrap_ex(), DHT_bootstrap_ex() renamed to tox_bootstrap_from_address(), DHT_bootstrap_from_address() - (handle_)sendnodes_ex() renamed to (handle_)sendnodes_ipv6() - only sending sendnodes_ipv6() if we're actually IPv6 enabled - changed comments to conform better nTox.c, Messenger_text.c, DHT_test.c, DHT_bootstrap.c: - fallout from *_ex() to *_from_address() DHT_bootstrap.c: - corrected a potentially wrong info message util.c: - fixed logfile name: now (funcptr) => now() (number) network.c: - addead comment about the necessity of bind() to succeed auto_test/messenger_test.c: - defaulting ipv6enabled to TOX_ENABLE_IPV6_DEFAULT LAN_discovery.c: - slight cleanup and comments for clarity --- auto_tests/messenger_test.c | 4 ++-- other/DHT_bootstrap.c | 5 +++-- testing/DHT_test.c | 2 +- testing/Messenger_test.c | 3 ++- testing/nTox.c | 2 +- toxcore/DHT.c | 13 ++++++++----- toxcore/DHT.h | 24 ++++++++++++++++++----- toxcore/LAN_discovery.c | 15 ++++++++------- toxcore/network.c | 14 +++++++++++++- toxcore/tox.c | 6 +++--- toxcore/tox.h | 38 ++++++++++++++++++++++++++++++++----- toxcore/util.c | 2 +- 12 files changed, 94 insertions(+), 34 deletions(-) diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index 6677c345..1b72a92f 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c @@ -278,8 +278,8 @@ int main(int argc, char *argv[]) good_id_b = hex_string_to_bin(good_id_b_str); bad_id = hex_string_to_bin(bad_id_str); - /* no IPv6 enabled yet */ - m = initMessenger(0); + /* IPv6 status from global define */ + m = initMessenger(TOX_ENABLE_IPV6_DEFAULT); /* setup a default friend and friendnum */ if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0) diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index f5fa9818..e6e8d59f 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -122,13 +122,14 @@ int main(int argc, char *argv[]) fclose(file); printf("\n"); - printf("Port: %u\n", PORT); + printf("Port: %u\n", ntohs(dht->c->lossless_udp->net->port)); if (argc > argvoffset + 3) { printf("Trying to bootstrap into the network...\n"); 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); + int res = DHT_bootstrap_from_address(dht, argv[argvoffset + 1], + ipv6enabled, port, bootstrap_key); free(bootstrap_key); if (!res) { diff --git a/testing/DHT_test.c b/testing/DHT_test.c index ba8c2f23..9eab60de 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -175,7 +175,7 @@ int main(int argc, char *argv[]) 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); + int res = DHT_bootstrap_from_address(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]); diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index fdcd3061..06f8bdbf 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -119,7 +119,8 @@ int main(int argc, char *argv[]) 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); + int res = DHT_bootstrap_from_address(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]); diff --git a/testing/nTox.c b/testing/nTox.c index 750970f9..9cfa3687 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -592,7 +592,7 @@ int main(int argc, char *argv[]) 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); + int res = tox_bootstrap_from_address(m, argv[argvoffset + 1], ipv6enabled, port, binary_string); free(binary_string); if (!res) { diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 44f7f101..96bfd663 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -617,7 +617,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl #ifdef TOX_ENABLE_IPV6 /* Send a send nodes response: message for IPv6 nodes */ -static int sendnodes_ex(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) +static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) { /* Check if packet is going to be sent to ourself. */ if (id_equal(public_key, dht->c->self_public_key)) @@ -691,7 +691,9 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 memcpy(&ping_id, plain, sizeof(ping_id)); sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); #ifdef TOX_ENABLE_IPV6 - sendnodes_ex(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); + /* only try to send IPv6 nodes if the ipv6enabled flag was given */ + if (dht->c->lossless_udp->net->family == AF_INET6) + sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); #endif //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ @@ -766,7 +768,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 } #ifdef TOX_ENABLE_IPV6 -static int handle_sendnodes_ex(void *object, IP_Port source, uint8_t *packet, uint32_t length) +static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, uint32_t length) { DHT *dht = object; uint64_t ping_id; @@ -979,7 +981,8 @@ 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); } -int DHT_bootstrap_ex(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) +int DHT_bootstrap_from_address(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); @@ -1400,7 +1403,7 @@ DHT *new_DHT(Net_Crypto *c) networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, temp); networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, temp); #ifdef TOX_ENABLE_IPV6 - networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_EX, &handle_sendnodes_ex, temp); + networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_EX, &handle_sendnodes_ipv6, temp); #endif init_cryptopackets(temp); cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, temp); diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 90b76a2f..255074b0 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -136,7 +136,7 @@ int DHT_delfriend(DHT *dht, uint8_t *client_id); * ip must be 4 bytes long. * port must be 2 bytes long. * - * !!! Signature changed!!! + * !!! Signature changed !!! * * OLD: IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id); * @@ -155,12 +155,26 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port); /* Run this function at least a couple times per second (It's the main loop). */ 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 +/* + * Use these two functions to bootstrap the client. + */ +/* Sends a "get nodes" request to the given node with ip, port and public_key + * to setup connections */ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); -int DHT_bootstrap_ex(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, uint8_t *public_key); +/* Resolves address into an IP address. If successful, sends a "get nodes" + * request to the given node with ip, port and public_key to setup connections + * + * address can be a hostname or an IP address (IPv4 or IPv6). + * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses + * if ipv6enabled is not 0 (zero), the resolving looks for IPv6 addresses first, + * then IPv4 addresses. + * + * returns 1 if the address could be converted into an IP address + * returns 0 otherwise + */ +int DHT_bootstrap_from_address(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/LAN_discovery.c b/toxcore/LAN_discovery.c index 933c2402..b429ea6e 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -112,7 +112,9 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) } } #else - ip.uint32 = INADDR_BROADCAST; + if (family_socket == AF_INET) + if (family_broadcast == AF_INET) + ip.uint32 = INADDR_BROADCAST; #endif return ip; @@ -193,23 +195,22 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c) ip_port.port = port; #ifdef TOX_ENABLE_IPV6 + /* IPv6 multicast */ if (c->lossless_udp->net->family == AF_INET6) { - ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET6); + ip_port.ip = broadcast_ip(AF_INET6, AF_INET6); if (ip_isset(&ip_port.ip)) if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0) res = 1; } + /* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is AF_INET6 */ ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET); - if (ip_isset(&ip_port.ip)) - if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) - res = 1; #else - ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET); + ip_port.ip = broadcast_ip(AF_INET, AF_INET); +#endif if (ip_isset(&ip_port.ip)) if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) res = 1; -#endif return res; } diff --git a/toxcore/network.c b/toxcore/network.c index de69bb3a..b40f7d56 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -312,7 +312,7 @@ Networking_Core *new_networking(IP ip, uint16_t port) return -1; */ - /* Enable broadcast on socket? */ + /* Enable broadcast on socket */ int broadcast = 1; setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); @@ -402,6 +402,18 @@ Networking_Core *new_networking(IP ip, uint16_t port) /* a hanging program or a different user might block the standard port; * as long as it isn't a parameter coming from the commandline, * try a few ports after it, to see if we can find a "free" one + * + * if we go on without binding, the first sendto() automatically binds to + * a free port chosen by the system (i.e. anything from 1024 to 65535) + * + * returning NULL after bind fails has both advantages and disadvantages: + * advantage: + * we can rely on getting the port in the range 33445..33450, which + * enables us to tell joe user to open their firewall to a small range + * + * disadvantage: + * some clients might not test return of tox_new(), blindly assuming that + * it worked ok (which it did previously without a successful bind) */ int tries, res; for(tries = 0; tries < 9; tries++) diff --git a/toxcore/tox.c b/toxcore/tox.c index 5e3893ec..31ae9c0f 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); } -int tox_bootstrap_ex(void *tox, const char *address, uint8_t ipv6enabled, - uint16_t port, uint8_t *public_key) +int tox_bootstrap_from_address(void *tox, const char *address, + uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) { Messenger *m = tox; - return DHT_bootstrap_ex(m->dht, address, ipv6enabled, port, public_key); + return DHT_bootstrap_from_address(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 cf5d6b2a..b331479e 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -338,12 +338,25 @@ void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_ */ void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata); -/* 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() returns 1 if the address could be converted, 0 otherwise +/* + * Use these two functions to bootstrap the client. + */ +/* Sends a "get nodes" request to the given node with ip, port and public_key + * to setup connections */ void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); -int tox_bootstrap_ex(Tox *tox, const char *address, uint8_t ipv6enabled, +/* Resolves address into an IP address. If successful, sends a "get nodes" + * request to the given node with ip, port and public_key to setup connections + * + * address can be a hostname or an IP address (IPv4 or IPv6). + * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses + * if ipv6enabled is not 0 (zero), the resolving looks for IPv6 addresses first, + * then IPv4 addresses. + * + * returns 1 if the address could be converted into an IP address + * returns 0 otherwise + */ +int tox_bootstrap_from_address(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. @@ -351,12 +364,27 @@ int tox_bootstrap_ex(Tox *tox, const char *address, uint8_t ipv6enabled, */ int tox_isconnected(Tox *tox); -/* Run this at startup. +/* + * Run one of the following two functions at startup. + */ +/* Initializes a tox structure + * Defaults to using ipv4 connections only. * * return allocated instance of tox on success. * return 0 if there are problems. */ Tox *tox_new(void); + +/* Initializes a tox structure + * The type of communication socket depends on ipv6enabled: + * If set to 0 (zero), creates an IPv4 socket which subsequently only allows + * IPv4 communication + * If set to anything else, creates an IPv6 socket which allows both IPv4 AND + * IPv6 communication + * + * return allocated instance of tox on success. + * return 0 if there are problems. + */ Tox *tox_new_ex(uint8_t ipv6enabled); /* Run this before closing shop. diff --git a/toxcore/util.c b/toxcore/util.c index 19d464d4..e751e9e4 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -51,7 +51,7 @@ void loginit(uint16_t port) if (logfile) fclose(logfile); - sprintf(logbuffer, "%u-%u.log", ntohs(port), now); + sprintf(logbuffer, "%u-%u.log", ntohs(port), now()); logfile = fopen(logbuffer, "w"); }; void loglog(char *text)