summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
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);