github.com/annchain/OG@v0.0.9/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 "net" 25 "sort" 26 "time" 27 ) 28 29 const ( 30 ntpPool = "pool.ntp.org" // ntpPool is the NTP server to query for the current time 31 ntpChecks = 3 // Number of measurements to do against the NTP server 32 ) 33 34 // durationSlice attaches the methods of sort.Interface to []time.Duration, 35 // sorting in increasing order. 36 type durationSlice []time.Duration 37 38 func (s durationSlice) Len() int { return len(s) } 39 func (s durationSlice) Less(i, j int) bool { return s[i] < s[j] } 40 func (s durationSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 41 42 // checkClockDrift queries an NTP server for clock drifts and warns the user if 43 // one large enough is detected. 44 func checkClockDrift() { 45 drift, err := sntpDrift(ntpChecks) 46 if err != nil { 47 return 48 } 49 if drift < -driftThreshold || drift > driftThreshold { 50 log.Warn(fmt.Sprintf("system clock seems off by %v, which can prevent network connectivity", drift)) 51 log.Warn("please enable network time synchronisation in system settings.") 52 } else { 53 log.Debug("NTP sanity check done", "drift", drift) 54 } 55 } 56 57 // sntpDrift does a naive time resolution against an NTP server and returns the 58 // measured drift. This method uses the simple version of NTP. It's not precise 59 // but should be fine for these purposes. 60 // 61 // Note, it executes two extra measurements compared to the number of requested 62 // ones to be able to discard the two extremes as outliers. 63 func sntpDrift(measurements int) (time.Duration, error) { 64 // Resolve the address of the NTP server 65 addr, err := net.ResolveUDPAddr("udp", ntpPool+":123") 66 if err != nil { 67 return 0, err 68 } 69 // Construct the time request (empty package with only 2 fields set): 70 // Bits 3-5: Protocol version, 3 71 // Bits 6-8: Mode of operation, client, 3 72 request := make([]byte, 48) 73 request[0] = 3<<3 | 3 74 75 // Execute each of the measurements 76 drifts := []time.Duration{} 77 for i := 0; i < measurements+2; i++ { 78 // Dial the NTP server and send the time retrieval request 79 conn, err := net.DialUDP("udp", nil, addr) 80 if err != nil { 81 return 0, err 82 } 83 defer conn.Close() 84 85 sent := time.Now() 86 if _, err = conn.Write(request); err != nil { 87 return 0, err 88 } 89 // Retrieve the reply and calculate the elapsed time 90 conn.SetDeadline(time.Now().Add(5 * time.Second)) 91 92 reply := make([]byte, 48) 93 if _, err = conn.Read(reply); err != nil { 94 return 0, err 95 } 96 elapsed := time.Since(sent) 97 98 // Reconstruct the time from the reply data 99 sec := uint64(reply[43]) | uint64(reply[42])<<8 | uint64(reply[41])<<16 | uint64(reply[40])<<24 100 frac := uint64(reply[47]) | uint64(reply[46])<<8 | uint64(reply[45])<<16 | uint64(reply[44])<<24 101 102 nanosec := sec*1e9 + (frac*1e9)>>32 103 104 t := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(nanosec)).Local() 105 106 // Calculate the drift based on an assumed answer time of RRT/2 107 drifts = append(drifts, sent.Sub(t)+elapsed/2) 108 } 109 // Calculate average drif (drop two extremities to avoid outliers) 110 sort.Sort(durationSlice(drifts)) 111 112 drift := time.Duration(0) 113 for i := 1; i < len(drifts)-1; i++ { 114 drift += drifts[i] 115 } 116 return drift / time.Duration(measurements), nil 117 }