long[] requestTime(String ntpHost,int timeoutInMillis) throws IOException {
  DatagramSocket socket=null;
  try {
    byte[] buffer=new byte[NTP_PACKET_SIZE];
    InetAddress address=InetAddress.getByName(ntpHost);
    DatagramPacket request=new DatagramPacket(buffer,buffer.length,address,NTP_PORT);
    _writeVersion(buffer);
    long requestTime=System.currentTimeMillis();
    long requestTicks=SystemClock.elapsedRealtime();
    _writeTimeStamp(buffer,INDEX_TRANSMIT_TIME,requestTime);
    socket=new DatagramSocket();
    socket.setSoTimeout(timeoutInMillis);
    socket.send(request);
    long t[]=new long[RESPONSE_INDEX_SIZE];
    DatagramPacket response=new DatagramPacket(buffer,buffer.length);
    socket.receive(response);
    long responseTicks=SystemClock.elapsedRealtime();
    t[RESPONSE_INDEX_RESPONSE_TICKS]=responseTicks;
    long originateTime=_readTimeStamp(buffer,INDEX_ORIGINATE_TIME);
    long receiveTime=_readTimeStamp(buffer,INDEX_RECEIVE_TIME);
    long transmitTime=_readTimeStamp(buffer,INDEX_TRANSMIT_TIME);
    long responseTime=requestTime + (responseTicks - requestTicks);
    t[RESPONSE_INDEX_ORIGINATE_TIME]=originateTime;
    t[RESPONSE_INDEX_RECEIVE_TIME]=receiveTime;
    t[RESPONSE_INDEX_TRANSMIT_TIME]=transmitTime;
    t[RESPONSE_INDEX_RESPONSE_TIME]=responseTime;
    long rootDelay=_read(buffer,INDEX_ROOT_DELAY);
    t[RESPONSE_INDEX_ROOT_DELAY]=rootDelay;
    if (rootDelay > 100) {
      throw new InvalidNtpServerResponseException("Invalid response from NTP server. Root delay violation " + rootDelay);
    }
    long rootDispersion=_read(buffer,INDEX_ROOT_DISPERSION);
    t[RESPONSE_INDEX_DISPERSION]=rootDispersion;
    if (rootDispersion > 100) {
      throw new InvalidNtpServerResponseException("Invalid response from NTP server. Root dispersion violation " + rootDispersion);
    }
    final byte mode=(byte)(buffer[0] & 0x7);
    if (mode != 4 && mode != 5) {
      throw new InvalidNtpServerResponseException("untrusted mode value for TrueTime: " + mode);
    }
    final int stratum=buffer[1] & 0xff;
    t[RESPONSE_INDEX_STRATUM]=stratum;
    if (stratum < 1 || stratum > 15) {
      throw new InvalidNtpServerResponseException("untrusted stratum value for TrueTime: " + stratum);
    }
    final byte leap=(byte)((buffer[0] >> 6) & 0x3);
    if (leap == 3) {
      throw new InvalidNtpServerResponseException("unsynchronized server responded for TrueTime");
    }
    long delay=Math.abs((responseTime - originateTime) - (transmitTime - receiveTime));
    if (delay >= 100) {
      throw new InvalidNtpServerResponseException("Server response delay too large for comfort " + delay);
    }
    long timeElapsedSinceRequest=Math.abs(originateTime - System.currentTimeMillis());
    if (timeElapsedSinceRequest >= 10_000) {
      throw new InvalidNtpServerResponseException("Request was sent more than 10 seconds back " + timeElapsedSinceRequest);
    }
    _sntpInitialized=true;
    TrueLog.i(TAG,"---- SNTP successful response from " + ntpHost);
    cacheTrueTimeInfo(t);
    return t;
  }
 catch (  Exception e) {
    TrueLog.d(TAG,"---- SNTP request failed for " + ntpHost);
    throw e;
  }
 finally {
    if (socket != null) {
      socket.close();
    }
  }
}

