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 void dns_resource_key_free(DnsResourceKey *key) {
33 unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
34 const DnsResourceKey *k = i;
37 ul = dns_name_hash_func(k->name, hash_key);
38 ul = ul * hash_key[0] + ul + k->class;
39 ul = ul * hash_key[1] + ul + k->type;
44 int dns_resource_key_compare_func(const void *a, const void *b) {
45 const DnsResourceKey *x = a, *y = b;
48 ret = dns_name_compare_func(x->name, y->name);
52 if (x->type < y->type)
54 if (x->type > y->type)
57 if (x->class < y->class)
59 if (x->class > y->class)
65 DnsResourceRecord* dns_resource_record_new(void) {
66 DnsResourceRecord *rr;
68 rr = new0(DnsResourceRecord, 1);
76 DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
80 assert(rr->n_ref > 0);
86 DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
90 assert(rr->n_ref > 0);
97 if (IN_SET(rr->key.type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
99 else if (rr->key.type == DNS_TYPE_HINFO) {
102 } else if (!IN_SET(rr->key.type, DNS_TYPE_A, DNS_TYPE_AAAA))
103 free(rr->generic.data);
105 dns_resource_key_free(&rr->key);
111 DnsResourceRecord** dns_resource_record_freev(DnsResourceRecord **rrs, unsigned n) {
114 assert(n == 0 || rrs);
116 for (i = 0; i < n; i++)
117 dns_resource_record_unref(rrs[i]);
123 int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
129 r = dns_name_equal(a->key.name, b->key.name);
133 if (a->key.class != b->key.class)
136 if (a->key.type != b->key.type)
139 if (IN_SET(a->key.type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
140 return dns_name_equal(a->ptr.name, b->ptr.name);
141 else if (a->key.type == DNS_TYPE_HINFO)
142 return strcasecmp(a->hinfo.cpu, b->hinfo.cpu) == 0 &&
143 strcasecmp(a->hinfo.os, b->hinfo.os) == 0;
144 else if (a->key.type == DNS_TYPE_A)
145 return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
146 else if (a->key.type == DNS_TYPE_AAAA)
147 return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
149 return a->generic.size == b->generic.size &&
150 memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
153 const char *dns_class_to_string(uint16_t class) {
167 const char *dns_type_to_string(uint16_t type) {