Skip to main content

rusl/network/
bind.rs

1use sc::syscall;
2
3use crate::platform::{Fd, SocketAddressInet, SocketArgUnix};
4use crate::Result;
5
6/// Bind the unix-socket with the fd `sock_fd` to the address `socket_address`
7/// See the [Linux documentation for details](https://man7.org/linux/man-pages/man2/connect.2.html)
8/// Similar to `connect` but on the 'server'-side
9/// # Errors
10/// See above
11#[inline]
12pub fn bind_unix(sock_fd: Fd, socket_address: &SocketArgUnix) -> Result<()> {
13    let res = unsafe {
14        syscall!(
15            BIND,
16            sock_fd.0,
17            core::ptr::addr_of!(socket_address.addr),
18            socket_address.addr_len
19        )
20    };
21    bail_on_below_zero!(res, "`BIND` syscall failed");
22    Ok(())
23}
24
25/// Bind the tcp-socket with the fd `sock_fd` to the address `socket_address`
26/// See the [Linux documentation for details](https://man7.org/linux/man-pages/man2/connect.2.html)
27/// Similar to `connect` but on the 'server'-side
28/// # Errors
29/// See above
30pub fn bind_inet(sock_fd: Fd, socket_address_inet: &SocketAddressInet) -> Result<()> {
31    let res = unsafe {
32        syscall!(
33            BIND,
34            sock_fd.0,
35            core::ptr::from_ref::<SocketAddressInet>(socket_address_inet),
36            SocketAddressInet::LENGTH
37        )
38    };
39    bail_on_below_zero!(res, "`BIND` syscall failed");
40    Ok(())
41}