Skip to content

Commit

Permalink
If addresses are overspecified we could assert...
Browse files Browse the repository at this point in the history
10.10.10.10/20 inserted after 10.10.10.10/32
  • Loading branch information
postwait committed Jan 15, 2014
1 parent 8b960df commit ca8d8d5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/btrie.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void add_route(btrie *tree, uint32_t *key, unsigned char prefix_len,
for(i=0;i<4;i++) addr[i] = (i*32)+1 >= prefix_len ? 0 : htonl(key[i]); \
inet_ntop(prefix_len > 32 ? AF_INET6 : AF_INET, addr, ipb, sizeof(ipb)); \
(n)->long_desc = strdup(ipb); \
fprintf(stderr, "N(%s/%d) -> %s\n", (n)->long_desc, pl, m ? m : "insert"); \
fprintf(stderr, "LINE[%d] N(%s/%d) -> %s\n", __LINE__, (n)->long_desc, pl, m ? m : "insert"); \
} while(0)
#else
#define DA(n, pl, m)
Expand Down Expand Up @@ -253,6 +253,7 @@ void add_route(btrie *tree, uint32_t *key, unsigned char prefix_len,
/* here we must be inserting between node and down */
bits_in_common = calc_bits_in_commons(down, key, prefix_len);
parent = node;
DA(newnode, prefix_len, NULL);
assert(bits_in_common <= prefix_len);
assert(!parent || parent->prefix_len < prefix_len);

Expand Down Expand Up @@ -288,15 +289,24 @@ void add_route(btrie *tree, uint32_t *key, unsigned char prefix_len,

void add_route_ipv4(btrie *tree, struct in_addr *a,
unsigned char prefix_len, void *data) {
uint32_t ia = ntohl(a->s_addr);
uint32_t ia = ntohl(a->s_addr), mask;
assert(prefix_len <= 32);
mask = (prefix_len == 32) ? 0xffffffff : ~(0xffffffff >> prefix_len);
ia &= mask;
add_route(tree, &ia, prefix_len, data);
}
void add_route_ipv6(btrie *tree, struct in6_addr *a,
unsigned char prefix_len, void *data) {
uint32_t ia[4], i;
uint32_t ia[4], i, mask;
int splen;
memcpy(ia, &a->s6_addr, sizeof(ia));
for(i=0;i<4;i++) ia[i] = ntohl(ia[i]);
for(i=0;i<4;i++) {
splen = prefix_len - i*32;
mask = 0;
if(splen >= 0)
mask = (splen >= 32) ? 0xffffffff : ~(0xffffffff >> splen);
ia[i] = ntohl(ia[i]) & mask;
}
assert(prefix_len <= 128);
add_route(tree, ia, prefix_len, data);
}
Expand Down
2 changes: 1 addition & 1 deletion test/t.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fs.readFile("test/test.cidr", 'utf-8', function(err, data) {
}
}
}
assert.equal(count, 9, "loaded entries");
assert.equal(count, 10, "loaded entries");
for(var target in expectations) {
var result = lookup.find(target);
assert.equal(result, expectations[target],
Expand Down
3 changes: 2 additions & 1 deletion test/test.cidr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
10.0.0.0/8 rfc1918
10.80.116.0/23 my special place
10.80.117.12/32 specific
10.80.117.12/23 my special place
2620:1f::/48 omniti
2001:470::/48 he
2001:470:0:76::2/128 website
Expand Down

0 comments on commit ca8d8d5

Please sign in to comment.