From f771bfbe30517ad2a01817eeaf5d16462b4913df Mon Sep 17 00:00:00 2001 From: saneki Date: Tue, 9 Sep 2014 11:34:39 -0500 Subject: [PATCH 1/3] Added tox_connect function, no connecting done during tox_load --- toxcore/DHT.c | 79 +++++++++++++++++++++++++++++++++++---------- toxcore/DHT.h | 13 ++++++++ toxcore/Messenger.c | 30 +++++++++++------ toxcore/Messenger.h | 7 ++++ toxcore/tox.c | 7 ++++ toxcore/tox.h | 7 ++++ 6 files changed, 117 insertions(+), 26 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 6bd23081..ac43371e 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -2333,6 +2333,58 @@ void DHT_save(DHT *dht, uint8_t *data) } } +static void DHT_bootstrap_loaded_clients(DHT *dht) +{ + uint32_t i; + + Client_data *client_list = dht->loaded_clients_list; + uint32_t client_count = dht->loaded_num_clients; + + for (i = 0; i < client_count; ++i) { + if (client_list[i].assoc4.timestamp != 0) + DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id); + + if (client_list[i].assoc6.timestamp != 0) + DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id); + } +} + +static void getnodes_of_loaded_friend_clients(DHT *dht) +{ + uint32_t i, j; + + DHT_Friend *friend_list = dht->loaded_friends_list; + uint32_t friend_count = dht->loaded_num_friends; + + for (i = 0; i < friend_count; ++i) { + for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { + Client_data *client = &friend_list[i].client_list[j]; + + if (client->assoc4.timestamp != 0) + getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id, NULL); + + if (client->assoc6.timestamp != 0) + getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id, NULL); + } + } +} + +/* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ +int DHT_connect_after_load(DHT *dht) +{ + if(dht == NULL || dht->loaded_friends_list == NULL || dht->loaded_clients_list == NULL) + return -1; + + getnodes_of_loaded_friend_clients(dht); + DHT_bootstrap_loaded_clients(dht); + + // Loaded lists were allocd, free them + free(dht->loaded_friends_list); + free(dht->loaded_clients_list); + + return 0; +} + static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) { DHT *dht = outer; @@ -2347,18 +2399,12 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le DHT_Friend *friend_list = (DHT_Friend *)data; num = length / sizeof(DHT_Friend); - for (i = 0; i < num; ++i) { + // Copy to loaded_friends_list + dht->loaded_friends_list = calloc(num, sizeof(DHT_Friend)); + for(i = 0; i < num; i++) + memcpy(&(dht->loaded_friends_list[i]), &(friend_list[i]), sizeof(DHT_Friend)); + dht->loaded_num_friends = num; - for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { - Client_data *client = &friend_list[i].client_list[j]; - - if (client->assoc4.timestamp != 0) - getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id, NULL); - - if (client->assoc6.timestamp != 0) - getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id, NULL); - } - } } /* localize declarations */ break; @@ -2371,13 +2417,12 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le num = length / sizeof(Client_data); Client_data *client_list = (Client_data *)data; - for (i = 0; i < num; ++i) { - if (client_list[i].assoc4.timestamp != 0) - DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id); + // Copy to loaded_clients_list + dht->loaded_clients_list = calloc(num, sizeof(Client_data)); + for(i = 0; i < num; i++) + memcpy(&(dht->loaded_clients_list[i]), &(client_list[i]), sizeof(Client_data)); + dht->loaded_num_clients = num; - if (client_list[i].assoc6.timestamp != 0) - DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id); - } } /* localize declarations */ break; diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 563ae08c..1ead6f5e 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -200,6 +200,13 @@ typedef struct { DHT_Friend *friends_list; uint16_t num_friends; + // Used after loading of file (tox_load), but no longer needed after connect (tox_connect) + // Unsure if friends_list and num_friends could just be used instead? + DHT_Friend *loaded_friends_list; + uint32_t loaded_num_friends; + Client_data *loaded_clients_list; + uint32_t loaded_num_clients; + Shared_Keys shared_keys_recv; Shared_Keys shared_keys_sent; @@ -332,6 +339,12 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, const uint8_t *public_key); int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, const uint8_t *public_key); +/* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set. + * + * returns 0 if successful + * returns -1 otherwise + */ +int DHT_connect_after_load(DHT *dht); /* ROUTING FUNCTIONS */ diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 7ea89ea6..e138b23e 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2803,18 +2803,11 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3 break; case MESSENGER_STATE_TYPE_TCP_RELAY: { - Node_format relays[NUM_SAVED_TCP_RELAYS]; - - if (length != sizeof(relays)) { + if (length != sizeof(m->loaded_relays)) { return -1; } - memcpy(relays, data, length); - uint32_t i; - - for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) { - add_tcp_relay(m->net_crypto, relays[i].ip_port, relays[i].client_id); - } + memcpy(m->loaded_relays, data, length); break; } @@ -2866,6 +2859,25 @@ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length) return -1; } +/* Connect after loading messenger from file */ +int messenger_connect(Messenger *m) +{ + int i; + + if(m == NULL) + return -1; + + DHT *dht = m->dht; + if(DHT_connect_after_load(dht) == -1) + return -1; + + for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) { + add_tcp_relay(m->net_crypto, m->loaded_relays[i].ip_port, m->loaded_relays[i].client_id); + } + + return 0; +} + /* Return the number of friends in the instance m. * You should use this to determine how much memory to allocate * for copy_friendlist. */ diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index cead6411..520fc460 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -219,6 +219,10 @@ typedef struct Messenger { uint64_t last_LANdiscovery; + // Relays loaded from config + // 8 should be NUM_SAVED_TCP_RELAYS but it is defined in c file + Node_format loaded_relays[8]; + void (*friend_message)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *); void *friend_message_userdata; void (*friend_action)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *); @@ -776,6 +780,9 @@ void messenger_save(const Messenger *m, uint8_t *data); /* Load the messenger from data of size length. */ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length); +/* Connect after loading messenger from file */ +int messenger_connect(Messenger *m); + /* Return the number of friends in the instance m. * You should use this to determine how much memory to allocate * for copy_friendlist. */ diff --git a/toxcore/tox.c b/toxcore/tox.c index b2aadd39..2b412ce8 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -928,3 +928,10 @@ int tox_load(Tox *tox, const uint8_t *data, uint32_t length) Messenger *m = tox; return messenger_load(m, data, length); } + +/* Connect after loading the messenger from file */ +int tox_connect(Tox *tox) +{ + Messenger *m = tox; + return messenger_connect(m); +} diff --git a/toxcore/tox.h b/toxcore/tox.h index 1f251085..8a564e6b 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -723,6 +723,13 @@ void tox_save(const Tox *tox, uint8_t *data); */ int tox_load(Tox *tox, const uint8_t *data, uint32_t length); +/* Perform connections after messenger has been loaded from file. + * + * returns 0 on success + * returns -1 on failure + */ +int tox_connect(Tox *tox); + #ifdef __cplusplus } #endif From 98a93c7880dfc4ce0e1466dae3d1757df580378b Mon Sep 17 00:00:00 2001 From: saneki Date: Tue, 9 Sep 2014 14:48:09 -0500 Subject: [PATCH 2/3] Fixed spacing --- toxcore/DHT.c | 78 ++++++++++++++++++++++----------------------- toxcore/DHT.h | 12 +++---- toxcore/Messenger.c | 20 ++++++------ toxcore/Messenger.h | 6 ++-- toxcore/tox.c | 4 +-- 5 files changed, 60 insertions(+), 60 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index ac43371e..5041cc4e 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -2335,54 +2335,54 @@ void DHT_save(DHT *dht, uint8_t *data) static void DHT_bootstrap_loaded_clients(DHT *dht) { - uint32_t i; + uint32_t i; - Client_data *client_list = dht->loaded_clients_list; - uint32_t client_count = dht->loaded_num_clients; + Client_data *client_list = dht->loaded_clients_list; + uint32_t client_count = dht->loaded_num_clients; - for (i = 0; i < client_count; ++i) { - if (client_list[i].assoc4.timestamp != 0) - DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id); + for (i = 0; i < client_count; ++i) { + if (client_list[i].assoc4.timestamp != 0) + DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id); - if (client_list[i].assoc6.timestamp != 0) - DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id); - } + if (client_list[i].assoc6.timestamp != 0) + DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id); + } } static void getnodes_of_loaded_friend_clients(DHT *dht) { - uint32_t i, j; + uint32_t i, j; - DHT_Friend *friend_list = dht->loaded_friends_list; - uint32_t friend_count = dht->loaded_num_friends; + DHT_Friend *friend_list = dht->loaded_friends_list; + uint32_t friend_count = dht->loaded_num_friends; - for (i = 0; i < friend_count; ++i) { - for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { - Client_data *client = &friend_list[i].client_list[j]; + for (i = 0; i < friend_count; ++i) { + for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { + Client_data *client = &friend_list[i].client_list[j]; - if (client->assoc4.timestamp != 0) - getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id, NULL); + if (client->assoc4.timestamp != 0) + getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id, NULL); - if (client->assoc6.timestamp != 0) - getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id, NULL); - } - } + if (client->assoc6.timestamp != 0) + getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id, NULL); + } + } } /* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ int DHT_connect_after_load(DHT *dht) { - if(dht == NULL || dht->loaded_friends_list == NULL || dht->loaded_clients_list == NULL) - return -1; + if(dht == NULL || dht->loaded_friends_list == NULL || dht->loaded_clients_list == NULL) + return -1; - getnodes_of_loaded_friend_clients(dht); - DHT_bootstrap_loaded_clients(dht); + getnodes_of_loaded_friend_clients(dht); + DHT_bootstrap_loaded_clients(dht); - // Loaded lists were allocd, free them - free(dht->loaded_friends_list); - free(dht->loaded_clients_list); + // Loaded lists were allocd, free them + free(dht->loaded_friends_list); + free(dht->loaded_clients_list); - return 0; + return 0; } static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) @@ -2399,11 +2399,11 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le DHT_Friend *friend_list = (DHT_Friend *)data; num = length / sizeof(DHT_Friend); - // Copy to loaded_friends_list - dht->loaded_friends_list = calloc(num, sizeof(DHT_Friend)); - for(i = 0; i < num; i++) - memcpy(&(dht->loaded_friends_list[i]), &(friend_list[i]), sizeof(DHT_Friend)); - dht->loaded_num_friends = num; + // Copy to loaded_friends_list + dht->loaded_friends_list = calloc(num, sizeof(DHT_Friend)); + for(i = 0; i < num; i++) + memcpy(&(dht->loaded_friends_list[i]), &(friend_list[i]), sizeof(DHT_Friend)); + dht->loaded_num_friends = num; } /* localize declarations */ @@ -2417,11 +2417,11 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le num = length / sizeof(Client_data); Client_data *client_list = (Client_data *)data; - // Copy to loaded_clients_list - dht->loaded_clients_list = calloc(num, sizeof(Client_data)); - for(i = 0; i < num; i++) - memcpy(&(dht->loaded_clients_list[i]), &(client_list[i]), sizeof(Client_data)); - dht->loaded_num_clients = num; + // Copy to loaded_clients_list + dht->loaded_clients_list = calloc(num, sizeof(Client_data)); + for(i = 0; i < num; i++) + memcpy(&(dht->loaded_clients_list[i]), &(client_list[i]), sizeof(Client_data)); + dht->loaded_num_clients = num; } /* localize declarations */ diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 1ead6f5e..bbba6209 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -200,12 +200,12 @@ typedef struct { DHT_Friend *friends_list; uint16_t num_friends; - // Used after loading of file (tox_load), but no longer needed after connect (tox_connect) - // Unsure if friends_list and num_friends could just be used instead? - DHT_Friend *loaded_friends_list; - uint32_t loaded_num_friends; - Client_data *loaded_clients_list; - uint32_t loaded_num_clients; + // Used after loading of file (tox_load), but no longer needed after connect (tox_connect) + // Unsure if friends_list and num_friends could just be used instead? + DHT_Friend *loaded_friends_list; + uint32_t loaded_num_friends; + Client_data *loaded_clients_list; + uint32_t loaded_num_clients; Shared_Keys shared_keys_recv; Shared_Keys shared_keys_sent; diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index e138b23e..a6bfbc8a 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2862,20 +2862,20 @@ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length) /* Connect after loading messenger from file */ int messenger_connect(Messenger *m) { - int i; + int i; - if(m == NULL) - return -1; + if(m == NULL) + return -1; - DHT *dht = m->dht; - if(DHT_connect_after_load(dht) == -1) - return -1; + DHT *dht = m->dht; + if(DHT_connect_after_load(dht) == -1) + return -1; - for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) { - add_tcp_relay(m->net_crypto, m->loaded_relays[i].ip_port, m->loaded_relays[i].client_id); - } + for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) { + add_tcp_relay(m->net_crypto, m->loaded_relays[i].ip_port, m->loaded_relays[i].client_id); + } - return 0; + return 0; } /* Return the number of friends in the instance m. diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 520fc460..ee3c3fa5 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -219,9 +219,9 @@ typedef struct Messenger { uint64_t last_LANdiscovery; - // Relays loaded from config - // 8 should be NUM_SAVED_TCP_RELAYS but it is defined in c file - Node_format loaded_relays[8]; + // Relays loaded from config + // 8 should be NUM_SAVED_TCP_RELAYS but it is defined in c file + Node_format loaded_relays[8]; void (*friend_message)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *); void *friend_message_userdata; diff --git a/toxcore/tox.c b/toxcore/tox.c index 2b412ce8..73bbe1c6 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -932,6 +932,6 @@ int tox_load(Tox *tox, const uint8_t *data, uint32_t length) /* Connect after loading the messenger from file */ int tox_connect(Tox *tox) { - Messenger *m = tox; - return messenger_connect(m); + Messenger *m = tox; + return messenger_connect(m); } From 27369ac76278aace29b52eb5523af80f3d9a1880 Mon Sep 17 00:00:00 2001 From: saneki Date: Fri, 12 Sep 2014 12:21:17 -0500 Subject: [PATCH 3/3] Removed tox_connect, initial connections are made on first tox_do --- toxcore/DHT.c | 7 +++++++ toxcore/DHT.h | 1 + toxcore/Messenger.c | 31 +++++++++++-------------------- toxcore/Messenger.h | 10 +++------- toxcore/tox.c | 7 ------- toxcore/tox.h | 7 ------- 6 files changed, 22 insertions(+), 41 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 5041cc4e..be675b26 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -2229,6 +2229,13 @@ DHT *new_DHT(Networking_Core *net) void do_DHT(DHT *dht) { + // Load friends/clients if first call to do_DHT + if(dht->has_loaded_friends_clients == 0) + { + dht->has_loaded_friends_clients = 1; + DHT_connect_after_load(dht); + } + unix_time_update(); if (dht->last_run == unix_time()) { diff --git a/toxcore/DHT.h b/toxcore/DHT.h index bbba6209..4beda37e 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -202,6 +202,7 @@ typedef struct { // Used after loading of file (tox_load), but no longer needed after connect (tox_connect) // Unsure if friends_list and num_friends could just be used instead? + int has_loaded_friends_clients; // Whether or not we have loaded on the first do_DHT DHT_Friend *loaded_friends_list; uint32_t loaded_num_friends; Client_data *loaded_clients_list; diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index a6bfbc8a..5dbed346 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2408,6 +2408,17 @@ uint32_t messenger_run_interval(Messenger *m) /* The main loop that needs to be run at least 20 times per second. */ void do_messenger(Messenger *m) { + // Add the TCP relays, but only if this is the first time calling do_messenger + if(m->has_added_relays == 0) + { + m->has_added_relays = 1; + + int i; + for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) { + add_tcp_relay(m->net_crypto, m->loaded_relays[i].ip_port, m->loaded_relays[i].client_id); + } + } + unix_time_update(); if (!m->options.udp_disabled) { @@ -2554,7 +2565,6 @@ void do_messenger(Messenger *m) #define MESSENGER_STATE_TYPE_PATH_NODE 11 #define SAVED_FRIEND_REQUEST_SIZE 1024 -#define NUM_SAVED_TCP_RELAYS 8 #define NUM_SAVED_PATH_NODES 8 struct SAVED_FRIEND { uint8_t status; @@ -2859,25 +2869,6 @@ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length) return -1; } -/* Connect after loading messenger from file */ -int messenger_connect(Messenger *m) -{ - int i; - - if(m == NULL) - return -1; - - DHT *dht = m->dht; - if(DHT_connect_after_load(dht) == -1) - return -1; - - for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) { - add_tcp_relay(m->net_crypto, m->loaded_relays[i].ip_port, m->loaded_relays[i].client_id); - } - - return 0; -} - /* Return the number of friends in the instance m. * You should use this to determine how much memory to allocate * for copy_friendlist. */ diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index ee3c3fa5..c3ae686a 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -189,7 +189,7 @@ typedef struct { } lossless_packethandlers[PACKET_ID_LOSSLESS_RANGE_SIZE]; } Friend; - +#define NUM_SAVED_TCP_RELAYS 8 typedef struct Messenger { Networking_Core *net; @@ -219,9 +219,8 @@ typedef struct Messenger { uint64_t last_LANdiscovery; - // Relays loaded from config - // 8 should be NUM_SAVED_TCP_RELAYS but it is defined in c file - Node_format loaded_relays[8]; + uint8_t has_added_relays; // If the first connection has occurred in do_messenger + Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config void (*friend_message)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *); void *friend_message_userdata; @@ -780,9 +779,6 @@ void messenger_save(const Messenger *m, uint8_t *data); /* Load the messenger from data of size length. */ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length); -/* Connect after loading messenger from file */ -int messenger_connect(Messenger *m); - /* Return the number of friends in the instance m. * You should use this to determine how much memory to allocate * for copy_friendlist. */ diff --git a/toxcore/tox.c b/toxcore/tox.c index 73bbe1c6..b2aadd39 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -928,10 +928,3 @@ int tox_load(Tox *tox, const uint8_t *data, uint32_t length) Messenger *m = tox; return messenger_load(m, data, length); } - -/* Connect after loading the messenger from file */ -int tox_connect(Tox *tox) -{ - Messenger *m = tox; - return messenger_connect(m); -} diff --git a/toxcore/tox.h b/toxcore/tox.h index 8a564e6b..1f251085 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -723,13 +723,6 @@ void tox_save(const Tox *tox, uint8_t *data); */ int tox_load(Tox *tox, const uint8_t *data, uint32_t length); -/* Perform connections after messenger has been loaded from file. - * - * returns 0 on success - * returns -1 on failure - */ -int tox_connect(Tox *tox); - #ifdef __cplusplus } #endif