chiark / gitweb /
resolved: add more const
[elogind.git] / src / resolve / resolved-bus.c
index ebb97d7fd34e487a2b327fe9fa7b576df5a9be08..f486fcdbcc3d99d3af136202be098f1233234d92 100644 (file)
@@ -43,7 +43,7 @@ static int reply_query_state(DnsQuery *q) {
         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");
@@ -59,18 +59,21 @@ static int reply_query_state(DnsQuery *q) {
 
         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;
                         }
 
@@ -127,11 +130,12 @@ static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifin
 }
 
 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);
 
@@ -140,14 +144,10 @@ static void bus_method_resolve_hostname_complete(DnsQuery *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;
@@ -156,48 +156,45 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
         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);
@@ -209,31 +206,31 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
 
                 /* 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;
@@ -246,6 +243,12 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
         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;
 
@@ -305,9 +308,13 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi
         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;
         }
 
@@ -316,8 +323,9 @@ static int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, voi
 
 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);
 
@@ -326,10 +334,8 @@ static void bus_method_resolve_address_complete(DnsQuery *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);
@@ -340,24 +346,14 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
         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;
 
@@ -369,7 +365,7 @@ static void bus_method_resolve_address_complete(DnsQuery *q) {
 
                 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;
         }
 
@@ -440,7 +436,7 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void
         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;
@@ -451,7 +447,7 @@ static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void
 
 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,
 };