resolved-dns-rr.c revision 8ac4e9e1e54397f6d1745c2a7a806132418c7da2
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering/***
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering This file is part of systemd.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Copyright 2014 Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering (at your option) any later version.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is distributed in the hope that it will be useful, but
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Lesser General Public License for more details.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering***/
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering#include <math.h>
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering#include "strv.h"
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering#include "resolved-dns-domain.h"
96115cdfe0241ae9b4e7177cd3874c0a93d00b39Thomas Hindoe Paaboel Andersen#include "resolved-dns-rr.h"
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
4b357e15876b730343db08719c877fdb45b6ad42Michael MarineauDnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
4b357e15876b730343db08719c877fdb45b6ad42Michael Marineau DnsResourceKey *k;
4b357e15876b730343db08719c877fdb45b6ad42Michael Marineau size_t l;
4b357e15876b730343db08719c877fdb45b6ad42Michael Marineau
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert(name);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering l = strlen(name);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering k = malloc0(sizeof(DnsResourceKey) + l + 1);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (!k)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return NULL;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering k->n_ref = 1;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering k->class = class;
9a5cb1371b6d8b0a04bd08665bcf9b06cb40c64cZbigniew Jędrzejewski-Szmek k->type = type;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering strcpy((char*) k + sizeof(DnsResourceKey), name);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return k;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
14bf2c9d375db6a4670bc0ef0e521e35a939a498Lennart PoetteringDnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek DnsResourceKey *k;
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering assert(name);
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering k = new0(DnsResourceKey, 1);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (!k)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return NULL;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering k->n_ref = 1;
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek k->class = class;
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek k->type = type;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering k->_name = name;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return k;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart PoetteringDnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (!k)
b94801803417c23d099cb7e508754181ecd27f9cZbigniew Jędrzejewski-Szmek return NULL;
b94801803417c23d099cb7e508754181ecd27f9cZbigniew Jędrzejewski-Szmek
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek assert(k->n_ref > 0);
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek k->n_ref++;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return k;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek
091526ab20485492124852dcf629787f35816df8Zbigniew Jędrzejewski-SzmekDnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
091526ab20485492124852dcf629787f35816df8Zbigniew Jędrzejewski-Szmek if (!k)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return NULL;
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek assert(k->n_ref > 0);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (k->n_ref == 1) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering free(k->_name);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering free(k);
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek } else
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek k->n_ref--;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return NULL;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringint dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering int r;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (r <= 0)
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek return r;
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (a->class != b->class)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (a->type != b->type)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 1;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringint dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert(key);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert(rr);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek return 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek return 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringint dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert(key);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert(rr);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (rr->key->type != DNS_TYPE_CNAME)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringunsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering const DnsResourceKey *k = i;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering unsigned long ul;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering ul = dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), hash_key);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering ul = ul * hash_key[0] + ul + k->class;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering ul = ul * hash_key[1] + ul + k->type;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return ul;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringint dns_resource_key_compare_func(const void *a, const void *b) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering const DnsResourceKey *x = a, *y = b;
1ebab691c7749779072741f71865bd0e055b7ecfLennart Poettering int ret;
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
ee530d8b73246f29781bd54a707ca75c7ef5a6cbLennart Poettering if (ret != 0)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return ret;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (x->type < y->type)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -1;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (x->type > y->type)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 1;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (x->class < y->class)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -1;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (x->class > y->class)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 1;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringint dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering char cbuf[DECIMAL_STR_MAX(uint16_t)], tbuf[DECIMAL_STR_MAX(uint16_t)];
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering const char *c, *t;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering char *s;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering c = dns_class_to_string(key->class);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (!c) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering sprintf(cbuf, "%i", key->class);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering c = cbuf;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering }
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering t = dns_type_to_string(key->type);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (!t){
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering sprintf(tbuf, "%i", key->type);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering t = tbuf;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering }
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering s = strjoin(DNS_RESOURCE_KEY_NAME(key), " ", c, " ", t, NULL);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (!s)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return -ENOMEM;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering *ret = s;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return 0;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart PoetteringDnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering DnsResourceRecord *rr;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering rr = new0(DnsResourceRecord, 1);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (!rr)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering rr->n_ref = 1;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering rr->key = dns_resource_key_ref(key);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return rr;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart PoetteringDnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering key = dns_resource_key_new(class, type, name);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (!key)
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering return NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return dns_resource_record_new(key);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart PoetteringDnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (!rr)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert(rr->n_ref > 0);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering rr->n_ref++;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return rr;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart PoetteringDnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (!rr)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert(rr->n_ref > 0);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (rr->n_ref > 1) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering rr->n_ref--;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering }
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (rr->key) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering switch(rr->key->type) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_PTR:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_NS:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_CNAME:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_DNAME:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering free(rr->ptr.name);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering break;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_HINFO:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering free(rr->hinfo.cpu);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering free(rr->hinfo.os);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering break;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_SPF:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_TXT:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering strv_free(rr->txt.strings);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering break;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_SOA:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering free(rr->soa.mname);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering free(rr->soa.rname);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering break;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_MX:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering free(rr->mx.exchange);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering break;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_LOC:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_A:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_AAAA:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering break;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering default:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering free(rr->generic.data);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering }
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering dns_resource_key_unref(rr->key);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering }
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering free(rr);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering}
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringint dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_free_ char *ptr = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering int r;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert(ret);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert(address);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert(hostname);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = dns_name_reverse(family, address, &ptr);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (r < 0)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return r;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (!key)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return -ENOMEM;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering ptr = NULL;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering rr = dns_resource_record_new(key);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (!rr)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return -ENOMEM;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering rr->ptr.name = strdup(hostname);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (!rr->ptr.name)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return -ENOMEM;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering *ret = rr;
4b1b14a6a6acb1640596d5e9542829d32989d385Lennart Poettering rr = NULL;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
4b1b14a6a6acb1640596d5e9542829d32989d385Lennart Poettering return 0;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringint dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering int r;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert(a);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering assert(b);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = dns_resource_key_equal(a->key, b->key);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (r <= 0)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return r;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering switch (a->key->type) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_PTR:
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_NS:
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering case DNS_TYPE_CNAME:
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering case DNS_TYPE_DNAME:
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering return dns_name_equal(a->ptr.name, b->ptr.name);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering case DNS_TYPE_HINFO:
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering strcaseeq(a->hinfo.os, b->hinfo.os);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_SPF: /* exactly the same as TXT */
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_TXT: {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering int i;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering for (i = 0; a->txt.strings[i] || b->txt.strings[i]; i++)
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering if (!streq_ptr(a->txt.strings[i], b->txt.strings[i]))
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering return false;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering return true;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering }
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering case DNS_TYPE_A:
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering case DNS_TYPE_AAAA:
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_SOA:
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = dns_name_equal(a->soa.mname, b->soa.mname);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (r <= 0)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return r;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = dns_name_equal(a->soa.rname, b->soa.rname);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (r <= 0)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return r;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering return a->soa.serial == b->soa.serial &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->soa.refresh == b->soa.refresh &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->soa.retry == b->soa.retry &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->soa.expire == b->soa.expire &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->soa.minimum == b->soa.minimum;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_MX:
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (a->mx.priority != b->mx.priority)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 0;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return dns_name_equal(a->mx.exchange, b->mx.exchange);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_LOC:
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert(a->loc.version == b->loc.version);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return a->loc.size == b->loc.size &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->loc.horiz_pre == b->loc.horiz_pre &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->loc.vert_pre == b->loc.vert_pre &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->loc.latitude == b->loc.latitude &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->loc.longitude == b->loc.longitude &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->loc.altitude == b->loc.altitude;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering default:
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek return a->generic.size == b->generic.size &&
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering }
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poetteringstatic char* format_location(uint32_t latitude, uint32_t longitude, uint32_t altitude,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering uint8_t size, uint8_t horiz_pre, uint8_t vert_pre) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering char *s;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering char NS = latitude >= 1U<<31 ? 'N' : 'S';
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek char EW = longitude >= 1U<<31 ? 'E' : 'W';
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering int lat = latitude >= 1U<<31 ? (int) (latitude - (1U<<31)) : (int) ((1U<<31) - latitude);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering int lon = longitude >= 1U<<31 ? (int) (longitude - (1U<<31)) : (int) ((1U<<31) - longitude);
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek double alt = altitude >= 10000000u ? altitude - 10000000u : -(double)(10000000u - altitude);
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek double siz = (size >> 4) * exp10((double) (size & 0xF));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering double hor = (horiz_pre >> 4) * exp10((double) (horiz_pre & 0xF));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering double ver = (vert_pre >> 4) * exp10((double) (vert_pre & 0xF));
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek if (asprintf(&s, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering (lat / 60000 / 60),
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering (lat / 60000) % 60,
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek (lat % 60000) / 1000.,
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek NS,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering (lon / 60000 / 60),
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering (lon / 60000) % 60,
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek (lon % 60000) / 1000.,
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek EW,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering alt / 100.,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering siz / 100.,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering hor / 100.,
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering ver / 100.) < 0)
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek return NULL;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return s;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringint dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering _cleanup_free_ char *k = NULL;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering char *s;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering int r;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert(rr);
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek r = dns_resource_key_to_string(rr->key, &k);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (r < 0)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return r;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek case DNS_TYPE_PTR:
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_NS:
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_CNAME:
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek case DNS_TYPE_DNAME:
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek s = strjoin(k, " ", rr->ptr.name, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (!s)
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering return -ENOMEM;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
843f737ade9c73609a2280dd3dd16e18222a5dcbŁukasz Stelmach break;
b94801803417c23d099cb7e508754181ecd27f9cZbigniew Jędrzejewski-Szmek
843f737ade9c73609a2280dd3dd16e18222a5dcbŁukasz Stelmach case DNS_TYPE_HINFO:
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek if (!s)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -ENOMEM;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering break;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_SPF: /* exactly the same as TXT */
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek case DNS_TYPE_TXT: {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering _cleanup_free_ char *t;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek t = strv_join_quoted(rr->txt.strings);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering if (!t)
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering return -ENOMEM;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering s = strjoin(k, " ", t, NULL);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering if (!s)
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering return -ENOMEM;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering break;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering }
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering case DNS_TYPE_A: {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering _cleanup_free_ char *x = NULL;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering if (r < 0)
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering return r;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering s = strjoin(k, " ", x, NULL);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering if (!s)
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering return -ENOMEM;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering break;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering }
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering case DNS_TYPE_AAAA: {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering _cleanup_free_ char *x = NULL;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &x);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering if (r < 0)
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering return r;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering s = strjoin(k, " ", x, NULL);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering if (!s)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -ENOMEM;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering break;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering }
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering case DNS_TYPE_SOA:
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering r = asprintf(&s, "%s %s %s %u %u %u %u %u",
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering k,
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering strna(rr->soa.mname),
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering strna(rr->soa.rname),
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering rr->soa.serial,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering rr->soa.refresh,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering rr->soa.retry,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering rr->soa.expire,
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek rr->soa.minimum);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (r < 0)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -ENOMEM;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering break;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_MX:
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = asprintf(&s, "%s %u %s",
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering k,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering rr->mx.priority,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering rr->mx.exchange);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (r < 0)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -ENOMEM;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering break;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_LOC: {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering _cleanup_free_ char *loc;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert(rr->loc.version == 0);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering loc = format_location(rr->loc.latitude,
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek rr->loc.longitude,
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek rr->loc.altitude,
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek rr->loc.size,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering rr->loc.horiz_pre,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering rr->loc.vert_pre);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (!loc)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -ENOMEM;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering s = strjoin(k, " ", loc, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (!s)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -ENOMEM;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering break;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering }
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering default: {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering _cleanup_free_ char *x = NULL;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering x = hexmem(rr->generic.data, rr->generic.size);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (!x)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -ENOMEM;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering s = strjoin(k, " ", x, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (!s)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -ENOMEM;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering break;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering }}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering *ret = s;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringconst char *dns_class_to_string(uint16_t class) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering switch (class) {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_CLASS_IN:
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return "IN";
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_CLASS_ANY:
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return "ANY";
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering }
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering return NULL;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering}
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
c51cf05646a11c65daf65c1123c77efb068f4f7bZbigniew Jędrzejewski-Szmekint dns_class_from_string(const char *s, uint16_t *class) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert(s);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert(class);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (strcaseeq(s, "IN"))
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering *class = DNS_CLASS_IN;
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering else if (strcaseeq(s, "ANY"))
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek *class = DNS_TYPE_ANY;
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering else
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering return -EINVAL;
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering return 0;
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek}
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek
3db604b907323b8df0fc810216f6112056d26a02Lennart Poetteringstatic const struct {
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering uint16_t type;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek const char *name;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek} dns_types[] = {
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering { DNS_TYPE_A, "A" },
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering { DNS_TYPE_NS, "NS" },
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek { DNS_TYPE_CNAME, "CNAME" },
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek { DNS_TYPE_SOA, "SOA" },
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering { DNS_TYPE_PTR, "PTR" },
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering { DNS_TYPE_HINFO, "HINFO" },
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek { DNS_TYPE_MX, "MX" },
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering { DNS_TYPE_TXT, "TXT" },
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering { DNS_TYPE_AAAA, "AAAA" },
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering { DNS_TYPE_LOC, "LOC" },
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek { DNS_TYPE_SRV, "SRV" },
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering { DNS_TYPE_SSHFP, "SSHFP" },
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering { DNS_TYPE_SPF, "SPF" },
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek { DNS_TYPE_DNAME, "DNAME" },
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering { DNS_TYPE_ANY, "ANY" },
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering { DNS_TYPE_OPT, "OPT" },
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering { DNS_TYPE_TKEY, "TKEY" },
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering { DNS_TYPE_TSIG, "TSIG" },
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering { DNS_TYPE_IXFR, "IXFR" },
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering { DNS_TYPE_AXFR, "AXFR" },
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering};
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringconst char *dns_type_to_string(uint16_t type) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering unsigned i;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering for (i = 0; i < ELEMENTSOF(dns_types); i++)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (dns_types[i].type == type)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return dns_types[i].name;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering return NULL;
9a5cb1371b6d8b0a04bd08665bcf9b06cb40c64cZbigniew Jędrzejewski-Szmek}
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
9a5cb1371b6d8b0a04bd08665bcf9b06cb40c64cZbigniew Jędrzejewski-Szmekint dns_type_from_string(const char *s, uint16_t *type) {
9a5cb1371b6d8b0a04bd08665bcf9b06cb40c64cZbigniew Jędrzejewski-Szmek unsigned i;
9a5cb1371b6d8b0a04bd08665bcf9b06cb40c64cZbigniew Jędrzejewski-Szmek
9a5cb1371b6d8b0a04bd08665bcf9b06cb40c64cZbigniew Jędrzejewski-Szmek assert(s);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering assert(type);
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering for (i = 0; i < ELEMENTSOF(dns_types); i++)
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering if (strcaseeq(dns_types[i].name, s)) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering *type = dns_types[i].type;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return 0;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering }
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return -EINVAL;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering}
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering