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>
This commit is contained in:
parent
22d51c11b0
commit
4edb9017c2
|
@ -32,6 +32,9 @@ static inline int inet_pton(sa_family_t family, const char *src, void *dst)
|
|||
|
||||
#endif /* CONFIG_NET_SOCKETS_POSIX_NAMES */
|
||||
|
||||
in_addr_t inet_addr(const char *cp);
|
||||
char *inet_ntoa(struct in_addr in);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -44,6 +44,7 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_CONFSTR confstr.c)
|
|||
zephyr_library_sources_ifdef(CONFIG_POSIX_ENV env.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POSIX_FS fs.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POSIX_MQUEUE mqueue.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POSIX_NETWORKING net.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POSIX_PUTMSG stropts.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_POSIX_SIGNAL signal.c ${STRSIGNAL_TABLE_H})
|
||||
zephyr_library_sources_ifdef(CONFIG_POSIX_SYSCONF_IMPL_FULL sysconf.c)
|
||||
|
|
|
@ -37,6 +37,7 @@ rsource "Kconfig.getopt"
|
|||
rsource "Kconfig.key"
|
||||
rsource "Kconfig.mqueue"
|
||||
rsource "Kconfig.mutex"
|
||||
rsource "Kconfig.net"
|
||||
rsource "Kconfig.pthread"
|
||||
rsource "Kconfig.rwlock"
|
||||
rsource "Kconfig.sched"
|
||||
|
|
11
lib/posix/options/Kconfig.net
Normal file
11
lib/posix/options/Kconfig.net
Normal file
|
@ -0,0 +1,11 @@
|
|||
# Copyright (c) 2024, Friedt Professional Engineering Services, Inc
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config POSIX_NETWORKING
|
||||
bool "POSIX Networking API"
|
||||
default y if POSIX_API
|
||||
depends on NETWORKING
|
||||
help
|
||||
Enable this option to support the POSIX networking API. This includes
|
||||
support for BSD Sockets.
|
67
lib/posix/options/net.c
Normal file
67
lib/posix/options/net.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Friedt Professional Engineering Services, Inc
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
#include <zephyr/posix/arpa/inet.h>
|
||||
#include <zephyr/posix/netinet/in.h>
|
||||
|
||||
in_addr_t inet_addr(const char *cp)
|
||||
{
|
||||
int val = 0;
|
||||
int len = 0;
|
||||
int dots = 0;
|
||||
int digits = 0;
|
||||
|
||||
/* error checking */
|
||||
if (cp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0, subdigits = 0; i <= INET_ADDRSTRLEN; ++i, ++len) {
|
||||
if (subdigits > 3) {
|
||||
return -1;
|
||||
}
|
||||
if (cp[i] == '\0') {
|
||||
break;
|
||||
} else if (cp[i] == '.') {
|
||||
if (subdigits == 0) {
|
||||
return -1;
|
||||
}
|
||||
++dots;
|
||||
subdigits = 0;
|
||||
continue;
|
||||
} else if (isdigit((int)cp[i])) {
|
||||
++digits;
|
||||
++subdigits;
|
||||
continue;
|
||||
} else if (isspace((int)cp[i])) {
|
||||
break;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dots != 3 || digits < 4) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* conversion */
|
||||
for (int i = 0, tmp = 0; i < len; ++i, ++cp) {
|
||||
if (*cp != '.') {
|
||||
tmp *= 10;
|
||||
tmp += *cp - '0';
|
||||
}
|
||||
|
||||
if (*cp == '.' || i == len - 1) {
|
||||
val <<= 8;
|
||||
val |= tmp;
|
||||
tmp = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return htonl(val);
|
||||
}
|
Loading…
Reference in a new issue