diff options
Diffstat (limited to 'toxcore/friend_connection.c')
-rw-r--r-- | toxcore/friend_connection.c | 62 |
1 files changed, 38 insertions, 24 deletions
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 892048ad..c13ca949 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c | |||
@@ -292,30 +292,6 @@ static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint | |||
292 | memcpy(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES); | 292 | memcpy(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES); |
293 | } | 293 | } |
294 | 294 | ||
295 | /* Callback for dht public key changes. */ | ||
296 | static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key) | ||
297 | { | ||
298 | Friend_Connections *fr_c = object; | ||
299 | Friend_Conn *friend_con = get_conn(fr_c, number); | ||
300 | |||
301 | if (!friend_con) | ||
302 | return; | ||
303 | |||
304 | if (memcmp(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0) | ||
305 | return; | ||
306 | |||
307 | change_dht_pk(fr_c, number, dht_public_key); | ||
308 | |||
309 | /* if pk changed, create a new connection.*/ | ||
310 | if (friend_con->crypt_connection_id != -1) { | ||
311 | crypto_kill(fr_c->net_crypto, friend_con->crypt_connection_id); | ||
312 | friend_con->crypt_connection_id = -1; | ||
313 | } | ||
314 | |||
315 | friend_new_connection(fr_c, number); | ||
316 | onion_set_friend_DHT_pubkey(fr_c->onion_c, friend_con->onion_friendnum, dht_public_key); | ||
317 | } | ||
318 | |||
319 | static int handle_status(void *object, int number, uint8_t status) | 295 | static int handle_status(void *object, int number, uint8_t status) |
320 | { | 296 | { |
321 | Friend_Connections *fr_c = object; | 297 | Friend_Connections *fr_c = object; |
@@ -356,6 +332,31 @@ static int handle_status(void *object, int number, uint8_t status) | |||
356 | return 0; | 332 | return 0; |
357 | } | 333 | } |
358 | 334 | ||
335 | /* Callback for dht public key changes. */ | ||
336 | static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key) | ||
337 | { | ||
338 | Friend_Connections *fr_c = object; | ||
339 | Friend_Conn *friend_con = get_conn(fr_c, number); | ||
340 | |||
341 | if (!friend_con) | ||
342 | return; | ||
343 | |||
344 | if (memcmp(friend_con->dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0) | ||
345 | return; | ||
346 | |||
347 | change_dht_pk(fr_c, number, dht_public_key); | ||
348 | |||
349 | /* if pk changed, create a new connection.*/ | ||
350 | if (friend_con->crypt_connection_id != -1) { | ||
351 | crypto_kill(fr_c->net_crypto, friend_con->crypt_connection_id); | ||
352 | friend_con->crypt_connection_id = -1; | ||
353 | handle_status(object, number, 0); /* Going offline. */ | ||
354 | } | ||
355 | |||
356 | friend_new_connection(fr_c, number); | ||
357 | onion_set_friend_DHT_pubkey(fr_c->onion_c, friend_con->onion_friendnum, dht_public_key); | ||
358 | } | ||
359 | |||
359 | static int handle_packet(void *object, int number, uint8_t *data, uint16_t length) | 360 | static int handle_packet(void *object, int number, uint8_t *data, uint16_t length) |
360 | { | 361 | { |
361 | if (length == 0) | 362 | if (length == 0) |
@@ -757,10 +758,20 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c) | |||
757 | temp->onion_c = onion_c; | 758 | temp->onion_c = onion_c; |
758 | 759 | ||
759 | new_connection_handler(temp->net_crypto, &handle_new_connections, temp); | 760 | new_connection_handler(temp->net_crypto, &handle_new_connections, temp); |
761 | LANdiscovery_init(temp->dht); | ||
760 | 762 | ||
761 | return temp; | 763 | return temp; |
762 | } | 764 | } |
763 | 765 | ||
766 | /* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ | ||
767 | static void LANdiscovery(Friend_Connections *fr_c) | ||
768 | { | ||
769 | if (fr_c->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { | ||
770 | send_LANdiscovery(htons(TOX_PORT_DEFAULT), fr_c->dht); | ||
771 | fr_c->last_LANdiscovery = unix_time(); | ||
772 | } | ||
773 | } | ||
774 | |||
764 | /* main friend_connections loop. */ | 775 | /* main friend_connections loop. */ |
765 | void do_friend_connections(Friend_Connections *fr_c) | 776 | void do_friend_connections(Friend_Connections *fr_c) |
766 | { | 777 | { |
@@ -808,6 +819,8 @@ void do_friend_connections(Friend_Connections *fr_c) | |||
808 | } | 819 | } |
809 | } | 820 | } |
810 | } | 821 | } |
822 | |||
823 | LANdiscovery(fr_c); | ||
811 | } | 824 | } |
812 | 825 | ||
813 | /* Free everything related with friend_connections. */ | 826 | /* Free everything related with friend_connections. */ |
@@ -822,5 +835,6 @@ void kill_friend_connections(Friend_Connections *fr_c) | |||
822 | kill_friend_connection(fr_c, i); | 835 | kill_friend_connection(fr_c, i); |
823 | } | 836 | } |
824 | 837 | ||
838 | LANdiscovery_kill(fr_c->dht); | ||
825 | free(fr_c); | 839 | free(fr_c); |
826 | } | 840 | } |