summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsaneki <fake.saneki@gmail.com>2014-09-09 11:34:39 -0500
committersaneki <fake.saneki@gmail.com>2014-09-09 11:34:39 -0500
commitf771bfbe30517ad2a01817eeaf5d16462b4913df (patch)
treea0afea2ee4b097cabdcd5a338bbae5ee5b66fe95
parenta0302b7acaef47805f93718630e501acdd3a3d00 (diff)
Added tox_connect function, no connecting done during tox_load
-rw-r--r--toxcore/DHT.c79
-rw-r--r--toxcore/DHT.h13
-rw-r--r--toxcore/Messenger.c30
-rw-r--r--toxcore/Messenger.h7
-rw-r--r--toxcore/tox.c7
-rw-r--r--toxcore/tox.h7
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)
2333 } 2333 }
2334} 2334}
2335 2335
2336static void DHT_bootstrap_loaded_clients(DHT *dht)
2337{
2338 uint32_t i;
2339
2340 Client_data *client_list = dht->loaded_clients_list;
2341 uint32_t client_count = dht->loaded_num_clients;
2342
2343 for (i = 0; i < client_count; ++i) {
2344 if (client_list[i].assoc4.timestamp != 0)
2345 DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id);
2346
2347 if (client_list[i].assoc6.timestamp != 0)
2348 DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id);
2349 }
2350}
2351
2352static void getnodes_of_loaded_friend_clients(DHT *dht)
2353{
2354 uint32_t i, j;
2355
2356 DHT_Friend *friend_list = dht->loaded_friends_list;
2357 uint32_t friend_count = dht->loaded_num_friends;
2358
2359 for (i = 0; i < friend_count; ++i) {
2360 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
2361 Client_data *client = &friend_list[i].client_list[j];
2362
2363 if (client->assoc4.timestamp != 0)
2364 getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id, NULL);
2365
2366 if (client->assoc6.timestamp != 0)
2367 getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id, NULL);
2368 }
2369 }
2370}
2371
2372/* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */
2373int DHT_connect_after_load(DHT *dht)
2374{
2375 if(dht == NULL || dht->loaded_friends_list == NULL || dht->loaded_clients_list == NULL)
2376 return -1;
2377
2378 getnodes_of_loaded_friend_clients(dht);
2379 DHT_bootstrap_loaded_clients(dht);
2380
2381 // Loaded lists were allocd, free them
2382 free(dht->loaded_friends_list);
2383 free(dht->loaded_clients_list);
2384
2385 return 0;
2386}
2387
2336static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) 2388static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
2337{ 2389{
2338 DHT *dht = outer; 2390 DHT *dht = outer;
@@ -2347,18 +2399,12 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le
2347 DHT_Friend *friend_list = (DHT_Friend *)data; 2399 DHT_Friend *friend_list = (DHT_Friend *)data;
2348 num = length / sizeof(DHT_Friend); 2400 num = length / sizeof(DHT_Friend);
2349 2401
2350 for (i = 0; i < num; ++i) { 2402 // Copy to loaded_friends_list
2351 2403 dht->loaded_friends_list = calloc(num, sizeof(DHT_Friend));
2352 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 2404 for(i = 0; i < num; i++)
2353 Client_data *client = &friend_list[i].client_list[j]; 2405 memcpy(&(dht->loaded_friends_list[i]), &(friend_list[i]), sizeof(DHT_Friend));
2354 2406 dht->loaded_num_friends = num;
2355 if (client->assoc4.timestamp != 0)
2356 getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id, NULL);
2357 2407
2358 if (client->assoc6.timestamp != 0)
2359 getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id, NULL);
2360 }
2361 }
2362 } /* localize declarations */ 2408 } /* localize declarations */
2363 2409
2364 break; 2410 break;
@@ -2371,13 +2417,12 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le
2371 num = length / sizeof(Client_data); 2417 num = length / sizeof(Client_data);
2372 Client_data *client_list = (Client_data *)data; 2418 Client_data *client_list = (Client_data *)data;
2373 2419
2374 for (i = 0; i < num; ++i) { 2420 // Copy to loaded_clients_list
2375 if (client_list[i].assoc4.timestamp != 0) 2421 dht->loaded_clients_list = calloc(num, sizeof(Client_data));
2376 DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id); 2422 for(i = 0; i < num; i++)
2423 memcpy(&(dht->loaded_clients_list[i]), &(client_list[i]), sizeof(Client_data));
2424 dht->loaded_num_clients = num;
2377 2425
2378 if (client_list[i].assoc6.timestamp != 0)
2379 DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id);
2380 }
2381 } /* localize declarations */ 2426 } /* localize declarations */
2382 2427
2383 break; 2428 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 {
200 DHT_Friend *friends_list; 200 DHT_Friend *friends_list;
201 uint16_t num_friends; 201 uint16_t num_friends;
202 202
203 // Used after loading of file (tox_load), but no longer needed after connect (tox_connect)
204 // Unsure if friends_list and num_friends could just be used instead?
205 DHT_Friend *loaded_friends_list;
206 uint32_t loaded_num_friends;
207 Client_data *loaded_clients_list;
208 uint32_t loaded_num_clients;
209
203 Shared_Keys shared_keys_recv; 210 Shared_Keys shared_keys_recv;
204 Shared_Keys shared_keys_sent; 211 Shared_Keys shared_keys_sent;
205 212
@@ -332,6 +339,12 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, const uint8_t *public_key);
332int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, 339int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled,
333 uint16_t port, const uint8_t *public_key); 340 uint16_t port, const uint8_t *public_key);
334 341
342/* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set.
343 *
344 * returns 0 if successful
345 * returns -1 otherwise
346 */
347int DHT_connect_after_load(DHT *dht);
335 348
336/* ROUTING FUNCTIONS */ 349/* ROUTING FUNCTIONS */
337 350
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
2803 break; 2803 break;
2804 2804
2805 case MESSENGER_STATE_TYPE_TCP_RELAY: { 2805 case MESSENGER_STATE_TYPE_TCP_RELAY: {
2806 Node_format relays[NUM_SAVED_TCP_RELAYS]; 2806 if (length != sizeof(m->loaded_relays)) {
2807
2808 if (length != sizeof(relays)) {
2809 return -1; 2807 return -1;
2810 } 2808 }
2811 2809
2812 memcpy(relays, data, length); 2810 memcpy(m->loaded_relays, data, length);
2813 uint32_t i;
2814
2815 for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) {
2816 add_tcp_relay(m->net_crypto, relays[i].ip_port, relays[i].client_id);
2817 }
2818 2811
2819 break; 2812 break;
2820 } 2813 }
@@ -2866,6 +2859,25 @@ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length)
2866 return -1; 2859 return -1;
2867} 2860}
2868 2861
2862/* Connect after loading messenger from file */
2863int messenger_connect(Messenger *m)
2864{
2865 int i;
2866
2867 if(m == NULL)
2868 return -1;
2869
2870 DHT *dht = m->dht;
2871 if(DHT_connect_after_load(dht) == -1)
2872 return -1;
2873
2874 for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) {
2875 add_tcp_relay(m->net_crypto, m->loaded_relays[i].ip_port, m->loaded_relays[i].client_id);
2876 }
2877
2878 return 0;
2879}
2880
2869/* Return the number of friends in the instance m. 2881/* Return the number of friends in the instance m.
2870 * You should use this to determine how much memory to allocate 2882 * You should use this to determine how much memory to allocate
2871 * for copy_friendlist. */ 2883 * 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 {
219 219
220 uint64_t last_LANdiscovery; 220 uint64_t last_LANdiscovery;
221 221
222 // Relays loaded from config
223 // 8 should be NUM_SAVED_TCP_RELAYS but it is defined in c file
224 Node_format loaded_relays[8];
225
222 void (*friend_message)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *); 226 void (*friend_message)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *);
223 void *friend_message_userdata; 227 void *friend_message_userdata;
224 void (*friend_action)(struct Messenger *m, int32_t, const uint8_t *, uint16_t, void *); 228 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);
776/* Load the messenger from data of size length. */ 780/* Load the messenger from data of size length. */
777int messenger_load(Messenger *m, const uint8_t *data, uint32_t length); 781int messenger_load(Messenger *m, const uint8_t *data, uint32_t length);
778 782
783/* Connect after loading messenger from file */
784int messenger_connect(Messenger *m);
785
779/* Return the number of friends in the instance m. 786/* Return the number of friends in the instance m.
780 * You should use this to determine how much memory to allocate 787 * You should use this to determine how much memory to allocate
781 * for copy_friendlist. */ 788 * 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)
928 Messenger *m = tox; 928 Messenger *m = tox;
929 return messenger_load(m, data, length); 929 return messenger_load(m, data, length);
930} 930}
931
932/* Connect after loading the messenger from file */
933int tox_connect(Tox *tox)
934{
935 Messenger *m = tox;
936 return messenger_connect(m);
937}
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);
723 */ 723 */
724int tox_load(Tox *tox, const uint8_t *data, uint32_t length); 724int tox_load(Tox *tox, const uint8_t *data, uint32_t length);
725 725
726/* Perform connections after messenger has been loaded from file.
727 *
728 * returns 0 on success
729 * returns -1 on failure
730 */
731int tox_connect(Tox *tox);
732
726#ifdef __cplusplus 733#ifdef __cplusplus
727} 734}
728#endif 735#endif