diff --git a/src/main.rs b/src/main.rs index a1171bc..921b344 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,15 +39,7 @@ fn main() { send_request(socket, current_ttl, &host, &request); - let timeval = libc::timeval { tv_sec: 2, tv_usec: 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::().try_into().unwrap(), - ) }); + set_timeout(socket, 2, 0); let mut response: Option = 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::().try_into().unwrap(), + ) }); +} + fn recv_response(socket: libc::c_int) -> Option { let response_body_data: [u8; 1024] = [0; 1024];