dns-domain: simplify dns_name_is_root() and dns_name_is_single_label()
Let's change the return value to bool. If we encounter an error while parsing, return "false" instead of the actual parsing error, after all the specified hostname does not qualify for what the function is supposed to test. Dealing with the additional error codes was always cumbersome, and easily misused, like for example in the DHCP code. Let's also rename the functions from dns_name_root() to dns_name_is_root(), to indicate that this function checks something and returns a bool. Similar for dns_name_is_signal_label().
This commit is contained in:
+9
-17
@@ -29,6 +29,7 @@
|
||||
#include "hexdecoct.h"
|
||||
#include "parse-util.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
#include "utf8.h"
|
||||
|
||||
int dns_label_unescape(const char **name, char *dest, size_t sz) {
|
||||
@@ -752,36 +753,27 @@ int dns_name_address(const char *p, int *family, union in_addr_union *address) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dns_name_root(const char *name) {
|
||||
char label[DNS_LABEL_MAX+1];
|
||||
int r;
|
||||
bool dns_name_is_root(const char *name) {
|
||||
|
||||
assert(name);
|
||||
|
||||
r = dns_label_unescape(&name, label, sizeof(label));
|
||||
if (r < 0)
|
||||
return r;
|
||||
/* There are exactly two ways to encode the root domain name:
|
||||
* as empty string, or with a single dot. */
|
||||
|
||||
return r == 0 && *name == 0;
|
||||
return STR_IN_SET(name, "", ".");
|
||||
}
|
||||
|
||||
int dns_name_single_label(const char *name) {
|
||||
bool dns_name_is_single_label(const char *name) {
|
||||
char label[DNS_LABEL_MAX+1];
|
||||
int r;
|
||||
|
||||
assert(name);
|
||||
|
||||
r = dns_label_unescape(&name, label, sizeof(label));
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
return 0;
|
||||
if (r <= 0)
|
||||
return false;
|
||||
|
||||
r = dns_label_unescape(&name, label, sizeof(label));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return r == 0 && *name == 0;
|
||||
return dns_name_is_root(name);
|
||||
}
|
||||
|
||||
/* Encode a domain name according to RFC 1035 Section 3.1 */
|
||||
|
||||
@@ -67,8 +67,8 @@ int dns_name_change_suffix(const char *name, const char *old_suffix, const char
|
||||
int dns_name_reverse(int family, const union in_addr_union *a, char **ret);
|
||||
int dns_name_address(const char *p, int *family, union in_addr_union *a);
|
||||
|
||||
int dns_name_root(const char *name);
|
||||
int dns_name_single_label(const char *name);
|
||||
bool dns_name_is_root(const char *name);
|
||||
bool dns_name_is_single_label(const char *name);
|
||||
|
||||
int dns_name_to_wire_format(const char *domain, uint8_t *buffer, size_t len);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user