@@ -62,16 +62,19 @@ static bool hostname_valid_char(char c) {
6262}
6363
6464/**
65- * Check if s looks like a valid host name or fqdn . This does not do
65+ * Check if s looks like a valid host name or FQDN . This does not do
6666 * full DNS validation, but only checks if the name is composed of
6767 * allowed characters and the length is not above the maximum allowed
6868 * by Linux (c.f. dns_name_is_valid()). Trailing dot is allowed if
69- * relax is true and at least two components are present in the name.
69+ * allow_trailing_dot is true and at least two components are present
70+ * in the name. Note that due to the restricted charset and length
71+ * this call is substantially more conservative than
72+ * dns_domain_is_valid().
7073 */
71- bool hostname_is_valid (const char * s , bool relax ) {
74+ bool hostname_is_valid (const char * s , bool allow_trailing_dot ) {
75+ unsigned n_dots = 0 ;
7276 const char * p ;
7377 bool dot ;
74- unsigned dots = 0 ;
7578
7679 if (isempty (s ))
7780 return false;
@@ -87,7 +90,7 @@ bool hostname_is_valid(const char *s, bool relax) {
8790 return false;
8891
8992 dot = true;
90- dots ++ ;
93+ n_dots ++ ;
9194 } else {
9295 if (!hostname_valid_char (* p ))
9396 return false;
@@ -96,10 +99,12 @@ bool hostname_is_valid(const char *s, bool relax) {
9699 }
97100 }
98101
99- if (dot && (dots < 2 || !relax ))
102+ if (dot && (n_dots < 2 || !allow_trailing_dot ))
100103 return false;
101104
102- if (p - s > HOST_NAME_MAX )
105+ if (p - s > HOST_NAME_MAX ) /* Note that HOST_NAME_MAX is 64 on
106+ * Linux, but DNS allows domain names
107+ * up to 255 characters */
103108 return false;
104109
105110 return true;
0 commit comments