From a74cfaea81fecb11a6f56e069d59b8ea68dd98a8 Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Thu, 12 Sep 2013 15:42:03 +0200 Subject: [PATCH] tox.h: - #define'd tox's network port (as range) - finally killed tox_new_ex() in favor of changing tox_new()'s signature - renamed tox_bootstrap() to tox_bootstrap_from_ip() network.h: - #define'd tox's network port (as range) - renamed SEND_NODES_EX to SEND_NODES_IPV6 - bind() loop uses #define'd port range DHT.c: - renamed SEND_NODES_EX to SEND_NODES_IPV6 - sending ipv6 node addresses even if can't use them ourselves nTox.c: - adapted to changed tox_new() --- testing/nTox.c | 2 +- toxcore/DHT.c | 8 +++----- toxcore/network.c | 17 +++++++++++------ toxcore/network.h | 5 ++++- toxcore/tox.c | 4 ++-- toxcore/tox.h | 26 +++++++++++--------------- 6 files changed, 32 insertions(+), 30 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index 9cfa3687..1e83f507 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -566,7 +566,7 @@ int main(int argc, char *argv[]) if (!strcmp(argv[argc - 2], "-f")) filename = argv[argc - 1]; - m = tox_new_ex(ipv6enabled); + m = tox_new(ipv6enabled); if ( !m ) { fputs("Failed to allocate Messenger datastructure", stderr); diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 96bfd663..3de7b6ae 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -654,7 +654,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING) return -1; - data[0] = NET_PACKET_SEND_NODES_EX; + data[0] = NET_PACKET_SEND_NODES_IPV6; memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE); memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); @@ -691,9 +691,7 @@ 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 - /* 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); + sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); #endif //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ @@ -1403,7 +1401,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_ipv6, temp); + networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, temp); #endif init_cryptopackets(temp); cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, temp); diff --git a/toxcore/network.c b/toxcore/network.c index b00204a2..c0bd366d 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -213,6 +213,7 @@ void networking_poll(Networking_Core *net) } } + uint8_t at_startup_ran = 0; static int at_startup(void) { @@ -342,7 +343,7 @@ Networking_Core *new_networking(IP ip, uint16_t port) addrsize = sizeof(struct sockaddr_in); struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; addr4->sin_family = AF_INET; - addr4->sin_port = htons(port); + addr4->sin_port = 0; addr4->sin_addr = ip4.in_addr; portptr = &addr4->sin_port; @@ -353,7 +354,7 @@ Networking_Core *new_networking(IP ip, uint16_t port) addrsize = sizeof(struct sockaddr_in6); struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addr6->sin6_family = AF_INET6; - addr6->sin6_port = htons(port); + addr6->sin6_port = 0; addr6->sin6_addr = ip.ip6; addr6->sin6_flowinfo = 0; @@ -413,8 +414,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) * some clients might not test return of tox_new(), blindly assuming that * it worked ok (which it did previously without a successful bind) */ + uint16_t port_to_try = port; + *portptr = htons(port_to_try); int tries, res; - for(tries = 0; tries < 9; tries++) + for(tries = TOX_PORTRANGE_FROM; tries <= TOX_PORTRANGE_TO; tries++) { res = bind(temp->sock, (struct sockaddr *)&addr, addrsize); if (!res) @@ -433,9 +436,11 @@ Networking_Core *new_networking(IP ip, uint16_t port) return temp; } - uint16_t port = ntohs(*portptr); - port++; - *portptr = htons(port); + port_to_try++; + if (port_to_try > TOX_PORTRANGE_TO) + port_to_try = TOX_PORTRANGE_FROM; + + *portptr = htons(port_to_try); } fprintf(stderr, "Failed to bind socket: %u, %s (IP/Port: %s:%u\n", errno, diff --git a/toxcore/network.h b/toxcore/network.h index aa0c4661..5bc04632 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -72,13 +72,16 @@ typedef int sock_t; #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID for IPv4 addresses. */ -#define NET_PACKET_SEND_NODES_EX 4 /* Send nodes response packet ID for other addresses. */ +#define NET_PACKET_SEND_NODES_IPV6 4 /* Send nodes response packet ID for other addresses. */ #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ #define NET_PACKET_DATA 18 /* Data packet ID. */ #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ +#define TOX_PORTRANGE_FROM 33445 +#define TOX_PORTRANGE_TO 33455 +#define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM /* Current time, unix format */ #define unix_time() ((uint64_t)time(NULL)) diff --git a/toxcore/tox.c b/toxcore/tox.c index 31ae9c0f..f5c6c8ba 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -366,10 +366,10 @@ void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, i m_callback_connectionstatus(m, function, userdata); } -/* Use this function to bootstrap the client. +/* Use these functions to bootstrap the client. * Sends a get nodes request to the given node with ip port and public_key. */ -void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) +void tox_bootstrap_from_ip(void *tox, IP_Port ip_port, uint8_t *public_key) { Messenger *m = tox; DHT_bootstrap(m->dht, ip_port, public_key); diff --git a/toxcore/tox.h b/toxcore/tox.h index b331479e..2fddfab4 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -52,8 +52,12 @@ extern "C" { #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) +#define TOX_PORTRANGE_FROM 33445 +#define TOX_PORTRANGE_TO 33455 +#define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM + typedef union { - uint8_t c[4]; + uint8_t c[4]; uint16_t s[2]; uint32_t i; } tox_IP4; @@ -71,7 +75,7 @@ typedef struct { typedef union { struct { - tox_IP4 ip; + tox_IP4 ip; uint16_t port; /* Not used for anything right now. */ uint16_t padding; @@ -83,7 +87,7 @@ typedef union { * removed the unused union and padding also */ typedef struct { tox_IPAny ip; - uint16_t port; + uint16_t port; } tox_IPAny_Port; #undef TOX_ENABLE_IPV6 @@ -344,7 +348,7 @@ void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uin /* 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); +void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_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 * @@ -365,17 +369,9 @@ int tox_bootstrap_from_address(Tox *tox, const char *address, uint8_t ipv6enable int tox_isconnected(Tox *tox); /* - * Run one of the following two functions at startup. - */ -/* Initializes a tox structure - * Defaults to using ipv4 connections only. + * Run this function at startup. * - * return allocated instance of tox on success. - * return 0 if there are problems. - */ -Tox *tox_new(void); - -/* Initializes a tox structure + * 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 @@ -385,7 +381,7 @@ Tox *tox_new(void); * return allocated instance of tox on success. * return 0 if there are problems. */ -Tox *tox_new_ex(uint8_t ipv6enabled); +Tox *tox_new(uint8_t ipv6enabled); /* Run this before closing shop. * Free all datastructures. */