aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Gschwantner <tharre3@gmail.com>2019-09-28 19:53:53 +0200
committerThomas Gschwantner <tharre3@gmail.com>2019-09-28 19:53:53 +0200
commitb7911cdfee83b16a4f7fa426a2387a819b43c7e9 (patch)
tree4d8b313e56e77ee8db270904017fdac32fcc714a
parentFix random_bounded() to always be in [0, bound) (diff)
downloadwg-dynamic-b7911cdfee83b16a4f7fa426a2387a819b43c7e9.tar.xz
wg-dynamic-b7911cdfee83b16a4f7fa426a2387a819b43c7e9.zip
Rename struct ip_pool to be more descriptive
-rw-r--r--lease.c44
-rw-r--r--radix-trie.c120
-rw-r--r--radix-trie.h37
3 files changed, 101 insertions, 100 deletions
diff --git a/lease.c b/lease.c
index a54298d..9613475 100644
--- a/lease.c
+++ b/lease.c
@@ -25,7 +25,7 @@
#define TIME_T_MAX (((time_t)1 << (sizeof(time_t) * CHAR_BIT - 2)) - 1) * 2 + 1
-static struct ip_pool pool;
+static struct ipns ipns;
static time_t gexpires = TIME_T_MAX;
static bool synchronized;
@@ -60,7 +60,7 @@ void leases_init(char *fname, struct mnl_socket *nlsock)
synchronized = false;
leases_ht = kh_init(leaseht);
- ipp_init(&pool);
+ ipp_init(&ipns);
nlh = mnl_nlmsg_put_header(buf);
nlh->nlmsg_type = RTM_GETROUTE;
@@ -87,7 +87,7 @@ void leases_free()
}
kh_destroy(leaseht, leases_ht);
- ipp_free(&pool);
+ ipp_free(&ipns);
}
struct wg_dynamic_lease *new_lease(wg_key pubkey, uint32_t leasetime,
@@ -106,21 +106,21 @@ struct wg_dynamic_lease *new_lease(wg_key pubkey, uint32_t leasetime,
if (!lease)
fatal("malloc()");
- if (wants_ipv4 && !pool.total_ipv4)
+ if (wants_ipv4 && !ipns.total_ipv4)
return NULL; /* no ipv4 addresses available */
- if (wants_ipv6 && !pool.totalh_ipv6 && !pool.totall_ipv6)
+ if (wants_ipv6 && !ipns.totalh_ipv6 && !ipns.totall_ipv6)
return NULL; /* no ipv6 addresses available */
if (wants_ipv4) {
if (!ipv4) {
- index = random_bounded(pool.total_ipv4);
+ index = random_bounded(ipns.total_ipv4);
debug("new_lease(v4): %u of %u\n", index,
- pool.total_ipv4);
+ ipns.total_ipv4);
- ipp_addnth_v4(&pool, &lease->ipv4, index);
+ ipp_addnth_v4(&ipns, &lease->ipv4, index);
} else {
- if (ipp_add_v4(&pool, ipv4, 32))
+ if (ipp_add_v4(&ipns, ipv4, 32))
return NULL;
memcpy(&lease->ipv4, ipv4, sizeof *ipv4);
@@ -129,21 +129,21 @@ struct wg_dynamic_lease *new_lease(wg_key pubkey, uint32_t leasetime,
if (wants_ipv6) {
if (!ipv6) {
- if (pool.totalh_ipv6 > 0) {
+ if (ipns.totalh_ipv6 > 0) {
index_l = random_u64();
- index_h = random_bounded(pool.totalh_ipv6);
+ index_h = random_bounded(ipns.totalh_ipv6);
} else {
- index_l = random_bounded(pool.totall_ipv6);
+ index_l = random_bounded(ipns.totall_ipv6);
index_h = 0;
}
debug("new_lease(v6): %u:%ju of %u:%ju\n", index_h,
- index_l, pool.totalh_ipv6, pool.totall_ipv6);
- ipp_addnth_v6(&pool, &lease->ipv6, index_l, index_h);
+ index_l, ipns.totalh_ipv6, ipns.totall_ipv6);
+ ipp_addnth_v6(&ipns, &lease->ipv6, index_l, index_h);
} else {
- if (ipp_add_v6(&pool, ipv6, 128)) {
+ if (ipp_add_v6(&ipns, ipv6, 128)) {
if (!ipv4 || ipv4->s_addr)
- ipp_del_v4(&pool, ipv4, 32);
+ ipp_del_v4(&ipns, ipv4, 32);
return NULL;
}
@@ -223,10 +223,10 @@ int leases_refresh()
time_t expires = (*pp)->start_mono + (*pp)->leasetime;
if (cur_time >= expires) {
if (ipv4->s_addr)
- ipp_del_v4(&pool, ipv4, 32);
+ ipp_del_v4(&ipns, ipv4, 32);
if (!IN6_IS_ADDR_UNSPECIFIED(ipv6))
- ipp_del_v6(&pool, ipv6, 128);
+ ipp_del_v6(&ipns, ipv6, 128);
tmp = *pp;
*pp = (*pp)->next;
@@ -315,18 +315,18 @@ static int process_nlpacket_cb(const struct nlmsghdr *nlh, void *data)
if (nlh->nlmsg_type == RTM_NEWROUTE) {
if (rm->rtm_family == AF_INET) {
- if (ipp_addpool_v4(&pool, addr, rm->rtm_dst_len))
+ if (ipp_addpool_v4(&ipns, addr, rm->rtm_dst_len))
die("ipp_addpool_v4()\n");
} else if (rm->rtm_family == AF_INET6) {
- if (ipp_addpool_v6(&pool, addr, rm->rtm_dst_len))
+ if (ipp_addpool_v6(&ipns, addr, rm->rtm_dst_len))
die("ipp_addpool_v6()\n");
}
} else if (nlh->nlmsg_type == RTM_DELROUTE) {
if (rm->rtm_family == AF_INET) {
- if (ipp_removepool_v4(&pool, addr) && synchronized)
+ if (ipp_removepool_v4(&ipns, addr) && synchronized)
die("ipp_removepool_v4()\n");
} else if (rm->rtm_family == AF_INET6) {
- if (ipp_removepool_v6(&pool, addr) && synchronized)
+ if (ipp_removepool_v6(&ipns, addr) && synchronized)
die("ipp_removepool_v6()\n");
}
}
diff --git a/radix-trie.c b/radix-trie.c
index 25bdd75..f8ff629 100644
--- a/radix-trie.c
+++ b/radix-trie.c
@@ -377,35 +377,35 @@ static int remove_node(struct radix_node *trie, const uint8_t *key,
return 0;
}
-static void totalip_inc(struct ip_pool *ipp, uint8_t bits, uint8_t val)
+static void totalip_inc(struct ipns *ns, uint8_t bits, uint8_t val)
{
if (bits == 32) {
BUG_ON(val >= 32);
- ipp->total_ipv4 += 1ULL << val;
+ ns->total_ipv4 += 1ULL << val;
} else if (bits == 128) {
- uint64_t tmp = ipp->totall_ipv6;
+ uint64_t tmp = ns->totall_ipv6;
BUG_ON(val > 64);
- ipp->totall_ipv6 += (val == 64) ? 0 : 1ULL << val;
- if (ipp->totall_ipv6 <= tmp)
- ++ipp->totalh_ipv6;
+ ns->totall_ipv6 += (val == 64) ? 0 : 1ULL << val;
+ if (ns->totall_ipv6 <= tmp)
+ ++ns->totalh_ipv6;
}
}
-static void totalip_dec(struct ip_pool *ipp, uint8_t bits, uint8_t val)
+static void totalip_dec(struct ipns *ns, uint8_t bits, uint8_t val)
{
if (bits == 32) {
BUG_ON(val >= 32);
- ipp->total_ipv4 -= 1ULL << val;
+ ns->total_ipv4 -= 1ULL << val;
} else if (bits == 128) {
- uint64_t tmp = ipp->totall_ipv6;
+ uint64_t tmp = ns->totall_ipv6;
BUG_ON(val > 64);
- ipp->totall_ipv6 -= (val == 64) ? 0 : 1ULL << val;
- if (ipp->totall_ipv6 >= tmp)
- --ipp->totalh_ipv6;
+ ns->totall_ipv6 -= (val == 64) ? 0 : 1ULL << val;
+ if (ns->totall_ipv6 >= tmp)
+ --ns->totalh_ipv6;
}
}
-static int ipp_addpool(struct ip_pool *ipp, struct radix_pool **pool,
+static int ipp_addpool(struct ipns *ns, struct radix_pool **pool,
struct radix_node **root, uint8_t bits,
const uint8_t *key, uint8_t cidr)
{
@@ -421,7 +421,7 @@ static int ipp_addpool(struct ip_pool *ipp, struct radix_pool **pool,
shadowed = true;
} else if (cidr < node->cidr && !(*pool)->shadowed) {
(*pool)->shadowed = true;
- totalip_dec(ipp, bits, bits - cidr);
+ totalip_dec(ns, bits, bits - cidr);
} else {
return -1;
}
@@ -439,7 +439,7 @@ static int ipp_addpool(struct ip_pool *ipp, struct radix_pool **pool,
}
if (!shadowed)
- totalip_inc(ipp, bits, bits - cidr);
+ totalip_inc(ns, bits, bits - cidr);
newpool = malloc(sizeof *newpool);
if (!newpool)
@@ -505,94 +505,94 @@ static void debug_print_trie(struct radix_node *root, uint8_t bits)
debug_print_trie(root->bit[1], bits);
}
-void debug_print_trie_v4(struct ip_pool *pool)
+void debug_print_trie_v4(struct ipns *ns)
{
- debug_print_trie(pool->ip4_root, 32);
+ debug_print_trie(ns->ip4_root, 32);
}
-void debug_print_trie_v6(struct ip_pool *pool)
+void debug_print_trie_v6(struct ipns *ns)
{
- debug_print_trie(pool->ip6_root, 128);
+ debug_print_trie(ns->ip6_root, 128);
}
#endif
-void ipp_init(struct ip_pool *pool)
+void ipp_init(struct ipns *ns)
{
- pool->ip4_root = pool->ip6_root = NULL;
- pool->ip4_pool = pool->ip6_pool = NULL;
- pool->totall_ipv6 = pool->totalh_ipv6 = pool->total_ipv4 = 0;
+ ns->ip4_root = ns->ip6_root = NULL;
+ ns->ip4_pools = ns->ip6_pools = NULL;
+ ns->totall_ipv6 = ns->totalh_ipv6 = ns->total_ipv4 = 0;
}
-void ipp_free(struct ip_pool *pool)
+void ipp_free(struct ipns *ns)
{
struct radix_pool *next;
- radix_free_nodes(pool->ip4_root);
- radix_free_nodes(pool->ip6_root);
+ radix_free_nodes(ns->ip4_root);
+ radix_free_nodes(ns->ip6_root);
- for (struct radix_pool *cur = pool->ip4_pool; cur; cur = next) {
+ for (struct radix_pool *cur = ns->ip4_pools; cur; cur = next) {
next = cur->next;
free(cur);
}
- for (struct radix_pool *cur = pool->ip6_pool; cur; cur = next) {
+ for (struct radix_pool *cur = ns->ip6_pools; cur; cur = next) {
next = cur->next;
free(cur);
}
}
-int ipp_add_v4(struct ip_pool *pool, const struct in_addr *ip, uint8_t cidr)
+int ipp_add_v4(struct ipns *ns, const struct in_addr *ip, uint8_t cidr)
{
- int ret = insert_v4(&pool->ip4_root, ip, cidr);
+ int ret = insert_v4(&ns->ip4_root, ip, cidr);
if (!ret)
- --pool->total_ipv4;
+ --ns->total_ipv4;
return ret;
}
-int ipp_add_v6(struct ip_pool *pool, const struct in6_addr *ip, uint8_t cidr)
+int ipp_add_v6(struct ipns *ns, const struct in6_addr *ip, uint8_t cidr)
{
- int ret = insert_v6(&pool->ip6_root, ip, cidr);
+ int ret = insert_v6(&ns->ip6_root, ip, cidr);
if (!ret) {
- if (pool->totall_ipv6 == 0)
- --pool->totalh_ipv6;
+ if (ns->totall_ipv6 == 0)
+ --ns->totalh_ipv6;
- --pool->totall_ipv6;
+ --ns->totall_ipv6;
}
return ret;
}
-int ipp_del_v4(struct ip_pool *pool, const struct in_addr *ip, uint8_t cidr)
+int ipp_del_v4(struct ipns *ns, const struct in_addr *ip, uint8_t cidr)
{
uint8_t key[4] __aligned(__alignof(uint32_t));
int ret;
swap_endian(key, (const uint8_t *)ip, 32);
- ret = remove_node(pool->ip4_root, key, cidr);
+ ret = remove_node(ns->ip4_root, key, cidr);
if (!ret)
- ++pool->total_ipv4;
+ ++ns->total_ipv4;
return ret;
}
-int ipp_del_v6(struct ip_pool *pool, const struct in6_addr *ip, uint8_t cidr)
+int ipp_del_v6(struct ipns *ns, const struct in6_addr *ip, uint8_t cidr)
{
uint8_t key[16] __aligned(__alignof(uint64_t));
int ret;
swap_endian(key, (const uint8_t *)ip, 128);
- ret = remove_node(pool->ip6_root, key, cidr);
+ ret = remove_node(ns->ip6_root, key, cidr);
if (!ret) {
- ++pool->totall_ipv6;
- if (pool->totall_ipv6 == 0)
- ++pool->totalh_ipv6;
+ ++ns->totall_ipv6;
+ if (ns->totall_ipv6 == 0)
+ ++ns->totalh_ipv6;
}
return ret;
}
-int ipp_addpool_v4(struct ip_pool *ipp, const struct in_addr *ip, uint8_t cidr)
+int ipp_addpool_v4(struct ipns *ns, const struct in_addr *ip, uint8_t cidr)
{
uint8_t key[4] __aligned(__alignof(uint32_t));
@@ -600,10 +600,10 @@ int ipp_addpool_v4(struct ip_pool *ipp, const struct in_addr *ip, uint8_t cidr)
return -1;
swap_endian(key, (const uint8_t *)ip, 32);
- return ipp_addpool(ipp, &ipp->ip4_pool, &ipp->ip4_root, 32, key, cidr);
+ return ipp_addpool(ns, &ns->ip4_pools, &ns->ip4_root, 32, key, cidr);
}
-int ipp_addpool_v6(struct ip_pool *ipp, const struct in6_addr *ip, uint8_t cidr)
+int ipp_addpool_v6(struct ipns *ns, const struct in6_addr *ip, uint8_t cidr)
{
uint8_t key[16] __aligned(__alignof(uint64_t));
@@ -611,26 +611,26 @@ int ipp_addpool_v6(struct ip_pool *ipp, const struct in6_addr *ip, uint8_t cidr)
return -1;
swap_endian(key, (const uint8_t *)ip, 128);
- return ipp_addpool(ipp, &ipp->ip6_pool, &ipp->ip6_root, 128, key, cidr);
+ return ipp_addpool(ns, &ns->ip6_pools, &ns->ip6_root, 128, key, cidr);
}
/* TODO: implement */
-int ipp_removepool_v4(struct ip_pool *pool, const struct in_addr *ip)
+int ipp_removepool_v4(struct ipns *ns, const struct in_addr *ip)
{
return 0;
}
/* TODO: implement */
-int ipp_removepool_v6(struct ip_pool *pool, const struct in6_addr *ip)
+int ipp_removepool_v6(struct ipns *ns, const struct in6_addr *ip)
{
return 0;
}
-void ipp_addnth_v4(struct ip_pool *pool, struct in_addr *dest, uint32_t index)
+void ipp_addnth_v4(struct ipns *ns, struct in_addr *dest, uint32_t index)
{
- struct radix_pool *current = pool->ip4_pool;
+ struct radix_pool *current = ns->ip4_pools;
- for (current = pool->ip4_pool; current; current = current->next) {
+ for (current = ns->ip4_pools; current; current = current->next) {
if (current->shadowed)
continue;
@@ -643,13 +643,13 @@ void ipp_addnth_v4(struct ip_pool *pool, struct in_addr *dest, uint32_t index)
BUG_ON(!current);
add_nth(current->node, 32, index, (uint8_t *)&dest->s_addr);
- --pool->total_ipv4;
+ --ns->total_ipv4;
}
-void ipp_addnth_v6(struct ip_pool *pool, struct in6_addr *dest,
- uint32_t index_low, uint64_t index_high)
+void ipp_addnth_v6(struct ipns *ns, struct in6_addr *dest, uint32_t index_low,
+ uint64_t index_high)
{
- struct radix_pool *current = pool->ip6_pool;
+ struct radix_pool *current = ns->ip6_pools;
uint64_t tmp;
while (current) {
@@ -676,8 +676,8 @@ void ipp_addnth_v6(struct ip_pool *pool, struct in6_addr *dest,
BUG_ON(!current || index_high);
add_nth(current->node, 128, index_low, (uint8_t *)&dest->s6_addr);
- if (pool->totall_ipv6 == 0)
- --pool->totalh_ipv6;
+ if (ns->totall_ipv6 == 0)
+ --ns->totalh_ipv6;
- --pool->totall_ipv6;
+ --ns->totall_ipv6;
}
diff --git a/radix-trie.h b/radix-trie.h
index a72ee50..4fa909b 100644
--- a/radix-trie.h
+++ b/radix-trie.h
@@ -10,37 +10,38 @@
#include <stdbool.h>
#include <stdint.h>
-struct ip_pool {
+struct ipns {
+ /* Total amount of available addresses over all pools */
uint64_t totall_ipv6;
uint32_t totalh_ipv6, total_ipv4;
+
struct radix_node *ip4_root, *ip6_root;
- struct radix_pool *ip4_pool, *ip6_pool;
+ struct radix_pool *ip4_pools, *ip6_pools;
};
-void ipp_init(struct ip_pool *pool);
-void ipp_free(struct ip_pool *pool);
+void ipp_init(struct ipns *ns);
+void ipp_free(struct ipns *ns);
-int ipp_add_v4(struct ip_pool *pool, const struct in_addr *ip, uint8_t cidr);
-int ipp_add_v6(struct ip_pool *pool, const struct in6_addr *ip, uint8_t cidr);
+int ipp_add_v4(struct ipns *ns, const struct in_addr *ip, uint8_t cidr);
+int ipp_add_v6(struct ipns *ns, const struct in6_addr *ip, uint8_t cidr);
-int ipp_del_v4(struct ip_pool *pool, const struct in_addr *ip, uint8_t cidr);
-int ipp_del_v6(struct ip_pool *pool, const struct in6_addr *ip, uint8_t cidr);
+int ipp_del_v4(struct ipns *ns, const struct in_addr *ip, uint8_t cidr);
+int ipp_del_v6(struct ipns *ns, const struct in6_addr *ip, uint8_t cidr);
-void ipp_addnth_v4(struct ip_pool *pool, struct in_addr *dest, uint32_t index);
-void ipp_addnth_v6(struct ip_pool *pool, struct in6_addr *dest,
- uint32_t index_low, uint64_t index_high);
+void ipp_addnth_v4(struct ipns *ns, struct in_addr *dest, uint32_t index);
+void ipp_addnth_v6(struct ipns *ns, struct in6_addr *dest, uint32_t index_low,
+ uint64_t index_high);
-int ipp_addpool_v4(struct ip_pool *ipp, const struct in_addr *ip, uint8_t cidr);
-int ipp_addpool_v6(struct ip_pool *ipp, const struct in6_addr *ip,
- uint8_t cidr);
+int ipp_addpool_v4(struct ipns *ns, const struct in_addr *ip, uint8_t cidr);
+int ipp_addpool_v6(struct ipns *ns, const struct in6_addr *ip, uint8_t cidr);
-int ipp_removepool_v4(struct ip_pool *pool, const struct in_addr *ip);
-int ipp_removepool_v6(struct ip_pool *pool, const struct in6_addr *ip);
+int ipp_removepool_v4(struct ipns *ns, const struct in_addr *ip);
+int ipp_removepool_v6(struct ipns *ns, const struct in6_addr *ip);
#ifdef DEBUG
void node_to_str(struct radix_node *node, char *buf, uint8_t bits);
-void debug_print_trie_v4(struct ip_pool *pool);
-void debug_print_trie_v6(struct ip_pool *pool);
+void debug_print_trie_v4(struct ipns *ns);
+void debug_print_trie_v6(struct ipns *ns);
#endif
#endif