summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/LAN_discovery.c5
-rw-r--r--toxcore/LAN_discovery.h3
-rw-r--r--toxcore/Messenger.c11
-rw-r--r--toxcore/Messenger.h2
-rw-r--r--toxcore/friend_connection.c62
-rw-r--r--toxcore/friend_connection.h2
6 files changed, 48 insertions, 37 deletions
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index bc020d87..dbce762e 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -336,3 +336,8 @@ void LANdiscovery_init(DHT *dht)
336{ 336{
337 networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht); 337 networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht);
338} 338}
339
340void LANdiscovery_kill(DHT *dht)
341{
342 networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, NULL, NULL);
343}
diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h
index 5dffc3ad..5243bd93 100644
--- a/toxcore/LAN_discovery.h
+++ b/toxcore/LAN_discovery.h
@@ -37,6 +37,9 @@ int send_LANdiscovery(uint16_t port, DHT *dht);
37/* Sets up packet handlers. */ 37/* Sets up packet handlers. */
38void LANdiscovery_init(DHT *dht); 38void LANdiscovery_init(DHT *dht);
39 39
40/* Clear packet handlers. */
41void LANdiscovery_kill(DHT *dht);
42
40/* checks if a given IP isn't routable 43/* checks if a given IP isn't routable
41 * 44 *
42 * return 0 if ip is a LAN ip. 45 * return 0 if ip is a LAN ip.
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index fd17ab98..a7e0a9fe 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1765,15 +1765,6 @@ static int friend_already_added(const uint8_t *real_pk, void *data)
1765 return -1; 1765 return -1;
1766} 1766}
1767 1767
1768/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
1769static void LANdiscovery(Messenger *m)
1770{
1771 if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
1772 send_LANdiscovery(htons(TOX_PORT_DEFAULT), m->dht);
1773 m->last_LANdiscovery = unix_time();
1774 }
1775}
1776
1777/* Run this at startup. */ 1768/* Run this at startup. */
1778Messenger *new_messenger(Messenger_Options *options, unsigned int *error) 1769Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
1779{ 1770{
@@ -1842,7 +1833,6 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
1842 1833
1843 m->options = *options; 1834 m->options = *options;
1844 friendreq_init(&(m->fr), m->fr_c); 1835 friendreq_init(&(m->fr), m->fr_c);
1845 LANdiscovery_init(m->dht);
1846 set_nospam(&(m->fr), random_int()); 1836 set_nospam(&(m->fr), random_int());
1847 set_filter_function(&(m->fr), &friend_already_added, m); 1837 set_filter_function(&(m->fr), &friend_already_added, m);
1848 1838
@@ -2308,7 +2298,6 @@ void do_messenger(Messenger *m)
2308 do_onion_client(m->onion_c); 2298 do_onion_client(m->onion_c);
2309 do_friend_connections(m->fr_c); 2299 do_friend_connections(m->fr_c);
2310 do_friends(m); 2300 do_friends(m);
2311 LANdiscovery(m);
2312 connection_status_cb(m); 2301 connection_status_cb(m);
2313 2302
2314#ifdef LOGGING 2303#ifdef LOGGING
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 5221e639..6943475f 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -234,8 +234,6 @@ struct Messenger {
234 234
235 uint32_t numonline_friends; 235 uint32_t numonline_friends;
236 236
237 uint64_t last_LANdiscovery;
238
239#define NUM_SAVED_TCP_RELAYS 8 237#define NUM_SAVED_TCP_RELAYS 8
240 uint8_t has_added_relays; // If the first connection has occurred in do_messenger 238 uint8_t has_added_relays; // If the first connection has occurred in do_messenger
241 Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config 239 Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config
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. */
296static 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
319static int handle_status(void *object, int number, uint8_t status) 295static 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. */
336static 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
359static int handle_packet(void *object, int number, uint8_t *data, uint16_t length) 360static 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. */
767static 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. */
765void do_friend_connections(Friend_Connections *fr_c) 776void 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}
diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h
index 60b62646..baca4b76 100644
--- a/toxcore/friend_connection.h
+++ b/toxcore/friend_connection.h
@@ -109,6 +109,8 @@ typedef struct {
109 109
110 int (*fr_request_callback)(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t len); 110 int (*fr_request_callback)(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t len);
111 void *fr_request_object; 111 void *fr_request_object;
112
113 uint64_t last_LANdiscovery;
112} Friend_Connections; 114} Friend_Connections;
113 115
114/* return friendcon_id corresponding to the real public key on success. 116/* return friendcon_id corresponding to the real public key on success.