summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-04-27 16:13:04 -0400
committerirungentoo <irungentoo@gmail.com>2015-04-27 16:13:04 -0400
commit69e3e5f3a4510e4883edecc78b5556d38cb61318 (patch)
treed3bd18907f54b1d115c628e79860221486e5cf2f
parent6a1efc32e635a86b0d848d058ba152403c49bec2 (diff)
Move LAN discovery from Messenger to friend_connection.
-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.c13
-rw-r--r--toxcore/friend_connection.h2
6 files changed, 23 insertions, 13 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 5182ce28..c13ca949 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -758,10 +758,20 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c)
758 temp->onion_c = onion_c; 758 temp->onion_c = onion_c;
759 759
760 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);
761 762
762 return temp; 763 return temp;
763} 764}
764 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
765/* main friend_connections loop. */ 775/* main friend_connections loop. */
766void do_friend_connections(Friend_Connections *fr_c) 776void do_friend_connections(Friend_Connections *fr_c)
767{ 777{
@@ -809,6 +819,8 @@ void do_friend_connections(Friend_Connections *fr_c)
809 } 819 }
810 } 820 }
811 } 821 }
822
823 LANdiscovery(fr_c);
812} 824}
813 825
814/* Free everything related with friend_connections. */ 826/* Free everything related with friend_connections. */
@@ -823,5 +835,6 @@ void kill_friend_connections(Friend_Connections *fr_c)
823 kill_friend_connection(fr_c, i); 835 kill_friend_connection(fr_c, i);
824 } 836 }
825 837
838 LANdiscovery_kill(fr_c->dht);
826 free(fr_c); 839 free(fr_c);
827} 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.