Try to compile with WAMR_BUILD_LIBC_WASI

This commit is contained in:
lucas 2024-04-17 16:15:46 +02:00
parent cdaab9f05c
commit 98bf69cf89
11 changed files with 376 additions and 57 deletions

View File

@ -175,14 +175,14 @@ blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle,
#ifndef BH_PLATFORM_WINDOWS #ifndef BH_PLATFORM_WINDOWS
/* REVISIT: apply the os_file_handle style abstraction for pollfd? */ /* REVISIT: apply the os_file_handle style abstraction for pollfd? */
__wasi_errno_t __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 timeout_ms, int *retp)
{ {
int ret; int ret;
if (!wasm_runtime_begin_blocking_op(exec_env)) { if (!wasm_runtime_begin_blocking_op(exec_env)) {
return __WASI_EINTR; return __WASI_EINTR;
} }
ret = poll(pfds, nfds, timeout_ms); ret = os_poll(pfds, nfds, timeout_ms);
wasm_runtime_end_blocking_op(exec_env); wasm_runtime_end_blocking_op(exec_env);
if (ret == -1) { if (ret == -1) {
return convert_errno(errno); return convert_errno(errno);

View File

@ -54,6 +54,6 @@ blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle,
#ifndef BH_PLATFORM_WINDOWS #ifndef BH_PLATFORM_WINDOWS
__wasi_errno_t __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); int timeout, int *retp);
#endif #endif

View File

@ -128,7 +128,7 @@ struct LOCKABLE cond {
clockid_t clock; clockid_t clock;
#endif #endif
}; };
#if !defined(BH_PLATFORM_ZEPHYR)
static inline bool static inline bool
cond_init_monotonic(struct cond *cond) cond_init_monotonic(struct cond *cond)
{ {
@ -177,7 +177,7 @@ cond_init_realtime(struct cond *cond)
return true; return true;
} }
#endif /* !defined(BH_PLATFORM_ZEPHYR) */
static inline void static inline void
cond_destroy(struct cond *cond) 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 bool abstime) REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS
{ {
int ret; int ret;
struct timespec ts = { struct os_timespec ts = {
.tv_sec = (time_t)(timeout / 1000000000), .tv_sec = (time_t)(timeout / 1000000000),
.tv_nsec = (long)(timeout % 1000000000), .tv_nsec = (long)(timeout % 1000000000),
}; };
@ -210,8 +210,8 @@ cond_timedwait(struct cond *cond, struct mutex *lock, uint64_t timeout,
* realtime clock. * realtime clock.
*/ */
if (cond->clock != CLOCK_REALTIME) { if (cond->clock != CLOCK_REALTIME) {
struct timespec ts_monotonic; struct os_timespec ts_monotonic;
struct timespec ts_realtime; struct os_timespec ts_realtime;
clock_gettime(cond->clock, &ts_monotonic); clock_gettime(cond->clock, &ts_monotonic);
ts.tv_sec -= ts_monotonic.tv_sec; 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; return ret == ETIMEDOUT;
#else #else
/* Convert to absolute timeout. */ /* Convert to absolute timeout. */
struct timespec ts_now; struct os_timespec ts_now;
#if CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK #if CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK
clock_gettime(cond->clock, &ts_now); clock_gettime(cond->clock, &ts_now);
#else #else

View File

@ -2057,6 +2057,8 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
{ {
#ifdef BH_PLATFORM_WINDOWS #ifdef BH_PLATFORM_WINDOWS
return __WASI_ENOSYS; return __WASI_ENOSYS;
#elif BH_PLATEFORM_ZEPHYR
return __WASI_ENOSYS;
#else #else
// Sleeping. // Sleeping.
if (nsubscriptions == 1 && in[0].u.type == __WASI_EVENTTYPE_CLOCK) { 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))); wasm_runtime_malloc((uint32)(nsubscriptions * sizeof(*fos)));
if (fos == NULL) if (fos == NULL)
return __WASI_ENOMEM; return __WASI_ENOMEM;
struct pollfd *pfds = struct os_poll_file_handle *pfds =
wasm_runtime_malloc((uint32)(nsubscriptions * sizeof(*pfds))); wasm_runtime_malloc((uint32)(nsubscriptions * sizeof(*pfds)));
if (pfds == NULL) { if (pfds == NULL) {
wasm_runtime_free(fos); 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); __WASI_RIGHT_POLL_FD_READWRITE, 0);
if (error == 0) { if (error == 0) {
// Proper file descriptor on which we can poll(). // Proper file descriptor on which we can poll().
pfds[i] = (struct pollfd){ pfds[i] = (struct os_poll_file_handle){
.fd = fos[i]->file_handle, .fd = fos[i]->file_handle,
.events = s->u.type == __WASI_EVENTTYPE_FD_READ .events = s->u.type == __WASI_EVENTTYPE_FD_READ
? POLLIN ? POLLIN
@ -2182,7 +2184,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
else { else {
// Invalid file descriptor or rights missing. // Invalid file descriptor or rights missing.
fos[i] = NULL; fos[i] = NULL;
pfds[i] = (struct pollfd){ .fd = -1 }; pfds[i] = (struct os_poll_file_handle){ .fd = -1 };
out[(*nevents)++] = (__wasi_event_t){ out[(*nevents)++] = (__wasi_event_t){
.userdata = s->userdata, .userdata = s->userdata,
.error = error, .error = error,
@ -2197,7 +2199,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
== 0) { == 0) {
// Relative timeout. // Relative timeout.
fos[i] = NULL; fos[i] = NULL;
pfds[i] = (struct pollfd){ .fd = -1 }; pfds[i] = (struct os_poll_file_handle){ .fd = -1 };
clock_subscription = s; clock_subscription = s;
break; break;
} }
@ -2205,7 +2207,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds,
default: default:
// Unsupported event. // Unsupported event.
fos[i] = NULL; fos[i] = NULL;
pfds[i] = (struct pollfd){ .fd = -1 }; pfds[i] = (struct os_poll_file_handle){ .fd = -1 };
out[(*nevents)++] = (__wasi_event_t){ out[(*nevents)++] = (__wasi_event_t){
.userdata = s->userdata, .userdata = s->userdata,
.error = __WASI_ENOSYS, .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; __wasi_filesize_t nbytes = 0;
if (in[i].u.type == __WASI_EVENTTYPE_FD_READ) { if (in[i].u.type == __WASI_EVENTTYPE_FD_READ) {
int l; 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; nbytes = (__wasi_filesize_t)l;
} }
if ((pfds[i].revents & POLLNVAL) != 0) { if ((pfds[i].revents & POLLNVAL) != 0) {

View File

@ -66,6 +66,18 @@ random_buf(void *buf, size_t len)
return ret ? __WASI_EINVAL : __WASI_ESUCCESS; 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 #else
static int urandom = -1; static int urandom = -1;

View File

@ -34,7 +34,8 @@
#define CONFIG_HAS_GETRANDOM 0 #define CONFIG_HAS_GETRANDOM 0
#endif #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 #define CONFIG_HAS_CAP_ENTER 1
#else #else
#define CONFIG_HAS_CAP_ENTER 0 #define CONFIG_HAS_CAP_ENTER 0
@ -42,7 +43,7 @@
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__EMSCRIPTEN__) \ #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__EMSCRIPTEN__) \
&& !defined(ESP_PLATFORM) && !defined(DISABLE_CLOCK_NANOSLEEP) \ && !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 #define CONFIG_HAS_CLOCK_NANOSLEEP 1
#else #else
#define CONFIG_HAS_CLOCK_NANOSLEEP 0 #define CONFIG_HAS_CLOCK_NANOSLEEP 0
@ -55,7 +56,7 @@
#endif #endif
#if !defined(__APPLE__) && !defined(BH_PLATFORM_LINUX_SGX) && !defined(_WIN32) \ #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 #define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 1
#else #else
#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 0 #define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 0

View File

@ -1636,4 +1636,12 @@ os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision,
} }
#endif #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 */ #endif /* #ifndef PLATFORM_API_EXTENSION_H */

View File

@ -48,6 +48,7 @@
#include <zephyr/net/net_ip.h> #include <zephyr/net/net_ip.h>
#include <zephyr/net/net_core.h> #include <zephyr/net/net_core.h>
#include <zephyr/net/net_context.h> #include <zephyr/net/net_context.h>
#include <zephyr/net/socket.h>
#endif /* end of KERNEL_VERSION_NUMBER < 0x030200 */ #endif /* end of KERNEL_VERSION_NUMBER < 0x030200 */
#if KERNEL_VERSION_NUMBER >= 0x030300 /* version 3.3.0 */ #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, /* korp_rwlock is used in platform_api_extension.h,
we just define the type to make the compiler happy */ we just define the type to make the compiler happy */
typedef struct {
int dummy;
} korp_rwlock;
struct os_thread_wait_node; struct os_thread_wait_node;
typedef struct os_thread_wait_node *os_thread_wait_list; typedef struct os_thread_wait_node *os_thread_wait_list;
typedef struct korp_cond { typedef struct korp_cond {
@ -169,6 +166,35 @@ typedef int os_file_handle;
typedef void *os_dir_stream; typedef void *os_dir_stream;
typedef int os_raw_file_handle; 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 static inline os_file_handle
os_get_invalid_handle() os_get_invalid_handle()
{ {

View File

@ -8,11 +8,22 @@ add_definitions(-DBH_PLATFORM_ZEPHYR)
include_directories(${PLATFORM_SHARED_DIR}) include_directories(${PLATFORM_SHARED_DIR})
include_directories(${PLATFORM_SHARED_DIR}/../include) 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) 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})

View File

@ -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;
}

View File

@ -1,10 +1,16 @@
#include "platform_api_extension.h" #include "platform_api_extension.h"
//#include "platform_wasi_types.h" #include "platform_api_vmcore.h"
#include <zephyr/net/net_ip.h> #include <zephyr/net/net_ip.h>
#include <zephyr/net/socket.h> #include <zephyr/net/socket.h>
#include <zephyr/net/socket_types.h> #include <zephyr/net/socket_types.h>
#include <zephyr/posix/poll.h>
#include <zephyr/posix/fcntl.h>
#include "zephyr_errno.h" #include "zephyr_errno.h"
#include <assert.h>
// Static functions // Static functions
static bool static bool
@ -19,7 +25,7 @@ textual_addr_to_sockaddr(const char *textual, int port, struct sockaddr *out,
assert(textual); assert(textual);
v4 = (struct sockaddr_in *)out; 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_family = AF_INET;
v4->sin_port = htons(port); v4->sin_port = htons(port);
*out_len = sizeof(struct sockaddr_in); *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 #ifdef IPPROTO_IPV6
v6 = (struct sockaddr_in *)out; 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_family = AF_INET6;
v6->sin6_port = htons(port); v6->sin6_port = htons(port);
*out_len = sizeof(struct sockaddr_in); *out_len = sizeof(struct sockaddr_in);
@ -114,13 +120,13 @@ static int
getaddrinfo_error_to_errno(int error) getaddrinfo_error_to_errno(int error)
{ {
switch (error) { switch (error) {
case EAI_AGAIN: case DNS_EAI_AGAIN:
return EAGAIN; return EAGAIN;
case EAI_FAIL: case DNS_EAI_FAIL:
return EFAULT; return EFAULT;
case EAI_MEMORY: case DNS_EAI_MEMORY:
return ENOMEM; return ENOMEM;
case EAI_SYSTEM: case DNS_EAI_SYSTEM:
return errno; return errno;
default: default:
return EINVAL; return EINVAL;
@ -128,7 +134,7 @@ getaddrinfo_error_to_errno(int error)
} }
static int static int
is_addrinfo_supported(struct addrinfo *info) is_addrinfo_supported(struct zsock_addrinfo *info)
{ {
return return
// Allow only IPv4 and IPv6 // Allow only IPv4 and IPv6
@ -161,7 +167,7 @@ os_socket_getbooloption(bh_socket_t socket, int level, int optname,
int optval; int optval;
socklen_t optval_size = sizeof(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; return BHT_ERROR;
} }
*is_enabled = (bool)optval; *is_enabled = (bool)optval;
@ -216,12 +222,12 @@ os_socket_bind(bh_socket_t socket, const char *host, int *port)
goto fail; goto fail;
} }
if (addr.sin_family == AF_INET) { if (addr.ss_family == AF_INET) { // addr.sin_family
*port = ntohs((&addr)->sin_port); *port = ntohs(((struct sockaddr_in *)&addr)->sin_port);
} }
else { else {
#ifdef IPPROTO_IPV6 #ifdef IPPROTO_IPV6
*port = ntohs((&addr)->sin6_port); *port = ntohs(((struct sockaddr_in *)&addr)->sin6_port);
#else #else
goto fail; goto fail;
#endif #endif
@ -270,18 +276,18 @@ os_socket_accept(bh_socket_t server_sock, bh_socket_t *sock, void *addr,
int int
os_socket_connect(bh_socket_t socket, const char *addr, int port) 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; socklen_t socklen;
int ret; int ret;
assert(addr); assert(addr);
if (!textual_addr_to_sockaddr(addr, port, (struct sockaddr *)&addr, if (!textual_addr_to_sockaddr(addr, port, (struct sockaddr *)&addr_in,
&socklen)) { &socklen)) {
return BHT_ERROR; return BHT_ERROR;
} }
ret = zsock_connect(socket, (struct sockaddr *)&addr, socklen); ret = zsock_connect(socket, (struct sockaddr *)&addr_in, socklen);
if (ret < 0) { if (ret < 0) {
return BHT_ERROR; 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 }; struct sockaddr_storage addr = { 0 };
socklen_t socklen; socklen_t socklen;
int ret;
if (bh_sockaddr_to_sockaddr(dest_addr, (struct sockaddr *)&addr, &socklen) (void)bh_sockaddr_to_sockaddr(dest_addr, (struct sockaddr *)&addr, &socklen);
== BHT_ERROR) {
return -1;
}
return zsock_sendto(socket, buf, len, flags, (struct sockaddr *)&addr, return zsock_sendto(socket, buf, len, flags, (struct sockaddr *)&addr,
socklen); socklen);
@ -543,14 +545,14 @@ int
os_socket_set_keep_alive(bh_socket_t socket, bool is_enabled) os_socket_set_keep_alive(bh_socket_t socket, bool is_enabled)
{ {
return os_socket_setbooloption(socket, SOL_SOCKET, SO_KEEPALIVE, return os_socket_setbooloption(socket, SOL_SOCKET, SO_KEEPALIVE,
is_enabled) is_enabled);
} }
int int
os_socket_get_keep_alive(bh_socket_t socket, bool *is_enabled) os_socket_get_keep_alive(bh_socket_t socket, bool *is_enabled)
{ {
return os_socket_getbooloption(socket, SOL_SOCKET, SO_KEEPALIVE, return os_socket_getbooloption(socket, SOL_SOCKET, SO_KEEPALIVE,
is_enabled) is_enabled);
} }
int 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); socklen_t time_s_len = sizeof(time_s_int);
#ifdef TCP_KEEPIDLE #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) { != 0) {
return BHT_ERROR; return BHT_ERROR;
} }
@ -847,11 +849,11 @@ os_socket_set_ip_add_membership(bh_socket_t socket,
#endif #endif
} }
else{ else{
struct ip_mreq mreq; struct ip_mreqn mreq;
mreq.imr_multiaddr.s_addr = imr_multiaddr->ipv4; 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)) sizeof(mreq))
!= 0) { != 0) {
return BHT_ERROR; return BHT_ERROR;
@ -878,7 +880,7 @@ os_socket_set_ip_drop_membership(bh_socket_t socket,
} }
mreq.ipv6mr_interface = imr_interface; 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)) sizeof(mreq))
!= 0) { != 0) {
return BHT_ERROR; return BHT_ERROR;
@ -889,11 +891,11 @@ os_socket_set_ip_drop_membership(bh_socket_t socket,
#endif #endif
} }
else { else {
struct ip_mreq mreq; struct ip_mreqn mreq;
mreq.imr_multiaddr.s_addr = imr_multiaddr->ipv4; 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)) sizeof(mreq))
!= 0) { != 0) {
return BHT_ERROR; return BHT_ERROR;
@ -990,3 +992,37 @@ os_socket_get_broadcast(bh_socket_t socket, bool *is_enabled)
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;
}