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 9a2befffa..5d26914b1 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 @@ -172,7 +172,7 @@ blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle, return error; } -#ifndef BH_PLATFORM_WINDOWS +#if !defined(BH_PLATFORM_WINDOWS) && !defined(WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) /* REVISIT: apply the os_file_handle style abstraction for pollfd? */ __wasi_errno_t blocking_op_poll(wasm_exec_env_t exec_env, os_poll_file_handle *pfds, os_nfds_t nfds, 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 01f89847e..14c112b78 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 @@ -55,7 +55,7 @@ blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle, __wasi_fdflags_t fd_flags, __wasi_lookupflags_t lookup_flags, wasi_libc_file_access_mode access_mode, os_file_handle *out); -#ifndef BH_PLATFORM_WINDOWS +#if !defined(BH_PLATFORM_WINDOWS) && !defined(WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) __wasi_errno_t blocking_op_poll(wasm_exec_env_t exec_env, os_poll_file_handle *pfds, os_nfds_t nfds, int timeout, int *retp); 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 bda554d42..5f442e69f 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) +#if !defined(WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) 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) */ +#endif /* !defWAMR) */ static inline void cond_destroy(struct cond *cond) { @@ -196,7 +196,12 @@ static inline bool cond_timedwait(struct cond *cond, struct mutex *lock, uint64_t timeout, bool abstime) REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS { +#if defined(WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) + return false; +} +#else int ret; + struct os_timespec ts = { .tv_sec = (time_t)(timeout / 1000000000), .tv_nsec = (long)(timeout % 1000000000), @@ -229,7 +234,7 @@ cond_timedwait(struct cond *cond, struct mutex *lock, uint64_t timeout, ++ts.tv_sec; } } -#endif +#endif /* !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK */ } else { #if CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP @@ -253,7 +258,7 @@ cond_timedwait(struct cond *cond, struct mutex *lock, uint64_t timeout, ts.tv_nsec -= 1000000000; ++ts.tv_sec; } -#endif +#endif /* CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP */ } ret = pthread_cond_timedwait(&cond->object, &lock->object, &ts); @@ -261,6 +266,7 @@ cond_timedwait(struct cond *cond, struct mutex *lock, uint64_t timeout, && "pthread_cond_timedwait() failed"); return ret == ETIMEDOUT; } +#endif /* WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR */ #endif static inline void 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 ef0743dee..cb889da55 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 @@ -22,6 +22,7 @@ #include "refcount.h" #include "rights.h" #include "str.h" +#include "assert.h" /* Some platforms (e.g. Windows) already define `min()` macro. We're undefing it here to make sure the `min` call does exactly @@ -1857,8 +1858,12 @@ wasmtime_ssp_fd_filestat_get(wasm_exec_env_t exec_env, struct fd_table *curfds, } static void -convert_timestamp(__wasi_timestamp_t in, struct timespec *out) +convert_timestamp(__wasi_timestamp_t in, struct os_timespec *out) { +#if defined(WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) + // No clock implem +} +#else // Store sub-second remainder. #if defined(__SYSCALL_SLONG_TYPE) out->tv_nsec = (__SYSCALL_SLONG_TYPE)(in % 1000000000); @@ -1870,7 +1875,7 @@ convert_timestamp(__wasi_timestamp_t in, struct timespec *out) // Clamp to the maximum in case it would overflow our system's time_t. out->tv_sec = (time_t)in < BH_TIME_T_MAX ? (time_t)in : BH_TIME_T_MAX; } - +#endif /* WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR */ __wasi_errno_t wasmtime_ssp_fd_filestat_set_size(wasm_exec_env_t exec_env, struct fd_table *curfds, __wasi_fd_t fd, @@ -2055,9 +2060,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, size_t nsubscriptions, size_t *nevents) NO_LOCK_ANALYSIS { -#ifdef BH_PLATFORM_WINDOWS - return __WASI_ENOSYS; -#elif BH_PLATEFORM_ZEPHYR +#if defined(BH_PLATFORM_WINDOWS) || defined(WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) return __WASI_ENOSYS; #else // Sleeping. @@ -2069,7 +2072,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, #if CONFIG_HAS_CLOCK_NANOSLEEP clockid_t clock_id; if (wasi_clockid_to_clockid(in[0].u.u.clock.clock_id, &clock_id)) { - struct timespec ts; + struct os_timespec ts; convert_timestamp(in[0].u.u.clock.timeout, &ts); int ret = clock_nanosleep( clock_id, @@ -2096,7 +2099,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, else { // Perform relative sleeps on the monotonic clock also using // nanosleep(). This is incorrect, but good enough for now. - struct timespec ts; + struct os_timespec ts; convert_timestamp(in[0].u.u.clock.timeout, &ts); nanosleep(&ts, NULL); } @@ -2124,7 +2127,7 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, } else { // Relative sleeps can be done using nanosleep(). - struct timespec ts; + struct os_timespec ts; convert_timestamp(in[0].u.u.clock.timeout, &ts); nanosleep(&ts, NULL); } @@ -2911,8 +2914,10 @@ wasmtime_ssp_sock_shutdown(wasm_exec_env_t exec_env, struct fd_table *curfds, __wasi_errno_t wasmtime_ssp_sched_yield(void) { -#ifdef BH_PLATFORM_WINDOWS +#if defined(BH_PLATFORM_WINDOWS) SwitchToThread(); +#elif defined(WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) + k_yield(); #else if (sched_yield() < 0) return convert_errno(errno); diff --git a/core/shared/platform/include/platform_api_extension.h b/core/shared/platform/include/platform_api_extension.h index 0f0daf584..b8c8ea0d8 100644 --- a/core/shared/platform/include/platform_api_extension.h +++ b/core/shared/platform/include/platform_api_extension.h @@ -1636,12 +1636,12 @@ os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision, } #endif - /* Expermimental :*/ +#if !defined(WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) __wasi_errno_t -os_ioctl(os_file_handle *handle, int request, void *argp); +os_ioctl(os_file_handle handle, int request, ...); __wasi_errno_t os_poll(os_poll_file_handle *pfds, os_nfds_t nfs, int timeout); - +#endif #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 08836c28a..5f05e4fa4 100644 --- a/core/shared/platform/zephyr/platform_internal.h +++ b/core/shared/platform/zephyr/platform_internal.h @@ -88,6 +88,12 @@ typedef struct korp_cond { os_thread_wait_list thread_wait_list; } korp_cond; +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; + #ifndef Z_TIMEOUT_MS #define Z_TIMEOUT_MS(ms) ms #endif @@ -167,7 +173,9 @@ typedef void *os_dir_stream; typedef int os_raw_file_handle; /*********************************************************/ -// try to stub POSIX implementation in sandboxed env. +//try to stub POSIX implementation in sandboxed env. +#if !defined(WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) + typedef struct zsock_pollfd os_poll_file_handle; typedef unsigned int os_nfds_t; @@ -188,11 +196,13 @@ typedef struct { #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; +// TODO: use it in sandboxed posix.c. +// int os_sched_yield(void) +// { +// k_yield(); +// return 0; +// } +#endif /*********************************************************/ static inline os_file_handle diff --git a/core/shared/platform/zephyr/shared_platform.cmake b/core/shared/platform/zephyr/shared_platform.cmake index b62abc840..ad84107fb 100644 --- a/core/shared/platform/zephyr/shared_platform.cmake +++ b/core/shared/platform/zephyr/shared_platform.cmake @@ -5,6 +5,8 @@ set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR}) add_definitions(-DBH_PLATFORM_ZEPHYR) +# add_definitions (-DWAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) + include_directories(${PLATFORM_SHARED_DIR}) include_directories(${PLATFORM_SHARED_DIR}/../include) @@ -18,6 +20,7 @@ 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) + list(REMOVE_ITEM source_all ${PLATFORM_SHARED_DIR}/zephyr_clock.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}) diff --git a/core/shared/platform/zephyr/zephyr_clock.c b/core/shared/platform/zephyr/zephyr_clock.c new file mode 100644 index 000000000..957bcfc11 --- /dev/null +++ b/core/shared/platform/zephyr/zephyr_clock.c @@ -0,0 +1,17 @@ +#include "platform_api_extension.h" +#include "platform_api_vmcore.h" + +#include "zephyr_errno.h" + +__wasi_errno_t +os_clock_res_get(__wasi_clockid_t clock_id, __wasi_timestamp_t *resolution) +{ + return __WASI_ENOSYS; +} + +__wasi_errno_t +os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision, + __wasi_timestamp_t *time) +{ + return __WASI_ENOSYS; +} \ No newline at end of file diff --git a/core/shared/platform/zephyr/zephyr_platform.c b/core/shared/platform/zephyr/zephyr_platform.c index fc54ba559..11e2e42d6 100644 --- a/core/shared/platform/zephyr/zephyr_platform.c +++ b/core/shared/platform/zephyr/zephyr_platform.c @@ -187,11 +187,14 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) return BH_MALLOC(size); } +#if !defined(WASM_ENABLE_LIBC_WASI) +/* os_mremap cause linking errors */ void * os_mremap(void *old_addr, size_t old_size, size_t new_size) { return os_mremap_slow(old_addr, old_size, new_size); } +#endif void os_munmap(void *addr, size_t size) diff --git a/core/shared/platform/zephyr/zephyr_socket.c b/core/shared/platform/zephyr/zephyr_socket.c index 0717532d0..0edde4650 100644 --- a/core/shared/platform/zephyr/zephyr_socket.c +++ b/core/shared/platform/zephyr/zephyr_socket.c @@ -1,7 +1,6 @@ #include "platform_api_extension.h" #include "platform_api_vmcore.h" - #include #include #include @@ -11,6 +10,7 @@ #include "zephyr_errno.h" #include +#include // Static functions static bool @@ -512,9 +512,9 @@ os_socket_get_send_buf_size(bh_socket_t socket, size_t *bufsiz) int os_socket_set_recv_buf_size(bh_socket_t socket, size_t bufsiz) -{ +{ int buf_size_int = (int)bufsiz; - + if (zsock_getsockopt(socket, SOL_SOCKET, SO_RCVBUF, &buf_size_int, sizeof(buf_size_int)) != 0) { @@ -574,7 +574,7 @@ os_socket_get_send_timeout(bh_socket_t socket, uint64 *timeout_us) struct timeval tv; socklen_t tv_len = sizeof(tv); - if (zsock_setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, &tv_len) != 0) { + if (zsock_setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, tv_len) != 0) { return BHT_ERROR; } *timeout_us = (tv.tv_sec * 1000000UL) + tv.tv_usec; @@ -602,7 +602,7 @@ os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us) struct timeval tv; socklen_t tv_len = sizeof(tv); - if (zsock_setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, &tv_len) != 0) { + if (zsock_setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, tv_len) != 0) { return BHT_ERROR; } *timeout_us = (tv.tv_sec * 1000000UL) + tv.tv_usec; @@ -722,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 (zsock_setsockopt(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; } @@ -730,7 +730,7 @@ os_socket_get_tcp_keep_idle(bh_socket_t socket, uint32_t *time_s) return BHT_OK; #elif defined(TCP_KEEPALIVE) - if (zsock_setsockopt(socket, IPPROTO_TCP, TCP_KEEPALIVE, &time_s_int, &time_s_len) + if (zsock_setsockopt(socket, IPPROTO_TCP, TCP_KEEPALIVE, &time_s_int, time_s_len) != 0) { return BHT_ERROR; } @@ -901,6 +901,7 @@ os_socket_set_ip_drop_membership(bh_socket_t socket, return BHT_ERROR; } } + return BHT_OK; } int @@ -918,7 +919,7 @@ os_socket_get_ip_ttl(bh_socket_t socket, uint8_t *ttl_s) { socklen_t opt_len = sizeof(*ttl_s); - if (zsock_setsockopt(socket, IPPROTO_IP, IP_MULTICAST_TTL, ttl_s, &opt_len) + if (zsock_setsockopt(socket, IPPROTO_IP, IP_MULTICAST_TTL, ttl_s, opt_len) != 0) { return BHT_ERROR; } @@ -942,7 +943,7 @@ os_socket_get_ip_multicast_ttl(bh_socket_t socket, uint8_t *ttl_s) { socklen_t opt_len = sizeof(*ttl_s); - if (zsock_setsockopt(socket, IPPROTO_IP, IP_MULTICAST_TTL, ttl_s, &opt_len) + if (zsock_setsockopt(socket, IPPROTO_IP, IP_MULTICAST_TTL, ttl_s, opt_len) != 0) { return BHT_ERROR; } @@ -993,14 +994,19 @@ os_socket_get_broadcast(bh_socket_t socket, bool *is_enabled) } // Experimental : +#if !defined(WAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) __wasi_errno_t -os_ioctl(os_file_handle *handle, int request, void *argp) +os_ioctl(os_file_handle handle, int request, ...) { __wasi_errno_t wasi_errno = __WASI_ESUCCESS; + va_list args; - if(zsock_ioctl_wrapper(handle, request, argp) < 0){ + va_start(args, request); + if(zsock_ioctl(handle, request, args) < 0){ wasi_errno = zephyr_to_wasi_errno(errno); } + va_end(args); + return wasi_errno; } @@ -1010,13 +1016,13 @@ 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) + rc = zsock_poll(fds, nfs, timeout); if(rc < 0){ wasi_errno = zephyr_to_wasi_errno(errno); } switch(rc){ case 0: - wasi_errno = __WASI_ETIMEOUT; + wasi_errno = __WASI_ETIMEDOUT; break; case -1: wasi_errno = zephyr_to_wasi_errno(errno); @@ -1025,4 +1031,5 @@ os_poll(os_poll_file_handle *fds, os_nfds_t nfs, int timeout) break; } return wasi_errno; -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/core/shared/platform/zephyr/zephyr_thread.c b/core/shared/platform/zephyr/zephyr_thread.c index 53ca71f62..c3ab92dc8 100644 --- a/core/shared/platform/zephyr/zephyr_thread.c +++ b/core/shared/platform/zephyr/zephyr_thread.c @@ -579,6 +579,41 @@ void os_thread_jit_write_protect_np(bool enabled) {} +int +os_rwlock_init(korp_rwlock *lock) +{ + /* Not implemented */ + return BHT_ERROR; +} + +int +os_rwlock_rdlock(korp_rwlock *lock) +{ + /* Not implemented */ + return BHT_ERROR; +} + +int +os_rwlock_wrlock(korp_rwlock *lock) +{ + /* Not implemented */ + return BHT_ERROR; +} + +int +os_rwlock_unlock(korp_rwlock *lock) +{ + /* Not implemented */ + return BHT_ERROR; +} + +int +os_rwlock_destroy(korp_rwlock *lock) +{ + /* Not implemented */ + return BHT_ERROR; +} + int os_thread_detach(korp_tid thread) { diff --git a/product-mini/platforms/zephyr/simple-http/CMakeLists.txt b/product-mini/platforms/zephyr/simple-http/CMakeLists.txt index 505f01b40..4a51aaa57 100644 --- a/product-mini/platforms/zephyr/simple-http/CMakeLists.txt +++ b/product-mini/platforms/zephyr/simple-http/CMakeLists.txt @@ -10,35 +10,17 @@ enable_language (ASM) set (WAMR_BUILD_PLATFORM "zephyr") -############################################################################################################################ -# __ __ __ __ _____ _____ ____ _ _ ______ _____ _____ _ _ _____ _______ _____ ____ _ _ # -# \ \ / /\ | \/ | __ \ / ____/ __ \| \ | | ____|_ _/ ____| | | | __ \ /\|__ __|_ _/ __ \| \ | | # -# \ \ /\ / / \ | \ / | |__) | | | | | | | \| | |__ | || | __| | | | |__) | / \ | | | || | | | \| | # -# \ \/ \/ / /\ \ | |\/| | _ / | | | | | | . ` | __| | || | |_ | | | | _ / / /\ \ | | | || | | | . ` | # -# \ /\ / ____ \| | | | | \ \ | |___| |__| | |\ | | _| || |__| | |__| | | \ \ / ____ \| | _| || |__| | |\ | # -# \/ \/_/ \_\_| |_|_| \_\ \_____\____/|_| \_|_| |_____\_____|\____/|_| \_\/_/ \_\_| |_____\____/|_| \_| # -############################################################################################################################ - -# Build as X86_32 by default, change to "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", "MIPS" or "XTENSA" -# if we want to support arm, thumb, mips or xtensa - +# WAMR Configuration: set (WAMR_BUILD_TARGET "THUMB") set (WAMR_BUILD_INTERP 1) set (WAMR_BUILD_AOT 1) set (WAMR_BUILD_LIBC_BUILTIN 1) set (WAMR_BUILD_LIBC_WASI 1) # 1 to use sockets -set (WAMR_BUILD_LIB_PTHREAD 0) # 1 to use sockets => cause errors +set (WAMR_BUILD_LIB_PTHREAD 0) set (WAMR_BUILD_GLOBAL_HEAP_POOL 1) set (WAMR_BUILD_GLOBAL_HEAP_SIZE 131072) # 128 KB -################################################### -# _____ ______ _______ ______ _ ___ __ # -# / ____| ____|__ __| | ____| \ | \ \ / / # -# | (___ | |__ | | | |__ | \| |\ \ / / # -# \___ \| __| | | | __| | . ` | \ \/ / # -# ____) | |____ | | | |____| |\ | \ / # -# |_____/|______| |_| |______|_| \_| \/ # -################################################### +# Environment variables: # Check if WAMR_ROOT_DIR is set if(DEFINED ENV{WAMR_ROOT_DIR}) @@ -78,7 +60,7 @@ set(WAMR_SDK_DIR ${WAMR_APP_FRAMEWORK_DIR}/wamr-sdk ) -# set the WAMR_LIBC_BUILTIN_DIR with the path specified in the environment variable +# set the WAMR_LIBC_BUILTIN_DIR set(WAMR_LIBC_BUILTIN_DIR ${WAMR_SDK_DIR}/wamr-sdk/app/libc-builtin-sysroot ) @@ -106,15 +88,8 @@ target_sources(app PRIVATE ${WAMR_RUNTIME_LIB_SOURCE} src/main.c) -#################################################################################################### -# ____ _ _ _____ _ _____ __ __ _____ __ __ __ __ ____ _____ -# | _ \| | | |_ _| | | __ \ \ \ / /\ / ____| \/ | | \/ |/ __ \| __ \ -# | |_) | | | | | | | | | | | | \ \ /\ / / \ | (___ | \ / | | \ / | | | | | | | -# | _ <| | | | | | | | | | | | \ \/ \/ / /\ \ \___ \| |\/| | | |\/| | | | | | | | -# | |_) | |__| |_| |_| |____| |__| | \ /\ / ____ \ ____) | | | | | | | | |__| | |__| | -# |____/ \____/|_____|______|_____/ \/ \/_/ \_\_____/|_| |_| |_| |_|\____/|_____/ -#################################################################################################### +# Build wasm module with WAMR SDK add_dependencies(app wamr-sdk) include(ExternalProject) @@ -141,6 +116,7 @@ ExternalProject_Add(wamr-sdk USES_TERMINAL ) +# Curently only one app is present set(WASM_APPS http_get ) @@ -156,7 +132,6 @@ foreach(WASM_APP ${WASM_APPS}) ${WASI_SDK_PATH}/bin/clang -O3 -I${WAMR_LIBC_BUILTIN_DIR}/include -I${WAMR_SDK_PACKAGE_OUT_DIR}/include - -I/usr/include/ # temp -L${WAMR_SDK_PACKAGE_OUT_DIR}/lib -lapp_framework -z stack-size=8192 -Wl,--initial-memory=65536 @@ -173,7 +148,6 @@ foreach(WASM_APP ${WASM_APPS}) USES_TERMINAL ) - # Run python script to generate the header file add_custom_command( OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/${WASM_APP}.h diff --git a/product-mini/platforms/zephyr/simple-http/README.md b/product-mini/platforms/zephyr/simple-http/README.md index 004acbdd0..dcb5401ca 100644 --- a/product-mini/platforms/zephyr/simple-http/README.md +++ b/product-mini/platforms/zephyr/simple-http/README.md @@ -2,15 +2,83 @@ This is a simple http client that make a GET request to a server. ## Setup -TODO +1. Connect the USB cable to the Nucleo board. + +2. Optional: Connect a network cable to the board ethernet port. ## Run Command -Replace `nucleo_h743zi` with your board name. +1. **Build:** Replace `nucleo_h743zi` with your board name and `THUMBV8` with your target architecture. + ```bash + ZEPHYR_BASE=~/zephyrproject/zephyr \ + WAMR_ROOT_DIR=~/wasm-micro-runtime \ + WASI_SDK_PATH=~/wasi-sdk-21.0 \ + WAMR_APP_FRAMEWORK_DIR=~/wamr-app-framework \ + west build . -b nucleo_h563zi -p always -- -DWAMR_BUILD_TARGET=THUMBV8 + ``` + ⚠️ **Warning:** The flags `ZEPHYR_BASE`, `WAMR_ROOT_DIR`, `WASI_SDK_PATH`, and `WAMR_APP_FRAMEWORK_DIR` need to be set otherwise the build will fail. + +2. **Flash:** + ```bash + ZEPHYR_BASE=~/zephyrproject/zephyr west flash + ``` + +3. **Monitor:** Use a serial link to monitor the output. Personally, I use minicom. + ```bash + minicom -D /dev/ttyACM0 + ``` + +4. **Debug:** Curently investigating. + +## Different approach + +* **Soft:** Trying to implement missing API, and enhancing the existing abstraction API. + +* **Hard:** Making the code compile and run with the existing API. + +By default, we are following the **Soft** approach. If you want to follow the **Hard** approach, you need to uncomment the following line in the `shared_plateform.cmake` file. +```cmake +# add_definitions (-DWAMR_PLATFORM_ZEPHYR_FORCE_NO_ERROR) +``` + +### Outputs + +* **Soft:** Unable to compile the code. + +* **Hard:** The code will compile but cause a stack overflow error. + ```bash + [00:00:00.001,000] os: ***** USAGE FAULT ***** + [00:00:00.007,000] os: Stack overflow (context area not valid) + [00:00:00.014,000] os: r0/a1: 0xf0f0f0f0 r1/a2: 0x693b613b r2/a3: 0x0807be46 + [00:00:00.022,000] os: r3/a4: 0x09000000 r12/ip: 0x2002fb58 r14/lr: 0x0804f0b1 + [00:00:00.031,000] os: xpsr: 0x08080c00 + [00:00:00.036,000] os: Faulting instruction address (r15/pc): 0x2a1b681b + [00:00:00.044,000] os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0 + [00:00:00.052,000] os: Current thread: 0x20002ef8 (unknown) + [00:00:00.059,000] os: Halting system + ``` + +## Expected Output + +### Host ```bash -ZEPHYR_BASE=~/zephyrproject/zephyr \ -WAMR_ROOT_DIR=~/wasm-micro-runtime \ -WASI_SDK_PATH=~/wasi-sdk-21.0 \ -WAMR_APP_FRAMEWORK_DIR=~/wamr-app-framework \ -west build . -b nucleo_h563zi -p always -- -DWAMR_BUILD_TARGET=THUMBV8 -DCONFIG_LOG_MODE_IMMEDIATE=y +python3 -m http.server --bind 0.0.0.0 +Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ... +127.0.0.1 - - [12/Apr/2024 09:54:24] "GET / HTTP/1.0" 200 - +``` + +### Target +```bash +Preparing HTTP GET request for http://127.0.0.1:8000/ +sock = 5 +Response: + +HTTP/1.0 200 OK +Server: SimpleHTTP/0.6 Python/3.10.12 +Date: Thu, 11 Apr 2024 13:41:11 GMT +Content-type: text/html +Content-Length: 14054 +Last-Modified: Thu, 30 Mar 2023 09:11:09 GMT + +# Skip HTML content ``` \ No newline at end of file diff --git a/product-mini/platforms/zephyr/simple-http/prj.conf b/product-mini/platforms/zephyr/simple-http/prj.conf index 70444d4fa..a34f1220e 100644 --- a/product-mini/platforms/zephyr/simple-http/prj.conf +++ b/product-mini/platforms/zephyr/simple-http/prj.conf @@ -1,7 +1,35 @@ # Copyright (C) 2019 Intel Corporation. All rights reserved. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -CONFIG_STACK_SENTINEL=y +# Log config CONFIG_PRINTK=y CONFIG_LOG=y -CONFIG_POSIX_API=n \ No newline at end of file +CONFIG_LOG_MODE_IMMEDIATE=y +CONFIG_LOG_MAX_LEVEL=4 +#Info=4 Debug=3 Warn=2 Error=1 Off=0 + +# Networking config +CONFIG_NETWORKING=y +CONFIG_NET_IPV4=y +CONFIG_NET_TCP=y +CONFIG_NET_SOCKETS=y +CONFIG_POSIX_API=n + +# Stack conf +CONFIG_NO_OPTIMIZATIONS=y +CONFIG_STACK_SENTINEL=y +CONFIG_HW_STACK_PROTECTION=y + +# Debug +# CONFIG_DEBUG=y +# CONFIG_STACK_USAGE=y +# CONFIG_THREAD_ANALYZER=y +# CONFIG_THREAD_ANALYZER_USE_PRINTK=y +# CONFIG_THREAD_ANALYZER_AUTO=y +# CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=5 + +# GDB +# CONFIG_SERIAL=y +# CONFIG_UART_USE_RUNTIME_CONFIGURE=y +# CONFIG_GDBSTUB=y +# CONFIG_GDBSTUB_SERIAL_BACKEND=y \ No newline at end of file diff --git a/product-mini/platforms/zephyr/simple-http/src/http_get.h b/product-mini/platforms/zephyr/simple-http/src/http_get.h index e921e7b31..46b01e381 100644 --- a/product-mini/platforms/zephyr/simple-http/src/http_get.h +++ b/product-mini/platforms/zephyr/simple-http/src/http_get.h @@ -1 +1 @@ -unsigned char __aligned(4) wasm_test_file[] = { 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x23, 0x06, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x60, 0x04, 0x7f, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x03, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x00, 0x01, 0x7f, 0x60, 0x01, 0x7f, 0x00, 0x02, 0x98, 0x01, 0x0b, 0x03, 0x65, 0x6e, 0x76, 0x06, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x00, 0x02, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x70, 0x75, 0x74, 0x73, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x0b, 0x67, 0x65, 0x74, 0x61, 0x64, 0x64, 0x72, 0x69, 0x6e, 0x66, 0x6f, 0x00, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x06, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x00, 0x03, 0x03, 0x65, 0x6e, 0x76, 0x07, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x00, 0x03, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x73, 0x65, 0x6e, 0x64, 0x00, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x72, 0x65, 0x63, 0x76, 0x00, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x07, 0x70, 0x75, 0x74, 0x63, 0x68, 0x61, 0x72, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x05, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x10, 0x5f, 0x5f, 0x65, 0x72, 0x72, 0x6e, 0x6f, 0x5f, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x04, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x65, 0x78, 0x69, 0x74, 0x00, 0x05, 0x03, 0x02, 0x01, 0x02, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x15, 0x03, 0x7f, 0x01, 0x41, 0xd0, 0xd3, 0x00, 0x0b, 0x7f, 0x00, 0x41, 0xd0, 0x13, 0x0b, 0x7f, 0x00, 0x41, 0xd0, 0xd3, 0x00, 0x0b, 0x07, 0x2c, 0x04, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x0b, 0x0a, 0x5f, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x65, 0x6e, 0x64, 0x03, 0x01, 0x0b, 0x5f, 0x5f, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x62, 0x61, 0x73, 0x65, 0x03, 0x02, 0x0a, 0xc6, 0x03, 0x01, 0xc3, 0x03, 0x02, 0x03, 0x7f, 0x01, 0x7e, 0x02, 0x7f, 0x23, 0x00, 0x41, 0x80, 0x01, 0x6b, 0x22, 0x00, 0x24, 0x00, 0x41, 0xce, 0x08, 0x10, 0x01, 0x1a, 0x41, 0xb4, 0x0b, 0x42, 0x82, 0x80, 0x80, 0x80, 0x10, 0x37, 0x02, 0x00, 0x20, 0x00, 0x41, 0xbe, 0x08, 0x41, 0xc9, 0x08, 0x41, 0xb0, 0x0b, 0x20, 0x00, 0x41, 0xfc, 0x00, 0x6a, 0x10, 0x02, 0x22, 0x01, 0x36, 0x02, 0x70, 0x41, 0xf7, 0x09, 0x20, 0x00, 0x41, 0xf0, 0x00, 0x6a, 0x10, 0x00, 0x1a, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x02, 0x40, 0x20, 0x01, 0x04, 0x40, 0x41, 0x83, 0x08, 0x10, 0x01, 0x1a, 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x28, 0x02, 0x7c, 0x22, 0x01, 0x29, 0x02, 0x08, 0x21, 0x05, 0x20, 0x01, 0x28, 0x02, 0x04, 0x21, 0x02, 0x20, 0x01, 0x28, 0x02, 0x14, 0x22, 0x03, 0x2f, 0x01, 0x02, 0x21, 0x04, 0x20, 0x00, 0x41, 0xe0, 0x00, 0x6a, 0x20, 0x03, 0x2f, 0x01, 0x00, 0x36, 0x02, 0x00, 0x20, 0x00, 0x41, 0xe4, 0x00, 0x6a, 0x20, 0x04, 0x36, 0x02, 0x00, 0x20, 0x00, 0x20, 0x01, 0x36, 0x02, 0x50, 0x20, 0x00, 0x20, 0x02, 0x36, 0x02, 0x54, 0x20, 0x00, 0x20, 0x05, 0x37, 0x03, 0x58, 0x41, 0x85, 0x09, 0x20, 0x00, 0x41, 0xd0, 0x00, 0x6a, 0x10, 0x00, 0x1a, 0x41, 0x02, 0x41, 0x01, 0x41, 0x06, 0x10, 0x03, 0x22, 0x01, 0x41, 0x7f, 0x46, 0x0d, 0x01, 0x20, 0x00, 0x20, 0x01, 0x36, 0x02, 0x40, 0x41, 0xec, 0x09, 0x20, 0x00, 0x41, 0x40, 0x6b, 0x10, 0x00, 0x1a, 0x20, 0x01, 0x20, 0x00, 0x28, 0x02, 0x7c, 0x22, 0x02, 0x28, 0x02, 0x14, 0x20, 0x02, 0x28, 0x02, 0x10, 0x10, 0x04, 0x41, 0x7f, 0x46, 0x0d, 0x02, 0x20, 0x01, 0x41, 0x86, 0x0b, 0x41, 0x24, 0x41, 0x00, 0x10, 0x05, 0x41, 0x7f, 0x46, 0x0d, 0x03, 0x41, 0x8f, 0x0a, 0x10, 0x01, 0x1a, 0x02, 0x40, 0x20, 0x01, 0x41, 0xd0, 0x0b, 0x41, 0xff, 0x07, 0x41, 0x00, 0x10, 0x06, 0x22, 0x02, 0x41, 0x00, 0x4e, 0x04, 0x40, 0x03, 0x40, 0x20, 0x02, 0x45, 0x0d, 0x02, 0x20, 0x02, 0x41, 0xd0, 0x0b, 0x6a, 0x41, 0x00, 0x3a, 0x00, 0x00, 0x20, 0x00, 0x41, 0xd0, 0x0b, 0x36, 0x02, 0x30, 0x41, 0x80, 0x08, 0x20, 0x00, 0x41, 0x30, 0x6a, 0x10, 0x00, 0x1a, 0x20, 0x01, 0x41, 0xd0, 0x0b, 0x41, 0xff, 0x07, 0x41, 0x00, 0x10, 0x06, 0x22, 0x02, 0x41, 0x00, 0x4e, 0x0d, 0x00, 0x0b, 0x0b, 0x41, 0xa7, 0x08, 0x10, 0x01, 0x1a, 0x0c, 0x01, 0x0b, 0x41, 0x0a, 0x10, 0x07, 0x1a, 0x20, 0x01, 0x10, 0x08, 0x1a, 0x0b, 0x20, 0x00, 0x41, 0x80, 0x01, 0x6a, 0x24, 0x00, 0x41, 0x00, 0x0c, 0x03, 0x0b, 0x20, 0x00, 0x10, 0x09, 0x28, 0x02, 0x00, 0x36, 0x02, 0x00, 0x41, 0xdc, 0x09, 0x20, 0x00, 0x10, 0x00, 0x1a, 0x41, 0x01, 0x10, 0x0a, 0x00, 0x0b, 0x20, 0x00, 0x10, 0x09, 0x28, 0x02, 0x00, 0x36, 0x02, 0x10, 0x41, 0x9a, 0x0a, 0x20, 0x00, 0x41, 0x10, 0x6a, 0x10, 0x00, 0x1a, 0x41, 0x01, 0x10, 0x0a, 0x00, 0x0b, 0x20, 0x00, 0x10, 0x09, 0x28, 0x02, 0x00, 0x36, 0x02, 0x20, 0x41, 0xd2, 0x0a, 0x20, 0x00, 0x41, 0x20, 0x6a, 0x10, 0x00, 0x1a, 0x41, 0x01, 0x10, 0x0a, 0x00, 0x0b, 0x0b, 0x0b, 0xb2, 0x03, 0x01, 0x00, 0x41, 0x80, 0x08, 0x0b, 0xaa, 0x03, 0x25, 0x73, 0x00, 0x55, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x74, 0x6f, 0x20, 0x72, 0x65, 0x73, 0x6f, 0x6c, 0x76, 0x65, 0x20, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x2c, 0x20, 0x71, 0x75, 0x69, 0x74, 0x74, 0x69, 0x6e, 0x67, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x72, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x00, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x31, 0x30, 0x00, 0x38, 0x30, 0x30, 0x30, 0x00, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x48, 0x54, 0x54, 0x50, 0x20, 0x47, 0x45, 0x54, 0x20, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x31, 0x30, 0x3a, 0x38, 0x30, 0x30, 0x30, 0x2f, 0x00, 0x61, 0x64, 0x64, 0x72, 0x69, 0x6e, 0x66, 0x6f, 0x20, 0x40, 0x25, 0x70, 0x3a, 0x20, 0x61, 0x69, 0x5f, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x3d, 0x25, 0x64, 0x2c, 0x20, 0x61, 0x69, 0x5f, 0x73, 0x6f, 0x63, 0x6b, 0x74, 0x79, 0x70, 0x65, 0x3d, 0x25, 0x64, 0x2c, 0x20, 0x61, 0x69, 0x5f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3d, 0x25, 0x64, 0x2c, 0x20, 0x73, 0x61, 0x5f, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x3d, 0x25, 0x64, 0x2c, 0x20, 0x73, 0x69, 0x6e, 0x5f, 0x70, 0x6f, 0x72, 0x74, 0x3d, 0x25, 0x78, 0x0a, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x25, 0x64, 0x3a, 0x20, 0x73, 0x6f, 0x63, 0x6b, 0x0a, 0x00, 0x73, 0x6f, 0x63, 0x6b, 0x20, 0x3d, 0x20, 0x25, 0x64, 0x0a, 0x00, 0x67, 0x65, 0x74, 0x61, 0x64, 0x64, 0x72, 0x69, 0x6e, 0x66, 0x6f, 0x20, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x3a, 0x20, 0x25, 0x64, 0x0a, 0x00, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x3a, 0x0a, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x25, 0x64, 0x3a, 0x20, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x28, 0x73, 0x6f, 0x63, 0x6b, 0x2c, 0x20, 0x72, 0x65, 0x73, 0x2d, 0x3e, 0x61, 0x69, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x2c, 0x20, 0x72, 0x65, 0x73, 0x2d, 0x3e, 0x61, 0x69, 0x5f, 0x61, 0x64, 0x64, 0x72, 0x6c, 0x65, 0x6e, 0x29, 0x0a, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x25, 0x64, 0x3a, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x28, 0x73, 0x6f, 0x63, 0x6b, 0x2c, 0x20, 0x52, 0x45, 0x51, 0x55, 0x45, 0x53, 0x54, 0x2c, 0x20, 0x53, 0x53, 0x54, 0x52, 0x4c, 0x45, 0x4e, 0x28, 0x52, 0x45, 0x51, 0x55, 0x45, 0x53, 0x54, 0x29, 0x2c, 0x20, 0x30, 0x29, 0x0a, 0x00, 0x47, 0x45, 0x54, 0x20, 0x2f, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x31, 0x30, 0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x26, 0x09, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x73, 0x01, 0x0c, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x65, 0x64, 0x2d, 0x62, 0x79, 0x01, 0x05, 0x63, 0x6c, 0x61, 0x6e, 0x67, 0x06, 0x31, 0x37, 0x2e, 0x30, 0x2e, 0x36, 0x00, 0x2c, 0x0f, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x5f, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x73, 0x02, 0x2b, 0x0f, 0x6d, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2d, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x73, 0x2b, 0x08, 0x73, 0x69, 0x67, 0x6e, 0x2d, 0x65, 0x78, 0x74 }; \ No newline at end of file +unsigned char __aligned(4) wasm_test_file[] = { 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x1f, 0x05, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x60, 0x03, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x04, 0x7f, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x01, 0x7f, 0x00, 0x02, 0x7f, 0x0a, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x70, 0x75, 0x74, 0x73, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x09, 0x69, 0x6e, 0x65, 0x74, 0x5f, 0x70, 0x74, 0x6f, 0x6e, 0x00, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x06, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x00, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x06, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x00, 0x02, 0x03, 0x65, 0x6e, 0x76, 0x07, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x00, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x73, 0x65, 0x6e, 0x64, 0x00, 0x03, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x72, 0x65, 0x63, 0x76, 0x00, 0x03, 0x03, 0x65, 0x6e, 0x76, 0x07, 0x70, 0x75, 0x74, 0x63, 0x68, 0x61, 0x72, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x05, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x65, 0x78, 0x69, 0x74, 0x00, 0x04, 0x03, 0x02, 0x01, 0x02, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x15, 0x03, 0x7f, 0x01, 0x41, 0xa0, 0xd2, 0x00, 0x0b, 0x7f, 0x00, 0x41, 0xa0, 0x12, 0x0b, 0x7f, 0x00, 0x41, 0xa0, 0xd2, 0x00, 0x0b, 0x07, 0x2c, 0x04, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x0a, 0x0a, 0x5f, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x65, 0x6e, 0x64, 0x03, 0x01, 0x0b, 0x5f, 0x5f, 0x68, 0x65, 0x61, 0x70, 0x5f, 0x62, 0x61, 0x73, 0x65, 0x03, 0x02, 0x0a, 0xd4, 0x02, 0x01, 0xd1, 0x02, 0x01, 0x01, 0x7f, 0x02, 0x7f, 0x23, 0x00, 0x41, 0xe0, 0x00, 0x6b, 0x22, 0x00, 0x24, 0x00, 0x41, 0xa5, 0x08, 0x10, 0x00, 0x1a, 0x20, 0x00, 0x42, 0x00, 0x37, 0x03, 0x50, 0x20, 0x00, 0x42, 0x00, 0x37, 0x03, 0x58, 0x20, 0x00, 0x41, 0x01, 0x3b, 0x01, 0x50, 0x20, 0x00, 0x41, 0x9f, 0x80, 0x01, 0x3b, 0x01, 0x52, 0x41, 0x01, 0x41, 0x9a, 0x08, 0x20, 0x00, 0x41, 0xd0, 0x00, 0x6a, 0x41, 0x04, 0x72, 0x10, 0x01, 0x1a, 0x02, 0x40, 0x02, 0x40, 0x41, 0x01, 0x41, 0x06, 0x41, 0x06, 0x10, 0x02, 0x22, 0x01, 0x41, 0x7f, 0x47, 0x04, 0x40, 0x20, 0x00, 0x20, 0x01, 0x36, 0x02, 0x40, 0x41, 0xec, 0x08, 0x20, 0x00, 0x41, 0x40, 0x6b, 0x10, 0x03, 0x1a, 0x20, 0x01, 0x20, 0x00, 0x41, 0xd0, 0x00, 0x6a, 0x41, 0x10, 0x10, 0x04, 0x41, 0x7f, 0x46, 0x0d, 0x01, 0x20, 0x01, 0x41, 0xf6, 0x09, 0x41, 0x24, 0x41, 0x00, 0x10, 0x05, 0x41, 0x7f, 0x46, 0x0d, 0x02, 0x41, 0xf7, 0x08, 0x10, 0x00, 0x1a, 0x02, 0x40, 0x02, 0x40, 0x20, 0x01, 0x41, 0xa0, 0x0a, 0x41, 0xff, 0x07, 0x41, 0x00, 0x10, 0x06, 0x22, 0x02, 0x41, 0x00, 0x4e, 0x04, 0x40, 0x03, 0x40, 0x20, 0x02, 0x45, 0x0d, 0x02, 0x20, 0x02, 0x41, 0xa0, 0x0a, 0x6a, 0x41, 0x00, 0x3a, 0x00, 0x00, 0x20, 0x00, 0x41, 0xa0, 0x0a, 0x36, 0x02, 0x30, 0x41, 0x80, 0x08, 0x20, 0x00, 0x41, 0x30, 0x6a, 0x10, 0x03, 0x1a, 0x20, 0x01, 0x41, 0xa0, 0x0a, 0x41, 0xff, 0x07, 0x41, 0x00, 0x10, 0x06, 0x22, 0x02, 0x41, 0x00, 0x4e, 0x0d, 0x00, 0x0b, 0x0b, 0x41, 0x83, 0x08, 0x10, 0x00, 0x1a, 0x0c, 0x01, 0x0b, 0x41, 0x0a, 0x10, 0x07, 0x1a, 0x20, 0x01, 0x10, 0x08, 0x1a, 0x0b, 0x20, 0x00, 0x41, 0xe0, 0x00, 0x6a, 0x24, 0x00, 0x41, 0x00, 0x0c, 0x03, 0x0b, 0x20, 0x00, 0x41, 0x00, 0x28, 0x02, 0x00, 0x36, 0x02, 0x00, 0x41, 0xdc, 0x08, 0x20, 0x00, 0x10, 0x03, 0x1a, 0x41, 0x01, 0x10, 0x09, 0x00, 0x0b, 0x20, 0x00, 0x41, 0x00, 0x28, 0x02, 0x00, 0x36, 0x02, 0x10, 0x41, 0xb6, 0x09, 0x20, 0x00, 0x41, 0x10, 0x6a, 0x10, 0x03, 0x1a, 0x41, 0x01, 0x10, 0x09, 0x00, 0x0b, 0x20, 0x00, 0x41, 0x00, 0x28, 0x02, 0x00, 0x36, 0x02, 0x20, 0x41, 0x82, 0x09, 0x20, 0x00, 0x41, 0x20, 0x6a, 0x10, 0x03, 0x1a, 0x41, 0x01, 0x10, 0x09, 0x00, 0x0b, 0x0b, 0x0b, 0xa2, 0x02, 0x01, 0x00, 0x41, 0x80, 0x08, 0x0b, 0x9a, 0x02, 0x25, 0x73, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x72, 0x65, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x00, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x31, 0x30, 0x00, 0x50, 0x72, 0x65, 0x70, 0x61, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x48, 0x54, 0x54, 0x50, 0x20, 0x47, 0x45, 0x54, 0x20, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x31, 0x30, 0x3a, 0x38, 0x30, 0x30, 0x30, 0x2f, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x25, 0x64, 0x3a, 0x20, 0x73, 0x6f, 0x63, 0x6b, 0x0a, 0x00, 0x73, 0x6f, 0x63, 0x6b, 0x20, 0x3d, 0x20, 0x25, 0x64, 0x0a, 0x00, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x3a, 0x0a, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x25, 0x64, 0x3a, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x28, 0x73, 0x6f, 0x63, 0x6b, 0x2c, 0x20, 0x52, 0x45, 0x51, 0x55, 0x45, 0x53, 0x54, 0x2c, 0x20, 0x53, 0x53, 0x54, 0x52, 0x4c, 0x45, 0x4e, 0x28, 0x52, 0x45, 0x51, 0x55, 0x45, 0x53, 0x54, 0x29, 0x2c, 0x20, 0x30, 0x29, 0x0a, 0x00, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x25, 0x64, 0x3a, 0x20, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x28, 0x73, 0x6f, 0x63, 0x6b, 0x2c, 0x20, 0x28, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x73, 0x6f, 0x63, 0x6b, 0x61, 0x64, 0x64, 0x72, 0x2a, 0x29, 0x26, 0x61, 0x64, 0x64, 0x72, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x6f, 0x66, 0x28, 0x61, 0x64, 0x64, 0x72, 0x29, 0x29, 0x0a, 0x00, 0x47, 0x45, 0x54, 0x20, 0x2f, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x31, 0x30, 0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x26, 0x09, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x73, 0x01, 0x0c, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x65, 0x64, 0x2d, 0x62, 0x79, 0x01, 0x05, 0x63, 0x6c, 0x61, 0x6e, 0x67, 0x06, 0x31, 0x37, 0x2e, 0x30, 0x2e, 0x36, 0x00, 0x2c, 0x0f, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x5f, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x73, 0x02, 0x2b, 0x0f, 0x6d, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2d, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x73, 0x2b, 0x08, 0x73, 0x69, 0x67, 0x6e, 0x2d, 0x65, 0x78, 0x74 }; \ No newline at end of file diff --git a/product-mini/platforms/zephyr/simple-http/src/main.c b/product-mini/platforms/zephyr/simple-http/src/main.c index f275236f4..be4593ff7 100644 --- a/product-mini/platforms/zephyr/simple-http/src/main.c +++ b/product-mini/platforms/zephyr/simple-http/src/main.c @@ -21,7 +21,7 @@ #define CONFIG_APP_HEAP_SIZE 8192 #ifdef CONFIG_NO_OPTIMIZATIONS -#define CONFIG_MAIN_THREAD_STACK_SIZE 8192 +#define CONFIG_MAIN_THREAD_STACK_SIZE 8192 * 4 #else #define CONFIG_MAIN_THREAD_STACK_SIZE 4096 #endif @@ -126,23 +126,19 @@ iwasm_main(void *arg1, void *arg2, void *arg3) #error "memory allocation scheme is not defined." #endif -// Use the address pool in wasm-micro-runtime -// #if WASM_ENABLE_LIBC_WASI != 0 -// #define HUMAN_READABLE_ADDRESS "192.0.2.10\\24" -// libc_wasi_parse_context_t wasi_parse_ctx; -// memset(&wasi_parse_ctx, 0, sizeof(wasi_parse_ctx)); +/* Use the address pool in wasm-micro-runtime */ +#if WASM_ENABLE_LIBC_WASI != 0 +#define ADDRESS_POOL_SIZE 1 + const char *addr_pool[ADDRESS_POOL_SIZE] = { + "192.0.2.10\\24", + // Add more addresses here if needed + }; -// libc_wasi_parse_result_t result = libc_wasi_parse(HUMAN_READABLE_ADDRESS, &wasi_parse_ctx); -// switch (result) { -// case LIBC_WASI_PARSE_RESULT_OK: -// continue; -// case LIBC_WASI_PARSE_RESULT_NEED_HELP: -// return; -// case LIBC_WASI_PARSE_RESULT_BAD_PARAM: -// return; -// } -// libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx); -// #endif + /*wasm_runtime_set_wasi_addr_pool(wasm_module_t module, + const char *addr_pool[], uint32_t addr_pool_size); */ + wasm_runtime_set_wasi_addr_pool(wasm_module, addr_pool, + ADDRESS_POOL_SIZE); +#endif /* initialize runtime environment */ if (!wasm_runtime_full_init(&init_args)) { @@ -176,7 +172,7 @@ iwasm_main(void *arg1, void *arg2, void *arg3) /* invoke the main function */ app_instance_main(wasm_module_inst); -// +// TODO: adapt app_instance_main to have a ret code. // #if WASM_ENABLE_LIBC_WASI != 0 // if (ret == 0) { // /* propagate wasi exit code. */ diff --git a/product-mini/platforms/zephyr/simple-http/src/test_wasm.h b/product-mini/platforms/zephyr/simple-http/src/test_wasm.h deleted file mode 100644 index a729cadef..000000000 --- a/product-mini/platforms/zephyr/simple-http/src/test_wasm.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -/** - * The byte array buffer is the file content of a test wasm binary file, - * which is compiled by wasi-sdk toolchain from C source file of: - * product-mini/app-samples/hello-world/main.c. - */ -unsigned char __aligned(4) wasm_test_file[] = { - 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x03, 0x60, - 0x01, 0x7F, 0x01, 0x7F, 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F, 0x60, 0x01, - 0x7F, 0x00, 0x02, 0x31, 0x04, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x70, 0x75, - 0x74, 0x73, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x6D, 0x61, 0x6C, - 0x6C, 0x6F, 0x63, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x70, 0x72, - 0x69, 0x6E, 0x74, 0x66, 0x00, 0x01, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x66, - 0x72, 0x65, 0x65, 0x00, 0x02, 0x03, 0x02, 0x01, 0x01, 0x04, 0x05, 0x01, - 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x13, 0x03, - 0x7F, 0x01, 0x41, 0xC0, 0x28, 0x0B, 0x7F, 0x00, 0x41, 0xBA, 0x08, 0x0B, - 0x7F, 0x00, 0x41, 0xC0, 0x28, 0x0B, 0x07, 0x2C, 0x04, 0x06, 0x6D, 0x65, - 0x6D, 0x6F, 0x72, 0x79, 0x02, 0x00, 0x0A, 0x5F, 0x5F, 0x64, 0x61, 0x74, - 0x61, 0x5F, 0x65, 0x6E, 0x64, 0x03, 0x01, 0x0B, 0x5F, 0x5F, 0x68, 0x65, - 0x61, 0x70, 0x5F, 0x62, 0x61, 0x73, 0x65, 0x03, 0x02, 0x04, 0x6D, 0x61, - 0x69, 0x6E, 0x00, 0x04, 0x0A, 0xB2, 0x01, 0x01, 0xAF, 0x01, 0x01, 0x03, - 0x7F, 0x23, 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x20, 0x6B, 0x22, 0x02, - 0x24, 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x9B, 0x88, 0x80, 0x80, 0x00, - 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x02, 0x40, 0x02, 0x40, 0x41, - 0x80, 0x08, 0x10, 0x81, 0x80, 0x80, 0x80, 0x00, 0x22, 0x03, 0x0D, 0x00, - 0x41, 0xA8, 0x88, 0x80, 0x80, 0x00, 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, - 0x1A, 0x41, 0x7F, 0x21, 0x04, 0x0C, 0x01, 0x0B, 0x20, 0x02, 0x20, 0x03, - 0x36, 0x02, 0x10, 0x41, 0x80, 0x88, 0x80, 0x80, 0x00, 0x20, 0x02, 0x41, - 0x10, 0x6A, 0x10, 0x82, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, 0x00, 0x21, - 0x04, 0x20, 0x03, 0x41, 0x04, 0x6A, 0x41, 0x00, 0x2F, 0x00, 0x91, 0x88, - 0x80, 0x80, 0x00, 0x3B, 0x00, 0x00, 0x20, 0x03, 0x41, 0x00, 0x28, 0x00, - 0x8D, 0x88, 0x80, 0x80, 0x00, 0x36, 0x00, 0x00, 0x20, 0x02, 0x20, 0x03, - 0x36, 0x02, 0x00, 0x41, 0x93, 0x88, 0x80, 0x80, 0x00, 0x20, 0x02, 0x10, - 0x82, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x20, 0x03, 0x10, 0x83, 0x80, 0x80, - 0x80, 0x00, 0x0B, 0x20, 0x02, 0x41, 0x20, 0x6A, 0x24, 0x80, 0x80, 0x80, - 0x80, 0x00, 0x20, 0x04, 0x0B, 0x0B, 0x41, 0x01, 0x00, 0x41, 0x80, 0x08, - 0x0B, 0x3A, 0x62, 0x75, 0x66, 0x20, 0x70, 0x74, 0x72, 0x3A, 0x20, 0x25, - 0x70, 0x0A, 0x00, 0x31, 0x32, 0x33, 0x34, 0x0A, 0x00, 0x62, 0x75, 0x66, - 0x3A, 0x20, 0x25, 0x73, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, - 0x6F, 0x72, 0x6C, 0x64, 0x21, 0x00, 0x6D, 0x61, 0x6C, 0x6C, 0x6F, 0x63, - 0x20, 0x62, 0x75, 0x66, 0x20, 0x66, 0x61, 0x69, 0x6C, 0x65, 0x64, 0x00 -}; diff --git a/product-mini/platforms/zephyr/simple-http/src/test_wasm_riscv64.h b/product-mini/platforms/zephyr/simple-http/src/test_wasm_riscv64.h deleted file mode 100644 index 1b45211d7..000000000 --- a/product-mini/platforms/zephyr/simple-http/src/test_wasm_riscv64.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -unsigned char __aligned(4) wasm_test_file[] = { - 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x03, 0x60, - 0x01, 0x7F, 0x01, 0x7F, 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F, 0x60, 0x01, - 0x7F, 0x00, 0x02, 0x31, 0x04, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x70, 0x75, - 0x74, 0x73, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x6D, 0x61, 0x6C, - 0x6C, 0x6F, 0x63, 0x00, 0x00, 0x03, 0x65, 0x6E, 0x76, 0x06, 0x70, 0x72, - 0x69, 0x6E, 0x74, 0x66, 0x00, 0x01, 0x03, 0x65, 0x6E, 0x76, 0x04, 0x66, - 0x72, 0x65, 0x65, 0x00, 0x02, 0x03, 0x02, 0x01, 0x01, 0x04, 0x05, 0x01, - 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x01, 0x06, 0x12, 0x03, - 0x7F, 0x01, 0x41, 0xC0, 0x01, 0x0B, 0x7F, 0x00, 0x41, 0x3A, 0x0B, 0x7F, - 0x00, 0x41, 0xC0, 0x01, 0x0B, 0x07, 0x2C, 0x04, 0x06, 0x6D, 0x65, 0x6D, - 0x6F, 0x72, 0x79, 0x02, 0x00, 0x04, 0x6D, 0x61, 0x69, 0x6E, 0x00, 0x04, - 0x0A, 0x5F, 0x5F, 0x64, 0x61, 0x74, 0x61, 0x5F, 0x65, 0x6E, 0x64, 0x03, - 0x01, 0x0B, 0x5F, 0x5F, 0x68, 0x65, 0x61, 0x70, 0x5F, 0x62, 0x61, 0x73, - 0x65, 0x03, 0x02, 0x0A, 0xB1, 0x01, 0x01, 0xAE, 0x01, 0x01, 0x03, 0x7F, - 0x23, 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x20, 0x6B, 0x22, 0x02, 0x24, - 0x80, 0x80, 0x80, 0x80, 0x00, 0x41, 0x9B, 0x80, 0x80, 0x80, 0x00, 0x10, - 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x02, 0x40, 0x02, 0x40, 0x41, 0x10, - 0x10, 0x81, 0x80, 0x80, 0x80, 0x00, 0x22, 0x03, 0x0D, 0x00, 0x41, 0xA8, - 0x80, 0x80, 0x80, 0x00, 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, - 0x7F, 0x21, 0x04, 0x0C, 0x01, 0x0B, 0x20, 0x02, 0x20, 0x03, 0x36, 0x02, - 0x10, 0x41, 0x80, 0x80, 0x80, 0x80, 0x00, 0x20, 0x02, 0x41, 0x10, 0x6A, - 0x10, 0x82, 0x80, 0x80, 0x80, 0x00, 0x1A, 0x41, 0x00, 0x21, 0x04, 0x20, - 0x03, 0x41, 0x04, 0x6A, 0x41, 0x00, 0x2F, 0x00, 0x91, 0x80, 0x80, 0x80, - 0x00, 0x3B, 0x00, 0x00, 0x20, 0x03, 0x41, 0x00, 0x28, 0x00, 0x8D, 0x80, - 0x80, 0x80, 0x00, 0x36, 0x00, 0x00, 0x20, 0x02, 0x20, 0x03, 0x36, 0x02, - 0x00, 0x41, 0x93, 0x80, 0x80, 0x80, 0x00, 0x20, 0x02, 0x10, 0x82, 0x80, - 0x80, 0x80, 0x00, 0x1A, 0x20, 0x03, 0x10, 0x83, 0x80, 0x80, 0x80, 0x00, - 0x0B, 0x20, 0x02, 0x41, 0x20, 0x6A, 0x24, 0x80, 0x80, 0x80, 0x80, 0x00, - 0x20, 0x04, 0x0B, 0x0B, 0x40, 0x01, 0x00, 0x41, 0x00, 0x0B, 0x3A, 0x62, - 0x75, 0x66, 0x20, 0x70, 0x74, 0x72, 0x3A, 0x20, 0x25, 0x70, 0x0A, 0x00, - 0x31, 0x32, 0x33, 0x34, 0x0A, 0x00, 0x62, 0x75, 0x66, 0x3A, 0x20, 0x25, - 0x73, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, - 0x64, 0x21, 0x00, 0x6D, 0x61, 0x6C, 0x6C, 0x6F, 0x63, 0x20, 0x62, 0x75, - 0x66, 0x20, 0x66, 0x61, 0x69, 0x6C, 0x65, 0x64, 0x00 -}; diff --git a/product-mini/platforms/zephyr/simple-http/to_c_header.py b/product-mini/platforms/zephyr/simple-http/to_c_header.py index 11f10b7cf..642f01344 100644 --- a/product-mini/platforms/zephyr/simple-http/to_c_header.py +++ b/product-mini/platforms/zephyr/simple-http/to_c_header.py @@ -1,9 +1,7 @@ # Python script to convert wasm file to byte array in a .h file import os - CWD = os.getcwd() - CMAKE_CURRENT_BINARY_DIR = os.getenv('CMAKE_CURRENT_BINARY_DIR', CWD) CMAKE_CURRENT_SOURCE_DIR = os.getenv('CMAKE_CURRENT_SOURCE_DIR', f'{CWD}/../src') diff --git a/product-mini/platforms/zephyr/simple-http/wasm-apps/http_get.c b/product-mini/platforms/zephyr/simple-http/wasm-apps/http_get.c index ca377189b..0e518f72e 100644 --- a/product-mini/platforms/zephyr/simple-http/wasm-apps/http_get.c +++ b/product-mini/platforms/zephyr/simple-http/wasm-apps/http_get.c @@ -7,22 +7,16 @@ #include #include -#if !defined(__ZEPHYR__) || defined(CONFIG_POSIX_API) - #include #include #include #include -#include #include -#else -#include -#include -#endif +#include -// #ifdef __wasi__ -// #include -// #endif +#ifdef __wasi__ +#include "inc/wasi_socket_ext.h" +#endif /* HTTP server to connect to */ @@ -40,65 +34,25 @@ static char response[1024]; -void dump_addrinfo(const struct addrinfo *ai) -{ - printf("addrinfo @%p: ai_family=%d, ai_socktype=%d, ai_protocol=%d, " - "sa_family=%d, sin_port=%x\n", - ai, ai->ai_family, ai->ai_socktype, ai->ai_protocol, - ai->ai_addr->sa_family, - ((struct sockaddr_in *)ai->ai_addr)->sin_port); -} - int main(void) { - static struct addrinfo hints; - struct addrinfo *res; int st, sock; + struct sockaddr_in addr; printf("Preparing HTTP GET request for http://" HTTP_HOST ":" HTTP_PORT HTTP_PATH "\n"); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - st = getaddrinfo(HTTP_HOST, HTTP_PORT, &hints, &res); - printf("getaddrinfo status: %d\n", st); + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons(atoi(HTTP_PORT)); + inet_pton(AF_INET, HTTP_HOST, &(addr.sin_addr)); - if (st != 0) { - printf("Unable to resolve address, quitting\n"); - return 0; - } - - dump_addrinfo(res); - - //sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); CHECK(sock); printf("sock = %d\n", sock); - - CHECK(connect(sock, res->ai_addr, res->ai_addrlen)); - // int rc = 0; - // for(int i = 0; i < 10; i++){ - // rc = connect(sock, res->ai_addr, res->ai_addrlen); - // if (rc == 0) { - // break; - // } - // else{ - // printf("[Debug] Connect try %d: status %d\n", i+1, errno); - // close(sock); - // k_sleep(K_MSEC(100)); // 10 mil seconds - // sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - // if (sock < 0) { - // printf("[Error] Unable to create socket, exiting..."); - // exit(1); - // } - // } - // k_sleep(K_MSEC(1000 * 5)); // 5 seconds - // } - // if(rc){ - // printf("[Error] Unable to Connect exiting..."); - // exit(1); - // } + + CHECK(connect(sock, (struct sockaddr*)&addr, sizeof(addr))); CHECK(send(sock, REQUEST, SSTRLEN(REQUEST), 0)); diff --git a/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/wasi_socket_ext.c b/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/wasi_socket_ext.c new file mode 100644 index 000000000..1172d0a77 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/wasi_socket_ext.c @@ -0,0 +1,1014 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define HANDLE_ERROR(error) \ + if (error != __WASI_ERRNO_SUCCESS) { \ + errno = error; \ + return -1; \ + } + +static void +ipv4_addr_to_wasi_ip4_addr(uint32_t addr_num, __wasi_addr_ip4_t *out) +{ + addr_num = ntohl(addr_num); + out->n0 = (addr_num & 0xFF000000) >> 24; + out->n1 = (addr_num & 0x00FF0000) >> 16; + out->n2 = (addr_num & 0x0000FF00) >> 8; + out->n3 = (addr_num & 0x000000FF); +} + +/* addr_num and port are in network order */ +static void +ipv4_addr_to_wasi_addr(uint32_t addr_num, uint16_t port, __wasi_addr_t *out) +{ + out->kind = IPv4; + out->addr.ip4.port = ntohs(port); + ipv4_addr_to_wasi_ip4_addr(addr_num, &(out->addr.ip4.addr)); +} + +static void +ipv6_addr_to_wasi_ipv6_addr(uint16_t *addr, __wasi_addr_ip6_t *out) +{ + out->n0 = ntohs(addr[0]); + out->n1 = ntohs(addr[1]); + out->n2 = ntohs(addr[2]); + out->n3 = ntohs(addr[3]); + out->h0 = ntohs(addr[4]); + out->h1 = ntohs(addr[5]); + out->h2 = ntohs(addr[6]); + out->h3 = ntohs(addr[7]); +} + +static void +ipv6_addr_to_wasi_addr(uint16_t *addr, uint16_t port, __wasi_addr_t *out) +{ + out->kind = IPv6; + out->addr.ip6.port = ntohs(port); + ipv6_addr_to_wasi_ipv6_addr(addr, &(out->addr.ip6.addr)); +} + +static __wasi_errno_t +sockaddr_to_wasi_addr(const struct sockaddr *sock_addr, socklen_t addrlen, + __wasi_addr_t *wasi_addr) +{ + __wasi_errno_t ret = __WASI_ERRNO_SUCCESS; + if (AF_INET == sock_addr->sa_family) { + assert(sizeof(struct sockaddr_in) <= addrlen); + + ipv4_addr_to_wasi_addr( + ((struct sockaddr_in *)sock_addr)->sin_addr.s_addr, + ((struct sockaddr_in *)sock_addr)->sin_port, wasi_addr); + } + else if (AF_INET6 == sock_addr->sa_family) { + assert(sizeof(struct sockaddr_in6) <= addrlen); + ipv6_addr_to_wasi_addr( + (uint16_t *)((struct sockaddr_in6 *)sock_addr)->sin6_addr.s6_addr, + ((struct sockaddr_in6 *)sock_addr)->sin6_port, wasi_addr); + } + else { + ret = __WASI_ERRNO_AFNOSUPPORT; + } + + return ret; +} + +static __wasi_errno_t +wasi_addr_to_sockaddr(const __wasi_addr_t *wasi_addr, + struct sockaddr *sock_addr, socklen_t *addrlen) +{ + switch (wasi_addr->kind) { + case IPv4: + { + struct sockaddr_in sock_addr_in; + uint32_t s_addr; + + memset(&sock_addr_in, 0, sizeof(sock_addr_in)); + + s_addr = (wasi_addr->addr.ip4.addr.n0 << 24) + | (wasi_addr->addr.ip4.addr.n1 << 16) + | (wasi_addr->addr.ip4.addr.n2 << 8) + | wasi_addr->addr.ip4.addr.n3; + + sock_addr_in.sin_family = AF_INET; + sock_addr_in.sin_addr.s_addr = htonl(s_addr); + sock_addr_in.sin_port = htons(wasi_addr->addr.ip4.port); + memcpy(sock_addr, &sock_addr_in, sizeof(sock_addr_in)); + + *addrlen = sizeof(sock_addr_in); + break; + } + case IPv6: + { + struct sockaddr_in6 sock_addr_in6; + + memset(&sock_addr_in6, 0, sizeof(sock_addr_in6)); + + uint16_t *addr_buf = (uint16_t *)sock_addr_in6.sin6_addr.s6_addr; + + addr_buf[0] = htons(wasi_addr->addr.ip6.addr.n0); + addr_buf[1] = htons(wasi_addr->addr.ip6.addr.n1); + addr_buf[2] = htons(wasi_addr->addr.ip6.addr.n2); + addr_buf[3] = htons(wasi_addr->addr.ip6.addr.n3); + addr_buf[4] = htons(wasi_addr->addr.ip6.addr.h0); + addr_buf[5] = htons(wasi_addr->addr.ip6.addr.h1); + addr_buf[6] = htons(wasi_addr->addr.ip6.addr.h2); + addr_buf[7] = htons(wasi_addr->addr.ip6.addr.h3); + + sock_addr_in6.sin6_family = AF_INET6; + sock_addr_in6.sin6_port = htons(wasi_addr->addr.ip6.port); + memcpy(sock_addr, &sock_addr_in6, sizeof(sock_addr_in6)); + + *addrlen = sizeof(sock_addr_in6); + break; + } + default: + return __WASI_ERRNO_AFNOSUPPORT; + } + return __WASI_ERRNO_SUCCESS; +} + +int +accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) +{ + __wasi_addr_t wasi_addr; + __wasi_fd_t new_sockfd; + __wasi_errno_t error; + + memset(&wasi_addr, 0, sizeof(wasi_addr)); + + error = __wasi_sock_accept(sockfd, 0, &new_sockfd); + HANDLE_ERROR(error) + + if (getpeername(new_sockfd, addr, addrlen) == -1) { + return -1; + } + + return new_sockfd; +} + +int +bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) +{ + __wasi_addr_t wasi_addr; + __wasi_errno_t error; + + memset(&wasi_addr, 0, sizeof(wasi_addr)); + + error = sockaddr_to_wasi_addr(addr, addrlen, &wasi_addr); + HANDLE_ERROR(error) + + error = __wasi_sock_bind(sockfd, &wasi_addr); + HANDLE_ERROR(error) + + return __WASI_ERRNO_SUCCESS; +} + +int +connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) +{ + __wasi_addr_t wasi_addr; + __wasi_errno_t error; + + memset(&wasi_addr, 0, sizeof(wasi_addr)); + + if (NULL == addr) { + HANDLE_ERROR(__WASI_ERRNO_INVAL) + } + + error = sockaddr_to_wasi_addr(addr, addrlen, &wasi_addr); + HANDLE_ERROR(error) + + error = __wasi_sock_connect(sockfd, &wasi_addr); + HANDLE_ERROR(error) + + return __WASI_ERRNO_SUCCESS; +} + +int +listen(int sockfd, int backlog) +{ + __wasi_errno_t error = __wasi_sock_listen(sockfd, backlog); + HANDLE_ERROR(error) + return __WASI_ERRNO_SUCCESS; +} + +ssize_t +recvmsg(int sockfd, struct msghdr *msg, int flags) +{ + // Prepare input parameters. + __wasi_iovec_t *ri_data = NULL; + size_t i = 0; + size_t ro_datalen = 0; + __wasi_roflags_t ro_flags = 0; + + if (NULL == msg) { + HANDLE_ERROR(__WASI_ERRNO_INVAL) + } + + // Validate flags. + if (flags != 0) { + HANDLE_ERROR(__WASI_ERRNO_NOPROTOOPT) + } + + // __wasi_ciovec_t -> struct iovec + if (!(ri_data = (__wasi_iovec_t *)malloc(sizeof(__wasi_iovec_t) + * msg->msg_iovlen))) { + HANDLE_ERROR(__WASI_ERRNO_NOMEM) + } + + for (i = 0; i < msg->msg_iovlen; i++) { + ri_data[i].buf = (uint8_t *)msg->msg_iov[i].iov_base; + ri_data[i].buf_len = msg->msg_iov[i].iov_len; + } + + // Perform system call. + __wasi_errno_t error = __wasi_sock_recv(sockfd, ri_data, msg->msg_iovlen, 0, + &ro_datalen, &ro_flags); + free(ri_data); + HANDLE_ERROR(error) + + return ro_datalen; +} + +ssize_t +sendmsg(int sockfd, const struct msghdr *msg, int flags) +{ + // Prepare input parameters. + __wasi_ciovec_t *si_data = NULL; + size_t so_datalen = 0; + size_t i = 0; + + if (NULL == msg) { + HANDLE_ERROR(__WASI_ERRNO_INVAL) + } + + // This implementation does not support any flags. + if (flags != 0) { + HANDLE_ERROR(__WASI_ERRNO_NOPROTOOPT) + } + + // struct iovec -> __wasi_ciovec_t + if (!(si_data = (__wasi_ciovec_t *)malloc(sizeof(__wasi_ciovec_t) + * msg->msg_iovlen))) { + HANDLE_ERROR(__WASI_ERRNO_NOMEM) + } + + for (i = 0; i < msg->msg_iovlen; i++) { + si_data[i].buf = (uint8_t *)msg->msg_iov[i].iov_base; + si_data[i].buf_len = msg->msg_iov[i].iov_len; + } + + // Perform system call. + __wasi_errno_t error = + __wasi_sock_send(sockfd, si_data, msg->msg_iovlen, 0, &so_datalen); + free(si_data); + HANDLE_ERROR(error) + + return so_datalen; +} + +ssize_t +sendto(int sockfd, const void *buf, size_t len, int flags, + const struct sockaddr *dest_addr, socklen_t addrlen) +{ + // Prepare input parameters. + __wasi_ciovec_t iov = { .buf = (uint8_t *)buf, .buf_len = len }; + uint32_t so_datalen = 0; + __wasi_addr_t wasi_addr; + __wasi_errno_t error; + size_t si_data_len = 1; + __wasi_siflags_t si_flags = 0; + + // This implementation does not support any flags. + if (flags != 0) { + HANDLE_ERROR(__WASI_ERRNO_NOPROTOOPT) + } + + error = sockaddr_to_wasi_addr(dest_addr, addrlen, &wasi_addr); + HANDLE_ERROR(error); + + // Perform system call. + error = __wasi_sock_send_to(sockfd, &iov, si_data_len, si_flags, &wasi_addr, + &so_datalen); + HANDLE_ERROR(error) + + return so_datalen; +} + +ssize_t +recvfrom(int sockfd, void *buf, size_t len, int flags, + struct sockaddr *src_addr, socklen_t *addrlen) +{ + // Prepare input parameters. + __wasi_ciovec_t iov = { .buf = (uint8_t *)buf, .buf_len = len }; + uint32_t so_datalen = 0; + __wasi_addr_t wasi_addr; + __wasi_errno_t error; + size_t si_data_len = 1; + __wasi_siflags_t si_flags = 0; + + // This implementation does not support any flags. + if (flags != 0) { + HANDLE_ERROR(__WASI_ERRNO_NOPROTOOPT) + } + + if (!src_addr) { + return recv(sockfd, buf, len, flags); + } + + // Perform system call. + error = __wasi_sock_recv_from(sockfd, &iov, si_data_len, si_flags, + &wasi_addr, &so_datalen); + HANDLE_ERROR(error); + + error = wasi_addr_to_sockaddr(&wasi_addr, src_addr, addrlen); + HANDLE_ERROR(error); + + return so_datalen; +} + +int +socket(int domain, int type, int protocol) +{ + // the stub of address pool fd + __wasi_fd_t poolfd = -1; + __wasi_fd_t sockfd; + __wasi_errno_t error; + __wasi_address_family_t af; + __wasi_sock_type_t socktype; + + if (AF_INET == domain) { + af = INET4; + } + else if (AF_INET6 == domain) { + af = INET6; + } + else { + return __WASI_ERRNO_NOPROTOOPT; + } + + if (SOCK_DGRAM == type) { + socktype = SOCKET_DGRAM; + } + else if (SOCK_STREAM == type) { + socktype = SOCKET_STREAM; + } + else { + return __WASI_ERRNO_NOPROTOOPT; + } + + error = __wasi_sock_open(poolfd, af, socktype, &sockfd); + HANDLE_ERROR(error) + + return sockfd; +} + +int +getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen) +{ + __wasi_addr_t wasi_addr; + __wasi_errno_t error; + + memset(&wasi_addr, 0, sizeof(wasi_addr)); + + error = __wasi_sock_addr_local(sockfd, &wasi_addr); + HANDLE_ERROR(error) + + error = wasi_addr_to_sockaddr(&wasi_addr, addr, addrlen); + HANDLE_ERROR(error) + + return __WASI_ERRNO_SUCCESS; +} + +int +getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) +{ + __wasi_addr_t wasi_addr; + __wasi_errno_t error; + + memset(&wasi_addr, 0, sizeof(wasi_addr)); + + error = __wasi_sock_addr_remote(sockfd, &wasi_addr); + HANDLE_ERROR(error) + + error = wasi_addr_to_sockaddr(&wasi_addr, addr, addrlen); + HANDLE_ERROR(error) + + return __WASI_ERRNO_SUCCESS; +} + +struct aibuf { + struct addrinfo ai; + union sa { + struct sockaddr_in sin; + struct sockaddr_in6 sin6; + } sa; +}; + +static __wasi_errno_t +addrinfo_hints_to_wasi_hints(const struct addrinfo *hints, + __wasi_addr_info_hints_t *wasi_hints) +{ + if (hints) { + wasi_hints->hints_enabled = 1; + + switch (hints->ai_family) { + case AF_INET: + wasi_hints->family = INET4; + break; + case AF_INET6: + wasi_hints->family = INET6; + break; + case AF_UNSPEC: + wasi_hints->family = INET_UNSPEC; + break; + default: + return __WASI_ERRNO_AFNOSUPPORT; + } + switch (hints->ai_socktype) { + case SOCK_STREAM: + wasi_hints->type = SOCKET_STREAM; + break; + case SOCK_DGRAM: + wasi_hints->type = SOCKET_DGRAM; + break; + case 0: + wasi_hints->type = SOCKET_ANY; + default: + return __WASI_ERRNO_NOTSUP; + } + + if (hints->ai_protocol != 0) { + return __WASI_ERRNO_NOTSUP; + } + + if (hints->ai_flags != 0) { + return __WASI_ERRNO_NOTSUP; + } + } + else { + wasi_hints->hints_enabled = 0; + } + + return __WASI_ERRNO_SUCCESS; +} + +static __wasi_errno_t +wasi_addr_info_to_addr_info(const __wasi_addr_info_t *addr_info, + struct addrinfo *ai) +{ + ai->ai_socktype = + addr_info->type == SOCKET_DGRAM ? SOCK_DGRAM : SOCK_STREAM; + ai->ai_protocol = 0; + ai->ai_canonname = NULL; + + if (addr_info->addr.kind == IPv4) { + ai->ai_family = AF_INET; + ai->ai_addrlen = sizeof(struct sockaddr_in); + } + else { + ai->ai_family = AF_INET6; + ai->ai_addrlen = sizeof(struct sockaddr_in6); + } + + return wasi_addr_to_sockaddr(&addr_info->addr, ai->ai_addr, + &ai->ai_addrlen); // TODO err handling +} + +int +getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, + struct addrinfo **res) +{ + __wasi_addr_info_hints_t wasi_hints; + __wasi_addr_info_t *addr_info = NULL; + __wasi_size_t addr_info_size, i; + __wasi_size_t max_info_size = 16; + __wasi_errno_t error; + struct aibuf *aibuf_res; + + error = addrinfo_hints_to_wasi_hints(hints, &wasi_hints); + HANDLE_ERROR(error) + + do { + if (addr_info) + free(addr_info); + + addr_info_size = max_info_size; + addr_info = (__wasi_addr_info_t *)malloc(addr_info_size + * sizeof(__wasi_addr_info_t)); + + if (!addr_info) { + HANDLE_ERROR(__WASI_ERRNO_NOMEM) + } + + error = __wasi_sock_addr_resolve(node, service == NULL ? "" : service, + &wasi_hints, addr_info, addr_info_size, + &max_info_size); + if (error != __WASI_ERRNO_SUCCESS) { + free(addr_info); + HANDLE_ERROR(error); + } + } while (max_info_size > addr_info_size); + + if (addr_info_size == 0) { + free(addr_info); + *res = NULL; + return __WASI_ERRNO_SUCCESS; + } + + aibuf_res = + (struct aibuf *)calloc(1, addr_info_size * sizeof(struct aibuf)); + if (!aibuf_res) { + free(addr_info); + HANDLE_ERROR(__WASI_ERRNO_NOMEM) + } + + *res = &aibuf_res[0].ai; + + if (addr_info_size) { + addr_info_size = max_info_size; + } + + for (i = 0; i < addr_info_size; i++) { + struct addrinfo *ai = &aibuf_res[i].ai; + ai->ai_addr = (struct sockaddr *)&aibuf_res[i].sa; + + error = wasi_addr_info_to_addr_info(&addr_info[i], ai); + if (error != __WASI_ERRNO_SUCCESS) { + free(addr_info); + free(aibuf_res); + HANDLE_ERROR(error) + } + ai->ai_next = i == addr_info_size - 1 ? NULL : &aibuf_res[i + 1].ai; + } + + free(addr_info); + + return __WASI_ERRNO_SUCCESS; +} + +void +freeaddrinfo(struct addrinfo *res) +{ + /* res is a pointer to a first field in the first element + * of aibuf array allocated in getaddrinfo, therefore this call + * frees the memory of the entire array. */ + free(res); +} + +static struct timeval +time_us_to_timeval(uint64_t time_us) +{ + struct timeval tv; + tv.tv_sec = time_us / 1000000UL; + tv.tv_usec = time_us % 1000000UL; + return tv; +} + +static uint64_t +timeval_to_time_us(struct timeval tv) +{ + return (tv.tv_sec * 1000000UL) + tv.tv_usec; +} + +static int +get_sol_socket_option(int sockfd, int optname, void *__restrict optval, + socklen_t *__restrict optlen) +{ + __wasi_errno_t error; + uint64_t timeout_us; + bool is_linger_enabled; + int linger_s; + + switch (optname) { + case SO_RCVTIMEO: + assert(*optlen == sizeof(struct timeval)); + error = __wasi_sock_get_recv_timeout(sockfd, &timeout_us); + HANDLE_ERROR(error); + *(struct timeval *)optval = time_us_to_timeval(timeout_us); + return error; + case SO_SNDTIMEO: + assert(*optlen == sizeof(struct timeval)); + error = __wasi_sock_get_send_timeout(sockfd, &timeout_us); + HANDLE_ERROR(error); + *(struct timeval *)optval = time_us_to_timeval(timeout_us); + return error; + case SO_SNDBUF: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_send_buf_size(sockfd, (size_t *)optval); + HANDLE_ERROR(error); + return error; + case SO_RCVBUF: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_recv_buf_size(sockfd, (size_t *)optval); + HANDLE_ERROR(error); + return error; + case SO_KEEPALIVE: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_keep_alive(sockfd, (bool *)optval); + HANDLE_ERROR(error); + return error; + case SO_REUSEADDR: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_reuse_addr(sockfd, (bool *)optval); + HANDLE_ERROR(error); + return error; + case SO_REUSEPORT: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_reuse_port(sockfd, (bool *)optval); + HANDLE_ERROR(error); + return error; + case SO_LINGER: + assert(*optlen == sizeof(struct linger)); + error = + __wasi_sock_get_linger(sockfd, &is_linger_enabled, &linger_s); + HANDLE_ERROR(error); + ((struct linger *)optval)->l_onoff = (int)is_linger_enabled; + ((struct linger *)optval)->l_linger = linger_s; + return error; + case SO_BROADCAST: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_broadcast(sockfd, (bool *)optval); + HANDLE_ERROR(error); + return error; + default: + error = __WASI_ERRNO_NOTSUP; + HANDLE_ERROR(error); + return error; + } +} + +static int +get_ipproto_tcp_option(int sockfd, int optname, void *__restrict optval, + socklen_t *__restrict optlen) +{ + __wasi_errno_t error; + switch (optname) { + case TCP_KEEPIDLE: + assert(*optlen == sizeof(uint32_t)); + error = __wasi_sock_get_tcp_keep_idle(sockfd, (uint32_t *)optval); + HANDLE_ERROR(error); + return error; + case TCP_KEEPINTVL: + assert(*optlen == sizeof(uint32_t)); + error = __wasi_sock_get_tcp_keep_intvl(sockfd, (uint32_t *)optval); + HANDLE_ERROR(error); + return error; + case TCP_FASTOPEN_CONNECT: + assert(*optlen == sizeof(int)); + error = + __wasi_sock_get_tcp_fastopen_connect(sockfd, (bool *)optval); + HANDLE_ERROR(error); + return error; + case TCP_NODELAY: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_tcp_no_delay(sockfd, (bool *)optval); + HANDLE_ERROR(error); + return error; + case TCP_QUICKACK: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_tcp_quick_ack(sockfd, (bool *)optval); + HANDLE_ERROR(error); + return error; + default: + error = __WASI_ERRNO_NOTSUP; + HANDLE_ERROR(error); + return error; + } +} + +static int +get_ipproto_ip_option(int sockfd, int optname, void *__restrict optval, + socklen_t *__restrict optlen) +{ + __wasi_errno_t error; + + switch (optname) { + case IP_MULTICAST_LOOP: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_ip_multicast_loop(sockfd, false, + (bool *)optval); + HANDLE_ERROR(error); + return error; + case IP_TTL: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_ip_ttl(sockfd, (uint8_t *)optval); + HANDLE_ERROR(error); + return error; + case IP_MULTICAST_TTL: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_ip_multicast_ttl(sockfd, (uint8_t *)optval); + HANDLE_ERROR(error); + return error; + default: + error = __WASI_ERRNO_NOTSUP; + HANDLE_ERROR(error); + return error; + } +} + +static int +get_ipproto_ipv6_option(int sockfd, int optname, void *__restrict optval, + socklen_t *__restrict optlen) +{ + __wasi_errno_t error; + + switch (optname) { + case IPV6_V6ONLY: + assert(*optlen == sizeof(int)); + error = __wasi_sock_get_ipv6_only(sockfd, (bool *)optval); + HANDLE_ERROR(error); + return error; + case IPV6_MULTICAST_LOOP: + assert(*optlen == sizeof(int)); + error = + __wasi_sock_get_ip_multicast_loop(sockfd, true, (bool *)optval); + HANDLE_ERROR(error); + return error; + default: + error = __WASI_ERRNO_NOTSUP; + HANDLE_ERROR(error); + return error; + } +} + +int +getsockopt(int sockfd, int level, int optname, void *__restrict optval, + socklen_t *__restrict optlen) +{ + __wasi_errno_t error; + + switch (level) { + case SOL_SOCKET: + return get_sol_socket_option(sockfd, optname, optval, optlen); + case IPPROTO_TCP: + return get_ipproto_tcp_option(sockfd, optname, optval, optlen); + case IPPROTO_IP: + return get_ipproto_ip_option(sockfd, optname, optval, optlen); + case IPPROTO_IPV6: + return get_ipproto_ipv6_option(sockfd, optname, optval, optlen); + default: + error = __WASI_ERRNO_NOTSUP; + HANDLE_ERROR(error); + return error; + } +} + +static int +set_sol_socket_option(int sockfd, int optname, const void *optval, + socklen_t optlen) +{ + __wasi_errno_t error; + uint64_t timeout_us; + + switch (optname) { + case SO_RCVTIMEO: + { + assert(optlen == sizeof(struct timeval)); + timeout_us = timeval_to_time_us(*(struct timeval *)optval); + error = __wasi_sock_set_recv_timeout(sockfd, timeout_us); + HANDLE_ERROR(error); + return error; + } + case SO_SNDTIMEO: + { + assert(optlen == sizeof(struct timeval)); + timeout_us = timeval_to_time_us(*(struct timeval *)optval); + error = __wasi_sock_set_send_timeout(sockfd, timeout_us); + HANDLE_ERROR(error); + return error; + } + case SO_SNDBUF: + { + assert(optlen == sizeof(int)); + error = __wasi_sock_set_send_buf_size(sockfd, *(size_t *)optval); + HANDLE_ERROR(error); + return error; + } + case SO_RCVBUF: + { + assert(optlen == sizeof(int)); + error = __wasi_sock_set_recv_buf_size(sockfd, *(size_t *)optval); + HANDLE_ERROR(error); + return error; + } + case SO_KEEPALIVE: + { + assert(optlen == sizeof(int)); + error = __wasi_sock_set_keep_alive(sockfd, *(bool *)optval); + HANDLE_ERROR(error); + return error; + } + case SO_REUSEADDR: + { + assert(optlen == sizeof(int)); + error = __wasi_sock_set_reuse_addr(sockfd, *(bool *)optval); + HANDLE_ERROR(error); + return error; + } + case SO_REUSEPORT: + { + assert(optlen == sizeof(int)); + error = __wasi_sock_set_reuse_port(sockfd, *(bool *)optval); + HANDLE_ERROR(error); + return error; + } + case SO_LINGER: + { + assert(optlen == sizeof(struct linger)); + struct linger *linger_opt = ((struct linger *)optval); + error = __wasi_sock_set_linger(sockfd, (bool)linger_opt->l_onoff, + linger_opt->l_linger); + HANDLE_ERROR(error); + return error; + } + case SO_BROADCAST: + { + assert(optlen == sizeof(int)); + error = __wasi_sock_set_broadcast(sockfd, *(bool *)optval); + HANDLE_ERROR(error); + return error; + } + default: + { + error = __WASI_ERRNO_NOTSUP; + HANDLE_ERROR(error); + return error; + } + } +} + +static int +set_ipproto_tcp_option(int sockfd, int optname, const void *optval, + socklen_t optlen) +{ + __wasi_errno_t error; + + switch (optname) { + case TCP_NODELAY: + assert(optlen == sizeof(int)); + error = __wasi_sock_set_tcp_no_delay(sockfd, *(bool *)optval); + HANDLE_ERROR(error); + return error; + case TCP_KEEPIDLE: + assert(optlen == sizeof(uint32_t)); + error = __wasi_sock_set_tcp_keep_idle(sockfd, *(uint32_t *)optval); + HANDLE_ERROR(error); + return error; + case TCP_KEEPINTVL: + assert(optlen == sizeof(uint32_t)); + error = __wasi_sock_set_tcp_keep_intvl(sockfd, *(uint32_t *)optval); + HANDLE_ERROR(error); + return error; + case TCP_FASTOPEN_CONNECT: + assert(optlen == sizeof(int)); + error = + __wasi_sock_set_tcp_fastopen_connect(sockfd, *(bool *)optval); + HANDLE_ERROR(error); + return error; + case TCP_QUICKACK: + assert(optlen == sizeof(int)); + error = __wasi_sock_set_tcp_quick_ack(sockfd, *(bool *)optval); + HANDLE_ERROR(error); + return error; + default: + error = __WASI_ERRNO_NOTSUP; + HANDLE_ERROR(error); + return error; + } +} + +static int +set_ipproto_ip_option(int sockfd, int optname, const void *optval, + socklen_t optlen) +{ + __wasi_errno_t error; + __wasi_addr_ip_t imr_multiaddr; + struct ip_mreq *ip_mreq_opt; + + switch (optname) { + case IP_MULTICAST_LOOP: + assert(optlen == sizeof(int)); + error = __wasi_sock_set_ip_multicast_loop(sockfd, false, + *(bool *)optval); + HANDLE_ERROR(error); + return error; + case IP_ADD_MEMBERSHIP: + assert(optlen == sizeof(struct ip_mreq)); + ip_mreq_opt = (struct ip_mreq *)optval; + imr_multiaddr.kind = IPv4; + ipv4_addr_to_wasi_ip4_addr(ip_mreq_opt->imr_multiaddr.s_addr, + &imr_multiaddr.addr.ip4); + error = __wasi_sock_set_ip_add_membership( + sockfd, &imr_multiaddr, ip_mreq_opt->imr_interface.s_addr); + HANDLE_ERROR(error); + return error; + case IP_DROP_MEMBERSHIP: + assert(optlen == sizeof(struct ip_mreq)); + ip_mreq_opt = (struct ip_mreq *)optval; + imr_multiaddr.kind = IPv4; + ipv4_addr_to_wasi_ip4_addr(ip_mreq_opt->imr_multiaddr.s_addr, + &imr_multiaddr.addr.ip4); + error = __wasi_sock_set_ip_drop_membership( + sockfd, &imr_multiaddr, ip_mreq_opt->imr_interface.s_addr); + HANDLE_ERROR(error); + return error; + case IP_TTL: + assert(optlen == sizeof(int)); + error = __wasi_sock_set_ip_ttl(sockfd, *(uint8_t *)optval); + HANDLE_ERROR(error); + return error; + case IP_MULTICAST_TTL: + assert(optlen == sizeof(int)); + error = + __wasi_sock_set_ip_multicast_ttl(sockfd, *(uint8_t *)optval); + HANDLE_ERROR(error); + return error; + default: + error = __WASI_ERRNO_NOTSUP; + HANDLE_ERROR(error); + return error; + } +} + +static int +set_ipproto_ipv6_option(int sockfd, int optname, const void *optval, + socklen_t optlen) +{ + __wasi_errno_t error; + struct ipv6_mreq *ipv6_mreq_opt; + __wasi_addr_ip_t imr_multiaddr; + + switch (optname) { + case IPV6_V6ONLY: + assert(optlen == sizeof(int)); + error = __wasi_sock_set_ipv6_only(sockfd, *(bool *)optval); + HANDLE_ERROR(error); + return error; + case IPV6_MULTICAST_LOOP: + assert(optlen == sizeof(int)); + error = __wasi_sock_set_ip_multicast_loop(sockfd, true, + *(bool *)optval); + HANDLE_ERROR(error); + return error; + case IPV6_JOIN_GROUP: + assert(optlen == sizeof(struct ipv6_mreq)); + ipv6_mreq_opt = (struct ipv6_mreq *)optval; + imr_multiaddr.kind = IPv6; + ipv6_addr_to_wasi_ipv6_addr( + (uint16_t *)ipv6_mreq_opt->ipv6mr_multiaddr.s6_addr, + &imr_multiaddr.addr.ip6); + error = __wasi_sock_set_ip_add_membership( + sockfd, &imr_multiaddr, ipv6_mreq_opt->ipv6mr_interface); + HANDLE_ERROR(error); + return error; + case IPV6_LEAVE_GROUP: + assert(optlen == sizeof(struct ipv6_mreq)); + ipv6_mreq_opt = (struct ipv6_mreq *)optval; + imr_multiaddr.kind = IPv6; + ipv6_addr_to_wasi_ipv6_addr( + (uint16_t *)ipv6_mreq_opt->ipv6mr_multiaddr.s6_addr, + &imr_multiaddr.addr.ip6); + error = __wasi_sock_set_ip_drop_membership( + sockfd, &imr_multiaddr, ipv6_mreq_opt->ipv6mr_interface); + HANDLE_ERROR(error); + return error; + default: + error = __WASI_ERRNO_NOTSUP; + HANDLE_ERROR(error); + return error; + } +} + +int +setsockopt(int sockfd, int level, int optname, const void *optval, + socklen_t optlen) +{ + __wasi_errno_t error; + + switch (level) { + case SOL_SOCKET: + return set_sol_socket_option(sockfd, optname, optval, optlen); + case IPPROTO_TCP: + return set_ipproto_tcp_option(sockfd, optname, optval, optlen); + case IPPROTO_IP: + return set_ipproto_ip_option(sockfd, optname, optval, optlen); + case IPPROTO_IPV6: + return set_ipproto_ipv6_option(sockfd, optname, optval, optlen); + default: + error = __WASI_ERRNO_NOTSUP; + HANDLE_ERROR(error); + return error; + } +} diff --git a/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/wasi_socket_ext.h b/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/wasi_socket_ext.h new file mode 100644 index 000000000..98429bbb0 --- /dev/null +++ b/product-mini/platforms/zephyr/simple-http/wasm-apps/inc/wasi_socket_ext.h @@ -0,0 +1,1001 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#ifndef _WASI_SOCKET_EXT_H_ +#define _WASI_SOCKET_EXT_H_ + +#include +#include +#include + +/*Be a part of */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + /* Used only for sock_addr_resolve hints */ + SOCKET_ANY = -1, + SOCKET_DGRAM = 0, + SOCKET_STREAM, +} __wasi_sock_type_t; + +typedef uint16_t __wasi_ip_port_t; + +typedef enum { IPv4 = 0, IPv6 } __wasi_addr_type_t; + +/* + n0.n1.n2.n3 + Example: + IP Address: 127.0.0.1 + Structure: {n0: 127, n1: 0, n2: 0, n3: 1} +*/ +typedef struct __wasi_addr_ip4_t { + uint8_t n0; + uint8_t n1; + uint8_t n2; + uint8_t n3; +} __wasi_addr_ip4_t; + +typedef struct __wasi_addr_ip4_port_t { + __wasi_addr_ip4_t addr; + __wasi_ip_port_t port; /* host byte order */ +} __wasi_addr_ip4_port_t; + +/* + n0:n1:n2:n3:h0:h1:h2:h3, each 16bit value uses host byte order + Example (little-endian system) + IP Address fe80::3ba2:893b:4be0:e3dd + Structure: { + n0: 0xfe80, n1:0x0, n2: 0x0, n3: 0x0, + h0: 0x3ba2, h1: 0x893b, h2: 0x4be0, h3: 0xe3dd + } +*/ +typedef struct __wasi_addr_ip6_t { + uint16_t n0; + uint16_t n1; + uint16_t n2; + uint16_t n3; + uint16_t h0; + uint16_t h1; + uint16_t h2; + uint16_t h3; +} __wasi_addr_ip6_t; + +typedef struct __wasi_addr_ip6_port_t { + __wasi_addr_ip6_t addr; + __wasi_ip_port_t port; /* host byte order */ +} __wasi_addr_ip6_port_t; + +typedef struct __wasi_addr_ip_t { + __wasi_addr_type_t kind; + union { + __wasi_addr_ip4_t ip4; + __wasi_addr_ip6_t ip6; + } addr; +} __wasi_addr_ip_t; + +typedef struct __wasi_addr_t { + __wasi_addr_type_t kind; + union { + __wasi_addr_ip4_port_t ip4; + __wasi_addr_ip6_port_t ip6; + } addr; +} __wasi_addr_t; + +typedef enum { INET4 = 0, INET6, INET_UNSPEC } __wasi_address_family_t; + +typedef struct __wasi_addr_info_t { + __wasi_addr_t addr; + __wasi_sock_type_t type; +} __wasi_addr_info_t; + +typedef struct __wasi_addr_info_hints_t { + __wasi_sock_type_t type; + __wasi_address_family_t family; + // this is to workaround lack of optional parameters + uint8_t hints_enabled; +} __wasi_addr_info_hints_t; + +#ifdef __wasi__ +/** + * Reimplement below POSIX APIs with __wasi_sock_XXX functions. + * + * Keep sync with + * + * + */ +#define SO_REUSEADDR 2 +#define SO_BROADCAST 6 +#define SO_SNDBUF 7 +#define SO_RCVBUF 8 +#define SO_KEEPALIVE 9 +#define SO_LINGER 13 +#define SO_REUSEPORT 15 +#define SO_RCVTIMEO 20 +#define SO_SNDTIMEO 21 + +#define TCP_NODELAY 1 +#define TCP_KEEPIDLE 4 +#define TCP_KEEPINTVL 5 +#define TCP_QUICKACK 12 +#define TCP_FASTOPEN_CONNECT 30 + +#define IP_TTL 2 +#define IP_MULTICAST_TTL 33 +#define IP_MULTICAST_LOOP 34 +#define IP_ADD_MEMBERSHIP 35 +#define IP_DROP_MEMBERSHIP 36 + +#define IPV6_MULTICAST_LOOP 19 +#define IPV6_JOIN_GROUP 20 +#define IPV6_LEAVE_GROUP 21 +#define IPV6_V6ONLY 26 + +struct addrinfo { + int ai_flags; /* Input flags. */ + int ai_family; /* Protocol family for socket. */ + int ai_socktype; /* Socket type. */ + int ai_protocol; /* Protocol for socket. */ + socklen_t ai_addrlen; /* Length of socket address. */ + struct sockaddr *ai_addr; /* Socket address for socket. */ + char *ai_canonname; /* Canonical name for service location. */ + struct addrinfo *ai_next; /* Pointer to next in list. */ +}; + +#ifndef __WASI_RIGHTS_SOCK_ACCEPT +int +accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); +#endif + +int +bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); + +int +connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); + +int +listen(int sockfd, int backlog); + +ssize_t +recvmsg(int sockfd, struct msghdr *msg, int flags); + +ssize_t +sendmsg(int sockfd, const struct msghdr *msg, int flags); + +ssize_t +sendto(int sockfd, const void *buf, size_t len, int flags, + const struct sockaddr *dest_addr, socklen_t addrlen); + +ssize_t +recvfrom(int sockfd, void *buf, size_t len, int flags, + struct sockaddr *src_addr, socklen_t *addrlen); + +int +socket(int domain, int type, int protocol); + +int +getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen); + +int +getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen); + +int +getsockopt(int sockfd, int level, int optname, void *__restrict optval, + socklen_t *__restrict optlen); + +int +setsockopt(int sockfd, int level, int optname, const void *optval, + socklen_t optlen); + +int +getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, + struct addrinfo **res); + +void +freeaddrinfo(struct addrinfo *res); +#endif + +/** + * __wasi_sock_accept was introduced in wasi-sdk v15. To + * temporarily maintain backward compatibility with the old + * wasi-sdk, we explicitly add that implementation here so it works + * with older versions of the SDK. + */ +#ifndef __WASI_RIGHTS_SOCK_ACCEPT +/** + * Accept a connection on a socket + * Note: This is similar to `accept` + */ +int32_t +__imported_wasi_snapshot_preview1_sock_accept(int32_t arg0, int32_t arg1, + int32_t arg2) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_accept"))); + +static inline __wasi_errno_t +__wasi_sock_accept(__wasi_fd_t fd, __wasi_fdflags_t flags, __wasi_fd_t *fd_new) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_accept( + (int32_t)fd, (int32_t)flags, (int32_t)fd_new); +} +#endif + +/** + * Returns the local address to which the socket is bound. + * + * Note: This is similar to `getsockname` in POSIX + * + * When successful, the contents of the output buffer consist of an IP address, + * either IP4 or IP6. + */ +int32_t +__imported_wasi_snapshot_preview1_sock_addr_local(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_addr_local"))); + +static inline __wasi_errno_t +__wasi_sock_addr_local(__wasi_fd_t fd, __wasi_addr_t *addr) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_addr_local( + (int32_t)fd, (int32_t)addr); +} + +/** + * Returns the remote address to which the socket is connected to. + * + * Note: This is similar to `getpeername` in POSIX + * + * When successful, the contents of the output buffer consist of an IP address, + * either IP4 or IP6. + */ +int32_t +__imported_wasi_snapshot_preview1_sock_addr_remote(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_addr_remote"))); + +static inline __wasi_errno_t +__wasi_sock_addr_remote(__wasi_fd_t fd, __wasi_addr_t *addr) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_addr_remote( + (int32_t)fd, (int32_t)addr); +} + +/** + * Resolve a hostname and a service to one or more IP addresses. Service is + * optional and you can pass empty string in most cases, it is used as a hint + * for protocol. + * + * Note: This is similar to `getaddrinfo` in POSIX + * + * When successful, the contents of the output buffer consist of a sequence of + * IPv4 and/or IPv6 addresses. Each address entry consists of a wasi_addr_t + * object. + * + * This function fills the output buffer as much as possible, truncating the + * entries that didn't fit into the buffer. A number of available addresses + * will be returned through the last parameter. + */ +int32_t +__imported_wasi_snapshot_preview1_sock_addr_resolve(int32_t arg0, int32_t arg1, + int32_t arg2, int32_t arg3, + int32_t arg4, int32_t arg5) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_addr_resolve"))); + +static inline __wasi_errno_t +__wasi_sock_addr_resolve(const char *host, const char *service, + __wasi_addr_info_hints_t *hints, + __wasi_addr_info_t *addr_info, + __wasi_size_t addr_info_size, + __wasi_size_t *max_info_size) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_addr_resolve( + (int32_t)host, (int32_t)service, (int32_t)hints, (int32_t)addr_info, + (int32_t)addr_info_size, (int32_t)max_info_size); +} + +/** + * Bind a socket + * Note: This is similar to `bind` in POSIX using PF_INET + */ +int32_t +__imported_wasi_snapshot_preview1_sock_bind(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_bind"))); + +static inline __wasi_errno_t +__wasi_sock_bind(__wasi_fd_t fd, __wasi_addr_t *addr) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_bind( + (int32_t)fd, (int32_t)addr); +} + +/** + * Send data to a specific target + * Note: This is similar to `sendto` in POSIX + */ +int32_t +__imported_wasi_snapshot_preview1_sock_send_to(int32_t arg0, int32_t arg1, + int32_t arg2, int32_t arg3, + int32_t arg4, int32_t arg5) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_send_to"))); + +static inline __wasi_errno_t +__wasi_sock_send_to(__wasi_fd_t fd, const __wasi_ciovec_t *si_data, + uint32_t si_data_len, __wasi_siflags_t si_flags, + const __wasi_addr_t *dest_addr, uint32_t *so_data_len) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_send_to( + (int32_t)fd, (int32_t)si_data, (int32_t)si_data_len, (int32_t)si_flags, + (uint32_t)dest_addr, (uint32_t)so_data_len); +} + +/** + * Receives data from a socket + * Note: This is similar to `recvfrom` in POSIX + */ +int32_t +__imported_wasi_snapshot_preview1_sock_recv_from(int32_t arg0, int32_t arg1, + int32_t arg2, int32_t arg3, + int32_t arg4, int32_t arg5) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_recv_from"))); + +static inline __wasi_errno_t +__wasi_sock_recv_from(__wasi_fd_t fd, __wasi_ciovec_t *ri_data, + uint32_t ri_data_len, __wasi_riflags_t ri_flags, + __wasi_addr_t *src_addr, uint32_t *ro_data_len) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_recv_from( + (int32_t)fd, (int32_t)ri_data, (int32_t)ri_data_len, (int32_t)ri_flags, + (uint32_t)src_addr, (uint32_t)ro_data_len); +} + +/** + * Close a socket (this is an alias for `fd_close`) + * Note: This is similar to `close` in POSIX. + */ +int32_t +__imported_wasi_snapshot_preview1_sock_close(int32_t arg0) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_close"))); + +static inline __wasi_errno_t +__wasi_sock_close(__wasi_fd_t fd) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_close( + (int32_t)fd); +} + +/** + * Initiate a connection on a socket to the specified address + * Note: This is similar to `connect` in POSIX + */ + +int32_t +__imported_wasi_snapshot_preview1_sock_connect(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_connect"))); + +static inline __wasi_errno_t +__wasi_sock_connect(__wasi_fd_t fd, __wasi_addr_t *addr) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_connect( + (int32_t)fd, (int32_t)addr); +} +/** + * Retrieve the size of the receive buffer + * Note: This is similar to `getsockopt` in POSIX for SO_RCVBUF + */ + +int32_t +__imported_wasi_snapshot_preview1_sock_get_recv_buf_size(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_recv_buf_size"))); + +static inline __wasi_errno_t +__wasi_sock_get_recv_buf_size(__wasi_fd_t fd, __wasi_size_t *size) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_recv_buf_size((int32_t)fd, + (int32_t)size); +} +/** + * Retrieve status of address reuse on a socket + * Note: This is similar to `getsockopt` in POSIX for SO_REUSEADDR + */ +int32_t +__imported_wasi_snapshot_preview1_sock_get_reuse_addr(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_reuse_addr"))); + +static inline __wasi_errno_t +__wasi_sock_get_reuse_addr(__wasi_fd_t fd, bool *reuse) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_reuse_addr((int32_t)fd, + (int32_t)reuse); +} + +/** + * Retrieve status of port reuse on a socket + * Note: This is similar to `getsockopt` in POSIX for SO_REUSEPORT + */ +int32_t +__imported_wasi_snapshot_preview1_sock_get_reuse_port(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_reuse_port"))); + +static inline __wasi_errno_t +__wasi_sock_get_reuse_port(__wasi_fd_t fd, bool *reuse) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_reuse_port((int32_t)fd, + (int32_t)reuse); +} + +/** + * Retrieve the size of the send buffer + * Note: This is similar to `getsockopt` in POSIX for SO_SNDBUF + */ +int32_t +__imported_wasi_snapshot_preview1_sock_get_send_buf_size(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_send_buf_size"))); + +static inline __wasi_errno_t +__wasi_sock_get_send_buf_size(__wasi_fd_t fd, __wasi_size_t *size) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_send_buf_size((int32_t)fd, + (int32_t)size); +} + +/** + * Listen for connections on a socket + * Note: This is similar to `listen` + */ +int32_t +__imported_wasi_snapshot_preview1_sock_listen(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_listen"))); + +static inline __wasi_errno_t +__wasi_sock_listen(__wasi_fd_t fd, __wasi_size_t backlog) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_listen( + (int32_t)fd, (int32_t)backlog); +} + +/** + * Open a socket + + * The first argument to this function is a handle to an + * address pool. The address pool determines what actions can + * be performed and at which addresses they can be performed to. + + * The address pool cannot be re-assigned. You will need to close + * the socket and open a new one to use a different address pool. + + * Note: This is similar to `socket` in POSIX using PF_INET + */ + +int32_t +__imported_wasi_snapshot_preview1_sock_open(int32_t arg0, int32_t arg1, + int32_t arg2, int32_t arg3) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_open"))); + +static inline __wasi_errno_t +__wasi_sock_open(__wasi_fd_t fd, __wasi_address_family_t af, + __wasi_sock_type_t socktype, __wasi_fd_t *sockfd) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_open( + (int32_t)fd, (int32_t)af, (int32_t)socktype, (int32_t)sockfd); +} + +/** + * Set size of receive buffer + * Note: This is similar to `setsockopt` in POSIX for SO_RCVBUF + */ +int32_t +__imported_wasi_snapshot_preview1_sock_set_recv_buf_size(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_recv_buf_size"))); + +static inline __wasi_errno_t +__wasi_sock_set_recv_buf_size(__wasi_fd_t fd, __wasi_size_t size) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_recv_buf_size((int32_t)fd, + (int32_t)size); +} + +/** + * Enable/disable address reuse on a socket + * Note: This is similar to `setsockopt` in POSIX for SO_REUSEADDR + */ +int32_t +__imported_wasi_snapshot_preview1_sock_set_reuse_addr(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_reuse_addr"))); + +static inline __wasi_errno_t +__wasi_sock_set_reuse_addr(__wasi_fd_t fd, bool reuse) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_reuse_addr((int32_t)fd, + (int32_t)reuse); +} + +/** + * Enable port reuse on a socket + * Note: This is similar to `setsockopt` in POSIX for SO_REUSEPORT + */ +int32_t +__imported_wasi_snapshot_preview1_sock_set_reuse_port(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_reuse_port"))); + +static inline __wasi_errno_t +__wasi_sock_set_reuse_port(__wasi_fd_t fd, bool reuse) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_reuse_port((int32_t)fd, + (int32_t)reuse); +} + +/** + * Set size of send buffer + * Note: This is similar to `setsockopt` in POSIX for SO_SNDBUF + */ +int32_t +__imported_wasi_snapshot_preview1_sock_set_send_buf_size(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_send_buf_size"))); + +static inline __wasi_errno_t +__wasi_sock_set_send_buf_size(__wasi_fd_t fd, __wasi_size_t buf_len) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_send_buf_size( + (int32_t)fd, (int32_t)buf_len); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_recv_timeout(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_recv_timeout"))); + +static inline __wasi_errno_t +__wasi_sock_get_recv_timeout(__wasi_fd_t fd, uint64_t *timeout_us) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_recv_timeout( + (int32_t)fd, (int32_t)timeout_us); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_recv_timeout(int32_t arg0, + int64_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_recv_timeout"))); + +static inline __wasi_errno_t +__wasi_sock_set_recv_timeout(__wasi_fd_t fd, uint64_t timeout_us) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_recv_timeout( + (int32_t)fd, (int64_t)timeout_us); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_send_timeout(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_send_timeout"))); + +static inline __wasi_errno_t +__wasi_sock_get_send_timeout(__wasi_fd_t fd, uint64_t *timeout_us) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_send_timeout( + (int32_t)fd, (int32_t)timeout_us); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_send_timeout(int32_t arg0, + int64_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_send_timeout"))); + +static inline __wasi_errno_t +__wasi_sock_set_send_timeout(__wasi_fd_t fd, uint64_t timeout_us) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_send_timeout( + (int32_t)fd, (int64_t)timeout_us); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_keep_alive(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_keep_alive"))); + +static inline __wasi_errno_t +__wasi_sock_set_keep_alive(__wasi_fd_t fd, bool option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_keep_alive((int32_t)fd, + (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_keep_alive(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_keep_alive"))); + +static inline __wasi_errno_t +__wasi_sock_get_keep_alive(__wasi_fd_t fd, bool *option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_keep_alive((int32_t)fd, + (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_linger(int32_t arg0, int32_t arg1, + int32_t arg2) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_linger"))); + +static inline __wasi_errno_t +__wasi_sock_set_linger(__wasi_fd_t fd, bool is_enabled, int linger_s) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_set_linger( + (int32_t)fd, (int32_t)is_enabled, (int32_t)linger_s); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_linger(int32_t arg0, int32_t arg1, + int32_t arg2) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_linger"))); + +static inline __wasi_errno_t +__wasi_sock_get_linger(__wasi_fd_t fd, bool *is_enabled, int *linger_s) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_get_linger( + (int32_t)fd, (int32_t)is_enabled, (int32_t)linger_s); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_tcp_keep_idle(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_tcp_keep_idle"))); + +static inline __wasi_errno_t +__wasi_sock_set_tcp_keep_idle(__wasi_fd_t fd, uint32_t time_s) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_tcp_keep_idle( + (int32_t)fd, (int32_t)time_s); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_tcp_keep_idle(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_tcp_keep_idle"))); + +static inline __wasi_errno_t +__wasi_sock_get_tcp_keep_idle(__wasi_fd_t fd, uint32_t *time_s) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_tcp_keep_idle( + (int32_t)fd, (int32_t)time_s); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_tcp_keep_intvl(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_tcp_keep_intvl"))); + +static inline __wasi_errno_t +__wasi_sock_set_tcp_keep_intvl(__wasi_fd_t fd, uint32_t time_s) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_tcp_keep_intvl( + (int32_t)fd, (int32_t)time_s); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_tcp_keep_intvl(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_tcp_keep_intvl"))); + +static inline __wasi_errno_t +__wasi_sock_get_tcp_keep_intvl(__wasi_fd_t fd, uint32_t *time_s) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_tcp_keep_intvl( + (int32_t)fd, (int32_t)time_s); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_tcp_fastopen_connect(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_tcp_fastopen_connect"))); + +static inline __wasi_errno_t +__wasi_sock_set_tcp_fastopen_connect(__wasi_fd_t fd, bool option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_tcp_fastopen_connect( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_tcp_fastopen_connect(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_tcp_fastopen_connect"))); + +static inline __wasi_errno_t +__wasi_sock_get_tcp_fastopen_connect(__wasi_fd_t fd, bool *option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_tcp_fastopen_connect( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_ip_multicast_loop(int32_t arg0, + int32_t arg1, + int32_t arg2) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_ip_multicast_loop"))); + +static inline __wasi_errno_t +__wasi_sock_set_ip_multicast_loop(__wasi_fd_t fd, bool ipv6, bool option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_ip_multicast_loop( + (int32_t)fd, (int32_t)ipv6, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_ip_multicast_loop(int32_t arg0, + int32_t arg1, + int32_t arg2) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_ip_multicast_loop"))); + +static inline __wasi_errno_t +__wasi_sock_get_ip_multicast_loop(__wasi_fd_t fd, bool ipv6, bool *option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_ip_multicast_loop( + (int32_t)fd, (int32_t)ipv6, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_ip_multicast_ttl(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_ip_multicast_ttl"))); + +static inline __wasi_errno_t +__wasi_sock_set_ip_multicast_ttl(__wasi_fd_t fd, uint8_t option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_ip_multicast_ttl( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_ip_multicast_ttl(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_ip_multicast_ttl"))); + +static inline __wasi_errno_t +__wasi_sock_get_ip_multicast_ttl(__wasi_fd_t fd, uint8_t *option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_ip_multicast_ttl( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_ip_add_membership(int32_t arg0, + int32_t arg1, + int32_t arg2) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_ip_add_membership"))); + +static inline __wasi_errno_t +__wasi_sock_set_ip_add_membership(__wasi_fd_t fd, + __wasi_addr_ip_t *imr_multiaddr, + uint32_t imr_interface) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_ip_add_membership( + (int32_t)fd, (int32_t)imr_multiaddr, (int32_t)imr_interface); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_ip_drop_membership(int32_t arg0, + int32_t arg1, + int32_t arg2) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_ip_drop_membership"))); + +static inline __wasi_errno_t +__wasi_sock_set_ip_drop_membership(__wasi_fd_t fd, + __wasi_addr_ip_t *imr_multiaddr, + uint32_t imr_interface) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_ip_drop_membership( + (int32_t)fd, (int32_t)imr_multiaddr, (int32_t)imr_interface); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_broadcast(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_broadcast"))); + +static inline __wasi_errno_t +__wasi_sock_set_broadcast(__wasi_fd_t fd, bool option) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_set_broadcast( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_broadcast(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_broadcast"))); + +static inline __wasi_errno_t +__wasi_sock_get_broadcast(__wasi_fd_t fd, bool *option) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_get_broadcast( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_tcp_no_delay(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_tcp_no_delay"))); + +static inline __wasi_errno_t +__wasi_sock_set_tcp_no_delay(__wasi_fd_t fd, bool option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_tcp_no_delay( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_tcp_no_delay(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_tcp_no_delay"))); + +static inline __wasi_errno_t +__wasi_sock_get_tcp_no_delay(__wasi_fd_t fd, bool *option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_tcp_no_delay( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_tcp_quick_ack(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_tcp_quick_ack"))); + +static inline __wasi_errno_t +__wasi_sock_set_tcp_quick_ack(__wasi_fd_t fd, bool option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_set_tcp_quick_ack( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_tcp_quick_ack(int32_t arg0, + int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_tcp_quick_ack"))); + +static inline __wasi_errno_t +__wasi_sock_get_tcp_quick_ack(__wasi_fd_t fd, bool *option) +{ + return (__wasi_errno_t) + __imported_wasi_snapshot_preview1_sock_get_tcp_quick_ack( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_ip_ttl(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_ip_ttl"))); + +static inline __wasi_errno_t +__wasi_sock_set_ip_ttl(__wasi_fd_t fd, uint8_t option) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_set_ip_ttl( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_ip_ttl(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_ip_ttl"))); + +static inline __wasi_errno_t +__wasi_sock_get_ip_ttl(__wasi_fd_t fd, uint8_t *option) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_get_ip_ttl( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_set_ipv6_only(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_set_ipv6_only"))); + +static inline __wasi_errno_t +__wasi_sock_set_ipv6_only(__wasi_fd_t fd, bool option) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_set_ipv6_only( + (int32_t)fd, (int32_t)option); +} + +int32_t +__imported_wasi_snapshot_preview1_sock_get_ipv6_only(int32_t arg0, int32_t arg1) + __attribute__((__import_module__("wasi_snapshot_preview1"), + __import_name__("sock_get_ipv6_only"))); + +static inline __wasi_errno_t +__wasi_sock_get_ipv6_only(__wasi_fd_t fd, bool *option) +{ + return (__wasi_errno_t)__imported_wasi_snapshot_preview1_sock_get_ipv6_only( + (int32_t)fd, (int32_t)option); +} +/** + * TODO: modify recv() and send() + * since don't want to re-compile the wasi-libc, + * we tend to keep original implentations of recv() and send(). + */ + +#ifdef __cplusplus +} +#endif + +#endif