net: Add hostname setting to Kconfig
User can configure hostname of the device in Kconfig. This can be used by mDNS responder to answer <hostname>.local queries. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
parent
ba85e4ad20
commit
e3adbf845c
7 changed files with 201 additions and 0 deletions
82
include/net/hostname.h
Normal file
82
include/net/hostname.h
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/** @file
|
||||
* @brief Hostname configuration definitions
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef __NET_HOSTNAME_H
|
||||
#define __NET_HOSTNAME_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Network hostname configuration library
|
||||
* @defgroup net_hostname Network Hostname Library
|
||||
* @{
|
||||
*/
|
||||
|
||||
#if defined(CONFIG_NET_HOSTNAME_ENABLE)
|
||||
/**
|
||||
* @brief Get the device hostname
|
||||
*
|
||||
* @details Return pointer to device hostname.
|
||||
*
|
||||
* @return Pointer to hostname or NULL if not set.
|
||||
*/
|
||||
const char *net_hostname_get(void);
|
||||
|
||||
/**
|
||||
* @brief Initialize and set the device hostname.
|
||||
*
|
||||
*/
|
||||
void net_hostname_init(void);
|
||||
|
||||
#else
|
||||
static inline const char *net_hostname_get(void)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline void net_hostname_init(void)
|
||||
{
|
||||
}
|
||||
#endif /* CONFIG_NET_HOSTNAME_ENABLE */
|
||||
|
||||
#if defined(CONFIG_NET_HOSTNAME_UNIQUE)
|
||||
/**
|
||||
* @brief Set the device hostname postfix
|
||||
*
|
||||
* @details Set the device hostname to some value. This is only used if
|
||||
* CONFIG_NET_HOSTNAME_UNIQUE is set.
|
||||
*
|
||||
* @param hostname_postfix Usually link address. The function will convert this
|
||||
* to a string.
|
||||
* @param postfix_len Length of the hostname_postfix array.
|
||||
*
|
||||
* @return 0 if ok, <0 if error
|
||||
*/
|
||||
int net_hostname_set_postfix(const u8_t *hostname_postfix,
|
||||
int postfix_len);
|
||||
#else
|
||||
static inline int net_hostname_set_postfix(const u8_t *hostname_postfix,
|
||||
int postfix_len)
|
||||
{
|
||||
return -EMSGSIZE;
|
||||
}
|
||||
#endif /* CONFIG_NET_HOSTNAME_UNIQUE */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __NET_HOSTNAME_H */
|
||||
|
|
@ -22,6 +22,7 @@
|
|||
#include <misc/slist.h>
|
||||
|
||||
#include <net/net_core.h>
|
||||
#include <net/hostname.h>
|
||||
#include <net/net_linkaddr.h>
|
||||
#include <net/net_ip.h>
|
||||
#include <net/net_l2.h>
|
||||
|
|
@ -453,6 +454,8 @@ static inline int net_if_set_link_addr(struct net_if *iface,
|
|||
iface->link_addr.len = len;
|
||||
iface->link_addr.type = type;
|
||||
|
||||
net_hostname_set_postfix(addr, len);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -81,6 +81,8 @@ config NETWORKING
|
|||
|
||||
if NETWORKING
|
||||
|
||||
source "subsys/net/Kconfig.hostname"
|
||||
|
||||
source "subsys/net/ip/Kconfig"
|
||||
|
||||
source "subsys/net/lib/Kconfig"
|
||||
|
|
|
|||
39
subsys/net/Kconfig.hostname
Normal file
39
subsys/net/Kconfig.hostname
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# Kconfig - Hostname config
|
||||
|
||||
#
|
||||
# Copyright (c) 2017 Intel Corporation.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
config NET_HOSTNAME_ENABLE
|
||||
bool "Add hostname to the device"
|
||||
default n
|
||||
help
|
||||
This is used for example in mDNS to respond to <hostname>.local
|
||||
mDNS queries.
|
||||
|
||||
if NET_HOSTNAME_ENABLE
|
||||
|
||||
config NET_HOSTNAME
|
||||
string "The hostname of this device"
|
||||
default "zephyr"
|
||||
help
|
||||
The string should be a valid hostname.
|
||||
|
||||
config NET_HOSTNAME_UNIQUE
|
||||
bool "Make hostname unique"
|
||||
default n
|
||||
help
|
||||
This will append link address to hostname to create a unique
|
||||
hostname. For example, zephyr00005e005357 could be the hostname
|
||||
if this setting is enabled.
|
||||
|
||||
config NET_DEBUG_HOSTNAME
|
||||
bool "Debug hostname configuration"
|
||||
default n
|
||||
depends on NET_LOG
|
||||
help
|
||||
Enables hostname configuration functions to output debug messages
|
||||
|
||||
endif
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
obj-$(CONFIG_NET_BUF) += buf.o
|
||||
obj-$(CONFIG_NET_HOSTNAME_ENABLE) += hostname.o
|
||||
|
||||
ifeq ($(CONFIG_NETWORKING),y)
|
||||
ifeq ($(CONFIG_NET_L2_RAW_CHANNEL),y)
|
||||
|
|
|
|||
72
subsys/net/hostname.c
Normal file
72
subsys/net/hostname.c
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
/** @file
|
||||
* @brief Hostname configuration
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#if defined(CONFIG_NET_DEBUG_HOSTNAME)
|
||||
#define SYS_LOG_DOMAIN "net/hostname"
|
||||
#define NET_LOG_ENABLED 1
|
||||
#endif
|
||||
|
||||
#include <zephyr.h>
|
||||
|
||||
#include <net/net_core.h>
|
||||
|
||||
#if defined(CONFIG_NET_HOSTNAME_UNIQUE)
|
||||
/* Allocate extra space to append MAC address to hostname */
|
||||
#define EXTRA_SPACE (8 * 2)
|
||||
#else
|
||||
#define EXTRA_SPACE 0
|
||||
#endif /* CONFIG_NET_HOSTNAME_UNIQUE */
|
||||
|
||||
static char hostname[sizeof(CONFIG_NET_HOSTNAME) - 1 + EXTRA_SPACE];
|
||||
|
||||
const char *net_hostname_get(void)
|
||||
{
|
||||
return hostname;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_NET_HOSTNAME_UNIQUE)
|
||||
int net_hostname_set_postfix(const u8_t *hostname_postfix,
|
||||
int postfix_len)
|
||||
{
|
||||
static bool postfix_set;
|
||||
int pos = 0;
|
||||
int i;
|
||||
|
||||
if (postfix_set) {
|
||||
return -EALREADY;
|
||||
}
|
||||
|
||||
NET_ASSERT(postfix_len > 0);
|
||||
|
||||
/* Note that we convert the postfix to hex (2 chars / byte) */
|
||||
if ((postfix_len * 2) >
|
||||
((sizeof(hostname) - 1) - (sizeof(CONFIG_NET_HOSTNAME) - 1))) {
|
||||
return -EMSGSIZE;
|
||||
}
|
||||
|
||||
for (i = 0; i < postfix_len; i++, pos += 2) {
|
||||
snprintk(&hostname[sizeof(CONFIG_NET_HOSTNAME) - 1 + pos],
|
||||
2 + 1, "%02x", hostname_postfix[i]);
|
||||
}
|
||||
|
||||
NET_DBG("New hostname %s", hostname);
|
||||
|
||||
postfix_set = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* CONFIG_NET_HOSTNAME_UNIQUE */
|
||||
|
||||
void net_hostname_init(void)
|
||||
{
|
||||
memcpy(hostname, CONFIG_NET_HOSTNAME, sizeof(CONFIG_NET_HOSTNAME) - 1);
|
||||
|
||||
NET_DBG("Hostname set to %s", hostname);
|
||||
}
|
||||
|
|
@ -381,6 +381,8 @@ static int net_init(struct device *unused)
|
|||
{
|
||||
int status = 0;
|
||||
|
||||
net_hostname_init();
|
||||
|
||||
NET_DBG("Priority %d", CONFIG_NET_INIT_PRIO);
|
||||
|
||||
net_pkt_init();
|
||||
|
|
|
|||
Loading…
Reference in a new issue