| 1 | use winapi::ctypes; |
| 2 | use winapi::shared::minwindef; |
| 3 | use winapi::um::winsock2; |
| 4 | use std::io; |
| 5 | |
| 6 | use super::{htons, ntohs}; |
| 7 | |
| 8 | pub mod public { |
| 9 | |
| 10 | use winapi::ctypes; |
| 11 | use winapi::shared::{in6addr, inaddr, ws2def, ws2ipdef}; |
| 12 | use winapi::um::winsock2; |
| 13 | use super::{htons, ntohs}; |
| 14 | use std::io; |
| 15 | use std::mem; |
| 16 | use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; |
| 17 | |
| 18 | pub type CSocket = winsock2::SOCKET; |
| 19 | pub type Buf = *const ctypes::c_char; |
| 20 | pub type MutBuf = *mut ctypes::c_char; |
| 21 | pub type BufLen = ctypes::c_int; |
| 22 | pub type CouldFail = ctypes::c_int; |
| 23 | pub type SockLen = ctypes::c_int; |
| 24 | pub type MutSockLen = *mut ctypes::c_int; |
| 25 | pub type SockAddr = ws2def::SOCKADDR; |
| 26 | pub type SockAddrIn = ws2def::SOCKADDR_IN; |
| 27 | pub type SockAddrIn6 = ws2ipdef::SOCKADDR_IN6_LH; |
| 28 | pub type SockAddrStorage = ws2def::SOCKADDR_STORAGE; |
| 29 | pub type SockAddrFamily = ws2def::ADDRESS_FAMILY; |
| 30 | pub type SockAddrFamily6 = ws2def::ADDRESS_FAMILY; |
| 31 | pub type InAddr = inaddr::IN_ADDR; |
| 32 | pub type In6Addr = in6addr::IN6_ADDR; |
| 33 | |
| 34 | pub const AF_INET: ctypes::c_int = ws2def::AF_INET; |
| 35 | pub const AF_INET6: ctypes::c_int = ws2def::AF_INET6; |
| 36 | pub const SOCK_RAW: ctypes::c_int = winsock2::SOCK_RAW; |
| 37 | |
| 38 | pub const IPPROTO_IP: ctypes::c_int = ws2def::IPPROTO_IP; |
| 39 | pub const IP_HDRINCL: ctypes::c_int = ws2ipdef::IP_HDRINCL; |
| 40 | pub const IP_TTL: ctypes::c_int = ws2ipdef::IP_TTL; |
| 41 | |
| 42 | pub const IPPROTO_IPV6: ctypes::c_int = ws2def::IPPROTO_IPV6 as ctypes::c_int; |
| 43 | pub const IPV6_UNICAST_HOPS: ctypes::c_int = ws2ipdef::IPV6_UNICAST_HOPS; |
| 44 | |
| 45 | pub const IFF_UP: ctypes::c_int = 0x00000001; |
| 46 | pub const IFF_BROADCAST: ctypes::c_int = 0x00000002; |
| 47 | pub const IFF_LOOPBACK: ctypes::c_int = 0x00000004; |
| 48 | pub const IFF_POINTTOPOINT: ctypes::c_int = 0x00000008; |
| 49 | pub const IFF_POINTOPOINT: ctypes::c_int = IFF_POINTTOPOINT; |
| 50 | pub const IFF_MULTICAST: ctypes::c_int = 0x00000010; |
| 51 | |
| 52 | pub const INVALID_SOCKET: CSocket = winsock2::INVALID_SOCKET; |
| 53 | |
| 54 | pub unsafe fn close(sock: CSocket) { |
| 55 | let _ = winsock2::closesocket(sock); |
| 56 | } |
| 57 | |
| 58 | pub unsafe fn socket(af: ctypes::c_int, sock: ctypes::c_int, proto: ctypes::c_int) -> CSocket { |
| 59 | winsock2::socket(af, sock, proto) |
| 60 | } |
| 61 | |
| 62 | pub unsafe fn setsockopt( |
| 63 | socket: CSocket, |
| 64 | level: ctypes::c_int, |
| 65 | name: ctypes::c_int, |
| 66 | value: Buf, |
| 67 | option_len: SockLen, |
| 68 | ) -> ctypes::c_int { |
| 69 | winsock2::setsockopt(socket, level, name, value, option_len) |
| 70 | } |
| 71 | |
| 72 | pub fn make_in6_addr(segments: [u16; 8]) -> In6Addr { |
| 73 | unsafe { |
| 74 | let mut val: In6Addr = mem::zeroed(); |
| 75 | *val.u.Word_mut() = [ |
| 76 | htons(segments[0]), |
| 77 | htons(segments[1]), |
| 78 | htons(segments[2]), |
| 79 | htons(segments[3]), |
| 80 | htons(segments[4]), |
| 81 | htons(segments[5]), |
| 82 | htons(segments[6]), |
| 83 | htons(segments[7]), |
| 84 | ]; |
| 85 | |
| 86 | val |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | pub fn addr_to_sockaddr(addr: SocketAddr, storage: &mut SockAddrStorage) -> SockLen { |
| 91 | unsafe { |
| 92 | let len = match addr { |
| 93 | SocketAddr::V4(sa) => { |
| 94 | let ip_addr = sa.ip(); |
| 95 | let octets = ip_addr.octets(); |
| 96 | let inaddr = super::mk_inaddr(u32::from_be( |
| 97 | ((octets[0] as u32) << 24) |
| 98 | | ((octets[1] as u32) << 16) |
| 99 | | ((octets[2] as u32) << 8) |
| 100 | | (octets[3] as u32), |
| 101 | )); |
| 102 | let storage = storage as *mut _ as *mut SockAddrIn; |
| 103 | (*storage).sin_family = AF_INET as SockAddrFamily; |
| 104 | (*storage).sin_port = htons(addr.port()); |
| 105 | (*storage).sin_addr = inaddr; |
| 106 | mem::size_of::<SockAddrIn>() |
| 107 | } |
| 108 | SocketAddr::V6(sa) => { |
| 109 | let ip_addr = sa.ip(); |
| 110 | let segments = ip_addr.segments(); |
| 111 | let inaddr = make_in6_addr(segments); |
| 112 | let storage = storage as *mut _ as *mut SockAddrIn6; |
| 113 | (*storage).sin6_family = AF_INET6 as SockAddrFamily6; |
| 114 | (*storage).sin6_port = htons(addr.port()); |
| 115 | (*storage).sin6_addr = inaddr; |
| 116 | (*(*storage).u.sin6_scope_id_mut()) = sa.scope_id(); |
| 117 | mem::size_of::<SockAddrIn6>() |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | len as SockLen |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | pub fn sockaddr_to_addr(storage: &SockAddrStorage, len: usize) -> io::Result<SocketAddr> { |
| 126 | unsafe { |
| 127 | match storage.ss_family as ctypes::c_int { |
| 128 | AF_INET => { |
| 129 | assert!(len as usize >= mem::size_of::<SockAddrIn>()); |
| 130 | let storage: &SockAddrIn = mem::transmute(storage); |
| 131 | let ip = super::ipv4_addr(storage.sin_addr); |
| 132 | let a = (ip >> 24) as u8; |
| 133 | let b = (ip >> 16) as u8; |
| 134 | let c = (ip >> 8) as u8; |
| 135 | let d = ip as u8; |
| 136 | let sockaddrv4 = |
| 137 | SocketAddrV4::new(Ipv4Addr::new(a, b, c, d), ntohs(storage.sin_port)); |
| 138 | Ok(SocketAddr::V4(sockaddrv4)) |
| 139 | } |
| 140 | AF_INET6 => { |
| 141 | assert!(len as usize >= mem::size_of::<SockAddrIn6>()); |
| 142 | let storage: &SockAddrIn6 = mem::transmute(storage); |
| 143 | let arr: [u16; 8] = mem::transmute(*storage.sin6_addr.u.Word()); |
| 144 | let a = ntohs(arr[0]); |
| 145 | let b = ntohs(arr[1]); |
| 146 | let c = ntohs(arr[2]); |
| 147 | let d = ntohs(arr[3]); |
| 148 | let e = ntohs(arr[4]); |
| 149 | let f = ntohs(arr[5]); |
| 150 | let g = ntohs(arr[6]); |
| 151 | let h = ntohs(arr[7]); |
| 152 | let ip = Ipv6Addr::new(a, b, c, d, e, f, g, h); |
| 153 | Ok(SocketAddr::V6(SocketAddrV6::new( |
| 154 | ip, |
| 155 | ntohs(storage.sin6_port), |
| 156 | u32::from_be(storage.sin6_flowinfo), |
| 157 | *storage.u.sin6_scope_id(), |
| 158 | ))) |
| 159 | } |
| 160 | _ => Err(io::Error::new( |
| 161 | io::ErrorKind::InvalidData, |
| 162 | "expected IPv4 or IPv6 socket", |
| 163 | )), |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | pub unsafe fn getsockopt( |
| 169 | socket: CSocket, |
| 170 | level: libc::c_int, |
| 171 | name: libc::c_int, |
| 172 | value: MutBuf, |
| 173 | option_len: MutSockLen, |
| 174 | ) -> libc::c_int { |
| 175 | winsock2::getsockopt(socket, level, name, value, option_len) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | use self::public::*; |
| 180 | |
| 181 | use std::mem; |
| 182 | |
| 183 | #[inline(always)] |
| 184 | pub fn ipv4_addr(addr: InAddr) -> u32 { |
| 185 | unsafe { (*addr.S_un.S_addr() as u32).to_be() } |
| 186 | } |
| 187 | |
| 188 | #[inline(always)] |
| 189 | pub fn mk_inaddr(addr: u32) -> InAddr { |
| 190 | unsafe { |
| 191 | let mut val: InAddr = mem::zeroed(); |
| 192 | *val.S_un.S_addr_mut() = addr as minwindef::ULONG; |
| 193 | |
| 194 | val |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | pub unsafe fn sendto( |
| 199 | socket: CSocket, |
| 200 | buf: Buf, |
| 201 | len: BufLen, |
| 202 | flags: ctypes::c_int, |
| 203 | to: *const SockAddr, |
| 204 | tolen: SockLen, |
| 205 | ) -> CouldFail { |
| 206 | winsock2::sendto(socket, buf, len, flags, to, tolen) |
| 207 | } |
| 208 | |
| 209 | pub unsafe fn recvfrom( |
| 210 | socket: CSocket, |
| 211 | buf: MutBuf, |
| 212 | len: BufLen, |
| 213 | flags: ctypes::c_int, |
| 214 | addr: *mut SockAddr, |
| 215 | addrlen: *mut SockLen, |
| 216 | ) -> CouldFail { |
| 217 | winsock2::recvfrom(socket, buf, len, flags, addr, addrlen) |
| 218 | } |
| 219 | |
| 220 | #[inline] |
| 221 | pub fn retry<F>(f: &mut F) -> ctypes::c_int |
| 222 | where |
| 223 | F: FnMut() -> ctypes::c_int, |
| 224 | { |
| 225 | loop { |
| 226 | let ret = f(); |
| 227 | if ret != -1 || errno() as isize != winapi::shared::winerror::WSAEINTR as isize { |
| 228 | return ret; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | fn errno() -> i32 { |
| 234 | io::Error::last_os_error().raw_os_error().unwrap() |
| 235 | } |