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 <l.bosma@interay.com>
This commit is contained in:
Luuk Bosma 2020-02-21 12:24:22 +01:00 committed by Jukka Rissanen
commit 9587a271f6
2 changed files with 28 additions and 0 deletions

View file

@ -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;
}

View file

@ -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