diff --git a/include/r3.h b/include/r3.h index 3b0d1ce..0e3bb18 100644 --- a/include/r3.h +++ b/include/r3.h @@ -81,6 +81,11 @@ struct _route { void * data; + char * ip_addr; + char * ip_addr_mask; + + + // todo: take of this char * remote_addr_pattern; int remote_addr_pattern_len; }; @@ -88,8 +93,8 @@ struct _route { typedef struct { str_array * vars; const char * path; // current path to dispatch - int path_len; // the length of the current path - int request_method; // current request method + int path_len; // the length of the current path + int request_method; // current request method void * data; // route ptr @@ -216,9 +221,8 @@ match_entry * match_entry_createl(const char * path, int path_len); void match_entry_free(match_entry * entry); -int r3_ip_cmp_str(const char* a, const char* b); -int r3_ip_cmp_long(long a, long b); - +int r3_ip_cmp(const char* a, const char* b); +int r3_ip_mask_cmp(const char *a, const char* mask_str, const char* b); #ifdef __cplusplus } diff --git a/src/ip.c b/src/ip.c index 947ef45..73a5e61 100644 --- a/src/ip.c +++ b/src/ip.c @@ -11,19 +11,19 @@ #include #include -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 bl = inet_addr(b); return al == bl; } -int r3_ip_cmp_long(long a, long b) { - return 0; +int r3_ip_mask_cmp(const char *a, const char* b, const char* mask_str) { + 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; diff --git a/tests/check_tree.c b/tests/check_tree.c index c577bc5..7a5b9ec 100644 --- a/tests/check_tree.c +++ b/tests/check_tree.c @@ -661,14 +661,24 @@ END_TEST START_TEST(test_ip_cmp) { - ck_assert(r3_ip_cmp_str("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","127.0.0.1")); + ck_assert(!r3_ip_cmp("127.0.0.1","220.12.12.12")); } 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) @@ -733,6 +743,7 @@ Suite* r3_suite (void) { tcase_add_test(tcase, test_pcre_patterns_insert_3); tcase_add_test(tcase, test_incomplete_slug_path); tcase_add_test(tcase, test_ip_cmp); + tcase_add_test(tcase, test_ip_mask_cmp); suite_add_tcase(suite, tcase); return suite;