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/>.
24 #include "resolved-dns-domain.h"
25 #include "resolved-dns-rr.h"
27 DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
34 k = malloc0(sizeof(DnsResourceKey) + l + 1);
42 strcpy((char*) k + sizeof(DnsResourceKey), name);
47 DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
52 k = new0(DnsResourceKey, 1);
64 DnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
75 DnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
90 int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
93 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
97 if (a->class != b->class)
100 if (a->type != b->type)
106 int dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr) {
110 if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
113 if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
116 return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
119 int dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr) {
123 if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
126 if (rr->key->type != DNS_TYPE_CNAME)
129 return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
132 unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
133 const DnsResourceKey *k = i;
136 ul = dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), hash_key);
137 ul = ul * hash_key[0] + ul + k->class;
138 ul = ul * hash_key[1] + ul + k->type;
143 int dns_resource_key_compare_func(const void *a, const void *b) {
144 const DnsResourceKey *x = a, *y = b;
147 ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
151 if (x->type < y->type)
153 if (x->type > y->type)
156 if (x->class < y->class)
158 if (x->class > y->class)
164 int dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
165 char cbuf[DECIMAL_STR_MAX(uint16_t)], tbuf[DECIMAL_STR_MAX(uint16_t)];
169 c = dns_class_to_string(key->class);
171 sprintf(cbuf, "%i", key->class);
175 t = dns_type_to_string(key->type);
177 sprintf(tbuf, "%i", key->type);
181 s = strjoin(DNS_RESOURCE_KEY_NAME(key), " ", c, " ", t, NULL);
189 DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
190 DnsResourceRecord *rr;
192 rr = new0(DnsResourceRecord, 1);
197 rr->key = dns_resource_key_ref(key);
202 DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
203 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
205 key = dns_resource_key_new(class, type, name);
209 return dns_resource_record_new(key);
212 DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
216 assert(rr->n_ref > 0);
222 DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
226 assert(rr->n_ref > 0);
234 switch(rr->key->type) {
246 strv_free(rr->txt.strings);
253 free(rr->mx.exchange);
259 free(rr->generic.data);
262 dns_resource_key_unref(rr->key);
270 int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
271 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
272 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
273 _cleanup_free_ char *ptr = NULL;
280 r = dns_name_reverse(family, address, &ptr);
284 key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
290 rr = dns_resource_record_new(key);
294 rr->ptr.name = strdup(hostname);
304 int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
310 r = dns_resource_key_equal(a->key, b->key);
314 switch (a->key->type) {
319 return dns_name_equal(a->ptr.name, b->ptr.name);
322 return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
323 strcaseeq(a->hinfo.os, b->hinfo.os);
325 case DNS_TYPE_SPF: /* exactly the same as TXT */
329 for (i = 0; a->txt.strings[i] || b->txt.strings[i]; i++)
330 if (!streq_ptr(a->txt.strings[i], b->txt.strings[i]))
336 return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
339 return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
342 r = dns_name_equal(a->soa.mname, b->soa.mname);
345 r = dns_name_equal(a->soa.rname, b->soa.rname);
349 return a->soa.serial == b->soa.serial &&
350 a->soa.refresh == b->soa.refresh &&
351 a->soa.retry == b->soa.retry &&
352 a->soa.expire == b->soa.expire &&
353 a->soa.minimum == b->soa.minimum;
355 if (a->mx.priority != b->mx.priority)
358 return dns_name_equal(a->mx.exchange, b->mx.exchange);
361 return a->generic.size == b->generic.size &&
362 memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
366 int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
367 _cleanup_free_ char *k = NULL;
373 r = dns_resource_key_to_string(rr->key, &k);
377 switch (rr->key->type) {
382 s = strjoin(k, " ", rr->ptr.name, NULL);
389 s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
394 case DNS_TYPE_SPF: /* exactly the same as TXT */
396 _cleanup_free_ char *t;
398 t = strv_join_quoted(rr->txt.strings);
402 s = strjoin(k, " ", t, NULL);
410 _cleanup_free_ char *x = NULL;
412 r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
416 s = strjoin(k, " ", x, NULL);
422 case DNS_TYPE_AAAA: {
423 _cleanup_free_ char *x = NULL;
425 r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &x);
429 s = strjoin(k, " ", x, NULL);
436 r = asprintf(&s, "%s %s %s %u %u %u %u %u",
438 strna(rr->soa.mname),
439 strna(rr->soa.rname),
450 r = asprintf(&s, "%s %u %s",
459 _cleanup_free_ char *x = NULL;
461 x = hexmem(rr->generic.data, rr->generic.size);
465 s = strjoin(k, " ", x, NULL);
475 const char *dns_class_to_string(uint16_t class) {
489 int dns_class_from_string(const char *s, uint16_t *class) {
493 if (strcaseeq(s, "IN"))
494 *class = DNS_CLASS_IN;
495 else if (strcaseeq(s, "ANY"))
496 *class = DNS_TYPE_ANY;
503 static const struct {
508 { DNS_TYPE_NS, "NS" },
509 { DNS_TYPE_CNAME, "CNAME" },
510 { DNS_TYPE_SOA, "SOA" },
511 { DNS_TYPE_PTR, "PTR" },
512 { DNS_TYPE_HINFO, "HINFO" },
513 { DNS_TYPE_MX, "MX" },
514 { DNS_TYPE_TXT, "TXT" },
515 { DNS_TYPE_AAAA, "AAAA" },
516 { DNS_TYPE_SRV, "SRV" },
517 { DNS_TYPE_SSHFP, "SSHFP" },
518 { DNS_TYPE_SPF, "SPF" },
519 { DNS_TYPE_DNAME, "DNAME" },
520 { DNS_TYPE_ANY, "ANY" },
521 { DNS_TYPE_OPT, "OPT" },
522 { DNS_TYPE_TKEY, "TKEY" },
523 { DNS_TYPE_TSIG, "TSIG" },
524 { DNS_TYPE_IXFR, "IXFR" },
525 { DNS_TYPE_AXFR, "AXFR" },
529 const char *dns_type_to_string(uint16_t type) {
532 for (i = 0; i < ELEMENTSOF(dns_types); i++)
533 if (dns_types[i].type == type)
534 return dns_types[i].name;
539 int dns_type_from_string(const char *s, uint16_t *type) {
545 for (i = 0; i < ELEMENTSOF(dns_types); i++)
546 if (strcaseeq(dns_types[i].name, s)) {
547 *type = dns_types[i].type;