1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "resolved-dns-domain.h"
23 #include "resolved-dns-rr.h"
25 DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
32 k = malloc0(sizeof(DnsResourceKey) + l + 1);
40 strcpy((char*) k + sizeof(DnsResourceKey), name);
45 DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
50 k = new0(DnsResourceKey, 1);
62 DnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
73 DnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
88 int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
91 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
95 if (a->class != b->class)
98 if (a->type != b->type)
104 int dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr) {
108 if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
111 if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
114 return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
117 int dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr) {
121 if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
124 if (rr->key->type != DNS_TYPE_CNAME)
127 return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
130 unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
131 const DnsResourceKey *k = i;
134 ul = dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), hash_key);
135 ul = ul * hash_key[0] + ul + k->class;
136 ul = ul * hash_key[1] + ul + k->type;
141 int dns_resource_key_compare_func(const void *a, const void *b) {
142 const DnsResourceKey *x = a, *y = b;
145 ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
149 if (x->type < y->type)
151 if (x->type > y->type)
154 if (x->class < y->class)
156 if (x->class > y->class)
162 int dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
163 char cbuf[DECIMAL_STR_MAX(uint16_t)], tbuf[DECIMAL_STR_MAX(uint16_t)];
167 c = dns_class_to_string(key->class);
169 sprintf(cbuf, "%i", key->class);
173 t = dns_type_to_string(key->type);
175 sprintf(tbuf, "%i", key->type);
179 s = strjoin(DNS_RESOURCE_KEY_NAME(key), " ", c, " ", t, NULL);
187 DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
188 DnsResourceRecord *rr;
190 rr = new0(DnsResourceRecord, 1);
195 rr->key = dns_resource_key_ref(key);
200 DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
201 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
203 key = dns_resource_key_new(class, type, name);
207 return dns_resource_record_new(key);
210 DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
214 assert(rr->n_ref > 0);
220 DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
224 assert(rr->n_ref > 0);
232 if (IN_SET(rr->key->type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
234 else if (rr->key->type == DNS_TYPE_HINFO) {
237 } else if (rr->key->type == DNS_TYPE_SOA) {
240 } else if (!IN_SET(rr->key->type, DNS_TYPE_A, DNS_TYPE_AAAA))
241 free(rr->generic.data);
243 dns_resource_key_unref(rr->key);
251 int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
252 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
253 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
254 _cleanup_free_ char *ptr = NULL;
261 r = dns_name_reverse(family, address, &ptr);
265 key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
271 rr = dns_resource_record_new(key);
275 rr->ptr.name = strdup(hostname);
285 int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
291 r = dns_resource_key_equal(a->key, b->key);
295 switch (a->key->type) {
300 return dns_name_equal(a->ptr.name, b->ptr.name);
303 return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
304 strcaseeq(a->hinfo.os, b->hinfo.os);
307 return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
310 return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
313 r = dns_name_equal(a->soa.mname, b->soa.mname);
316 r = dns_name_equal(a->soa.rname, b->soa.rname);
320 return a->soa.serial == b->soa.serial &&
321 a->soa.refresh == b->soa.refresh &&
322 a->soa.retry == b->soa.retry &&
323 a->soa.expire == b->soa.expire &&
324 a->soa.minimum == b->soa.minimum;
326 return a->generic.size == b->generic.size &&
327 memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
331 int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
332 _cleanup_free_ char *k = NULL;
338 r = dns_resource_key_to_string(rr->key, &k);
342 switch (rr->key->type) {
347 s = strjoin(k, " ", rr->ptr.name, NULL);
354 s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
360 _cleanup_free_ char *x = NULL;
362 r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
366 s = strjoin(k, " ", x, NULL);
372 case DNS_TYPE_AAAA: {
373 _cleanup_free_ char *x = NULL;
375 r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &x);
379 s = strjoin(k, " ", x, NULL);
386 r = asprintf(&s, "%s %s %s %u %u %u %u %u",
388 strna(rr->soa.mname),
389 strna(rr->soa.rname),
400 _cleanup_free_ char *x = NULL;
402 x = hexmem(rr->generic.data, rr->generic.size);
406 s = strjoin(k, " ", x, NULL);
416 const char *dns_class_to_string(uint16_t class) {
430 int dns_class_from_string(const char *s, uint16_t *class) {
434 if (strcaseeq(s, "IN"))
435 *class = DNS_CLASS_IN;
436 else if (strcaseeq(s, "ANY"))
437 *class = DNS_TYPE_ANY;
444 static const struct {
449 { DNS_TYPE_NS, "NS" },
450 { DNS_TYPE_CNAME, "CNAME" },
451 { DNS_TYPE_SOA, "SOA" },
452 { DNS_TYPE_PTR, "PTR" },
453 { DNS_TYPE_HINFO, "HINFO" },
454 { DNS_TYPE_MX, "MX" },
455 { DNS_TYPE_TXT, "TXT" },
456 { DNS_TYPE_AAAA, "AAAA" },
457 { DNS_TYPE_SRV, "SRV" },
458 { DNS_TYPE_SSHFP, "SSHFP" },
459 { DNS_TYPE_DNAME, "DNAME" },
460 { DNS_TYPE_ANY, "ANY" },
461 { DNS_TYPE_OPT, "OPT" },
462 { DNS_TYPE_TKEY, "TKEY" },
463 { DNS_TYPE_TSIG, "TSIG" },
464 { DNS_TYPE_IXFR, "IXFR" },
465 { DNS_TYPE_AXFR, "AXFR" },
469 const char *dns_type_to_string(uint16_t type) {
472 for (i = 0; i < ELEMENTSOF(dns_types); i++)
473 if (dns_types[i].type == type)
474 return dns_types[i].name;
479 int dns_type_from_string(const char *s, uint16_t *type) {
485 for (i = 0; i < ELEMENTSOF(dns_types); i++)
486 if (strcaseeq(dns_types[i].name, s)) {
487 *type = dns_types[i].type;