From adeaad2b73ceadb795e6ca7b95a33aa00cd67c02 Mon Sep 17 00:00:00 2001 From: Thomas Gschwantner Date: Sat, 28 Sep 2019 19:53:53 +0200 Subject: Rename struct ip_pool to be more descriptive --- lease.c | 46 +++++++++++------------ radix-trie.c | 120 +++++++++++++++++++++++++++++------------------------------ radix-trie.h | 37 +++++++++--------- 3 files changed, 102 insertions(+), 101 deletions(-) diff --git a/lease.c b/lease.c index 2191035..37396e5 100644 --- a/lease.c +++ b/lease.c @@ -28,7 +28,7 @@ static const char *devname = NULL; static int ifindex = 0; -static struct ip_pool pool; +static struct ipns ipns; static time_t gexpires = TIME_T_MAX; static bool synchronized; @@ -67,7 +67,7 @@ void leases_init(const char *device_name, int interface_index, char *fname, synchronized = false; leases_ht = kh_init(leaseht); - ipp_init(&pool); + ipp_init(&ipns); nlh = mnl_nlmsg_put_header(buf); nlh->nlmsg_type = RTM_GETROUTE; @@ -94,7 +94,7 @@ void leases_free() } kh_destroy(leaseht, leases_ht); - ipp_free(&pool); + ipp_free(&ipns); } struct allowedips_update { @@ -219,7 +219,7 @@ struct wg_dynamic_lease *set_lease(wg_key pubkey, uint32_t leasetime, if (lease->ipv4.s_addr && (delete_ipv4 || (ipv4 && memcmp(&lease->ipv4, ipv4, sizeof(*ipv4))))) { - if (ipp_del_v4(&pool, &lease->ipv4, 32)) + if (ipp_del_v4(&ipns, &lease->ipv4, 32)) die("ipp_del_v4()\n"); memset(&lease->ipv4, 0, sizeof(lease->ipv4)); } @@ -227,26 +227,26 @@ struct wg_dynamic_lease *set_lease(wg_key pubkey, uint32_t leasetime, if (!IN6_IS_ADDR_UNSPECIFIED(&lease->ipv6) && (delete_ipv6 || (ipv6 && memcmp(&lease->ipv6, ipv6, sizeof(*ipv6))))) { - if (ipp_del_v6(&pool, &lease->ipv6, 128)) + if (ipp_del_v6(&ipns, &lease->ipv6, 128)) die("ipp_del_v6()\n"); memset(&lease->ipv6, 0, sizeof(lease->ipv6)); } if (!ipv4) { /* Wants random IPv4 address? */ - if (!pool.total_ipv4) { + if (!ipns.total_ipv4) { debug("IPv4 pool empty\n"); memset(&lease->ipv4, 0, sizeof(lease->ipv4)); } else { - uint32_t index = random_bounded(pool.total_ipv4); + uint32_t index = random_bounded(ipns.total_ipv4); debug("new_lease(v4): %u of %u\n", index, - pool.total_ipv4); - ipp_addnth_v4(&pool, &lease->ipv4, index); + ipns.total_ipv4); + ipp_addnth_v4(&ipns, &lease->ipv4, index); } } else if (ipv4->s_addr) { if (!memcmp(&lease->ipv4, ipv4, sizeof(*ipv4))) { debug("extending(v4)\n"); } else { - if (!ipp_add_v4(&pool, ipv4, 32)) { + if (!ipp_add_v4(&ipns, ipv4, 32)) { lease->ipv4 = *ipv4; } else { memset(&lease->ipv4, 0, sizeof(lease->ipv4)); @@ -255,29 +255,29 @@ struct wg_dynamic_lease *set_lease(wg_key pubkey, uint32_t leasetime, } if (!ipv6) { /* Wants random IPv6 address? */ - if (!pool.totalh_ipv6 && !pool.totall_ipv6) { + if (!ipns.totalh_ipv6 && !ipns.totall_ipv6) { debug("IPv6 pool empty\n"); memset(&lease->ipv6, 0, sizeof(lease->ipv6)); } else { uint64_t index_l; uint32_t index_h; - 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 (!IN6_IS_ADDR_UNSPECIFIED(ipv6)) { if (!memcmp(&lease->ipv6, ipv6, sizeof(*ipv6))) { debug("extending(v6)\n"); } else { - if (!ipp_add_v6(&pool, ipv6, 128)) { + if (!ipp_add_v6(&ipns, ipv6, 128)) { lease->ipv6 = *ipv6; } else { memset(&lease->ipv6, 0, sizeof(lease->ipv6)); @@ -342,10 +342,10 @@ int leases_refresh() time_t expires = lease->start_mono + lease->leasetime; if (cur_time >= expires) { if (lease->ipv4.s_addr) - ipp_del_v4(&pool, &lease->ipv4, 32); + ipp_del_v4(&ipns, &lease->ipv4, 32); if (!IN6_IS_ADDR_UNSPECIFIED(&lease->ipv6)) - ipp_del_v6(&pool, &lease->ipv6, 128); + ipp_del_v6(&ipns, &lease->ipv6, 128); memcpy(updates[i].peer_pubkey, kh_key(leases_ht, k), sizeof(wg_key)); @@ -458,18 +458,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 #include -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 -- cgit v1.2.3-59-g8ed1b