]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
networkd: DHCP override hostname 452/head
authorSusant Sahani <susant@redhat.com>
Fri, 10 Jul 2015 10:12:27 +0000 (15:42 +0530)
committerSusant Sahani <susant@redhat.com>
Fri, 10 Jul 2015 10:12:27 +0000 (15:42 +0530)
This patch enhances the DHCP client to send the hostname
reference
http://lists.freedesktop.org/archives/systemd-devel/2014-July/021550.html

Tested with
Example conf:

[Match]
Name=eth1

[Network]
DHCP=v4

[DHCP]
SendHostname=true
Hostname=test

src/network/networkd-dhcp4.c
src/network/networkd-network-gperf.gperf
src/network/networkd-network.c
src/network/networkd.h

index d446bfa8b3fdb4d929ab0969f823167fe0b73499..4aa301b112a4afbda50b81f65ffd19d510e1ba18 100644 (file)
@@ -270,12 +270,18 @@ static int dhcp_lease_lost(Link *link) {
         if (link->network->dhcp_hostname) {
                 const char *hostname = NULL;
 
-                r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
-                if (r >= 0 && hostname) {
-                        r = link_set_hostname(link, "");
+                if (!link->network->hostname)
+                        r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
+                else
+                        hostname = link->network->hostname;
+
+                if (r >= 0 || hostname) {
+                        r = link_set_hostname(link, hostname);
                         if (r < 0)
-                                log_link_error(link,
-                                               "Failed to reset transient hostname");
+                                log_link_error_errno(link, r,
+                                                     "Failed to set transient hostname to '%s': %m",
+                                                     hostname);
+
                 }
         }
 
@@ -464,8 +470,12 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
         if (link->network->dhcp_hostname) {
                 const char *hostname;
 
-                r = sd_dhcp_lease_get_hostname(lease, &hostname);
-                if (r >= 0) {
+                if (!link->network->hostname)
+                        r = sd_dhcp_lease_get_hostname(lease, &hostname);
+                else
+                        hostname = link->network->hostname;
+
+                if (r >= 0 || hostname) {
                         r = link_set_hostname(link, hostname);
                         if (r < 0)
                                 log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname);
@@ -616,14 +626,19 @@ int dhcp4_configure(Link *link) {
 
         if (link->network->dhcp_sendhost) {
                 _cleanup_free_ char *hostname = NULL;
+                const char *hn = NULL;
+
+                if (!link->network->hostname)  {
+                        hostname = gethostname_malloc();
+                        if (!hostname)
+                                return -ENOMEM;
 
-                hostname = gethostname_malloc();
-                if (!hostname)
-                        return -ENOMEM;
+                        hn = hostname;
+                } else
+                        hn = link->network->hostname;
 
-                if (!is_localhost(hostname)) {
-                        r = sd_dhcp_client_set_hostname(link->dhcp_client,
-                                                        hostname);
+                if (!is_localhost(hn)) {
+                        r = sd_dhcp_client_set_hostname(link->dhcp_client, hn);
                         if (r < 0)
                                 return r;
                 }
index 787fc2ff5bec1c05e163cc3b362954cc8328efcf..3a78c3d8a89abee280a702dabb3cd637bbcfab57 100644 (file)
@@ -67,6 +67,7 @@ DHCP.UseHostname,              config_parse_bool,                              0
 DHCP.UseDomains,               config_parse_bool,                              0,                             offsetof(Network, dhcp_domains)
 DHCP.UseRoutes,                config_parse_bool,                              0,                             offsetof(Network, dhcp_routes)
 DHCP.SendHostname,             config_parse_bool,                              0,                             offsetof(Network, dhcp_sendhost)
+DHCP.Hostname,                 config_parse_hostname,                          0,                             offsetof(Network, hostname)
 DHCP.RequestBroadcast,         config_parse_bool,                              0,                             offsetof(Network, dhcp_broadcast)
 DHCP.CriticalConnection,       config_parse_bool,                              0,                             offsetof(Network, dhcp_critical)
 DHCP.VendorClassIdentifier,    config_parse_string,                            0,                             offsetof(Network, dhcp_vendor_class_identifier)
index a8e9ef909c3eb20df33ff387ba410708250f8c5f..55e735e6bf44b792a84fa471d83b762caee779be 100644 (file)
@@ -207,6 +207,7 @@ void network_free(Network *network) {
 
         free(network->description);
         free(network->dhcp_vendor_class_identifier);
+        free(network->hostname);
 
         free(network->mac);
 
@@ -809,3 +810,38 @@ int config_parse_ipv6_privacy_extensions(
 
         return 0;
 }
+
+int config_parse_hostname(const char *unit,
+                          const char *filename,
+                          unsigned line,
+                          const char *section,
+                          unsigned section_line,
+                          const char *lvalue,
+                          int ltype,
+                          const char *rvalue,
+                          void *data,
+                          void *userdata) {
+        char **hostname = data;
+        char *hn = NULL;
+        int r;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+
+        r = config_parse_string(unit, filename, line, section, section_line,
+                                lvalue, ltype, rvalue, &hn, userdata);
+        if (r < 0)
+                return r;
+
+        if (!hostname_is_valid(hn)) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL, "hostname is not valid, ignoring assignment: %s", rvalue);
+
+                free(hn);
+                return 0;
+        }
+
+        *hostname = hn;
+
+        return 0;
+}
index f98c640822ca02a90983443f3d1d26b7a2bd993b..ccec4cf6b222795198f14982b158e012ced66774 100644 (file)
@@ -133,6 +133,7 @@ struct Network {
         AddressFamilyBoolean dhcp;
         DCHPClientIdentifier dhcp_client_identifier;
         char *dhcp_vendor_class_identifier;
+        char *hostname;
         bool dhcp_dns;
         bool dhcp_ntp;
         bool dhcp_mtu;
@@ -473,3 +474,7 @@ const char* ipv6_privacy_extensions_to_string(IPv6PrivacyExtensions i) _const_;
 IPv6PrivacyExtensions ipv6_privacy_extensions_from_string(const char *s) _pure_;
 
 int config_parse_ipv6_privacy_extensions(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+
+
+/* Hostname */
+int config_parse_hostname(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);