- #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()
This commit is contained in:
Coren[m] 2013-09-12 15:42:03 +02:00
parent 5e1523e61d
commit a74cfaea81
6 changed files with 32 additions and 30 deletions

View File

@ -566,7 +566,7 @@ int main(int argc, char *argv[])
if (!strcmp(argv[argc - 2], "-f")) if (!strcmp(argv[argc - 2], "-f"))
filename = argv[argc - 1]; filename = argv[argc - 1];
m = tox_new_ex(ipv6enabled); m = tox_new(ipv6enabled);
if ( !m ) { if ( !m ) {
fputs("Failed to allocate Messenger datastructure", stderr); fputs("Failed to allocate Messenger datastructure", stderr);

View File

@ -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) if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING)
return -1; 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, dht->c->self_public_key, CLIENT_ID_SIZE);
memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len);
@ -691,8 +691,6 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32
memcpy(&ping_id, plain, sizeof(ping_id)); memcpy(&ping_id, plain, sizeof(ping_id));
sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id);
#ifdef TOX_ENABLE_IPV6 #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 #endif
@ -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_GET_NODES, &handle_getnodes, temp);
networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, temp); networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, temp);
#ifdef TOX_ENABLE_IPV6 #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 #endif
init_cryptopackets(temp); init_cryptopackets(temp);
cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, temp); cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, temp);

View File

@ -213,6 +213,7 @@ void networking_poll(Networking_Core *net)
} }
} }
uint8_t at_startup_ran = 0; uint8_t at_startup_ran = 0;
static int at_startup(void) static int at_startup(void)
{ {
@ -342,7 +343,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
addrsize = sizeof(struct sockaddr_in); addrsize = sizeof(struct sockaddr_in);
struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
addr4->sin_family = AF_INET; addr4->sin_family = AF_INET;
addr4->sin_port = htons(port); addr4->sin_port = 0;
addr4->sin_addr = ip4.in_addr; addr4->sin_addr = ip4.in_addr;
portptr = &addr4->sin_port; portptr = &addr4->sin_port;
@ -353,7 +354,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
addrsize = sizeof(struct sockaddr_in6); addrsize = sizeof(struct sockaddr_in6);
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
addr6->sin6_family = AF_INET6; addr6->sin6_family = AF_INET6;
addr6->sin6_port = htons(port); addr6->sin6_port = 0;
addr6->sin6_addr = ip.ip6; addr6->sin6_addr = ip.ip6;
addr6->sin6_flowinfo = 0; 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 * some clients might not test return of tox_new(), blindly assuming that
* it worked ok (which it did previously without a successful bind) * 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; 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); res = bind(temp->sock, (struct sockaddr *)&addr, addrsize);
if (!res) if (!res)
@ -433,9 +436,11 @@ Networking_Core *new_networking(IP ip, uint16_t port)
return temp; return temp;
} }
uint16_t port = ntohs(*portptr); port_to_try++;
port++; if (port_to_try > TOX_PORTRANGE_TO)
*portptr = htons(port); 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, fprintf(stderr, "Failed to bind socket: %u, %s (IP/Port: %s:%u\n", errno,

View File

@ -72,13 +72,16 @@ typedef int sock_t;
#define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */
#define NET_PACKET_GET_NODES 2 /* Get nodes request 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 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_HANDSHAKE 16 /* Handshake packet ID. */
#define NET_PACKET_SYNC 17 /* SYNC packet ID. */ #define NET_PACKET_SYNC 17 /* SYNC packet ID. */
#define NET_PACKET_DATA 18 /* Data packet ID. */ #define NET_PACKET_DATA 18 /* Data packet ID. */
#define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */
#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery 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 */ /* Current time, unix format */
#define unix_time() ((uint64_t)time(NULL)) #define unix_time() ((uint64_t)time(NULL))

View File

@ -366,10 +366,10 @@ void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, i
m_callback_connectionstatus(m, function, userdata); 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. * 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; Messenger *m = tox;
DHT_bootstrap(m->dht, ip_port, public_key); DHT_bootstrap(m->dht, ip_port, public_key);

View File

@ -52,6 +52,10 @@ extern "C" {
#define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) #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 { typedef union {
uint8_t c[4]; uint8_t c[4];
uint16_t s[2]; uint16_t s[2];
@ -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 /* Sends a "get nodes" request to the given node with ip, port and public_key
* to setup connections * 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" /* 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 * 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); int tox_isconnected(Tox *tox);
/* /*
* Run one of the following two functions at startup. * Run this function at startup.
*/
/* Initializes a tox structure
* Defaults to using ipv4 connections only.
* *
* return allocated instance of tox on success. * Initializes a tox structure
* return 0 if there are problems.
*/
Tox *tox_new(void);
/* Initializes a tox structure
* The type of communication socket depends on ipv6enabled: * The type of communication socket depends on ipv6enabled:
* If set to 0 (zero), creates an IPv4 socket which subsequently only allows * If set to 0 (zero), creates an IPv4 socket which subsequently only allows
* IPv4 communication * IPv4 communication
@ -385,7 +381,7 @@ Tox *tox_new(void);
* return allocated instance of tox on success. * return allocated instance of tox on success.
* return 0 if there are problems. * return 0 if there are problems.
*/ */
Tox *tox_new_ex(uint8_t ipv6enabled); Tox *tox_new(uint8_t ipv6enabled);
/* Run this before closing shop. /* Run this before closing shop.
* Free all datastructures. */ * Free all datastructures. */