Skip to content

Commit 4edb901

Browse files
committed
posix: net: add kconfig option and inet_addr()
Add a Kconfig option for POSIX_NETWORKING and implement inet_addr(). Signed-off-by: Chris Friedt <chrisfriedt@gmail.com>
1 parent 22d51c1 commit 4edb901

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

‎include/zephyr/posix/arpa/inet.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ static inline int inet_pton(sa_family_t family, const char *src, void *dst)
3232

3333
#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */
3434

35+
in_addr_t inet_addr(const char *cp);
36+
char *inet_ntoa(struct in_addr in);
37+
3538
#ifdef __cplusplus
3639
}
3740
#endif

‎lib/posix/options/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_CONFSTR confstr.c)
4444
zephyr_library_sources_ifdef(CONFIG_POSIX_ENV env.c)
4545
zephyr_library_sources_ifdef(CONFIG_POSIX_FS fs.c)
4646
zephyr_library_sources_ifdef(CONFIG_POSIX_MQUEUE mqueue.c)
47+
zephyr_library_sources_ifdef(CONFIG_POSIX_NETWORKING net.c)
4748
zephyr_library_sources_ifdef(CONFIG_POSIX_PUTMSG stropts.c)
4849
zephyr_library_sources_ifdef(CONFIG_POSIX_SIGNAL signal.c ${STRSIGNAL_TABLE_H})
4950
zephyr_library_sources_ifdef(CONFIG_POSIX_SYSCONF_IMPL_FULL sysconf.c)

‎lib/posix/options/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ rsource "Kconfig.getopt"
3737
rsource "Kconfig.key"
3838
rsource "Kconfig.mqueue"
3939
rsource "Kconfig.mutex"
40+
rsource "Kconfig.net"
4041
rsource "Kconfig.pthread"
4142
rsource "Kconfig.rwlock"
4243
rsource "Kconfig.sched"

‎lib/posix/options/Kconfig.net‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2024, Friedt Professional Engineering Services, Inc
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
config POSIX_NETWORKING
6+
bool "POSIX Networking API"
7+
default y if POSIX_API
8+
depends on NETWORKING
9+
help
10+
Enable this option to support the POSIX networking API. This includes
11+
support for BSD Sockets.

‎lib/posix/options/net.c‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2024, Friedt Professional Engineering Services, Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <ctype.h>
8+
9+
#include <zephyr/posix/arpa/inet.h>
10+
#include <zephyr/posix/netinet/in.h>
11+
12+
in_addr_t inet_addr(const char *cp)
13+
{
14+
int val = 0;
15+
int len = 0;
16+
int dots = 0;
17+
int digits = 0;
18+
19+
/* error checking */
20+
if (cp == NULL) {
21+
return -1;
22+
}
23+
24+
for (int i = 0, subdigits = 0; i <= INET_ADDRSTRLEN; ++i, ++len) {
25+
if (subdigits > 3) {
26+
return -1;
27+
}
28+
if (cp[i] == '\0') {
29+
break;
30+
} else if (cp[i] == '.') {
31+
if (subdigits == 0) {
32+
return -1;
33+
}
34+
++dots;
35+
subdigits = 0;
36+
continue;
37+
} else if (isdigit((int)cp[i])) {
38+
++digits;
39+
++subdigits;
40+
continue;
41+
} else if (isspace((int)cp[i])) {
42+
break;
43+
}
44+
45+
return -1;
46+
}
47+
48+
if (dots != 3 || digits < 4) {
49+
return -1;
50+
}
51+
52+
/* conversion */
53+
for (int i = 0, tmp = 0; i < len; ++i, ++cp) {
54+
if (*cp != '.') {
55+
tmp *= 10;
56+
tmp += *cp - '0';
57+
}
58+
59+
if (*cp == '.' || i == len - 1) {
60+
val <<= 8;
61+
val |= tmp;
62+
tmp = 0;
63+
}
64+
}
65+
66+
return htonl(val);
67+
}

0 commit comments

Comments
 (0)