diff options
Diffstat (limited to 'toxcore/LAN_discovery.c')
-rw-r--r-- | toxcore/LAN_discovery.c | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index dbce762e..5ab5b0e1 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c | |||
@@ -227,18 +227,43 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) | |||
227 | return ip; | 227 | return ip; |
228 | } | 228 | } |
229 | 229 | ||
230 | /* Is IP a local ip or not. */ | ||
231 | _Bool Local_ip(IP ip) | ||
232 | { | ||
233 | if (ip.family == AF_INET) { | ||
234 | IP4 ip4 = ip.ip4; | ||
235 | |||
236 | /* Loopback. */ | ||
237 | if (ip4.uint8[0] == 127) | ||
238 | return 1; | ||
239 | } else { | ||
240 | /* embedded IPv4-in-IPv6 */ | ||
241 | if (IPV6_IPV4_IN_V6(ip.ip6)) { | ||
242 | IP ip4; | ||
243 | ip4.family = AF_INET; | ||
244 | ip4.ip4.uint32 = ip.ip6.uint32[3]; | ||
245 | return Local_ip(ip4); | ||
246 | } | ||
247 | |||
248 | /* localhost in IPv6 (::1) */ | ||
249 | if (ip.ip6.uint64[0] == 0 && ip.ip6.uint32[2] == 0 && ip.ip6.uint32[3] == htonl(1)) | ||
250 | return 1; | ||
251 | } | ||
252 | |||
253 | return 0; | ||
254 | } | ||
255 | |||
230 | /* return 0 if ip is a LAN ip. | 256 | /* return 0 if ip is a LAN ip. |
231 | * return -1 if it is not. | 257 | * return -1 if it is not. |
232 | */ | 258 | */ |
233 | int LAN_ip(IP ip) | 259 | int LAN_ip(IP ip) |
234 | { | 260 | { |
261 | if (Local_ip(ip)) | ||
262 | return 0; | ||
263 | |||
235 | if (ip.family == AF_INET) { | 264 | if (ip.family == AF_INET) { |
236 | IP4 ip4 = ip.ip4; | 265 | IP4 ip4 = ip.ip4; |
237 | 266 | ||
238 | /* Loopback. */ | ||
239 | if (ip4.uint8[0] == 127) | ||
240 | return 0; | ||
241 | |||
242 | /* 10.0.0.0 to 10.255.255.255 range. */ | 267 | /* 10.0.0.0 to 10.255.255.255 range. */ |
243 | if (ip4.uint8[0] == 10) | 268 | if (ip4.uint8[0] == 10) |
244 | return 0; | 269 | return 0; |
@@ -276,10 +301,6 @@ int LAN_ip(IP ip) | |||
276 | ip4.ip4.uint32 = ip.ip6.uint32[3]; | 301 | ip4.ip4.uint32 = ip.ip6.uint32[3]; |
277 | return LAN_ip(ip4); | 302 | return LAN_ip(ip4); |
278 | } | 303 | } |
279 | |||
280 | /* localhost in IPv6 (::1) */ | ||
281 | if (ip.ip6.uint64[0] == 0 && ip.ip6.uint32[2] == 0 && ip.ip6.uint32[3] == htonl(1)) | ||
282 | return 0; | ||
283 | } | 304 | } |
284 | 305 | ||
285 | return -1; | 306 | return -1; |