pub fn request_with_bind_addr(addr: SocketAddr, bind_addr: IpAddr) -> io::Result<protocol::Packet> {
    // Create a packet for requesting from an NTP server as a client.
    let mut packet = {
        let leap_indicator = protocol::LeapIndicator::default();
        let version = protocol::Version::V4;
        let mode = protocol::Mode::Client;
        let poll = 0;
        let precision = 0;
        let root_delay = protocol::ShortFormat::default();
        let root_dispersion = protocol::ShortFormat::default();
        let transmit_timestamp = unix_time::Instant::now().into();
        let stratum = protocol::Stratum::UNSPECIFIED;
        let src = protocol::PrimarySource::Null;
        let reference_id = protocol::ReferenceIdentifier::PrimarySource(src);
        let reference_timestamp = protocol::TimestampFormat::default();
        let receive_timestamp = protocol::TimestampFormat::default();
        let origin_timestamp = protocol::TimestampFormat::default();
        protocol::Packet {
            leap_indicator,
            version,
            mode,
            stratum,
            poll,
            precision,
            root_delay,
            root_dispersion,
            reference_id,
            reference_timestamp,
            origin_timestamp,
            receive_timestamp,
            transmit_timestamp,
        }
    };

    // Write the packet to a slice of bytes.
    let mut bytes = [0u8; protocol::Packet::PACKED_SIZE_BYTES];
    (&mut bytes[..]).write_bytes(&packet)?;

    // Create the socket from which we will send the packet.
    let sock = UdpSocket::bind(SocketAddr::new(bind_addr, 0))?;
    sock.set_read_timeout(Some(Duration::from_secs(5)))?;
    sock.set_write_timeout(Some(Duration::from_secs(5)))?;

    // Send the data.
    let sz = sock.send_to(&bytes, addr)?;
    debug!("{:?}", sock.local_addr());
    debug!("sent: {}", sz);

    // Receive the response.
    let res = sock.recv(&mut bytes[..])?;
    debug!("recv: {:?}", res);
    debug!("{:?}", &bytes[..]);

    // Read the received packet from the response.
    packet = (&bytes[..]).read_bytes()?;
    Ok(packet)
}
/// Send a blocking request to an ntp server with a hardcoded 5 second timeout.
///
///   `addr` can be any valid socket address
///   `bind_addr` specifies the client IP to send packets from
///
/// Returns an error if the server cannot be reached or the response is invalid.
/// This will also fail when `addr` and `bind_addr` are not of the same IP version.
// **TODO**: remove hardcoded timeout
Send a blocking request to an ntp server with a hardcoded 5 second timeout.
`addr` can be any valid socket address
`bind_addr` specifies the client IP to send packets from

Returns an error if the server cannot be reached or the response is invalid.
This will also fail when `addr` and `bind_addr` are not of the same IP version.
TODO**: remove hardcoded timeout
Send a blocking request to an ntp server with a hardcoded 5 second timeout . ` addr ` can be any valid socket address ` bind_addr ` specifies the client IP to send packets from Returns an error if the server cannot be reached or the response is invalid . This will also fail when ` addr ` and ` bind_addr ` are not of the same IP version . TODO ** : remove hardcoded timeout
pub fn request_with_bind_addr(addr: SocketAddr, bind_addr: IpAddr) -> io::Result<protocol::Packet> {
    // Create a packet for requesting from an NTP server as a client.
    let mut packet = {
        let leap_indicator = protocol::LeapIndicator::default();
        let version = protocol::Version::V4;
        let mode = protocol::Mode::Client;
        let poll = 0;
        let precision = 0;
        let root_delay = protocol::ShortFormat::default();
        let root_dispersion = protocol::ShortFormat::default();
        let transmit_timestamp = unix_time::Instant::now().into();
        let stratum = protocol::Stratum::UNSPECIFIED;
        let src = protocol::PrimarySource::Null;
        let reference_id = protocol::ReferenceIdentifier::PrimarySource(src);
        let reference_timestamp = protocol::TimestampFormat::default();
        let receive_timestamp = protocol::TimestampFormat::default();
        let origin_timestamp = protocol::TimestampFormat::default();
        protocol::Packet {
            leap_indicator,
            version,
            mode,
            stratum,
            poll,
            precision,
            root_delay,
            root_dispersion,
            reference_id,
            reference_timestamp,
            origin_timestamp,
            receive_timestamp,
            transmit_timestamp,
        }
    };
    // Write the packet to a slice of bytes.
    let mut bytes = [0u8; protocol::Packet::PACKED_SIZE_BYTES];
    (&mut bytes[..]).write_bytes(&packet)?;
    // Create the socket from which we will send the packet.
    let sock = UdpSocket::bind(SocketAddr::new(bind_addr, 0))?;
    sock.set_read_timeout(Some(Duration::from_secs(5)))?;
    sock.set_write_timeout(Some(Duration::from_secs(5)))?;
    // Send the data.
    let sz = sock.send_to(&bytes, addr)?;
    debug!("{:?}", sock.local_addr());
    debug!("sent: {}", sz);
    // Receive the response.
    let res = sock.recv(&mut bytes[..])?;
    debug!("recv: {:?}", res);
    debug!("{:?}", &bytes[..]);
    // Read the received packet from the response.
    packet = (&bytes[..]).read_bytes()?;
    Ok(packet)
}
pub fn request_with_bind_addr ( addr : SocketAddr , bind_addr : IpAddr ) -> io :: Result < protocol :: Packet > { // Create a packet for requesting from an NTP server as a client. let mut packet = { let leap_indicator = protocol :: LeapIndicator :: default ( ) ; let version = protocol :: Version :: V4 ; let mode = protocol :: Mode :: Client ; let poll = 0 ; let precision = 0 ; let root_delay = protocol :: ShortFormat :: default ( ) ; let root_dispersion = protocol :: ShortFormat :: default ( ) ; let transmit_timestamp = unix_time :: Instant :: now ( ) . into ( ) ; let stratum = protocol :: Stratum :: UNSPECIFIED ; let src = protocol :: PrimarySource :: Null ; let reference_id = protocol :: ReferenceIdentifier :: PrimarySource ( src ) ; let reference_timestamp = protocol :: TimestampFormat :: default ( ) ; let receive_timestamp = protocol :: TimestampFormat :: default ( ) ; let origin_timestamp = protocol :: TimestampFormat :: default ( ) ; protocol :: Packet { leap_indicator , version , mode , stratum , poll , precision , root_delay , root_dispersion , reference_id , reference_timestamp , origin_timestamp , receive_timestamp , transmit_timestamp , } } ; // Write the packet to a slice of bytes. let mut bytes = [ 0u8 ; protocol :: Packet :: PACKED_SIZE_BYTES ] ; ( & mut bytes [ .. ] ) . write_bytes ( & packet ) ? ; // Create the socket from which we will send the packet. let sock = UdpSocket :: bind ( SocketAddr :: new ( bind_addr , 0 ) ) ? ; sock . set_read_timeout ( Some ( Duration :: from_secs ( 5 ) ) ) ? ; sock . set_write_timeout ( Some ( Duration :: from_secs ( 5 ) ) ) ? ; // Send the data. let sz = sock . send_to ( & bytes , addr ) ? ; debug ! ( " " sock local_addr ( ) ) ; debug ! ( " " sz ) ; // Receive the response. let res = sock . recv ( & mut bytes [ .. ] ) ? ; debug ! ( " " res ) ; debug ! ( " " bytes [ ] ) ; // Read the received packet from the response. packet = ( & bytes [ .. ] ) . read_bytes ( ) ? ; Ok ( packet ) }
Send a blocking request to an ntp server with a hardcoded 5 second timeout.
Send a blocking request to an ntp server with a hardcoded 5 second timeout .
addr
SocketAddr
bind_addr
IpAddr
addr
SocketAddr
bind_addr
IpAddr