From 9587a271f602a0c15c3b858c27f23a4800935bf9 Mon Sep 17 00:00:00 2001 From: Luuk Bosma Date: Fri, 21 Feb 2020 12:24:22 +0100 Subject: [PATCH] net/ip/dhcpv4: Set Host Name in DHCP Request This option specifies the name of the client. See https://tools.ietf.org/html/rfc2132#section-3.14 This is useful for identification when looking in DHCP lease table. Signed-off-by: Luuk Bosma --- subsys/net/ip/dhcpv4.c | 26 ++++++++++++++++++++++++++ subsys/net/ip/dhcpv4.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/subsys/net/ip/dhcpv4.c b/subsys/net/ip/dhcpv4.c index f0a8b0add52..c028bb4de1c 100644 --- a/subsys/net/ip/dhcpv4.c +++ b/subsys/net/ip/dhcpv4.c @@ -117,6 +117,15 @@ static bool dhcpv4_add_req_ipaddr(struct net_pkt *pkt, 4, addr->s4_addr); } +#if defined(CONFIG_NET_HOSTNAME_ENABLE) +static bool dhcpv4_add_hostname(struct net_pkt *pkt, + const char *hostname, const size_t size) +{ + return dhcpv4_add_option_length_value(pkt, DHCPV4_OPTIONS_HOST_NAME, + size, hostname); +} +#endif + /* Add DHCPv4 Options end, rest of the message can be padded wit zeros */ static inline bool dhcpv4_add_end(struct net_pkt *pkt) { @@ -159,6 +168,10 @@ static struct net_pkt *dhcpv4_create_message(struct net_if *iface, u8_t type, size_t size = DHCPV4_MESSAGE_SIZE; struct net_pkt *pkt; struct dhcp_msg *msg; +#if defined(CONFIG_NET_HOSTNAME_ENABLE) + const char *hostname = net_hostname_get(); + const size_t hostname_size = strlen(hostname); +#endif if (src_addr == NULL) { addr = net_ipv4_unspecified_address(); @@ -178,6 +191,12 @@ static struct net_pkt *dhcpv4_create_message(struct net_if *iface, u8_t type, size += DHCPV4_OLV_MSG_REQ_LIST; } +#if defined(CONFIG_NET_HOSTNAME_ENABLE) + if (hostname_size > 0) { + size += DHCPV4_OLV_MSG_HOST_NAME + hostname_size; + } +#endif + pkt = net_pkt_alloc_with_buffer(iface, size, AF_INET, IPPROTO_UDP, K_FOREVER); @@ -232,6 +251,13 @@ static struct net_pkt *dhcpv4_create_message(struct net_if *iface, u8_t type, goto fail; } +#if defined(CONFIG_NET_HOSTNAME_ENABLE) + if (hostname_size > 0 && + !dhcpv4_add_hostname(pkt, hostname, hostname_size)) { + goto fail; + } +#endif + if (!dhcpv4_add_end(pkt)) { goto fail; } diff --git a/subsys/net/ip/dhcpv4.h b/subsys/net/ip/dhcpv4.h index 6e1ab31f57e..1828b142973 100644 --- a/subsys/net/ip/dhcpv4.h +++ b/subsys/net/ip/dhcpv4.h @@ -69,6 +69,7 @@ enum dhcpv4_msg_type { #define DHCPV4_OPTIONS_SUBNET_MASK 1 #define DHCPV4_OPTIONS_ROUTER 3 #define DHCPV4_OPTIONS_DNS_SERVER 6 +#define DHCPV4_OPTIONS_HOST_NAME 12 #define DHCPV4_OPTIONS_REQ_IPADDR 50 #define DHCPV4_OPTIONS_LEASE_TIME 51 #define DHCPV4_OPTIONS_MSG_TYPE 53 @@ -79,6 +80,7 @@ enum dhcpv4_msg_type { #define DHCPV4_OPTIONS_END 255 /* Useful size macros */ +#define DHCPV4_OLV_MSG_HOST_NAME 2 #define DHCPV4_OLV_MSG_REQ_IPADDR 6 #define DHCPV4_OLV_MSG_TYPE_SIZE 3 #define DHCPV4_OLV_MSG_SERVER_ID 6