From 666e417b9991cd323aa9c6ed3f3bea926124dbe9 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Tue, 26 Dec 2017 11:53:52 +0100 Subject: Improve the portability of our ipv6 support Apparently, the in6_addr can have different fields in its union, making it a bit non-portable. We're solving this via macros. This should close #100, thanks to @fichtner for the report ♥ --- src/sp_network_utils.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/sp_network_utils.c b/src/sp_network_utils.c index 206ae5b..6fc0b1f 100644 --- a/src/sp_network_utils.c +++ b/src/sp_network_utils.c @@ -23,28 +23,30 @@ static inline bool cidr4_match(const struct in_addr addr, static inline bool cidr6_match(const struct in6_addr address, const struct in6_addr network, uint8_t bits) { - //#ifdef LINUX - const uint32_t *a = address.s6_addr32; - const uint32_t *n = network.s6_addr32; - /* -#else +#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) const uint32_t *a = address.__u6_addr.__u6_addr32; const uint32_t *n = network.__u6_addr.__u6_addr32; +#else + const uint32_t *a = address.s6_addr32; + const uint32_t *n = network.s6_addr32; #endif -*/ + int bits_whole = bits >> 5; // number of whole u32 int bits_incomplete = bits & 0x1F; // number of bits in incomplete u32 + if (bits_whole) { if (memcmp(a, n, bits_whole << 2)) { return false; } } + if (bits_incomplete) { uint32_t mask = htonl((0xFFFFFFFFu) << (32 - bits_incomplete)); if ((a[bits_whole] ^ n[bits_whole]) & mask) { return false; } } + return true; } -- cgit v1.3