-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntp.go
71 lines (53 loc) · 1.64 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
package main
import (
"encoding/binary"
"fmt"
"net"
"sync"
"time"
)
func CallNTPAsync(address string, quiet bool, channel chan *time.Time, group *sync.WaitGroup) {
currentTime := CallNTP(address, quiet)
if currentTime != nil {
channel <- currentTime
}
group.Done()
}
// CallNTP Calls an NTP server according to [RFC 5905](https://datatracker.ietf.org/doc/html/rfc5905), and returns the current time.
// Uses the NTP v4
func CallNTP(address string, quiet bool) *time.Time {
if !quiet {
fmt.Printf("Calling NTP server at %s...\n", address)
}
packet := make([]byte, 48)
// leap 0, version 4, mode 3 (client)
// 0 4 3 -> 00 100 011 -> 0x23
packet[0] = 0x23
connection, err := net.Dial("udp", address)
if err != nil {
fmt.Println("An error occurred: ", err)
return nil
}
defer connection.Close()
// Set a deadline for the connection
if deadlineExceededError := connection.SetDeadline(time.Now().Add(3 * time.Second)); deadlineExceededError != nil {
fmt.Println("Deadline exceeded: ", deadlineExceededError)
return nil
}
// Send the packet to the server
if _, writeError := connection.Write(packet); writeError != nil {
fmt.Println("Writing the packet failed: ", writeError)
return nil
}
responsePacket := make([]byte, 48)
// Read the response from the server
if _, readError := connection.Read(responsePacket); readError != nil {
fmt.Println("Reading the response failed: ", readError)
return nil
}
ntpSeconds := binary.BigEndian.Uint32(responsePacket[40:44])
// RFC 868: https://datatracker.ietf.org/doc/rfc868/
unixSeconds := int64(ntpSeconds) - 2208988800
unixTime := time.Unix(unixSeconds, 0)
return &unixTime
}