Debian Codegraph @

.cargo/vendor/wasip2/wit/deps/sockets/tcp.wit

raw
1 @since(version = 0.2.0)
2 interface tcp {
3 @since(version = 0.2.0)
4 use wasi:io/[email protected].{input-stream, output-stream};
5 @since(version = 0.2.0)
6 use wasi:io/[email protected].{pollable};
7 @since(version = 0.2.0)
8 use wasi:clocks/[email protected].{duration};
9 @since(version = 0.2.0)
10 use network.{network, error-code, ip-socket-address, ip-address-family};
11
12 @since(version = 0.2.0)
13 enum shutdown-type {
14 /// Similar to `SHUT_RD` in POSIX.
15 receive,
16
17 /// Similar to `SHUT_WR` in POSIX.
18 send,
19
20 /// Similar to `SHUT_RDWR` in POSIX.
21 both,
22 }
23
24 /// A TCP socket resource.
25 ///
26 /// The socket can be in one of the following states:
27 /// - `unbound`
28 /// - `bind-in-progress`
29 /// - `bound` (See note below)
30 /// - `listen-in-progress`
31 /// - `listening`
32 /// - `connect-in-progress`
33 /// - `connected`
34 /// - `closed`
35 /// See <https://github.com/WebAssembly/wasi-sockets/blob/main/TcpSocketOperationalSemantics.md>
36 /// for more information.
37 ///
38 /// Note: Except where explicitly mentioned, whenever this documentation uses
39 /// the term "bound" without backticks it actually means: in the `bound` state *or higher*.
40 /// (i.e. `bound`, `listen-in-progress`, `listening`, `connect-in-progress` or `connected`)
41 ///
42 /// In addition to the general error codes documented on the
43 /// `network::error-code` type, TCP socket methods may always return
44 /// `error(invalid-state)` when in the `closed` state.
45 @since(version = 0.2.0)
46 resource tcp-socket {
47 /// Bind the socket to a specific network on the provided IP address and port.
48 ///
49 /// If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is left to the implementation to decide which
50 /// network interface(s) to bind to.
51 /// If the TCP/UDP port is zero, the socket will be bound to a random free port.
52 ///
53 /// Bind can be attempted multiple times on the same socket, even with
54 /// different arguments on each iteration. But never concurrently and
55 /// only as long as the previous bind failed. Once a bind succeeds, the
56 /// binding can't be changed anymore.
57 ///
58 /// # Typical errors
59 /// - `invalid-argument`: The `local-address` has the wrong address family. (EAFNOSUPPORT, EFAULT on Windows)
60 /// - `invalid-argument`: `local-address` is not a unicast address. (EINVAL)
61 /// - `invalid-argument`: `local-address` is an IPv4-mapped IPv6 address. (EINVAL)
62 /// - `invalid-state`: The socket is already bound. (EINVAL)
63 /// - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)
64 /// - `address-in-use`: Address is already in use. (EADDRINUSE)
65 /// - `address-not-bindable`: `local-address` is not an address that the `network` can bind to. (EADDRNOTAVAIL)
66 /// - `not-in-progress`: A `bind` operation is not in progress.
67 /// - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
68 ///
69 /// # Implementors note
70 /// When binding to a non-zero port, this bind operation shouldn't be affected by the TIME_WAIT
71 /// state of a recently closed socket on the same local address. In practice this means that the SO_REUSEADDR
72 /// socket option should be set implicitly on all platforms, except on Windows where this is the default behavior
73 /// and SO_REUSEADDR performs something different entirely.
74 ///
75 /// Unlike in POSIX, in WASI the bind operation is async. This enables
76 /// interactive WASI hosts to inject permission prompts. Runtimes that
77 /// don't want to make use of this ability can simply call the native
78 /// `bind` as part of either `start-bind` or `finish-bind`.
79 ///
80 /// # References
81 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>
82 /// - <https://man7.org/linux/man-pages/man2/bind.2.html>
83 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind>
84 /// - <https://man.freebsd.org/cgi/man.cgi?query=bind&sektion=2&format=html>
85 @since(version = 0.2.0)
86 start-bind: func(network: borrow<network>, local-address: ip-socket-address) -> result<_, error-code>;
87 @since(version = 0.2.0)
88 finish-bind: func() -> result<_, error-code>;
89
90 /// Connect to a remote endpoint.
91 ///
92 /// On success:
93 /// - the socket is transitioned into the `connected` state.
94 /// - a pair of streams is returned that can be used to read & write to the connection
95 ///
96 /// After a failed connection attempt, the socket will be in the `closed`
97 /// state and the only valid action left is to `drop` the socket. A single
98 /// socket can not be used to connect more than once.
99 ///
100 /// # Typical errors
101 /// - `invalid-argument`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
102 /// - `invalid-argument`: `remote-address` is not a unicast address. (EINVAL, ENETUNREACH on Linux, EAFNOSUPPORT on MacOS)
103 /// - `invalid-argument`: `remote-address` is an IPv4-mapped IPv6 address. (EINVAL, EADDRNOTAVAIL on Illumos)
104 /// - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EADDRNOTAVAIL on Windows)
105 /// - `invalid-argument`: The port in `remote-address` is set to 0. (EADDRNOTAVAIL on Windows)
106 /// - `invalid-argument`: The socket is already attached to a different network. The `network` passed to `connect` must be identical to the one passed to `bind`.
107 /// - `invalid-state`: The socket is already in the `connected` state. (EISCONN)
108 /// - `invalid-state`: The socket is already in the `listening` state. (EOPNOTSUPP, EINVAL on Windows)
109 /// - `timeout`: Connection timed out. (ETIMEDOUT)
110 /// - `connection-refused`: The connection was forcefully rejected. (ECONNREFUSED)
111 /// - `connection-reset`: The connection was reset. (ECONNRESET)
112 /// - `connection-aborted`: The connection was aborted. (ECONNABORTED)
113 /// - `remote-unreachable`: The remote address is not reachable. (EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
114 /// - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
115 /// - `not-in-progress`: A connect operation is not in progress.
116 /// - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
117 ///
118 /// # Implementors note
119 /// The POSIX equivalent of `start-connect` is the regular `connect` syscall.
120 /// Because all WASI sockets are non-blocking this is expected to return
121 /// EINPROGRESS, which should be translated to `ok()` in WASI.
122 ///
123 /// The POSIX equivalent of `finish-connect` is a `poll` for event `POLLOUT`
124 /// with a timeout of 0 on the socket descriptor. Followed by a check for
125 /// the `SO_ERROR` socket option, in case the poll signaled readiness.
126 ///
127 /// # References
128 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>
129 /// - <https://man7.org/linux/man-pages/man2/connect.2.html>
130 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect>
131 /// - <https://man.freebsd.org/cgi/man.cgi?connect>
132 @since(version = 0.2.0)
133 start-connect: func(network: borrow<network>, remote-address: ip-socket-address) -> result<_, error-code>;
134 @since(version = 0.2.0)
135 finish-connect: func() -> result<tuple<input-stream, output-stream>, error-code>;
136
137 /// Start listening for new connections.
138 ///
139 /// Transitions the socket into the `listening` state.
140 ///
141 /// Unlike POSIX, the socket must already be explicitly bound.
142 ///
143 /// # Typical errors
144 /// - `invalid-state`: The socket is not bound to any local address. (EDESTADDRREQ)
145 /// - `invalid-state`: The socket is already in the `connected` state. (EISCONN, EINVAL on BSD)
146 /// - `invalid-state`: The socket is already in the `listening` state.
147 /// - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE)
148 /// - `not-in-progress`: A listen operation is not in progress.
149 /// - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
150 ///
151 /// # Implementors note
152 /// Unlike in POSIX, in WASI the listen operation is async. This enables
153 /// interactive WASI hosts to inject permission prompts. Runtimes that
154 /// don't want to make use of this ability can simply call the native
155 /// `listen` as part of either `start-listen` or `finish-listen`.
156 ///
157 /// # References
158 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html>
159 /// - <https://man7.org/linux/man-pages/man2/listen.2.html>
160 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen>
161 /// - <https://man.freebsd.org/cgi/man.cgi?query=listen&sektion=2>
162 @since(version = 0.2.0)
163 start-listen: func() -> result<_, error-code>;
164 @since(version = 0.2.0)
165 finish-listen: func() -> result<_, error-code>;
166
167 /// Accept a new client socket.
168 ///
169 /// The returned socket is bound and in the `connected` state. The following properties are inherited from the listener socket:
170 /// - `address-family`
171 /// - `keep-alive-enabled`
172 /// - `keep-alive-idle-time`
173 /// - `keep-alive-interval`
174 /// - `keep-alive-count`
175 /// - `hop-limit`
176 /// - `receive-buffer-size`
177 /// - `send-buffer-size`
178 ///
179 /// On success, this function returns the newly accepted client socket along with
180 /// a pair of streams that can be used to read & write to the connection.
181 ///
182 /// # Typical errors
183 /// - `invalid-state`: Socket is not in the `listening` state. (EINVAL)
184 /// - `would-block`: No pending connections at the moment. (EWOULDBLOCK, EAGAIN)
185 /// - `connection-aborted`: An incoming connection was pending, but was terminated by the client before this listener could accept it. (ECONNABORTED)
186 /// - `new-socket-limit`: The new socket resource could not be created because of a system limit. (EMFILE, ENFILE)
187 ///
188 /// # References
189 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html>
190 /// - <https://man7.org/linux/man-pages/man2/accept.2.html>
191 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept>
192 /// - <https://man.freebsd.org/cgi/man.cgi?query=accept&sektion=2>
193 @since(version = 0.2.0)
194 accept: func() -> result<tuple<tcp-socket, input-stream, output-stream>, error-code>;
195
196 /// Get the bound local address.
197 ///
198 /// POSIX mentions:
199 /// > If the socket has not been bound to a local name, the value
200 /// > stored in the object pointed to by `address` is unspecified.
201 ///
202 /// WASI is stricter and requires `local-address` to return `invalid-state` when the socket hasn't been bound yet.
203 ///
204 /// # Typical errors
205 /// - `invalid-state`: The socket is not bound to any local address.
206 ///
207 /// # References
208 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>
209 /// - <https://man7.org/linux/man-pages/man2/getsockname.2.html>
210 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockname>
211 /// - <https://man.freebsd.org/cgi/man.cgi?getsockname>
212 @since(version = 0.2.0)
213 local-address: func() -> result<ip-socket-address, error-code>;
214
215 /// Get the remote address.
216 ///
217 /// # Typical errors
218 /// - `invalid-state`: The socket is not connected to a remote address. (ENOTCONN)
219 ///
220 /// # References
221 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>
222 /// - <https://man7.org/linux/man-pages/man2/getpeername.2.html>
223 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getpeername>
224 /// - <https://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2&n=1>
225 @since(version = 0.2.0)
226 remote-address: func() -> result<ip-socket-address, error-code>;
227
228 /// Whether the socket is in the `listening` state.
229 ///
230 /// Equivalent to the SO_ACCEPTCONN socket option.
231 @since(version = 0.2.0)
232 is-listening: func() -> bool;
233
234 /// Whether this is a IPv4 or IPv6 socket.
235 ///
236 /// Equivalent to the SO_DOMAIN socket option.
237 @since(version = 0.2.0)
238 address-family: func() -> ip-address-family;
239
240 /// Hints the desired listen queue size. Implementations are free to ignore this.
241 ///
242 /// If the provided value is 0, an `invalid-argument` error is returned.
243 /// Any other value will never cause an error, but it might be silently clamped and/or rounded.
244 ///
245 /// # Typical errors
246 /// - `not-supported`: (set) The platform does not support changing the backlog size after the initial listen.
247 /// - `invalid-argument`: (set) The provided value was 0.
248 /// - `invalid-state`: (set) The socket is in the `connect-in-progress` or `connected` state.
249 @since(version = 0.2.0)
250 set-listen-backlog-size: func(value: u64) -> result<_, error-code>;
251
252 /// Enables or disables keepalive.
253 ///
254 /// The keepalive behavior can be adjusted using:
255 /// - `keep-alive-idle-time`
256 /// - `keep-alive-interval`
257 /// - `keep-alive-count`
258 /// These properties can be configured while `keep-alive-enabled` is false, but only come into effect when `keep-alive-enabled` is true.
259 ///
260 /// Equivalent to the SO_KEEPALIVE socket option.
261 @since(version = 0.2.0)
262 keep-alive-enabled: func() -> result<bool, error-code>;
263 @since(version = 0.2.0)
264 set-keep-alive-enabled: func(value: bool) -> result<_, error-code>;
265
266 /// Amount of time the connection has to be idle before TCP starts sending keepalive packets.
267 ///
268 /// If the provided value is 0, an `invalid-argument` error is returned.
269 /// Any other value will never cause an error, but it might be silently clamped and/or rounded.
270 /// I.e. after setting a value, reading the same setting back may return a different value.
271 ///
272 /// Equivalent to the TCP_KEEPIDLE socket option. (TCP_KEEPALIVE on MacOS)
273 ///
274 /// # Typical errors
275 /// - `invalid-argument`: (set) The provided value was 0.
276 @since(version = 0.2.0)
277 keep-alive-idle-time: func() -> result<duration, error-code>;
278 @since(version = 0.2.0)
279 set-keep-alive-idle-time: func(value: duration) -> result<_, error-code>;
280
281 /// The time between keepalive packets.
282 ///
283 /// If the provided value is 0, an `invalid-argument` error is returned.
284 /// Any other value will never cause an error, but it might be silently clamped and/or rounded.
285 /// I.e. after setting a value, reading the same setting back may return a different value.
286 ///
287 /// Equivalent to the TCP_KEEPINTVL socket option.
288 ///
289 /// # Typical errors
290 /// - `invalid-argument`: (set) The provided value was 0.
291 @since(version = 0.2.0)
292 keep-alive-interval: func() -> result<duration, error-code>;
293 @since(version = 0.2.0)
294 set-keep-alive-interval: func(value: duration) -> result<_, error-code>;
295
296 /// The maximum amount of keepalive packets TCP should send before aborting the connection.
297 ///
298 /// If the provided value is 0, an `invalid-argument` error is returned.
299 /// Any other value will never cause an error, but it might be silently clamped and/or rounded.
300 /// I.e. after setting a value, reading the same setting back may return a different value.
301 ///
302 /// Equivalent to the TCP_KEEPCNT socket option.
303 ///
304 /// # Typical errors
305 /// - `invalid-argument`: (set) The provided value was 0.
306 @since(version = 0.2.0)
307 keep-alive-count: func() -> result<u32, error-code>;
308 @since(version = 0.2.0)
309 set-keep-alive-count: func(value: u32) -> result<_, error-code>;
310
311 /// Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
312 ///
313 /// If the provided value is 0, an `invalid-argument` error is returned.
314 ///
315 /// # Typical errors
316 /// - `invalid-argument`: (set) The TTL value must be 1 or higher.
317 @since(version = 0.2.0)
318 hop-limit: func() -> result<u8, error-code>;
319 @since(version = 0.2.0)
320 set-hop-limit: func(value: u8) -> result<_, error-code>;
321
322 /// The kernel buffer space reserved for sends/receives on this socket.
323 ///
324 /// If the provided value is 0, an `invalid-argument` error is returned.
325 /// Any other value will never cause an error, but it might be silently clamped and/or rounded.
326 /// I.e. after setting a value, reading the same setting back may return a different value.
327 ///
328 /// Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
329 ///
330 /// # Typical errors
331 /// - `invalid-argument`: (set) The provided value was 0.
332 @since(version = 0.2.0)
333 receive-buffer-size: func() -> result<u64, error-code>;
334 @since(version = 0.2.0)
335 set-receive-buffer-size: func(value: u64) -> result<_, error-code>;
336 @since(version = 0.2.0)
337 send-buffer-size: func() -> result<u64, error-code>;
338 @since(version = 0.2.0)
339 set-send-buffer-size: func(value: u64) -> result<_, error-code>;
340
341 /// Create a `pollable` which can be used to poll for, or block on,
342 /// completion of any of the asynchronous operations of this socket.
343 ///
344 /// When `finish-bind`, `finish-listen`, `finish-connect` or `accept`
345 /// return `error(would-block)`, this pollable can be used to wait for
346 /// their success or failure, after which the method can be retried.
347 ///
348 /// The pollable is not limited to the async operation that happens to be
349 /// in progress at the time of calling `subscribe` (if any). Theoretically,
350 /// `subscribe` only has to be called once per socket and can then be
351 /// (re)used for the remainder of the socket's lifetime.
352 ///
353 /// See <https://github.com/WebAssembly/wasi-sockets/blob/main/TcpSocketOperationalSemantics.md#pollable-readiness>
354 /// for more information.
355 ///
356 /// Note: this function is here for WASI 0.2 only.
357 /// It's planned to be removed when `future` is natively supported in Preview3.
358 @since(version = 0.2.0)
359 subscribe: func() -> pollable;
360
361 /// Initiate a graceful shutdown.
362 ///
363 /// - `receive`: The socket is not expecting to receive any data from
364 /// the peer. The `input-stream` associated with this socket will be
365 /// closed. Any data still in the receive queue at time of calling
366 /// this method will be discarded.
367 /// - `send`: The socket has no more data to send to the peer. The `output-stream`
368 /// associated with this socket will be closed and a FIN packet will be sent.
369 /// - `both`: Same effect as `receive` & `send` combined.
370 ///
371 /// This function is idempotent; shutting down a direction more than once
372 /// has no effect and returns `ok`.
373 ///
374 /// The shutdown function does not close (drop) the socket.
375 ///
376 /// # Typical errors
377 /// - `invalid-state`: The socket is not in the `connected` state. (ENOTCONN)
378 ///
379 /// # References
380 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/shutdown.html>
381 /// - <https://man7.org/linux/man-pages/man2/shutdown.2.html>
382 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-shutdown>
383 /// - <https://man.freebsd.org/cgi/man.cgi?query=shutdown&sektion=2>
384 @since(version = 0.2.0)
385 shutdown: func(shutdown-type: shutdown-type) -> result<_, error-code>;
386 }
387 }