resolved-dns-question.c revision 8013e860b6344cb109e68208a3a91b0fc3cb9ed1
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering/***
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering This file is part of systemd.
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering Copyright 2014 Lennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering systemd is free software; you can redistribute it and/or modify it
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering under the terms of the GNU Lesser General Public License as published by
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering (at your option) any later version.
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering systemd is distributed in the hope that it will be useful, but
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering Lesser General Public License for more details.
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering You should have received a copy of the GNU Lesser General Public License
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering***/
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering#include "resolved-dns-question.h"
4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3Lennart Poettering#include "dns-domain.h"
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
4871690d9e32608bbd9b18505b5326c2079c9690Allin CottrellDnsQuestion *dns_question_new(unsigned n) {
4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3Lennart Poettering DnsQuestion *q;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3Lennart Poettering assert(n > 0);
4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3Lennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering q = malloc0(offsetof(DnsQuestion, keys) + sizeof(DnsResourceKey*) * n);
4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3Lennart Poettering if (!q)
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering return NULL;
4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3Lennart Poettering
afc5dbf37fd2399d37976388d9dd9ab470ecf446Lennart Poettering q->n_ref = 1;
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering q->n_allocated = n;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering return q;
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering}
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart PoetteringDnsQuestion *dns_question_ref(DnsQuestion *q) {
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering if (!q)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return NULL;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(q->n_ref > 0);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering q->n_ref++;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return q;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart PoetteringDnsQuestion *dns_question_unref(DnsQuestion *q) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!q)
3b97fcbd28f92a1e51887fef5de8844a89bde523Lennart Poettering return NULL;
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmek
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(q->n_ref > 0);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (q->n_ref == 1) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned i;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (i = 0; i < q->n_keys; i++)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering dns_resource_key_unref(q->keys[i]);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering free(q);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering } else
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering q->n_ref--;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return NULL;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringint dns_question_add(DnsQuestion *q, DnsResourceKey *key) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned i;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(key);
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmek
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!q)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return -ENOSPC;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (i = 0; i < q->n_keys; i++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = dns_resource_key_equal(q->keys[i], key);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r < 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r > 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmek }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (q->n_keys >= q->n_allocated)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return -ENOSPC;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering q->keys[q->n_keys++] = dns_resource_key_ref(key);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringint dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned i;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(rr);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!q)
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (i = 0; i < q->n_keys; i++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = dns_resource_key_match_rr(q->keys[i], rr);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r != 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringint dns_question_matches_cname(DnsQuestion *q, DnsResourceRecord *rr) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned i;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(rr);
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!q)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (i = 0; i < q->n_keys; i++) {
e9f600f2fb4b0df55c7a8fb4b4d09f9979997223Lennart Poettering r = dns_resource_key_match_cname(q->keys[i], rr);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r != 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringint dns_question_is_valid(DnsQuestion *q) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering const char *name;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned i;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!q)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (q->n_keys <= 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (q->n_keys > 65535)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering name = DNS_RESOURCE_KEY_NAME(q->keys[0]);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!name)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* Check that all keys in this question bear the same name */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (i = 1; i < q->n_keys; i++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(q->keys[i]);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(q->keys[i]), name);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r <= 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 1;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringint dns_question_is_superset(DnsQuestion *q, DnsQuestion *other) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned j;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
507f22bd0172bff5e5d98145b1419bd472a2c57fZbigniew Jędrzejewski-Szmek /* Checks if all keys in "other" are also contained in "q" */
b9c488f60050248b35640f28e4d00958702ba1c3Eelco Dolstra
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!other)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 1;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (j = 0; j < other->n_keys; j++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering DnsResourceKey *b = other->keys[j];
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering bool found = false;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned i;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!q)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (i = 0; i < q->n_keys; i++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering DnsResourceKey *a = q->keys[i];
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r < 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r == 0)
e9f600f2fb4b0df55c7a8fb4b4d09f9979997223Lennart Poettering continue;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (a->class != b->class && a->class != DNS_CLASS_ANY)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering continue;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (a->type != b->type && a->type != DNS_TYPE_ANY)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering continue;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering found = true;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering break;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!found)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 1;
dc61b7e45d89a69f0469ab7b3289cdde7fcc55abTorstein Husebø}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringint dns_question_contains(DnsQuestion *a, DnsResourceKey *k) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned j;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(k);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!a)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (j = 0; j < a->n_keys; j++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = dns_resource_key_equal(a->keys[j], k);
527b7a421ff3927d4f3f170b1b143452e88ae1dcLennart Poettering if (r != 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringint dns_question_is_equal(DnsQuestion *a, DnsQuestion *b) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned j;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!a)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return !b || b->n_keys == 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!b)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return a->n_keys == 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* Checks if all keys in a are also contained b, and vice versa */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (j = 0; j < a->n_keys; j++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = dns_question_contains(b, a->keys[j]);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r <= 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (j = 0; j < b->n_keys; j++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = dns_question_contains(a, b->keys[j]);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r <= 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 1;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringint dns_question_cname_redirect(DnsQuestion *q, const char *name, DnsQuestion **ret) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *n = NULL;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering bool same = true;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned i;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(name);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(ret);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!q) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering n = dns_question_new(0);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!n)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return -ENOMEM;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering *ret = n;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering n = 0;
4b94f3b8f7693f076e5c85bc2c02cf028192d8deZbigniew Jędrzejewski-Szmek return 0;
4b94f3b8f7693f076e5c85bc2c02cf028192d8deZbigniew Jędrzejewski-Szmek }
4b94f3b8f7693f076e5c85bc2c02cf028192d8deZbigniew Jędrzejewski-Szmek
4b94f3b8f7693f076e5c85bc2c02cf028192d8deZbigniew Jędrzejewski-Szmek for (i = 0; i < q->n_keys; i++) {
4b94f3b8f7693f076e5c85bc2c02cf028192d8deZbigniew Jędrzejewski-Szmek r = dns_name_equal(DNS_RESOURCE_KEY_NAME(q->keys[i]), name);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r < 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r == 0) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering same = false;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering break;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
e9f600f2fb4b0df55c7a8fb4b4d09f9979997223Lennart Poettering if (same) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering /* Shortcut, the names are already right */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering *ret = dns_question_ref(q);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering n = dns_question_new(q->n_keys);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!n)
36dd072cdf03dcac0fcd2d6b42f261444dc7ac88Michal Sekletar return -ENOMEM;
36dd072cdf03dcac0fcd2d6b42f261444dc7ac88Michal Sekletar
36dd072cdf03dcac0fcd2d6b42f261444dc7ac88Michal Sekletar /* Create a new question, and patch in the new name */
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (i = 0; i < q->n_keys; i++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
e88baee88fad8bc59d33b55a7a2d640ef9e16cd6Zbigniew Jędrzejewski-Szmek k = dns_resource_key_new(q->keys[i]->class, q->keys[i]->type, name);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!k)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return -ENOMEM;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = dns_question_add(n, k);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (r < 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return r;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering *ret = n;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering n = NULL;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 1;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringint dns_question_endswith(DnsQuestion *q, const char *suffix) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned i;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(suffix);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
527b7a421ff3927d4f3f170b1b143452e88ae1dcLennart Poettering if (!q)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 1;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek for (i = 0; i < q->n_keys; i++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int k;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering k = dns_name_endswith(DNS_RESOURCE_KEY_NAME(q->keys[i]), suffix);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (k <= 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return k;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 1;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringint dns_question_extract_reverse_address(DnsQuestion *q, int *family, union in_addr_union *address) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering unsigned i;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(family);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering assert(address);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (!q)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering for (i = 0; i < q->n_keys; i++) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int k;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering k = dns_name_address(DNS_RESOURCE_KEY_NAME(q->keys[i]), family, address);
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering if (k != 0)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return k;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering }
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering return 0;
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering}
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering