diff options
| author | 2020-07-29 04:56:48 +0000 | |
|---|---|---|
| committer | 2020-07-29 04:56:48 +0000 | |
| commit | 19f12168d16fca020a83b69a3943bb722609c69b (patch) | |
| tree | 370dac8f5de032e0e56230b1eac2ac3e9252aab0 | |
| parent | 136c4f98b438ace567a50773109fba98156915ee (diff) | |
| parent | bb601496d32a32167cf9a8c273f51ad2ec77ad9b (diff) | |
Merge "Change the logic of getting ntp server" am: 7024d9205b am: bb601496d3
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1108155
Change-Id: I77a3f9c3a1143934345c38bf982ae4507e06c238
| -rw-r--r-- | core/java/android/net/SntpClient.java | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/core/java/android/net/SntpClient.java b/core/java/android/net/SntpClient.java index 6c94b7a00204..f6852e681439 100644 --- a/core/java/android/net/SntpClient.java +++ b/core/java/android/net/SntpClient.java @@ -25,6 +25,7 @@ import com.android.internal.util.TrafficStatsConstants; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.Arrays; /** @@ -86,21 +87,26 @@ public class SntpClient { * Sends an SNTP request to the given host and processes the response. * * @param host host name of the server. - * @param timeout network timeout in milliseconds. + * @param timeout network timeout in milliseconds. the timeout doesn't include the DNS lookup + * time, and it applies to each individual query to the resolved addresses of + * the NTP server. * @param network network over which to send the request. * @return true if the transaction was successful. */ public boolean requestTime(String host, int timeout, Network network) { final Network networkForResolv = network.getPrivateDnsBypassingCopy(); - InetAddress address = null; try { - address = networkForResolv.getByName(host); - } catch (Exception e) { + final InetAddress[] addresses = networkForResolv.getAllByName(host); + for (int i = 0; i < addresses.length; i++) { + if (requestTime(addresses[i], NTP_PORT, timeout, networkForResolv)) return true; + } + } catch (UnknownHostException e) { + Log.w(TAG, "Unknown host: " + host); EventLogTags.writeNtpFailure(host, e.toString()); - if (DBG) Log.d(TAG, "request time failed: " + e); - return false; } - return requestTime(address, NTP_PORT, timeout, networkForResolv); + + if (DBG) Log.d(TAG, "request time failed"); + return false; } public boolean requestTime(InetAddress address, int port, int timeout, Network network) { @@ -139,10 +145,11 @@ public class SntpClient { final long originateTime = readTimeStamp(buffer, ORIGINATE_TIME_OFFSET); final long receiveTime = readTimeStamp(buffer, RECEIVE_TIME_OFFSET); final long transmitTime = readTimeStamp(buffer, TRANSMIT_TIME_OFFSET); + final long referenceTime = readTimeStamp(buffer, REFERENCE_TIME_OFFSET); /* Do validation according to RFC */ // TODO: validate originateTime == requestTime. - checkValidServerReply(leap, mode, stratum, transmitTime); + checkValidServerReply(leap, mode, stratum, transmitTime, referenceTime); long roundTripTime = responseTicks - requestTicks - (transmitTime - receiveTime); // receiveTime = originateTime + transit + skew @@ -218,7 +225,7 @@ public class SntpClient { } private static void checkValidServerReply( - byte leap, byte mode, int stratum, long transmitTime) + byte leap, byte mode, int stratum, long transmitTime, long referenceTime) throws InvalidServerReplyException { if (leap == NTP_LEAP_NOSYNC) { throw new InvalidServerReplyException("unsynchronized server"); @@ -232,6 +239,9 @@ public class SntpClient { if (transmitTime == 0) { throw new InvalidServerReplyException("zero transmitTime"); } + if (referenceTime == 0) { + throw new InvalidServerReplyException("zero reference timestamp"); + } } /** |