Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
platform_frameworks_base-old
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Farzin Kazemzadeh
platform_frameworks_base-old
Commits
7024d920
Commit
7024d920
authored
4 years ago
by
Dan Zhang
Committed by
Gerrit Code Review
4 years ago
Browse files
Options
Downloads
Plain Diff
Merge "Change the logic of getting ntp server"
parents
0ae446b5
9ea70f6f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
core/java/android/net/SntpClient.java
+19
-9
19 additions, 9 deletions
core/java/android/net/SntpClient.java
with
19 additions
and
9 deletions
core/java/android/net/SntpClient.java
+
19
−
9
View file @
7024d920
...
...
@@ -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"
);
}
}
/**
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment