switch (q->state) {
case DNS_QUERY_NO_SERVERS:
- return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "Not appropriate name servers or networks found");
+ return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
case DNS_QUERY_TIMEOUT:
return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
case DNS_QUERY_FAILURE: {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ int rcode;
- assert(q->received);
+ rcode = dns_query_get_rcode(q);
+ if (rcode < 0)
+ return rcode;
- if (DNS_PACKET_RCODE(q->received) == DNS_RCODE_NXDOMAIN)
+ if (rcode == DNS_RCODE_NXDOMAIN)
sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", name);
else {
const char *rc, *n;
char p[3]; /* the rcode is 4 bits long */
- rc = dns_rcode_to_string(DNS_PACKET_RCODE(q->received));
+ rc = dns_rcode_to_string(rcode);
if (!rc) {
- sprintf(p, "%i", DNS_PACKET_RCODE(q->received));
+ sprintf(p, "%i", rcode);
rc = p;
}
}
static void bus_method_resolve_hostname_complete(DnsQuery *q) {
- _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL;
+ _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL, *canonical = NULL;
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- unsigned i, n, added = 0;
- size_t answer_rindex;
- int r;
+ DnsResourceRecord **rrs;
+ unsigned added = 0;
+ int ifindex;
+ int r, n, i;
assert(q);
goto finish;
}
- assert(q->received);
-
- r = dns_packet_skip_question(q->received);
- if (r < 0)
+ n = dns_query_get_rrs(q, &rrs);
+ if (n < 0)
goto parse_fail;
- answer_rindex = q->received->rindex;
-
r = sd_bus_message_new_method_return(q->request, &reply);
if (r < 0)
goto finish;
if (r < 0)
goto finish;
- n = DNS_PACKET_ANCOUNT(q->received) +
- DNS_PACKET_NSCOUNT(q->received) +
- DNS_PACKET_ARCOUNT(q->received);
+ ifindex = dns_query_get_ifindex(q);
+ if (ifindex < 0)
+ ifindex = 0;
for (i = 0; i < n; i++) {
- _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
-
- r = dns_packet_read_rr(q->received, &rr, NULL);
- if (r < 0)
- goto parse_fail;
-
- r = dns_query_matches_rr(q, rr);
+ r = dns_query_matches_rr(q, rrs[i]);
if (r < 0)
goto parse_fail;
if (r == 0) {
/* Hmm, if this is not an address record,
maybe it's a cname? If so, remember this */
- r = dns_query_matches_cname(q, rr);
+ r = dns_query_matches_cname(q, rrs[i]);
if (r < 0)
goto parse_fail;
if (r > 0)
- cname = dns_resource_record_ref(rr);
+ cname = dns_resource_record_ref(rrs[i]);
continue;
}
- r = append_address(reply, rr, q->received->ifindex);
+ r = append_address(reply, rrs[i], ifindex);
if (r < 0)
goto finish;
+ if (!canonical)
+ canonical = dns_resource_record_ref(rrs[i]);
+
added ++;
}
if (added <= 0) {
if (!cname) {
- r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have RR of this type", q->request_hostname);
+ r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of requested type", q->request_hostname);
goto finish;
}
/* This has a cname? Then update the query with the
* new cname. */
- r = dns_query_follow_cname(q, cname->cname.name);
+ r = dns_query_cname_redirect(q, cname->cname.name);
if (r < 0) {
if (r == -ELOOP)
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop on '%s'", q->request_hostname);
/* Before we restart the query, let's see if any of
* the RRs we already got already answers our query */
- dns_packet_rewind(q->received, answer_rindex);
for (i = 0; i < n; i++) {
- _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
-
- r = dns_packet_read_rr(q->received, &rr, NULL);
- if (r < 0)
- goto parse_fail;
-
- r = dns_query_matches_rr(q, rr);
+ r = dns_query_matches_rr(q, rrs[i]);
if (r < 0)
goto parse_fail;
if (r == 0)
continue;
- r = append_address(reply, rr, q->received->ifindex);
+ r = append_address(reply, rrs[i], ifindex);
if (r < 0)
goto finish;
+ if (!canonical)
+ canonical = dns_resource_record_ref(rrs[i]);
+
added++;
}
/* If we didn't find anything, then let's restart the
* query, this time with the cname */
if (added <= 0) {
- r = dns_query_start(q);
+ r = dns_query_go(q);
+ if (r == -ESRCH) {
+ r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
+ goto finish;
+ }
if (r < 0) {
r = sd_bus_reply_method_errno(q->request, -r, NULL);
goto finish;
if (r < 0)
goto finish;
+ /* Return the precise spelling and uppercasing reported by the server */
+ assert(canonical);
+ r = sd_bus_message_append(reply, "s", canonical->key.name);
+ if (r < 0)
+ goto finish;
+
r = sd_bus_send(q->manager->bus, reply, NULL);
goto finish;
q->request_hostname = hostname;
q->complete = bus_method_resolve_hostname_complete;
- r = dns_query_start(q);
+ r = dns_query_go(q);
if (r < 0) {
dns_query_free(q);
+
+ if (r == -ESRCH)
+ sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
+
return r;
}
static void bus_method_resolve_address_complete(DnsQuery *q) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- unsigned i, n, added = 0;
- int r;
+ DnsResourceRecord **rrs;
+ unsigned added = 0;
+ int r, n, i;
assert(q);
goto finish;
}
- assert(q->received);
-
- r = dns_packet_skip_question(q->received);
- if (r < 0)
+ n = dns_query_get_rrs(q, &rrs);
+ if (n < 0)
goto parse_fail;
r = sd_bus_message_new_method_return(q->request, &reply);
if (r < 0)
goto finish;
- n = DNS_PACKET_ANCOUNT(q->received) +
- DNS_PACKET_NSCOUNT(q->received) +
- DNS_PACKET_ARCOUNT(q->received);
-
for (i = 0; i < n; i++) {
- _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
-
- r = dns_packet_read_rr(q->received, &rr, NULL);
- if (r < 0)
- goto parse_fail;
-
- r = dns_query_matches_rr(q, rr);
+ r = dns_query_matches_rr(q, rrs[i]);
if (r < 0)
goto parse_fail;
if (r == 0)
continue;
- r = sd_bus_message_append(reply, "s", rr->ptr.name);
+ r = sd_bus_message_append(reply, "s", rrs[i]->ptr.name);
if (r < 0)
goto finish;
in_addr_to_string(q->request_family, &q->request_address, &ip);
- r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address '%s' does not have RR of this type", ip);
+ r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address '%s' does not have any RR of requested type", ip);
goto finish;
}
memcpy(&q->request_address, d, sz);
q->complete = bus_method_resolve_address_complete;
- r = dns_query_start(q);
+ r = dns_query_go(q);
if (r < 0) {
dns_query_free(q);
return r;
static const sd_bus_vtable resolve_vtable[] = {
SD_BUS_VTABLE_START(0),
- SD_BUS_METHOD("ResolveHostname", "sy", "a(yayi)", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("ResolveHostname", "sy", "a(yayi)s", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ResolveAddress", "yayi", "as", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END,
};