feature: supported to match ipv6 address.
This commit is contained in:
parent
577cfa0ccb
commit
79fd587615
3 changed files with 138 additions and 5 deletions
|
@ -88,6 +88,9 @@ struct _R3Route {
|
|||
unsigned int remote_addr_v4;
|
||||
int remote_addr_v4_bits;
|
||||
|
||||
unsigned int remote_addr_v6[4];
|
||||
int remote_addr_v6_bits[4];
|
||||
|
||||
int http_scheme; // can be (SCHEME_HTTP or SCHEME_HTTPS)
|
||||
|
||||
} __attribute__((aligned(64)));
|
||||
|
|
39
src/node.c
39
src/node.c
|
@ -224,7 +224,7 @@ int r3_tree_compile_patterns(R3Node * n, char **errstr) {
|
|||
n->combined_pattern = cpat;
|
||||
|
||||
const char *pcre_error = NULL;
|
||||
int pcre_erroffset;
|
||||
int pcre_erroffset = 0;
|
||||
unsigned int option_bits = 0;
|
||||
|
||||
n->ov_cnt = (1 + n->edges.size) * 3;
|
||||
|
@ -932,16 +932,45 @@ inline int r3_route_cmp(const R3Route *r1, const match_entry *r2) {
|
|||
}
|
||||
}
|
||||
|
||||
if (r1->remote_addr_v4 > 0 && r1->remote_addr_v4_bits > 0) {
|
||||
if (r1->remote_addr_v4_bits > 0) {
|
||||
if (!r2->remote_addr.base) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned int r2_addr2 = inet_network(r2->remote_addr.base);
|
||||
int bits = 32 - r1->remote_addr_v4_bits;
|
||||
if (r1->remote_addr_v4 >> bits != r2_addr2 >> bits) {
|
||||
uint32_t addr;
|
||||
if(inet_pton(AF_INET, r2->remote_addr.base, (void *)&addr) != 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned int r2_addr = ntohl(addr);
|
||||
int bits = 32 - r1->remote_addr_v4_bits;
|
||||
if (r1->remote_addr_v4 >> bits != r2_addr >> bits) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
info("r1 v6_bits[0]: %d\n", r1->remote_addr_v6_bits[0]);
|
||||
if (r1->remote_addr_v6_bits[0] > 0) {
|
||||
if (!r2->remote_addr.base) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct in6_addr addr6;
|
||||
if(inet_pton(AF_INET6, r2->remote_addr.base, (void *)&addr6) != 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int bits = 32 - r1->remote_addr_v6_bits[i];
|
||||
if (bits == 32) {
|
||||
continue;
|
||||
}
|
||||
|
||||
unsigned int r2_addr = ntohl(addr6.__in6_u.__u6_addr32[i]);
|
||||
if (r1->remote_addr_v6[i] >> bits != r2_addr >> bits) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -77,16 +77,117 @@ START_TEST (test_remote_addrs)
|
|||
matched_route = r3_tree_match_route(n, entry);
|
||||
ck_assert(matched_route == NULL);
|
||||
|
||||
entry = match_entry_create("/boo");
|
||||
entry->remote_addr.base = "127.0.1.333"; // invalid ip address
|
||||
entry->remote_addr.len = sizeof("127.0.1.333") - 1;
|
||||
matched_route = r3_tree_match_route(n, entry);
|
||||
ck_assert(matched_route == NULL);
|
||||
|
||||
r3_tree_free(n);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
void parse_ipv6(const char *ipv6, int nmask, R3Route * route)
|
||||
{
|
||||
struct in6_addr addr6;
|
||||
int ret = inet_pton(AF_INET6, ipv6, (void *)&addr6);
|
||||
ck_assert(ret == 1);
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
route->remote_addr_v6[i] = ntohl(addr6.__in6_u.__u6_addr32[i]);
|
||||
|
||||
if (nmask >= 32) {
|
||||
route->remote_addr_v6_bits[i] = 32;
|
||||
} else if (nmask > 0) {
|
||||
route->remote_addr_v6_bits[i] = nmask;
|
||||
} else {
|
||||
route->remote_addr_v6_bits[i] = 0;
|
||||
}
|
||||
|
||||
nmask -= 32;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
START_TEST (test_remote_addrs_ipv6)
|
||||
{
|
||||
R3Node * n = r3_tree_create(10);
|
||||
R3Route * route = NULL;
|
||||
match_entry * entry;
|
||||
R3Route *matched_route;
|
||||
|
||||
char * uri0 = "/foo";
|
||||
route = r3_tree_insert_routel(n, 0, uri0, strlen(uri0), &uri0);
|
||||
parse_ipv6("fe80:fe80::1", 128, route); // "fe80:fe80::1"
|
||||
|
||||
char * uri1 = "/bar";
|
||||
route = r3_tree_insert_routel(n, 0, uri1, strlen(uri1), &uri1);
|
||||
parse_ipv6("fe80:fe80::", 32, route); // "fe80:fe80::/32"
|
||||
|
||||
char * uri2 = "/goo";
|
||||
route = r3_tree_insert_routel(n, 0, uri2, strlen(uri2), &uri2);
|
||||
parse_ipv6("::1", 128, route); // "::1"
|
||||
|
||||
char * err = NULL;
|
||||
r3_tree_compile(n, &err);
|
||||
ck_assert(err == NULL);
|
||||
|
||||
entry = match_entry_create("/foo");
|
||||
entry->remote_addr.base = "fe80:fe80::1";
|
||||
entry->remote_addr.len = sizeof("fe80:fe80::1") - 1;
|
||||
matched_route = r3_tree_match_route(n, entry);
|
||||
ck_assert(matched_route != NULL);
|
||||
ck_assert(matched_route->data == &uri0);
|
||||
|
||||
entry = match_entry_create("/foo");
|
||||
entry->remote_addr.base = "fe80:fe80::2";
|
||||
entry->remote_addr.len = sizeof("fe80:fe80::2") - 1;
|
||||
matched_route = r3_tree_match_route(n, entry);
|
||||
ck_assert(matched_route == NULL);
|
||||
|
||||
entry = match_entry_create("/foo");
|
||||
entry->remote_addr.base = "fe88:fe80::1";
|
||||
entry->remote_addr.len = sizeof("fe88:fe80::1") - 1;
|
||||
matched_route = r3_tree_match_route(n, entry);
|
||||
ck_assert(matched_route == NULL);
|
||||
|
||||
entry = match_entry_create("/bar");
|
||||
entry->remote_addr.base = "fe80:fe80::1";
|
||||
entry->remote_addr.len = sizeof("fe80:fe80::1") - 1;
|
||||
matched_route = r3_tree_match_route(n, entry);
|
||||
ck_assert(matched_route != NULL);
|
||||
ck_assert(matched_route->data == &uri1);
|
||||
|
||||
entry = match_entry_create("/bar");
|
||||
entry->remote_addr.base = "fe88:fe80::1";
|
||||
entry->remote_addr.len = sizeof("fe88:fe80::1") - 1;
|
||||
matched_route = r3_tree_match_route(n, entry);
|
||||
ck_assert(matched_route == NULL);
|
||||
|
||||
entry = match_entry_create("/goo");
|
||||
entry->remote_addr.base = "::1";
|
||||
entry->remote_addr.len = sizeof("::1") - 1;
|
||||
matched_route = r3_tree_match_route(n, entry);
|
||||
ck_assert(matched_route != NULL);
|
||||
ck_assert(matched_route->data == &uri2);
|
||||
|
||||
entry = match_entry_create("/goo");
|
||||
entry->remote_addr.base = "::2";
|
||||
entry->remote_addr.len = sizeof("::2") - 1;
|
||||
matched_route = r3_tree_match_route(n, entry);
|
||||
ck_assert(matched_route == NULL);
|
||||
|
||||
r3_tree_free(n);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
Suite* r3_suite (void) {
|
||||
Suite *suite = suite_create("r3 remote_addr tests");
|
||||
TCase *tcase = tcase_create("testcase");
|
||||
tcase_add_test(tcase, test_remote_addrs);
|
||||
tcase_add_test(tcase, test_remote_addrs_ipv6);
|
||||
suite_add_tcase(suite, tcase);
|
||||
return suite;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue