Socket Support
Last updated
Last updated
Berkeley sockets usually means an API for Internet sockets and Unix domain sockets. A socket is an abstract representation of the local endpoint of a network communication path.
Currently, WAMR supports some Socket API features:
Support TCP and UDP
Support IPv4 and IPv6
Support get/set socket options
Support access control
This document introduces a way to support the Berkeley/POSIX Socket API in WebAssembly code.
The first step is to include a header file of the WAMR socket extension in the native source code.
__wasi__
is a macro defined by WASI. The host compiler will not enable it.
It is recommended that the project should use CMake as its build system. Use as a toolchain to compile C/C++ to WebAssembly
In the CMakeLists.txt, include an extension of socket support and link with it.
Now, the native code with socket APIs is ready for compilation.
If having the .wasm, the last step is to run it with iwasm.
The iwasm should be compiled with WAMR_BUILD_LIBC_WASI=1
. By default, it is enabled.
iwasm accepts address ranges via an option, --addr-pool
, to implement the capability control. All IP address the WebAssembly application may need to bind()
or connect()
should be announced first. Every IP address should be in CIDR notation.
iwasm also accepts list of domain names and domain name patterns for the address resolution via an option, --allow-resolve
, to implement the capability control. Every domain that will be resolved using sock_addr_resolve
needs to be added to the allowlist first.
The example above shows how to allow for resolving all example.com
's subdomains (e.g. x.example.com
, a.b.c.example.com
) and domain.com
domain.
WAMR also supports the socket API within Intel SGX enclaves.
The iwasm should be compiled with WAMR_BUILD_LIBC_WASI=1
and WAMR_BUILD_LIB_PTHREAD=1
, which are enabled by default.
Similarly to running iwasm outside of an enclave, the allowed address ranges are given via the option --addr-pool
.
Refer to for more details.
Refer to for the compilation of the Wasm applications and for the Wasm runtime.