Commit 30f0f0ed authored by Michael Tremer's avatar Michael Tremer

network: Move a couple of helper functions into headers

Signed-off-by: default avatarMichael Tremer <michael.tremer@ipfire.org>
parent f1fb21bd
......@@ -78,6 +78,44 @@ static inline void in6_addr_set_bit(struct in6_addr* address, unsigned int i, un
address->s6_addr[i / 8] ^= (-val ^ address->s6_addr[i / 8]) & (1 << (7 - (i % 8)));
}
static inline struct in6_addr loc_prefix_to_bitmask(const unsigned int prefix) {
struct in6_addr bitmask;
for (unsigned int i = 0; i < 16; i++)
bitmask.s6_addr[i] = 0;
for (int i = prefix, j = 0; i > 0; i -= 8, j++) {
if (i >= 8)
bitmask.s6_addr[j] = 0xff;
else
bitmask.s6_addr[j] = 0xff << (8 - i);
}
return bitmask;
}
static inline struct in6_addr loc_address_and(
const struct in6_addr* address, const struct in6_addr* bitmask) {
struct in6_addr a;
// Perform bitwise AND
for (unsigned int i = 0; i < 4; i++)
a.s6_addr32[i] = address->s6_addr32[i] & bitmask->s6_addr32[i];
return a;
}
static inline struct in6_addr loc_address_or(
const struct in6_addr* address, const struct in6_addr* bitmask) {
struct in6_addr a;
// Perform bitwise OR
for (unsigned int i = 0; i < 4; i++)
a.s6_addr32[i] = address->s6_addr32[i] | ~bitmask->s6_addr32[i];
return a;
}
static inline void hexdump(struct loc_ctx* ctx, const void* addr, size_t len) {
char buffer_hex[16 * 3 + 6];
char buffer_ascii[17];
......
......@@ -141,11 +141,11 @@ LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network
n->prefix = prefix;
// Convert the prefix into a bitmask
struct in6_addr bitmask = prefix_to_bitmask(n->prefix);
struct in6_addr bitmask = loc_prefix_to_bitmask(n->prefix);
// Store the first and last address in the network
n->first_address = make_first_address(address, &bitmask);
n->last_address = make_last_address(&n->first_address, &bitmask);
n->first_address = loc_address_and(address, &bitmask);
n->last_address = loc_address_or(&n->first_address, &bitmask);
// Set family
n->family = loc_address_family(&n->first_address);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment