resolved: add ResolveService() bus call for resolving SRV and DNS-SD services
This also adds client-side support for this to systemd-resolve-host. Note that the ResolveService() API can deal both with DNS-SD service (consisting of service name, type and domain), as well as classic SRV services (consisting just of a type and a domain), all exposed in the same call. This patch also reworks CNAME handling in order to reuse it between hostname, RR and service lookups. In contrast to Avahi and Bonjour, this new API will actually reolve the A/AAAA RRs the SRV RRs point to in one go (unless this is explicitly disabled). This normally comes for free, as these RRs are sent along the SRV responses anyway, hence let's make use of that. This makes the API considerably easier to use, as a single ResolveService() invocation will return all necessary data to pick a server and connect() to it. Note that this only implements the DNS-SD resolving step, it does not implement DNS-SD browsing, as that makes sense primarily on mDNS, due to its continuous nature.
This commit is contained in:
+654
-190
File diff suppressed because it is too large
Load Diff
@@ -24,7 +24,9 @@
|
||||
#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_NO_CNAME ((uint64_t) 8)
|
||||
#define SD_RESOLVED_NO_TXT ((uint64_t) 16)
|
||||
#define SD_RESOLVED_NO_ADDRESS ((uint64_t) 32)
|
||||
|
||||
#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
|
||||
#define SD_RESOLVED_LLMNR (SD_RESOLVED_LLMNR_IPV4|SD_RESOLVED_LLMNR_IPV6)
|
||||
#define SD_RESOLVED_PROTOCOLS_ALL (SD_RESOLVED_LLMNR|SD_RESOLVED_DNS)
|
||||
|
||||
@@ -58,3 +58,20 @@ void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local);
|
||||
int dns_answer_reserve(DnsAnswer **a, unsigned n_free);
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(DnsAnswer*, dns_answer_unref);
|
||||
|
||||
#define DNS_ANSWER_FOREACH(kk, a) \
|
||||
for (unsigned _i = ({ \
|
||||
(kk) = ((a) && (a)->n_rrs > 0 ? (a)->items[0].rr : NULL); \
|
||||
0; \
|
||||
}); \
|
||||
(a) && ((_i) < (a)->n_rrs); \
|
||||
_i++, (kk) = (_i < (a)->n_rrs ? (a)->items[_i].rr : NULL))
|
||||
|
||||
#define DNS_ANSWER_FOREACH_IFINDEX(kk, ifindex, a) \
|
||||
for (unsigned _i = ({ \
|
||||
(kk) = ((a) && (a)->n_rrs > 0 ? (a)->items[0].rr : NULL); \
|
||||
(ifindex) = ((a) && (a)->n_rrs > 0 ? (a)->items[0].ifindex : 0); \
|
||||
0; \
|
||||
}); \
|
||||
(a) && ((_i) < (a)->n_rrs); \
|
||||
_i++, (kk) = (_i < (a)->n_rrs ? (a)->items[_i].rr : NULL), (ifindex) = (_i < (a)->n_rrs ? (a)->items[_i].ifindex : 0))
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
#define CNAME_MAX 8
|
||||
#define QUERIES_MAX 2048
|
||||
#define AUXILIARY_QUERIES_MAX 64
|
||||
|
||||
static void dns_query_stop(DnsQuery *q) {
|
||||
DnsTransaction *t;
|
||||
@@ -48,6 +49,15 @@ DnsQuery *dns_query_free(DnsQuery *q) {
|
||||
if (!q)
|
||||
return NULL;
|
||||
|
||||
while (q->auxiliary_queries)
|
||||
dns_query_free(q->auxiliary_queries);
|
||||
|
||||
if (q->auxiliary_for) {
|
||||
assert(q->auxiliary_for->n_auxiliary_queries > 0);
|
||||
q->auxiliary_for->n_auxiliary_queries--;
|
||||
LIST_REMOVE(auxiliary_queries, q->auxiliary_for->auxiliary_queries, q);
|
||||
}
|
||||
|
||||
dns_query_stop(q);
|
||||
set_free(q->transactions);
|
||||
|
||||
@@ -111,6 +121,29 @@ int dns_query_new(Manager *m, DnsQuery **ret, DnsQuestion *question, int ifindex
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dns_query_make_auxiliary(DnsQuery *q, DnsQuery *auxiliary_for) {
|
||||
assert(q);
|
||||
assert(auxiliary_for);
|
||||
|
||||
/* Ensure that that the query is not auxiliary yet, and
|
||||
* nothing else is auxiliary to it either */
|
||||
assert(!q->auxiliary_for);
|
||||
assert(!q->auxiliary_queries);
|
||||
|
||||
/* Ensure that the unit we shall be made auxiliary for isn't
|
||||
* auxiliary itself */
|
||||
assert(!auxiliary_for->auxiliary_for);
|
||||
|
||||
if (auxiliary_for->n_auxiliary_queries >= AUXILIARY_QUERIES_MAX)
|
||||
return -EAGAIN;
|
||||
|
||||
LIST_PREPEND(auxiliary_queries, auxiliary_for->auxiliary_queries, q);
|
||||
q->auxiliary_for = auxiliary_for;
|
||||
|
||||
auxiliary_for->n_auxiliary_queries++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void dns_query_complete(DnsQuery *q, DnsTransactionState state) {
|
||||
assert(q);
|
||||
assert(!IN_SET(state, DNS_TRANSACTION_NULL, DNS_TRANSACTION_PENDING));
|
||||
@@ -622,7 +655,7 @@ int dns_query_go(DnsQuery *q) {
|
||||
assert(q->question);
|
||||
assert(q->question->n_keys > 0);
|
||||
|
||||
name = DNS_RESOURCE_KEY_NAME(q->question->keys[0]);
|
||||
name = dns_question_name(q->question);
|
||||
|
||||
LIST_FOREACH(scopes, s, q->manager->dns_scopes) {
|
||||
DnsScopeMatch match;
|
||||
@@ -831,12 +864,13 @@ void dns_query_ready(DnsQuery *q) {
|
||||
dns_query_complete(q, state);
|
||||
}
|
||||
|
||||
int dns_query_cname_redirect(DnsQuery *q, const DnsResourceRecord *cname) {
|
||||
static int dns_query_cname_redirect(DnsQuery *q, const DnsResourceRecord *cname) {
|
||||
_cleanup_(dns_question_unrefp) DnsQuestion *nq = NULL;
|
||||
int r;
|
||||
|
||||
assert(q);
|
||||
|
||||
q->n_cname_redirects ++;
|
||||
if (q->n_cname_redirects > CNAME_MAX)
|
||||
return -ELOOP;
|
||||
|
||||
@@ -848,14 +882,66 @@ int dns_query_cname_redirect(DnsQuery *q, const DnsResourceRecord *cname) {
|
||||
q->question = nq;
|
||||
nq = NULL;
|
||||
|
||||
q->n_cname_redirects++;
|
||||
|
||||
dns_query_stop(q);
|
||||
q->state = DNS_TRANSACTION_NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dns_query_process_cname(DnsQuery *q) {
|
||||
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL;
|
||||
DnsResourceRecord *rr;
|
||||
int r;
|
||||
|
||||
assert(q);
|
||||
|
||||
if (q->state != DNS_TRANSACTION_SUCCESS)
|
||||
return 0;
|
||||
|
||||
DNS_ANSWER_FOREACH(rr, q->answer) {
|
||||
|
||||
r = dns_question_matches_rr(q->question, rr);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0)
|
||||
return 0; /* The answer matches directly, no need to follow cnames */
|
||||
|
||||
r = dns_question_matches_cname(q->question, rr);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0 && !cname)
|
||||
cname = dns_resource_record_ref(rr);
|
||||
}
|
||||
|
||||
if (!cname)
|
||||
return 0; /* No cname to follow */
|
||||
|
||||
if (q->flags & SD_RESOLVED_NO_CNAME)
|
||||
return -ELOOP;
|
||||
|
||||
/* OK, let's actually follow the CNAME */
|
||||
r = dns_query_cname_redirect(q, cname);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
/* Let's see if the answer can already answer the new
|
||||
* redirected question */
|
||||
DNS_ANSWER_FOREACH(rr, q->answer) {
|
||||
r = dns_question_matches_rr(q->question, rr);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0)
|
||||
return 0; /* It can answer it, yay! */
|
||||
}
|
||||
|
||||
/* OK, it cannot, let's begin with the new query */
|
||||
r = dns_query_go(q);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return 1; /* We return > 0, if we restarted the query for a new cname */
|
||||
}
|
||||
|
||||
static int on_bus_track(sd_bus_track *t, void *userdata) {
|
||||
DnsQuery *q = userdata;
|
||||
|
||||
|
||||
@@ -34,6 +34,16 @@ typedef struct DnsQuery DnsQuery;
|
||||
|
||||
struct DnsQuery {
|
||||
Manager *manager;
|
||||
|
||||
/* When resolving a service, we first create a TXT+SRV query,
|
||||
* and then for the hostnames we discover auxiliary A+AAAA
|
||||
* queries. This pointer always points from the auxiliary
|
||||
* queries back to the TXT+SRV query. */
|
||||
DnsQuery *auxiliary_for;
|
||||
LIST_HEAD(DnsQuery, auxiliary_queries);
|
||||
unsigned n_auxiliary_queries;
|
||||
int auxiliary_result;
|
||||
|
||||
DnsQuestion *question;
|
||||
|
||||
uint64_t flags;
|
||||
@@ -53,8 +63,9 @@ struct DnsQuery {
|
||||
/* Bus client information */
|
||||
sd_bus_message *request;
|
||||
int request_family;
|
||||
const char *request_hostname;
|
||||
bool request_address_valid;
|
||||
union in_addr_union request_address;
|
||||
unsigned block_all_complete;
|
||||
|
||||
/* Completion callback */
|
||||
void (*complete)(DnsQuery* q);
|
||||
@@ -65,15 +76,18 @@ struct DnsQuery {
|
||||
sd_bus_track *bus_track;
|
||||
|
||||
LIST_FIELDS(DnsQuery, queries);
|
||||
LIST_FIELDS(DnsQuery, auxiliary_queries);
|
||||
};
|
||||
|
||||
int dns_query_new(Manager *m, DnsQuery **q, DnsQuestion *question, int family, uint64_t flags);
|
||||
DnsQuery *dns_query_free(DnsQuery *q);
|
||||
|
||||
int dns_query_make_auxiliary(DnsQuery *q, DnsQuery *auxiliary_for);
|
||||
|
||||
int dns_query_go(DnsQuery *q);
|
||||
void dns_query_ready(DnsQuery *q);
|
||||
|
||||
int dns_query_cname_redirect(DnsQuery *q, const DnsResourceRecord *cname);
|
||||
int dns_query_process_cname(DnsQuery *q);
|
||||
|
||||
int dns_query_bus_track(DnsQuery *q, sd_bus_message *m);
|
||||
|
||||
|
||||
@@ -301,3 +301,129 @@ int dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname,
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *dns_question_name(DnsQuestion *q) {
|
||||
assert(q);
|
||||
|
||||
if (q->n_keys < 1)
|
||||
return NULL;
|
||||
|
||||
return DNS_RESOURCE_KEY_NAME(q->keys[0]);
|
||||
}
|
||||
|
||||
int dns_question_new_address(DnsQuestion **ret, int family, const char *name) {
|
||||
_cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
assert(name);
|
||||
|
||||
if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
|
||||
return -EAFNOSUPPORT;
|
||||
|
||||
q = dns_question_new(family == AF_UNSPEC ? 2 : 1);
|
||||
if (!q)
|
||||
return -ENOMEM;
|
||||
|
||||
if (family != AF_INET6) {
|
||||
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
|
||||
|
||||
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, name);
|
||||
if (!key)
|
||||
return -ENOMEM;
|
||||
|
||||
r = dns_question_add(q, key);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
if (family != AF_INET) {
|
||||
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
|
||||
|
||||
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
|
||||
if (!key)
|
||||
return -ENOMEM;
|
||||
|
||||
r = dns_question_add(q, key);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
*ret = q;
|
||||
q = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dns_question_new_reverse(DnsQuestion **ret, int family, const union in_addr_union *a) {
|
||||
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
|
||||
_cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
|
||||
_cleanup_free_ char *reverse = NULL;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
assert(a);
|
||||
|
||||
if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
|
||||
return -EAFNOSUPPORT;
|
||||
|
||||
r = dns_name_reverse(family, a, &reverse);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
q = dns_question_new(1);
|
||||
if (!q)
|
||||
return -ENOMEM;
|
||||
|
||||
key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, reverse);
|
||||
if (!key)
|
||||
return -ENOMEM;
|
||||
|
||||
reverse = NULL;
|
||||
|
||||
r = dns_question_add(q, key);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
*ret = q;
|
||||
q = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dns_question_new_service(DnsQuestion **ret, const char *name, bool with_txt) {
|
||||
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
|
||||
_cleanup_(dns_question_unrefp) DnsQuestion *q = NULL;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
assert(name);
|
||||
|
||||
q = dns_question_new(1 + with_txt);
|
||||
if (!q)
|
||||
return -ENOMEM;
|
||||
|
||||
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, name);
|
||||
if (!key)
|
||||
return -ENOMEM;
|
||||
|
||||
r = dns_question_add(q, key);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (with_txt) {
|
||||
dns_resource_key_unref(key);
|
||||
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_TXT, name);
|
||||
if (!key)
|
||||
return -ENOMEM;
|
||||
|
||||
r = dns_question_add(q, key);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
*ret = q;
|
||||
q = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ typedef struct DnsQuestion DnsQuestion;
|
||||
|
||||
#include "resolved-dns-rr.h"
|
||||
|
||||
/* A simple array of resources keys */
|
||||
/* A simple array of resources keys, all sharing the same domain */
|
||||
|
||||
struct DnsQuestion {
|
||||
unsigned n_ref;
|
||||
@@ -37,6 +37,10 @@ DnsQuestion *dns_question_new(unsigned n);
|
||||
DnsQuestion *dns_question_ref(DnsQuestion *q);
|
||||
DnsQuestion *dns_question_unref(DnsQuestion *q);
|
||||
|
||||
int dns_question_new_address(DnsQuestion **ret, int family, const char *name);
|
||||
int dns_question_new_reverse(DnsQuestion **ret, int family, const union in_addr_union *a);
|
||||
int dns_question_new_service(DnsQuestion **ret, const char *name, bool with_txt);
|
||||
|
||||
int dns_question_add(DnsQuestion *q, DnsResourceKey *key);
|
||||
|
||||
int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr);
|
||||
@@ -48,4 +52,6 @@ int dns_question_is_equal(DnsQuestion *a, DnsQuestion *b);
|
||||
|
||||
int dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname, DnsQuestion **ret);
|
||||
|
||||
const char *dns_question_name(DnsQuestion *q);
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(DnsQuestion*, dns_question_unref);
|
||||
|
||||
Reference in New Issue
Block a user