resolved-dns-rr.c revision 946c70944ebdf428ffeb9991a7449edbd4011461
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 "resolved-dns-domain.h"
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include "resolved-dns-rr.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering
4ad7f2761da661853dcc29d542efb4727abb1101Nick OwensDnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek DnsResourceKey *k;
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering size_t l;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering assert(name);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering l = strlen(name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering k = malloc0(sizeof(DnsResourceKey) + l + 1);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!k)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart 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_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;
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen k->n_ref = 1;
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen k->class = class;
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen k->type = type;
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen k->_name = name;
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen return k;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering}
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom GundersenDnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen if (!k)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return NULL;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(k->n_ref > 0);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering k->n_ref++;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return k;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering}
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart PoetteringDnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (!k)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return NULL;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(k->n_ref > 0);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (k->n_ref == 1) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering free(k->_name);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering free(k);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering } else
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering k->n_ref--;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return NULL;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering}
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersenint dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering int r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r <= 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (a->class != b->class)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (a->type != b->type)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 1;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringint dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr) {
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 != key->type && key->type != DNS_TYPE_ANY)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringint dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(key);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(rr);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (rr->key->type != DNS_TYPE_CNAME)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringunsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const DnsResourceKey *k = i;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering unsigned long ul;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering ul = dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), hash_key);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering ul = ul * hash_key[0] + ul + k->class;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering ul = ul * hash_key[1] + ul + k->type;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return ul;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint dns_resource_key_compare_func(const void *a, const void *b) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const DnsResourceKey *x = a, *y = b;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int ret;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (ret != 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return ret;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (x->type < y->type)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (x->type > y->type)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (x->class < y->class)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (x->class > y->class)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char cbuf[DECIMAL_STR_MAX(uint16_t)], tbuf[DECIMAL_STR_MAX(uint16_t)];
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *c, *t;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char *s;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering c = dns_class_to_string(key->class);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!c) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sprintf(cbuf, "%i", key->class);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering c = cbuf;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering t = dns_type_to_string(key->type);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!t){
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering sprintf(tbuf, "%i", key->type);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering t = tbuf;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering s = strjoin(DNS_RESOURCE_KEY_NAME(key), " ", c, " ", t, NULL);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!s)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return -ENOMEM;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering *ret = s;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart PoetteringDnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering DnsResourceRecord *rr;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rr = new0(DnsResourceRecord, 1);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!rr)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rr->n_ref = 1;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering rr->key = dns_resource_key_ref(key);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return rr;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart PoetteringDnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering key = dns_resource_key_new(class, type, name);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!key)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return dns_resource_record_new(key);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!rr)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(rr->n_ref > 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rr->n_ref++;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return rr;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (!rr)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return NULL;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(rr->n_ref > 0);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (rr->n_ref > 1) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering rr->n_ref--;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (rr->key) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (IN_SET(rr->key->type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering free(rr->ptr.name);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering else if (rr->key->type == DNS_TYPE_HINFO) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering free(rr->hinfo.cpu);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering free(rr->hinfo.os);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering } else if (rr->key->type == DNS_TYPE_SOA) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering free(rr->soa.mname);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering free(rr->soa.rname);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering } else if (rr->key->type == DNS_TYPE_MX) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering free(rr->mx.exchange);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering } else if (!IN_SET(rr->key->type, DNS_TYPE_A, DNS_TYPE_AAAA))
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering free(rr->generic.data);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering dns_resource_key_unref(rr->key);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen free(rr);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return NULL;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersenint dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_free_ char *ptr = NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(ret);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(address);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(hostname);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = dns_name_reverse(family, address, &ptr);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!key)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -ENOMEM;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering ptr = NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering rr = dns_resource_record_new(key);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!rr)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -ENOMEM;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering rr->ptr.name = strdup(hostname);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!rr->ptr.name)
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt return -ENOMEM;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt *ret = rr;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt rr = NULL;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return 0;
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringint dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(a);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen assert(b);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = dns_resource_key_equal(a->key, b->key);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r <= 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen switch (a->key->type) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case DNS_TYPE_PTR:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case DNS_TYPE_NS:
23432a1c249b9c513e438bffe9778a7ce2ee74feZbigniew Jędrzejewski-Szmek case DNS_TYPE_CNAME:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return dns_name_equal(a->ptr.name, b->ptr.name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering case DNS_TYPE_HINFO:
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering strcaseeq(a->hinfo.os, b->hinfo.os);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering case DNS_TYPE_A:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case DNS_TYPE_AAAA:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering case DNS_TYPE_SOA:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_name_equal(a->soa.mname, b->soa.mname);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r <= 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_name_equal(a->soa.rname, b->soa.rname);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r <= 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return a->soa.serial == b->soa.serial &&
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering a->soa.refresh == b->soa.refresh &&
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering a->soa.retry == b->soa.retry &&
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering a->soa.expire == b->soa.expire &&
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering a->soa.minimum == b->soa.minimum;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering case DNS_TYPE_MX:
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (a->mx.priority != b->mx.priority)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return 0;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return dns_name_equal(a->mx.exchange, b->mx.exchange);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering default:
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return a->generic.size == b->generic.size &&
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *k = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char *s;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert(rr);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_resource_key_to_string(rr->key, &k);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering switch (rr->key->type) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering case DNS_TYPE_PTR:
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek case DNS_TYPE_NS:
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering case DNS_TYPE_CNAME:
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering s = strjoin(k, " ", rr->ptr.name, NULL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (!s)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return -ENOMEM;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek break;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek case DNS_TYPE_HINFO:
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!s)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return -ENOMEM;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering break;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering case DNS_TYPE_A: {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *x = NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (r < 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek s = strjoin(k, " ", x, NULL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (!s)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return -ENOMEM;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering break;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering }
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering case DNS_TYPE_AAAA: {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek _cleanup_free_ char *x = NULL;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &x);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (r < 0)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return r;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen s = strjoin(k, " ", x, NULL);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (!s)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return -ENOMEM;
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen break;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek case DNS_TYPE_SOA:
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = asprintf(&s, "%s %s %s %u %u %u %u %u",
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek k,
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek strna(rr->soa.mname),
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek strna(rr->soa.rname),
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek rr->soa.serial,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek rr->soa.refresh,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek rr->soa.retry,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek rr->soa.expire,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen rr->soa.minimum);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (r < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return -ENOMEM;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen break;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen case DNS_TYPE_MX:
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = asprintf(&s, "%s %u %s",
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen k,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen rr->mx.priority,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen rr->mx.exchange);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -ENOMEM;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek break;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek default: {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering _cleanup_free_ char *x = NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering x = hexmem(rr->generic.data, rr->generic.size);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (!x)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -ENOMEM;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering s = strjoin(k, " ", x, NULL);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!s)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -ENOMEM;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering break;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering *ret = s;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return 0;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringconst char *dns_class_to_string(uint16_t class) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering switch (class) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering case DNS_CLASS_IN:
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return "IN";
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering case DNS_CLASS_ANY:
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return "ANY";
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return NULL;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_class_from_string(const char *s, uint16_t *class) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(s);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering assert(class);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (strcaseeq(s, "IN"))
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering *class = DNS_CLASS_IN;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering else if (strcaseeq(s, "ANY"))
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering *class = DNS_TYPE_ANY;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering else
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return -EINVAL;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return 0;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringstatic const struct {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering uint16_t type;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char *name;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering} dns_types[] = {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_A, "A" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_NS, "NS" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_CNAME, "CNAME" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_SOA, "SOA" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_PTR, "PTR" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_HINFO, "HINFO" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_MX, "MX" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_TXT, "TXT" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_AAAA, "AAAA" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_SRV, "SRV" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_SSHFP, "SSHFP" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_DNAME, "DNAME" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_ANY, "ANY" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_OPT, "OPT" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_TKEY, "TKEY" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_TSIG, "TSIG" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_IXFR, "IXFR" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering { DNS_TYPE_AXFR, "AXFR" },
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering};
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poetteringconst char *dns_type_to_string(uint16_t type) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering unsigned i;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering for (i = 0; i < ELEMENTSOF(dns_types); i++)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (dns_types[i].type == type)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return dns_types[i].name;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringint dns_type_from_string(const char *s, uint16_t *type) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering unsigned i;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(s);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(type);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering for (i = 0; i < ELEMENTSOF(dns_types); i++)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (strcaseeq(dns_types[i].name, s)) {
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering *type = dns_types[i].type;
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering return 0;
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering }
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -EINVAL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering}
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering