forked from beevik/ntp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntp.go
92 lines (78 loc) · 2.03 KB
/
ntp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Package ntp provides a simple mechanism for querying the current time
// from a remote NTP server. This package only supports NTP client mode
// behavior and version 4 of the NTP protocol. See RFC 5905.
// Approach inspired by go-nuts post by Michael Hofmann:
// https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/FlcdMU5fkLQ
package ntp
import (
"encoding/binary"
"net"
"time"
)
type mode byte
const (
reserved mode = 0 + iota
symmetricActive
symmetricPassive
client
server
broadcast
controlMessage
reservedPrivate
)
type ntpTime struct {
Seconds uint32
Fraction uint32
}
func (t ntpTime) UTC() time.Time {
nsec := uint64(t.Seconds)*1e9 + (uint64(t.Fraction) * 1e9 >> 32)
return time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(nsec))
}
type msg struct {
LiVnMode byte // Leap Indicator (2) + Version (3) + Mode (3)
Stratum byte
Poll byte
Precision byte
RootDelay uint32
RootDispersion uint32
ReferenceId uint32
ReferenceTime ntpTime
OriginTime ntpTime
ReceiveTime ntpTime
TransmitTime ntpTime
}
// SetVersion sets the NTP protocol version on the message.
func (m *msg) SetVersion(v byte) {
m.LiVnMode = (m.LiVnMode & 0xc7) | v<<3
}
// SetMode sets the NTP protocol mode on the message.
func (m *msg) SetMode(md mode) {
m.LiVnMode = (m.LiVnMode & 0xf8) | byte(md)
}
// Time returns the "receive time" from the remote NTP server
// specifed as host. NTP client mode is used.
func Time(host string) (time.Time, error) {
raddr, err := net.ResolveUDPAddr("udp", host+":123")
if err != nil {
return time.Now(), err
}
con, err := net.DialUDP("udp", nil, raddr)
if err != nil {
return time.Now(), err
}
defer con.Close()
con.SetDeadline(time.Now().Add(5 * time.Second))
m := new(msg)
m.SetMode(client)
m.SetVersion(4)
err = binary.Write(con, binary.BigEndian, m)
if err != nil {
return time.Now(), err
}
err = binary.Read(con, binary.BigEndian, m)
if err != nil {
return time.Now(), err
}
t := m.ReceiveTime.UTC().Local()
return t, nil
}