dhcp6: use dns_name_from_wire_format
Convert some of the option parsing to use dns_name_from_wire_format, introduced earlier. No change in behavior intended.
This commit is contained in:
@@ -820,74 +820,6 @@ int dhcp6_option_parse_addresses(
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parse_domain(const uint8_t **data, size_t *len, char **ret) {
|
|
||||||
_cleanup_free_ char *domain = NULL;
|
|
||||||
const uint8_t *optval;
|
|
||||||
size_t optlen, n = 0;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
assert(data);
|
|
||||||
assert(len);
|
|
||||||
assert(*data || *len == 0);
|
|
||||||
assert(ret);
|
|
||||||
|
|
||||||
optval = *data;
|
|
||||||
optlen = *len;
|
|
||||||
|
|
||||||
if (optlen <= 1)
|
|
||||||
return -ENODATA;
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
const char *label;
|
|
||||||
uint8_t c;
|
|
||||||
|
|
||||||
if (optlen == 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
c = *optval;
|
|
||||||
optval++;
|
|
||||||
optlen--;
|
|
||||||
|
|
||||||
if (c == 0)
|
|
||||||
/* End label */
|
|
||||||
break;
|
|
||||||
if (c > 63)
|
|
||||||
return -EBADMSG;
|
|
||||||
if (c > optlen)
|
|
||||||
return -EMSGSIZE;
|
|
||||||
|
|
||||||
/* Literal label */
|
|
||||||
label = (const char*) optval;
|
|
||||||
optval += c;
|
|
||||||
optlen -= c;
|
|
||||||
|
|
||||||
if (!GREEDY_REALLOC(domain, n + (n != 0) + DNS_LABEL_ESCAPED_MAX))
|
|
||||||
return -ENOMEM;
|
|
||||||
|
|
||||||
if (n != 0)
|
|
||||||
domain[n++] = '.';
|
|
||||||
|
|
||||||
r = dns_label_escape(label, c, domain + n, DNS_LABEL_ESCAPED_MAX);
|
|
||||||
if (r < 0)
|
|
||||||
return r;
|
|
||||||
|
|
||||||
n += r;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n > 0) {
|
|
||||||
if (!GREEDY_REALLOC(domain, n + 1))
|
|
||||||
return -ENOMEM;
|
|
||||||
|
|
||||||
domain[n] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
*ret = TAKE_PTR(domain);
|
|
||||||
*data = optval;
|
|
||||||
*len = optlen;
|
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
int dhcp6_option_parse_domainname(const uint8_t *optval, size_t optlen, char **ret) {
|
int dhcp6_option_parse_domainname(const uint8_t *optval, size_t optlen, char **ret) {
|
||||||
_cleanup_free_ char *domain = NULL;
|
_cleanup_free_ char *domain = NULL;
|
||||||
int r;
|
int r;
|
||||||
@@ -895,7 +827,7 @@ int dhcp6_option_parse_domainname(const uint8_t *optval, size_t optlen, char **r
|
|||||||
assert(optval || optlen == 0);
|
assert(optval || optlen == 0);
|
||||||
assert(ret);
|
assert(ret);
|
||||||
|
|
||||||
r = parse_domain(&optval, &optlen, &domain);
|
r = dns_name_from_wire_format(&optval, &optlen, &domain);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
if (r == 0)
|
if (r == 0)
|
||||||
@@ -922,11 +854,11 @@ int dhcp6_option_parse_domainname_list(const uint8_t *optval, size_t optlen, cha
|
|||||||
while (optlen > 0) {
|
while (optlen > 0) {
|
||||||
_cleanup_free_ char *name = NULL;
|
_cleanup_free_ char *name = NULL;
|
||||||
|
|
||||||
r = parse_domain(&optval, &optlen, &name);
|
r = dns_name_from_wire_format(&optval, &optlen, &name);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
if (r == 0)
|
if (dns_name_is_root(name)) /* root domain */
|
||||||
continue;
|
return -EBADMSG;
|
||||||
|
|
||||||
r = strv_consume(&names, TAKE_PTR(name));
|
r = strv_consume(&names, TAKE_PTR(name));
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
|
|||||||
@@ -165,10 +165,7 @@ TEST(parse_domain) {
|
|||||||
domain = mfree(domain);
|
domain = mfree(domain);
|
||||||
|
|
||||||
data = (uint8_t []) { 4, 't', 'e', 's', 't' };
|
data = (uint8_t []) { 4, 't', 'e', 's', 't' };
|
||||||
assert_se(dhcp6_option_parse_domainname(data, 5, &domain) >= 0);
|
assert_se(dhcp6_option_parse_domainname(data, 5, &domain) < 0);
|
||||||
assert_se(domain);
|
|
||||||
assert_se(streq(domain, "test"));
|
|
||||||
domain = mfree(domain);
|
|
||||||
|
|
||||||
data = (uint8_t []) { 0 };
|
data = (uint8_t []) { 0 };
|
||||||
assert_se(dhcp6_option_parse_domainname(data, 1, &domain) < 0);
|
assert_se(dhcp6_option_parse_domainname(data, 1, &domain) < 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user