From b9dc94bbee254b958b9cc92ee5b009ca3b80adeb Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 12 Jun 2014 19:01:19 +0800 Subject: [PATCH] Add ip cmp function --- include/r3.h | 2 ++ src/Makefile.am | 2 +- src/ip.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ tests/check_tree.c | 11 +++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/ip.c diff --git a/include/r3.h b/include/r3.h index e5daead..3b0d1ce 100644 --- a/include/r3.h +++ b/include/r3.h @@ -216,6 +216,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); #ifdef __cplusplus diff --git a/src/Makefile.am b/src/Makefile.am index 31a3aed..12d2cbc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/ip.c b/src/ip.c new file mode 100644 index 0000000..947ef45 --- /dev/null +++ b/src/ip.c @@ -0,0 +1,48 @@ +/* + * ip.c + * Copyright (C) 2014 c9s + * + * Distributed under terms of the MIT license. + */ + + +// #include "ip.h" +#include +#include +#include + +int r3_ip_cmp_str(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( ) { + return 0; +} + +/* + 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); +*/ diff --git a/tests/check_tree.c b/tests/check_tree.c index 630ae52..c577bc5 100644 --- a/tests/check_tree.c +++ b/tests/check_tree.c @@ -659,6 +659,16 @@ START_TEST(test_pcre_pattern_more) 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")); +} +END_TEST + + + + START_TEST(test_insert_route) @@ -722,6 +732,7 @@ 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); suite_add_tcase(suite, tcase); return suite;