From 51323288fc628a5cac50914df915545d685b793e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 14 Aug 2014 01:00:15 +0200 Subject: resolved: allow passing on which protocol, family and interface to look something up Also, return on which protocol/family/interface we found something. --- src/resolve/resolved-bus.c | 97 ++++++++++++++++++++++++++++----------- src/resolve/resolved-def.h | 30 ++++++++++++ src/resolve/resolved-dns-packet.h | 14 ++++++ src/resolve/resolved-dns-query.c | 12 +++-- src/resolve/resolved-dns-query.h | 7 ++- src/resolve/resolved-dns-scope.c | 8 +++- src/resolve/resolved-dns-scope.h | 2 +- 7 files changed, 137 insertions(+), 33 deletions(-) create mode 100644 src/resolve/resolved-def.h (limited to 'src/resolve') diff --git a/src/resolve/resolved-bus.c b/src/resolve/resolved-bus.c index cfe12d3bed..0029023bcc 100644 --- a/src/resolve/resolved-bus.c +++ b/src/resolve/resolved-bus.c @@ -24,6 +24,7 @@ #include "resolved-dns-domain.h" #include "resolved-bus.h" +#include "resolved-def.h" static int reply_query_state(DnsQuery *q) { _cleanup_free_ char *ip = NULL; @@ -90,13 +91,13 @@ static int reply_query_state(DnsQuery *q) { } } -static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) { +static int append_address(sd_bus_message *reply, DnsResourceRecord *rr) { int r; assert(reply); assert(rr); - r = sd_bus_message_open_container(reply, 'r', "iayi"); + r = sd_bus_message_open_container(reply, 'r', "iay"); if (r < 0) return r; @@ -119,10 +120,6 @@ static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifin if (r < 0) return r; - r = sd_bus_message_append(reply, "i", ifindex); - if (r < 0) - return r; - r = sd_bus_message_close_container(reply); if (r < 0) return r; @@ -135,7 +132,7 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) { _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; unsigned added = 0, i; - int r, ifindex; + int r; assert(q); @@ -148,11 +145,13 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) { if (r < 0) goto finish; - r = sd_bus_message_open_container(reply, 'a', "(iayi)"); + r = sd_bus_message_append(reply, "i", q->answer_ifindex); if (r < 0) goto finish; - ifindex = q->answer_ifindex; + r = sd_bus_message_open_container(reply, 'a', "(iay)"); + if (r < 0) + goto finish; if (q->answer) { answer = dns_answer_ref(q->answer); @@ -173,7 +172,7 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) { continue; } - r = append_address(reply, answer->rrs[i], ifindex); + r = append_address(reply, answer->rrs[i]); if (r < 0) goto finish; @@ -211,7 +210,7 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) { if (r == 0) continue; - r = append_address(reply, answer->rrs[i], ifindex); + r = append_address(reply, answer->rrs[i]); if (r < 0) goto finish; @@ -244,7 +243,7 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) { /* Return the precise spelling and uppercasing reported by the server */ assert(canonical); - r = sd_bus_message_append(reply, "s", DNS_RESOURCE_KEY_NAME(canonical->key)); + r = sd_bus_message_append(reply, "st", DNS_RESOURCE_KEY_NAME(canonical->key), SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family)); if (r < 0) goto finish; @@ -259,11 +258,27 @@ finish: dns_query_free(q); } +static int check_ifindex_flags(int ifindex, uint64_t *flags, sd_bus_error *error) { + assert(flags); + + if (ifindex < 0) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index"); + + if (*flags & ~SD_RESOLVED_FLAGS_ALL) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter"); + + if (*flags == 0) + *flags = SD_RESOLVED_FLAGS_DEFAULT; + + return 0; +} + static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; Manager *m = userdata; const char *hostname; - int family; + int family, ifindex; + uint64_t flags; DnsQuery *q; int r; @@ -271,7 +286,7 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi assert(message); assert(m); - r = sd_bus_message_read(message, "si", &hostname, &family); + r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags); if (r < 0) return r; @@ -282,6 +297,10 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi if (r < 0) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname); + r = check_ifindex_flags(ifindex, &flags, error); + if (r < 0) + return r; + question = dns_question_new(family == AF_UNSPEC ? 2 : 1); if (!question) return -ENOMEM; @@ -310,7 +329,7 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi return r; } - r = dns_query_new(m, &q, question); + r = dns_query_new(m, &q, question, ifindex, flags); if (r < 0) return r; @@ -353,6 +372,10 @@ static void bus_method_resolve_address_complete(DnsQuery *q) { if (r < 0) goto finish; + r = sd_bus_message_append(reply, "i", q->answer_ifindex); + if (r < 0) + goto finish; + r = sd_bus_message_open_container(reply, 'a', "s"); if (r < 0) goto finish; @@ -388,6 +411,10 @@ static void bus_method_resolve_address_complete(DnsQuery *q) { if (r < 0) goto finish; + r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family)); + if (r < 0) + goto finish; + r = sd_bus_send(q->manager->bus, reply, NULL); finish: @@ -405,6 +432,7 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void _cleanup_free_ char *reverse = NULL; Manager *m = userdata; int family, ifindex; + uint64_t flags; const void *d; DnsQuery *q; size_t sz; @@ -414,7 +442,7 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void assert(message); assert(m); - r = sd_bus_message_read(message, "i", &family); + r = sd_bus_message_read(message, "ii", &ifindex, &family); if (r < 0) return r; @@ -428,11 +456,13 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void if (sz != FAMILY_ADDRESS_SIZE(family)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size"); - r = sd_bus_message_read(message, "i", &ifindex); + r = sd_bus_message_read(message, "t", &flags); + if (r < 0) + return r; + + r = check_ifindex_flags(ifindex, &flags, error); if (r < 0) return r; - if (ifindex < 0) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index"); r = dns_name_reverse(family, d, &reverse); if (r < 0) @@ -452,7 +482,7 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void if (r < 0) return r; - r = dns_query_new(m, &q, question); + r = dns_query_new(m, &q, question, ifindex, flags); if (r < 0) return r; @@ -495,6 +525,10 @@ static void bus_method_resolve_record_complete(DnsQuery *q) { if (r < 0) goto finish; + r = sd_bus_message_append(reply, "i", q->answer_ifindex); + if (r < 0) + goto finish; + r = sd_bus_message_open_container(reply, 'a', "(qqay)"); if (r < 0) goto finish; @@ -549,6 +583,10 @@ static void bus_method_resolve_record_complete(DnsQuery *q) { if (r < 0) goto finish; + r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family)); + if (r < 0) + goto finish; + r = sd_bus_send(q->manager->bus, reply, NULL); finish: @@ -564,16 +602,17 @@ static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; Manager *m = userdata; - DnsQuery *q; - int r; uint16_t class, type; const char *name; + int r, ifindex; + uint64_t flags; + DnsQuery *q; assert(bus); assert(message); assert(m); - r = sd_bus_message_read(message, "sqq", &name, &class, &type); + r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags); if (r < 0) return r; @@ -581,6 +620,10 @@ static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void if (r < 0) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name); + r = check_ifindex_flags(ifindex, &flags, error); + if (r < 0) + return r; + question = dns_question_new(1); if (!question) return -ENOMEM; @@ -593,7 +636,7 @@ static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void if (r < 0) return r; - r = dns_query_new(m, &q, question); + r = dns_query_new(m, &q, question, ifindex, flags); if (r < 0) return r; @@ -620,9 +663,9 @@ static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void static const sd_bus_vtable resolve_vtable[] = { SD_BUS_VTABLE_START(0), - SD_BUS_METHOD("ResolveHostname", "si", "a(iayi)s", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("ResolveAddress", "iayi", "as", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("ResolveRecord", "sqq", "a(qqay)", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("ResolveHostname", "isit", "ia(iay)st", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("ResolveAddress", "iiayt", "iast", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("ResolveRecord", "isqqt", "ia(qqay)t", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_VTABLE_END, }; diff --git a/src/resolve/resolved-def.h b/src/resolve/resolved-def.h new file mode 100644 index 0000000000..086d111205 --- /dev/null +++ b/src/resolve/resolved-def.h @@ -0,0 +1,30 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#pragma once + +/*** + This file is part of systemd. + + Copyright 2014 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#define SD_RESOLVED_DNS ((uint64_t) 1) +#define SD_RESOLVED_LLMNR_IPV4 ((uint64_t) 2) +#define SD_RESOLVED_LLMNR_IPV6 ((uint64_t) 4) +#define SD_RESOLVED_LLMNR (SD_RESOLVED_LLMNR_IPV4|SD_RESOLVED_LLMNR_IPV6) + +#define SD_RESOLVED_FLAGS_ALL (SD_RESOLVED_DNS|SD_RESOLVED_LLMNR_IPV4|SD_RESOLVED_LLMNR_IPV6) +#define SD_RESOLVED_FLAGS_DEFAULT SD_RESOLVED_FLAGS_ALL diff --git a/src/resolve/resolved-dns-packet.h b/src/resolve/resolved-dns-packet.h index 6a865a2d5b..561dd3adfa 100644 --- a/src/resolve/resolved-dns-packet.h +++ b/src/resolve/resolved-dns-packet.h @@ -34,6 +34,7 @@ typedef struct DnsPacket DnsPacket; #include "resolved-dns-rr.h" #include "resolved-dns-question.h" #include "resolved-dns-answer.h" +#include "resolved-def.h" typedef enum DnsProtocol { DNS_PROTOCOL_DNS, @@ -220,3 +221,16 @@ enum { const char* dnssec_algorithm_to_string(int i) _const_; int dnssec_algorithm_from_string(const char *s) _pure_; + +static inline uint64_t SD_RESOLVED_FLAGS_MAKE(DnsProtocol protocol, int family) { + + /* Converts a protocol + family into a flags field as used in queries */ + + if (protocol == DNS_PROTOCOL_DNS) + return SD_RESOLVED_DNS; + + if (protocol == DNS_PROTOCOL_LLMNR) + return family == AF_INET6 ? SD_RESOLVED_LLMNR_IPV6 : SD_RESOLVED_LLMNR_IPV4; + + return 0; +} diff --git a/src/resolve/resolved-dns-query.c b/src/resolve/resolved-dns-query.c index 6d77c109b4..669595d7f9 100644 --- a/src/resolve/resolved-dns-query.c +++ b/src/resolve/resolved-dns-query.c @@ -66,7 +66,7 @@ DnsQuery *dns_query_free(DnsQuery *q) { return NULL; } -int dns_query_new(Manager *m, DnsQuery **ret, DnsQuestion *question) { +int dns_query_new(Manager *m, DnsQuery **ret, DnsQuestion *question, int ifindex, uint64_t flags) { _cleanup_(dns_query_freep) DnsQuery *q = NULL; unsigned i; int r; @@ -86,6 +86,8 @@ int dns_query_new(Manager *m, DnsQuery **ret, DnsQuestion *question) { return -ENOMEM; q->question = dns_question_ref(question); + q->ifindex = ifindex; + q->flags = flags; for (i = 0; i < question->n_keys; i++) { _cleanup_free_ char *p; @@ -233,7 +235,7 @@ int dns_query_go(DnsQuery *q) { LIST_FOREACH(scopes, s, q->manager->dns_scopes) { DnsScopeMatch match; - match = dns_scope_good_domain(s, name); + match = dns_scope_good_domain(s, q->ifindex, q->flags, name); if (match < 0) return match; @@ -263,7 +265,7 @@ int dns_query_go(DnsQuery *q) { LIST_FOREACH(scopes, s, first->scopes_next) { DnsScopeMatch match; - match = dns_scope_good_domain(s, name); + match = dns_scope_good_domain(s, q->ifindex, q->flags, name); if (match < 0) goto fail; @@ -278,6 +280,8 @@ int dns_query_go(DnsQuery *q) { q->answer = dns_answer_unref(q->answer); q->answer_ifindex = 0; q->answer_rcode = 0; + q->answer_family = AF_UNSPEC; + q->answer_protocol = _DNS_PROTOCOL_INVALID; r = sd_event_add_time( q->manager->event, @@ -422,6 +426,8 @@ void dns_query_ready(DnsQuery *q) { q->answer = dns_answer_ref(answer); q->answer_rcode = rcode; q->answer_ifindex = (scope && scope->link) ? scope->link->ifindex : 0; + q->answer_protocol = scope ? scope->protocol : _DNS_PROTOCOL_INVALID; + q->answer_family = scope ? scope->family : AF_UNSPEC; } dns_query_complete(q, state); diff --git a/src/resolve/resolved-dns-query.h b/src/resolve/resolved-dns-query.h index 50fa3a2fe2..13b3ee4f81 100644 --- a/src/resolve/resolved-dns-query.h +++ b/src/resolve/resolved-dns-query.h @@ -41,6 +41,9 @@ struct DnsQuery { Manager *manager; DnsQuestion *question; + uint64_t flags; + int ifindex; + DnsTransactionState state; unsigned n_cname_redirects; @@ -49,6 +52,8 @@ struct DnsQuery { /* Discovered data */ DnsAnswer *answer; int answer_ifindex; + int answer_family; + DnsProtocol answer_protocol; int answer_rcode; /* Bus client information */ @@ -68,7 +73,7 @@ struct DnsQuery { LIST_FIELDS(DnsQuery, queries); }; -int dns_query_new(Manager *m, DnsQuery **q, DnsQuestion *question); +int dns_query_new(Manager *m, DnsQuery **q, DnsQuestion *question, int family, uint64_t flags); DnsQuery *dns_query_free(DnsQuery *q); int dns_query_go(DnsQuery *q); diff --git a/src/resolve/resolved-dns-scope.c b/src/resolve/resolved-dns-scope.c index 174249a9f7..396ae19e48 100644 --- a/src/resolve/resolved-dns-scope.c +++ b/src/resolve/resolved-dns-scope.c @@ -291,12 +291,18 @@ int dns_scope_tcp_socket(DnsScope *s, int family, const union in_addr_union *add return ret; } -DnsScopeMatch dns_scope_good_domain(DnsScope *s, const char *domain) { +DnsScopeMatch dns_scope_good_domain(DnsScope *s, int ifindex, uint64_t flags, const char *domain) { char **i; assert(s); assert(domain); + if (ifindex != 0 && (!s->link || s->link->ifindex != ifindex)) + return DNS_SCOPE_NO; + + if ((SD_RESOLVED_FLAGS_MAKE(s->protocol, s->family) & flags) == 0) + return DNS_SCOPE_NO; + STRV_FOREACH(i, s->domains) if (dns_name_endswith(domain, *i) > 0) return DNS_SCOPE_YES; diff --git a/src/resolve/resolved-dns-scope.h b/src/resolve/resolved-dns-scope.h index 6ba5ef2419..a02230999b 100644 --- a/src/resolve/resolved-dns-scope.h +++ b/src/resolve/resolved-dns-scope.h @@ -71,7 +71,7 @@ DnsScope* dns_scope_free(DnsScope *s); int dns_scope_emit(DnsScope *s, DnsPacket *p); int dns_scope_tcp_socket(DnsScope *s, int family, const union in_addr_union *address, uint16_t port); -DnsScopeMatch dns_scope_good_domain(DnsScope *s, const char *domain); +DnsScopeMatch dns_scope_good_domain(DnsScope *s, int ifindex, uint64_t flags, const char *domain); int dns_scope_good_key(DnsScope *s, DnsResourceKey *key); int dns_scope_good_dns_server(DnsScope *s, int family, const union in_addr_union *address); -- cgit v1.2.3-54-g00ecf