[systemd-devel] [PATCH] resolved: re-add support for getting local domain from DHCP

Michael Marineau michael.marineau at coreos.com
Tue Jul 22 22:08:53 PDT 2014


When the code for generating resolv.conf was moved from networkd to
resolved the DHCP domain name code was dropped.
---
 src/network/networkd-link.c    |  2 ++
 src/network/sd-network.c       |  4 ++++
 src/resolve/resolved-link.c    | 31 +++++++++++++++++++++++++++++++
 src/resolve/resolved-link.h    |  2 ++
 src/resolve/resolved-manager.c |  7 +++++++
 src/systemd/sd-network.h       |  3 +++
 6 files changed, 49 insertions(+)

diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 0a6f524..afca172 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -2524,9 +2524,11 @@ int link_save(Link *link) {
                 fprintf(f,
                         "DHCP_LEASE=%s\n"
                         "DHCP_USE_DNS=%s\n"
+                        "DHCP_USE_DOMAINNAME=%s\n"
                         "DHCP_USE_NTP=%s\n",
                         link->lease_file,
                         yes_no(link->network->dhcp_dns),
+                        yes_no(link->network->dhcp_domainname),
                         yes_no(link->network->dhcp_ntp));
         } else
                 unlink(link->lease_file);
diff --git a/src/network/sd-network.c b/src/network/sd-network.c
index 91d6275..5dfdd59 100644
--- a/src/network/sd-network.c
+++ b/src/network/sd-network.c
@@ -202,6 +202,10 @@ _public_ int sd_network_dhcp_use_dns(int ifindex) {
         return network_get_boolean("DHCP_USE_DNS", ifindex);
 }
 
+_public_ int sd_network_dhcp_use_domainname(int ifindex) {
+        return network_get_boolean("DHCP_USE_DOMAINNAME", ifindex);
+}
+
 _public_ int sd_network_dhcp_use_ntp(int ifindex) {
         return network_get_boolean("DHCP_USE_NTP", ifindex);
 }
diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c
index 078301a..c0b19a6 100644
--- a/src/resolve/resolved-link.c
+++ b/src/resolve/resolved-link.c
@@ -77,6 +77,7 @@ Link *link_free(Link *l) {
         while (l->link_dns_servers)
                 dns_server_free(l->link_dns_servers);
 
+        free(l->dhcp_domainname);
         free(l);
         return NULL;
 }
@@ -249,11 +250,41 @@ clear:
         return r;
 }
 
+static int link_update_dhcp_domainname(Link *l) {
+        _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
+        const char *domainname = NULL;
+        int r;
+
+        assert(l);
+
+        free(l->dhcp_domainname);
+        l->dhcp_domainname = NULL;
+
+        r = sd_network_dhcp_use_dns(l->ifindex);
+        if (r <= 0)
+                return r;
+
+        r = sd_network_get_dhcp_lease(l->ifindex, &lease);
+        if (r < 0)
+                return r;
+
+        r = sd_dhcp_lease_get_domainname(lease, &domainname);
+        if (r < 0)
+                return r;
+
+        l->dhcp_domainname = strdup(domainname);
+        if (!l->dhcp_domainname)
+                return -ENOMEM;
+
+        return 0;
+}
+
 int link_update_monitor(Link *l) {
         assert(l);
 
         link_update_dhcp_dns_servers(l);
         link_update_link_dns_servers(l);
+        link_update_dhcp_domainname(l);
         link_allocate_scopes(l);
 
         return 0;
diff --git a/src/resolve/resolved-link.h b/src/resolve/resolved-link.h
index bd32a70..8ea3acd 100644
--- a/src/resolve/resolved-link.h
+++ b/src/resolve/resolved-link.h
@@ -65,6 +65,8 @@ struct Link {
 
         RateLimit mdns_ratelimit;
         RateLimit llmnr_ratelimit;
+
+        char *dhcp_domainname;
 };
 
 int link_new(Manager *m, Link **ret, int ifindex);
diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c
index 9672843..09f7695 100644
--- a/src/resolve/resolved-manager.c
+++ b/src/resolve/resolved-manager.c
@@ -497,6 +497,7 @@ int manager_write_resolv_conf(Manager *m) {
         const char *path = "/run/systemd/resolve/resolv.conf";
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
+        const char *domainname = NULL;
         unsigned count = 0;
         DnsServer *s;
         Iterator i;
@@ -523,11 +524,17 @@ int manager_write_resolv_conf(Manager *m) {
 
                 LIST_FOREACH(servers, s, l->dhcp_dns_servers)
                         write_resolve_conf_server(s, f, &count);
+
+                if (!domainname && l->dhcp_domainname)
+                        domainname = l->dhcp_domainname;
         }
 
         LIST_FOREACH(servers, s, m->dns_servers)
                 write_resolve_conf_server(s, f, &count);
 
+        if (domainname)
+                fprintf(f, "domain %s\n", domainname);
+
         r = fflush_and_check(f);
         if (r < 0)
                 goto fail;
diff --git a/src/systemd/sd-network.h b/src/systemd/sd-network.h
index e454705..826cec7 100644
--- a/src/systemd/sd-network.h
+++ b/src/systemd/sd-network.h
@@ -79,6 +79,9 @@ int sd_network_get_dhcp_lease(int ifindex, sd_dhcp_lease **ret);
 /* Returns true if link is configured to respect DNS entries received by DHCP */
 int sd_network_dhcp_use_dns(int ifindex);
 
+/* Returns true if link is configured to use the domain name received by DHCP */
+int sd_network_dhcp_use_domainname(int ifindex);
+
 /* Returns true if link is configured to respect NTP entries received by DHCP */
 int sd_network_dhcp_use_ntp(int ifindex);
 
-- 
1.8.5.5



More information about the systemd-devel mailing list