Enable addrset to update from another addrset

This commit is contained in:
dmiller 2026-08-28 04:03:20 +00:00
parent 66a5cbe7ff
commit 8ecc9a6729
2 changed files with 86 additions and 0 deletions

View file

@ -560,6 +560,7 @@ extern void addrset_free(struct addrset *set);
extern void addrset_print(FILE *fp, const struct addrset *set);
extern int addrset_add_spec(struct addrset *set, const char *spec, int af, int dns);
extern int addrset_add_file(struct addrset *set, FILE *fd, int af, int dns);
extern void addrset_update(struct addrset *set, const struct addrset *other);
extern int addrset_contains(const struct addrset *set, const struct sockaddr *sa);
extern int addrset_matches_all(const struct addrset *set, int af);

View file

@ -485,6 +485,9 @@ static int trie_matches_all(const struct trie_node *this, int af)
}
return 0;
}
static void trie_insert_addr (struct trie_node *this, const u32 *addr, const u32 *mask);
/* Insert a sockaddr into the trie */
static void trie_insert (struct trie_node *this, const struct sockaddr *sa, int bits)
{
@ -498,6 +501,11 @@ static void trie_insert (struct trie_node *this, const struct sockaddr *sa, int
log_debug("Bad netmask length %d for address family %u, address not inserted.", bits, sa->sa_family);
return;
}
return trie_insert_addr(this, addr, mask);
}
static void trie_insert_addr (struct trie_node *this, const u32 *addr, const u32 *mask)
{
if (mask[0] == 0) {
/* Special case for ::/0 */
this->next_bit_one = TRIE_NODE_TRUE;
@ -527,6 +535,68 @@ static void trie_insert (struct trie_node *this, const struct sockaddr *sa, int
}
}
static void trie_update(struct trie_node * restrict this, const struct trie_node * restrict other)
{
const struct trie_node *stack[128+4] = {NULL};
const struct trie_node *curr = NULL;
u32 addr[4];
u32 mask[4];
int i = 0;
int j = 0;
u32 tmp = 0;
if (this == NULL || other == NULL || this == other) {
return;
}
stack[i++] = other;
while (i > 0) {
curr = stack[--i];
if (curr->next_bit_one == TRIE_NODE_TRUE
&& curr->next_bit_zero == TRIE_NODE_TRUE) {
trie_insert_addr(this, curr->addr, curr->mask);
}
else {
if (curr->next_bit_one != NULL) {
if (curr->next_bit_one == TRIE_NODE_TRUE) {
memcpy(addr, curr->addr, sizeof(addr));
memcpy(mask, curr->mask, sizeof(mask));
for (j = 0; j < 4; j++) {
tmp = (mask[j] >> 1) | ((u32)1 << 31);
if (tmp != mask[j]) {
addr[j] = addr[j] | (~mask[j] & tmp);
mask[j] = tmp;
break;
}
}
trie_insert_addr(this, addr, mask);
}
else {
stack[i++] = curr->next_bit_one;
}
}
if (curr->next_bit_zero != NULL) {
if (curr->next_bit_zero == TRIE_NODE_TRUE) {
memcpy(addr, curr->addr, sizeof(addr));
memcpy(mask, curr->mask, sizeof(mask));
for (j = 0; j < 4; j++) {
tmp = (mask[j] >> 1) | ((u32)1 << 31);
if (tmp != mask[j]) {
addr[j] = addr[j] & (~mask[j] ^ tmp);
mask[j] = tmp;
break;
}
}
trie_insert_addr(this, addr, mask);
}
else {
stack[i++] = curr->next_bit_zero;
}
}
}
}
}
/* Helper for matching addresses */
static int _trie_match (const struct trie_node *this, const u32 *addr)
{
@ -1012,3 +1082,18 @@ int addrset_matches_all(const struct addrset *set, int af)
}
return 0;
}
void addrset_update(struct addrset *set, const struct addrset *other)
{
struct addrset_elem *oe, *elem;
trie_update(set->trie4, other->trie4);
trie_update(set->trie6, other->trie6);
for (oe = other->head; oe != NULL; oe = oe->next) {
elem = (struct addrset_elem *) safe_malloc(sizeof(*elem));
memcpy(elem->ipv4.bits, oe->ipv4.bits, sizeof(elem->ipv4.bits));
elem->next = set->head;
set->head = elem;
}
}