1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "resolved-dns-question.h"
23 #include "resolved-dns-domain.h"
25 DnsQuestion *dns_question_new(unsigned n) {
30 q = malloc0(offsetof(DnsQuestion, keys) + sizeof(DnsResourceKey*) * n);
40 DnsQuestion *dns_question_ref(DnsQuestion *q) {
49 DnsQuestion *dns_question_unref(DnsQuestion *q) {
58 for (i = 0; i < q->n_keys; i++)
59 dns_resource_key_unref(q->keys[i]);
67 int dns_question_add(DnsQuestion *q, DnsResourceKey *key) {
74 for (i = 0; i < q->n_keys; i++) {
75 r = dns_resource_key_equal(q->keys[i], key);
82 if (q->n_keys >= q->n_allocated)
85 q->keys[q->n_keys++] = dns_resource_key_ref(key);
89 int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr) {
96 for (i = 0; i < q->n_keys; i++) {
97 r = dns_resource_key_match_rr(q->keys[i], rr);
105 int dns_question_matches_cname(DnsQuestion *q, DnsResourceRecord *rr) {
112 for (i = 0; i < q->n_keys; i++) {
113 r = dns_resource_key_match_cname(q->keys[i], rr);
121 int dns_question_is_valid(DnsQuestion *q) {
131 if (q->n_keys > 65535)
134 name = DNS_RESOURCE_KEY_NAME(q->keys[0]);
138 /* Check that all keys in this question bear the same name */
139 for (i = 1; i < q->n_keys; i++) {
142 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(q->keys[i]), name);
150 int dns_question_is_superset(DnsQuestion *q, DnsQuestion *other) {
157 /* Checks if all keys in "other" are also contained in "q" */
159 for (j = 0; j < other->n_keys; j++) {
160 DnsResourceKey *b = other->keys[j];
164 for (i = 0; i < q->n_keys; i++) {
165 DnsResourceKey *a = q->keys[i];
167 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
174 if (a->class != b->class && a->class != DNS_CLASS_ANY)
177 if (a->type != b->type && a->type != DNS_TYPE_ANY)
191 int dns_question_cname_redirect(DnsQuestion *q, const char *name, DnsQuestion **ret) {
192 _cleanup_(dns_question_unrefp) DnsQuestion *n = NULL;
201 for (i = 0; i < q->n_keys; i++) {
202 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(q->keys[i]), name);
213 /* Shortcut, the names are already right */
214 *ret = dns_question_ref(q);
218 n = dns_question_new(q->n_keys);
222 /* Create a new question, and patch in the new name */
223 for (i = 0; i < q->n_keys; i++) {
224 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL;
226 k = dns_resource_key_new(q->keys[i]->class, q->keys[i]->type, name);
230 r = dns_question_add(n, k);
241 int dns_question_endswith(DnsQuestion *q, const char *suffix) {
247 for (i = 0; i < q->n_keys; i++) {
250 k = dns_name_endswith(DNS_RESOURCE_KEY_NAME(q->keys[i]), suffix);
258 int dns_question_extract_reverse_address(DnsQuestion *q, int *family, union in_addr_union *address) {
265 for (i = 0; i < q->n_keys; i++) {
268 k = dns_name_address(DNS_RESOURCE_KEY_NAME(q->keys[i]), family, address);