add NAT hole punching level to Tox API

This commit is contained in:
Gregory Mullen (grayhatter) 2016-11-11 03:10:24 -08:00
parent 19711d0fd0
commit ad517eb1df
No known key found for this signature in database
GPG Key ID: AEA10D4EA0530876
12 changed files with 45 additions and 14 deletions

View File

@ -303,7 +303,7 @@ static void test_addto_lists(IP ip)
Networking_Core *net = new_networking(NULL, ip, TOX_PORT_DEFAULT); Networking_Core *net = new_networking(NULL, ip, TOX_PORT_DEFAULT);
ck_assert_msg(net != 0, "Failed to create Networking_Core"); ck_assert_msg(net != 0, "Failed to create Networking_Core");
DHT *dht = new_DHT(NULL, net); DHT *dht = new_DHT(NULL, net, true);
ck_assert_msg(dht != 0, "Failed to create DHT"); ck_assert_msg(dht != 0, "Failed to create DHT");
IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT }; IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT };
@ -440,7 +440,7 @@ static void test_list_main(void)
IP ip; IP ip;
ip_init(&ip, 1); ip_init(&ip, 1);
dhts[i] = new_DHT(NULL, new_networking(NULL, ip, DHT_DEFAULT_PORT + i)); dhts[i] = new_DHT(NULL, new_networking(NULL, ip, DHT_DEFAULT_PORT + i), true);
ck_assert_msg(dhts[i] != 0, "Failed to create dht instances %u", i); ck_assert_msg(dhts[i] != 0, "Failed to create dht instances %u", i);
ck_assert_msg(dhts[i]->net->port != DHT_DEFAULT_PORT + i, "Bound to wrong port"); ck_assert_msg(dhts[i]->net->port != DHT_DEFAULT_PORT + i, "Bound to wrong port");
} }
@ -584,7 +584,7 @@ START_TEST(test_DHT_test)
IP ip; IP ip;
ip_init(&ip, 1); ip_init(&ip, 1);
dhts[i] = new_DHT(NULL, new_networking(NULL, ip, DHT_DEFAULT_PORT + i)); dhts[i] = new_DHT(NULL, new_networking(NULL, ip, DHT_DEFAULT_PORT + i), true);
ck_assert_msg(dhts[i] != 0, "Failed to create dht instances %u", i); ck_assert_msg(dhts[i] != 0, "Failed to create dht instances %u", i);
ck_assert_msg(dhts[i]->net->port != DHT_DEFAULT_PORT + i, "Bound to wrong port"); ck_assert_msg(dhts[i]->net->port != DHT_DEFAULT_PORT + i, "Bound to wrong port");
} }

View File

@ -144,8 +144,8 @@ START_TEST(test_basic)
IP ip; IP ip;
ip_init(&ip, 1); ip_init(&ip, 1);
ip.ip6.uint8[15] = 1; ip.ip6.uint8[15] = 1;
Onion *onion1 = new_onion(new_DHT(NULL, new_networking(NULL, ip, 34567))); Onion *onion1 = new_onion(new_DHT(NULL, new_networking(NULL, ip, 34567), true));
Onion *onion2 = new_onion(new_DHT(NULL, new_networking(NULL, ip, 34568))); Onion *onion2 = new_onion(new_DHT(NULL, new_networking(NULL, ip, 34568), true));
ck_assert_msg((onion1 != NULL) && (onion2 != NULL), "Onion failed initializing."); ck_assert_msg((onion1 != NULL) && (onion2 != NULL), "Onion failed initializing.");
networking_registerhandler(onion2->net, 'I', &handle_test_1, onion2); networking_registerhandler(onion2->net, 'I', &handle_test_1, onion2);
@ -222,7 +222,7 @@ START_TEST(test_basic)
} }
c_sleep(1000); c_sleep(1000);
Onion *onion3 = new_onion(new_DHT(NULL, new_networking(NULL, ip, 34569))); Onion *onion3 = new_onion(new_DHT(NULL, new_networking(NULL, ip, 34569), true));
ck_assert_msg((onion3 != NULL), "Onion failed initializing."); ck_assert_msg((onion3 != NULL), "Onion failed initializing.");
random_nonce(nonce); random_nonce(nonce);
@ -285,7 +285,7 @@ static Onions *new_onions(uint16_t port)
ip_init(&ip, 1); ip_init(&ip, 1);
ip.ip6.uint8[15] = 1; ip.ip6.uint8[15] = 1;
Onions *on = (Onions *)malloc(sizeof(Onions)); Onions *on = (Onions *)malloc(sizeof(Onions));
DHT *dht = new_DHT(NULL, new_networking(NULL, ip, port)); DHT *dht = new_DHT(NULL, new_networking(NULL, ip, port), true);
on->onion = new_onion(dht); on->onion = new_onion(dht);
on->onion_a = new_onion_announce(dht); on->onion_a = new_onion_announce(dht);
TCP_Proxy_Info inf = {{{0}}}; TCP_Proxy_Info inf = {{{0}}};

