summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorrl1987 <rl1987@sdf.lonestar.org>2014-10-12 19:33:08 +0300
committerNick Mathewson <nickm@torproject.org>2014-11-04 00:35:43 -0500
commit1ea9a6fd72b66ec634446cbd2119641a5ed1e703 (patch)
tree5f8ab6a546218e39d4843e954452e79d428a10c4 /src/common
parentf94e5f2e5212034cb8b2666716eeaa61e874065b (diff)
downloadtor-1ea9a6fd72b66ec634446cbd2119641a5ed1e703.tar.gz
tor-1ea9a6fd72b66ec634446cbd2119641a5ed1e703.zip
Introducing helper function to validate DNS name strings.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.c36
-rw-r--r--src/common/util.h1
2 files changed, 37 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c
index f4d293c838..606b9e1ea0 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -957,6 +957,42 @@ 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.
+ */
+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_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 61bb05f016..2634adc62b 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -227,6 +227,7 @@ 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 tor_mem_is_zero(const char *mem, size_t len);
int tor_digest_is_zero(const char *digest);