diff --git a/include/zephyr/posix/arpa/inet.h b/include/zephyr/posix/arpa/inet.h index 244b98b24f..e4e6934020 100644 --- a/include/zephyr/posix/arpa/inet.h +++ b/include/zephyr/posix/arpa/inet.h @@ -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 diff --git a/lib/posix/options/CMakeLists.txt b/lib/posix/options/CMakeLists.txt index eccaded728..d7f3ef94e5 100644 --- a/lib/posix/options/CMakeLists.txt +++ b/lib/posix/options/CMakeLists.txt @@ -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) diff --git a/lib/posix/options/Kconfig b/lib/posix/options/Kconfig index 496a7697d1..7b67244d12 100644 --- a/lib/posix/options/Kconfig +++ b/lib/posix/options/Kconfig @@ -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" diff --git a/lib/posix/options/Kconfig.net b/lib/posix/options/Kconfig.net new file mode 100644 index 0000000000..5e2b363e16 --- /dev/null +++ b/lib/posix/options/Kconfig.net @@ -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. diff --git a/lib/posix/options/net.c b/lib/posix/options/net.c new file mode 100644 index 0000000000..735254f06b --- /dev/null +++ b/lib/posix/options/net.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2024, Friedt Professional Engineering Services, Inc + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include + +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); +}