1use crate::SocketErrorKind;
2
3#[cfg(not(feature = "mock-ffi"))]
4#[link(wasm_import_module = "blockless_socket")]
5extern "C" {
6 #[link_name = "create_tcp_bind_socket"]
7 pub(crate) fn create_tcp_bind_socket_native(
8 addr: *const u8,
9 addr_len: u32,
10 fd: *mut u32,
11 ) -> u32;
12}
13
14#[cfg(feature = "mock-ffi")]
15mod mock_ffi {
16 pub unsafe fn create_tcp_bind_socket_native(
17 _addr: *const u8,
18 _addr_len: u32,
19 _fd: *mut u32,
20 ) -> u32 {
21 unimplemented!()
22 }
23}
24
25#[cfg(feature = "mock-ffi")]
26use mock_ffi::*;
27
28pub fn create_tcp_bind_socket(addr: &str) -> Result<u32, SocketErrorKind> {
29 unsafe {
30 let addr_ptr = addr.as_ptr();
31 let mut fd: u32 = 0;
32 let rs = create_tcp_bind_socket_native(addr_ptr, addr.len() as _, (&mut fd) as *mut u32);
33 if rs == 0 {
34 return Ok(fd);
35 }
36 Err(match rs {
37 1 => SocketErrorKind::ConnectRefused,
38 2 => SocketErrorKind::ParameterError,
39 3 => SocketErrorKind::ConnectionReset,
40 4 => SocketErrorKind::AddressInUse,
41 _ => unreachable!("unreach."),
42 })
43 }
44}