Skip to content

Commit

Permalink
Better support for deleting entries in NetmaskTree and NetmaskGroup
Browse files Browse the repository at this point in the history
- NetmaskTree erase() method now has optional cleanup parameter
  - cleanup_tree() method will remove "hanging" branches of the tree
- NetmaskGroup now has deleteMask() methods that use NetmaskTree:erase()
  • Loading branch information
neilcook committed Aug 8, 2017
1 parent 8a80cc8 commit d5c916a
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions common/iputils.hh
Expand Up @@ -722,13 +722,31 @@ public:
return ret;
}

//<! Removes key from TreeMap. This does not clean up the tree.
void erase(const key_type& key) {
void cleanup_tree(TreeNode* node)
{
// only cleaup this node if it has no children and node4 and node6 are both empty
if (!(node->left || node->right || node->node6 || node->node4)) {
// get parent node ptr
TreeNode* parent = node->parent;
// delete this node
if (parent) {
if (parent->left.get() == node)
parent->left.reset();
else
parent->right.reset();
// now recurse up to the parent
cleanup_tree(parent);
}
}
}

//<! Removes key from TreeMap. This optionally cleans up the tree.
void erase(const key_type& key, bool cleanup=false) {
TreeNode *node = root.get();

// no tree, no value
if ( node == nullptr ) return;

// exact same thing as above, except
if (key.getNetwork().sin4.sin_family == AF_INET) {
std::bitset<32> addr(be32toh(key.getNetwork().sin4.sin_addr.s_addr));
Expand All @@ -750,6 +768,7 @@ public:
it++;
}
node->node4.reset();
if (cleanup) cleanup_tree(node);
}
} else {
uint64_t* addr = (uint64_t*)key.getNetwork().sin6.sin6_addr.s6_addr;
Expand All @@ -775,6 +794,7 @@ public:
it++;
}
node->node6.reset();
if (cleanup) cleanup_tree(node);
}
}
}
Expand Down Expand Up @@ -839,6 +859,21 @@ public:
return match(&ip);
}

bool lookup(const ComboAddress* ip, Netmask* nmp) const
{
const auto &ret = tree.lookup(*ip);
if (ret) {
if (nmp) *nmp = ret->first;
return ret->second;
}
return false;
}

bool lookup(const ComboAddress& ip, Netmask* nmp) const
{
return lookup(&ip, nmp);
}

//! Add this string to the list of possible matches
void addMask(const std::string &ip, bool positive=true)
{
Expand All @@ -855,6 +890,18 @@ public:
tree.insert(nm).second=positive;
}

//! Delete this Netmask from the list of possible matches
void deleteMask(const Netmask& nm)
{
tree.erase(nm, true);
}

void deleteMask(const std::string& ip)
{
if (!ip.empty())
deleteMask(Netmask(ip));
}

void clear()
{
tree.clear();
Expand Down

0 comments on commit d5c916a

Please sign in to comment.