resolved-dns-rr.c revision a8812dd7f161a3e459c1730ac92ff2bbc9986ff1
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <math.h>
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include "dns-domain.h"
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek#include "dns-type.h"
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include "hexdecoct.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "resolved-dns-packet.h"
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include "resolved-dns-rr.h"
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#include "string-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "strv.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering DnsResourceKey *k;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering size_t l;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering l = strlen(name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k = malloc0(sizeof(DnsResourceKey) + l + 1);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!k)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k->n_ref = 1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k->class = class;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k->type = type;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering strcpy((char*) k + sizeof(DnsResourceKey), name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return k;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsResourceKey* dns_resource_key_new_cname(const DnsResourceKey *key) {
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen assert(key);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return dns_resource_key_new(key->class, DNS_TYPE_CNAME, DNS_RESOURCE_KEY_NAME(key));
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen}
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom GundersenDnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering int r;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(key);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(cname);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (cname->key->type == DNS_TYPE_CNAME)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return dns_resource_key_new(key->class, key->type, cname->cname.name);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering else {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering DnsResourceKey *k;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering char *destination = NULL;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering r = dns_name_change_suffix(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname->key), cname->dname.name, &destination);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (r < 0)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return NULL;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (r == 0)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return dns_resource_key_ref((DnsResourceKey*) key);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering k = dns_resource_key_new_consume(key->class, key->type, destination);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (!k) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering free(destination);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen return NULL;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return k;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringint dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering DnsResourceKey *new_key;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering char *joined;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering int r;
dc477e7385e8ab29efb8fadb72ec994077a105c6Lennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(ret);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(key);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(name);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (dns_name_is_root(name)) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering *ret = dns_resource_key_ref(key);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), name, &joined);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering new_key = dns_resource_key_new_consume(key->class, key->type, joined);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!new_key) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering free(joined);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return -ENOMEM;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *ret = new_key;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering DnsResourceKey *k;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k = new0(DnsResourceKey, 1);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!k)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k->n_ref = 1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k->class = class;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k->type = type;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k->_name = name;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return k;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart PoetteringDnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering if (!k)
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(k->n_ref > 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k->n_ref++;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return k;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!k)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(k->n_ref > 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (k->n_ref == 1) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(k->_name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(k);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering } else
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k->n_ref--;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poetteringint dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering int r;
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering if (r <= 0)
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering return r;
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering if (a->class != b->class)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (a->type != b->type)
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering return 0;
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering return 1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr, const char *search_domain) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(key);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(rr);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Checks if an rr matches the specified key. If a search
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * domain is specified, it will also be checked if the key
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * with the search domain suffixed might match the RR. */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering if (r != 0)
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering return r;
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (search_domain) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering _cleanup_free_ char *joined = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), search_domain, &joined);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), joined);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringint dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr, const char *search_domain) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering int r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(key);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(rr);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (rr->key->type == DNS_TYPE_CNAME)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(rr->key));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering else if (rr->key->type == DNS_TYPE_DNAME)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = dns_name_endswith(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(rr->key));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r != 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (search_domain) {
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen _cleanup_free_ char *joined = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), search_domain, &joined);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen if (r < 0)
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen return r;
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen if (rr->key->type == DNS_TYPE_CNAME)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return dns_name_equal(joined, DNS_RESOURCE_KEY_NAME(rr->key));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (rr->key->type == DNS_TYPE_DNAME)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return dns_name_endswith(joined, DNS_RESOURCE_KEY_NAME(rr->key));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic void dns_resource_key_hash_func(const void *i, struct siphash *state) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering const DnsResourceKey *k = i;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(k);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), state);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen siphash24_compress(&k->class, sizeof(k->class), state);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen siphash24_compress(&k->type, sizeof(k->type), state);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic int dns_resource_key_compare_func(const void *a, const void *b) {
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering const DnsResourceKey *x = a, *y = b;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering int ret;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering if (ret != 0)
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering return ret;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering if (x->type < y->type)
65b200e70d01cb7c513114b602c9b5cf41348f77Lennart Poettering return -1;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering if (x->type > y->type)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 1;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering if (x->class < y->class)
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering return -1;
0936416a1cba187dc76903dae73246f4cbddf312Lennart Poettering if (x->class > y->class)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersenconst struct hash_ops dns_resource_key_hash_ops = {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering .hash = dns_resource_key_hash_func,
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen .compare = dns_resource_key_compare_func
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen};
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringint dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering char cbuf[strlen("CLASS") + DECIMAL_STR_MAX(uint16_t)], tbuf[strlen("TYPE") + DECIMAL_STR_MAX(uint16_t)];
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt const char *c, *t;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering char *s;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering c = dns_class_to_string(key->class);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!c) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sprintf(cbuf, "CLASS%u", key->class);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering c = cbuf;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering t = dns_type_to_string(key->type);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!t){
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering sprintf(tbuf, "TYPE%u", key->type);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering t = tbuf;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (asprintf(&s, "%s %s %-5s", DNS_RESOURCE_KEY_NAME(key), c, t) < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -ENOMEM;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering *ret = s;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal SchmidtDnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt DnsResourceRecord *rr;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt rr = new0(DnsResourceRecord, 1);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!rr)
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen return NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering rr->n_ref = 1;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering rr->key = dns_resource_key_ref(key);
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering return rr;
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart PoetteringDnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering key = dns_resource_key_new(class, type, name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!key)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen return dns_resource_record_new(key);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart PoetteringDnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
c52a97b896c914e17ba5be73c0e806455fd9ad4dLennart Poettering if (!rr)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return NULL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(rr->n_ref > 0);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering rr->n_ref++;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return rr;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!rr)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(rr->n_ref > 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (rr->n_ref > 1) {
ee3d6aff9bd73c1b23e29d1fa1fa6f7a1ef0533bLennart Poettering rr->n_ref--;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (rr->key) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering switch(rr->key->type) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering case DNS_TYPE_SRV:
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering free(rr->srv.name);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering break;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering case DNS_TYPE_PTR:
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering case DNS_TYPE_NS:
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering case DNS_TYPE_CNAME:
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering case DNS_TYPE_DNAME:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(rr->ptr.name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case DNS_TYPE_HINFO:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(rr->hinfo.cpu);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(rr->hinfo.os);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case DNS_TYPE_TXT:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case DNS_TYPE_SPF:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering dns_txt_item_free_all(rr->txt.items);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case DNS_TYPE_SOA:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(rr->soa.mname);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(rr->soa.rname);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case DNS_TYPE_MX:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering free(rr->mx.exchange);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering break;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek case DNS_TYPE_DS:
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering free(rr->ds.digest);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering break;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering case DNS_TYPE_SSHFP:
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering free(rr->sshfp.fingerprint);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek break;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek case DNS_TYPE_DNSKEY:
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering free(rr->dnskey.key);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering break;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering case DNS_TYPE_RRSIG:
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek free(rr->rrsig.signer);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(rr->rrsig.signature);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering break;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering case DNS_TYPE_NSEC:
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek free(rr->nsec.next_domain_name);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering bitmap_free(rr->nsec.types);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering break;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering case DNS_TYPE_NSEC3:
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek free(rr->nsec3.next_hashed_name);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering free(rr->nsec3.salt);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering bitmap_free(rr->nsec3.types);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek break;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek case DNS_TYPE_LOC:
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek case DNS_TYPE_A:
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek case DNS_TYPE_AAAA:
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering break;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen default:
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen free(rr->generic.data);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen free(rr->wire_format);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering dns_resource_key_unref(rr->key);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek free(rr);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekint dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen _cleanup_free_ char *ptr = NULL;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen int r;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(ret);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(address);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen assert(hostname);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = dns_name_reverse(family, address, &ptr);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (!key)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return -ENOMEM;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering ptr = NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rr = dns_resource_record_new(key);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (!rr)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -ENOMEM;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rr->ptr.name = strdup(hostname);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!rr->ptr.name)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -ENOMEM;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *ret = rr;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering rr = NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering DnsResourceRecord *rr;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(ret);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(address);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(family);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (family == AF_INET) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, name);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!rr)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return -ENOMEM;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering rr->a.in_addr = address->in;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering } else if (family == AF_INET6) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!rr)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return -ENOMEM;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering rr->aaaa.in6_addr = address->in6;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering } else
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return -EAFNOSUPPORT;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering *ret = rr;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return 0;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering int r;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering assert(a);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering assert(b);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = dns_resource_key_equal(a->key, b->key);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r <= 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (a->unparseable != b->unparseable)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return 0;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering switch (a->unparseable ? _DNS_TYPE_INVALID : a->key->type) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering case DNS_TYPE_SRV:
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = dns_name_equal(a->srv.name, b->srv.name);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r <= 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return a->srv.priority == b->srv.priority &&
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering a->srv.weight == b->srv.weight &&
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering a->srv.port == b->srv.port;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering case DNS_TYPE_PTR:
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering case DNS_TYPE_NS:
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering case DNS_TYPE_CNAME:
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering case DNS_TYPE_DNAME:
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return dns_name_equal(a->ptr.name, b->ptr.name);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering case DNS_TYPE_HINFO:
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering strcaseeq(a->hinfo.os, b->hinfo.os);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering case DNS_TYPE_SPF: /* exactly the same as TXT */
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering case DNS_TYPE_TXT:
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return dns_txt_item_equal(a->txt.items, b->txt.items);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering case DNS_TYPE_A:
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering case DNS_TYPE_AAAA:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering case DNS_TYPE_SOA:
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = dns_name_equal(a->soa.mname, b->soa.mname);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering if (r <= 0)
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering return r;
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering r = dns_name_equal(a->soa.rname, b->soa.rname);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering if (r <= 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return a->soa.serial == b->soa.serial &&
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering a->soa.refresh == b->soa.refresh &&
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering a->soa.retry == b->soa.retry &&
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering a->soa.expire == b->soa.expire &&
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering a->soa.minimum == b->soa.minimum;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering case DNS_TYPE_MX:
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (a->mx.priority != b->mx.priority)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return dns_name_equal(a->mx.exchange, b->mx.exchange);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering case DNS_TYPE_LOC:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(a->loc.version == b->loc.version);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return a->loc.size == b->loc.size &&
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering a->loc.horiz_pre == b->loc.horiz_pre &&
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering a->loc.vert_pre == b->loc.vert_pre &&
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek a->loc.latitude == b->loc.latitude &&
0f84a72e3c0f58d71cff2121e6df1611eaf9c9eaDavid Herrmann a->loc.longitude == b->loc.longitude &&
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering a->loc.altitude == b->loc.altitude;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case DNS_TYPE_DS:
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return a->ds.key_tag == b->ds.key_tag &&
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering a->ds.algorithm == b->ds.algorithm &&
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering a->ds.digest_type == b->ds.digest_type &&
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering a->ds.digest_size == b->ds.digest_size &&
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering memcmp(a->ds.digest, b->ds.digest, a->ds.digest_size) == 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering case DNS_TYPE_SSHFP:
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return a->sshfp.algorithm == b->sshfp.algorithm &&
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering a->sshfp.fptype == b->sshfp.fptype &&
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering a->sshfp.fingerprint_size == b->sshfp.fingerprint_size &&
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering memcmp(a->sshfp.fingerprint, b->sshfp.fingerprint, a->sshfp.fingerprint_size) == 0;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering case DNS_TYPE_DNSKEY:
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return a->dnskey.flags == b->dnskey.flags &&
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering a->dnskey.protocol == b->dnskey.protocol &&
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering a->dnskey.algorithm == b->dnskey.algorithm &&
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering a->dnskey.key_size == b->dnskey.key_size &&
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering memcmp(a->dnskey.key, b->dnskey.key, a->dnskey.key_size) == 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek case DNS_TYPE_RRSIG:
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek /* do the fast comparisons first */
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (a->rrsig.type_covered != b->rrsig.type_covered ||
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek a->rrsig.algorithm != b->rrsig.algorithm ||
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek a->rrsig.labels != b->rrsig.labels ||
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek a->rrsig.original_ttl != b->rrsig.original_ttl ||
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek a->rrsig.expiration != b->rrsig.expiration ||
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek a->rrsig.inception != b->rrsig.inception ||
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek a->rrsig.key_tag != b->rrsig.key_tag ||
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek a->rrsig.signature_size != b->rrsig.signature_size ||
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek memcmp(a->rrsig.signature, b->rrsig.signature, a->rrsig.signature_size) != 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return false;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return dns_name_equal(a->rrsig.signer, b->rrsig.signer);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek case DNS_TYPE_NSEC:
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return dns_name_equal(a->nsec.next_domain_name, b->nsec.next_domain_name) &&
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen bitmap_equal(a->nsec.types, b->nsec.types);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen case DNS_TYPE_NSEC3:
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return a->nsec3.algorithm == b->nsec3.algorithm &&
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen a->nsec3.flags == b->nsec3.flags &&
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen a->nsec3.iterations == b->nsec3.iterations &&
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering a->nsec3.salt_size == b->nsec3.salt_size &&
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering memcmp(a->nsec3.salt, b->nsec3.salt, a->nsec3.salt_size) == 0 &&
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering memcmp(a->nsec3.next_hashed_name, b->nsec3.next_hashed_name, a->nsec3.next_hashed_name_size) == 0 &&
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen bitmap_equal(a->nsec3.types, b->nsec3.types);
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering default:
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return a->generic.size == b->generic.size &&
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic char* format_location(uint32_t latitude, uint32_t longitude, uint32_t altitude,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek uint8_t size, uint8_t horiz_pre, uint8_t vert_pre) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek char *s;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek char NS = latitude >= 1U<<31 ? 'N' : 'S';
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn char EW = longitude >= 1U<<31 ? 'E' : 'W';
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn int lat = latitude >= 1U<<31 ? (int) (latitude - (1U<<31)) : (int) ((1U<<31) - latitude);
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn int lon = longitude >= 1U<<31 ? (int) (longitude - (1U<<31)) : (int) ((1U<<31) - longitude);
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn double alt = altitude >= 10000000u ? altitude - 10000000u : -(double)(10000000u - altitude);
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn double siz = (size >> 4) * exp10((double) (size & 0xF));
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn double hor = (horiz_pre >> 4) * exp10((double) (horiz_pre & 0xF));
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek double ver = (vert_pre >> 4) * exp10((double) (vert_pre & 0xF));
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (asprintf(&s, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek (lat / 60000 / 60),
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek (lat / 60000) % 60,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek (lat % 60000) / 1000.,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen NS,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen (lon / 60000 / 60),
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen (lon / 60000) % 60,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen (lon % 60000) / 1000.,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen EW,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen alt / 100.,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen siz / 100.,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen hor / 100.,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen ver / 100.) < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return NULL;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return s;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int format_timestamp_dns(char *buf, size_t l, time_t sec) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering struct tm tm;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(buf);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(l > strlen("YYYYMMDDHHmmSS"));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!gmtime_r(&sec, &tm))
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -EINVAL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (strftime(buf, l, "%Y%m%d%H%M%S", &tm) <= 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -EINVAL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekstatic char *format_types(Bitmap *types) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_strv_free_ char **strv = NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *str = NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek Iterator i;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek unsigned type;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek int r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek BITMAP_FOREACH(type, types, i) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (dns_type_to_string(type)) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = strv_extend(&strv, dns_type_to_string(type));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek } else {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek char *t;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = asprintf(&t, "TYPE%u", type);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = strv_consume(&strv, t);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return NULL;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen }
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen }
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen str = strv_join(strv, " ");
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (!str)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return NULL;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return strjoin("( ", str, " )", NULL);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen}
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersenstatic char *format_txt(DnsTxtItem *first) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen DnsTxtItem *i;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen size_t c = 1;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen char *p, *s;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen LIST_FOREACH(items, i, first)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen c += i->length * 4 + 3;
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen p = s = new(char, c);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!s)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return NULL;
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen LIST_FOREACH(items, i, first) {
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering size_t j;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (i != first)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *(p++) = ' ';
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *(p++) = '"';
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen for (j = 0; j < i->length; j++) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (i->data[j] < ' ' || i->data[j] == '"' || i->data[j] >= 127) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *(p++) = '\\';
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering *(p++) = '0' + (i->data[j] / 100);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *(p++) = '0' + ((i->data[j] / 10) % 10);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *(p++) = '0' + (i->data[j] % 10);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen } else
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *(p++) = i->data[j];
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *(p++) = '"';
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen *p = 0;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return s;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering _cleanup_free_ char *k = NULL, *t = NULL;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering char *s;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(rr);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = dns_resource_key_to_string(rr->key, &k);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering case DNS_TYPE_SRV:
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = asprintf(&s, "%s %u %u %u %s",
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering k,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering rr->srv.priority,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering rr->srv.weight,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering rr->srv.port,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering strna(rr->srv.name));
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return -ENOMEM;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering break;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering case DNS_TYPE_PTR:
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering case DNS_TYPE_NS:
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering case DNS_TYPE_CNAME:
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering case DNS_TYPE_DNAME:
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering s = strjoin(k, " ", rr->ptr.name, NULL);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!s)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return -ENOMEM;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering break;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering case DNS_TYPE_HINFO:
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (!s)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return -ENOMEM;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering break;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering case DNS_TYPE_SPF: /* exactly the same as TXT */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case DNS_TYPE_TXT:
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering t = format_txt(rr->txt.items);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (!t)
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering return -ENOMEM;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering s = strjoin(k, " ", t, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!s)
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering return -ENOMEM;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering break;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering case DNS_TYPE_A: {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering _cleanup_free_ char *x = NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (r < 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return r;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering s = strjoin(k, " ", x, NULL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (!s)
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering return -ENOMEM;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering break;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case DNS_TYPE_AAAA:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &t);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering s = strjoin(k, " ", t, NULL);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!s)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -ENOMEM;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering break;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case DNS_TYPE_SOA:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = asprintf(&s, "%s %s %s %u %u %u %u %u",
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering k,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering strna(rr->soa.mname),
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering strna(rr->soa.rname),
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek rr->soa.serial,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek rr->soa.refresh,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering rr->soa.retry,
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek rr->soa.expire,
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering rr->soa.minimum);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (r < 0)
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return -ENOMEM;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek break;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek case DNS_TYPE_MX:
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek r = asprintf(&s, "%s %u %s",
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering k,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering rr->mx.priority,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering rr->mx.exchange);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -ENOMEM;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering break;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case DNS_TYPE_LOC:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(rr->loc.version == 0);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering t = format_location(rr->loc.latitude,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering rr->loc.longitude,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering rr->loc.altitude,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek rr->loc.size,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek rr->loc.horiz_pre,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering rr->loc.vert_pre);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (!t)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -ENOMEM;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering s = strjoin(k, " ", t, NULL);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (!s)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -ENOMEM;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering break;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case DNS_TYPE_DS:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering t = hexmem(rr->ds.digest, rr->ds.digest_size);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!t)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -ENOMEM;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = asprintf(&s, "%s %u %u %u %s",
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering k,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering rr->ds.key_tag,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering rr->ds.algorithm,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering rr->ds.digest_type,
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering t);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -ENOMEM;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek break;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek case DNS_TYPE_SSHFP:
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek t = hexmem(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (!t)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return -ENOMEM;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = asprintf(&s, "%s %u %u %s",
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek k,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek rr->sshfp.algorithm,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek rr->sshfp.fptype,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek t);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (r < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return -ENOMEM;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek break;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek case DNS_TYPE_DNSKEY: {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek const char *alg;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering alg = dnssec_algorithm_to_string(rr->dnskey.algorithm);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek t = base64mem(rr->dnskey.key, rr->dnskey.key_size);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!t)
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering return -ENOMEM;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = asprintf(&s, "%s %u %u %.*s%.*u %s",
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen k,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen rr->dnskey.flags,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen rr->dnskey.protocol,
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering alg ? -1 : 0, alg,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen alg ? 0 : 1, alg ? 0u : (unsigned) rr->dnskey.algorithm,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen t);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (r < 0)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return -ENOMEM;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen break;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen case DNS_TYPE_RRSIG: {
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering const char *type, *alg;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen char expiration[strlen("YYYYMMDDHHmmSS") + 1], inception[strlen("YYYYMMDDHHmmSS") + 1];
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek type = dns_type_to_string(rr->rrsig.type_covered);
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen alg = dnssec_algorithm_to_string(rr->rrsig.algorithm);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering t = base64mem(rr->rrsig.signature, rr->rrsig.signature_size);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (!t)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return -ENOMEM;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering r = format_timestamp_dns(expiration, sizeof(expiration), rr->rrsig.expiration);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (r < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return r;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering r = format_timestamp_dns(inception, sizeof(inception), rr->rrsig.inception);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (r < 0)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return r;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek /* TYPE?? follows
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek * http://tools.ietf.org/html/rfc3597#section-5 */
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek r = asprintf(&s, "%s %s%.*u %.*s%.*u %u %u %s %s %u %s %s",
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen k,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek type ?: "TYPE",
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering type ? 0 : 1, type ? 0u : (unsigned) rr->rrsig.type_covered,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering alg ? -1 : 0, alg,
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering alg ? 0 : 1, alg ? 0u : (unsigned) rr->rrsig.algorithm,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek rr->rrsig.labels,
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering rr->rrsig.original_ttl,
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering expiration,
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek inception,
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek rr->rrsig.key_tag,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek rr->rrsig.signer,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek t);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (r < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return -ENOMEM;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek break;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek case DNS_TYPE_NSEC:
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen t = format_types(rr->nsec.types);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!t)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return -ENOMEM;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = asprintf(&s, "%s %s %s",
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen k,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek rr->nsec.next_domain_name,
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering t);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r < 0)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return -ENOMEM;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen break;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen case DNS_TYPE_NSEC3: {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen _cleanup_free_ char *salt = NULL, *hash = NULL;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (rr->nsec3.salt_size > 0) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen salt = hexmem(rr->nsec3.salt, rr->nsec3.salt_size);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!salt)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return -ENOMEM;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek hash = base32hexmem(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, false);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!hash)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return -ENOMEM;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek t = format_types(rr->nsec3.types);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!t)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return -ENOMEM;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen r = asprintf(&s, "%s %"PRIu8" %"PRIu8" %"PRIu16" %s %s %s",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek k,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek rr->nsec3.algorithm,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek rr->nsec3.flags,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek rr->nsec3.iterations,
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering rr->nsec3.salt_size > 0 ? salt : "-",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek hash,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek t);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return -ENOMEM;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen break;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen default:
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen t = hexmem(rr->generic.data, rr->generic.size);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!t)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return -ENOMEM;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering r = asprintf(&s, "%s \\# %zu %s", k, rr->generic.size, t);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (r < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return -ENOMEM;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen break;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering *ret = s;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return 0;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen}
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenint dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen DnsPacket packet = {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen .n_ref = 1,
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering .protocol = DNS_PROTOCOL_DNS,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen .on_stack = true,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen .refuse_compression = true,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen .canonical_form = canonical,
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering };
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen size_t start, rds;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen int r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen assert(rr);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering /* Generates the RR in wire-format, optionally in the
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * canonical form as discussed in the DNSSEC RFC 4034, Section
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * 6.2. We allocate a throw-away DnsPacket object on the stack
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * here, because we need some book-keeping for memory
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering * management, and can reuse the DnsPacket serializer, that
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * can generate the canonical form, too, but also knows label
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * compression and suchlike. */
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (rr->wire_format && rr->wire_format_canonical == canonical)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return 0;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = dns_packet_append_rr(&packet, rr, &start, &rds);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (r < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return r;
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering assert(start == 0);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen assert(packet._data);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering free(rr->wire_format);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek rr->wire_format = packet._data;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering rr->wire_format_size = packet.size;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering rr->wire_format_rdata_offset = rds;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering rr->wire_format_canonical = canonical;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering packet._data = NULL;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering dns_packet_unref(&packet);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return 0;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering}
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poetteringconst char *dns_class_to_string(uint16_t class) {
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering switch (class) {
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering case DNS_CLASS_IN:
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return "IN";
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering case DNS_CLASS_ANY:
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return "ANY";
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering }
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return NULL;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering}
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poetteringint dns_class_from_string(const char *s, uint16_t *class) {
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering assert(s);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering assert(class);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering if (strcaseeq(s, "IN"))
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering *class = DNS_CLASS_IN;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering else if (strcaseeq(s, "ANY"))
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering *class = DNS_CLASS_ANY;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering else
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return -EINVAL;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return 0;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering}
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart PoetteringDnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i) {
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering DnsTxtItem *n;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering if (!i)
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return NULL;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering n = i->items_next;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering free(i);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return dns_txt_item_free_all(n);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering}
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringbool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!a != !b)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return false;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!a)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return true;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (a->length != b->length)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return false;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (memcmp(a->data, b->data, a->length) != 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return false;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering return dns_txt_item_equal(a->items_next, b->items_next);
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering}
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering