networkd: send hostname to dhcp server

Send hostname (option 12) in DISCOVER and REQUEST messages so the
DHCP server could use it to register with dynamic DNS and such.

To opt-out of this behaviour set SendHostname to false in [DHCP]
section of .network file

[tomegun: rebased, made sure a failing set_hostname is a noop and moved
          config from DHCPv4 to DHCP]
This commit is contained in:
Eugene Yakubovich
2014-07-01 22:02:25 +02:00
committed by Tom Gundersen
parent 0a8a0fad01
commit 4cc7a82c94
7 changed files with 81 additions and 2 deletions
+26
View File
@@ -21,6 +21,7 @@
#include <netinet/ether.h>
#include <linux/if.h>
#include <unistd.h>
#include "networkd.h"
#include "libudev-private.h"
@@ -1927,6 +1928,18 @@ static int link_enter_enslave(Link *link) {
return 0;
}
/* make sure the hostname is not "localhost" */
static bool is_localhost(const char *hostname) {
assert(hostname);
return streq(hostname, "localhost") ||
streq(hostname, "localhost.") ||
endswith(hostname, ".localhost") ||
endswith(hostname, ".localhost.") ||
endswith(hostname, ".localdomain") ||
endswith(hostname, ".localdomain.");
}
static int link_configure(Link *link) {
int r;
@@ -1992,6 +2005,7 @@ static int link_configure(Link *link) {
if (r < 0)
return r;
}
if (link->network->dhcp_routes) {
r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_STATIC_ROUTE);
if (r < 0)
@@ -2000,6 +2014,18 @@ static int link_configure(Link *link) {
if (r < 0)
return r;
}
if (link->network->dhcp_sendhost) {
_cleanup_free_ char *hostname = gethostname_malloc();
if (!hostname)
return -ENOMEM;
if (!is_localhost(hostname)) {
r = sd_dhcp_client_set_hostname(link->dhcp_client, hostname);
if (r < 0)
return r;
}
}
}
if (link->network->dhcp_server) {
+3 -2
View File
@@ -47,11 +47,12 @@ DHCP.UseDNS, config_parse_bool, 0,
DHCP.UseMTU, config_parse_bool, 0, offsetof(Network, dhcp_mtu)
DHCP.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp_hostname)
DHCP.UseDomainName, config_parse_bool, 0, offsetof(Network, dhcp_domainname)
DHCP.UseRoutes, config_parse_bool, 0, offsetof(Network, dhcp_routes)
DHCP.SendHostname, config_parse_bool, 0, offsetof(Network, dhcp_sendhost)
DHCP.CriticalConnection, config_parse_bool, 0, offsetof(Network, dhcp_critical)
/* backwards compatibility */
/* backwards compatibility: do not add new entries to this section */
DHCPv4.UseDNS, config_parse_bool, 0, offsetof(Network, dhcp_dns)
DHCPv4.UseMTU, config_parse_bool, 0, offsetof(Network, dhcp_mtu)
DHCPv4.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp_hostname)
DHCPv4.UseDomainName, config_parse_bool, 0, offsetof(Network, dhcp_domainname)
DHCPv4.UseRoutes, config_parse_bool, 0, offsetof(Network, dhcp_routes)
DHCPv4.CriticalConnection, config_parse_bool, 0, offsetof(Network, dhcp_critical)
+1
View File
@@ -90,6 +90,7 @@ static int network_load_one(Manager *manager, const char *filename) {
network->dhcp_hostname = true;
network->dhcp_domainname = true;
network->dhcp_routes = true;
network->dhcp_sendhost = true;
r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0DHCPv4\0", config_item_perf_lookup,
(void*) network_network_gperf_lookup, false, false, network);
+1
View File
@@ -169,6 +169,7 @@ struct Network {
bool dhcp_mtu;
bool dhcp_hostname;
bool dhcp_domainname;
bool dhcp_sendhost;
bool dhcp_critical;
bool dhcp_routes;
bool ipv4ll;