Loading core/java/android/net/SntpClient.java +6 −3 Original line number Diff line number Diff line Loading @@ -60,7 +60,7 @@ public class SntpClient { private static final int TRANSMIT_TIME_OFFSET = 40; private static final int NTP_PACKET_SIZE = 48; private static final int NTP_PORT = 123; public static final int STANDARD_NTP_PORT = 123; private static final int NTP_MODE_CLIENT = 3; private static final int NTP_MODE_SERVER = 4; private static final int NTP_MODE_BROADCAST = 5; Loading Loading @@ -108,18 +108,21 @@ public class SntpClient { * Sends an SNTP request to the given host and processes the response. * * @param host host name of the server. * @param port port of the server. * @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) { public boolean requestTime(String host, int port, int timeout, Network network) { final Network networkForResolv = network.getPrivateDnsBypassingCopy(); try { final InetAddress[] addresses = networkForResolv.getAllByName(host); for (int i = 0; i < addresses.length; i++) { if (requestTime(addresses[i], NTP_PORT, timeout, networkForResolv)) return true; if (requestTime(addresses[i], port, timeout, networkForResolv)) { return true; } } } catch (UnknownHostException e) { Log.w(TAG, "Unknown host: " + host); Loading core/java/android/util/NtpTrustedTime.java +27 −4 Original line number Diff line number Diff line Loading @@ -138,6 +138,10 @@ public class NtpTrustedTime implements TrustedTime { @Nullable private String mHostnameForTests; /** An in-memory config override for use during tests. */ @Nullable private Integer mPortForTests; /** An in-memory config override for use during tests. */ @Nullable private Duration mTimeoutForTests; Loading @@ -163,9 +167,11 @@ public class NtpTrustedTime implements TrustedTime { * Overrides the NTP server config for tests. Passing {@code null} to a parameter clears the * test value, i.e. so the normal value will be used next time. */ public void setServerConfigForTests(@Nullable String hostname, @Nullable Duration timeout) { public void setServerConfigForTests( @Nullable String hostname, @Nullable Integer port, @Nullable Duration timeout) { synchronized (this) { mHostnameForTests = hostname; mPortForTests = port; mTimeoutForTests = timeout; } } Loading Loading @@ -195,8 +201,9 @@ public class NtpTrustedTime implements TrustedTime { if (LOGD) Log.d(TAG, "forceRefresh() from cache miss"); final SntpClient client = new SntpClient(); final String serverName = connectionInfo.getServer(); final int port = connectionInfo.getPort(); final int timeoutMillis = connectionInfo.getTimeoutMillis(); if (client.requestTime(serverName, timeoutMillis, network)) { if (client.requestTime(serverName, port, timeoutMillis, network)) { long ntpCertainty = client.getRoundTripTime() / 2; mTimeResult = new TimeResult( client.getNtpTime(), client.getNtpTimeReference(), ntpCertainty); Loading Loading @@ -297,10 +304,12 @@ public class NtpTrustedTime implements TrustedTime { private static class NtpConnectionInfo { @NonNull private final String mServer; private final int mPort; private final int mTimeoutMillis; NtpConnectionInfo(@NonNull String server, int timeoutMillis) { NtpConnectionInfo(@NonNull String server, int port, int timeoutMillis) { mServer = Objects.requireNonNull(server); mPort = port; mTimeoutMillis = timeoutMillis; } Loading @@ -309,6 +318,11 @@ public class NtpTrustedTime implements TrustedTime { return mServer; } @NonNull public int getPort() { return mPort; } int getTimeoutMillis() { return mTimeoutMillis; } Loading @@ -317,6 +331,7 @@ public class NtpTrustedTime implements TrustedTime { public String toString() { return "NtpConnectionInfo{" + "mServer='" + mServer + '\'' + ", mPort='" + mPort + '\'' + ", mTimeoutMillis=" + mTimeoutMillis + '}'; } Loading @@ -341,6 +356,13 @@ public class NtpTrustedTime implements TrustedTime { } } final Integer port; if (mPortForTests != null) { port = mPortForTests; } else { port = SntpClient.STANDARD_NTP_PORT; } final int timeoutMillis; if (mTimeoutForTests != null) { timeoutMillis = (int) mTimeoutForTests.toMillis(); Loading @@ -350,7 +372,8 @@ public class NtpTrustedTime implements TrustedTime { timeoutMillis = Settings.Global.getInt( resolver, Settings.Global.NTP_TIMEOUT, defaultTimeoutMillis); } return TextUtils.isEmpty(hostname) ? null : new NtpConnectionInfo(hostname, timeoutMillis); return TextUtils.isEmpty(hostname) ? null : new NtpConnectionInfo(hostname, port, timeoutMillis); } /** Prints debug information. */ Loading core/tests/coretests/src/android/net/SntpClientTest.java +2 −1 Original line number Diff line number Diff line Loading @@ -293,7 +293,8 @@ public class SntpClientTest { @Test public void testDnsResolutionFailure() throws Exception { assertFalse(mClient.requestTime("ntp.server.doesnotexist.example", 5000, mNetwork)); assertFalse(mClient.requestTime("ntp.server.doesnotexist.example", SntpClient.STANDARD_NTP_PORT, 5000, mNetwork)); } @Test Loading services/core/java/com/android/server/NetworkTimeUpdateService.java +4 −2 Original line number Diff line number Diff line Loading @@ -196,13 +196,15 @@ public class NetworkTimeUpdateService extends Binder { * Overrides the NTP server config for tests. Passing {@code null} to a parameter clears the * test value, i.e. so the normal value will be used next time. */ void setServerConfigForTests(@Nullable String hostname, @Nullable Duration timeout) { void setServerConfigForTests( @Nullable String hostname, @Nullable Integer port, @Nullable Duration timeout) { mContext.enforceCallingPermission( android.Manifest.permission.SET_TIME, "set NTP server config for tests"); mLocalLog.log("Setting server config for tests: hostname=" + hostname + ", port=" + port + ", timeout=" + timeout); mTime.setServerConfigForTests(hostname, timeout); mTime.setServerConfigForTests(hostname, port, timeout); } private void onPollNetworkTime(int event) { Loading services/core/java/com/android/server/NetworkTimeUpdateServiceShellCommand.java +10 −3 Original line number Diff line number Diff line Loading @@ -46,6 +46,7 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand { */ private static final String SHELL_COMMAND_SET_SERVER_CONFIG = "set_server_config"; private static final String SET_SERVER_CONFIG_HOSTNAME_ARG = "--hostname"; private static final String SET_SERVER_CONFIG_PORT_ARG = "--port"; private static final String SET_SERVER_CONFIG_TIMEOUT_ARG = "--timeout_millis"; @NonNull Loading Loading @@ -87,6 +88,7 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand { private int runSetServerConfig() { String hostname = null; Integer port = null; Duration timeout = null; String opt; while ((opt = getNextArg()) != null) { Loading @@ -95,6 +97,10 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand { hostname = getNextArgRequired(); break; } case SET_SERVER_CONFIG_PORT_ARG: { port = Integer.parseInt(getNextArgRequired()); break; } case SET_SERVER_CONFIG_TIMEOUT_ARG: { timeout = Duration.ofMillis(Integer.parseInt(getNextArgRequired())); break; Loading @@ -104,7 +110,7 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand { } } } mNetworkTimeUpdateService.setServerConfigForTests(hostname, timeout); mNetworkTimeUpdateService.setServerConfigForTests(hostname, port, timeout); return 0; } Loading @@ -120,8 +126,9 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand { pw.printf(" Refreshes the latest time. Prints whether it was successful.\n"); pw.printf(" %s\n", SHELL_COMMAND_SET_SERVER_CONFIG); pw.printf(" Sets the NTP server config for tests. The config is not persisted.\n"); pw.printf(" Options: [%s <hostname>] [%s <millis>]\n", SET_SERVER_CONFIG_HOSTNAME_ARG, SET_SERVER_CONFIG_TIMEOUT_ARG); pw.printf(" Options: [%s <hostname>] [%s <port>] [%s <millis>]\n", SET_SERVER_CONFIG_HOSTNAME_ARG, SET_SERVER_CONFIG_PORT_ARG, SET_SERVER_CONFIG_TIMEOUT_ARG); pw.printf(" Each key/value is optional and must be specified to override the\n"); pw.printf(" normal value, not specifying a key causes it to reset to the original.\n"); pw.println(); Loading Loading
core/java/android/net/SntpClient.java +6 −3 Original line number Diff line number Diff line Loading @@ -60,7 +60,7 @@ public class SntpClient { private static final int TRANSMIT_TIME_OFFSET = 40; private static final int NTP_PACKET_SIZE = 48; private static final int NTP_PORT = 123; public static final int STANDARD_NTP_PORT = 123; private static final int NTP_MODE_CLIENT = 3; private static final int NTP_MODE_SERVER = 4; private static final int NTP_MODE_BROADCAST = 5; Loading Loading @@ -108,18 +108,21 @@ public class SntpClient { * Sends an SNTP request to the given host and processes the response. * * @param host host name of the server. * @param port port of the server. * @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) { public boolean requestTime(String host, int port, int timeout, Network network) { final Network networkForResolv = network.getPrivateDnsBypassingCopy(); try { final InetAddress[] addresses = networkForResolv.getAllByName(host); for (int i = 0; i < addresses.length; i++) { if (requestTime(addresses[i], NTP_PORT, timeout, networkForResolv)) return true; if (requestTime(addresses[i], port, timeout, networkForResolv)) { return true; } } } catch (UnknownHostException e) { Log.w(TAG, "Unknown host: " + host); Loading
core/java/android/util/NtpTrustedTime.java +27 −4 Original line number Diff line number Diff line Loading @@ -138,6 +138,10 @@ public class NtpTrustedTime implements TrustedTime { @Nullable private String mHostnameForTests; /** An in-memory config override for use during tests. */ @Nullable private Integer mPortForTests; /** An in-memory config override for use during tests. */ @Nullable private Duration mTimeoutForTests; Loading @@ -163,9 +167,11 @@ public class NtpTrustedTime implements TrustedTime { * Overrides the NTP server config for tests. Passing {@code null} to a parameter clears the * test value, i.e. so the normal value will be used next time. */ public void setServerConfigForTests(@Nullable String hostname, @Nullable Duration timeout) { public void setServerConfigForTests( @Nullable String hostname, @Nullable Integer port, @Nullable Duration timeout) { synchronized (this) { mHostnameForTests = hostname; mPortForTests = port; mTimeoutForTests = timeout; } } Loading Loading @@ -195,8 +201,9 @@ public class NtpTrustedTime implements TrustedTime { if (LOGD) Log.d(TAG, "forceRefresh() from cache miss"); final SntpClient client = new SntpClient(); final String serverName = connectionInfo.getServer(); final int port = connectionInfo.getPort(); final int timeoutMillis = connectionInfo.getTimeoutMillis(); if (client.requestTime(serverName, timeoutMillis, network)) { if (client.requestTime(serverName, port, timeoutMillis, network)) { long ntpCertainty = client.getRoundTripTime() / 2; mTimeResult = new TimeResult( client.getNtpTime(), client.getNtpTimeReference(), ntpCertainty); Loading Loading @@ -297,10 +304,12 @@ public class NtpTrustedTime implements TrustedTime { private static class NtpConnectionInfo { @NonNull private final String mServer; private final int mPort; private final int mTimeoutMillis; NtpConnectionInfo(@NonNull String server, int timeoutMillis) { NtpConnectionInfo(@NonNull String server, int port, int timeoutMillis) { mServer = Objects.requireNonNull(server); mPort = port; mTimeoutMillis = timeoutMillis; } Loading @@ -309,6 +318,11 @@ public class NtpTrustedTime implements TrustedTime { return mServer; } @NonNull public int getPort() { return mPort; } int getTimeoutMillis() { return mTimeoutMillis; } Loading @@ -317,6 +331,7 @@ public class NtpTrustedTime implements TrustedTime { public String toString() { return "NtpConnectionInfo{" + "mServer='" + mServer + '\'' + ", mPort='" + mPort + '\'' + ", mTimeoutMillis=" + mTimeoutMillis + '}'; } Loading @@ -341,6 +356,13 @@ public class NtpTrustedTime implements TrustedTime { } } final Integer port; if (mPortForTests != null) { port = mPortForTests; } else { port = SntpClient.STANDARD_NTP_PORT; } final int timeoutMillis; if (mTimeoutForTests != null) { timeoutMillis = (int) mTimeoutForTests.toMillis(); Loading @@ -350,7 +372,8 @@ public class NtpTrustedTime implements TrustedTime { timeoutMillis = Settings.Global.getInt( resolver, Settings.Global.NTP_TIMEOUT, defaultTimeoutMillis); } return TextUtils.isEmpty(hostname) ? null : new NtpConnectionInfo(hostname, timeoutMillis); return TextUtils.isEmpty(hostname) ? null : new NtpConnectionInfo(hostname, port, timeoutMillis); } /** Prints debug information. */ Loading
core/tests/coretests/src/android/net/SntpClientTest.java +2 −1 Original line number Diff line number Diff line Loading @@ -293,7 +293,8 @@ public class SntpClientTest { @Test public void testDnsResolutionFailure() throws Exception { assertFalse(mClient.requestTime("ntp.server.doesnotexist.example", 5000, mNetwork)); assertFalse(mClient.requestTime("ntp.server.doesnotexist.example", SntpClient.STANDARD_NTP_PORT, 5000, mNetwork)); } @Test Loading
services/core/java/com/android/server/NetworkTimeUpdateService.java +4 −2 Original line number Diff line number Diff line Loading @@ -196,13 +196,15 @@ public class NetworkTimeUpdateService extends Binder { * Overrides the NTP server config for tests. Passing {@code null} to a parameter clears the * test value, i.e. so the normal value will be used next time. */ void setServerConfigForTests(@Nullable String hostname, @Nullable Duration timeout) { void setServerConfigForTests( @Nullable String hostname, @Nullable Integer port, @Nullable Duration timeout) { mContext.enforceCallingPermission( android.Manifest.permission.SET_TIME, "set NTP server config for tests"); mLocalLog.log("Setting server config for tests: hostname=" + hostname + ", port=" + port + ", timeout=" + timeout); mTime.setServerConfigForTests(hostname, timeout); mTime.setServerConfigForTests(hostname, port, timeout); } private void onPollNetworkTime(int event) { Loading
services/core/java/com/android/server/NetworkTimeUpdateServiceShellCommand.java +10 −3 Original line number Diff line number Diff line Loading @@ -46,6 +46,7 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand { */ private static final String SHELL_COMMAND_SET_SERVER_CONFIG = "set_server_config"; private static final String SET_SERVER_CONFIG_HOSTNAME_ARG = "--hostname"; private static final String SET_SERVER_CONFIG_PORT_ARG = "--port"; private static final String SET_SERVER_CONFIG_TIMEOUT_ARG = "--timeout_millis"; @NonNull Loading Loading @@ -87,6 +88,7 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand { private int runSetServerConfig() { String hostname = null; Integer port = null; Duration timeout = null; String opt; while ((opt = getNextArg()) != null) { Loading @@ -95,6 +97,10 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand { hostname = getNextArgRequired(); break; } case SET_SERVER_CONFIG_PORT_ARG: { port = Integer.parseInt(getNextArgRequired()); break; } case SET_SERVER_CONFIG_TIMEOUT_ARG: { timeout = Duration.ofMillis(Integer.parseInt(getNextArgRequired())); break; Loading @@ -104,7 +110,7 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand { } } } mNetworkTimeUpdateService.setServerConfigForTests(hostname, timeout); mNetworkTimeUpdateService.setServerConfigForTests(hostname, port, timeout); return 0; } Loading @@ -120,8 +126,9 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand { pw.printf(" Refreshes the latest time. Prints whether it was successful.\n"); pw.printf(" %s\n", SHELL_COMMAND_SET_SERVER_CONFIG); pw.printf(" Sets the NTP server config for tests. The config is not persisted.\n"); pw.printf(" Options: [%s <hostname>] [%s <millis>]\n", SET_SERVER_CONFIG_HOSTNAME_ARG, SET_SERVER_CONFIG_TIMEOUT_ARG); pw.printf(" Options: [%s <hostname>] [%s <port>] [%s <millis>]\n", SET_SERVER_CONFIG_HOSTNAME_ARG, SET_SERVER_CONFIG_PORT_ARG, SET_SERVER_CONFIG_TIMEOUT_ARG); pw.printf(" Each key/value is optional and must be specified to override the\n"); pw.printf(" normal value, not specifying a key causes it to reset to the original.\n"); pw.println(); Loading