commit e8e45ff13ed86d8851bab77d65d899d0ca6e3b89 Author: rl1987 rl1987@sdf.lonestar.org Date: Sun Oct 12 20:39:00 2014 +0300
Introducing helper function to validate IPv4 address strings. --- src/common/util.c | 19 +++++++++++++++++-- src/common/util.h | 1 + src/test/test_util.c | 21 +++++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/src/common/util.c b/src/common/util.c index 606b9e1..ba9d78a 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -957,8 +957,19 @@ string_is_key_value(int severity, const char *string) return 1; }
-/** Return true iff <b>string</b> is valid DNS name, as defined in - * RFC 1035 Section 2.3.1. +/** Return true if <b>string</b> represents a valid IPv4 adddress in + * 'a.b.c.d' form. + */ +int +string_is_valid_ipv4_address(const char *string) +{ + struct sockaddr_in sockaddr; + + return (tor_inet_pton(AF_INET,string,&sockaddr) == 1); +} + +/** Return true iff <b>string</b> matches a pattern of DNS names + * that we allow Tor clients to connect to. */ int string_is_valid_hostname(const char *string) @@ -988,6 +999,10 @@ string_is_valid_hostname(const char *string)
} SMARTLIST_FOREACH_END(c);
+ SMARTLIST_FOREACH_BEGIN(components, char *, c) { + tor_free(c); + } SMARTLIST_FOREACH_END(c); + smartlist_free(components);
return result; diff --git a/src/common/util.h b/src/common/util.h index 2634adc..dcb54f0 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -228,6 +228,7 @@ const char *find_str_at_start_of_line(const char *haystack, int string_is_C_identifier(const char *string); int string_is_key_value(int severity, const char *string); int string_is_valid_hostname(const char *string); +int string_is_valid_ipv4_address(const char *string);
int tor_mem_is_zero(const char *mem, size_t len); int tor_digest_is_zero(const char *digest); diff --git a/src/test/test_util.c b/src/test/test_util.c index fb3ce7d..fba90da 100644 --- a/src/test/test_util.c +++ b/src/test/test_util.c @@ -4138,9 +4138,9 @@ test_util_hostname_validation(void *arg) tt_assert(!string_is_valid_hostname("-torproject.org")); tt_assert(!string_is_valid_hostname("subdomain.-domain.org")); tt_assert(!string_is_valid_hostname("-subdomain.domain.org")); - + // Hostnames cannot contain non-alphanumeric characters. - tt_assert(!string_is_valid_hostname("%%domain.\org.")); + tt_assert(!string_is_valid_hostname("%%domain.\org.")); tt_assert(!string_is_valid_hostname("***x.net")); tt_assert(!string_is_valid_hostname("___abc.org")); tt_assert(!string_is_valid_hostname("\xff\xffxyz.org")); @@ -4152,6 +4152,22 @@ test_util_hostname_validation(void *arg) return; }
+static void +test_util_ipv4_validation(void *arg) +{ + (void)arg; + + tt_assert(string_is_valid_ipv4_address("192.168.0.1")); + tt_assert(string_is_valid_ipv4_address("8.8.8.8")); + + tt_assert(!string_is_valid_ipv4_address("abcd")); + tt_assert(!string_is_valid_ipv4_address("300.300.300.300")); + tt_assert(!string_is_valid_ipv4_address("8.8.")); + + done: + return; +} + struct testcase_t util_tests[] = { UTIL_LEGACY(time), UTIL_TEST(parse_http_time, 0), @@ -4225,6 +4241,7 @@ struct testcase_t util_tests[] = { &socketpair_setup, (void*)"1" }, UTIL_TEST(max_mem, 0), UTIL_TEST(hostname_validation, 0), + UTIL_TEST(ipv4_validation, 0), END_OF_TESTCASES };
tor-commits@lists.torproject.org