HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/dns-rr.c
Git blob: 26d2e3934a0bb04bd375985dfea1ee4f397cf9a2
Lines 1–1000 of 2,688
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "alloc-util.h"
#include "bitmap.h"
#include "dns-answer.h" /* IWYU pragma: keep */
#include "dns-domain.h"
#include "dns-packet.h"
#include "dns-rr.h"
#include "dns-type.h"
#include "escape.h"
#include "hash-funcs.h"
#include "hexdecoct.h"
#include "json-util.h"
#include "math-util.h"
#include "memory-util.h"
#include "siphash24.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "time-util.h"
#include "unaligned.h"
DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
DnsResourceKey *k;
size_t l;
assert(name);
l = strlen(name);
k = malloc0(sizeof(DnsResourceKey) + l + 1);
if (!k)
return NULL;
k->n_ref = 1;
k->class = class;
k->type = type;
strcpy((char*) k + sizeof(DnsResourceKey), name);
return k;
}
DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname) {
int r;
assert(key);
assert(cname);
assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
if (cname->key->type == DNS_TYPE_CNAME)
return dns_resource_key_new(key->class, key->type, cname->cname.name);
else {
_cleanup_free_ char *destination = NULL;
DnsResourceKey *k;
r = dns_name_change_suffix(dns_resource_key_name(key), dns_resource_key_name(cname->key), cname->dname.name, &destination);
if (r < 0)
return NULL;
if (r == 0)
return dns_resource_key_ref((DnsResourceKey*) key);
k = dns_resource_key_new_consume(key->class, key->type, destination);
if (!k)
return NULL;
TAKE_PTR(destination);
return k;
}
}
int dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name) {
DnsResourceKey *new_key;
char *joined;
int r;
assert(ret);
assert(key);
assert(name);
if (dns_name_is_root(name)) {
*ret = dns_resource_key_ref(key);
return 0;
}
r = dns_name_concat(dns_resource_key_name(key), name, 0, &joined);
if (r < 0)
return r;
new_key = dns_resource_key_new_consume(key->class, key->type, joined);
if (!new_key) {
free(joined);
return -ENOMEM;
}
*ret = new_key;
return 0;
}
DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
DnsResourceKey *k;
assert(name);
k = new(DnsResourceKey, 1);
if (!k)
return NULL;
*k = (DnsResourceKey) {
.n_ref = 1,
.class = class,
.type = type,
._name = name,
};
return k;
}
DnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
if (!k)
return NULL;
/* Static/const keys created with DNS_RESOURCE_KEY_CONST will
* set this to -1, they should not be reffed/unreffed */
assert(k->n_ref != UINT_MAX);
assert(k->n_ref > 0);
k->n_ref++;
return k;
}
DnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
if (!k)
return NULL;
assert(k->n_ref != UINT_MAX);
assert(k->n_ref > 0);
if (k->n_ref == 1) {
free(k->_name);
free(k);
} else
k->n_ref--;
return NULL;
}
const char* dns_resource_key_name(const DnsResourceKey *key) {
const char *name;
if (!key)
return NULL;
if (key->_name)
name = key->_name;
else
name = (char*) key + sizeof(DnsResourceKey);
if (dns_name_is_root(name))
return ".";
else
return name;
}
bool dns_resource_key_is_address(const DnsResourceKey *key) {
assert(key);
/* Check if this is an A or AAAA resource key */
return key->class == DNS_CLASS_IN && IN_SET(key->type, DNS_TYPE_A, DNS_TYPE_AAAA);
}
bool dns_resource_key_is_dnssd_ptr(const DnsResourceKey *key) {
assert(key);
/* Check if this is a PTR resource key used in
Service Instance Enumeration as described in RFC6763 p4.1. */
if (key->type != DNS_TYPE_PTR)
return false;
return dns_name_endswith(dns_resource_key_name(key), "_tcp.local") ||
dns_name_endswith(dns_resource_key_name(key), "_udp.local");
}
bool dns_resource_key_is_dnssd_two_label_ptr(const DnsResourceKey *key) {
assert(key);
/* Check if this is a PTR resource key used in Service Instance
* Enumeration as described in RFC6763 § 4.1, excluding selective
* service names described in RFC6763 § 7.1. */
if (key->type != DNS_TYPE_PTR)
return false;
const char *name = dns_resource_key_name(key);
if (dns_name_parent(&name) <= 0)
return false;
return dns_name_equal(name, "_tcp.local") > 0 || dns_name_equal(name, "_udp.local") > 0;
}
int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
int r;
if (a == b)
return 1;
r = dns_name_equal(dns_resource_key_name(a), dns_resource_key_name(b));
if (r <= 0)
return r;
if (a->class != b->class)
return 0;
if (a->type != b->type)
return 0;
return 1;
}
int dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain) {
int r;
assert(key);
assert(rr);
if (key == rr->key)
return 1;
/* Checks if an rr matches the specified key. If a search
* domain is specified, it will also be checked if the key
* with the search domain suffixed might match the RR. */
if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
return 0;
if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
return 0;
r = dns_name_equal(dns_resource_key_name(rr->key), dns_resource_key_name(key));
if (r != 0)
return r;
if (search_domain) {
_cleanup_free_ char *joined = NULL;
r = dns_name_concat(dns_resource_key_name(key), search_domain, 0, &joined);
if (r < 0)
return r;
return dns_name_equal(dns_resource_key_name(rr->key), joined);
}
return 0;
}
int dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain) {
int r;
assert(key);
assert(cname);
if (cname->class != key->class && key->class != DNS_CLASS_ANY)
return 0;
if (!dns_type_may_redirect(key->type))
return 0;
if (cname->type == DNS_TYPE_CNAME)
r = dns_name_equal(dns_resource_key_name(key), dns_resource_key_name(cname));
else if (cname->type == DNS_TYPE_DNAME)
r = dns_name_endswith(dns_resource_key_name(key), dns_resource_key_name(cname));
else
return 0;
if (r != 0)
return r;
if (search_domain) {
_cleanup_free_ char *joined = NULL;
r = dns_name_concat(dns_resource_key_name(key), search_domain, 0, &joined);
if (r < 0)
return r;
if (cname->type == DNS_TYPE_CNAME)
return dns_name_equal(joined, dns_resource_key_name(cname));
else if (cname->type == DNS_TYPE_DNAME)
return dns_name_endswith(joined, dns_resource_key_name(cname));
}
return 0;
}
int dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa) {
assert(soa);
assert(key);
/* Checks whether 'soa' is a SOA record for the specified key. */
if (soa->class != key->class)
return 0;
if (soa->type != DNS_TYPE_SOA)
return 0;
return dns_name_endswith(dns_resource_key_name(key), dns_resource_key_name(soa));
}
void dns_resource_key_hash_func(const DnsResourceKey *k, struct siphash *state) {
assert(k);
dns_name_hash_func(dns_resource_key_name(k), state);
siphash24_compress_typesafe(k->class, state);
siphash24_compress_typesafe(k->type, state);
}
int dns_resource_key_compare_func(const DnsResourceKey *x, const DnsResourceKey *y) {
int r;
r = dns_name_compare_func(dns_resource_key_name(x), dns_resource_key_name(y));
if (r != 0)
return r;
r = CMP(x->type, y->type);
if (r != 0)
return r;
return CMP(x->class, y->class);
}
DEFINE_HASH_OPS(dns_resource_key_hash_ops, DnsResourceKey, dns_resource_key_hash_func, dns_resource_key_compare_func);
char* dns_resource_key_to_string(const DnsResourceKey *key, char *buf, size_t buf_size) {
const char *c, *t;
char *ans = buf;
/* If we cannot convert the CLASS/TYPE into a known string,
use the format recommended by RFC 3597, Section 5. */
c = dns_class_to_string(key->class);
t = dns_type_to_string(key->type);
(void) snprintf(buf, buf_size, "%s %s%s%.0u %s%s%.0u",
dns_resource_key_name(key),
strempty(c), c ? "" : "CLASS", c ? 0u : key->class,
strempty(t), t ? "" : "TYPE", t ? 0u : key->type);
return ans;
}
bool dns_resource_key_reduce(DnsResourceKey **a, DnsResourceKey **b) {
assert(a);
assert(b);
/* Try to replace one RR key by another if they are identical, thus saving a bit of memory. Note that we do
* this only for RR keys, not for RRs themselves, as they carry a lot of additional metadata (where they come
* from, validity data, and suchlike), and cannot be replaced so easily by other RRs that have the same
* superficial data. */
if (!*a)
return false;
if (!*b)
return false;
/* We refuse merging const keys */
if ((*a)->n_ref == UINT_MAX)
return false;
if ((*b)->n_ref == UINT_MAX)
return false;
/* Already the same? */
if (*a == *b)
return true;
/* Are they really identical? */
if (dns_resource_key_equal(*a, *b) <= 0)
return false;
/* Keep the one which already has more references. */
if ((*a)->n_ref > (*b)->n_ref)
DNS_RESOURCE_KEY_REPLACE(*b, dns_resource_key_ref(*a));
else
DNS_RESOURCE_KEY_REPLACE(*a, dns_resource_key_ref(*b));
return true;
}
DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
DnsResourceRecord *rr;
rr = new(DnsResourceRecord, 1);
if (!rr)
return NULL;
*rr = (DnsResourceRecord) {
.n_ref = 1,
.key = dns_resource_key_ref(key),
.expiry = USEC_INFINITY,
.n_skip_labels_signer = UINT8_MAX,
.n_skip_labels_source = UINT8_MAX,
};
return rr;
}
DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
key = dns_resource_key_new(class, type, name);
if (!key)
return NULL;
return dns_resource_record_new(key);
}
static DnsResourceRecord* dns_resource_record_free(DnsResourceRecord *rr) {
assert(rr);
if (rr->key) {
switch (rr->key->type) {
case DNS_TYPE_SRV:
free(rr->srv.name);
break;
case DNS_TYPE_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME:
free(rr->ptr.name);
break;
case DNS_TYPE_HINFO:
free(rr->hinfo.cpu);
free(rr->hinfo.os);
break;
case DNS_TYPE_TXT:
case DNS_TYPE_SPF:
dns_txt_item_free_all(rr->txt.items);
break;
case DNS_TYPE_SOA:
free(rr->soa.mname);
free(rr->soa.rname);
break;
case DNS_TYPE_MX:
free(rr->mx.exchange);
break;
case DNS_TYPE_DS:
free(rr->ds.digest);
break;
case DNS_TYPE_SSHFP:
free(rr->sshfp.fingerprint);
break;
case DNS_TYPE_DNSKEY:
free(rr->dnskey.key);
break;
case DNS_TYPE_RRSIG:
free(rr->rrsig.signer);
free(rr->rrsig.signature);
break;
case DNS_TYPE_NSEC:
free(rr->nsec.next_domain_name);
bitmap_free(rr->nsec.types);
break;
case DNS_TYPE_NSEC3:
free(rr->nsec3.next_hashed_name);
free(rr->nsec3.salt);
bitmap_free(rr->nsec3.types);
break;
case DNS_TYPE_LOC:
case DNS_TYPE_A:
case DNS_TYPE_AAAA:
break;
case DNS_TYPE_TLSA:
free(rr->tlsa.data);
break;
case DNS_TYPE_SVCB:
case DNS_TYPE_HTTPS:
free(rr->svcb.target_name);
dns_svc_param_free_all(rr->svcb.params);
break;
case DNS_TYPE_CAA:
free(rr->caa.tag);
free(rr->caa.value);
break;
case DNS_TYPE_NAPTR:
free(rr->naptr.flags);
free(rr->naptr.services);
free(rr->naptr.regexp);
free(rr->naptr.replacement);
break;
case DNS_TYPE_OPENPGPKEY:
default:
if (!rr->unparsable)
free(rr->generic.data);
}
if (rr->unparsable)
free(rr->generic.data);
free(rr->wire_format);
dns_resource_key_unref(rr->key);
}
free(rr->to_string);
return mfree(rr);
}
DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsResourceRecord, dns_resource_record, dns_resource_record_free);
int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
_cleanup_free_ char *ptr = NULL;
int r;
assert(ret);
assert(address);
assert(hostname);
r = dns_name_reverse(family, address, &ptr);
if (r < 0)
return r;
key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
if (!key)
return -ENOMEM;
ptr = NULL;
rr = dns_resource_record_new(key);
if (!rr)
return -ENOMEM;
rr->ptr.name = strdup(hostname);
if (!rr->ptr.name)
return -ENOMEM;
*ret = TAKE_PTR(rr);
return 0;
}
int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name) {
DnsResourceRecord *rr;
assert(ret);
assert(address);
assert(family);
if (family == AF_INET) {
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, name);
if (!rr)
return -ENOMEM;
rr->a.in_addr = address->in;
} else if (family == AF_INET6) {
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
if (!rr)
return -ENOMEM;
rr->aaaa.in6_addr = address->in6;
} else
return -EAFNOSUPPORT;
*ret = rr;
return 0;
}
#define FIELD_EQUAL(a, b, field) \
((a).field ## _size == (b).field ## _size && \
memcmp_safe((a).field, (b).field, (a).field ## _size) == 0)
int dns_resource_record_payload_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
int r;
/* Check if a and b are the same, but don't look at their keys */
if (a->unparsable != b->unparsable)
return 0;
switch (a->unparsable ? _DNS_TYPE_INVALID : a->key->type) {
case DNS_TYPE_SRV:
r = dns_name_equal(a->srv.name, b->srv.name);
if (r <= 0)
return r;
return a->srv.priority == b->srv.priority &&
a->srv.weight == b->srv.weight &&
a->srv.port == b->srv.port;
case DNS_TYPE_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME:
return dns_name_equal(a->ptr.name, b->ptr.name);
case DNS_TYPE_HINFO:
return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
strcaseeq(a->hinfo.os, b->hinfo.os);
case DNS_TYPE_SPF: /* exactly the same as TXT */
case DNS_TYPE_TXT:
return dns_txt_item_equal(a->txt.items, b->txt.items);
case DNS_TYPE_A:
return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
case DNS_TYPE_AAAA:
return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
case DNS_TYPE_SOA:
r = dns_name_equal(a->soa.mname, b->soa.mname);
if (r <= 0)
return r;
r = dns_name_equal(a->soa.rname, b->soa.rname);
if (r <= 0)
return r;
return a->soa.serial == b->soa.serial &&
a->soa.refresh == b->soa.refresh &&
a->soa.retry == b->soa.retry &&
a->soa.expire == b->soa.expire &&
a->soa.minimum == b->soa.minimum;
case DNS_TYPE_MX:
if (a->mx.priority != b->mx.priority)
return 0;
return dns_name_equal(a->mx.exchange, b->mx.exchange);
case DNS_TYPE_LOC:
assert(a->loc.version == b->loc.version);
return a->loc.size == b->loc.size &&
a->loc.horiz_pre == b->loc.horiz_pre &&
a->loc.vert_pre == b->loc.vert_pre &&
a->loc.latitude == b->loc.latitude &&
a->loc.longitude == b->loc.longitude &&
a->loc.altitude == b->loc.altitude;
case DNS_TYPE_DS:
return a->ds.key_tag == b->ds.key_tag &&
a->ds.algorithm == b->ds.algorithm &&
a->ds.digest_type == b->ds.digest_type &&
FIELD_EQUAL(a->ds, b->ds, digest);
case DNS_TYPE_SSHFP:
return a->sshfp.algorithm == b->sshfp.algorithm &&
a->sshfp.fptype == b->sshfp.fptype &&
FIELD_EQUAL(a->sshfp, b->sshfp, fingerprint);
case DNS_TYPE_DNSKEY:
return a->dnskey.flags == b->dnskey.flags &&
a->dnskey.protocol == b->dnskey.protocol &&
a->dnskey.algorithm == b->dnskey.algorithm &&
FIELD_EQUAL(a->dnskey, b->dnskey, key);
case DNS_TYPE_RRSIG:
/* do the fast comparisons first */
if (!(a->rrsig.type_covered == b->rrsig.type_covered &&
a->rrsig.algorithm == b->rrsig.algorithm &&
a->rrsig.labels == b->rrsig.labels &&
a->rrsig.original_ttl == b->rrsig.original_ttl &&
a->rrsig.expiration == b->rrsig.expiration &&
a->rrsig.inception == b->rrsig.inception &&
a->rrsig.key_tag == b->rrsig.key_tag &&
FIELD_EQUAL(a->rrsig, b->rrsig, signature)))
return false;
return dns_name_equal(a->rrsig.signer, b->rrsig.signer);
case DNS_TYPE_NSEC:
r = dns_name_equal(a->nsec.next_domain_name, b->nsec.next_domain_name);
if (r <= 0)
return r;
return bitmap_equal(a->nsec.types, b->nsec.types);
case DNS_TYPE_NSEC3:
return a->nsec3.algorithm == b->nsec3.algorithm &&
a->nsec3.flags == b->nsec3.flags &&
a->nsec3.iterations == b->nsec3.iterations &&
FIELD_EQUAL(a->nsec3, b->nsec3, salt) &&
FIELD_EQUAL(a->nsec3, b->nsec3, next_hashed_name) &&
bitmap_equal(a->nsec3.types, b->nsec3.types);
case DNS_TYPE_TLSA:
return a->tlsa.cert_usage == b->tlsa.cert_usage &&
a->tlsa.selector == b->tlsa.selector &&
a->tlsa.matching_type == b->tlsa.matching_type &&
FIELD_EQUAL(a->tlsa, b->tlsa, data);
case DNS_TYPE_SVCB:
case DNS_TYPE_HTTPS:
if (!(a->svcb.priority == b->svcb.priority &&
dns_svc_params_equal(a->svcb.params, b->svcb.params)))
return false;
return dns_name_equal(a->svcb.target_name, b->svcb.target_name);
case DNS_TYPE_CAA:
return a->caa.flags == b->caa.flags &&
streq(a->caa.tag, b->caa.tag) &&
FIELD_EQUAL(a->caa, b->caa, value);
case DNS_TYPE_NAPTR:
r = dns_name_equal(a->naptr.replacement, b->naptr.replacement);
if (r <= 0)
return r;
return a->naptr.order == b->naptr.order &&
a->naptr.preference == b->naptr.preference &&
streq(a->naptr.flags, b->naptr.flags) &&
streq(a->naptr.services, b->naptr.services) &&
streq(a->naptr.regexp, b->naptr.regexp);
case DNS_TYPE_OPENPGPKEY:
default:
return FIELD_EQUAL(a->generic, b->generic, data);
}
}
int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
int r;
assert(a);
assert(b);
if (a == b)
return 1;
r = dns_resource_key_equal(a->key, b->key);
if (r <= 0)
return r;
return dns_resource_record_payload_equal(a, b);
}
static char* format_location(uint32_t latitude, uint32_t longitude, uint32_t altitude,
uint8_t size, uint8_t horiz_pre, uint8_t vert_pre) {
char *s;
char NS = latitude >= 1U<<31 ? 'N' : 'S';
char EW = longitude >= 1U<<31 ? 'E' : 'W';
int lat = latitude >= 1U<<31 ? (int) (latitude - (1U<<31)) : (int) ((1U<<31) - latitude);
int lon = longitude >= 1U<<31 ? (int) (longitude - (1U<<31)) : (int) ((1U<<31) - longitude);
double alt = altitude >= 10000000u ? altitude - 10000000u : -(double)(10000000u - altitude);
double siz = (size >> 4) * xexp10i(size & 0xF);
double hor = (horiz_pre >> 4) * xexp10i(horiz_pre & 0xF);
double ver = (vert_pre >> 4) * xexp10i(vert_pre & 0xF);
if (asprintf(&s, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
(lat / 60000 / 60),
(lat / 60000) % 60,
(lat % 60000) / 1000.,
NS,
(lon / 60000 / 60),
(lon / 60000) % 60,
(lon % 60000) / 1000.,
EW,
alt / 100.,
siz / 100.,
hor / 100.,
ver / 100.) < 0)
return NULL;
return s;
}
static int format_timestamp_dns(char *buf, size_t l, time_t sec) {
struct tm tm;
int r;
assert(buf);
assert(l > STRLEN("YYYYMMDDHHmmSS"));
r = localtime_or_gmtime_usec(sec * USEC_PER_SEC, /* utc= */ true, &tm);
if (r < 0)
return r;
if (strftime(buf, l, "%Y%m%d%H%M%S", &tm) <= 0)
return -EINVAL;
return 0;
}
static char *format_types(Bitmap *types) {
_cleanup_strv_free_ char **strv = NULL;
_cleanup_free_ char *str = NULL;
unsigned type;
int r;
BITMAP_FOREACH(type, types) {
if (dns_type_to_string(type))
r = strv_extend(&strv, dns_type_to_string(type));
else
r = strv_extendf(&strv, "TYPE%u", type);
if (r < 0)
return NULL;
}
str = strv_join(strv, " ");
if (!str)
return NULL;
return strjoin("( ", str, " )");
}
static char *format_txt(DnsTxtItem *first) {
size_t c = 1;
char *p, *s;
LIST_FOREACH(items, i, first)
c += i->length * 4 + 3;
p = s = new(char, c);
if (!s)
return NULL;
LIST_FOREACH(items, i, first) {
if (i != first)
*(p++) = ' ';
*(p++) = '"';
for (size_t j = 0; j < i->length; j++) {
if (i->data[j] < ' ' || i->data[j] == '"' || i->data[j] >= 127) {
*(p++) = '\\';
*(p++) = '0' + (i->data[j] / 100);
*(p++) = '0' + ((i->data[j] / 10) % 10);
*(p++) = '0' + (i->data[j] % 10);
} else
*(p++) = i->data[j];
}
*(p++) = '"';
}
*p = 0;
return s;
}
static char *format_svc_param_value(DnsSvcParam *i) {
_cleanup_free_ char *value = NULL;
assert(i);
switch (i->key) {
case DNS_SVC_PARAM_KEY_ALPN: {
size_t offset = 0;
_cleanup_strv_free_ char **values_strv = NULL;
while (offset < i->length) {
size_t sz = i->value[offset++];
char *alpn = cescape_length((char *)&i->value[offset], sz);
if (!alpn)
return NULL;
if (strv_push(&values_strv, alpn) < 0)
return NULL;
offset += sz;
}
value = strv_join(values_strv, ",");
if (!value)
return NULL;
break;
}
case DNS_SVC_PARAM_KEY_PORT: {
uint16_t port = unaligned_read_be16(i->value);
if (asprintf(&value, "%" PRIu16, port) < 0)
return NULL;
return TAKE_PTR(value);
}
case DNS_SVC_PARAM_KEY_IPV4HINT: {
const struct in_addr *addrs = i->value_in_addr;
_cleanup_strv_free_ char **values_strv = NULL;
for (size_t n = 0; n < i->length / sizeof (struct in_addr); n++) {
char *addr;
if (in_addr_to_string(AF_INET, (const union in_addr_union*) &addrs[n], &addr) < 0)
return NULL;
if (strv_push(&values_strv, addr) < 0)
return NULL;
}
return strv_join(values_strv, ",");
}
case DNS_SVC_PARAM_KEY_IPV6HINT: {
const struct in6_addr *addrs = i->value_in6_addr;
_cleanup_strv_free_ char **values_strv = NULL;
for (size_t n = 0; n < i->length / sizeof (struct in6_addr); n++) {
char *addr;
if (in_addr_to_string(AF_INET6, (const union in_addr_union*) &addrs[n], &addr) < 0)
return NULL;
if (strv_push(&values_strv, addr) < 0)
return NULL;
}
return strv_join(values_strv, ",");
}
default:
value = decescape((char*) &i->value, i->length, " ,");
if (!value)
return NULL;
}
char *qvalue;
if (asprintf(&qvalue, "\"%s\"", value) < 0)
return NULL;
return qvalue;
}
static char *format_svc_param(DnsSvcParam *i) {
const char *key = FORMAT_DNS_SVC_PARAM_KEY(i->key);
_cleanup_free_ char *value = NULL;
assert(i);
if (i->length == 0)
return strdup(key);
value = format_svc_param_value(i);
if (!value)
return NULL;
return strjoin(key, "=", value);
}
static char *format_svc_params(DnsSvcParam *first) {
_cleanup_strv_free_ char **params = NULL;
LIST_FOREACH(params, i, first) {
char *param = format_svc_param(i);
if (!param)
return NULL;
if (strv_push(¶ms, param) < 0)
return NULL;
}
return strv_join(params, " ");
}
uint16_t dnssec_keytag(DnsResourceRecord *dnskey, bool mask_revoke) {
const uint8_t *p;
uint32_t sum, f;
/* The algorithm from RFC 4034, Appendix B. */
assert(dnskey);
assert(dnskey->key->type == DNS_TYPE_DNSKEY);
f = (uint32_t) dnskey->dnskey.flags;
if (mask_revoke)
f &= ~DNSKEY_FLAG_REVOKE;
sum = f + ((((uint32_t) dnskey->dnskey.protocol) << 8) + (uint32_t) dnskey->dnskey.algorithm);
p = dnskey->dnskey.key;
for (size_t i = 0; i < dnskey->dnskey.key_size; i++)
sum += (i & 1) == 0 ? (uint32_t) p[i] << 8 : (uint32_t) p[i];
sum += (sum >> 16) & UINT32_C(0xFFFF);
return sum & UINT32_C(0xFFFF);
}
const char* dns_resource_record_to_string(DnsResourceRecord *rr) {
_cleanup_free_ char *s = NULL, *t = NULL;
char k[DNS_RESOURCE_KEY_STRING_MAX];
int r;
assert(rr);