Skip to content
Snippets Groups Projects
Commit deffff6d authored by Guillaume Roguez's avatar Guillaume Roguez Committed by Adrien Béraud
Browse files

IpAddr: isLoopback() and isPrivate()

These methods are bugged with IPv4 address on little endian CPU.
Fix it by taking care of the endianness.

Change-Id: If4f0eb1dafc2c4dc6cde7d94ced7a8fec552cd80
parent 56a78bfb
No related branches found
No related tags found
No related merge requests found
...@@ -333,7 +333,8 @@ IpAddr::isLoopback() const ...@@ -333,7 +333,8 @@ IpAddr::isLoopback() const
{ {
switch (addr.addr.sa_family) { switch (addr.addr.sa_family) {
case AF_INET: { case AF_INET: {
uint8_t b1 = (uint8_t)(addr.ipv4.sin_addr.s_addr >> 24); auto addr_host = ntohl(addr.ipv4.sin_addr.s_addr);
uint8_t b1 = (uint8_t)(addr_host >> 24);
return b1 == 127; return b1 == 127;
} }
case AF_INET6: case AF_INET6:
...@@ -350,10 +351,11 @@ IpAddr::isPrivate() const ...@@ -350,10 +351,11 @@ IpAddr::isPrivate() const
return true; return true;
} }
switch (addr.addr.sa_family) { switch (addr.addr.sa_family) {
case AF_INET: case AF_INET: {
auto addr_host = ntohl(addr.ipv4.sin_addr.s_addr);
uint8_t b1, b2; uint8_t b1, b2;
b1 = (uint8_t)(addr.ipv4.sin_addr.s_addr >> 24); b1 = (uint8_t)(addr_host >> 24);
b2 = (uint8_t)((addr.ipv4.sin_addr.s_addr >> 16) & 0x0ff); b2 = (uint8_t)((addr_host >> 16) & 0x0ff);
// 10.x.y.z // 10.x.y.z
if (b1 == 10) if (b1 == 10)
return true; return true;
...@@ -364,6 +366,7 @@ IpAddr::isPrivate() const ...@@ -364,6 +366,7 @@ IpAddr::isPrivate() const
if ((b1 == 192) && (b2 == 168)) if ((b1 == 192) && (b2 == 168))
return true; return true;
return false; return false;
}
case AF_INET6: { case AF_INET6: {
const pj_uint8_t* addr6 = reinterpret_cast<const pj_uint8_t*>(&addr.ipv6.sin6_addr); const pj_uint8_t* addr6 = reinterpret_cast<const pj_uint8_t*>(&addr.ipv6.sin6_addr);
if (addr6[0] == 0xfc) if (addr6[0] == 0xfc)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment