Add ip cmp function

This commit is contained in:
c9s 2014-06-12 19:01:19 +08:00
parent 98f27dadc9
commit b9dc94bbee
4 changed files with 62 additions and 1 deletions

View file

@ -216,6 +216,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_str(const char* a, const char* b);
int r3_ip_cmp_long(long a, long 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_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);
*/

View file

@ -659,6 +659,16 @@ START_TEST(test_pcre_pattern_more)
END_TEST 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) 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_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);
suite_add_tcase(suite, tcase); suite_add_tcase(suite, tcase);
return suite; return suite;