Compare commits

...

3 commits

Author SHA1 Message Date
c9s cebf4cfba0 Add ip_mask 2014-06-13 19:38:04 +08:00
c9s 5a536b9145 Add ip_addr and ip_mask field to route struct 2014-06-12 22:34:11 +08:00
c9s b9dc94bbee Add ip cmp function 2014-06-12 19:01:19 +08:00
4 changed files with 80 additions and 4 deletions

View file

@ -81,6 +81,11 @@ struct _route {
void * data; void * data;
char * ip_addr;
char * ip_mask;
// todo: take of this
char * remote_addr_pattern; char * remote_addr_pattern;
int remote_addr_pattern_len; int remote_addr_pattern_len;
}; };
@ -88,8 +93,8 @@ struct _route {
typedef struct { typedef struct {
str_array * vars; str_array * vars;
const char * path; // current path to dispatch const char * path; // current path to dispatch
int path_len; // the length of the current path int path_len; // the length of the current path
int request_method; // current request method int request_method; // current request method
void * data; // route ptr void * data; // route ptr
@ -216,7 +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(const char* a, const char* b);
int r3_ip_mask_cmp(const char *a, const char* mask_str, const char* b);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -4,7 +4,7 @@ MAYBE_COVERAGE=--coverage
noinst_LTLIBRARIES = libr3core.la noinst_LTLIBRARIES = libr3core.la
# lib_LIBRARIES = libr3.a # lib_LIBRARIES = libr3.a
libr3core_la_SOURCES = node.c edge.c str.c token.c match_entry.c slug.c libr3core_la_SOURCES = node.c edge.c str.c token.c match_entry.c slug.c ip.c
if ENABLE_JSON if ENABLE_JSON
libr3core_la_SOURCES += json.c libr3core_la_SOURCES += json.c

48
src/ip.c Normal file
View file

@ -0,0 +1,48 @@
/*
* ip.c
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
// #include "ip.h"
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
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_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);
}
/*
long in;
in = inet_addr("192.168.1.1");
printf("My unreadable addres is %ld\n", in);
in = inet_addr("8.8.8.8");
printf("My unreadable addres is %ld\n", in);
printf("My unreadable addres is %u %d\n", inet_addr("255.255.255.255"), 2 << 16 );
printf("My unreadable addres is %u %d\n", inet_addr("255.0.0.0") , (2 << 7) - 1 );
printf("My unreadable addres is %u %d\n", inet_addr("255.255.0.0") , (2 << 15) - 1 );
printf("My unreadable addres is %u %d\n", inet_addr("255.255.255.0") , (2 << 23) - 1 );
printf("My unreadable addres is %u\n", inet_addr("255.255.255.0") & inet_addr("192.168.0.1") );
struct in_addr in2;
in2.s_addr = inet_addr("192.168.0.1");
char *dot_ip = inet_ntoa(in2);
printf("%s\n", dot_ip);
*/

View file

@ -659,6 +659,26 @@ START_TEST(test_pcre_pattern_more)
END_TEST END_TEST
START_TEST(test_ip_cmp)
{
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) START_TEST(test_insert_route)
@ -722,6 +742,8 @@ Suite* r3_suite (void) {
tcase_add_test(tcase, test_pcre_patterns_insert_2); tcase_add_test(tcase, test_pcre_patterns_insert_2);
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_mask_cmp);
suite_add_tcase(suite, tcase); suite_add_tcase(suite, tcase);
return suite; return suite;