diff options
Diffstat (limited to 'core/LAN_discovery.c')
-rw-r--r-- | core/LAN_discovery.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c new file mode 100644 index 00000000..3cfcb067 --- /dev/null +++ b/core/LAN_discovery.c | |||
@@ -0,0 +1,79 @@ | |||
1 | /* LAN_discovery.c | ||
2 | * | ||
3 | * LAN discovery implementation. | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include "LAN_discovery.h" | ||
25 | |||
26 | |||
27 | /*Return the broadcast ip | ||
28 | TODO: make it return the real one, not the 255.255.255.255 one.*/ | ||
29 | IP broadcast_ip() | ||
30 | { | ||
31 | IP ip; | ||
32 | ip.i = ~0; | ||
33 | return ip; | ||
34 | } | ||
35 | |||
36 | /*return 0 if ip is a LAN ip | ||
37 | return -1 if it is not */ | ||
38 | int LAN_ip(IP ip) | ||
39 | { | ||
40 | if(ip.c[0] == 127)/* Loopback */ | ||
41 | return 0; | ||
42 | if(ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */ | ||
43 | return 0; | ||
44 | if(ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */ | ||
45 | return 0; | ||
46 | if(ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */ | ||
47 | return 0; | ||
48 | 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 */ | ||
49 | return 0; | ||
50 | return -1; | ||
51 | } | ||
52 | |||
53 | int handle_LANdiscovery(uint8_t * packet, uint32_t length, IP_Port source) | ||
54 | { | ||
55 | if(LAN_ip(source.ip) == -1) | ||
56 | return 1; | ||
57 | if(length != crypto_box_PUBLICKEYBYTES + 1) | ||
58 | return 1; | ||
59 | DHT_bootstrap(source, packet + 1); | ||
60 | return 0; | ||
61 | } | ||
62 | |||
63 | |||
64 | int send_LANdiscovery(uint16_t port) | ||
65 | { | ||
66 | uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; | ||
67 | data[0] = 32; | ||
68 | memcpy(data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); | ||
69 | IP_Port ip_port = {broadcast_ip(), port}; | ||
70 | return sendpacket(ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); | ||
71 | } | ||
72 | |||
73 | |||
74 | int LANdiscovery_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | ||
75 | { | ||
76 | if(packet[0] == 32) | ||
77 | return handle_LANdiscovery(packet, length, source); | ||
78 | return 1; | ||
79 | } | ||