Skip to content

Commit b59abc4

Browse files
committed
util: make hostname_is_valid() easier to read
Add more comments, and rename some parameters and variables to be more expressive.
1 parent 077c8c3 commit b59abc4

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/basic/hostname-util.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/basic/hostname-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ bool hostname_is_set(void);
2929

3030
char* gethostname_malloc(void);
3131

32-
bool hostname_is_valid(const char *s, bool relax) _pure_;
32+
bool hostname_is_valid(const char *s, bool allow_trailing_dot) _pure_;
3333
char* hostname_cleanup(char *s);
3434

3535
bool is_localhost(const char *hostname);

0 commit comments

Comments
 (0)