diff options
Diffstat (limited to 'core/LAN_discovery.c')
-rw-r--r-- | core/LAN_discovery.c | 78 |
1 files changed, 78 insertions, 0 deletions
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 | } | ||