Network Time Protocol (NTP) Verion 4 is defined in RFC 5905 and can be used to synchronize computers over the Internet. It uses UDP Port 123 by default.
NTP with Go |
Outline
The following is a client request and a server response [PCAP file]. We can see we use UDP Port 123 for the request.
No. Time Source Destination Protocol Length Info 1 0.000000 87.106.189.123 213.209.109.45 NTP 90 NTP Version 4, client Frame 1: 90 bytes on wire (720 bits), 90 bytes captured (720 bits) Ethernet II, Src: FujitsuT_84:99:86 (00:79:99:84:99:86), Dst: Cisco_cb:21:42 (00:24:33:cb:21:42) Internet Protocol Version 4, Src: 87.106.189.123, Dst: 213.209.109.45 User Datagram Protocol, Src Port: 51266, Dst Port: 123 Network Time Protocol (NTP Version 4, client) Flags: 0xe3, Leap Indicator: unknown (clock unsynchronized), Version number: NTP Version 4, Mode: client Peer Clock Stratum: unspecified or invalid (0) Peer Polling Interval: invalid (0) Peer Clock Precision: 1.000000 sec Root Delay: 0 seconds Root Dispersion: 0 seconds Reference ID: NULL Reference Timestamp: Jan 1, 1970 00:00:00.000000000 UTC Origin Timestamp: Jan 1, 1970 00:00:00.000000000 UTC Receive Timestamp: Jan 1, 1970 00:00:00.000000000 UTC Transmit Timestamp: Aug 15, 1970 21:51:32.878910025 UTC No. Time Source Destination Protocol Length Info 2 0.011624 213.209.109.45 87.106.189.123 NTP 90 NTP Version 4, server Frame 2: 90 bytes on wire (720 bits), 90 bytes captured (720 bits) Ethernet II, Src: Cisco_cb:21:42 (00:24:33:cb:21:42), Dst: FujitsuT_84:99:86 (00:79:99:84:99:86) Internet Protocol Version 4, Src: 213.209.109.45, Dst: 87.106.189.123 User Datagram Protocol, Src Port: 123, Dst Port: 51266 Network Time Protocol (NTP Version 4, server) Flags: 0x24, Leap Indicator: no warning, Version number: NTP Version 4, Mode: server Peer Clock Stratum: secondary reference (2) Peer Polling Interval: invalid (3) Peer Clock Precision: 0.000000 sec Root Delay: 0.00030517578125 seconds Root Dispersion: 0.02203369140625 seconds Reference ID: 10.129.40.211 Reference Timestamp: Jun 25, 2019 20:38:51.069389370 UTC Origin Timestamp: Aug 15, 1970 21:51:32.878910025 UTC Receive Timestamp: Jun 25, 2019 20:40:19.685272016 UTC Transmit Timestamp: Jun 25, 2019 20:40:19.685292736 UTC
Coding
package main import ( "fmt" "time" "github.com/beevik/ntp" ) func main() { t, _ := ntp.Time("uk.pool.ntp.org") fmt.Printf("Time: %s\n\n",t) r, err := ntp.Query("uk.pool.ntp.org") if (err!=nil) { fmt.Printf("%v",err) return } fmt.Printf("\n XmitTime: %v\n", r) fmt.Printf(" LocalTime: %v", time.Now()) fmt.Printf("\n XmitTime: %v\n", r.Time) fmt.Printf(" RefTime: %v\n", r.ReferenceTime) fmt.Printf(" RTT: %v\n", r.RTT) fmt.Printf(" Offset: %v\n", r.ClockOffset) fmt.Printf(" Poll: %v\n", r.Poll) fmt.Printf(" Precision: %v\n", r.Precision) fmt.Printf(" Stratum: %v\n", r.Stratum) fmt.Printf(" RefID: 0x%08x\n", r.ReferenceID) fmt.Printf(" RootDelay: %v\n", r.RootDelay) fmt.Printf(" RootDisp: %v\n", r.RootDispersion) fmt.Printf(" RootDist: %v\n", r.RootDistance) fmt.Printf(" MinError: %v\n", r.MinError) fmt.Printf(" Leap: %v\n", r.Leap) }
A sample run is:
Time: 2019-06-25 21:31:32.292998009 +0100 BST m=-0.842076190 LocalTime: 2019-06-25 21:31:33.1930775 +0100 BST m=+0.058003301 XmitTime: 2019-06-25 20:31:32.297852049 +0000 UTC RefTime: 2019-06-25 20:14:52.318413351 +0000 UTC RTT: 7.899667ms Offset: -891.275617ms Poll: 8s Precision: 1.907µs Stratum: 2 RefID: 0xc035676c RootDelay: 18.127441ms RootDisp: 30.242919ms RootDist: 43.256473ms MinError: 887.325783ms Leap: 0