From 530a554c3f7ce6eb18c750a43b9731e3129f5d29 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Thu, 15 May 2008 08:31:44 +0300 Subject: [PATCH] net_is_in_network(): Fixed to work with big endian machines. --HG-- branch : HEAD --- src/lib/network.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/network.c b/src/lib/network.c index b104145124..857f4d703c 100644 --- a/src/lib/network.c +++ b/src/lib/network.c @@ -708,7 +708,7 @@ bool net_is_in_network(const struct ip_addr *ip, const struct ip_addr *net_ip, unsigned int bits) { const uint32_t *ip1, *ip2; - uint32_t mask; + uint32_t mask, i1, i2; unsigned int pos, i; if (IPADDR_IS_V4(ip) != IPADDR_IS_V4(net_ip)) { @@ -735,17 +735,19 @@ bool net_is_in_network(const struct ip_addr *ip, if (ip1[i] != ip2[i]) return FALSE; } + i1 = htonl(ip1[i]); + i2 = htonl(ip2[i]); /* check the last full bytes */ - for (mask = 0xff; pos + 8 <= bits; pos += 8, mask <<= 8) { - if ((ip1[i] & mask) != (ip2[i] & mask)) + for (mask = 0xff000000; pos + 8 <= bits; pos += 8, mask >>= 8) { + if ((i1 & mask) != (i2 & mask)) return FALSE; } /* check the last bits, they're reversed in bytes */ bits -= pos; - for (mask = 0x80 << (pos % 32); bits > 0; bits--, mask >>= 1) { - if ((ip1[i] & mask) != (ip2[i] & mask)) + for (mask = 0x80000000 >> (pos % 32); bits > 0; bits--, mask >>= 1) { + if ((i1 & mask) != (i2 & mask)) return FALSE; } return TRUE; -- 2.47.3