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
This commit is contained in:
Coren[m] 2013-09-11 20:50:15 +02:00
parent d0f5ad34ae
commit 513e37815d
12 changed files with 94 additions and 34 deletions

View File

@ -278,8 +278,8 @@ int main(int argc, char *argv[])
good_id_b = hex_string_to_bin(good_id_b_str); good_id_b = hex_string_to_bin(good_id_b_str);
bad_id = hex_string_to_bin(bad_id_str); bad_id = hex_string_to_bin(bad_id_str);
/* no IPv6 enabled yet */ /* IPv6 status from global define */
m = initMessenger(0); m = initMessenger(TOX_ENABLE_IPV6_DEFAULT);
/* setup a default friend and friendnum */ /* setup a default friend and friendnum */
if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0) if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0)

View File

@ -122,13 +122,14 @@ int main(int argc, char *argv[])
fclose(file); fclose(file);
printf("\n"); printf("\n");
printf("Port: %u\n", PORT); printf("Port: %u\n", ntohs(dht->c->lossless_udp->net->port));
if (argc > argvoffset + 3) { if (argc > argvoffset + 3) {
printf("Trying to bootstrap into the network...\n"); printf("Trying to bootstrap into the network...\n");
uint16_t port = htons(atoi(argv[argvoffset + 2])); uint16_t port = htons(atoi(argv[argvoffset + 2]));
uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); 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); free(bootstrap_key);
if (!res) { if (!res) {

View File

@ -175,7 +175,7 @@ int main(int argc, char *argv[])
uint16_t port = htons(atoi(argv[argvoffset + 2])); uint16_t port = htons(atoi(argv[argvoffset + 2]));
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); 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); free(binary_string);
if (!res) { if (!res) {
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);

View File

@ -119,7 +119,8 @@ int main(int argc, char *argv[])
if (argc == argvoffset + 4) { if (argc == argvoffset + 4) {
uint16_t port = htons(atoi(argv[argvoffset + 2])); uint16_t port = htons(atoi(argv[argvoffset + 2]));
uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); 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); free(bootstrap_key);
if (!res) { if (!res) {
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);

View File

@ -592,7 +592,7 @@ int main(int argc, char *argv[])
uint16_t port = htons(atoi(argv[argvoffset + 2])); uint16_t port = htons(atoi(argv[argvoffset + 2]));
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); 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); free(binary_string);
if (!res) { if (!res) {

View File

@ -617,7 +617,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
#ifdef TOX_ENABLE_IPV6 #ifdef TOX_ENABLE_IPV6
/* Send a send nodes response: message for IPv6 nodes */ /* 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. */ /* Check if packet is going to be sent to ourself. */
if (id_equal(public_key, dht->c->self_public_key)) 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)); 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
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 #endif
//send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ //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 #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; DHT *dht = object;
uint64_t ping_id; 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); getnodes(dht, ip_port, public_key, dht->c->self_public_key);
send_ping_request(dht->ping, dht->c, ip_port, 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_Port ip_port;
ip_init(&ip_port.ip, ipv6enabled); 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_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_ex, temp); networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_EX, &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

@ -136,7 +136,7 @@ int DHT_delfriend(DHT *dht, uint8_t *client_id);
* ip must be 4 bytes long. * ip must be 4 bytes long.
* port must be 2 bytes long. * port must be 2 bytes long.
* *
* !!! Signature changed!!! * !!! Signature changed !!!
* *
* OLD: IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id); * 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). */ /* Run this function at least a couple times per second (It's the main loop). */
void do_DHT(DHT *dht); 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. * Use these two functions to bootstrap the client.
* DHT_bootstrap_ex() returns 1 if the address could be converted, 0 otherwise */
/* 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); 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. /* Add nodes to the toping list.
* All nodes in this list are pinged every TIME_TOPING seconds * All nodes in this list are pinged every TIME_TOPING seconds

View File

@ -112,7 +112,9 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
} }
} }
#else #else
ip.uint32 = INADDR_BROADCAST; if (family_socket == AF_INET)
if (family_broadcast == AF_INET)
ip.uint32 = INADDR_BROADCAST;
#endif #endif
return ip; return ip;
@ -193,23 +195,22 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
ip_port.port = port; ip_port.port = port;
#ifdef TOX_ENABLE_IPV6 #ifdef TOX_ENABLE_IPV6
/* IPv6 multicast */
if (c->lossless_udp->net->family == AF_INET6) { 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 (ip_isset(&ip_port.ip))
if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0) if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0)
res = 1; 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); 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 #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 (ip_isset(&ip_port.ip))
if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES))
res = 1; res = 1;
#endif
return res; return res;
} }

View File

@ -312,7 +312,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
return -1; return -1;
*/ */
/* Enable broadcast on socket? */ /* Enable broadcast on socket */
int broadcast = 1; int broadcast = 1;
setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); 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; /* a hanging program or a different user might block the standard port;
* as long as it isn't a parameter coming from the commandline, * 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 * 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; int tries, res;
for(tries = 0; tries < 9; tries++) for(tries = 0; tries < 9; tries++)

View File

@ -374,11 +374,11 @@ void tox_bootstrap(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);
} }
int tox_bootstrap_ex(void *tox, const char *address, uint8_t ipv6enabled, int tox_bootstrap_from_address(void *tox, const char *address,
uint16_t port, uint8_t *public_key) uint8_t ipv6enabled, uint16_t port, uint8_t *public_key)
{ {
Messenger *m = tox; 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. /* return 0 if we are not connected to the DHT.

View File

@ -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); 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. * Use these two functions to bootstrap the client.
* tox_bootstrap_ex() returns 1 if the address could be converted, 0 otherwise */
/* 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(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); uint16_t port, uint8_t *public_key);
/* return 0 if we are not connected to the DHT. /* 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); 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 allocated instance of tox on success.
* return 0 if there are problems. * return 0 if there are problems.
*/ */
Tox *tox_new(void); 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); Tox *tox_new_ex(uint8_t ipv6enabled);
/* Run this before closing shop. /* Run this before closing shop.

View File

@ -51,7 +51,7 @@ void loginit(uint16_t port)
if (logfile) if (logfile)
fclose(logfile); fclose(logfile);
sprintf(logbuffer, "%u-%u.log", ntohs(port), now); sprintf(logbuffer, "%u-%u.log", ntohs(port), now());
logfile = fopen(logbuffer, "w"); logfile = fopen(logbuffer, "w");
}; };
void loglog(char *text) void loglog(char *text)