long [ ] requestTime ( String ntpHost , int timeoutInMillis ) throws IOException { DatagramSocket socket = null ; try { byte [ ] buffer = new byte [ NTP_PACKET_SIZE ] ; InetAddress address = InetAddress . getByName ( ntpHost ) ; DatagramPacket request = new DatagramPacket ( buffer , buffer . length , address , NTP_PORT ) ; _writeVersion ( buffer ) ; long requestTime = System . currentTimeMillis ( ) ; long requestTicks = SystemClock . elapsedRealtime ( ) ; _writeTimeStamp ( buffer , INDEX_TRANSMIT_TIME , requestTime ) ; socket = new DatagramSocket ( ) ; socket . setSoTimeout ( timeoutInMillis ) ; socket . send ( request ) ; long t [ ] = new long [ RESPONSE_INDEX_SIZE ] ; DatagramPacket response = new DatagramPacket ( buffer , buffer . length ) ; socket . receive ( response ) ; long responseTicks = SystemClock . elapsedRealtime ( ) ; t [ RESPONSE_INDEX_RESPONSE_TICKS ] = responseTicks ; long originateTime = _readTimeStamp ( buffer , INDEX_ORIGINATE_TIME ) ; long receiveTime = _readTimeStamp ( buffer , INDEX_RECEIVE_TIME ) ; long transmitTime = _readTimeStamp ( buffer , INDEX_TRANSMIT_TIME ) ; long responseTime = requestTime + ( responseTicks - requestTicks ) ; t [ RESPONSE_INDEX_ORIGINATE_TIME ] = originateTime ; t [ RESPONSE_INDEX_RECEIVE_TIME ] = receiveTime ; t [ RESPONSE_INDEX_TRANSMIT_TIME ] = transmitTime ; t [ RESPONSE_INDEX_RESPONSE_TIME ] = responseTime ; long rootDelay = _read ( buffer , INDEX_ROOT_DELAY ) ; t [ RESPONSE_INDEX_ROOT_DELAY ] = rootDelay ; if ( rootDelay > 100 ) { throw new InvalidNtpServerResponseException ( "Invalid response from NTP server. Root delay violation " + rootDelay ) ; } long rootDispersion = _read ( buffer , INDEX_ROOT_DISPERSION ) ; t [ RESPONSE_INDEX_DISPERSION ] = rootDispersion ; if ( rootDispersion > 100 ) { throw new InvalidNtpServerResponseException ( "Invalid response from NTP server. Root dispersion violation " + rootDispersion ) ; } final byte mode = ( byte ) ( buffer [ 0 ] & 0x7 ) ; if ( mode != 4 && mode != 5 ) { throw new InvalidNtpServerResponseException ( "untrusted mode value for TrueTime: " + mode ) ; } final int stratum = buffer [ 1 ] & 0xff ; t [ RESPONSE_INDEX_STRATUM ] = stratum ; if ( stratum < 1 || stratum > 15 ) { throw new InvalidNtpServerResponseException ( "untrusted stratum value for TrueTime: " + stratum ) ; } final byte leap = ( byte ) ( ( buffer [ 0 ] > > 6 ) & 0x3 ) ; if ( leap == 3 ) { throw new InvalidNtpServerResponseException ( "unsynchronized server responded for TrueTime" ) ; } long delay = Math . abs ( ( responseTime - originateTime ) - ( transmitTime - receiveTime ) ) ; if ( delay >= 100 ) { throw new InvalidNtpServerResponseException ( "Server response delay too large for comfort " + delay ) ; } long timeElapsedSinceRequest = Math . abs ( originateTime - System . currentTimeMillis ( ) ) ; if ( timeElapsedSinceRequest >= 10_000 ) { throw new InvalidNtpServerResponseException ( "Request was sent more than 10 seconds back " + timeElapsedSinceRequest ) ; } _sntpInitialized = true ; TrueLog . i ( TAG , "---- SNTP successful response from " + ntpHost ) ; cacheTrueTimeInfo ( t ) ; return t ; } catch ( Exception e ) { TrueLog . d ( TAG , "---- SNTP request failed for " + ntpHost ) ; throw e ; } finally { if ( socket != null ) { socket . close ( ) ; } } }
sends an ntp request to the given host and processes the response .
