From 98bf69cf8925e56b442d53b2d39860b258debdd5 Mon Sep 17 00:00:00 2001 From: lucas Date: Wed, 17 Apr 2024 16:15:46 +0200 Subject: [PATCH] Try to compile with WAMR_BUILD_LIBC_WASI --- .../src/blocking_op.c | 4 +- .../src/blocking_op.h | 2 +- .../sandboxed-system-primitives/src/locking.h | 12 +- .../sandboxed-system-primitives/src/posix.c | 14 +- .../sandboxed-system-primitives/src/random.c | 12 + .../src/ssp_config.h | 7 +- .../platform/include/platform_api_extension.h | 8 + .../platform/zephyr/platform_internal.h | 34 ++- .../platform/zephyr/shared_platform.cmake | 21 +- core/shared/platform/zephyr/zephyr_file.c | 223 ++++++++++++++++++ core/shared/platform/zephyr/zephyr_socket.c | 96 +++++--- 11 files changed, 376 insertions(+), 57 deletions(-) create mode 100644 core/shared/platform/zephyr/zephyr_file.c diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c index 4dcc4f5b0..9a2befffa 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c @@ -175,14 +175,14 @@ blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle, #ifndef BH_PLATFORM_WINDOWS /* REVISIT: apply the os_file_handle style abstraction for pollfd? */ __wasi_errno_t -blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds, +blocking_op_poll(wasm_exec_env_t exec_env, os_poll_file_handle *pfds, os_nfds_t nfds, int timeout_ms, int *retp) { int ret; if (!wasm_runtime_begin_blocking_op(exec_env)) { return __WASI_EINTR; } - ret = poll(pfds, nfds, timeout_ms); + ret = os_poll(pfds, nfds, timeout_ms); wasm_runtime_end_blocking_op(exec_env); if (ret == -1) { return convert_errno(errno); diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h index 9c36d7df6..f00719113 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h @@ -54,6 +54,6 @@ blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle, #ifndef BH_PLATFORM_WINDOWS __wasi_errno_t -blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds, +blocking_op_poll(wasm_exec_env_t exec_env, os_poll_file_handle *pfds, os_nfds_t nfds, int timeout, int *retp); #endif diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h index 0ad40ecfd..bda554d42 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h @@ -128,7 +128,7 @@ struct LOCKABLE cond { clockid_t clock; #endif }; - +#if !defined(BH_PLATFORM_ZEPHYR) static inline bool cond_init_monotonic(struct cond *cond) { @@ -177,7 +177,7 @@ cond_init_realtime(struct cond *cond) return true; } - +#endif /* !defined(BH_PLATFORM_ZEPHYR) */ static inline void cond_destroy(struct cond *cond) { @@ -197,7 +197,7 @@ cond_timedwait(struct cond *cond, struct mutex *lock, uint64_t timeout, bool abstime) REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS { int ret; - struct timespec ts = { + struct os_timespec ts = { .tv_sec = (time_t)(timeout / 1000000000), .tv_nsec = (long)(timeout % 1000000000), }; @@ -210,8 +210,8 @@ cond_timedwait(struct cond *cond, struct mutex *lock, uint64_t timeout, * realtime clock. */ if (cond->clock != CLOCK_REALTIME) { - struct timespec ts_monotonic; - struct timespec ts_realtime; + struct os_timespec ts_monotonic; + struct os_timespec ts_realtime; clock_gettime(cond->clock, &ts_monotonic); ts.tv_sec -= ts_monotonic.tv_sec; @@ -241,7 +241,7 @@ cond_timedwait(struct cond *cond, struct mutex *lock, uint64_t timeout, return ret == ETIMEDOUT; #else /* Convert to absolute timeout. */ - struct timespec ts_now; + struct os_timespec ts_now; #if CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK clock_gettime(cond->clock, &ts_now); #else diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c index 03ff50a3f..ef0743dee 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c @@ -2057,6 +2057,8 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, { #ifdef BH_PLATFORM_WINDOWS return __WASI_ENOSYS; +#elif BH_PLATEFORM_ZEPHYR + return __WASI_ENOSYS; #else // Sleeping. if (nsubscriptions == 1 && in[0].u.type == __WASI_EVENTTYPE_CLOCK) { @@ -2147,7 +2149,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, wasm_runtime_malloc((uint32)(nsubscriptions * sizeof(*fos))); if (fos == NULL) return __WASI_ENOMEM; - struct pollfd *pfds = + struct os_poll_file_handle *pfds = wasm_runtime_malloc((uint32)(nsubscriptions * sizeof(*pfds))); if (pfds == NULL) { wasm_runtime_free(fos); @@ -2172,7 +2174,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, __WASI_RIGHT_POLL_FD_READWRITE, 0); if (error == 0) { // Proper file descriptor on which we can poll(). - pfds[i] = (struct pollfd){ + pfds[i] = (struct os_poll_file_handle){ .fd = fos[i]->file_handle, .events = s->u.type == __WASI_EVENTTYPE_FD_READ ? POLLIN @@ -2182,7 +2184,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, else { // Invalid file descriptor or rights missing. fos[i] = NULL; - pfds[i] = (struct pollfd){ .fd = -1 }; + pfds[i] = (struct os_poll_file_handle){ .fd = -1 }; out[(*nevents)++] = (__wasi_event_t){ .userdata = s->userdata, .error = error, @@ -2197,7 +2199,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, == 0) { // Relative timeout. fos[i] = NULL; - pfds[i] = (struct pollfd){ .fd = -1 }; + pfds[i] = (struct os_poll_file_handle){ .fd = -1 }; clock_subscription = s; break; } @@ -2205,7 +2207,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, default: // Unsupported event. fos[i] = NULL; - pfds[i] = (struct pollfd){ .fd = -1 }; + pfds[i] = (struct os_poll_file_handle){ .fd = -1 }; out[(*nevents)++] = (__wasi_event_t){ .userdata = s->userdata, .error = __WASI_ENOSYS, @@ -2249,7 +2251,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, __wasi_filesize_t nbytes = 0; if (in[i].u.type == __WASI_EVENTTYPE_FD_READ) { int l; - if (ioctl(fos[i]->file_handle, FIONREAD, &l) == 0) + if (os_ioctl(fos[i]->file_handle, FIONREAD, &l) == 0) nbytes = (__wasi_filesize_t)l; } if ((pfds[i].revents & POLLNVAL) != 0) { diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/random.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/random.c index 29c50dd87..a74f6e6de 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/random.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/random.c @@ -66,6 +66,18 @@ random_buf(void *buf, size_t len) return ret ? __WASI_EINVAL : __WASI_ESUCCESS; } +#elif defined(BH_PLATFORM_ZEPHYR) +static void +open_urandom(void) +{ + // Not implemented +} +__wasi_errno_t +random_buf(void *buf, size_t len) +{ + return __WASI_ENOSYS; +} + #else static int urandom = -1; diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h index 298dea674..9247ace3d 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h @@ -34,7 +34,8 @@ #define CONFIG_HAS_GETRANDOM 0 #endif -#if defined(__CloudABI__) || defined(BH_PLATFORM_FREERTOS) +#if defined(__CloudABI__) || defined(BH_PLATFORM_FREERTOS) \ + || defined(BH_PLATFORM_ZEPHYR) #define CONFIG_HAS_CAP_ENTER 1 #else #define CONFIG_HAS_CAP_ENTER 0 @@ -42,7 +43,7 @@ #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__EMSCRIPTEN__) \ && !defined(ESP_PLATFORM) && !defined(DISABLE_CLOCK_NANOSLEEP) \ - && !defined(BH_PLATFORM_FREERTOS) + && !defined(BH_PLATFORM_FREERTOS) && !defined(BH_PLATFORM_ZEPHYR) #define CONFIG_HAS_CLOCK_NANOSLEEP 1 #else #define CONFIG_HAS_CLOCK_NANOSLEEP 0 @@ -55,7 +56,7 @@ #endif #if !defined(__APPLE__) && !defined(BH_PLATFORM_LINUX_SGX) && !defined(_WIN32) \ - && !defined(__COSMOPOLITAN__) && !defined(BH_PLATFORM_FREERTOS) + && !defined(__COSMOPOLITAN__) && !defined(BH_PLATFORM_FREERTOS) && !defined(BH_PLATFORM_ZEPHYR) #define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 1 #else #define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 0 diff --git a/core/shared/platform/include/platform_api_extension.h b/core/shared/platform/include/platform_api_extension.h index 7c6120ba2..0f0daf584 100644 --- a/core/shared/platform/include/platform_api_extension.h +++ b/core/shared/platform/include/platform_api_extension.h @@ -1636,4 +1636,12 @@ os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision, } #endif + +/* Expermimental :*/ +__wasi_errno_t +os_ioctl(os_file_handle *handle, int request, void *argp); + +__wasi_errno_t +os_poll(os_poll_file_handle *pfds, os_nfds_t nfs, int timeout); + #endif /* #ifndef PLATFORM_API_EXTENSION_H */ diff --git a/core/shared/platform/zephyr/platform_internal.h b/core/shared/platform/zephyr/platform_internal.h index 3c0f55266..08836c28a 100644 --- a/core/shared/platform/zephyr/platform_internal.h +++ b/core/shared/platform/zephyr/platform_internal.h @@ -48,6 +48,7 @@ #include #include #include +#include #endif /* end of KERNEL_VERSION_NUMBER < 0x030200 */ #if KERNEL_VERSION_NUMBER >= 0x030300 /* version 3.3.0 */ @@ -80,10 +81,6 @@ typedef unsigned int korp_sem; /* korp_rwlock is used in platform_api_extension.h, we just define the type to make the compiler happy */ -typedef struct { - int dummy; -} korp_rwlock; - struct os_thread_wait_node; typedef struct os_thread_wait_node *os_thread_wait_list; typedef struct korp_cond { @@ -169,6 +166,35 @@ typedef int os_file_handle; typedef void *os_dir_stream; typedef int os_raw_file_handle; +/*********************************************************/ +// try to stub POSIX implementation in sandboxed env. +typedef struct zsock_pollfd os_poll_file_handle; +typedef unsigned int os_nfds_t; + +#define POLLIN ZSOCK_POLLIN +#define POLLPRI ZSOCK_POLLPRI +#define POLLOUT ZSOCK_POLLOUT +#define POLLERR ZSOCK_POLLERR +#define POLLHUP ZSOCK_POLLHUP +#define POLLNVAL ZSOCK_POLLNVAL + +#define FIONREAD ZFD_IOCTL_FIONREAD + +typedef struct { + time_t tv_sec; + long tv_nsec; +} os_timespec; + +#define CLOCK_REALTIME 1 +#define CLOCK_MONOTONIC 4 + +typedef struct { + struct k_mutex mtx; // Mutex for exclusive access + struct k_sem sem; // Semaphore for shared access + int read_count; // Number of readers +} korp_rwlock; +/*********************************************************/ + static inline os_file_handle os_get_invalid_handle() { diff --git a/core/shared/platform/zephyr/shared_platform.cmake b/core/shared/platform/zephyr/shared_platform.cmake index dfd45a406..b62abc840 100644 --- a/core/shared/platform/zephyr/shared_platform.cmake +++ b/core/shared/platform/zephyr/shared_platform.cmake @@ -8,11 +8,22 @@ add_definitions(-DBH_PLATFORM_ZEPHYR) include_directories(${PLATFORM_SHARED_DIR}) include_directories(${PLATFORM_SHARED_DIR}/../include) -if(${CONFIG_MINIMAL_LIBC}) - include (${CMAKE_CURRENT_LIST_DIR}/../common/math/platform_api_math.cmake) -endif() - file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c) -set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_MATH_SOURCE}) +if(${CONFIG_MINIMAL_LIBC}) + include (${CMAKE_CURRENT_LIST_DIR}/../common/math/platform_api_math.cmake) + set (source_all ${source_all} ${PLATFORM_COMMON_MATH_SOURCE}) +endif() +if (NOT WAMR_BUILD_LIBC_WASI EQUAL 1) + list(REMOVE_ITEM source_all ${PLATFORM_SHARED_DIR}/zephyr_socket.c) + list(REMOVE_ITEM source_all ${PLATFORM_SHARED_DIR}/zephyr_file.c) +else() + include (${CMAKE_CURRENT_LIST_DIR}/../common/libc-util/platform_common_libc_util.cmake) + set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE}) +endif () + +include (${CMAKE_CURRENT_LIST_DIR}/../common/memory/platform_api_memory.cmake) +set (source_all ${source_all} ${PLATFORM_COMMON_MEMORY_SOURCE}) + +set (PLATFORM_SHARED_SOURCE ${source_all}) diff --git a/core/shared/platform/zephyr/zephyr_file.c b/core/shared/platform/zephyr/zephyr_file.c new file mode 100644 index 000000000..7dc60f198 --- /dev/null +++ b/core/shared/platform/zephyr/zephyr_file.c @@ -0,0 +1,223 @@ +#include "platform_api_extension.h" +#include "libc_errno.h" +#include "zephyr_errno.h" + +__wasi_errno_t +os_fstat(os_file_handle handle, struct __wasi_filestat_t *buf){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_fstatat(os_file_handle handle, const char *path, + struct __wasi_filestat_t *buf, __wasi_lookupflags_t lookup_flags){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_file_get_fdflags(os_file_handle handle, __wasi_fdflags_t *flags){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_file_set_fdflags(os_file_handle handle, __wasi_fdflags_t flags){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_fdatasync(os_file_handle handle){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_fsync(os_file_handle handle){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_open_preopendir(const char *path, os_file_handle *out){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_openat(os_file_handle handle, const char *path, __wasi_oflags_t oflags, + __wasi_fdflags_t fd_flags, __wasi_lookupflags_t lookup_flags, + wasi_libc_file_access_mode access_mode, os_file_handle *out){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_file_get_access_mode(os_file_handle handle, + wasi_libc_file_access_mode *access_mode){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_close(os_file_handle handle, bool is_stdio){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_preadv(os_file_handle handle, const struct __wasi_iovec_t *iov, int iovcnt, + __wasi_filesize_t offset, size_t *nread){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_pwritev(os_file_handle handle, const struct __wasi_ciovec_t *iov, int iovcnt, + __wasi_filesize_t offset, size_t *nwritten){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_readv(os_file_handle handle, const struct __wasi_iovec_t *iov, int iovcnt, + size_t *nread){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_writev(os_file_handle handle, const struct __wasi_ciovec_t *iov, int iovcnt, + size_t *nwritten){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_fallocate(os_file_handle handle, __wasi_filesize_t offset, + __wasi_filesize_t length){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_ftruncate(os_file_handle handle, __wasi_filesize_t size){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_futimens(os_file_handle handle, __wasi_timestamp_t access_time, + __wasi_timestamp_t modification_time, __wasi_fstflags_t fstflags){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_utimensat(os_file_handle handle, const char *path, + __wasi_timestamp_t access_time, + __wasi_timestamp_t modification_time, __wasi_fstflags_t fstflags, + __wasi_lookupflags_t lookup_flags){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_readlinkat(os_file_handle handle, const char *path, char *buf, + size_t bufsize, size_t *nread){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_linkat(os_file_handle from_handle, const char *from_path, + os_file_handle to_handle, const char *to_path, + __wasi_lookupflags_t lookup_flags){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_symlinkat(const char *old_path, os_file_handle handle, const char *new_path){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_mkdirat(os_file_handle handle, const char *path){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_renameat(os_file_handle old_handle, const char *old_path, + os_file_handle new_handle, const char *new_path){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_unlinkat(os_file_handle handle, const char *path, bool is_dir){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_lseek(os_file_handle handle, __wasi_filedelta_t offset, + __wasi_whence_t whence, __wasi_filesize_t *new_offset){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_fadvise(os_file_handle handle, __wasi_filesize_t offset, + __wasi_filesize_t length, __wasi_advice_t advice){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_isatty(os_file_handle handle){ + return __WASI_ENOSYS; +} + +os_file_handle +os_convert_stdin_handle(os_raw_file_handle raw_stdin){ + return NULL; +} + +os_file_handle +os_convert_stdout_handle(os_raw_file_handle raw_stdout){ + return NULL; +} + +os_file_handle +os_convert_stderr_handle(os_raw_file_handle raw_stderr){ + return NULL; +} + +__wasi_errno_t +os_fdopendir(os_file_handle handle, os_dir_stream *dir_stream){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_rewinddir(os_dir_stream dir_stream){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_seekdir(os_dir_stream dir_stream, __wasi_dircookie_t position){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_readdir(os_dir_stream dir_stream, __wasi_dirent_t *entry, + const char **d_name){ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_closedir(os_dir_stream dir_stream){ + return __WASI_ENOSYS; +} + +os_dir_stream +os_get_invalid_dir_stream(){ + return NULL; +} + +bool +os_is_dir_stream_valid(os_dir_stream *dir_stream){ + return false; +} + +// os_file_handle +// os_get_invalid_handle(){ +// return NULL; +// } + +bool +os_is_handle_valid(os_file_handle *handle){ + return false; +} + +char * +os_realpath(const char *path, char *resolved_path){ + return NULL; +} \ No newline at end of file diff --git a/core/shared/platform/zephyr/zephyr_socket.c b/core/shared/platform/zephyr/zephyr_socket.c index 2739ecba7..0717532d0 100644 --- a/core/shared/platform/zephyr/zephyr_socket.c +++ b/core/shared/platform/zephyr/zephyr_socket.c @@ -1,10 +1,16 @@ #include "platform_api_extension.h" -//#include "platform_wasi_types.h" +#include "platform_api_vmcore.h" + #include #include #include + +#include +#include + #include "zephyr_errno.h" +#include // Static functions static bool @@ -19,7 +25,7 @@ textual_addr_to_sockaddr(const char *textual, int port, struct sockaddr *out, assert(textual); v4 = (struct sockaddr_in *)out; - if (inet_pton(AF_INET, textual, &v4->sin_addr.s_addr) == 1) { + if (zsock_inet_pton(AF_INET, textual, &v4->sin_addr.s_addr) == 1) { v4->sin_family = AF_INET; v4->sin_port = htons(port); *out_len = sizeof(struct sockaddr_in); @@ -28,7 +34,7 @@ textual_addr_to_sockaddr(const char *textual, int port, struct sockaddr *out, #ifdef IPPROTO_IPV6 v6 = (struct sockaddr_in *)out; - if (inet_pton(AF_INET6, textual, &v6->sin6_addr.s6_addr) == 1) { + if (zsock_inet_pton(AF_INET6, textual, &v6->sin6_addr.s6_addr) == 1) { v6->sin6_family = AF_INET6; v6->sin6_port = htons(port); *out_len = sizeof(struct sockaddr_in); @@ -114,13 +120,13 @@ static int getaddrinfo_error_to_errno(int error) { switch (error) { - case EAI_AGAIN: + case DNS_EAI_AGAIN: return EAGAIN; - case EAI_FAIL: + case DNS_EAI_FAIL: return EFAULT; - case EAI_MEMORY: + case DNS_EAI_MEMORY: return ENOMEM; - case EAI_SYSTEM: + case DNS_EAI_SYSTEM: return errno; default: return EINVAL; @@ -128,7 +134,7 @@ getaddrinfo_error_to_errno(int error) } static int -is_addrinfo_supported(struct addrinfo *info) +is_addrinfo_supported(struct zsock_addrinfo *info) { return // Allow only IPv4 and IPv6 @@ -161,7 +167,7 @@ os_socket_getbooloption(bh_socket_t socket, int level, int optname, int optval; socklen_t optval_size = sizeof(optval); - if (zsock_setsockopt(socket, level, optname, &optval, &optval_size) != 0) { + if (zsock_setsockopt(socket, level, optname, &optval, optval_size) != 0) { return BHT_ERROR; } *is_enabled = (bool)optval; @@ -216,12 +222,12 @@ os_socket_bind(bh_socket_t socket, const char *host, int *port) goto fail; } - if (addr.sin_family == AF_INET) { - *port = ntohs((&addr)->sin_port); + if (addr.ss_family == AF_INET) { // addr.sin_family + *port = ntohs(((struct sockaddr_in *)&addr)->sin_port); } else { #ifdef IPPROTO_IPV6 - *port = ntohs((&addr)->sin6_port); + *port = ntohs(((struct sockaddr_in *)&addr)->sin6_port); #else goto fail; #endif @@ -270,18 +276,18 @@ os_socket_accept(bh_socket_t server_sock, bh_socket_t *sock, void *addr, int os_socket_connect(bh_socket_t socket, const char *addr, int port) { - struct sockaddr_storage addr = { 0 }; + struct sockaddr_storage addr_in = { 0 }; socklen_t socklen; int ret; assert(addr); - if (!textual_addr_to_sockaddr(addr, port, (struct sockaddr *)&addr, + if (!textual_addr_to_sockaddr(addr, port, (struct sockaddr *)&addr_in, &socklen)) { return BHT_ERROR; } - ret = zsock_connect(socket, (struct sockaddr *)&addr, socklen); + ret = zsock_connect(socket, (struct sockaddr *)&addr_in, socklen); if (ret < 0) { return BHT_ERROR; } @@ -331,12 +337,8 @@ os_socket_send_to(bh_socket_t socket, const void *buf, unsigned int len, { struct sockaddr_storage addr = { 0 }; socklen_t socklen; - int ret; - if (bh_sockaddr_to_sockaddr(dest_addr, (struct sockaddr *)&addr, &socklen) - == BHT_ERROR) { - return -1; - } + (void)bh_sockaddr_to_sockaddr(dest_addr, (struct sockaddr *)&addr, &socklen); return zsock_sendto(socket, buf, len, flags, (struct sockaddr *)&addr, socklen); @@ -543,14 +545,14 @@ int os_socket_set_keep_alive(bh_socket_t socket, bool is_enabled) { return os_socket_setbooloption(socket, SOL_SOCKET, SO_KEEPALIVE, - is_enabled) + is_enabled); } int os_socket_get_keep_alive(bh_socket_t socket, bool *is_enabled) { return os_socket_getbooloption(socket, SOL_SOCKET, SO_KEEPALIVE, - is_enabled) + is_enabled); } int @@ -720,7 +722,7 @@ os_socket_get_tcp_keep_idle(bh_socket_t socket, uint32_t *time_s) socklen_t time_s_len = sizeof(time_s_int); #ifdef TCP_KEEPIDLE - if (getsozsock_setsockoptkopt(socket, IPPROTO_TCP, TCP_KEEPIDLE, &time_s_int, &time_s_len) + if (zsock_setsockopt(socket, IPPROTO_TCP, TCP_KEEPIDLE, &time_s_int, &time_s_len) != 0) { return BHT_ERROR; } @@ -847,11 +849,11 @@ os_socket_set_ip_add_membership(bh_socket_t socket, #endif } else{ - struct ip_mreq mreq; + struct ip_mreqn mreq; mreq.imr_multiaddr.s_addr = imr_multiaddr->ipv4; - mreq.imr_interface.s_addr = imr_interface; + mreq.imr_address.s_addr = imr_interface; - if (setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, + if (zsock_setsockopt(socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) { return BHT_ERROR; @@ -878,7 +880,7 @@ os_socket_set_ip_drop_membership(bh_socket_t socket, } mreq.ipv6mr_interface = imr_interface; - if (setsockopt(socket, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq, + if (zsock_setsockopt(socket, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) { return BHT_ERROR; @@ -889,11 +891,11 @@ os_socket_set_ip_drop_membership(bh_socket_t socket, #endif } else { - struct ip_mreq mreq; + struct ip_mreqn mreq; mreq.imr_multiaddr.s_addr = imr_multiaddr->ipv4; - mreq.imr_interface.s_addr = imr_interface; + mreq.imr_address.s_addr = imr_interface; - if (setsockopt(socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, + if (zsock_setsockopt(socket, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) { return BHT_ERROR; @@ -990,3 +992,37 @@ os_socket_get_broadcast(bh_socket_t socket, bool *is_enabled) is_enabled); } +// Experimental : +__wasi_errno_t +os_ioctl(os_file_handle *handle, int request, void *argp) +{ + __wasi_errno_t wasi_errno = __WASI_ESUCCESS; + + if(zsock_ioctl_wrapper(handle, request, argp) < 0){ + wasi_errno = zephyr_to_wasi_errno(errno); + } + return wasi_errno; +} + +__wasi_errno_t +os_poll(os_poll_file_handle *fds, os_nfds_t nfs, int timeout) +{ + __wasi_errno_t wasi_errno = __WASI_ESUCCESS; + int rc = 0; + + rc = zsock_poll(fds, nfs, timeout) + if(rc < 0){ + wasi_errno = zephyr_to_wasi_errno(errno); + } + switch(rc){ + case 0: + wasi_errno = __WASI_ETIMEOUT; + break; + case -1: + wasi_errno = zephyr_to_wasi_errno(errno); + break; + default: + break; + } + return wasi_errno; +} \ No newline at end of file