Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid recursion in ip_is_lan and ip_is_local. #1139

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 54 additions & 49 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,71 +272,77 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast)
return ip;
}

static bool ip4_is_local(IP4 ip4)
{
/* Loopback. */
return ip4.uint8[0] == 127;
}

/* Is IP a local ip or not. */
bool ip_is_local(IP ip)
{
if (net_family_is_ipv4(ip.family)) {
IP4 ip4 = ip.ip.v4;
return ip4_is_local(ip.ip.v4);
}

/* Loopback. */
if (ip4.uint8[0] == 127) {
return 1;
}
} else {
/* embedded IPv4-in-IPv6 */
if (ipv6_ipv4_in_v6(ip.ip.v6)) {
IP ip4;
ip4.family = net_family_ipv4;
ip4.ip.v4.uint32 = ip.ip.v6.uint32[3];
return ip_is_local(ip4);
}
/* embedded IPv4-in-IPv6 */
if (ipv6_ipv4_in_v6(ip.ip.v6)) {
IP4 ip4;
ip4.uint32 = ip.ip.v6.uint32[3];
return ip4_is_local(ip4);
}

/* localhost in IPv6 (::1) */
if (ip.ip.v6.uint64[0] == 0 && ip.ip.v6.uint32[2] == 0 && ip.ip.v6.uint32[3] == net_htonl(1)) {
return 1;
}
/* localhost in IPv6 (::1) */
if (ip.ip.v6.uint64[0] == 0 && ip.ip.v6.uint32[2] == 0 && ip.ip.v6.uint32[3] == net_htonl(1)) {
return true;
}

return 0;
return false;
}

bool ip_is_lan(IP ip)
static bool ip4_is_lan(IP4 ip4)
{
if (ip_is_local(ip)) {
/* 10.0.0.0 to 10.255.255.255 range. */
if (ip4.uint8[0] == 10) {
return true;
}

if (net_family_is_ipv4(ip.family)) {
IP4 ip4 = ip.ip.v4;
/* 172.16.0.0 to 172.31.255.255 range. */
if (ip4.uint8[0] == 172 && ip4.uint8[1] >= 16 && ip4.uint8[1] <= 31) {
return true;
}

/* 10.0.0.0 to 10.255.255.255 range. */
if (ip4.uint8[0] == 10) {
return true;
}
/* 192.168.0.0 to 192.168.255.255 range. */
if (ip4.uint8[0] == 192 && ip4.uint8[1] == 168) {
return true;
}

/* 172.16.0.0 to 172.31.255.255 range. */
if (ip4.uint8[0] == 172 && ip4.uint8[1] >= 16 && ip4.uint8[1] <= 31) {
return true;
}
/* 169.254.1.0 to 169.254.254.255 range. */
if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0
&& ip4.uint8[2] != 255) {
return true;
}

/* 192.168.0.0 to 192.168.255.255 range. */
if (ip4.uint8[0] == 192 && ip4.uint8[1] == 168) {
return true;
}
/* RFC 6598: 100.64.0.0 to 100.127.255.255 (100.64.0.0/10)
* (shared address space to stack another layer of NAT) */
if ((ip4.uint8[0] == 100) && ((ip4.uint8[1] & 0xC0) == 0x40)) {
return true;
}

/* 169.254.1.0 to 169.254.254.255 range. */
if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0
&& ip4.uint8[2] != 255) {
return true;
}
return false;
}

/* RFC 6598: 100.64.0.0 to 100.127.255.255 (100.64.0.0/10)
* (shared address space to stack another layer of NAT) */
if ((ip4.uint8[0] == 100) && ((ip4.uint8[1] & 0xC0) == 0x40)) {
return true;
}
} else if (net_family_is_ipv6(ip.family)) {
bool ip_is_lan(IP ip)
{
if (ip_is_local(ip)) {
return true;
}

if (net_family_is_ipv4(ip.family)) {
return ip4_is_lan(ip.ip.v4);
}

if (net_family_is_ipv6(ip.family)) {
/* autogenerated for each interface: FE80::* (up to FEBF::*)
FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */
if (((ip.ip.v6.uint8[0] == 0xFF) && (ip.ip.v6.uint8[1] < 3) && (ip.ip.v6.uint8[15] == 1)) ||
Expand All @@ -346,10 +352,9 @@ bool ip_is_lan(IP ip)

/* embedded IPv4-in-IPv6 */
if (ipv6_ipv4_in_v6(ip.ip.v6)) {
IP ip4;
ip4.family = net_family_ipv4;
ip4.ip.v4.uint32 = ip.ip.v6.uint32[3];
return ip_is_lan(ip4);
IP4 ip4;
ip4.uint32 = ip.ip.v6.uint32[3];
return ip4_is_lan(ip4);
}
}

Expand Down