resolved-dns-question.c revision 0f7091e624fdba6c0bf281f2a9a23cd3e9ca93fb
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen This file is part of systemd.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Copyright 2014 Lennart Poettering
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is free software; you can redistribute it and/or modify it
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen under the terms of the GNU Lesser General Public License as published by
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen (at your option) any later version.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen systemd is distributed in the hope that it will be useful, but
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen Lesser General Public License for more details.
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen You should have received a copy of the GNU Lesser General Public License
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering q = malloc0(offsetof(DnsQuestion, keys) + sizeof(DnsResourceKey*) * n);
99634696183dfabae20104e58157c69029a11594Tom GundersenDnsQuestion *dns_question_ref(DnsQuestion *q) {
99634696183dfabae20104e58157c69029a11594Tom GundersenDnsQuestion *dns_question_unref(DnsQuestion *q) {
99634696183dfabae20104e58157c69029a11594Tom Gundersen for (i = 0; i < q->n_keys; i++)
99634696183dfabae20104e58157c69029a11594Tom Gundersenint dns_question_add(DnsQuestion *q, DnsResourceKey *key) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen for (i = 0; i < q->n_keys; i++) {
99634696183dfabae20104e58157c69029a11594Tom Gundersen q->keys[q->n_keys++] = dns_resource_key_ref(key);
20af7091de0cdf92bf299addfc3f96c3ef805bd8Tom Gundersenint dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen for (i = 0; i < q->n_keys; i++) {
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering r = dns_resource_key_match_rr(q->keys[i], rr, search_domain);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenint dns_question_matches_cname(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen for (i = 0; i < q->n_keys; i++) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen r = dns_resource_key_match_cname_or_dname(q->keys[i], rr->key, search_domain);
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersenint dns_question_is_valid_for_query(DnsQuestion *q) {
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen /* Check that all keys in this question bear the same name */
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersen for (i = 0; i < q->n_keys; i++) {
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(q->keys[i]), name);
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering if (!dns_type_is_valid_query(q->keys[i]->type))
87322b3aee0dc649ff1ae7a403dcc9d7305baba2Tom Gundersenint dns_question_contains(DnsQuestion *a, const DnsResourceKey *k) {
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering for (j = 0; j < a->n_keys; j++) {
3bdace9bf779ce051f00c14914b35c3a26164aa9Lennart Poettering r = dns_resource_key_equal(a->keys[j], k);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenint dns_question_is_equal(DnsQuestion *a, DnsQuestion *b) {
3a864fe4a894745ac61f1ecabd7cadf04139a284Tom Gundersen return !b || b->n_keys == 0;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen return a->n_keys == 0;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen /* Checks if all keys in a are also contained b, and vice versa */
3733eec3e292e4ddb4cba5eb8d3bd8cbee7102d8Lennart Poettering for (j = 0; j < a->n_keys; j++) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt for (j = 0; j < b->n_keys; j++) {
586ac6f711e2eccceb12421df22fca4f117226c4Lennart Poettering r = dns_question_contains(a, b->keys[j]);
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersenint dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname, DnsQuestion **ret) {
a6f1e036de8f212f33ead7f5387c297afd8be26eTom Gundersen _cleanup_(dns_question_unrefp) DnsQuestion *n = NULL;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen for (i = 0; i < q->n_keys; i++) {
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen const char *d;
b44cd8821087f2afebf85fec5b588f5720a9415cTom Gundersen r = dns_name_change_suffix(DNS_RESOURCE_KEY_NAME(q->keys[i]), DNS_RESOURCE_KEY_NAME(cname->key), cname->dname.name, &destination);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen r = dns_name_equal(DNS_RESOURCE_KEY_NAME(q->keys[i]), d);
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen if (r == 0) {
ff734080aa02cd70b13bc0fdeec4a5886166163aTom Gundersen /* Shortcut, the names are already right */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen /* Create a new question, and patch in the new name */
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen for (i = 0; i < q->n_keys; i++) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen k = dns_resource_key_new_redirect(q->keys[i], cname);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenconst char *dns_question_first_name(DnsQuestion *q) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenint dns_question_new_address(DnsQuestion **ret, int family, const char *name) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen q = dns_question_new(family == AF_UNSPEC ? 2 : 1);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, name);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenint dns_question_new_reverse(DnsQuestion **ret, int family, const union in_addr_union *a) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, reverse);
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersenint dns_question_new_service(DnsQuestion **ret, const char *name, bool with_txt) {
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen _cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
969b009d9416806911b9b52e7e7bc619c0c1a931Tom Gundersen key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, name);