1
0
Fork 0

Add function "set_timeout"

This commit is contained in:
Alex Kotov 2021-01-03 05:25:55 +05:00
parent e08d8d69a0
commit e6fa791fd0
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08

View file

@ -39,15 +39,7 @@ fn main() {
send_request(socket, current_ttl, &host, &request); send_request(socket, current_ttl, &host, &request);
let timeval = libc::timeval { tv_sec: 2, tv_usec: 0 }; set_timeout(socket, 2, 0);
assert_eq!(0, unsafe { libc::setsockopt(
socket,
libc::SOL_SOCKET,
libc::SO_RCVTIMEO,
&timeval as *const libc::timeval as *const libc::c_void,
std::mem::size_of::<libc::timeval>().try_into().unwrap(),
) });
let mut response: Option<Response> = None; let mut response: Option<Response> = None;
@ -120,6 +112,18 @@ fn send_request(
) }); ) });
} }
fn set_timeout(socket: libc::c_int, sec: i64, usec: i64) {
let timeval = libc::timeval { tv_sec: sec, tv_usec: usec };
assert_eq!(0, unsafe { libc::setsockopt(
socket,
libc::SOL_SOCKET,
libc::SO_RCVTIMEO,
&timeval as *const libc::timeval as *const libc::c_void,
std::mem::size_of::<libc::timeval>().try_into().unwrap(),
) });
}
fn recv_response(socket: libc::c_int) -> Option<Response> { fn recv_response(socket: libc::c_int) -> Option<Response> {
let response_body_data: [u8; 1024] = [0; 1024]; let response_body_data: [u8; 1024] = [0; 1024];