Compare commits
3 commits
2.0
...
feature/ip
Author | SHA1 | Date | |
---|---|---|---|
|
cebf4cfba0 | ||
|
5a536b9145 | ||
|
b9dc94bbee |
4 changed files with 80 additions and 4 deletions
|
@ -81,6 +81,11 @@ struct _route {
|
|||
|
||||
void * data;
|
||||
|
||||
char * ip_addr;
|
||||
char * ip_mask;
|
||||
|
||||
|
||||
// todo: take of this
|
||||
char * remote_addr_pattern;
|
||||
int remote_addr_pattern_len;
|
||||
};
|
||||
|
@ -216,7 +221,8 @@ match_entry * match_entry_createl(const char * path, int path_len);
|
|||
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
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ MAYBE_COVERAGE=--coverage
|
|||
|
||||
noinst_LTLIBRARIES = libr3core.la
|
||||
# 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
|
||||
libr3core_la_SOURCES += json.c
|
||||
|
|
48
src/ip.c
Normal file
48
src/ip.c
Normal 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);
|
||||
*/
|
|
@ -659,6 +659,26 @@ START_TEST(test_pcre_pattern_more)
|
|||
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)
|
||||
|
@ -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_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