Add ip_addr and ip_mask field to route struct

This commit is contained in:
c9s 2014-06-12 22:34:11 +08:00
parent b9dc94bbee
commit 5a536b9145
3 changed files with 28 additions and 13 deletions

View file

@ -81,6 +81,11 @@ struct _route {
void * data; void * data;
char * ip_addr;
char * ip_addr_mask;
// todo: take of this
char * remote_addr_pattern; char * remote_addr_pattern;
int remote_addr_pattern_len; int remote_addr_pattern_len;
}; };
@ -216,9 +221,8 @@ match_entry * match_entry_createl(const char * path, int path_len);
void match_entry_free(match_entry * entry); void match_entry_free(match_entry * entry);
int r3_ip_cmp_str(const char* a, const char* b); int r3_ip_cmp(const char* a, const char* b);
int r3_ip_cmp_long(long a, long b); int r3_ip_mask_cmp(const char *a, const char* mask_str, const char* b);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -11,19 +11,19 @@
#include <stdio.h> #include <stdio.h>
#include <arpa/inet.h> #include <arpa/inet.h>
int r3_ip_cmp_str(const char* a, const char* b) { int r3_ip_cmp(const char* a, const char* b) {
long al = inet_addr(a); long al = inet_addr(a);
long bl = inet_addr(b); long bl = inet_addr(b);
return al == bl; return al == bl;
} }
int r3_ip_cmp_long(long a, long b) { int r3_ip_mask_cmp(const char *a, const char* b, const char* mask_str) {
return 0; long m = inet_addr(mask_str);
long al = inet_addr(a);
long bl = inet_addr(b);
return !((al ^ bl) & m);
} }
int r3_ip_mask_cmp( ) {
return 0;
}
/* /*
long in; long in;

View file

@ -661,14 +661,24 @@ END_TEST
START_TEST(test_ip_cmp) START_TEST(test_ip_cmp)
{ {
ck_assert(r3_ip_cmp_str("127.0.0.1","127.0.0.1")); ck_assert(r3_ip_cmp("127.0.0.1","127.0.0.1"));
ck_assert(!r3_ip_cmp_str("127.0.0.1","220.12.12.12")); ck_assert(!r3_ip_cmp("127.0.0.1","220.12.12.12"));
} }
END_TEST END_TEST
START_TEST(test_ip_mask_cmp)
{
ck_assert( r3_ip_mask_cmp("127.123.123.123" , "127.0.0.1" , "255.0.0.0" ));
ck_assert( r3_ip_mask_cmp("192.168.123.123" , "192.168.0.1" , "255.0.0.0" ));
ck_assert( r3_ip_mask_cmp("192.168.123.123" , "192.168.0.1" , "255.255.0.0" ));
ck_assert( r3_ip_mask_cmp("192.168.123.123" , "192.168.123.1" , "255.255.255.0" ));
ck_assert(!r3_ip_mask_cmp("127.123.123.123" , "127.0.0.1", "255.255.0.0" ));
ck_assert(!r3_ip_mask_cmp("127.123.123.123" , "127.0.0.1", "255.255.255.0" ));
}
END_TEST
START_TEST(test_insert_route) START_TEST(test_insert_route)
@ -733,6 +743,7 @@ Suite* r3_suite (void) {
tcase_add_test(tcase, test_pcre_patterns_insert_3); tcase_add_test(tcase, test_pcre_patterns_insert_3);
tcase_add_test(tcase, test_incomplete_slug_path); tcase_add_test(tcase, test_incomplete_slug_path);
tcase_add_test(tcase, test_ip_cmp); tcase_add_test(tcase, test_ip_cmp);
tcase_add_test(tcase, test_ip_mask_cmp);
suite_add_tcase(suite, tcase); suite_add_tcase(suite, tcase);
return suite; return suite;