Skip to content

Commit 72c0a13

Browse files
net: posix: fix hostname config conflict with unique updates
Decouple NET_HOSTNAME_MAX_LEN from NET_HOSTNAME_DYNAMIC to resolve Kconfig conflict when using POSIX networking with unique hostname updates. This restores functionality that worked in Zephyr 3.6. - Remove forced selection of NET_HOSTNAME_DYNAMIC from POSIX_NETWORKING - Make NET_HOSTNAME_MAX_LEN available independently - Add build assertions for hostname length validation - Update test configurations for new dependency structure Fixes #95811 Signed-off-by: Pragati Garg <[email protected]> Signed-off-by: PragatiGarg-eaton <[email protected]>
1 parent 91b1b84 commit 72c0a13

File tree

6 files changed

+127
-10
lines changed

6 files changed

+127
-10
lines changed

lib/posix/options/Kconfig.net

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ menuconfig POSIX_NETWORKING
66
bool "POSIX Networking API"
77
depends on NETWORKING
88
select NET_HOSTNAME_ENABLE
9-
select NET_HOSTNAME_DYNAMIC
109
select NET_INTERFACE_NAME
1110
select NET_SOCKETPAIR
1211
select NET_SOCKETS

subsys/net/Kconfig.hostname

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ config NET_HOSTNAME_ENABLE
99
This is used for example in mDNS to respond to <hostname>.local
1010
mDNS queries.
1111

12+
config NET_HOSTNAME_MAX_LEN
13+
int "The maximum allowed hostname length"
14+
# This corresponds to "zephyr"
15+
default 6 if !NET_HOSTNAME_ENABLE
16+
default 63
17+
help
18+
This will set the number of bytes allocated for the hostname.
19+
When NET_HOSTNAME_ENABLE is disabled, this defaults to 6 to
20+
accommodate the default "zephyr" hostname.
21+
1222
if NET_HOSTNAME_ENABLE
1323

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

27-
config NET_HOSTNAME_MAX_LEN
28-
int "The maximum allowed hostname length"
29-
depends on NET_HOSTNAME_DYNAMIC
30-
range 1 63
31-
default 63
32-
help
33-
This will set the number of bytes allocateed for the hostname.
34-
3537
config NET_HOSTNAME_UNIQUE
3638
bool "Make hostname unique"
3739
help

subsys/net/hostname.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ LOG_MODULE_REGISTER(net_hostname, CONFIG_NET_HOSTNAME_LOG_LEVEL);
1818
#include <zephyr/net/net_mgmt.h>
1919
#include <zephyr/logging/log_backend_net.h>
2020

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

