diff options
-rw-r--r-- | core/CMakeLists.txt | 1 | ||||
-rw-r--r-- | core/LAN_discovery.c | 78 | ||||
-rw-r--r-- | core/LAN_discovery.h | 28 | ||||
-rw-r--r-- | core/Messenger.c | 21 | ||||
-rw-r--r-- | core/Messenger.h | 1 | ||||
-rw-r--r-- | core/network.c | 2 |
6 files changed, 129 insertions, 2 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index ab4ff4bc..6ddd5b9b 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt | |||
@@ -11,6 +11,7 @@ set(core_sources | |||
11 | Lossless_UDP.c | 11 | Lossless_UDP.c |
12 | net_crypto.c | 12 | net_crypto.c |
13 | friend_requests.c | 13 | friend_requests.c |
14 | LAN_discovery.c | ||
14 | Messenger.c) | 15 | Messenger.c) |
15 | 16 | ||
16 | add_library(core ${core_sources}) | 17 | add_library(core ${core_sources}) |
diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c new file mode 100644 index 00000000..a19e4126 --- /dev/null +++ b/core/LAN_discovery.c | |||
@@ -0,0 +1,78 @@ | |||
1 | /* LAN_discovery.c | ||
2 | * | ||
3 | * LAN discovery implementation. | ||
4 | * | ||
5 | */ | ||
6 | |||
7 | #include "routing.h" | ||
8 | |||
9 | |||
10 | /*Return the broadcast ip | ||
11 | TODO: make it return the real one, not the 255.255.255.255 one.*/ | ||
12 | IP broadcast_ip() | ||
13 | { | ||
14 | IP ip; | ||
15 | ip.i = ~0; | ||
16 | return ip; | ||
17 | } | ||
18 | |||
19 | /*return 0 if ip is a LAN ip | ||
20 | return -1 if it is not */ | ||
21 | int LAN_ip(IP ip) | ||
22 | { | ||
23 | if(ip.c[0] == 127)/* Loopback */ | ||
24 | { | ||
25 | return 0; | ||
26 | } | ||
27 | if(ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */ | ||
28 | { | ||
29 | return 0; | ||
30 | } | ||
31 | if(ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */ | ||
32 | { | ||
33 | return 0; | ||
34 | } | ||
35 | if(ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */ | ||
36 | { | ||
37 | return 0; | ||
38 | } | ||
39 | if(ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */ | ||
40 | { | ||
41 | return 0; | ||
42 | } | ||
43 | return -1; | ||
44 | } | ||
45 | |||
46 | int handle_LANdiscovery(uint8_t * packet, uint32_t length, IP_Port source) | ||
47 | { | ||
48 | if(LAN_ip(source.ip) == -1) | ||
49 | { | ||
50 | return 1; | ||
51 | } | ||
52 | if(length != crypto_box_PUBLICKEYBYTES + 1) | ||
53 | { | ||
54 | return 1; | ||
55 | } | ||
56 | DHT_bootstrap(source, packet + 1); | ||
57 | return 0; | ||
58 | } | ||
59 | |||
60 | |||
61 | int send_LANdiscovery(uint16_t port) | ||
62 | { | ||
63 | uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; | ||
64 | data[0] = 32; | ||
65 | memcpy(data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); | ||
66 | IP_Port ip_port = {broadcast_ip(), port}; | ||
67 | return sendpacket(ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); | ||
68 | } | ||
69 | |||
70 | |||
71 | int LANdiscovery_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | ||
72 | { | ||
73 | if(packet[0] == 32) | ||
74 | { | ||
75 | return handle_LANdiscovery(packet, length, source); | ||
76 | } | ||
77 | return 1; | ||
78 | } | ||
diff --git a/core/LAN_discovery.h b/core/LAN_discovery.h new file mode 100644 index 00000000..7448abac --- /dev/null +++ b/core/LAN_discovery.h | |||
@@ -0,0 +1,28 @@ | |||
1 | /* LAN_discovery.h | ||
2 | * | ||
3 | * LAN discovery implementation. | ||
4 | * | ||
5 | */ | ||
6 | |||
7 | |||
8 | #ifndef LAN_DISCOVERY_H | ||
9 | #define LAN_DISCOVERY_H | ||
10 | |||
11 | |||
12 | #include "DHT.h" | ||
13 | |||
14 | |||
15 | /*Send a LAN discovery pcaket to the broadcast address with port port*/ | ||
16 | int send_LANdiscovery(uint16_t port); | ||
17 | |||
18 | |||
19 | /* if we receive a packet we call this function so it can be handled. | ||
20 | return 0 if packet is handled correctly. | ||
21 | return 1 if it didn't handle the packet or if the packet was shit. */ | ||
22 | int LANdiscovery_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | ||
23 | |||
24 | |||
25 | |||
26 | |||
27 | |||
28 | #endif | ||
diff --git a/core/Messenger.c b/core/Messenger.c index d58e895c..faa3cefc 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -530,6 +530,22 @@ static void doInbound() | |||
530 | } | 530 | } |
531 | } | 531 | } |
532 | 532 | ||
533 | /*Interval in seconds between LAN discovery packet sending*/ | ||
534 | #define LAN_DISCOVERY_INTERVAL 60 | ||
535 | |||
536 | static uint32_t last_LANdiscovery; | ||
537 | |||
538 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ | ||
539 | static void LANdiscovery() | ||
540 | { | ||
541 | if(last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) | ||
542 | { | ||
543 | send_LANdiscovery(htons(PORT)); | ||
544 | last_LANdiscovery = unix_time(); | ||
545 | } | ||
546 | } | ||
547 | |||
548 | |||
533 | /* the main loop that needs to be run at least 200 times per second. */ | 549 | /* the main loop that needs to be run at least 200 times per second. */ |
534 | void doMessenger() | 550 | void doMessenger() |
535 | { | 551 | { |
@@ -541,7 +557,8 @@ void doMessenger() | |||
541 | #ifdef DEBUG | 557 | #ifdef DEBUG |
542 | /* if(rand() % 3 != 1) //simulate packet loss */ | 558 | /* if(rand() % 3 != 1) //simulate packet loss */ |
543 | /* { */ | 559 | /* { */ |
544 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) | 560 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && |
561 | friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) | ||
545 | { | 562 | { |
546 | /* if packet is discarded */ | 563 | /* if packet is discarded */ |
547 | printf("Received unhandled packet with length: %u\n", length); | 564 | printf("Received unhandled packet with length: %u\n", length); |
@@ -556,6 +573,7 @@ void doMessenger() | |||
556 | DHT_handlepacket(data, length, ip_port); | 573 | DHT_handlepacket(data, length, ip_port); |
557 | LosslessUDP_handlepacket(data, length, ip_port); | 574 | LosslessUDP_handlepacket(data, length, ip_port); |
558 | friendreq_handlepacket(data, length, ip_port); | 575 | friendreq_handlepacket(data, length, ip_port); |
576 | LANdiscovery_handlepacket(data, length, ip_port); | ||
559 | #endif | 577 | #endif |
560 | 578 | ||
561 | } | 579 | } |
@@ -564,6 +582,7 @@ void doMessenger() | |||
564 | doNetCrypto(); | 582 | doNetCrypto(); |
565 | doInbound(); | 583 | doInbound(); |
566 | doFriends(); | 584 | doFriends(); |
585 | LANdiscovery(); | ||
567 | } | 586 | } |
568 | 587 | ||
569 | /* returns the size of the messenger data (for saving) */ | 588 | /* returns the size of the messenger data (for saving) */ |
diff --git a/core/Messenger.h b/core/Messenger.h index c0432e68..1067d156 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -29,6 +29,7 @@ | |||
29 | #include "net_crypto.h" | 29 | #include "net_crypto.h" |
30 | #include "DHT.h" | 30 | #include "DHT.h" |
31 | #include "friend_requests.h" | 31 | #include "friend_requests.h" |
32 | #include "LAN_discovery.h" | ||
32 | 33 | ||
33 | #ifdef __cplusplus | 34 | #ifdef __cplusplus |
34 | extern "C" { | 35 | extern "C" { |
diff --git a/core/network.c b/core/network.c index 5aa0d304..6a4d0bce 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -144,7 +144,7 @@ int init_networking(IP ip, uint16_t port) | |||
144 | /*Enable broadcast on socket*/ | 144 | /*Enable broadcast on socket*/ |
145 | int broadcast = 1; | 145 | int broadcast = 1; |
146 | setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)); | 146 | setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)); |
147 | 147 | ||
148 | /* Set socket nonblocking */ | 148 | /* Set socket nonblocking */ |
149 | #ifdef WIN32 | 149 | #ifdef WIN32 |
150 | /* I think this works for windows */ | 150 | /* I think this works for windows */ |