]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/resolve/resolved-dns-stub.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / resolve / resolved-dns-stub.c
index 73590e3f9bddef82e6456034471e07fb9ed9e577..8a8a0b19eee53f561388214c9c68c0512edfbf68 100644 (file)
@@ -528,18 +528,26 @@ static int dns_stub_send(
         if (s)
                 r = dns_stream_write_packet(s, reply);
         else {
-                int fd;
+                int fd, ifindex;
 
-                fd = find_socket_fd(m, l, p->family, &p->sender, SOCK_DGRAM);
+                fd = find_socket_fd(m, l, p->family, &p->destination, SOCK_DGRAM);
                 if (fd < 0)
                         return fd;
 
+                if (address_is_proxy(p->family, &p->destination))
+                        /* Force loopback iface if this is the loopback proxy stub
+                         * and ifindex was normalized to 0 by manager_recv(). */
+                        ifindex = p->ifindex ?: LOOPBACK_IFINDEX;
+                else
+                        /* Force loopback iface if this is the main listener stub. */
+                        ifindex = l ? p->ifindex : LOOPBACK_IFINDEX;
+
                 /* Note that it is essential here that we explicitly choose the source IP address for this
                  * packet. This is because otherwise the kernel will choose it automatically based on the
-                 * routing table and will thus pick 127.0.0.1 rather than 127.0.0.53. */
+                 * routing table and will thus pick 127.0.0.1 rather than 127.0.0.53/54. */
                 r = manager_send(m,
                                  fd,
-                                 l || address_is_proxy(p->family, &p->destination) ? p->ifindex : LOOPBACK_IFINDEX, /* force loopback iface if this is the main listener stub */
+                                 ifindex,
                                  p->family, &p->sender, p->sender_port, &p->destination,
                                  reply);
         }
@@ -720,7 +728,8 @@ static int dns_stub_patch_bypass_reply_packet(
         return 0;
 }
 
-static void dns_stub_query_complete(DnsQuery *q) {
+static void dns_stub_query_complete(DnsQuery *query) {
+        _cleanup_(dns_query_freep) DnsQuery *q = query;
         int r;
 
         assert(q);
@@ -741,7 +750,6 @@ static void dns_stub_query_complete(DnsQuery *q) {
                         else
                                 (void) dns_stub_send(q->manager, q->stub_listener_extra, q->request_stream, q->request_packet, reply);
 
-                        dns_query_free(q);
                         return;
                 }
         }
@@ -753,11 +761,8 @@ static void dns_stub_query_complete(DnsQuery *q) {
                         q,
                         dns_query_question_for_protocol(q, DNS_PROTOCOL_DNS),
                         dns_stub_reply_with_edns0_do(q));
-        if (r < 0) {
-                log_debug_errno(r, "Failed to assign sections: %m");
-                dns_query_free(q);
-                return;
-        }
+        if (r < 0)
+                return (void) log_debug_errno(r, "Failed to assign sections: %m");
 
         switch (q->state) {
 
@@ -792,8 +797,9 @@ static void dns_stub_query_complete(DnsQuery *q) {
                                  * now with the redirected question. We'll */
                                 r = dns_query_go(q);
                                 if (r < 0)
-                                        log_debug_errno(r, "Failed to restart query: %m");
+                                        return (void) log_debug_errno(r, "Failed to restart query: %m");
 
+                                TAKE_PTR(q);
                                 return;
                         }
 
@@ -801,11 +807,8 @@ static void dns_stub_query_complete(DnsQuery *q) {
                                         q,
                                         dns_query_question_for_protocol(q, DNS_PROTOCOL_DNS),
                                         dns_stub_reply_with_edns0_do(q));
-                        if (r < 0) {
-                                log_debug_errno(r, "Failed to assign sections: %m");
-                                dns_query_free(q);
-                                return;
-                        }
+                        if (r < 0)
+                                return (void) log_debug_errno(r, "Failed to assign sections: %m");
 
                         if (cname_result == DNS_QUERY_MATCH) /* A match? Then we are done, let's return what we got */
                                 break;
@@ -851,8 +854,6 @@ static void dns_stub_query_complete(DnsQuery *q) {
         default:
                 assert_not_reached();
         }
-
-        dns_query_free(q);
 }
 
 static int dns_stub_stream_complete(DnsStream *s, int error) {
@@ -1037,19 +1038,14 @@ static int on_dns_stub_packet(sd_event_source *s, int fd, uint32_t revents, void
 }
 
 static int on_dns_stub_packet_extra(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
-        DnsStubListenerExtra *l = userdata;
-
-        assert(l);
+        DnsStubListenerExtra *l = ASSERT_PTR(userdata);
 
         return on_dns_stub_packet_internal(s, fd, revents, l->manager, l);
 }
 
-static int on_dns_stub_stream_packet(DnsStream *s) {
-        _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
-
+static int on_dns_stub_stream_packet(DnsStream *s, DnsPacket *p) {
         assert(s);
-
-        p = dns_stream_take_read_packet(s);
+        assert(s->manager);
         assert(p);
 
         if (dns_packet_validate_query(p) > 0) {
@@ -1074,15 +1070,14 @@ static int on_dns_stub_stream_internal(sd_event_source *s, int fd, uint32_t reve
                 return -errno;
         }
 
-        r = dns_stream_new(m, &stream, DNS_STREAM_STUB, DNS_PROTOCOL_DNS, cfd, NULL, DNS_STREAM_STUB_TIMEOUT_USEC);
+        r = dns_stream_new(m, &stream, DNS_STREAM_STUB, DNS_PROTOCOL_DNS, cfd, NULL,
+                           on_dns_stub_stream_packet, dns_stub_stream_complete, DNS_STREAM_STUB_TIMEOUT_USEC);
         if (r < 0) {
                 safe_close(cfd);
                 return r;
         }
 
         stream->stub_listener_extra = l;
-        stream->on_packet = on_dns_stub_stream_packet;
-        stream->complete = dns_stub_stream_complete;
 
         /* We let the reference to the stream dangle here, it will be dropped later by the complete callback. */
 
@@ -1094,9 +1089,8 @@ static int on_dns_stub_stream(sd_event_source *s, int fd, uint32_t revents, void
 }
 
 static int on_dns_stub_stream_extra(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
-        DnsStubListenerExtra *l = userdata;
+        DnsStubListenerExtra *l = ASSERT_PTR(userdata);
 
-        assert(l);
         return on_dns_stub_stream_internal(s, fd, revents, l->manager, l);
 }