2324
static void trigger_net_event(void)
2425
{

tests/net/hostname/testcase.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ tests:
2121
extra_configs:
2222
- CONFIG_NET_HOSTNAME_UNIQUE=y
2323
- CONFIG_NET_HOSTNAME_DYNAMIC=y
24+
net.hostname.posix_api.unique_update_compatibility:
25+
extra_configs:
26+
- CONFIG_POSIX_API=y
27+
- CONFIG_NET_HOSTNAME_UNIQUE_UPDATE=y
28+
- CONFIG_NET_HOSTNAME_UNIQUE=y
29+
net.hostname.posix_api.dynamic_hostname:
30+
extra_configs:
31+
- CONFIG_POSIX_API=y
32+
- CONFIG_NET_HOSTNAME_DYNAMIC=y
2433
net.hostname.event:
2534
extra_configs:
2635
- CONFIG_NET_MGMT=y

tests/posix/net/src/gethostname.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
*/
66

77
#include <unistd.h>
8+
#include <errno.h>
9+
#include <string.h>
810

911
#include <zephyr/ztest.h>
12+
#include <zephyr/net/hostname.h>
1013

1114
ZTEST(net, test_gethostname)
1215
{
@@ -19,3 +22,87 @@ ZTEST(net, test_gethostname)
1922
zassert_equal(strcmp(hostname, CONFIG_NET_HOSTNAME), 0,
2023
"gethostname() returned unexpected hostname: %s", hostname);
2124
}
25+
26+
ZTEST(net, test_gethostname_buffer_too_small)
27+
{
28+
char small_hostname[2];
29+
int ret;
30+
31+
ret = gethostname(small_hostname, sizeof(small_hostname));
32+
/* Should fail with buffer too small */
33+
zassert_equal(ret, -1, "gethostname() should fail with small buffer");
34+
zassert_equal(errno, ENAMETOOLONG, "Expected ENAMETOOLONG, got %d", errno);
35+
}
36+
37+
ZTEST(net, test_gethostname_zero_length)
38+
{
39+
char hostname[CONFIG_NET_HOSTNAME_MAX_LEN + 1];
40+
int ret;
41+
42+
ret = gethostname(hostname, 0);
43+
/* Should fail with zero length */
44+
zassert_equal(ret, -1, "gethostname() should fail with zero length");
45+
zassert_equal(errno, EINVAL, "Expected EINVAL, got %d", errno);
46+
}
47+
48+
ZTEST(posix_net, test_hostname_max_len_consistency)
49+
{
50+
/* Verify that CONFIG_NET_HOSTNAME_MAX_LEN is properly defined and > 0 */
51+
zassert_true(CONFIG_NET_HOSTNAME_MAX_LEN > 0,
52+
"CONFIG_NET_HOSTNAME_MAX_LEN must be positive");
53+
54+
/* Verify it can hold at least the configured hostname */
55+
zassert_true(CONFIG_NET_HOSTNAME_MAX_LEN >= strlen(CONFIG_NET_HOSTNAME),
56+
"CONFIG_NET_HOSTNAME_MAX_LEN too small for CONFIG_NET_HOSTNAME");
57+
58+
#ifdef CONFIG_POSIX_HOST_NAME_MAX
59+
/* If POSIX is enabled, verify consistency */
60+
zassert_true(CONFIG_POSIX_HOST_NAME_MAX >= CONFIG_NET_HOSTNAME_MAX_LEN,
61+
"POSIX_HOST_NAME_MAX should be >= NET_HOSTNAME_MAX_LEN");
62+
#endif
63+
}
64+
65+
#ifdef CONFIG_NET_HOSTNAME_DYNAMIC
66+
ZTEST(posix_net, test_gethostname_dynamic_update)
67+
{
68+
char hostname[CONFIG_NET_HOSTNAME_MAX_LEN + 1];
69+
char original_hostname[CONFIG_NET_HOSTNAME_MAX_LEN + 1];
70+
char test_hostname[] = "test-dynamic";
71+
int ret;
72+
73+
/* Get original hostname */
74+
ret = gethostname(original_hostname, sizeof(original_hostname));
75+
zassert_equal(ret, 0, "Failed to get original hostname");
76+
77+
/* Set new hostname */
78+
ret = net_hostname_set(test_hostname, strlen(test_hostname));
79+
zassert_equal(ret, 0, "Failed to set hostname");
80+
81+
/* Verify hostname changed */
82+
ret = gethostname(hostname, sizeof(hostname));
83+
zassert_equal(ret, 0, "Failed to get hostname after update");
84+
zassert_equal(strcmp(hostname, test_hostname), 0,
85+
"Hostname not updated correctly");
86+
87+
/* Restore original hostname */
88+
ret = net_hostname_set(original_hostname, strlen(original_hostname));
89+
zassert_equal(ret, 0, "Failed to restore original hostname");
90+
}
91+
#endif /* CONFIG_NET_HOSTNAME_DYNAMIC */
92+
93+
#ifdef CONFIG_NET_HOSTNAME_UNIQUE_UPDATE
94+
ZTEST(net, test_gethostname_with_unique_update)
95+
{
96+
char hostname[CONFIG_NET_HOSTNAME_MAX_LEN + 1];
97+
int ret;
98+
99+
/* This test validates that gethostname works when NET_HOSTNAME_UNIQUE_UPDATE is enabled */
100+
ret = gethostname(hostname, sizeof(hostname));
101+
zassert_equal(ret, 0, "gethostname() failed with NET_HOSTNAME_UNIQUE_UPDATE enabled");
102+
103+
/* Hostname should contain the configured name possibly with unique suffix */
104+
zassert_true(strlen(hostname) > 0, "Hostname should not be empty");
105+
zassert_true(strlen(hostname) <= CONFIG_NET_HOSTNAME_MAX_LEN,
106+
"Hostname length exceeds maximum");
107+
}
108+
#endif /* CONFIG_NET_HOSTNAME_UNIQUE_UPDATE */

tests/posix/net/testcase.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,22 @@ tests:
3737
filter: CONFIG_PICOLIBC_SUPPORTED
3838
extra_configs:
3939
- CONFIG_PICOLIBC=y
40+
portability.posix.net.hostname.dynamic:
41+
extra_configs:
42+
- CONFIG_NET_HOSTNAME_ENABLE=y
43+
- CONFIG_NET_HOSTNAME_DYNAMIC=y
44+
portability.posix.net.hostname.unique:
45+
extra_configs:
46+
- CONFIG_NET_HOSTNAME_ENABLE=y
47+
- CONFIG_NET_HOSTNAME_UNIQUE=y
48+
- CONFIG_NET_HOSTNAME_UNIQUE_UPDATE=y
49+
portability.posix.net.hostname.dynamic.minimal_libc:
50+
extra_configs:
51+
- CONFIG_MINIMAL_LIBC=y
52+
- CONFIG_NET_HOSTNAME_ENABLE=y
53+
- CONFIG_NET_HOSTNAME_DYNAMIC=y
54+
portability.posix.net.hostname.unique_update:
55+
extra_configs:
56+
- CONFIG_POSIX_API=y
57+
- CONFIG_NET_HOSTNAME_UNIQUE_UPDATE=y
58+
- CONFIG_NET_HOSTNAME_UNIQUE=y

0 commit comments

Comments
 (0)