github.com/amazechain/amc@v0.1.3/internal/p2p/discover/ntp.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Contains the NTP time drift detection via the SNTP protocol: 18 // https://tools.ietf.org/html/rfc4330 19 20 package discover 21 22 import ( 23 "fmt" 24 "github.com/amazechain/amc/log" 25 "net" 26 "sort" 27 "time" 28 ) 29 30 const ( 31 ntpPool = "pool.ntp.org" // ntpPool is the NTP server to query for the current time 32 ntpChecks = 3 // Number of measurements to do against the NTP server 33 ) 34 35 // durationSlice attaches the methods of sort.Interface to []time.Duration, 36 // sorting in increasing order. 37 type durationSlice []time.Duration 38 39 func (s durationSlice) Len() int { return len(s) } 40 func (s durationSlice) Less(i, j int) bool { return s[i] < s[j] } 41 func (s durationSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 42 43 // checkClockDrift queries an NTP server for clock drifts and warns the user if 44 // one large enough is detected. 45 func checkClockDrift() { 46 drift, err := sntpDrift(ntpChecks) 47 if err != nil { 48 return 49 } 50 if drift < -driftThreshold || drift > driftThreshold { 51 log.Warn(fmt.Sprintf("System clock seems off by %v, which can prevent network connectivity", drift)) 52 log.Warn("Please enable network time synchronisation in system settings.") 53 } else { 54 log.Debug("NTP sanity check done", "drift", drift) 55 } 56 } 57 58 // sntpDrift does a naive time resolution against an NTP server and returns the 59 // measured drift. This method uses the simple version of NTP. It's not precise 60 // but should be fine for these purposes. 61 // 62 // Note, it executes two extra measurements compared to the number of requested 63 // ones to be able to discard the two extremes as outliers. 64 func sntpDrift(measurements int) (time.Duration, error) { 65 // Resolve the address of the NTP server 66 addr, err := net.ResolveUDPAddr("udp", ntpPool+":123") 67 if err != nil { 68 return 0, err 69 } 70 // Construct the time request (empty package with only 2 fields set): 71 // Bits 3-5: Protocol version, 3 72 // Bits 6-8: Mode of operation, client, 3 73 request := make([]byte, 48) 74 request[0] = 3<<3 | 3 75 76 // Execute each of the measurements 77 drifts := []time.Duration{} 78 for i := 0; i < measurements+2; i++ { 79 // Dial the NTP server and send the time retrieval request 80 conn, err := net.DialUDP("udp", nil, addr) 81 if err != nil { 82 return 0, err 83 } 84 defer conn.Close() 85 86 sent := time.Now() 87 if _, err = conn.Write(request); err != nil { 88 return 0, err 89 } 90 // Retrieve the reply and calculate the elapsed time 91 conn.SetDeadline(time.Now().Add(5 * time.Second)) 92 93 reply := make([]byte, 48) 94 if _, err = conn.Read(reply); err != nil { 95 return 0, err 96 } 97 elapsed := time.Since(sent) 98 99 // Reconstruct the time from the reply data 100 sec := uint64(reply[43]) | uint64(reply[42])<<8 | uint64(reply[41])<<16 | uint64(reply[40])<<24 101 frac := uint64(reply[47]) | uint64(reply[46])<<8 | uint64(reply[45])<<16 | uint64(reply[44])<<24 102 103 nanosec := sec*1e9 + (frac*1e9)>>32 104 105 t := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(nanosec)).Local() 106 107 // Calculate the drift based on an assumed answer time of RRT/2 108 drifts = append(drifts, sent.Sub(t)+elapsed/2) 109 } 110 // Calculate average drift (drop two extremities to avoid outliers) 111 sort.Sort(durationSlice(drifts)) 112 113 drift := time.Duration(0) 114 for i := 1; i < len(drifts)-1; i++ { 115 drift += drifts[i] 116 } 117 return drift / time.Duration(measurements), nil 118 }