View File

@ -27,8 +27,9 @@
#endif #endif
#include "../toxcore/DHT.h" #include "../toxcore/DHT.h"
#include "../toxcore/LAN_discovery.h"
#include "../toxcore/friend_requests.h" #include "../toxcore/friend_requests.h"
#include "../toxcore/LAN_discovery.h"
#include "../toxcore/tox.h"
#include "../toxcore/util.h" #include "../toxcore/util.h"
#define TCP_RELAY_ENABLED #define TCP_RELAY_ENABLED
@ -120,7 +121,7 @@ int main(int argc, char *argv[])
IP ip; IP ip;
ip_init(&ip, ipv6enabled); ip_init(&ip, ipv6enabled);
DHT *dht = new_DHT(NULL, new_networking(NULL, ip, PORT)); DHT *dht = new_DHT(NULL, new_networking(NULL, ip, PORT), true);
Onion *onion = new_onion(dht); Onion *onion = new_onion(dht);
Onion_Announce *onion_a = new_onion_announce(dht); Onion_Announce *onion_a = new_onion_announce(dht);

View File

@ -33,6 +33,7 @@
#include <sys/stat.h> #include <sys/stat.h>
// toxcore // toxcore
#include "../../../toxcore/tox.h"
#include "../../../toxcore/LAN_discovery.h" #include "../../../toxcore/LAN_discovery.h"
#include "../../../toxcore/TCP_server.h" #include "../../../toxcore/TCP_server.h"
#include "../../../toxcore/onion_announce.h" #include "../../../toxcore/onion_announce.h"
@ -243,7 +244,7 @@ int main(int argc, char *argv[])
} }
} }
DHT *dht = new_DHT(NULL, net); DHT *dht = new_DHT(NULL, net, true);
if (dht == NULL) { if (dht == NULL) {
write_log(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n"); write_log(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n");

View File

@ -193,7 +193,7 @@ int main(int argc, char *argv[])
IP ip; IP ip;
ip_init(&ip, ipv6enabled); ip_init(&ip, ipv6enabled);
DHT *dht = new_DHT(NULL, new_networking(NULL, ip, PORT)); DHT *dht = new_DHT(NULL, new_networking(NULL, ip, PORT), true);
printf("OUR ID: "); printf("OUR ID: ");
uint32_t i; uint32_t i;

View File

@ -2088,6 +2088,10 @@ static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t
static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num) static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num)
{ {
if (!dht->hole_punching_enabled) {
return;
}
if (numports > MAX_FRIEND_CLIENTS || numports == 0) { if (numports > MAX_FRIEND_CLIENTS || numports == 0) {
return; return;
} }
@ -2577,7 +2581,7 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack
/*----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------*/
DHT *new_DHT(Logger *log, Networking_Core *net) DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled)
{ {
/* init time */ /* init time */
unix_time_update(); unix_time_update();
@ -2594,6 +2598,9 @@ DHT *new_DHT(Logger *log, Networking_Core *net)
dht->log = log; dht->log = log;
dht->net = net; dht->net = net;
dht->hole_punching_enabled = holepunching_enabled;
dht->ping = new_ping(dht); dht->ping = new_ping(dht);
if (dht->ping == NULL) { if (dht->ping == NULL) {

View File

@ -238,6 +238,8 @@ typedef struct {
Logger *log; Logger *log;
Networking_Core *net; Networking_Core *net;
bool hole_punching_enabled;
Client_data close_clientlist[LCLIENT_LIST]; Client_data close_clientlist[LCLIENT_LIST];
uint64_t close_lastgetnodes; uint64_t close_lastgetnodes;
uint32_t close_bootstrap_times; uint32_t close_bootstrap_times;
@ -435,7 +437,7 @@ void DHT_save(DHT *dht, uint8_t *data);
int DHT_load(DHT *dht, const uint8_t *data, uint32_t length); int DHT_load(DHT *dht, const uint8_t *data, uint32_t length);
/* Initialize DHT. */ /* Initialize DHT. */
DHT *new_DHT(Logger *log, Networking_Core *net); DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled);
void kill_DHT(DHT *dht); void kill_DHT(DHT *dht);

View File

@ -1934,7 +1934,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
return NULL; return NULL;
} }
m->dht = new_DHT(m->log, m->net); m->dht = new_DHT(m->log, m->net, options->hole_punching_enabled);
if (m->dht == NULL) { if (m->dht == NULL) {
kill_networking(m->net); kill_networking(m->net);

View File

@ -77,6 +77,8 @@ typedef struct {
uint16_t port_range[2]; uint16_t port_range[2];
uint16_t tcp_server_port; uint16_t tcp_server_port;
uint8_t hole_punching_enabled;
logger_cb *log_callback; logger_cb *log_callback;
void *log_user_data; void *log_user_data;
} Messenger_Options; } Messenger_Options;

View File

@ -519,6 +519,11 @@ static class options {
*/ */
uint16_t tcp_port; uint16_t tcp_port;
/**
* Enables or disables UDP hole-punching in toxcore. (Default: enabled).
*/
bool hole_punching_enabled;
namespace savedata { namespace savedata {
/** /**
* The type of savedata to load from. * The type of savedata to load from.

View File

@ -131,6 +131,7 @@ ACCESSORS(uint16_t, proxy_ , port)
ACCESSORS(uint16_t, , start_port) ACCESSORS(uint16_t, , start_port)
ACCESSORS(uint16_t, , end_port) ACCESSORS(uint16_t, , end_port)
ACCESSORS(uint16_t, , tcp_port) ACCESSORS(uint16_t, , tcp_port)
ACCESSORS(bool, , hole_punching_enabled)
ACCESSORS(TOX_SAVEDATA_TYPE, savedata_, type) ACCESSORS(TOX_SAVEDATA_TYPE, savedata_, type)
ACCESSORS(size_t, savedata_, length) ACCESSORS(size_t, savedata_, length)
ACCESSORS(tox_log_cb *, log_, callback) ACCESSORS(tox_log_cb *, log_, callback)
@ -155,6 +156,7 @@ void tox_options_default(struct Tox_Options *options)
options->ipv6_enabled = 1; options->ipv6_enabled = 1;
options->udp_enabled = 1; options->udp_enabled = 1;
options->proxy_type = TOX_PROXY_TYPE_NONE; options->proxy_type = TOX_PROXY_TYPE_NONE;
options->hole_punching_enabled = true;
} }
} }
@ -219,6 +221,7 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error)
m_options.port_range[0] = options->start_port; m_options.port_range[0] = options->start_port;
m_options.port_range[1] = options->end_port; m_options.port_range[1] = options->end_port;
m_options.tcp_server_port = options->tcp_port; m_options.tcp_server_port = options->tcp_port;
m_options.hole_punching_enabled = options->hole_punching_enabled;
m_options.log_callback = (logger_cb *)options->log_callback; m_options.log_callback = (logger_cb *)options->log_callback;
m_options.log_user_data = options->log_user_data; m_options.log_user_data = options->log_user_data;

View File

@ -575,6 +575,12 @@ struct Tox_Options {
uint16_t tcp_port; uint16_t tcp_port;
/**
* Enables or disables UDP hole-punching in toxcore. (Default: enabled).
*/
bool hole_punching_enabled;
/** /**
* The type of savedata to load from. * The type of savedata to load from.
*/ */
@ -642,6 +648,10 @@ uint16_t tox_options_get_tcp_port(const struct Tox_Options *options);
void tox_options_set_tcp_port(struct Tox_Options *options, uint16_t tcp_port); void tox_options_set_tcp_port(struct Tox_Options *options, uint16_t tcp_port);
bool tox_options_get_hole_punching_enabled(const struct Tox_Options *options);
void tox_options_set_hole_punching_enabled(struct Tox_Options *options, bool hole_punching_enabled);
TOX_SAVEDATA_TYPE tox_options_get_savedata_type(const struct Tox_Options *options); TOX_SAVEDATA_TYPE tox_options_get_savedata_type(const struct Tox_Options *options);
void tox_options_set_savedata_type(struct Tox_Options *options, TOX_SAVEDATA_TYPE type); void tox_options_set_savedata_type(struct Tox_Options *options, TOX_SAVEDATA_TYPE type);