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_NULL:
- case DNS_QUERY_SENT:
+ case DNS_QUERY_PENDING:
case DNS_QUERY_SUCCESS:
+ default:
assert_not_reached("Impossible state");
}
}
+static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
+ int r;
+
+ assert(reply);
+ assert(rr);
+
+ r = sd_bus_message_open_container(reply, 'r', "yayi");
+ if (r < 0)
+ return r;
+
+ if (rr->key.type == DNS_TYPE_A) {
+ r = sd_bus_message_append(reply, "y", AF_INET);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
+ } else {
+ r = sd_bus_message_append(reply, "y", AF_INET6);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
+ }
+ 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;
+
+ return 0;
+}
+
static void bus_method_resolve_hostname_complete(DnsQuery *q) {
+ _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;
assert(q);
if (r < 0)
goto parse_fail;
+ answer_rindex = q->received->rindex;
+
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;
if (r < 0)
goto parse_fail;
- if (rr->key.class != DNS_CLASS_IN)
- continue;
-
- if (!(q->request_family != AF_INET6 && rr->key.type == DNS_TYPE_A) &&
- !(q->request_family != AF_INET && rr->key.type == DNS_TYPE_AAAA))
- continue;
+ r = dns_query_matches_rr(q, rr);
+ 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);
+ if (r < 0)
+ goto parse_fail;
+ if (r > 0)
+ cname = dns_resource_record_ref(rr);
- if (!dns_name_equal(rr->key.name, q->request_hostname))
continue;
+ }
- r = sd_bus_message_open_container(reply, 'r', "yayi");
+ r = append_address(reply, rr, q->received->ifindex);
if (r < 0)
goto finish;
- if (rr->key.type == DNS_TYPE_A) {
- r = sd_bus_message_append(reply, "y", AF_INET);
- if (r < 0)
- goto finish;
+ if (!canonical)
+ canonical = dns_resource_record_ref(rr);
- r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
- } else {
- r = sd_bus_message_append(reply, "y", AF_INET6);
- if (r < 0)
- goto finish;
+ added ++;
+ }
- r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
- }
- if (r < 0)
+ if (added <= 0) {
+ if (!cname) {
+ 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;
+ }
- r = sd_bus_message_append(reply, "i", q->received->ifindex);
- if (r < 0)
- goto finish;
+ /* This has a cname? Then update the query with the
+ * new cname. */
+ r = dns_query_follow_cname(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);
+ else
+ r = sd_bus_reply_method_errno(q->request, -r, NULL);
- r = sd_bus_message_close_container(reply);
- if (r < 0)
goto finish;
+ }
- added ++;
- }
+ /* 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;
- if (added <= 0) {
- r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have RR of this type", q->request_hostname);
- goto finish;
+ r = dns_packet_read_rr(q->received, &rr, NULL);
+ if (r < 0)
+ goto parse_fail;
+
+ r = dns_query_matches_rr(q, rr);
+ if (r < 0)
+ goto parse_fail;
+ if (r == 0)
+ continue;
+
+ r = append_address(reply, rr, q->received->ifindex);
+ if (r < 0)
+ goto finish;
+
+ if (!canonical)
+ canonical = dns_resource_record_ref(rr);
+
+ 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);
+ 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;
+ }
+ return;
+ }
}
r = sd_bus_message_close_container(reply);
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;
r = dns_query_start(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;
- _cleanup_free_ char *reverse = NULL;
int r;
assert(q);
assert(q->received);
- r = dns_name_reverse(q->request_family, &q->request_address, &reverse);
- if (r < 0)
- goto finish;
-
r = dns_packet_skip_question(q->received);
if (r < 0)
goto parse_fail;
if (r < 0)
goto parse_fail;
- if (rr->key.class != DNS_CLASS_IN)
- continue;
- if (rr->key.type != DNS_TYPE_PTR)
- continue;
- if (!dns_name_equal(rr->key.name, reverse))
+ r = dns_query_matches_rr(q, rr);
+ if (r < 0)
+ goto parse_fail;
+ if (r == 0)
continue;
r = sd_bus_message_append(reply, "s", rr->ptr.name);
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;
}
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,
};