diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 8 | ||||
-rw-r--r-- | toxcore/network.c | 17 | ||||
-rw-r--r-- | toxcore/network.h | 5 | ||||
-rw-r--r-- | toxcore/tox.c | 4 | ||||
-rw-r--r-- | toxcore/tox.h | 26 |
5 files changed, 31 insertions, 29 deletions
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_ | |||
654 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING) | 654 | if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING) |
655 | return -1; | 655 | return -1; |
656 | 656 | ||
657 | data[0] = NET_PACKET_SEND_NODES_EX; | 657 | data[0] = NET_PACKET_SEND_NODES_IPV6; |
658 | memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE); | 658 | memcpy(data + 1, dht->c->self_public_key, CLIENT_ID_SIZE); |
659 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 659 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
660 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | 660 | 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 | |||
691 | memcpy(&ping_id, plain, sizeof(ping_id)); | 691 | memcpy(&ping_id, plain, sizeof(ping_id)); |
692 | sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); | 692 | sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); |
693 | #ifdef TOX_ENABLE_IPV6 | 693 | #ifdef TOX_ENABLE_IPV6 |
694 | /* only try to send IPv6 nodes if the ipv6enabled flag was given */ | 694 | sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); |
695 | if (dht->c->lossless_udp->net->family == AF_INET6) | ||
696 | sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); | ||
697 | #endif | 695 | #endif |
698 | 696 | ||
699 | //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ | 697 | //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ |
@@ -1403,7 +1401,7 @@ DHT *new_DHT(Net_Crypto *c) | |||
1403 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, temp); | 1401 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, temp); |
1404 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, temp); | 1402 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, temp); |
1405 | #ifdef TOX_ENABLE_IPV6 | 1403 | #ifdef TOX_ENABLE_IPV6 |
1406 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_EX, &handle_sendnodes_ipv6, temp); | 1404 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, temp); |
1407 | #endif | 1405 | #endif |
1408 | init_cryptopackets(temp); | 1406 | init_cryptopackets(temp); |
1409 | cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, temp); | 1407 | 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) | |||
213 | } | 213 | } |
214 | } | 214 | } |
215 | 215 | ||
216 | |||
216 | uint8_t at_startup_ran = 0; | 217 | uint8_t at_startup_ran = 0; |
217 | static int at_startup(void) | 218 | static int at_startup(void) |
218 | { | 219 | { |
@@ -342,7 +343,7 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
342 | addrsize = sizeof(struct sockaddr_in); | 343 | addrsize = sizeof(struct sockaddr_in); |
343 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; | 344 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; |
344 | addr4->sin_family = AF_INET; | 345 | addr4->sin_family = AF_INET; |
345 | addr4->sin_port = htons(port); | 346 | addr4->sin_port = 0; |
346 | addr4->sin_addr = ip4.in_addr; | 347 | addr4->sin_addr = ip4.in_addr; |
347 | 348 | ||
348 | portptr = &addr4->sin_port; | 349 | portptr = &addr4->sin_port; |
@@ -353,7 +354,7 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
353 | addrsize = sizeof(struct sockaddr_in6); | 354 | addrsize = sizeof(struct sockaddr_in6); |
354 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 355 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; |
355 | addr6->sin6_family = AF_INET6; | 356 | addr6->sin6_family = AF_INET6; |
356 | addr6->sin6_port = htons(port); | 357 | addr6->sin6_port = 0; |
357 | addr6->sin6_addr = ip.ip6; | 358 | addr6->sin6_addr = ip.ip6; |
358 | 359 | ||
359 | addr6->sin6_flowinfo = 0; | 360 | addr6->sin6_flowinfo = 0; |
@@ -413,8 +414,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
413 | * some clients might not test return of tox_new(), blindly assuming that | 414 | * some clients might not test return of tox_new(), blindly assuming that |
414 | * it worked ok (which it did previously without a successful bind) | 415 | * it worked ok (which it did previously without a successful bind) |
415 | */ | 416 | */ |
417 | uint16_t port_to_try = port; | ||
418 | *portptr = htons(port_to_try); | ||
416 | int tries, res; | 419 | int tries, res; |
417 | for(tries = 0; tries < 9; tries++) | 420 | for(tries = TOX_PORTRANGE_FROM; tries <= TOX_PORTRANGE_TO; tries++) |
418 | { | 421 | { |
419 | res = bind(temp->sock, (struct sockaddr *)&addr, addrsize); | 422 | res = bind(temp->sock, (struct sockaddr *)&addr, addrsize); |
420 | if (!res) | 423 | if (!res) |
@@ -433,9 +436,11 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
433 | return temp; | 436 | return temp; |
434 | } | 437 | } |
435 | 438 | ||
436 | uint16_t port = ntohs(*portptr); | 439 | port_to_try++; |
437 | port++; | 440 | if (port_to_try > TOX_PORTRANGE_TO) |
438 | *portptr = htons(port); | 441 | port_to_try = TOX_PORTRANGE_FROM; |
442 | |||
443 | *portptr = htons(port_to_try); | ||
439 | } | 444 | } |
440 | 445 | ||
441 | fprintf(stderr, "Failed to bind socket: %u, %s (IP/Port: %s:%u\n", errno, | 446 | 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; | |||
72 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ | 72 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ |
73 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ | 73 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ |
74 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID for IPv4 addresses. */ | 74 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID for IPv4 addresses. */ |
75 | #define NET_PACKET_SEND_NODES_EX 4 /* Send nodes response packet ID for other addresses. */ | 75 | #define NET_PACKET_SEND_NODES_IPV6 4 /* Send nodes response packet ID for other addresses. */ |
76 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ | 76 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ |
77 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ | 77 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ |
78 | #define NET_PACKET_DATA 18 /* Data packet ID. */ | 78 | #define NET_PACKET_DATA 18 /* Data packet ID. */ |
79 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ | 79 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ |
80 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ | 80 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ |
81 | 81 | ||
82 | #define TOX_PORTRANGE_FROM 33445 | ||
83 | #define TOX_PORTRANGE_TO 33455 | ||
84 | #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM | ||
82 | 85 | ||
83 | /* Current time, unix format */ | 86 | /* Current time, unix format */ |
84 | #define unix_time() ((uint64_t)time(NULL)) | 87 | #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 | |||
366 | m_callback_connectionstatus(m, function, userdata); | 366 | m_callback_connectionstatus(m, function, userdata); |
367 | } | 367 | } |
368 | 368 | ||
369 | /* Use this function to bootstrap the client. | 369 | /* Use these functions to bootstrap the client. |
370 | * Sends a get nodes request to the given node with ip port and public_key. | 370 | * Sends a get nodes request to the given node with ip port and public_key. |
371 | */ | 371 | */ |
372 | void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) | 372 | void tox_bootstrap_from_ip(void *tox, IP_Port ip_port, uint8_t *public_key) |
373 | { | 373 | { |
374 | Messenger *m = tox; | 374 | Messenger *m = tox; |
375 | DHT_bootstrap(m->dht, ip_port, public_key); | 375 | 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" { | |||
52 | 52 | ||
53 | #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) | 53 | #define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) |
54 | 54 | ||
55 | #define TOX_PORTRANGE_FROM 33445 | ||
56 | #define TOX_PORTRANGE_TO 33455 | ||
57 | #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM | ||
58 | |||
55 | typedef union { | 59 | typedef union { |
56 | uint8_t c[4]; | 60 | uint8_t c[4]; |
57 | uint16_t s[2]; | 61 | uint16_t s[2]; |
58 | uint32_t i; | 62 | uint32_t i; |
59 | } tox_IP4; | 63 | } tox_IP4; |
@@ -71,7 +75,7 @@ typedef struct { | |||
71 | 75 | ||
72 | typedef union { | 76 | typedef union { |
73 | struct { | 77 | struct { |
74 | tox_IP4 ip; | 78 | tox_IP4 ip; |
75 | uint16_t port; | 79 | uint16_t port; |
76 | /* Not used for anything right now. */ | 80 | /* Not used for anything right now. */ |
77 | uint16_t padding; | 81 | uint16_t padding; |
@@ -83,7 +87,7 @@ typedef union { | |||
83 | * removed the unused union and padding also */ | 87 | * removed the unused union and padding also */ |
84 | typedef struct { | 88 | typedef struct { |
85 | tox_IPAny ip; | 89 | tox_IPAny ip; |
86 | uint16_t port; | 90 | uint16_t port; |
87 | } tox_IPAny_Port; | 91 | } tox_IPAny_Port; |
88 | 92 | ||
89 | #undef TOX_ENABLE_IPV6 | 93 | #undef TOX_ENABLE_IPV6 |
@@ -344,7 +348,7 @@ void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uin | |||
344 | /* Sends a "get nodes" request to the given node with ip, port and public_key | 348 | /* Sends a "get nodes" request to the given node with ip, port and public_key |
345 | * to setup connections | 349 | * to setup connections |
346 | */ | 350 | */ |
347 | void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); | 351 | void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); |
348 | /* Resolves address into an IP address. If successful, sends a "get nodes" | 352 | /* Resolves address into an IP address. If successful, sends a "get nodes" |
349 | * request to the given node with ip, port and public_key to setup connections | 353 | * request to the given node with ip, port and public_key to setup connections |
350 | * | 354 | * |
@@ -365,17 +369,9 @@ int tox_bootstrap_from_address(Tox *tox, const char *address, uint8_t ipv6enable | |||
365 | int tox_isconnected(Tox *tox); | 369 | int tox_isconnected(Tox *tox); |
366 | 370 | ||
367 | /* | 371 | /* |
368 | * Run one of the following two functions at startup. | 372 | * Run this function at startup. |
369 | */ | ||
370 | /* Initializes a tox structure | ||
371 | * Defaults to using ipv4 connections only. | ||
372 | * | 373 | * |
373 | * return allocated instance of tox on success. | 374 | * Initializes a tox structure |
374 | * return 0 if there are problems. | ||
375 | */ | ||
376 | Tox *tox_new(void); | ||
377 | |||
378 | /* Initializes a tox structure | ||
379 | * The type of communication socket depends on ipv6enabled: | 375 | * The type of communication socket depends on ipv6enabled: |
380 | * If set to 0 (zero), creates an IPv4 socket which subsequently only allows | 376 | * If set to 0 (zero), creates an IPv4 socket which subsequently only allows |
381 | * IPv4 communication | 377 | * IPv4 communication |
@@ -385,7 +381,7 @@ Tox *tox_new(void); | |||
385 | * return allocated instance of tox on success. | 381 | * return allocated instance of tox on success. |
386 | * return 0 if there are problems. | 382 | * return 0 if there are problems. |
387 | */ | 383 | */ |
388 | Tox *tox_new_ex(uint8_t ipv6enabled); | 384 | Tox *tox_new(uint8_t ipv6enabled); |
389 | 385 | ||
390 | /* Run this before closing shop. | 386 | /* Run this before closing shop. |
391 | * Free all datastructures. */ | 387 | * Free all datastructures. */ |