From 83cf00e0117c4b41001a56f93cf4beae720cc6a0 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Sun, 2 Jun 2013 05:53:13 -0700 Subject: Add interface address validation support. Fix IPv4 CIDR regex to support one-digit mask properly. --- src/ipaddrcheck_functions.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/ipaddrcheck_functions.c') diff --git a/src/ipaddrcheck_functions.c b/src/ipaddrcheck_functions.c index dfcdcfb..0240914 100644 --- a/src/ipaddrcheck_functions.c +++ b/src/ipaddrcheck_functions.c @@ -48,7 +48,7 @@ int is_ipv4_cidr(char* address_str) const char *error; int erroffset; - re = pcre_compile("^((([1-9]\\d{0,2}|0)\\.){3}([1-9]\\d{0,2}|0)(\\/([1-3][0-9]|0)))$", + re = pcre_compile("^((([1-9]\\d{0,2}|0)\\.){3}([1-9]\\d{0,2}|0)\\/([1-9]\\d*|0))$", 0, &error, &erroffset, NULL); rc = pcre_exec(re, NULL, address_str, strlen(address_str), 0, 0, offsets, 1); @@ -443,3 +443,26 @@ int is_ipv6_link_local(CIDR *address) return(result); } +/* Is it an address that can belong an interface? */ +int is_valid_intf_address(CIDR *address, char* address_str, int allow_loopback) +{ + int result; + + if( (is_ipv4_broadcast(address) == RESULT_FAILURE) && + (is_ipv4_multicast(address) == RESULT_FAILURE) && + (is_ipv6_multicast(address) == RESULT_FAILURE) && + ((is_ipv4_loopback(address) == RESULT_FAILURE) || (allow_loopback == LOOPBACK_ALLOWED)) && + (cidr_equals(address, cidr_from_str(IPV6_LOOPBACK)) != 0) && + (cidr_equals(address, cidr_from_str(IPV4_UNSPECIFIED)) != 0) && + (cidr_contains(cidr_from_str(IPV4_THIS), address) != 0) && + (is_any_cidr(address_str) == RESULT_SUCCESS) ) + { + result = RESULT_SUCCESS; + } + else + { + result = RESULT_FAILURE; + } + + return(result); +} -- cgit v1.2.3