diff options
| author | jvoisin | 2017-12-26 11:53:52 +0100 |
|---|---|---|
| committer | jvoisin | 2017-12-26 11:53:52 +0100 |
| commit | 666e417b9991cd323aa9c6ed3f3bea926124dbe9 (patch) | |
| tree | 7560900390bc5bd79e4b5830e09ae59048aad4ef /src | |
| parent | 0ba1b4fe0d9bb89d08931c64f203f16e826c6104 (diff) | |
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 ♥
Diffstat (limited to 'src')
| -rw-r--r-- | src/sp_network_utils.c | 14 |
1 files 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, | |||
| 23 | 23 | ||
| 24 | static inline bool cidr6_match(const struct in6_addr address, | 24 | static inline bool cidr6_match(const struct in6_addr address, |
| 25 | const struct in6_addr network, uint8_t bits) { | 25 | const struct in6_addr network, uint8_t bits) { |
| 26 | //#ifdef LINUX | 26 | #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) |
| 27 | const uint32_t *a = address.s6_addr32; | ||
| 28 | const uint32_t *n = network.s6_addr32; | ||
| 29 | /* | ||
| 30 | #else | ||
| 31 | const uint32_t *a = address.__u6_addr.__u6_addr32; | 27 | const uint32_t *a = address.__u6_addr.__u6_addr32; |
| 32 | const uint32_t *n = network.__u6_addr.__u6_addr32; | 28 | const uint32_t *n = network.__u6_addr.__u6_addr32; |
| 29 | #else | ||
| 30 | const uint32_t *a = address.s6_addr32; | ||
| 31 | const uint32_t *n = network.s6_addr32; | ||
| 33 | #endif | 32 | #endif |
| 34 | */ | 33 | |
| 35 | int bits_whole = bits >> 5; // number of whole u32 | 34 | int bits_whole = bits >> 5; // number of whole u32 |
| 36 | int bits_incomplete = bits & 0x1F; // number of bits in incomplete u32 | 35 | int bits_incomplete = bits & 0x1F; // number of bits in incomplete u32 |
| 36 | |||
| 37 | if (bits_whole) { | 37 | if (bits_whole) { |
| 38 | if (memcmp(a, n, bits_whole << 2)) { | 38 | if (memcmp(a, n, bits_whole << 2)) { |
| 39 | return false; | 39 | return false; |
| 40 | } | 40 | } |
| 41 | } | 41 | } |
| 42 | |||
| 42 | if (bits_incomplete) { | 43 | if (bits_incomplete) { |
| 43 | uint32_t mask = htonl((0xFFFFFFFFu) << (32 - bits_incomplete)); | 44 | uint32_t mask = htonl((0xFFFFFFFFu) << (32 - bits_incomplete)); |
| 44 | if ((a[bits_whole] ^ n[bits_whole]) & mask) { | 45 | if ((a[bits_whole] ^ n[bits_whole]) & mask) { |
| 45 | return false; | 46 | return false; |
| 46 | } | 47 | } |
| 47 | } | 48 | } |
| 49 | |||
| 48 | return true; | 50 | return true; |
| 49 | } | 51 | } |
| 50 | 52 | ||
