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 if (IN_SET(rr
->key
->type
, DNS_TYPE_PTR
, DNS_TYPE_NS
, DNS_TYPE_CNAME
))
236 else if (rr
->key
->type
== DNS_TYPE_HINFO
) {
239 } else if (rr
->key
->type
== DNS_TYPE_TXT
) {
240 strv_free(rr
->txt
.strings
);
241 } else if (rr
->key
->type
== DNS_TYPE_SOA
) {
244 } else if (rr
->key
->type
== DNS_TYPE_MX
) {
245 free(rr
->mx
.exchange
);
246 } else if (!IN_SET(rr
->key
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
))
247 free(rr
->generic
.data
);
249 dns_resource_key_unref(rr
->key
);
257 int dns_resource_record_new_reverse(DnsResourceRecord
**ret
, int family
, const union in_addr_union
*address
, const char *hostname
) {
258 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
259 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
260 _cleanup_free_
char *ptr
= NULL
;
267 r
= dns_name_reverse(family
, address
, &ptr
);
271 key
= dns_resource_key_new_consume(DNS_CLASS_IN
, DNS_TYPE_PTR
, ptr
);
277 rr
= dns_resource_record_new(key
);
281 rr
->ptr
.name
= strdup(hostname
);
291 int dns_resource_record_equal(const DnsResourceRecord
*a
, const DnsResourceRecord
*b
) {
297 r
= dns_resource_key_equal(a
->key
, b
->key
);
301 switch (a
->key
->type
) {
306 return dns_name_equal(a
->ptr
.name
, b
->ptr
.name
);
309 return strcaseeq(a
->hinfo
.cpu
, b
->hinfo
.cpu
) &&
310 strcaseeq(a
->hinfo
.os
, b
->hinfo
.os
);
315 for (i
= 0; a
->txt
.strings
[i
] || b
->txt
.strings
[i
]; i
++)
316 if (!streq_ptr(a
->txt
.strings
[i
], b
->txt
.strings
[i
]))
322 return memcmp(&a
->a
.in_addr
, &b
->a
.in_addr
, sizeof(struct in_addr
)) == 0;
325 return memcmp(&a
->aaaa
.in6_addr
, &b
->aaaa
.in6_addr
, sizeof(struct in6_addr
)) == 0;
328 r
= dns_name_equal(a
->soa
.mname
, b
->soa
.mname
);
331 r
= dns_name_equal(a
->soa
.rname
, b
->soa
.rname
);
335 return a
->soa
.serial
== b
->soa
.serial
&&
336 a
->soa
.refresh
== b
->soa
.refresh
&&
337 a
->soa
.retry
== b
->soa
.retry
&&
338 a
->soa
.expire
== b
->soa
.expire
&&
339 a
->soa
.minimum
== b
->soa
.minimum
;
341 if (a
->mx
.priority
!= b
->mx
.priority
)
344 return dns_name_equal(a
->mx
.exchange
, b
->mx
.exchange
);
347 return a
->generic
.size
== b
->generic
.size
&&
348 memcmp(a
->generic
.data
, b
->generic
.data
, a
->generic
.size
) == 0;
352 int dns_resource_record_to_string(const DnsResourceRecord
*rr
, char **ret
) {
353 _cleanup_free_
char *k
= NULL
;
359 r
= dns_resource_key_to_string(rr
->key
, &k
);
363 switch (rr
->key
->type
) {
368 s
= strjoin(k
, " ", rr
->ptr
.name
, NULL
);
375 s
= strjoin(k
, " ", rr
->hinfo
.cpu
, " ", rr
->hinfo
.os
, NULL
);
381 _cleanup_free_
char *t
;
383 t
= strv_join_quoted(rr
->txt
.strings
);
387 s
= strjoin(k
, " ", t
, NULL
);
395 _cleanup_free_
char *x
= NULL
;
397 r
= in_addr_to_string(AF_INET
, (const union in_addr_union
*) &rr
->a
.in_addr
, &x
);
401 s
= strjoin(k
, " ", x
, NULL
);
407 case DNS_TYPE_AAAA
: {
408 _cleanup_free_
char *x
= NULL
;
410 r
= in_addr_to_string(AF_INET6
, (const union in_addr_union
*) &rr
->aaaa
.in6_addr
, &x
);
414 s
= strjoin(k
, " ", x
, NULL
);
421 r
= asprintf(&s
, "%s %s %s %u %u %u %u %u",
423 strna(rr
->soa
.mname
),
424 strna(rr
->soa
.rname
),
435 r
= asprintf(&s
, "%s %u %s",
444 _cleanup_free_
char *x
= NULL
;
446 x
= hexmem(rr
->generic
.data
, rr
->generic
.size
);
450 s
= strjoin(k
, " ", x
, NULL
);
460 const char *dns_class_to_string(uint16_t class) {
474 int dns_class_from_string(const char *s
, uint16_t *class) {
478 if (strcaseeq(s
, "IN"))
479 *class = DNS_CLASS_IN
;
480 else if (strcaseeq(s
, "ANY"))
481 *class = DNS_TYPE_ANY
;
488 static const struct {
493 { DNS_TYPE_NS
, "NS" },
494 { DNS_TYPE_CNAME
, "CNAME" },
495 { DNS_TYPE_SOA
, "SOA" },
496 { DNS_TYPE_PTR
, "PTR" },
497 { DNS_TYPE_HINFO
, "HINFO" },
498 { DNS_TYPE_MX
, "MX" },
499 { DNS_TYPE_TXT
, "TXT" },
500 { DNS_TYPE_AAAA
, "AAAA" },
501 { DNS_TYPE_SRV
, "SRV" },
502 { DNS_TYPE_SSHFP
, "SSHFP" },
503 { DNS_TYPE_DNAME
, "DNAME" },
504 { DNS_TYPE_ANY
, "ANY" },
505 { DNS_TYPE_OPT
, "OPT" },
506 { DNS_TYPE_TKEY
, "TKEY" },
507 { DNS_TYPE_TSIG
, "TSIG" },
508 { DNS_TYPE_IXFR
, "IXFR" },
509 { DNS_TYPE_AXFR
, "AXFR" },
513 const char *dns_type_to_string(uint16_t type
) {
516 for (i
= 0; i
< ELEMENTSOF(dns_types
); i
++)
517 if (dns_types
[i
].type
== type
)
518 return dns_types
[i
].name
;
523 int dns_type_from_string(const char *s
, uint16_t *type
) {
529 for (i
= 0; i
< ELEMENTSOF(dns_types
); i
++)
530 if (strcaseeq(dns_types
[i
].name
, s
)) {
531 *type
= dns_types
[i
].type
;