Function nc::socket

source ·
pub unsafe fn socket(
    domain: i32,
    sock_type: i32,
    protocol: i32
) -> Result<i32, Errno>
Expand description

Create an endpoint for communication.

§Example

let socket_fd = unsafe { nc::socket(nc::AF_INET, nc::SOCK_STREAM, 0) };
assert!(socket_fd.is_ok());
let socket_fd = socket_fd.unwrap();
let ret = unsafe { nc::close(socket_fd) };
assert!(ret.is_ok());
Examples found in repository?
examples/bind_device.rs (line 6)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() -> Result<(), nc::Errno> {
    let socket_fd = unsafe { nc::socket(nc::AF_INET, nc::SOCK_STREAM, 0)? };
    let interface_name = "lo";
    let ret = unsafe {
        nc::setsockopt(
            socket_fd,
            nc::SOL_SOCKET,
            nc::SO_BINDTODEVICE,
            interface_name.as_ptr() as usize,
            interface_name.len() as nc::socklen_t,
        )
    };
    match ret {
        Err(errno) => eprintln!("socket() err: {}", nc::strerror(errno)),
        Ok(_) => println!("Now socket is bind to {}", interface_name),
    }
    let ret = unsafe { nc::close(socket_fd) };
    assert!(ret.is_ok());
    Ok(())
}
More examples
Hide additional examples
examples/tcp_fast_open.rs (line 6)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
fn main() -> Result<(), nc::Errno> {
    let socket_fd = unsafe { nc::socket(nc::AF_INET, nc::SOCK_STREAM, 0)? };

    // For Linux, value is the queue length of pending packets.
    // See https://github.com/rust-lang/socket2/issues/49
    #[cfg(any(target_os = "linux", target_os = "android"))]
    let queue_len: i32 = 5;
    // For the others, just a boolean value for enable and disable.
    #[cfg(target_os = "freebsd")]
    let queue_len: i32 = 1;
    let queue_len_ptr = &queue_len as *const i32 as usize;

    let ret = unsafe {
        nc::setsockopt(
            socket_fd,
            nc::IPPROTO_TCP,
            nc::TCP_FASTOPEN,
            queue_len_ptr,
            std::mem::size_of_val(&queue_len) as u32,
        )
    };
    assert!(ret.is_ok());

    unsafe { nc::close(socket_fd) }
}
examples/socket.rs (line 15)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
fn main() -> Result<(), Errno> {
    let listen_fd = unsafe { nc::socket(nc::AF_INET, nc::SOCK_STREAM, 0)? };

    let addr = nc::sockaddr_in_t {
        sin_family: nc::AF_INET as nc::sa_family_t,
        sin_port: htons(80),
        sin_addr: nc::in_addr_t {
            s_addr: nc::INADDR_ANY as u32,
        },
        ..Default::default()
    };

    unsafe {
        let addr_alias = transmute::<&nc::sockaddr_in_t, &nc::sockaddr_t>(&addr);
        let ret = nc::bind(listen_fd, addr_alias, size_of::<nc::sockaddr_in_t>() as u32);
        assert_eq!(ret, Err(nc::EACCES));
        return Ok(());

        nc::listen(listen_fd, nc::SOCK_STREAM)?;
    }

    let mut conn_addr = nc::sockaddr_in_t::default();
    let mut conn_addr_len: nc::socklen_t = 0;
    let conn_fd = unsafe {
        nc::accept4(
            listen_fd,
            &mut conn_addr,
            &mut conn_addr_len,
            nc::SOCK_CLOEXEC,
        )?
    };
    println!("conn_fd: {:?}", conn_fd);

    Ok(())
}