This commit is contained in:
irungentoo 2014-09-12 17:07:27 -04:00
commit 269fd6814e
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
4 changed files with 101 additions and 25 deletions

View File

@ -2224,6 +2224,12 @@ DHT *new_DHT(Networking_Core *net)
void do_DHT(DHT *dht) 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(); unix_time_update();
if (dht->last_run == unix_time()) { if (dht->last_run == unix_time()) {
@ -2328,6 +2334,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) static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
{ {
DHT *dht = outer; DHT *dht = outer;
@ -2342,18 +2400,14 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le
DHT_Friend *friend_list = (DHT_Friend *)data; DHT_Friend *friend_list = (DHT_Friend *)data;
num = length / sizeof(DHT_Friend); 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 (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { for (i = 0; i < num; i++)
Client_data *client = &friend_list[i].client_list[j]; memcpy(&(dht->loaded_friends_list[i]), &(friend_list[i]), sizeof(DHT_Friend));
if (client->assoc4.timestamp != 0) dht->loaded_num_friends = num;
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 */ } /* localize declarations */
break; break;
@ -2366,13 +2420,14 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le
num = length / sizeof(Client_data); num = length / sizeof(Client_data);
Client_data *client_list = (Client_data *)data; Client_data *client_list = (Client_data *)data;
for (i = 0; i < num; ++i) { // Copy to loaded_clients_list
if (client_list[i].assoc4.timestamp != 0) dht->loaded_clients_list = calloc(num, sizeof(Client_data));
DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id);
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 */ } /* localize declarations */
break; break;

View File

@ -200,6 +200,14 @@ typedef struct {
DHT_Friend *friends_list; DHT_Friend *friends_list;
uint16_t num_friends; 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?
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;
uint32_t loaded_num_clients;
Shared_Keys shared_keys_recv; Shared_Keys shared_keys_recv;
Shared_Keys shared_keys_sent; Shared_Keys shared_keys_sent;
@ -332,6 +340,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, int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled,
uint16_t port, const uint8_t *public_key); 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 */ /* ROUTING FUNCTIONS */

View File

@ -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. */ /* The main loop that needs to be run at least 20 times per second. */
void do_messenger(Messenger *m) 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(); unix_time_update();
if (!m->options.udp_disabled) { if (!m->options.udp_disabled) {
@ -2554,7 +2565,6 @@ void do_messenger(Messenger *m)
#define MESSENGER_STATE_TYPE_PATH_NODE 11 #define MESSENGER_STATE_TYPE_PATH_NODE 11
#define SAVED_FRIEND_REQUEST_SIZE 1024 #define SAVED_FRIEND_REQUEST_SIZE 1024
#define NUM_SAVED_TCP_RELAYS 8
#define NUM_SAVED_PATH_NODES 8 #define NUM_SAVED_PATH_NODES 8
struct SAVED_FRIEND { struct SAVED_FRIEND {
uint8_t status; uint8_t status;
@ -2803,18 +2813,11 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
break; break;
case MESSENGER_STATE_TYPE_TCP_RELAY: { case MESSENGER_STATE_TYPE_TCP_RELAY: {
Node_format relays[NUM_SAVED_TCP_RELAYS]; if (length != sizeof(m->loaded_relays)) {
if (length != sizeof(relays)) {
return -1; return -1;
} }
memcpy(relays, data, length); memcpy(m->loaded_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);
}
break; break;
} }

View File

@ -219,6 +219,10 @@ typedef struct Messenger {
uint64_t last_LANdiscovery; uint64_t last_LANdiscovery;
#define NUM_SAVED_TCP_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)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *);
void *friend_message_userdata; void *friend_message_userdata;
void (*friend_action)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *); void (*friend_action)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *);