summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-11-04 00:48:25 -0500
committerNick Mathewson <nickm@torproject.org>2014-11-04 00:48:25 -0500
commit60c86a3b79d542543f191a78f5c7ea2c77c0d3f0 (patch)
treef24602f15430925d2449054d2f5fae025936d41c /src/common
parent593909ea70db9c00d3ae6d3448fb096464e0853d (diff)
parent74cbd8d55953969c15c60f025889b1e07a185a79 (diff)
downloadtor-60c86a3b79d542543f191a78f5c7ea2c77c0d3f0.tar.gz
tor-60c86a3b79d542543f191a78f5c7ea2c77c0d3f0.zip
Merge branch 'bug13315_squashed'
Conflicts: src/or/buffers.c
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.c62
-rw-r--r--src/common/util.h3
2 files changed, 65 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 27cc9df878..1359776b21 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -964,6 +964,68 @@ string_is_key_value(int severity, const char *string)
return 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 in_addr addr;
+
+ return (tor_inet_pton(AF_INET,string,&addr) == 1);
+}
+
+/** Return true if <b>string</b> represents a valid IPv6 address in
+ * a form that inet_pton() can parse.
+ */
+int
+string_is_valid_ipv6_address(const char *string)
+{
+ struct in6_addr addr;
+
+ return (tor_inet_pton(AF_INET6,string,&addr) == 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)
+{
+ int result = 1;
+ smartlist_t *components;
+
+ components = smartlist_new();
+
+ smartlist_split_string(components,string,".",0,0);
+
+ SMARTLIST_FOREACH_BEGIN(components, char *, c) {
+ if (c[0] == '-') {
+ result = 0;
+ break;
+ }
+
+ do {
+ if ((*c >= 'a' && *c <= 'z') ||
+ (*c >= 'A' && *c <= 'Z') ||
+ (*c >= '0' && *c <= '9') ||
+ (*c == '-'))
+ c++;
+ else
+ result = 0;
+ } while (result && *c);
+
+ } SMARTLIST_FOREACH_END(c);
+
+ SMARTLIST_FOREACH_BEGIN(components, char *, c) {
+ tor_free(c);
+ } SMARTLIST_FOREACH_END(c);
+
+ smartlist_free(components);
+
+ return result;
+}
+
/** Return true iff the DIGEST256_LEN bytes in digest are all zero. */
int
tor_digest256_is_zero(const char *digest)
diff --git a/src/common/util.h b/src/common/util.h
index d7feb6e925..9886b2db6a 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -227,6 +227,9 @@ const char *find_str_at_start_of_line(const char *haystack,
const char *needle);
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 string_is_valid_ipv6_address(const char *string);
int tor_mem_is_zero(const char *mem, size_t len);
int tor_digest_is_zero(const char *digest);