From a9b76228b0261879101bc3509dd819b49f6f0b96 Mon Sep 17 00:00:00 2001 From: Marcin Kolny Date: Tue, 4 Jul 2023 14:21:21 +0100 Subject: [PATCH] Allow defining hints without exact socket type or address family (#2337) --- core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h | 4 +++- .../libraries/lib-socket/src/wasi/wasi_socket_ext.c | 5 +++++ .../sandboxed-system-primitives/include/wasmtime_ssp.h | 4 +++- .../libc-wasi/sandboxed-system-primitives/src/posix.c | 9 ++++++--- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h b/core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h index c9a07eb72..98429bbb0 100644 --- a/core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h +++ b/core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h @@ -17,6 +17,8 @@ extern "C" { #endif typedef enum { + /* Used only for sock_addr_resolve hints */ + SOCKET_ANY = -1, SOCKET_DGRAM = 0, SOCKET_STREAM, } __wasi_sock_type_t; @@ -84,7 +86,7 @@ typedef struct __wasi_addr_t { } addr; } __wasi_addr_t; -typedef enum { INET4 = 0, INET6 } __wasi_address_family_t; +typedef enum { INET4 = 0, INET6, INET_UNSPEC } __wasi_address_family_t; typedef struct __wasi_addr_info_t { __wasi_addr_t addr; diff --git a/core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c b/core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c index defbc6efe..1172d0a77 100644 --- a/core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c +++ b/core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c @@ -430,6 +430,9 @@ addrinfo_hints_to_wasi_hints(const struct addrinfo *hints, case AF_INET6: wasi_hints->family = INET6; break; + case AF_UNSPEC: + wasi_hints->family = INET_UNSPEC; + break; default: return __WASI_ERRNO_AFNOSUPPORT; } @@ -440,6 +443,8 @@ addrinfo_hints_to_wasi_hints(const struct addrinfo *hints, case SOCK_DGRAM: wasi_hints->type = SOCKET_DGRAM; break; + case 0: + wasi_hints->type = SOCKET_ANY; default: return __WASI_ERRNO_NOTSUP; } diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h index feadf2f95..586e5816d 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h @@ -552,6 +552,8 @@ _Static_assert(offsetof(__wasi_subscription_t, u) == 8, "witx calculated offset" /* keep syncing with wasi_socket_ext.h */ typedef enum { + /* Used only for sock_addr_resolve hints */ + SOCKET_ANY = -1, SOCKET_DGRAM = 0, SOCKET_STREAM, } __wasi_sock_type_t; @@ -605,7 +607,7 @@ typedef struct __wasi_addr_t { } addr; } __wasi_addr_t; -typedef enum { INET4 = 0, INET6 } __wasi_address_family_t; +typedef enum { INET4 = 0, INET6, INET_UNSPEC } __wasi_address_family_t; typedef struct __wasi_addr_info_t { __wasi_addr_t addr; 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 61e841836..3038e3d44 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 @@ -3102,9 +3102,12 @@ wasi_ssp_sock_addr_resolve( } int ret = os_socket_addr_resolve( - host, service, hints->hints_enabled ? &hints_is_tcp : NULL, - hints->hints_enabled ? &hints_is_ipv4 : NULL, wamr_addr_info, - addr_info_size, &_max_info_size); + host, service, + hints->hints_enabled && hints->type != SOCKET_ANY ? &hints_is_tcp + : NULL, + hints->hints_enabled && hints->family != INET_UNSPEC ? &hints_is_ipv4 + : NULL, + wamr_addr_info, addr_info_size, &_max_info_size); if (ret != BHT_OK) { wasm_runtime_free(wamr_addr_info);