Debian Codegraph @

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

raw
1 @since(version = 0.2.0)
2 interface udp {
3 @since(version = 0.2.0)
4 use wasi:io/[email protected].{pollable};
5 @since(version = 0.2.0)
6 use network.{network, error-code, ip-socket-address, ip-address-family};
7
8 /// A received datagram.
9 @since(version = 0.2.0)
10 record incoming-datagram {
11 /// The payload.
12 ///
13 /// Theoretical max size: ~64 KiB. In practice, typically less than 1500 bytes.
14 data: list<u8>,
15
16 /// The source address.
17 ///
18 /// This field is guaranteed to match the remote address the stream was initialized with, if any.
19 ///
20 /// Equivalent to the `src_addr` out parameter of `recvfrom`.
21 remote-address: ip-socket-address,
22 }
23
24 /// A datagram to be sent out.
25 @since(version = 0.2.0)
26 record outgoing-datagram {
27 /// The payload.
28 data: list<u8>,
29
30 /// The destination address.
31 ///
32 /// The requirements on this field depend on how the stream was initialized:
33 /// - with a remote address: this field must be None or match the stream's remote address exactly.
34 /// - without a remote address: this field is required.
35 ///
36 /// If this value is None, the send operation is equivalent to `send` in POSIX. Otherwise it is equivalent to `sendto`.
37 remote-address: option<ip-socket-address>,
38 }
39
40 /// A UDP socket handle.
41 @since(version = 0.2.0)
42 resource udp-socket {
43 /// Bind the socket to a specific network on the provided IP address and port.
44 ///
45 /// If the IP address is zero (`0.0.0.0` in IPv4, `::` in IPv6), it is left to the implementation to decide which
46 /// network interface(s) to bind to.
47 /// If the port is zero, the socket will be bound to a random free port.
48 ///
49 /// # Typical errors
50 /// - `invalid-argument`: The `local-address` has the wrong address family. (EAFNOSUPPORT, EFAULT on Windows)
51 /// - `invalid-state`: The socket is already bound. (EINVAL)
52 /// - `address-in-use`: No ephemeral ports available. (EADDRINUSE, ENOBUFS on Windows)
53 /// - `address-in-use`: Address is already in use. (EADDRINUSE)
54 /// - `address-not-bindable`: `local-address` is not an address that the `network` can bind to. (EADDRNOTAVAIL)
55 /// - `not-in-progress`: A `bind` operation is not in progress.
56 /// - `would-block`: Can't finish the operation, it is still in progress. (EWOULDBLOCK, EAGAIN)
57 ///
58 /// # Implementors note
59 /// Unlike in POSIX, in WASI the bind operation is async. This enables
60 /// interactive WASI hosts to inject permission prompts. Runtimes that
61 /// don't want to make use of this ability can simply call the native
62 /// `bind` as part of either `start-bind` or `finish-bind`.
63 ///
64 /// # References
65 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>
66 /// - <https://man7.org/linux/man-pages/man2/bind.2.html>
67 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind>
68 /// - <https://man.freebsd.org/cgi/man.cgi?query=bind&sektion=2&format=html>
69 @since(version = 0.2.0)
70 start-bind: func(network: borrow<network>, local-address: ip-socket-address) -> result<_, error-code>;
71 @since(version = 0.2.0)
72 finish-bind: func() -> result<_, error-code>;
73
74 /// Set up inbound & outbound communication channels, optionally to a specific peer.
75 ///
76 /// This function only changes the local socket configuration and does not generate any network traffic.
77 /// On success, the `remote-address` of the socket is updated. The `local-address` may be updated as well,
78 /// based on the best network path to `remote-address`.
79 ///
80 /// When a `remote-address` is provided, the returned streams are limited to communicating with that specific peer:
81 /// - `send` can only be used to send to this destination.
82 /// - `receive` will only return datagrams sent from the provided `remote-address`.
83 ///
84 /// This method may be called multiple times on the same socket to change its association, but
85 /// only the most recently returned pair of streams will be operational. Implementations may trap if
86 /// the streams returned by a previous invocation haven't been dropped yet before calling `stream` again.
87 ///
88 /// The POSIX equivalent in pseudo-code is:
89 /// ```text
90 /// if (was previously connected) {
91 /// connect(s, AF_UNSPEC)
92 /// }
93 /// if (remote_address is Some) {
94 /// connect(s, remote_address)
95 /// }
96 /// ```
97 ///
98 /// Unlike in POSIX, the socket must already be explicitly bound.
99 ///
100 /// # Typical errors
101 /// - `invalid-argument`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
102 /// - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EDESTADDRREQ, EADDRNOTAVAIL)
103 /// - `invalid-argument`: The port in `remote-address` is set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)
104 /// - `invalid-state`: The socket is not bound.
105 /// - `address-in-use`: Tried to perform an implicit bind, but there were no ephemeral ports available. (EADDRINUSE, EADDRNOTAVAIL on Linux, EAGAIN on BSD)
106 /// - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, ENETRESET, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
107 /// - `connection-refused`: The connection was refused. (ECONNREFUSED)
108 ///
109 /// # References
110 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>
111 /// - <https://man7.org/linux/man-pages/man2/connect.2.html>
112 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect>
113 /// - <https://man.freebsd.org/cgi/man.cgi?connect>
114 @since(version = 0.2.0)
115 %stream: func(remote-address: option<ip-socket-address>) -> result<tuple<incoming-datagram-stream, outgoing-datagram-stream>, error-code>;
116
117 /// Get the current bound address.
118 ///
119 /// POSIX mentions:
120 /// > If the socket has not been bound to a local name, the value
121 /// > stored in the object pointed to by `address` is unspecified.
122 ///
123 /// WASI is stricter and requires `local-address` to return `invalid-state` when the socket hasn't been bound yet.
124 ///
125 /// # Typical errors
126 /// - `invalid-state`: The socket is not bound to any local address.
127 ///
128 /// # References
129 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>
130 /// - <https://man7.org/linux/man-pages/man2/getsockname.2.html>
131 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockname>
132 /// - <https://man.freebsd.org/cgi/man.cgi?getsockname>
133 @since(version = 0.2.0)
134 local-address: func() -> result<ip-socket-address, error-code>;
135
136 /// Get the address the socket is currently streaming to.
137 ///
138 /// # Typical errors
139 /// - `invalid-state`: The socket is not streaming to a specific remote address. (ENOTCONN)
140 ///
141 /// # References
142 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>
143 /// - <https://man7.org/linux/man-pages/man2/getpeername.2.html>
144 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getpeername>
145 /// - <https://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2&n=1>
146 @since(version = 0.2.0)
147 remote-address: func() -> result<ip-socket-address, error-code>;
148
149 /// Whether this is a IPv4 or IPv6 socket.
150 ///
151 /// Equivalent to the SO_DOMAIN socket option.
152 @since(version = 0.2.0)
153 address-family: func() -> ip-address-family;
154
155 /// Equivalent to the IP_TTL & IPV6_UNICAST_HOPS socket options.
156 ///
157 /// If the provided value is 0, an `invalid-argument` error is returned.
158 ///
159 /// # Typical errors
160 /// - `invalid-argument`: (set) The TTL value must be 1 or higher.
161 @since(version = 0.2.0)
162 unicast-hop-limit: func() -> result<u8, error-code>;
163 @since(version = 0.2.0)
164 set-unicast-hop-limit: func(value: u8) -> result<_, error-code>;
165
166 /// The kernel buffer space reserved for sends/receives on this socket.
167 ///
168 /// If the provided value is 0, an `invalid-argument` error is returned.
169 /// Any other value will never cause an error, but it might be silently clamped and/or rounded.
170 /// I.e. after setting a value, reading the same setting back may return a different value.
171 ///
172 /// Equivalent to the SO_RCVBUF and SO_SNDBUF socket options.
173 ///
174 /// # Typical errors
175 /// - `invalid-argument`: (set) The provided value was 0.
176 @since(version = 0.2.0)
177 receive-buffer-size: func() -> result<u64, error-code>;
178 @since(version = 0.2.0)
179 set-receive-buffer-size: func(value: u64) -> result<_, error-code>;
180 @since(version = 0.2.0)
181 send-buffer-size: func() -> result<u64, error-code>;
182 @since(version = 0.2.0)
183 set-send-buffer-size: func(value: u64) -> result<_, error-code>;
184
185 /// Create a `pollable` which will resolve once the socket is ready for I/O.
186 ///
187 /// Note: this function is here for WASI 0.2 only.
188 /// It's planned to be removed when `future` is natively supported in Preview3.
189 @since(version = 0.2.0)
190 subscribe: func() -> pollable;
191 }
192
193 @since(version = 0.2.0)
194 resource incoming-datagram-stream {
195 /// Receive messages on the socket.
196 ///
197 /// This function attempts to receive up to `max-results` datagrams on the socket without blocking.
198 /// The returned list may contain fewer elements than requested, but never more.
199 ///
200 /// This function returns successfully with an empty list when either:
201 /// - `max-results` is 0, or:
202 /// - `max-results` is greater than 0, but no results are immediately available.
203 /// This function never returns `error(would-block)`.
204 ///
205 /// # Typical errors
206 /// - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
207 /// - `connection-refused`: The connection was refused. (ECONNREFUSED)
208 ///
209 /// # References
210 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvfrom.html>
211 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvmsg.html>
212 /// - <https://man7.org/linux/man-pages/man2/recv.2.html>
213 /// - <https://man7.org/linux/man-pages/man2/recvmmsg.2.html>
214 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recv>
215 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom>
216 /// - <https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms741687(v=vs.85)>
217 /// - <https://man.freebsd.org/cgi/man.cgi?query=recv&sektion=2>
218 @since(version = 0.2.0)
219 receive: func(max-results: u64) -> result<list<incoming-datagram>, error-code>;
220
221 /// Create a `pollable` which will resolve once the stream is ready to receive again.
222 ///
223 /// Note: this function is here for WASI 0.2 only.
224 /// It's planned to be removed when `future` is natively supported in Preview3.
225 @since(version = 0.2.0)
226 subscribe: func() -> pollable;
227 }
228
229 @since(version = 0.2.0)
230 resource outgoing-datagram-stream {
231 /// Check readiness for sending. This function never blocks.
232 ///
233 /// Returns the number of datagrams permitted for the next call to `send`,
234 /// or an error. Calling `send` with more datagrams than this function has
235 /// permitted will trap.
236 ///
237 /// When this function returns ok(0), the `subscribe` pollable will
238 /// become ready when this function will report at least ok(1), or an
239 /// error.
240 ///
241 /// Never returns `would-block`.
242 check-send: func() -> result<u64, error-code>;
243
244 /// Send messages on the socket.
245 ///
246 /// This function attempts to send all provided `datagrams` on the socket without blocking and
247 /// returns how many messages were actually sent (or queued for sending). This function never
248 /// returns `error(would-block)`. If none of the datagrams were able to be sent, `ok(0)` is returned.
249 ///
250 /// This function semantically behaves the same as iterating the `datagrams` list and sequentially
251 /// sending each individual datagram until either the end of the list has been reached or the first error occurred.
252 /// If at least one datagram has been sent successfully, this function never returns an error.
253 ///
254 /// If the input list is empty, the function returns `ok(0)`.
255 ///
256 /// Each call to `send` must be permitted by a preceding `check-send`. Implementations must trap if
257 /// either `check-send` was not called or `datagrams` contains more items than `check-send` permitted.
258 ///
259 /// # Typical errors
260 /// - `invalid-argument`: The `remote-address` has the wrong address family. (EAFNOSUPPORT)
261 /// - `invalid-argument`: The IP address in `remote-address` is set to INADDR_ANY (`0.0.0.0` / `::`). (EDESTADDRREQ, EADDRNOTAVAIL)
262 /// - `invalid-argument`: The port in `remote-address` is set to 0. (EDESTADDRREQ, EADDRNOTAVAIL)
263 /// - `invalid-argument`: The socket is in "connected" mode and `remote-address` is `some` value that does not match the address passed to `stream`. (EISCONN)
264 /// - `invalid-argument`: The socket is not "connected" and no value for `remote-address` was provided. (EDESTADDRREQ)
265 /// - `remote-unreachable`: The remote address is not reachable. (ECONNRESET, ENETRESET on Windows, EHOSTUNREACH, EHOSTDOWN, ENETUNREACH, ENETDOWN, ENONET)
266 /// - `connection-refused`: The connection was refused. (ECONNREFUSED)
267 /// - `datagram-too-large`: The datagram is too large. (EMSGSIZE)
268 ///
269 /// # References
270 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html>
271 /// - <https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendmsg.html>
272 /// - <https://man7.org/linux/man-pages/man2/send.2.html>
273 /// - <https://man7.org/linux/man-pages/man2/sendmmsg.2.html>
274 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-send>
275 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-sendto>
276 /// - <https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasendmsg>
277 /// - <https://man.freebsd.org/cgi/man.cgi?query=send&sektion=2>
278 @since(version = 0.2.0)
279 send: func(datagrams: list<outgoing-datagram>) -> result<u64, error-code>;
280
281 /// Create a `pollable` which will resolve once the stream is ready to send again.
282 ///
283 /// Note: this function is here for WASI 0.2 only.
284 /// It's planned to be removed when `future` is natively supported in Preview3.
285 @since(version = 0.2.0)
286 subscribe: func() -> pollable;
287 }
288 }