Skip to content

Commit

Permalink
Add sock_accept() to snapshot1 (WebAssembly#458)
Browse files Browse the repository at this point in the history
This function allows a server to accept incoming connections. A few
notes are in order.

Although there are legitimate questions about the capabilities model in
numerous networking APIs, this particular API is both immensely useful
and not a violation of the capabilities model. There is still no way to
create an arbitrary socket. The only thing that WASM can do is accept
incoming connections on a predefined listening socket.

Signed-off-by: Nathaniel McCallum <[email protected]>
  • Loading branch information
npmccallum authored Jan 18, 2022
1 parent 3423c9c commit 0ba0c5e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
34 changes: 33 additions & 1 deletion phases/snapshot/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ The right to invoke [`sock_shutdown`](#sock_shutdown).

Bit: 28

- <a href="#rights.sock_accept" name="rights.sock_accept"></a> `sock_accept`: `bool`
The right to invoke [`sock_accept`](#sock_accept).

Bit: 29

## <a href="#fd" name="fd"></a> `fd`: `Handle`
A file descriptor handle.

Expand Down Expand Up @@ -2011,7 +2016,7 @@ The path at which to create the directory.

#### <a href="#path_filestat_get" name="path_filestat_get"></a> `path_filestat_get(fd: fd, flags: lookupflags, path: string) -> Result<filestat, errno>`
Return the attributes of a file or directory.
Note: This is similar to `fstatat` in POSIX.
Note: This is similar to `stat` in POSIX.

##### Params
- <a href="#path_filestat_get.fd" name="path_filestat_get.fd"></a> `fd`: [`fd`](#fd)
Expand Down Expand Up @@ -2414,6 +2419,33 @@ The buffer to fill with random data.
- <a href="#random_get.error.err" name="random_get.error.err"></a> `err`: [`errno`](#errno)


---

#### <a href="#sock_accept" name="sock_accept"></a> `sock_accept(fd: fd, flags: fdflags) -> Result<fd, errno>`
Accept a new incoming connection.
Note: This is similar to `accept` in POSIX.

##### Params
- <a href="#sock_accept.fd" name="sock_accept.fd"></a> `fd`: [`fd`](#fd)
The listening socket.

- <a href="#sock_accept.flags" name="sock_accept.flags"></a> `flags`: [`fdflags`](#fdflags)
The desired values of the file descriptor flags.

##### Results
- <a href="#sock_accept.error" name="sock_accept.error"></a> `error`: `Result<fd, errno>`
New socket connection

###### Variant Layout
- size: 8
- align: 4
- tag_size: 4
###### Variant cases
- <a href="#sock_accept.error.ok" name="sock_accept.error.ok"></a> `ok`: [`fd`](#fd)

- <a href="#sock_accept.error.err" name="sock_accept.error.err"></a> `err`: [`errno`](#errno)


---

#### <a href="#sock_recv" name="sock_recv"></a> `sock_recv(fd: fd, ri_data: iovec_array, ri_flags: riflags) -> Result<(size, roflags), errno>`
Expand Down
2 changes: 2 additions & 0 deletions phases/snapshot/witx/typenames.witx
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@
$poll_fd_readwrite
;;; The right to invoke `sock_shutdown`.
$sock_shutdown
;;; The right to invoke `sock_accept`.
$sock_accept
)
)

Expand Down
11 changes: 11 additions & 0 deletions phases/snapshot/witx/wasi_snapshot_preview1.witx
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,17 @@
(result $error (expected (error $errno)))
)

;;; Accept a new incoming connection.
;;; Note: This is similar to `accept` in POSIX.
(@interface func (export "sock_accept")
;;; The listening socket.
(param $fd $fd)
;;; The desired values of the file descriptor flags.
(param $flags $fdflags)
;;; New socket connection
(result $error (expected $fd (error $errno)))
)

;;; Receive a message from a socket.
;;; Note: This is similar to `recv` in POSIX, though it also supports reading
;;; the data into multiple buffers in the manner of `readv`.
Expand Down

0 comments on commit 0ba0c5e

Please sign in to comment.