- #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"))
filename = argv[argc - 1];
m = tox_new_ex(ipv6enabled);
m = tox_new(ipv6enabled);
if ( !m ) {
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)
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);

View File

@ -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,

View File

@ -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))

View File

@ -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);

View File

@ -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. */