diff options
author | Daniil Baturin <daniil@baturin.org> | 2013-06-01 13:26:42 -0700 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2013-06-01 13:26:42 -0700 |
commit | 00f98b395795533a9a424fa9bd55e52957ad1031 (patch) | |
tree | 94795ac4f40f10e2987fd9a951577ed75c7a83d1 | |
parent | 962a4544caaa4b43a5000ccfab55ac5b621dfb5d (diff) | |
download | ipaddrcheck-00f98b395795533a9a424fa9bd55e52957ad1031.tar.gz ipaddrcheck-00f98b395795533a9a424fa9bd55e52957ad1031.zip |
Add missing address format validation functions.
-rw-r--r-- | src/ipaddrcheck_functions.c | 54 | ||||
-rw-r--r-- | src/ipaddrcheck_functions.h | 2 | ||||
-rw-r--r-- | tests/check_ipaddrcheck.c | 38 |
3 files changed, 94 insertions, 0 deletions
diff --git a/src/ipaddrcheck_functions.c b/src/ipaddrcheck_functions.c index 27a33fb..061ba8f 100644 --- a/src/ipaddrcheck_functions.c +++ b/src/ipaddrcheck_functions.c @@ -109,6 +109,60 @@ int is_ipv4_single(char* address_str) return(result); } +/* Is it an IPv6 address with prefix length? */ +int is_ipv6_cidr(char* address_str) +{ + int result; + + int offsets[1]; + pcre *re; + int rc; + const char *error; + int erroffset; + + re = pcre_compile("^((([0-9a-fA-F\\:])+)(\\/\\d{1,3}))$", + 0, &error, &erroffset, NULL); + rc = pcre_exec(re, NULL, address_str, strlen(address_str), 0, 0, offsets, 1); + + if( rc < 0 ) + { + result = RESULT_FAILURE; + } + else + { + result = RESULT_SUCCESS; + } + + return(result); +} + +/* Is it a single IPv6 address? */ +int is_ipv6_single(char* address_str) +{ + int result; + + int offsets[1]; + pcre *re; + int rc; + const char *error; + int erroffset; + + re = pcre_compile("^(([0-9a-fA-F\\:])+)$", + 0, &error, &erroffset, NULL); + rc = pcre_exec(re, NULL, address_str, strlen(address_str), 0, 0, offsets, 1); + + if( rc < 0 ) + { + result = RESULT_FAILURE; + } + else + { + result = RESULT_SUCCESS; + } + + return(result); +} + /* * Address checking functions that rely on libcidr */ diff --git a/src/ipaddrcheck_functions.h b/src/ipaddrcheck_functions.h index b06fc22..e81c03c 100644 --- a/src/ipaddrcheck_functions.h +++ b/src/ipaddrcheck_functions.h @@ -46,6 +46,8 @@ int has_mask(char* address_str); int is_ipv4_cidr(char* address_str); int is_ipv4_single(char* address_str); +int is_ipv6_cidr(char* address_str); +int is_ipv6_single(char* address_str); int is_valid_address(CIDR *address); int is_ipv4(CIDR *address); int is_ipv4_host(CIDR *address); diff --git a/tests/check_ipaddrcheck.c b/tests/check_ipaddrcheck.c index 7671453..542093e 100644 --- a/tests/check_ipaddrcheck.c +++ b/tests/check_ipaddrcheck.c @@ -62,11 +62,46 @@ START_TEST (test_is_ipv4_cidr) char* address_str_no_mask = "192.0.2.1"; ck_assert_int_eq(is_ipv4_cidr(address_str_no_mask), RESULT_FAILURE); + /* libcidr allows it, but we don't want to support it */ char* address_str_decimal_mask = "192.0.2.1/255.255.255.0"; ck_assert_int_eq(is_ipv4_cidr(address_str_decimal_mask), RESULT_FAILURE); } END_TEST +START_TEST (test_is_ipv4_single) +{ + char* good_address_str = "192.0.2.1"; + ck_assert_int_eq(is_ipv4_single(good_address_str), RESULT_SUCCESS); + + char* bad_address_str = "192.0.2.1/25"; + ck_assert_int_eq(is_ipv4_single(bad_address_str), RESULT_FAILURE); +} +END_TEST + +START_TEST (test_is_ipv6_cidr) +{ + char* good_address_str = "2001:db8:abcd::/64"; + ck_assert_int_eq(is_ipv6_cidr(good_address_str), RESULT_SUCCESS); + + char* address_str_no_mask = "2001:db8::1"; + ck_assert_int_eq(is_ipv6_cidr(address_str_no_mask), RESULT_FAILURE); + + /* libcidr allows fully spellt hex masks, but we don't want to support it */ + char* address_str_decimal_mask = "::/0:0:0:0:0:0:0:0"; + ck_assert_int_eq(is_ipv6_cidr(address_str_decimal_mask), RESULT_FAILURE); +} +END_TEST + +START_TEST (test_is_ipv6_single) +{ + char* good_address_str = "2001:db8::10"; + ck_assert_int_eq(is_ipv6_single(good_address_str), RESULT_SUCCESS); + + char* bad_address_str = "2001:db8::/32"; + ck_assert_int_eq(is_ipv6_single(bad_address_str), RESULT_FAILURE); +} +END_TEST + START_TEST (test_is_ipv4) { char* good_address_str = "192.0.2.1"; @@ -285,6 +320,9 @@ Suite *ipaddrcheck_suite(void) tcase_add_test(tc_core, test_has_mask); tcase_add_test(tc_core, test_is_valid_address); tcase_add_test(tc_core, test_is_ipv4_cidr); + tcase_add_test(tc_core, test_is_ipv4_single); + tcase_add_test(tc_core, test_is_ipv6_cidr); + tcase_add_test(tc_core, test_is_ipv6_single); tcase_add_test(tc_core, test_is_ipv4); tcase_add_test(tc_core, test_is_ipv4_host); tcase_add_test(tc_core, test_is_ipv4_net); |