Add ip_addr and ip_mask field to route struct
This commit is contained in:
parent
b9dc94bbee
commit
5a536b9145
3 changed files with 28 additions and 13 deletions
14
include/r3.h
14
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
|
||||
}
|
||||
|
|
12
src/ip.c
12
src/ip.c
|
@ -11,19 +11,19 @@
|
|||
#include <stdio.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 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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue