Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/posix/options/Kconfig.net
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ menuconfig POSIX_NETWORKING
bool "POSIX Networking API"
depends on NETWORKING
select NET_HOSTNAME_ENABLE
select NET_HOSTNAME_DYNAMIC
select NET_INTERFACE_NAME
select NET_SOCKETPAIR
select NET_SOCKETS
Expand Down
18 changes: 10 additions & 8 deletions subsys/net/Kconfig.hostname
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ config NET_HOSTNAME_ENABLE
This is used for example in mDNS to respond to <hostname>.local
mDNS queries.

config NET_HOSTNAME_MAX_LEN
int "The maximum allowed hostname length"
# This corresponds to "zephyr"
default 6 if !NET_HOSTNAME_ENABLE
default 63
help
This will set the number of bytes allocated for the hostname.
When NET_HOSTNAME_ENABLE is disabled, this defaults to 6 to
accommodate the default "zephyr" hostname.

if NET_HOSTNAME_ENABLE

config NET_HOSTNAME
Expand All @@ -24,14 +34,6 @@ config NET_HOSTNAME_DYNAMIC
This will enable the net_hostname_set() function. NET_HOSTNAME
will be used as default hostname.

config NET_HOSTNAME_MAX_LEN
int "The maximum allowed hostname length"
depends on NET_HOSTNAME_DYNAMIC
range 1 63
default 63
help
This will set the number of bytes allocateed for the hostname.

config NET_HOSTNAME_UNIQUE
bool "Make hostname unique"
help
Expand Down
3 changes: 2 additions & 1 deletion subsys/net/hostname.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ LOG_MODULE_REGISTER(net_hostname, CONFIG_NET_HOSTNAME_LOG_LEVEL);
#include <zephyr/net/net_mgmt.h>
#include <zephyr/logging/log_backend_net.h>

static char hostname[NET_HOSTNAME_SIZE];
BUILD_ASSERT(CONFIG_NET_HOSTNAME_MAX_LEN > 0, "NET_HOSTNAME_MAX_LEN must be a positive value");
static char hostname[NET_HOSTNAME_SIZE] = CONFIG_NET_HOSTNAME;

static void trigger_net_event(void)
{
Expand Down
9 changes: 9 additions & 0 deletions tests/net/hostname/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ tests:
extra_configs:
- CONFIG_NET_HOSTNAME_UNIQUE=y
- CONFIG_NET_HOSTNAME_DYNAMIC=y
net.hostname.posix_api.unique_update_compatibility:
extra_configs:
- CONFIG_POSIX_API=y
- CONFIG_NET_HOSTNAME_UNIQUE_UPDATE=y
- CONFIG_NET_HOSTNAME_UNIQUE=y
net.hostname.posix_api.dynamic_hostname:
extra_configs:
- CONFIG_POSIX_API=y
- CONFIG_NET_HOSTNAME_DYNAMIC=y
net.hostname.event:
extra_configs:
- CONFIG_NET_MGMT=y
Expand Down
97 changes: 97 additions & 0 deletions tests/posix/net/src/gethostname.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
*/

#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <zephyr/ztest.h>
#include <zephyr/net/hostname.h>

ZTEST(net, test_gethostname)
{
Expand All @@ -19,3 +22,97 @@
zassert_equal(strcmp(hostname, CONFIG_NET_HOSTNAME), 0,
"gethostname() returned unexpected hostname: %s", hostname);
}

ZTEST(net, test_gethostname_buffer_too_small)
{
char small_hostname[2];
int ret;

ret = gethostname(small_hostname, sizeof(small_hostname));
/* Should fail with buffer too small */
zassert_equal(ret, -1, "gethostname() should fail with small buffer");
zassert_equal(errno, ENAMETOOLONG, "Expected ENAMETOOLONG, got %d", errno);
}

ZTEST(net, test_gethostname_null_buffer)
{
int ret;

ret = gethostname(NULL, 10);
/* Should fail with null buffer */
zassert_equal(ret, -1, "gethostname() should fail with NULL buffer");
zassert_equal(errno, EFAULT, "Expected EFAULT, got %d", errno);
}

ZTEST(net, test_gethostname_zero_length)
{
char hostname[CONFIG_NET_HOSTNAME_MAX_LEN + 1];
int ret;

ret = gethostname(hostname, 0);
/* Should fail with zero length */
zassert_equal(ret, -1, "gethostname() should fail with zero length");
zassert_equal(errno, EINVAL, "Expected EINVAL, got %d", errno);
}

ZTEST(posix_net, test_hostname_max_len_consistency)
{
/* Verify that CONFIG_NET_HOSTNAME_MAX_LEN is properly defined and > 0 */
zassert_true(CONFIG_NET_HOSTNAME_MAX_LEN > 0,

Check failure on line 61 in tests/posix/net/src/gethostname.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TRAILING_WHITESPACE

tests/posix/net/src/gethostname.c:61 trailing whitespace
"CONFIG_NET_HOSTNAME_MAX_LEN must be positive");

Check failure on line 63 in tests/posix/net/src/gethostname.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TRAILING_WHITESPACE

tests/posix/net/src/gethostname.c:63 trailing whitespace
/* Verify it can hold at least the configured hostname */
zassert_true(CONFIG_NET_HOSTNAME_MAX_LEN >= strlen(CONFIG_NET_HOSTNAME),
"CONFIG_NET_HOSTNAME_MAX_LEN too small for CONFIG_NET_HOSTNAME");

#ifdef CONFIG_POSIX_HOST_NAME_MAX
/* If POSIX is enabled, verify consistency */
zassert_true(CONFIG_POSIX_HOST_NAME_MAX >= CONFIG_NET_HOSTNAME_MAX_LEN,
"POSIX_HOST_NAME_MAX should be >= NET_HOSTNAME_MAX_LEN");
#endif
}

#ifdef CONFIG_NET_HOSTNAME_DYNAMIC
ZTEST(posix_net, test_gethostname_dynamic_update)
{
char hostname[CONFIG_NET_HOSTNAME_MAX_LEN + 1];
char original_hostname[CONFIG_NET_HOSTNAME_MAX_LEN + 1];
char test_hostname[] = "test-dynamic";
int ret;

/* Get original hostname */
ret = gethostname(original_hostname, sizeof(original_hostname));
zassert_equal(ret, 0, "Failed to get original hostname");

/* Set new hostname */
ret = net_hostname_set(test_hostname, strlen(test_hostname));
zassert_equal(ret, 0, "Failed to set hostname");

/* Verify hostname changed */
ret = gethostname(hostname, sizeof(hostname));
zassert_equal(ret, 0, "Failed to get hostname after update");
zassert_equal(strcmp(hostname, test_hostname), 0,
"Hostname not updated correctly");

/* Restore original hostname */
ret = net_hostname_set(original_hostname, strlen(original_hostname));
zassert_equal(ret, 0, "Failed to restore original hostname");
}
#endif /* CONFIG_NET_HOSTNAME_DYNAMIC */

#ifdef CONFIG_NET_HOSTNAME_UNIQUE_UPDATE
ZTEST(net, test_gethostname_with_unique_update)
{
char hostname[CONFIG_NET_HOSTNAME_MAX_LEN + 1];
int ret;

/* This test validates that gethostname works when NET_HOSTNAME_UNIQUE_UPDATE is enabled */
ret = gethostname(hostname, sizeof(hostname));
zassert_equal(ret, 0, "gethostname() failed with NET_HOSTNAME_UNIQUE_UPDATE enabled");

/* Hostname should contain the configured name possibly with unique suffix */
zassert_true(strlen(hostname) > 0, "Hostname should not be empty");
zassert_true(strlen(hostname) <= CONFIG_NET_HOSTNAME_MAX_LEN,
"Hostname length exceeds maximum");
}
#endif /* CONFIG_NET_HOSTNAME_UNIQUE_UPDATE */
19 changes: 19 additions & 0 deletions tests/posix/net/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,22 @@ tests:
filter: CONFIG_PICOLIBC_SUPPORTED
extra_configs:
- CONFIG_PICOLIBC=y
portability.posix.net.hostname.dynamic:
extra_configs:
- CONFIG_NET_HOSTNAME_ENABLE=y
- CONFIG_NET_HOSTNAME_DYNAMIC=y
portability.posix.net.hostname.unique:
extra_configs:
- CONFIG_NET_HOSTNAME_ENABLE=y
- CONFIG_NET_HOSTNAME_UNIQUE=y
- CONFIG_NET_HOSTNAME_UNIQUE_UPDATE=y
portability.posix.net.hostname.dynamic.minimal_libc:
extra_configs:
- CONFIG_MINIMAL_LIBC=y
- CONFIG_NET_HOSTNAME_ENABLE=y
- CONFIG_NET_HOSTNAME_DYNAMIC=y
portability.posix.net.hostname.unique_update:
extra_configs:
- CONFIG_POSIX_API=y
- CONFIG_NET_HOSTNAME_UNIQUE_UPDATE=y
- CONFIG_NET_HOSTNAME_UNIQUE=y
Loading