1use crate::SocketErrorKind;
2
3#[link(wasm_import_module = "blockless_socket")]
4extern "C" {
5 #[link_name = "create_tcp_bind_socket"]
6 pub(crate) fn create_tcp_bind_socket_native(
7 addr: *const u8,
8 addr_len: u32,
9 fd: *mut u32,
10 ) -> u32;
11}
12
13pub fn create_tcp_bind_socket(addr: &str) -> Result<u32, SocketErrorKind> {
14 unsafe {
15 let addr_ptr = addr.as_ptr();
16 let mut fd: u32 = 0;
17 let rs = create_tcp_bind_socket_native(addr_ptr, addr.len() as _, (&mut fd) as *mut u32);
18 if rs == 0 {
19 return Ok(fd);
20 }
21 Err(match rs {
22 1 => SocketErrorKind::ConnectRefused,
23 2 => SocketErrorKind::ParameterError,
24 3 => SocketErrorKind::ConnectionReset,
25 4 => SocketErrorKind::AddressInUse,
26 _ => unreachable!("unreach."),
27 })
28 }
29}