Skip to content

Commit

Permalink
Add ip_addr and ip_mask field to route struct
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jun 12, 2014
1 parent b9dc94b commit 5a536b9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
14 changes: 9 additions & 5 deletions include/r3.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,20 @@ struct _route {

void * data;

char * ip_addr;
char * ip_addr_mask;


// todo: take of this
char * remote_addr_pattern;
int remote_addr_pattern_len;
};

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

Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions src/ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 13 additions & 2 deletions tests/check_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5a536b9

Please sign in to comment.