-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrt.cpp
More file actions
138 lines (112 loc) · 2.47 KB
/
rt.cpp
File metadata and controls
138 lines (112 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdlib.h>
#include <list>
#include <stdexcept>
#include "bgp_attributes.hpp"
#include "rt.hpp"
using namespace std;
RadixTree::RadixTree()
{
rt_root = new struct rt_node;
rt_root->parent = NULL;
rt_root->right = NULL;
rt_root->left = NULL;
}
list<bgp_attributes*>*
RadixTree::find(in_addr_t net, uint8_t mask)
{
uint32_t bit = 0x80000000;
struct rt_node *result = NULL, *node = rt_root;
while(node) {
if(!node->data_list.empty())
result = node;
if(net & bit)
node = node->right;
else
node = node->left;
bit >>= 1;
}
if(result == NULL)
return NULL;
return &result->data_list;
}
int
RadixTree::add(bgp_attributes *attrs, in_addr_t net, uint8_t mask)
{
uint32_t bit = 0x80000000;
struct rt_node *node, *next;
node = next = rt_root;
while (bit & (0xffffffff << (32-mask))) {
if(net & bit)
next = node->right;
else
next = node->left;
if(next == NULL)
break;
bit >>= 1;
node = next;
}
/* We already have an allocated entry. Just fill it. */
if(next != NULL) {
node->data_list.push_back(attrs);
return 1;
}
/* Create an entry and all entries till it. */
while(bit & (0xffffffff << (32-mask))) {
next = new struct rt_node;
next->right = NULL;
next->left = NULL;
next->parent = node;
if(net & bit)
node->right = next;
else
node->left = next;
bit >>= 1;
node = next;
}
node->data_list.push_back(attrs);
return 1;
}
int
RadixTree::remove(in_addr_t net, uint8_t mask, in_addr_t peer)
{
uint32_t bit = 0x80000000;
struct rt_node *node = rt_root, *tmp;
while (node && (bit & (0xffffffff << (32-mask)))) {
if(net & bit)
node = node->right;
else
node = node->left;
bit >>= 1;
}
/* Not found */
if(node == NULL)
return 0;
for(list<bgp_attributes*>::iterator ii=node->data_list.begin();
ii != node->data_list.end(); ++ii)
if(peer == (*ii)->peer.as_in_addr()) {
node->data_list.erase(ii);
break;
}
if(!node->data_list.empty())
/* We still have entries in the list. Keep the node. */
return 1;
/* We have both branches. Just clear data. */
if(node->right || node->left)
return 1;
while(1) {
if(node->parent->right == node)
node->parent->right = NULL;
else
node->parent->left = NULL;
tmp = node;
node = node->parent;
free(tmp);
if(node->right || node->left)
break;
if(!node->data_list.empty())
break;
if(node->parent == NULL)
break;
}
return 1;
}