[systemd-devel] [PATCH] networkd: send hostname to dhcp server

Greg KH gregkh at linuxfoundation.org
Mon Jun 30 17:09:51 PDT 2014


On Mon, Jun 30, 2014 at 04:52:21PM -0700, Eugene Yakubovich wrote:
> Send hostname (option 12) in DISCOVER and REQUEST messages so the
> DHCP server could use it to register with dynamic DNS and such.
> ---
>  src/libsystemd-network/sd-dhcp-client.c | 35 +++++++++++++++++++++++++++++++++
>  src/network/networkd-link.c             | 26 ++++++++++++++++++++++++
>  src/systemd/sd-dhcp-client.h            |  1 +
>  3 files changed, 62 insertions(+)
> 
> diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c
> index 1603c41..44a5e8a 100644
> --- a/src/libsystemd-network/sd-dhcp-client.c
> +++ b/src/libsystemd-network/sd-dhcp-client.c
> @@ -56,6 +56,7 @@ struct sd_dhcp_client {
>                  uint8_t type;
>                  struct ether_addr mac_addr;
>          } _packed_ client_id;
> +        char *hostname;
>          uint32_t xid;
>          usec_t start_time;
>          uint16_t secs;
> @@ -178,6 +179,20 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client,
>          return 0;
>  }
>  
> +int sd_dhcp_client_set_hostname(sd_dhcp_client *client,
> +                        const char *hostname) {
> +
> +        assert_return(client, -EINVAL);
> +        assert_return(hostname, -EINVAL);
> +
> +        if (client->hostname && streq(client->hostname, hostname) )
> +                return 0;
> +
> +        client->hostname = strdup(hostname);
> +
> +        return 0;
> +}
> +
>  int sd_dhcp_client_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret) {
>          assert_return(client, -EINVAL);
>          assert_return(ret, -EINVAL);
> @@ -386,6 +401,17 @@ static int client_send_discover(sd_dhcp_client *client) {
>                          return r;
>          }
>  
> +        /* it is unclear from RFC 2131 if client should send hostname in
> +           DHCPDISCOVER but dhclient does and so we do as well
> +        */
> +        if (client->hostname) {
> +                r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0,
> +                                       DHCP_OPTION_HOST_NAME,
> +                                       strlen(client->hostname), client->hostname);
> +                if (r < 0)
> +                        return r;
> +        }
> +
>          r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0,
>                                 DHCP_OPTION_END, 0, NULL);
>          if (r < 0)
> @@ -477,6 +503,14 @@ static int client_send_request(sd_dhcp_client *client) {
>                  return -EINVAL;
>          }
>  
> +        if (client->hostname) {
> +                r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
> +                                       DHCP_OPTION_HOST_NAME,
> +                                       strlen(client->hostname), client->hostname);
> +                if (r < 0)
> +                        return r;
> +        }
> +
>          r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0,
>                                 DHCP_OPTION_END, 0, NULL);
>          if (r < 0)
> @@ -1363,6 +1397,7 @@ sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client) {
>                  sd_dhcp_lease_unref(client->lease);
>  
>                  free(client->req_opts);
> +                free(client->hostname);
>                  free(client);
>          }
>  
> diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
> index 9296a59..9d849a0 100644
> --- a/src/network/networkd-link.c
> +++ b/src/network/networkd-link.c
> @@ -21,6 +21,7 @@
>  
>  #include <netinet/ether.h>
>  #include <linux/if.h>
> +#include <unistd.h>
>  
>  #include "networkd.h"
>  #include "libudev-private.h"
> @@ -1846,6 +1847,19 @@ static int link_enter_enslave(Link *link) {
>          return 0;
>  }
>  
> +/* make sure the hostname is not "localhost" */
> +static bool is_localhost(const char *hostname) {
> +        char *hostend;
> +
> +        assert(hostname);

You only call this in one place, with an array on the stack, so it can
never be NULL.  So why check this at all?

Other than that minor nit, this looks good, thanks for adding it.

greg k-h


More information about the systemd-devel mailing list