1
2
3
4
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/// Platform-compatible socket type definitions.
///
/// The `libc` crate on Windows (MSVC) does not expose POSIX socket types such
/// as `sockaddr_in`, `sockaddr_in6`, `sa_family_t`, `AF_INET`, or `AF_INET6`.
/// This module re-exports them from `libc` on Unix-like targets and provides
/// equivalent definitions (matching the WinSock2 / RFC layouts) on Windows.
#[cfg(not(target_os = "windows"))]
pub use libc::{AF_INET, AF_INET6, sa_family_t, sockaddr_in, sockaddr_in6};
#[cfg(target_os = "windows")]
mod win {
/// WinSock2 `AF_INET` (same value as POSIX: 2).
pub const AF_INET: i32 = 2;
/// WinSock2 `AF_INET6` (23 on Windows, vs 10 on Linux).
pub const AF_INET6: i32 = 23;
pub type sa_family_t = u16;
#[repr(C)]
pub struct in_addr {
pub s_addr: u32,
}
/// Mirrors `SOCKADDR_IN` from `<ws2def.h>`.
#[repr(C)]
pub struct sockaddr_in {
pub sin_family: sa_family_t,
pub sin_port: u16,
pub sin_addr: in_addr,
pub sin_zero: [u8; 8],
}
#[repr(C)]
pub struct in6_addr {
pub s6_addr: [u8; 16],
}
/// Mirrors `SOCKADDR_IN6` from `<ws2ipdef.h>`.
#[repr(C)]
pub struct sockaddr_in6 {
pub sin6_family: sa_family_t,
pub sin6_port: u16,
pub sin6_flowinfo: u32,
pub sin6_addr: in6_addr,
pub sin6_scope_id: u32,
}
}
#[cfg(target_os = "windows")]
pub use win::{AF_INET, AF_INET6, sa_family_t, sockaddr_in, sockaddr_in6};