faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering This file is part of systemd.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Copyright 2014 Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering systemd is free software; you can redistribute it and/or modify it
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering under the terms of the GNU Lesser General Public License as published by
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering (at your option) any later version.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering systemd is distributed in the hope that it will be useful, but
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Lesser General Public License for more details.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering You should have received a copy of the GNU Lesser General Public License
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsQuestion *dns_question_new(unsigned n) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering q = malloc0(offsetof(DnsQuestion, keys) + sizeof(DnsResourceKey*) * n);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsQuestion *dns_question_ref(DnsQuestion *q) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsQuestion *dns_question_unref(DnsQuestion *q) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering for (i = 0; i < q->n_keys; i++)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint dns_question_add(DnsQuestion *q, DnsResourceKey *key) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering for (i = 0; i < q->n_keys; i++) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = dns_resource_key_equal(q->keys[i], key);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering q->keys[q->n_keys++] = dns_resource_key_ref(key);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringint dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering for (i = 0; i < q->n_keys; i++) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = dns_resource_key_match_rr(q->keys[i], rr, search_domain);
542e0c84d1518a1515e03194dd25299b2652778cLennart Poetteringint dns_question_matches_cname_or_dname(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {
542e0c84d1518a1515e03194dd25299b2652778cLennart Poettering if (!IN_SET(rr->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering for (i = 0; i < q->n_keys; i++) {
542e0c84d1518a1515e03194dd25299b2652778cLennart Poettering /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
542e0c84d1518a1515e03194dd25299b2652778cLennart Poettering if (!dns_type_may_redirect(q->keys[i]->type))
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen r = dns_resource_key_match_cname_or_dname(q->keys[i], rr->key, search_domain);
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poetteringint dns_question_is_valid_for_query(DnsQuestion *q) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering name = DNS_RESOURCE_KEY_NAME(q->keys[0]);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Check that all keys in this question bear the same name */
0f7091e624fdba6c0bf281f2a9a23cd3e9ca93fbLennart Poettering for (i = 0; i < q->n_keys; i++) {
0f7091e624fdba6c0bf281f2a9a23cd3e9ca93fbLennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(q->keys[i]), name);
0f7091e624fdba6c0bf281f2a9a23cd3e9ca93fbLennart Poettering if (!dns_type_is_valid_query(q->keys[i]->type))
6a21960c0be378799db51a2735ff68474e5e21f8Lennart Poetteringint dns_question_contains(DnsQuestion *a, const DnsResourceKey *k) {
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering for (j = 0; j < a->n_keys; j++) {
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering r = dns_resource_key_equal(a->keys[j], k);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poetteringint dns_question_is_equal(DnsQuestion *a, DnsQuestion *b) {
8013e860b6344cb109e68208a3a91b0fc3cb9ed1Lennart Poettering return !b || b->n_keys == 0;
8013e860b6344cb109e68208a3a91b0fc3cb9ed1Lennart Poettering return a->n_keys == 0;
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering /* Checks if all keys in a are also contained b, and vice versa */
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering for (j = 0; j < a->n_keys; j++) {
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering r = dns_question_contains(b, a->keys[j]);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering for (j = 0; j < b->n_keys; j++) {
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering r = dns_question_contains(a, b->keys[j]);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersenint dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname, DnsQuestion **ret) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *n = NULL;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering const char *d;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering r = dns_name_change_suffix(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname->key), cname->dname.name, &destination);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(key), d);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* Fully the same, indicate we didn't do a thing */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Create a new question, and patch in the new name */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering k = dns_resource_key_new_redirect(key, cname);
703e4f5e39c019da8c002ba10bd450ce378c0e91Lennart Poetteringconst char *dns_question_first_name(DnsQuestion *q) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return DNS_RESOURCE_KEY_NAME(q->keys[0]);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringint dns_question_new_address(DnsQuestion **ret, int family, const char *name, bool convert_idna) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering q = dns_question_new(family == AF_UNSPEC ? 2 : 1);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, name);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringint dns_question_new_reverse(DnsQuestion **ret, int family, const union in_addr_union *a) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = dns_name_reverse(family, a, &reverse);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, reverse);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering _cleanup_free_ char *buf = NULL, *joined = NULL;
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* We support three modes of invocation:
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * 1. Only a domain is specified, in which case we assume a properly encoded SRV RR name, including service
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * type and possibly a service name. If specified in this way we assume it's already IDNA converted if
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * that's necessary.
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * 2. Both service type and a domain specified, in which case a normal SRV RR is assumed, without a DNS-SD
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * style prefix. In this case we'll IDNA convert the domain, if that's requested.
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * 3. All three of service name, type and domain are specified, in which case a DNS-SD service is put
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * together. The service name is never IDNA converted, and the domain is if requested.
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering * It's not supported to specify a service name without a type, or no domain name.
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering r = dns_service_join(service, type, domain, &joined);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, name);