Add support for WASI sockets to C API (#5624)

* Add support for WASI sockets to C API

Add support for WASI sockets in the C API by adding a new API to handle
preopening sockets for clients. This uses HashMap instead of Vec for
preopened sockets to identify if caller has called in more than once
with the same FD number. If so, then we return false so caller is given
hint that they are attempting to overwrite an already existing socket
FD.

* Apply suggestions from code review

Co-authored-by: Peter Huene <peter@huene.dev>

* s/stdlistener/listener/

---------

Co-authored-by: Peter Huene <peter@huene.dev>
This commit is contained in:
Ivan Font
2023-02-09 16:22:11 -08:00
committed by GitHub
parent 15fe9c7c93
commit de68cc1726
2 changed files with 58 additions and 4 deletions

View File

@@ -7,6 +7,7 @@
#ifndef WASI_H
#define WASI_H
#include <stdint.h>
#include "wasm.h"
#ifndef WASI_API_EXTERN
@@ -156,6 +157,19 @@ WASI_API_EXTERN void wasi_config_inherit_stderr(wasi_config_t* config);
*/
WASI_API_EXTERN bool wasi_config_preopen_dir(wasi_config_t* config, const char* path, const char* guest_path);
/**
* \brief Configures a "preopened" listen socket to be available to WASI APIs.
*
* By default WASI programs do not have access to open up network sockets on
* the host. This API can be used to grant WASI programs access to a network
* socket file descriptor on the host.
*
* The fd_num argument is the number of the file descriptor by which it will be
* known in WASM and the host_port is the IP address and port (e.g.
* "127.0.0.1:8080") requested to listen on.
*/
WASI_API_EXTERN bool wasi_config_preopen_socket(wasi_config_t* config, uint32_t fd_num, const char* host_port);
#undef own
#ifdef __cplusplus