ac3a84
From d34f971ad09e43f583ff570e26c87e6cdc83d69d Mon Sep 17 00:00:00 2001
ac3a84
From: Yu Watanabe <watanabe.yu+github@gmail.com>
ac3a84
Date: Fri, 4 Nov 2022 12:53:07 +0900
ac3a84
Subject: [PATCH] resolve: introduce link_get_llmnr_support() and
ac3a84
 link_get_mdns_support()
ac3a84
ac3a84
(cherry picked from commit bce459e3275249574f2142236275b2c33a9f88e3)
ac3a84
ac3a84
Related: #2138081
ac3a84
---
ac3a84
 src/resolve/resolved-link.c | 49 ++++++++++++++++++++++---------------
ac3a84
 src/resolve/resolved-link.h |  3 +++
ac3a84
 src/shared/resolve-util.h   |  3 ++-
ac3a84
 3 files changed, 34 insertions(+), 21 deletions(-)
ac3a84
ac3a84
diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c
ac3a84
index 409d725686..86112f3a3b 100644
ac3a84
--- a/src/resolve/resolved-link.c
ac3a84
+++ b/src/resolve/resolved-link.c
ac3a84
@@ -140,8 +140,7 @@ void link_allocate_scopes(Link *l) {
ac3a84
                 l->unicast_scope = dns_scope_free(l->unicast_scope);
ac3a84
 
ac3a84
         if (link_relevant(l, AF_INET, true) &&
ac3a84
-            l->llmnr_support != RESOLVE_SUPPORT_NO &&
ac3a84
-            l->manager->llmnr_support != RESOLVE_SUPPORT_NO) {
ac3a84
+            link_get_llmnr_support(l) != RESOLVE_SUPPORT_NO) {
ac3a84
                 if (!l->llmnr_ipv4_scope) {
ac3a84
                         r = dns_scope_new(l->manager, &l->llmnr_ipv4_scope, l, DNS_PROTOCOL_LLMNR, AF_INET);
ac3a84
                         if (r < 0)
ac3a84
@@ -151,8 +150,7 @@ void link_allocate_scopes(Link *l) {
ac3a84
                 l->llmnr_ipv4_scope = dns_scope_free(l->llmnr_ipv4_scope);
ac3a84
 
ac3a84
         if (link_relevant(l, AF_INET6, true) &&
ac3a84
-            l->llmnr_support != RESOLVE_SUPPORT_NO &&
ac3a84
-            l->manager->llmnr_support != RESOLVE_SUPPORT_NO) {
ac3a84
+            link_get_llmnr_support(l) != RESOLVE_SUPPORT_NO) {
ac3a84
                 if (!l->llmnr_ipv6_scope) {
ac3a84
                         r = dns_scope_new(l->manager, &l->llmnr_ipv6_scope, l, DNS_PROTOCOL_LLMNR, AF_INET6);
ac3a84
                         if (r < 0)
ac3a84
@@ -162,8 +160,7 @@ void link_allocate_scopes(Link *l) {
ac3a84
                 l->llmnr_ipv6_scope = dns_scope_free(l->llmnr_ipv6_scope);
ac3a84
 
ac3a84
         if (link_relevant(l, AF_INET, true) &&
ac3a84
-            l->mdns_support != RESOLVE_SUPPORT_NO &&
ac3a84
-            l->manager->mdns_support != RESOLVE_SUPPORT_NO) {
ac3a84
+            link_get_mdns_support(l) != RESOLVE_SUPPORT_NO) {
ac3a84
                 if (!l->mdns_ipv4_scope) {
ac3a84
                         r = dns_scope_new(l->manager, &l->mdns_ipv4_scope, l, DNS_PROTOCOL_MDNS, AF_INET);
ac3a84
                         if (r < 0)
ac3a84
@@ -173,8 +170,7 @@ void link_allocate_scopes(Link *l) {
ac3a84
                 l->mdns_ipv4_scope = dns_scope_free(l->mdns_ipv4_scope);
ac3a84
 
ac3a84
         if (link_relevant(l, AF_INET6, true) &&
ac3a84
-            l->mdns_support != RESOLVE_SUPPORT_NO &&
ac3a84
-            l->manager->mdns_support != RESOLVE_SUPPORT_NO) {
ac3a84
+            link_get_mdns_support(l) != RESOLVE_SUPPORT_NO) {
ac3a84
                 if (!l->mdns_ipv6_scope) {
ac3a84
                         r = dns_scope_new(l->manager, &l->mdns_ipv6_scope, l, DNS_PROTOCOL_MDNS, AF_INET6);
ac3a84
                         if (r < 0)
ac3a84
@@ -191,8 +187,7 @@ void link_add_rrs(Link *l, bool force_remove) {
ac3a84
                 link_address_add_rrs(a, force_remove);
ac3a84
 
ac3a84
         if (!force_remove &&
ac3a84
-            l->mdns_support == RESOLVE_SUPPORT_YES &&
ac3a84
-            l->manager->mdns_support == RESOLVE_SUPPORT_YES) {
ac3a84
+            link_get_mdns_support(l) == RESOLVE_SUPPORT_YES) {
ac3a84
 
ac3a84
                 if (l->mdns_ipv4_scope) {
ac3a84
                         r = dns_scope_add_dnssd_services(l->mdns_ipv4_scope);
ac3a84
@@ -651,13 +646,13 @@ int link_update(Link *l) {
ac3a84
         if (r < 0)
ac3a84
                 return r;
ac3a84
 
ac3a84
-        if (l->llmnr_support != RESOLVE_SUPPORT_NO) {
ac3a84
+        if (link_get_llmnr_support(l) != RESOLVE_SUPPORT_NO) {
ac3a84
                 r = manager_llmnr_start(l->manager);
ac3a84
                 if (r < 0)
ac3a84
                         return r;
ac3a84
         }
ac3a84
 
ac3a84
-        if (l->mdns_support != RESOLVE_SUPPORT_NO) {
ac3a84
+        if (link_get_mdns_support(l) != RESOLVE_SUPPORT_NO) {
ac3a84
                 r = manager_mdns_start(l->manager);
ac3a84
                 if (r < 0)
ac3a84
                         return r;
ac3a84
@@ -802,6 +797,24 @@ bool link_dnssec_supported(Link *l) {
ac3a84
         return true;
ac3a84
 }
ac3a84
 
ac3a84
+ResolveSupport link_get_llmnr_support(Link *link) {
ac3a84
+        assert(link);
ac3a84
+        assert(link->manager);
ac3a84
+
ac3a84
+        /* This provides the effective LLMNR support level for the link, instead of the 'internal' per-link setting. */
ac3a84
+
ac3a84
+        return MIN(link->llmnr_support, link->manager->llmnr_support);
ac3a84
+}
ac3a84
+
ac3a84
+ResolveSupport link_get_mdns_support(Link *link) {
ac3a84
+        assert(link);
ac3a84
+        assert(link->manager);
ac3a84
+
ac3a84
+        /* This provides the effective mDNS support level for the link, instead of the 'internal' per-link setting. */
ac3a84
+
ac3a84
+        return MIN(link->mdns_support, link->manager->mdns_support);
ac3a84
+}
ac3a84
+
ac3a84
 int link_address_new(Link *l, LinkAddress **ret, int family, const union in_addr_union *in_addr) {
ac3a84
         LinkAddress *a;
ac3a84
 
ac3a84
@@ -885,8 +898,7 @@ void link_address_add_rrs(LinkAddress *a, bool force_remove) {
ac3a84
                 if (!force_remove &&
ac3a84
                     link_address_relevant(a, true) &&
ac3a84
                     a->link->llmnr_ipv4_scope &&
ac3a84
-                    a->link->llmnr_support == RESOLVE_SUPPORT_YES &&
ac3a84
-                    a->link->manager->llmnr_support == RESOLVE_SUPPORT_YES) {
ac3a84
+                    link_get_llmnr_support(a->link) == RESOLVE_SUPPORT_YES) {
ac3a84
 
ac3a84
                         if (!a->link->manager->llmnr_host_ipv4_key) {
ac3a84
                                 a->link->manager->llmnr_host_ipv4_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, a->link->manager->llmnr_hostname);
ac3a84
@@ -939,8 +951,7 @@ void link_address_add_rrs(LinkAddress *a, bool force_remove) {
ac3a84
                 if (!force_remove &&
ac3a84
                     link_address_relevant(a, true) &&
ac3a84
                     a->link->mdns_ipv4_scope &&
ac3a84
-                    a->link->mdns_support == RESOLVE_SUPPORT_YES &&
ac3a84
-                    a->link->manager->mdns_support == RESOLVE_SUPPORT_YES) {
ac3a84
+                    link_get_mdns_support(a->link) == RESOLVE_SUPPORT_YES) {
ac3a84
                         if (!a->link->manager->mdns_host_ipv4_key) {
ac3a84
                                 a->link->manager->mdns_host_ipv4_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, a->link->manager->mdns_hostname);
ac3a84
                                 if (!a->link->manager->mdns_host_ipv4_key) {
ac3a84
@@ -995,8 +1006,7 @@ void link_address_add_rrs(LinkAddress *a, bool force_remove) {
ac3a84
                 if (!force_remove &&
ac3a84
                     link_address_relevant(a, true) &&
ac3a84
                     a->link->llmnr_ipv6_scope &&
ac3a84
-                    a->link->llmnr_support == RESOLVE_SUPPORT_YES &&
ac3a84
-                    a->link->manager->llmnr_support == RESOLVE_SUPPORT_YES) {
ac3a84
+                    link_get_llmnr_support(a->link) == RESOLVE_SUPPORT_YES) {
ac3a84
 
ac3a84
                         if (!a->link->manager->llmnr_host_ipv6_key) {
ac3a84
                                 a->link->manager->llmnr_host_ipv6_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, a->link->manager->llmnr_hostname);
ac3a84
@@ -1049,8 +1059,7 @@ void link_address_add_rrs(LinkAddress *a, bool force_remove) {
ac3a84
                 if (!force_remove &&
ac3a84
                     link_address_relevant(a, true) &&
ac3a84
                     a->link->mdns_ipv6_scope &&
ac3a84
-                    a->link->mdns_support == RESOLVE_SUPPORT_YES &&
ac3a84
-                    a->link->manager->mdns_support == RESOLVE_SUPPORT_YES) {
ac3a84
+                    link_get_mdns_support(a->link) == RESOLVE_SUPPORT_YES) {
ac3a84
 
ac3a84
                         if (!a->link->manager->mdns_host_ipv6_key) {
ac3a84
                                 a->link->manager->mdns_host_ipv6_key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, a->link->manager->mdns_hostname);
ac3a84
diff --git a/src/resolve/resolved-link.h b/src/resolve/resolved-link.h
ac3a84
index b5299e0b5b..d2043a1000 100644
ac3a84
--- a/src/resolve/resolved-link.h
ac3a84
+++ b/src/resolve/resolved-link.h
ac3a84
@@ -104,6 +104,9 @@ bool link_dnssec_supported(Link *l);
ac3a84
 
ac3a84
 DnsOverTlsMode link_get_dns_over_tls_mode(Link *l);
ac3a84
 
ac3a84
+ResolveSupport link_get_llmnr_support(Link *link);
ac3a84
+ResolveSupport link_get_mdns_support(Link *link);
ac3a84
+
ac3a84
 int link_save_user(Link *l);
ac3a84
 int link_load_user(Link *l);
ac3a84
 void link_remove_user(Link *l);
ac3a84
diff --git a/src/shared/resolve-util.h b/src/shared/resolve-util.h
ac3a84
index d9ab387301..e58173d864 100644
ac3a84
--- a/src/shared/resolve-util.h
ac3a84
+++ b/src/shared/resolve-util.h
ac3a84
@@ -25,10 +25,11 @@ typedef enum ResolveSupport ResolveSupport;
ac3a84
 typedef enum DnssecMode DnssecMode;
ac3a84
 typedef enum DnsOverTlsMode DnsOverTlsMode;
ac3a84
 
ac3a84
+/* Do not change the order, see link_get_llmnr_support() or link_get_mdns_support(). */
ac3a84
 enum ResolveSupport {
ac3a84
         RESOLVE_SUPPORT_NO,
ac3a84
-        RESOLVE_SUPPORT_YES,
ac3a84
         RESOLVE_SUPPORT_RESOLVE,
ac3a84
+        RESOLVE_SUPPORT_YES,
ac3a84
         _RESOLVE_SUPPORT_MAX,
ac3a84
         _RESOLVE_SUPPORT_INVALID = -EINVAL,
ac3a84
 };