mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-10 21:23:58 +01:00
Introducing helper function to validate IPv4 address strings.
This commit is contained in:
parent
1ea9a6fd72
commit
e8e45ff13e
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user