Debian Codegraph @

src/unix.rs

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 use super::{htons, ntohs};
2 use std::io;
3
4 pub mod public {
5
6 use libc;
7 use super::{htons, ntohs};
8 use std::io;
9 use std::mem;
10 use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
11 use std::time::Duration;
12
13 pub type CSocket = libc::c_int;
14 pub type Buf = *const libc::c_void;
15 pub type MutBuf = *mut libc::c_void;
16 pub type BufLen = libc::size_t;
17 pub type CouldFail = libc::ssize_t;
18 pub type SockLen = libc::socklen_t;
19 pub type MutSockLen = *mut libc::socklen_t;
20 pub type SockAddr = libc::sockaddr;
21 pub type SockAddrIn = libc::sockaddr_in;
22 pub type SockAddrIn6 = libc::sockaddr_in6;
23 pub type SockAddrStorage = libc::sockaddr_storage;
24 pub type SockAddrFamily = libc::sa_family_t;
25 pub type SockAddrFamily6 = libc::sa_family_t;
26 pub type InAddr = libc::in_addr;
27 pub type In6Addr = libc::in6_addr;
28
29 #[cfg(not(any(
30 target_os = "macos",
31 target_os = "ios",
32 target_os = "tvos",
33 target_os = "netbsd"
34 )))]
35 pub type TvUsecType = libc::c_long;
36 #[cfg(any(
37 target_os = "macos",
38 target_os = "ios",
39 target_os = "tvos",
40 target_os = "netbsd"
41 ))]
42 pub type TvUsecType = libc::c_int;
43 #[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
44 pub type InAddrType = libc::c_uint;
45 #[cfg(any(target_os = "illumos", target_os = "solaris"))]
46 pub type InAddrType = libc::c_ulonglong;
47
48 pub const AF_INET: libc::c_int = libc::AF_INET;
49 pub const AF_INET6: libc::c_int = libc::AF_INET6;
50 pub const SOCK_RAW: libc::c_int = libc::SOCK_RAW;
51
52 pub const SOL_SOCKET: libc::c_int = libc::SOL_SOCKET;
53 pub const SO_RCVTIMEO: libc::c_int = libc::SO_RCVTIMEO;
54 pub const SO_SNDTIMEO: libc::c_int = libc::SO_SNDTIMEO;
55
56 pub const IPPROTO_IP: libc::c_int = libc::IPPROTO_IP;
57 pub const IP_HDRINCL: libc::c_int = libc::IP_HDRINCL;
58 pub const IP_TTL: libc::c_int = libc::IP_TTL;
59 pub const IP_TOS: libc::c_int = 1;
60
61 pub const IPPROTO_IPV6: libc::c_int = libc::IPPROTO_IPV6;
62 pub const IPV6_UNICAST_HOPS: libc::c_int = libc::IPV6_UNICAST_HOPS;
63 pub const IPV6_TCLASS: libc::c_int = libc::IPV6_TCLASS;
64
65 pub use libc::{IFF_BROADCAST, IFF_LOOPBACK, IFF_RUNNING, IFF_MULTICAST, IFF_POINTOPOINT, IFF_UP};
66
67 #[cfg(any(target_os = "linux", target_os = "android"))]
68 pub use libc::{IFF_LOWER_UP, IFF_DORMANT};
69
70 pub const INVALID_SOCKET: CSocket = -1;
71
72 pub unsafe fn close(sock: CSocket) {
73 let _ = libc::close(sock);
74 }
75
76 pub unsafe fn socket(af: libc::c_int, sock: libc::c_int, proto: libc::c_int) -> CSocket {
77 libc::socket(af, sock, proto)
78 }
79
80 pub unsafe fn getsockopt(
81 socket: CSocket,
82 level: libc::c_int,
83 name: libc::c_int,
84 value: MutBuf,
85 option_len: MutSockLen,
86 ) -> libc::c_int {
87 libc::getsockopt(socket, level, name, value, option_len)
88 }
89
90 pub unsafe fn setsockopt(
91 socket: CSocket,
92 level: libc::c_int,
93 name: libc::c_int,
94 value: Buf,
95 option_len: SockLen,
96 ) -> libc::c_int {
97 libc::setsockopt(socket, level, name, value, option_len)
98 }
99
100 /// Convert a platform specific `timeval` into a Duration.
101 pub fn timeval_to_duration(tv: libc::timeval) -> Duration {
102 Duration::new(tv.tv_sec as u64, (tv.tv_usec as u32) * 1000)
103 }
104
105 /// Convert a Duration into a platform specific `timeval`.
106 pub fn duration_to_timeval(dur: Duration) -> libc::timeval {
107 libc::timeval {
108 tv_sec: dur.as_secs() as libc::time_t,
109 tv_usec: dur.subsec_micros() as TvUsecType,
110 }
111 }
112
113 /// Convert a platform specific `timespec` into a Duration.
114 pub fn timespec_to_duration(ts: libc::timespec) -> Duration {
115 Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32)
116 }
117
118 /// Convert a Duration into a platform specific `timespec`.
119 pub fn duration_to_timespec(dur: Duration) -> libc::timespec {
120 libc::timespec {
121 tv_sec: dur.as_secs() as libc::time_t,
122 tv_nsec: (dur.subsec_nanos() as TvUsecType).into(),
123 }
124 }
125
126 fn make_in6_addr(segments: [u16; 8]) -> In6Addr {
127 // Safety: We're transmuting an array of ints to an array of ints.
128 // There is no padding involved, and they must be the same size.
129 let s6_addr = unsafe {
130 mem::transmute::<[u16; 8], [u8; 16]>([
131 htons(segments[0]),
132 htons(segments[1]),
133 htons(segments[2]),
134 htons(segments[3]),
135 htons(segments[4]),
136 htons(segments[5]),
137 htons(segments[6]),
138 htons(segments[7]),
139 ])
140 };
141
142 In6Addr { s6_addr }
143 }
144
145 pub fn addr_to_sockaddr(addr: SocketAddr, storage: &mut SockAddrStorage) -> SockLen {
146 unsafe {
147 let len = match addr {
148 SocketAddr::V4(sa) => {
149 let ip_addr = sa.ip();
150 let octets = ip_addr.octets();
151 let inaddr = super::mk_inaddr(u32::from_be(
152 ((octets[0] as u32) << 24)
153 | ((octets[1] as u32) << 16)
154 | ((octets[2] as u32) << 8)
155 | (octets[3] as u32),
156 ));
157 let storage = storage as *mut _ as *mut SockAddrIn;
158 (*storage).sin_family = AF_INET as SockAddrFamily;
159 (*storage).sin_port = htons(addr.port());
160 (*storage).sin_addr = inaddr;
161 mem::size_of::<SockAddrIn>()
162 }
163 SocketAddr::V6(sa) => {
164 let ip_addr = sa.ip();
165 let segments = ip_addr.segments();
166 let inaddr = make_in6_addr(segments);
167 let storage = storage as *mut _ as *mut SockAddrIn6;
168 (*storage).sin6_family = AF_INET6 as SockAddrFamily6;
169 (*storage).sin6_port = htons(addr.port());
170 (*storage).sin6_addr = inaddr;
171 (*storage).sin6_scope_id = sa.scope_id();
172 mem::size_of::<SockAddrIn6>()
173 }
174 };
175
176 len as SockLen
177 }
178 }
179
180 pub fn sockaddr_to_addr(storage: &SockAddrStorage, len: usize) -> io::Result<SocketAddr> {
181 match storage.ss_family as libc::c_int {
182 AF_INET => {
183 assert!(len as usize >= mem::size_of::<SockAddrIn>());
184 let storage: &SockAddrIn = unsafe { mem::transmute(storage) };
185 let ip = super::ipv4_addr(storage.sin_addr);
186 let a = (ip >> 24) as u8;
187 let b = (ip >> 16) as u8;
188 let c = (ip >> 8) as u8;
189 let d = ip as u8;
190 let sockaddrv4 =
191 SocketAddrV4::new(Ipv4Addr::new(a, b, c, d), ntohs(storage.sin_port));
192 Ok(SocketAddr::V4(sockaddrv4))
193 }
194 AF_INET6 => {
195 assert!(len as usize >= mem::size_of::<SockAddrIn6>());
196 let storage: &SockAddrIn6 = unsafe { mem::transmute(storage) };
197 let arr: [u16; 8] = unsafe { mem::transmute(storage.sin6_addr.s6_addr) };
198 let a = ntohs(arr[0]);
199 let b = ntohs(arr[1]);
200 let c = ntohs(arr[2]);
201 let d = ntohs(arr[3]);
202 let e = ntohs(arr[4]);
203 let f = ntohs(arr[5]);
204 let g = ntohs(arr[6]);
205 let h = ntohs(arr[7]);
206 let ip = Ipv6Addr::new(a, b, c, d, e, f, g, h);
207 Ok(SocketAddr::V6(SocketAddrV6::new(
208 ip,
209 ntohs(storage.sin6_port),
210 u32::from_be(storage.sin6_flowinfo),
211 storage.sin6_scope_id,
212 )))
213 }
214 _ => Err(io::Error::new(
215 io::ErrorKind::InvalidData,
216 "expected IPv4 or IPv6 socket",
217 )),
218 }
219 }
220 }
221
222 use self::public::*;
223
224 #[inline(always)]
225 pub fn ipv4_addr(addr: InAddr) -> InAddrType {
226 (addr.s_addr as InAddrType).to_be()
227 }
228
229 #[inline(always)]
230 pub fn mk_inaddr(addr: u32) -> InAddr {
231 InAddr { s_addr: addr }
232 }
233
234 pub unsafe fn sendto(
235 socket: CSocket,
236 buf: Buf,
237 len: BufLen,
238 flags: libc::c_int,
239 addr: *const SockAddr,
240 addrlen: SockLen,
241 ) -> CouldFail {
242 libc::sendto(socket, buf, len, flags, addr, addrlen)
243 }
244
245 pub unsafe fn recvfrom(
246 socket: CSocket,
247 buf: MutBuf,
248 len: BufLen,
249 flags: libc::c_int,
250 addr: *mut SockAddr,
251 addrlen: *mut SockLen,
252 ) -> CouldFail {
253 libc::recvfrom(socket, buf, len, flags, addr, addrlen)
254 }
255
256 #[inline]
257 pub fn retry<F>(f: &mut F) -> libc::ssize_t
258 where
259 F: FnMut() -> libc::ssize_t,
260 {
261 loop {
262 let ret = f();
263 if ret != -1 || errno() as isize != libc::EINTR as isize {
264 return ret;
265 }
266 }
267 }
268
269 fn errno() -> i32 {
270 io::Error::last_os_error().raw_os_error().unwrap()
271 }
272
273 #[cfg(test)]
274 mod tests {
275 use crate::duration_to_timespec;
276 use std::time::Duration;
277 use crate::timespec_to_duration;
278
279 #[test]
280 fn test_duration_to_timespec() {
281 let d1 = Duration::new(1, 0);
282 let d2 = Duration::from_millis(500);
283
284 let t1 = duration_to_timespec(d1);
285 let t2 = duration_to_timespec(d2);
286
287 let r1 = timespec_to_duration(t1);
288 let r2 = timespec_to_duration(t2);
289
290 assert_eq!(d1, r1);
291 assert_eq!(d2, r2);
292 }
293 }