forked from vladimirvivien/go-ntp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntpc.go
150 lines (131 loc) · 5.21 KB
/
ntpc.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/binary"
"flag"
"net"
"os/exec"
"os/user"
"time"
)
const ntpEpochOffset = 2208988800
// NTP packet format (v3 with optional v4 fields removed)
//
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |LI | VN |Mode | Stratum | Poll | Precision |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Root Delay |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Root Dispersion |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Reference ID |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + Reference Timestamp (64) +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + Origin Timestamp (64) +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + Receive Timestamp (64) +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + Transmit Timestamp (64) +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
type packet struct {
Settings uint8 // leap yr indicator, ver number, and mode
Stratum uint8 // stratum of local clock
Poll int8 // poll exponent
Precision int8 // precision exponent
RootDelay uint32 // root delay
RootDispersion uint32 // root dispersion
ReferenceID uint32 // reference id
RefTimeSec uint32 // reference timestamp sec
RefTimeFrac uint32 // reference timestamp fractional
OrigTimeSec uint32 // origin time secs
OrigTimeFrac uint32 // origin time fractional
RxTimeSec uint32 // receive time secs
RxTimeFrac uint32 // receive time frac
TxTimeSec uint32 // transmit time secs
TxTimeFrac uint32 // transmit time frac
}
var debug bool
// This program implements a trivial NTP client over UDP.
//
// Usage:
// time -e <host endpoint as addr>
func main() {
var err error
var host string
var save bool
flag.StringVar(&host, "e", "us.pool.ntp.org", "NTP host")
flag.BoolVar(&save, "s", false, "Update system date & time")
flag.BoolVar(&debug, "d", false, "Show detailed results")
flag.Parse()
host = host + ":123"
logger()
if save && !isRoot() {
Warn.Println("System clock update can only be done by root")
save = false
}
// Setup a UDP connection
conn, err := net.Dial("udp", host)
if err != nil {
Error.Fatalf("Failed to connect: %v", err)
}
defer conn.Close()
Debug.Printf("Connected to %v", host)
if err = conn.SetDeadline(time.Now().Add(15 * time.Second)); err != nil {
Error.Fatalf("Failed to set deadline: %v", err)
}
Debug.Print("Got responce")
// configure request settings by specifying the first byte as
// 00 011 011 (or 0x1B)
// | | +-- client mode (3)
// | + ----- version (3)
// + -------- leap year indicator, 0 no warning
req := &packet{Settings: 0x1B}
// send time request
if err = binary.Write(conn, binary.BigEndian, req); err != nil {
Error.Fatalf("failed to send request: %v", err)
}
// block to receive server response
rsp := &packet{}
dateLoc := time.Now()
if err := binary.Read(conn, binary.BigEndian, rsp); err != nil {
Error.Fatalf("Failed to read server response: %v", err)
}
// On POSIX-compliant OS, time is expressed
// using the Unix time epoch (or secs since year 1970).
// NTP seconds are counted since 1900 and therefore must
// be corrected with an epoch offset to convert NTP seconds
// to Unix time by removing 70 yrs of seconds (1970-1900)
// or 2208988800 seconds.
secs := float64(rsp.TxTimeSec) - ntpEpochOffset
nanos := (int64(rsp.TxTimeFrac) * 1e9) >> 32 // convert fractional to nanos
dateNTP := time.Unix(int64(secs), nanos)
dateNTPF := dateNTP.Format(time.RFC3339Nano)
var updated = "Not Updated"
if save && dateLoc.Sub(dateNTP).Abs() > time.Millisecond*100 {
out, err := exec.Command("/bin/date", "-s", dateNTPF).Output()
if err != nil {
Error.Fatalf("Date out: %v, cmd: %v", out, err)
}
updated = "Updated"
}
Info.Printf("Time Diff %s: %v", updated, dateLoc.Sub(dateNTP))
Debug.Println("Time Local: ", dateLoc.Format(time.RFC3339Nano))
Debug.Println("Time NTP : ", dateNTPF)
}
func isRoot() bool {
currentUser, err := user.Current()
if err != nil {
Error.Fatalf("[isRoot] Unable to get current user: %s", err)
}
return currentUser.Username == "root"
}