]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util: make hostname_is_valid() easier to read
authorLennart Poettering <lennart@poettering.net>
Sun, 23 Aug 2015 12:29:59 +0000 (14:29 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 24 Aug 2015 20:46:45 +0000 (22:46 +0200)
Add more comments, and rename some parameters and variables to be more
expressive.

src/basic/hostname-util.c
src/basic/hostname-util.h

index dc5434fcd1205da762c3c51acece82904dc3e35b..1b816fb77af6823d4febde3a2480952ca910e3f1 100644 (file)
@@ -62,16 +62,19 @@ static bool hostname_valid_char(char c) {
 }
 
 /**
- * Check if s looks like a valid host name or fqdn. This does not do
+ * Check if s looks like a valid host name or FQDN. This does not do
  * full DNS validation, but only checks if the name is composed of
  * allowed characters and the length is not above the maximum allowed
  * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
- * relax is true and at least two components are present in the name.
+ * allow_trailing_dot is true and at least two components are present
+ * in the name. Note that due to the restricted charset and length
+ * this call is substantially more conservative than
+ * dns_domain_is_valid().
  */
-bool hostname_is_valid(const char *s, bool relax) {
+bool hostname_is_valid(const char *s, bool allow_trailing_dot) {
+        unsigned n_dots = 0;
         const char *p;
         bool dot;
-        unsigned dots = 0;
 
         if (isempty(s))
                 return false;
@@ -87,7 +90,7 @@ bool hostname_is_valid(const char *s, bool relax) {
                                 return false;
 
                         dot = true;
-                        dots ++;
+                        n_dots ++;
                 } else {
                         if (!hostname_valid_char(*p))
                                 return false;
@@ -96,10 +99,12 @@ bool hostname_is_valid(const char *s, bool relax) {
                 }
         }
 
-        if (dot && (dots < 2 || !relax))
+        if (dot && (n_dots < 2 || !allow_trailing_dot))
                 return false;
 
-        if (p-s > HOST_NAME_MAX)
+        if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on
+                                  * Linux, but DNS allows domain names
+                                  * up to 255 characters */
                 return false;
 
         return true;
index a1ca94980d7f5316869d68933bb83d7766971345..7c50260d73c64b6060dcad8dff661b6f4bfde64f 100644 (file)
@@ -29,7 +29,7 @@ bool hostname_is_set(void);
 
 char* gethostname_malloc(void);
 
-bool hostname_is_valid(const char *s, bool relax) _pure_;
+bool hostname_is_valid(const char *s, bool allow_trailing_dot) _pure_;
 char* hostname_cleanup(char *s);
 
 bool is_localhost(const char *hostname);