Skip to content

Commit

Permalink
Exclude the high broadcast address
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed Sep 3, 2015
1 parent b315665 commit 6ca59f9
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/modules/rlm_redis_ippool/rlm_redis_ippool_tool.c
Expand Up @@ -223,11 +223,12 @@ static uint128_t uint128_gen_mask(uint8_t bits)
*
* @param[in,out] ipaddr to increment.
* @param[in] prefix Length of the prefix.
* @param[in] ex_broadcast whether we should include the upper broadcast address.
* @return
* - true if the prefix bits are not high (continue).
* - false if the prefix bits are high (stop).
*/
static bool ipaddr_next(fr_ipaddr_t *ipaddr, uint8_t prefix)
static bool ipaddr_next(fr_ipaddr_t *ipaddr, uint8_t prefix, bool ex_broadcast)
{
/*
* Single IP addresses
Expand All @@ -236,6 +237,8 @@ static bool ipaddr_next(fr_ipaddr_t *ipaddr, uint8_t prefix)

rad_assert(prefix > ipaddr->prefix);

if (ex_broadcast && (ipaddr->prefix >= (IPADDR_LEN(ipaddr->af) - 1))) return false;

switch (ipaddr->af) {
default:
case AF_UNSPEC:
Expand All @@ -244,7 +247,7 @@ static bool ipaddr_next(fr_ipaddr_t *ipaddr, uint8_t prefix)

case AF_INET6:
{
uint128_t ip, p_mask, p_curr;
uint128_t ip, p_mask;

rad_assert((prefix > 0) && (prefix <= 128));

Expand All @@ -255,35 +258,47 @@ static bool ipaddr_next(fr_ipaddr_t *ipaddr, uint8_t prefix)

/* Generate a mask that covers the prefix bits */
p_mask = uint128_gen_mask(prefix - ipaddr->prefix) << (128 - prefix);
p_curr = uint128_band(ip, p_mask);

/* Stopping condition - all prefix bits high (and busy attempting to locate maltesers) */
if (uint128_eq(p_mask, p_curr)) return false;
if (ex_broadcast) {
/* Increment the prefix */
ip = uint128_add(ip, uint128_lshift((uint128_t)1, (128 - prefix)));
/* Stopping condition - all prefix bits high */
if (uint128_eq(uint128_band(ip, p_mask), p_mask)) return false;
} else {
/* Stopping condition - all prefix bits high */
if (uint128_eq(uint128_band(ip, p_mask), p_mask)) return false;
/* Increment the prefix */
ip = uint128_add(ip, uint128_lshift((uint128_t)1, (128 - prefix)));
}

/* Increment the prefix */
ip = uint128_add(ip, uint128_lshift((uint128_t)1, (128 - prefix)));
ip = htonlll(ip);
memcpy(&ipaddr->ipaddr.ip6addr.s6_addr, &ip, sizeof(ipaddr->ipaddr.ip6addr.s6_addr));
return true;
}

case AF_INET:
{
uint32_t ip, p_mask, p_curr;
uint32_t ip, p_mask;

rad_assert((prefix > 0) && (prefix <= 32));

ip = ntohl(ipaddr->ipaddr.ip4addr.s_addr);

/* Generate a mask that covers the prefix bits */
p_mask = uint32_gen_mask(prefix - ipaddr->prefix) << (32 - prefix);
p_curr = ip & p_mask;

/* Stopping condition (all prefix bits high) */
if (p_curr == p_mask) return false;
if (ex_broadcast) {
/* Increment the prefix */
ip += 1 << (32 - prefix);
/* Stopping condition (all prefix bits high) */
if ((ip & p_mask) == p_mask) return false;
} else {
/* Stopping condition (all prefix bits high) */
if ((ip & p_mask) == p_mask) return false;
/* Increment the prefix */
ip += 1 << (32 - prefix);
}

/* Increment the prefix */
ip += 1 << (32 - prefix);
ipaddr->ipaddr.ip4addr.s_addr = htonl(ip);
return true;
}
Expand Down Expand Up @@ -333,10 +348,7 @@ static int driver_do_lease(void *out, void *instance,
*/
if (s_ret == REDIS_RCODE_TRY_AGAIN) ipaddr = acked;

/*
* Iterate over all possible prefixes (or IP addresses)
*/
for (i = 0; (i < MAX_PIPELINED) && more; i++, more = ipaddr_next(&ipaddr, prefix)) {
for (i = 0; (i < MAX_PIPELINED) && more; i++, more = ipaddr_next(&ipaddr, prefix, true)) {
int enqueued;

enqueued = enqueue(inst, conn, key_prefix, key_prefix_len,
Expand Down Expand Up @@ -367,7 +379,7 @@ static int driver_do_lease(void *out, void *instance,

ret = process(out, &to_process, replies[i]);
if (ret < 0) continue;
ipaddr_next(&to_process, prefix);
ipaddr_next(&to_process, prefix, true);
}
}
fr_redis_pipeline_free(replies, reply_cnt);
Expand Down

0 comments on commit 6ca59f9

Please sign in to comment.