]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
net_is_in_network(): Fixed to work with big endian machines.
authorTimo Sirainen <tss@iki.fi>
Thu, 15 May 2008 05:31:44 +0000 (08:31 +0300)
committerTimo Sirainen <tss@iki.fi>
Thu, 15 May 2008 05:31:44 +0000 (08:31 +0300)
--HG--
branch : HEAD

src/lib/network.c

index b1041451244f647be1da27380b5220cfa5e3f881..857f4d703cf462735a2318d511c6228e0677d2e7 100644 (file)
@@ -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;