resolved-dns-rr.c revision 946c70944ebdf428ffeb9991a7449edbd4011461
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Lennart 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 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 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/>.
4ad7f2761da661853dcc29d542efb4727abb1101Nick OwensDnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering k = malloc0(sizeof(DnsResourceKey) + l + 1);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering strcpy((char*) k + sizeof(DnsResourceKey), name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom GundersenDnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart PoetteringDnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersenint dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringint dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringint dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringunsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering unsigned long ul;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering ul = dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), hash_key);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint dns_resource_key_compare_func(const void *a, const void *b) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const DnsResourceKey *x = a, *y = b;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
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;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering s = strjoin(DNS_RESOURCE_KEY_NAME(key), " ", c, " ", t, NULL);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart PoetteringDnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
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 key = dns_resource_key_new(class, type, name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (IN_SET(rr->key->type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering else if (rr->key->type == DNS_TYPE_HINFO) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering } else if (rr->key->type == DNS_TYPE_SOA) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering } else if (rr->key->type == DNS_TYPE_MX) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering } else if (!IN_SET(rr->key->type, DNS_TYPE_A, DNS_TYPE_AAAA))
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 r = dns_name_reverse(family, address, &ptr);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringint dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = dns_resource_key_equal(a->key, b->key);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return dns_name_equal(a->ptr.name, b->ptr.name);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_name_equal(a->soa.mname, b->soa.mname);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_name_equal(a->soa.rname, b->soa.rname);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return dns_name_equal(a->mx.exchange, b->mx.exchange);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return a->generic.size == b->generic.size &&
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_resource_key_to_string(rr->key, &k);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &x);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = asprintf(&s, "%s %s %s %u %u %u %u %u",
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering x = hexmem(rr->generic.data, rr->generic.size);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringconst char *dns_class_to_string(uint16_t class) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_class_from_string(const char *s, uint16_t *class) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringstatic const struct {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poetteringconst char *dns_type_to_string(uint16_t type) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering for (i = 0; i < ELEMENTSOF(dns_types); i++)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringint dns_type_from_string(const char *s, uint16_t *type) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering for (i = 0; i < ELEMENTSOF(dns_types); i++)