sock_bind()
Binds a socket to a specific address.
Description
The sock_bind()
function is used to bind a socket to a specific address. This function is similar to the bind
function in POSIX, which is used to associate a specific address with a socket.
In POSIX, the bind
function is typically used with sockets that use the PF_INET address family, which corresponds to IPv4 addresses. Similarly, in the WASI context, sock_bind()
uses the PF_INET address family to bind the socket.
Binding a socket to a specific address allows the socket to receive incoming connections or send data from the specified address. It restricts the socket to operate on a specific network interface and port.
Syntax
;;; Note: This is similar to `bind` in POSIX using PF_INET
;;; Bind a socket
(@interface func (export "sock_bind")
;;; File descriptor of the socket to be bind
(param $fd $fd)
;;; Address to bind the socket to
(param $addr (@witx const_pointer $addr_port))
(result $error (expected (error $errno)))
)
Parameters
ctx
: A mutable reference to the function environment.sock
: The file descriptor of the socket to bind.addr
: A WebAssembly pointer to the address to bind the socket to.
Return Value
The function returns an Errno
value indicating the outcome of the operation. If the operation is successful, Errno::Success
is returned. If an error occurs, an appropriate Errno
value is returned.
Notes
- The
sock_bind()
function is used to associate a specific address with a socket. - The behavior and limitations of the
sock_bind()
function may vary depending on the specific runtime environment and underlying networking implementation. It is important to consult the documentation or specifications of the specific environment to understand its behavior in that context. - In POSIX, the
bind
function can be used with different address families and protocols, such as IPv6 (PF_INET6) and Unix domain sockets (PF_UNIX). However, in the current context of WASI, thesock_bind()
function specifically uses the PF_INET address family to bind the socket. - The
addr
parameter should contain the IP address and port number to which the socket will be bound.
Sock Address In Layout
For information on valid sin_family
values refer to the documentation on sock_open()
IPv4
interface sockaddr_in {
sin_family: i16;
sin_port: i16;
sin_addr: i8[4]; // IPv4 address, i.e. 124.456.789.012
sin_zero: i8[8]; // padding for size compatibility with sockaddr
}
IPv6
interface sockaddr_in6 {
sin6_family: i16;
sin6_port: i16;
sin6_flowinfo: i32;
sin6_addr: i16[8];
sin6_scope_id: i32;
}
IPC
interface sockaddr_un {
sun_family: i16;
sun_path: i8[108];
}