]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/resolve/resolved-dns-scope.c
tree-wide: define iterator inside of the macro
[thirdparty/systemd.git] / src / resolve / resolved-dns-scope.c
index d7e7b5a853ca3fb590fbca0b4a343048bffab806..e69ba3c758e1a41d6e4f574aa82c6de51fd6133a 100644 (file)
@@ -403,11 +403,11 @@ static int dns_scope_socket(
                 /* RFC 4795, section 2.5 requires the TTL to be set to 1 */
 
                 if (sa.sa.sa_family == AF_INET) {
-                        r = setsockopt_int(fd, IPPROTO_IP, IP_TTL, true);
+                        r = setsockopt_int(fd, IPPROTO_IP, IP_TTL, 1);
                         if (r < 0)
                                 return r;
                 } else if (sa.sa.sa_family == AF_INET6) {
-                        r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, true);
+                        r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 1);
                         if (r < 0)
                                 return r;
                 }
@@ -447,8 +447,8 @@ static int dns_scope_socket(
         return TAKE_FD(fd);
 }
 
-int dns_scope_socket_udp(DnsScope *s, DnsServer *server, uint16_t port) {
-        return dns_scope_socket(s, SOCK_DGRAM, AF_UNSPEC, NULL, server, port, NULL);
+int dns_scope_socket_udp(DnsScope *s, DnsServer *server) {
+        return dns_scope_socket(s, SOCK_DGRAM, AF_UNSPEC, NULL, server, dns_server_port(server), NULL);
 }
 
 int dns_scope_socket_tcp(DnsScope *s, int family, const union in_addr_union *address, DnsServer *server, uint16_t port, union sockaddr_union *ret_socket_address) {
@@ -496,9 +496,8 @@ DnsScopeMatch dns_scope_good_domain(
         assert(s);
         assert(domain);
 
-        /* Checks if the specified domain is something to look up on
-         * this scope. Note that this accepts non-qualified hostnames,
-         * i.e. those without any search path prefixed yet. */
+        /* Checks if the specified domain is something to look up on this scope. Note that this accepts
+         * non-qualified hostnames, i.e. those without any search path suffixed. */
 
         if (ifindex != 0 && (!s->link || s->link->ifindex != ifindex))
                 return DNS_SCOPE_NO;
@@ -620,7 +619,7 @@ DnsScopeMatch dns_scope_good_domain(
                      manager_is_own_hostname(s->manager, domain) <= 0))  /* never resolve the local hostname via LLMNR */
                         return DNS_SCOPE_YES_BASE + 1; /* Return +1, as we consider ourselves authoritative
                                                         * for single-label names, i.e. one label. This is
-                                                        * particular relevant as it means a "." route on some
+                                                        * particularly relevant as it means a "." route on some
                                                         * other scope won't pull all traffic away from
                                                         * us. (If people actually want to pull traffic away
                                                         * from us they should turn off LLMNR on the
@@ -652,20 +651,21 @@ bool dns_scope_good_key(DnsScope *s, const DnsResourceKey *key) {
 
         if (s->protocol == DNS_PROTOCOL_DNS) {
 
-                /* On classic DNS, looking up non-address RRs is always
-                 * fine. (Specifically, we want to permit looking up
-                 * DNSKEY and DS records on the root and top-level
-                 * domains.) */
+                /* On classic DNS, looking up non-address RRs is always fine. (Specifically, we want to
+                 * permit looking up DNSKEY and DS records on the root and top-level domains.) */
                 if (!dns_resource_key_is_address(key))
                         return true;
 
-                /* However, we refuse to look up A and AAAA RRs on the
-                 * root and single-label domains, under the assumption
-                 * that those should be resolved via LLMNR or search
-                 * path only, and should not be leaked onto the
-                 * internet. */
-                return !(dns_name_is_single_label(dns_resource_key_name(key)) ||
-                         dns_name_is_root(dns_resource_key_name(key)));
+                /* Unless explicitly overridden, we refuse to look up A and AAAA RRs on the root and
+                 * single-label domains, under the assumption that those should be resolved via LLMNR or
+                 * search path only, and should not be leaked onto the internet. */
+                const char* name = dns_resource_key_name(key);
+
+                if (!s->manager->resolve_unicast_single_label &&
+                    dns_name_is_single_label(name))
+                        return false;
+
+                return !dns_name_is_root(name);
         }
 
         /* On mDNS and LLMNR, send A and AAAA queries only on the
@@ -1059,12 +1059,13 @@ int dns_scope_notify_conflict(DnsScope *scope, DnsResourceRecord *rr) {
         random_bytes(&jitter, sizeof(jitter));
         jitter %= LLMNR_JITTER_INTERVAL_USEC;
 
-        r = sd_event_add_time(scope->manager->event,
-                              &scope->conflict_event_source,
-                              clock_boottime_or_monotonic(),
-                              now(clock_boottime_or_monotonic()) + jitter,
-                              LLMNR_JITTER_INTERVAL_USEC,
-                              on_conflict_dispatch, scope);
+        r = sd_event_add_time_relative(
+                        scope->manager->event,
+                        &scope->conflict_event_source,
+                        clock_boottime_or_monotonic(),
+                        jitter,
+                        LLMNR_JITTER_INTERVAL_USEC,
+                        on_conflict_dispatch, scope);
         if (r < 0)
                 return log_debug_errno(r, "Failed to add conflict dispatch event: %m");
 
@@ -1170,7 +1171,7 @@ DnsSearchDomain *dns_scope_get_search_domains(DnsScope *s) {
         return s->manager->search_domains;
 }
 
-bool dns_scope_name_needs_search_domain(DnsScope *s, const char *name) {
+bool dns_scope_name_wants_search_domain(DnsScope *s, const char *name) {
         assert(s);
 
         if (s->protocol != DNS_PROTOCOL_DNS)
@@ -1217,7 +1218,6 @@ int dns_scope_announce(DnsScope *scope, bool goodbye) {
         DnsTransaction *t;
         DnsZoneItem *z, *i;
         unsigned size = 0;
-        Iterator iterator;
         char *service_type;
         int r;
 
@@ -1237,7 +1237,7 @@ int dns_scope_announce(DnsScope *scope, bool goodbye) {
                 return 0; /* we reach this point only if changing hostname didn't help */
 
         /* Calculate answer's size. */
-        HASHMAP_FOREACH(z, scope->zone.by_key, iterator) {
+        HASHMAP_FOREACH(z, scope->zone.by_key) {
                 if (z->state != DNS_ZONE_ITEM_ESTABLISHED)
                         continue;
 
@@ -1254,11 +1254,7 @@ int dns_scope_announce(DnsScope *scope, bool goodbye) {
                 if (!scope->announced &&
                     dns_resource_key_is_dnssd_ptr(z->rr->key)) {
                         if (!set_contains(types, dns_resource_key_name(z->rr->key))) {
-                                r = set_ensure_allocated(&types, &dns_name_hash_ops);
-                                if (r < 0)
-                                        return log_debug_errno(r, "Failed to allocate set: %m");
-
-                                r = set_put(types, dns_resource_key_name(z->rr->key));
+                                r = set_ensure_put(&types, &dns_name_hash_ops, dns_resource_key_name(z->rr->key));
                                 if (r < 0)
                                         return log_debug_errno(r, "Failed to add item to set: %m");
                         }
@@ -1273,7 +1269,7 @@ int dns_scope_announce(DnsScope *scope, bool goodbye) {
                 return log_oom();
 
         /* Second iteration, actually add RRs to the answer. */
-        HASHMAP_FOREACH(z, scope->zone.by_key, iterator)
+        HASHMAP_FOREACH(z, scope->zone.by_key)
                 LIST_FOREACH (by_key, i, z) {
                         DnsAnswerFlags flags;
 
@@ -1291,7 +1287,7 @@ int dns_scope_announce(DnsScope *scope, bool goodbye) {
                 }
 
         /* Since all the active services are in the zone make them discoverable now. */
-        SET_FOREACH(service_type, types, iterator) {
+        SET_FOREACH(service_type, types) {
                 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr;
 
                 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR,
@@ -1322,18 +1318,13 @@ int dns_scope_announce(DnsScope *scope, bool goodbye) {
         /* In section 8.3 of RFC6762: "The Multicast DNS responder MUST send at least two unsolicited
          * responses, one second apart." */
         if (!scope->announced) {
-                usec_t ts;
-
                 scope->announced = true;
 
-                assert_se(sd_event_now(scope->manager->event, clock_boottime_or_monotonic(), &ts) >= 0);
-                ts += MDNS_ANNOUNCE_DELAY;
-
-                r = sd_event_add_time(
+                r = sd_event_add_time_relative(
                                 scope->manager->event,
                                 &scope->announce_event_source,
                                 clock_boottime_or_monotonic(),
-                                ts,
+                                MDNS_ANNOUNCE_DELAY,
                                 MDNS_JITTER_RANGE_USEC,
                                 on_announcement_timeout, scope);
                 if (r < 0)
@@ -1346,7 +1337,6 @@ int dns_scope_announce(DnsScope *scope, bool goodbye) {
 }
 
 int dns_scope_add_dnssd_services(DnsScope *scope) {
-        Iterator i;
         DnssdService *service;
         DnssdTxtData *txt_data;
         int r;
@@ -1358,7 +1348,7 @@ int dns_scope_add_dnssd_services(DnsScope *scope) {
 
         scope->announced = false;
 
-        HASHMAP_FOREACH(service, scope->manager->dnssd_services, i) {
+        HASHMAP_FOREACH(service, scope->manager->dnssd_services) {
                 service->withdrawn = false;
 
                 r = dns_zone_put(&scope->zone, scope, service->ptr_rr, false);
@@ -1381,7 +1371,6 @@ int dns_scope_add_dnssd_services(DnsScope *scope) {
 
 int dns_scope_remove_dnssd_services(DnsScope *scope) {
         _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
-        Iterator i;
         DnssdService *service;
         DnssdTxtData *txt_data;
         int r;
@@ -1397,7 +1386,7 @@ int dns_scope_remove_dnssd_services(DnsScope *scope) {
         if (r < 0)
                 return r;
 
-        HASHMAP_FOREACH(service, scope->manager->dnssd_services, i) {
+        HASHMAP_FOREACH(service, scope->manager->dnssd_services) {
                 dns_zone_remove_rr(&scope->zone, service->ptr_rr);
                 dns_zone_remove_rr(&scope->zone, service->srv_rr);
                 LIST_FOREACH(items, txt_data, service->txt_data_items)