| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#![deny(warnings)] |
| 10 |
extern crate libc; |
| 11 |
|
| 12 |
use std::io; |
| 13 |
use std::mem; |
| 14 |
|
| 15 |
#[cfg(unix)] |
| 16 |
use std::time::Duration; |
| 17 |
|
| 18 |
#[cfg(unix)] |
| 19 |
#[path = "unix.rs"] |
| 20 |
mod imp; |
| 21 |
|
| 22 |
#[cfg(windows)] |
| 23 |
#[path = "windows.rs"] |
| 24 |
mod imp; |
| 25 |
|
| 26 |
pub use self::imp::public::*; |
| 27 |
|
| 28 |
|
| 29 |
pub struct FileDesc { |
| 30 |
pub fd: CSocket, |
| 31 |
} |
| 32 |
|
| 33 |
impl Drop for FileDesc { |
| 34 |
fn drop(&mut self) { |
| 35 |
unsafe { |
| 36 |
close(self.fd); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
pub fn send_to( |
| 42 |
socket: CSocket, |
| 43 |
buffer: &[u8], |
| 44 |
dst: *const SockAddr, |
| 45 |
slen: SockLen, |
| 46 |
) -> io::Result<usize> { |
| 47 |
let send_len = imp::retry(&mut || unsafe { |
| 48 |
imp::sendto( |
| 49 |
socket, |
| 50 |
buffer.as_ptr() as Buf, |
| 51 |
buffer.len() as BufLen, |
| 52 |
0, |
| 53 |
dst, |
| 54 |
slen, |
| 55 |
) |
| 56 |
}); |
| 57 |
|
| 58 |
if send_len < 0 { |
| 59 |
Err(io::Error::last_os_error()) |
| 60 |
} else { |
| 61 |
Ok(send_len as usize) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
pub fn recv_from( |
| 66 |
socket: CSocket, |
| 67 |
buffer: &mut [u8], |
| 68 |
caddr: *mut SockAddrStorage, |
| 69 |
) -> io::Result<usize> { |
| 70 |
let mut caddrlen = mem::size_of::<SockAddrStorage>() as SockLen; |
| 71 |
let len = imp::retry(&mut || unsafe { |
| 72 |
imp::recvfrom( |
| 73 |
socket, |
| 74 |
buffer.as_ptr() as MutBuf, |
| 75 |
buffer.len() as BufLen, |
| 76 |
0, |
| 77 |
caddr as *mut SockAddr, |
| 78 |
&mut caddrlen, |
| 79 |
) |
| 80 |
}); |
| 81 |
|
| 82 |
if len < 0 { |
| 83 |
Err(io::Error::last_os_error()) |
| 84 |
} else { |
| 85 |
Ok(len as usize) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
#[cfg(unix)] |
| 91 |
pub fn set_socket_receive_timeout(socket: CSocket, t: Duration) -> io::Result<()> { |
| 92 |
let ts = duration_to_timeval(t); |
| 93 |
let r = unsafe { |
| 94 |
setsockopt( |
| 95 |
socket, |
| 96 |
SOL_SOCKET, |
| 97 |
SO_RCVTIMEO, |
| 98 |
(&ts as *const libc::timeval) as Buf, |
| 99 |
mem::size_of::<libc::timeval>() as SockLen, |
| 100 |
) |
| 101 |
}; |
| 102 |
|
| 103 |
if r < 0 { |
| 104 |
Err(io::Error::last_os_error()) |
| 105 |
} else if r > 0 { |
| 106 |
Err(io::Error::new( |
| 107 |
io::ErrorKind::Other, |
| 108 |
format!("Unknown return value from getsockopt(): {}", r), |
| 109 |
)) |
| 110 |
} else { |
| 111 |
Ok(()) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
#[cfg(unix)] |
| 117 |
pub fn get_socket_receive_timeout(socket: CSocket) -> io::Result<Duration> { |
| 118 |
let ts = libc::timeval { |
| 119 |
tv_sec: 0, |
| 120 |
tv_usec: 0, |
| 121 |
}; |
| 122 |
let len: SockLen = mem::size_of::<libc::timeval>() as SockLen; |
| 123 |
let r = unsafe { |
| 124 |
getsockopt( |
| 125 |
socket, |
| 126 |
SOL_SOCKET, |
| 127 |
SO_RCVTIMEO, |
| 128 |
(&ts as *const libc::timeval) as MutBuf, |
| 129 |
(&len as *const SockLen) as MutSockLen, |
| 130 |
) |
| 131 |
}; |
| 132 |
assert_eq!( |
| 133 |
len, |
| 134 |
mem::size_of::<libc::timeval>() as SockLen, |
| 135 |
"getsockopt did not set size of return value" |
| 136 |
); |
| 137 |
|
| 138 |
if r < 0 { |
| 139 |
Err(io::Error::last_os_error()) |
| 140 |
} else if r > 0 { |
| 141 |
Err(io::Error::new( |
| 142 |
io::ErrorKind::Other, |
| 143 |
format!("Unknown return value from getsockopt(): {}", r), |
| 144 |
)) |
| 145 |
} else { |
| 146 |
Ok(timeval_to_duration(ts)) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
fn htons(u: u16) -> u16 { |
| 153 |
u.to_be() |
| 154 |
} |
| 155 |
fn ntohs(u: u16) -> u16 { |
| 156 |
u16::from_be(u) |
| 157 |
} |
| 158 |
|
| 159 |
#[cfg(test)] |
| 160 |
mod tests { |
| 161 |
use crate::get_socket_receive_timeout; |
| 162 |
use crate::recv_from; |
| 163 |
use crate::set_socket_receive_timeout; |
| 164 |
use std::mem; |
| 165 |
use std::time::{Duration, Instant}; |
| 166 |
use crate::CSocket; |
| 167 |
use crate::SockAddrStorage; |
| 168 |
|
| 169 |
fn test_timeout(socket: CSocket) -> Duration { |
| 170 |
let mut buffer = [0u8; 1024]; |
| 171 |
let mut caddr: SockAddrStorage = unsafe { mem::zeroed() }; |
| 172 |
|
| 173 |
let t0 = Instant::now(); |
| 174 |
let res = recv_from(socket, &mut buffer, &mut caddr); |
| 175 |
assert!(!res.is_ok()); |
| 176 |
Instant::now() - t0 |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn test_set_socket_receive_timeout_1s() { |
| 181 |
let socket = unsafe { libc::socket(libc::AF_INET, libc::SOCK_RAW, 1 as libc::c_int) }; |
| 182 |
let d = Duration::new(1, 0); |
| 183 |
let res = set_socket_receive_timeout(socket, d.clone()); |
| 184 |
match res { |
| 185 |
Err(e) => panic!("set_socket_receive_timeout reslted in error: {}", e), |
| 186 |
_ => {} |
| 187 |
}; |
| 188 |
|
| 189 |
let t = test_timeout(socket); |
| 190 |
assert!(t >= Duration::new(1, 0)); |
| 191 |
assert!(t < Duration::from_millis(1100)); |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn test_set_socket_receive_timeout_500ms() { |
| 196 |
let socket = unsafe { libc::socket(libc::AF_INET, libc::SOCK_RAW, 1 as libc::c_int) }; |
| 197 |
let d = Duration::from_millis(500); |
| 198 |
let res = set_socket_receive_timeout(socket, d); |
| 199 |
match res { |
| 200 |
Err(e) => panic!("set_socket_receive_timeout reslted in error: {}", e), |
| 201 |
_ => {} |
| 202 |
}; |
| 203 |
|
| 204 |
let t = test_timeout(socket); |
| 205 |
assert!(t >= Duration::from_millis(500)); |
| 206 |
assert!(t < Duration::from_millis(600)); |
| 207 |
} |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn test_set_socket_receive_timeout_1500ms() { |
| 211 |
let socket = unsafe { libc::socket(libc::AF_INET, libc::SOCK_RAW, 1 as libc::c_int) }; |
| 212 |
let d = Duration::from_millis(1500); |
| 213 |
let res = set_socket_receive_timeout(socket, d); |
| 214 |
match res { |
| 215 |
Err(e) => panic!("set_socket_receive_timeout reslted in error: {}", e), |
| 216 |
_ => {} |
| 217 |
}; |
| 218 |
|
| 219 |
let t = test_timeout(socket); |
| 220 |
assert!(t >= Duration::from_millis(1500)); |
| 221 |
assert!(t < Duration::from_millis(1600)); |
| 222 |
} |
| 223 |
|
| •224 |
#[cfg(not(target_arch = "arm"))] |
| 225 |
#[test] |
| 226 |
fn test_get_socket_receive_timeout() { |
| 227 |
let socket = unsafe { libc::socket(libc::AF_INET, libc::SOCK_RAW, 1 as libc::c_int) }; |
| 228 |
let s1 = Duration::new(1, 0); |
| 229 |
set_socket_receive_timeout(socket, s1).ok(); |
| 230 |
let g1 = get_socket_receive_timeout(socket); |
| 231 |
match g1 { |
| 232 |
Err(e) => panic!("get_socket_receive_timeout resulted in error: {}", e), |
| 233 |
Ok(t) => assert_eq!(s1, t, "Expected to receive 1s timeout"), |
| 234 |
} |
| 235 |
|
| 236 |
let s2 = Duration::from_millis(500); |
| 237 |
set_socket_receive_timeout(socket, s2).ok(); |
| 238 |
let g2 = get_socket_receive_timeout(socket); |
| 239 |
match g2 { |
| 240 |
Err(e) => panic!("get_socket_receive_timeout resulted in error: {}", e), |
| 241 |
Ok(t) => assert_eq!(s2, t, "Expected to receive 500ms timeout"), |
| 242 |
} |
| 243 |
} |
| 244 |
} |