From 2e8f418d2cf9065692563c7c9c3321e3c4838915 Mon Sep 17 00:00:00 2001 From: david Date: Tue, 17 Apr 2012 04:36:08 +0000 Subject: [PATCH] Remove byte order dependency in in_addr_to_octets. Because in_addr is stored in network byte order, this function could extract the octets in MSB-to-LSB order or in LSB-to-MSB order. This didn't matter in the case of resolved names, because the same order was used when generating the octet array and later when matching against it. But the function parse_ipv4_ranges, which handles literal IPv4 addresses, always uses octet[0] as the MSB, so comparisons failed in later matching. As it was, the code worked on little-endian architectures but didn't on big-endian. --- nbase/nbase_addrset.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/nbase/nbase_addrset.c b/nbase/nbase_addrset.c index 5efd265e1..6b462b7e5 100644 --- a/nbase/nbase_addrset.c +++ b/nbase/nbase_addrset.c @@ -203,13 +203,16 @@ static char *address_to_string(const struct sockaddr *sa, size_t sa_len, return buf; } -/* Break an IPv4 address into an array of octets. */ +/* Break an IPv4 address into an array of octets. octets[0] contains the most + significant octet and octets[3] the least significant. */ static void in_addr_to_octets(const struct in_addr *ia, uint8_t octets[4]) { - octets[0] = (uint8_t) (ia->s_addr & 0xFF); - octets[1] = (uint8_t) ((ia->s_addr & (0xFF << 8)) >> 8); - octets[2] = (uint8_t) ((ia->s_addr & (0xFF << 16)) >> 16); - octets[3] = (uint8_t) ((ia->s_addr & (0xFF << 24)) >> 24); + u32 hbo = ntohl(ia->s_addr); + + octets[0] = (uint8_t) ((hbo & (0xFF << 24)) >> 24); + octets[1] = (uint8_t) ((hbo & (0xFF << 16)) >> 16); + octets[2] = (uint8_t) ((hbo & (0xFF << 8)) >> 8); + octets[3] = (uint8_t) (hbo & 0xFF); } #define BITVECTOR_BITS (sizeof(bitvector_t) * CHAR_BIT)