forked from beevik/ntp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathntp.go
1135 lines (950 loc) · 27.9 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015-2017 Brett Vickers.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package ntp provides an implementation of a Simple NTP (SNTP) client
// capable of querying the current time from a remote NTP server. See
// RFC5905 (https://tools.ietf.org/html/rfc5905) for more details.
//
// This approach grew out of a go-nuts post by Michael Hofmann:
// https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/FlcdMU5fkLQ
package ntp
import (
"bytes"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"reflect"
"strings"
"time"
"github.com/secure-io/siv-go"
"golang.org/x/net/ipv4"
)
// The LeapIndicator is used to warn if a leap second should be inserted
// or deleted in the last minute of the current month.
type LeapIndicator uint8
const (
// LeapNoWarning indicates no impending leap second.
LeapNoWarning LeapIndicator = 0
// LeapAddSecond indicates the last minute of the day has 61 seconds.
LeapAddSecond = 1
// LeapDelSecond indicates the last minute of the day has 59 seconds.
LeapDelSecond = 2
// LeapNotInSync indicates an unsynchronized leap second.
LeapNotInSync = 3
)
// Internal constants
const (
defaultNtpVersion = 4
nanoPerSec = 1000000000
maxStratum = 16
defaultTimeout = 5 * time.Second
maxPollInterval = (1 << 17) * time.Second
maxDispersion = 16 * time.Second
)
// Internal variables
var (
ntpEpoch = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
)
// NTS Extension Field Types taken from https://github.com/Netnod/nts-poc-python
const (
ExtUniqueIdentifier uint16 = 0x104
ExtCookie uint16 = 0x204
ExtCookiePlaceholder uint16 = 0x304
ExtAuthenticator uint16 = 0x404
)
type mode uint8
// NTP modes. This package uses only client mode.
const (
reserved mode = 0 + iota
symmetricActive
symmetricPassive
client
server
broadcast
controlMessage
reservedPrivate
)
// An ntpTime is a 64-bit fixed-point (Q32.32) representation of the number of
// seconds elapsed.
type ntpTime uint64
// Duration interprets the fixed-point ntpTime as a number of elapsed seconds
// and returns the corresponding time.Duration value.
func (t ntpTime) Duration() time.Duration {
sec := (t >> 32) * nanoPerSec
frac := (t & 0xffffffff) * nanoPerSec >> 32
return time.Duration(sec + frac)
}
// Time interprets the fixed-point ntpTime as an absolute time and returns
// the corresponding time.Time value.
func (t ntpTime) Time() time.Time {
return ntpEpoch.Add(t.Duration())
}
// toNtpTime converts the time.Time value t into its 64-bit fixed-point
// ntpTime representation.
func toNtpTime(t time.Time) ntpTime {
nsec := uint64(t.Sub(ntpEpoch))
sec := nsec / nanoPerSec
// Round up the fractional component so that repeated conversions
// between time.Time and ntpTime do not yield continually decreasing
// results.
frac := (((nsec - sec*nanoPerSec) << 32) + nanoPerSec - 1) / nanoPerSec
return ntpTime(sec<<32 | frac)
}
// An ntpTimeShort is a 32-bit fixed-point (Q16.16) representation of the
// number of seconds elapsed.
type ntpTimeShort uint32
// Duration interprets the fixed-point ntpTimeShort as a number of elapsed
// seconds and returns the corresponding time.Duration value.
func (t ntpTimeShort) Duration() time.Duration {
t64 := uint64(t)
sec := (t64 >> 16) * nanoPerSec
frac := (t64 & 0xffff) * nanoPerSec >> 16
return time.Duration(sec + frac)
}
type NtpMsg struct {
Hdr NtpHdr
Extension []ExtensionField
}
func (nm NtpMsg) String() {
fmt.Println(nm.Hdr.string())
for _, ef := range nm.Extension {
fmt.Println(ef.string())
}
}
// Pack converts an NtpMsg to wire format. First the NTP header, then
// all the Extension Fields.
func (nm NtpMsg) Pack() (buf *bytes.Buffer, err error) {
buf = new(bytes.Buffer)
err = nm.Hdr.pack(buf)
if err != nil {
return nil, err
}
for _, ef := range nm.Extension {
err = ef.pack(buf)
if err != nil {
return nil, err
}
}
return buf, nil
}
// unpack an NTP message and all extension fields from wire format.
func (nm *NtpMsg) unpack(buf []byte, key Key) error {
var pos int // Keep track of where in the original buf we are
msgbuf := bytes.NewReader(buf)
err := nm.Hdr.unpack(msgbuf)
if err != nil {
return fmt.Errorf("unpack header: %s", err)
}
pos += 48
for msgbuf.Len() >= 28 {
var eh ExtHdr
err := eh.unpack(msgbuf)
if err != nil {
return fmt.Errorf("unpack extension field: %s", err)
}
switch eh.Type {
case ExtUniqueIdentifier:
u := UniqueIdentifier{ExtHdr: eh}
err = u.unpack(msgbuf)
if err != nil {
return fmt.Errorf("unpack UniqueIdentifier: %s", err)
}
nm.AddExt(u)
case ExtAuthenticator:
a := Authenticator{ExtHdr: eh}
err = a.unpack(msgbuf)
if err != nil {
return fmt.Errorf("unpack Authenticator: %s", err)
}
aessiv, err := siv.NewCMAC(key)
if err != nil {
return err
}
_, err = aessiv.Open(nil, a.Nonce, a.CipherText, buf[:pos])
if err != nil {
return err
}
nm.AddExt(a)
default:
// Unknown extension field. Skip it.
_, err := msgbuf.Seek(int64(eh.Length), io.SeekCurrent)
if err != nil {
return err
}
}
pos += int(eh.Length)
}
return nil
}
func (nm *NtpMsg) AddExt(ext ExtensionField) {
nm.Extension = append(nm.Extension, ext)
}
type NtpHdr struct {
Version int
Mode mode
LeapIndicator LeapIndicator
Stratum uint8
Poll int8
Precision int8
RootDelay time.Duration
RootDispersion time.Duration
ReferenceID uint32
ReferenceTime time.Time
OriginTime time.Time
ReceiveTime time.Time
TransmitTime time.Time
SpoofCookie ntpTime
Wire msg
}
func (nh NtpHdr) string() string {
s := strings.Builder{}
e := reflect.ValueOf(&nh).Elem()
for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
varValue := e.Field(i).Interface()
if varName != "Wire" {
s.WriteString(fmt.Sprintf("%v: %v\n", varName, varValue))
}
}
return s.String()
}
func (nm *NtpMsg) antiSpoof(time time.Time) (ntpTime, error) {
bits := make([]byte, 8)
_, err := rand.Read(bits)
if err != nil {
return 0, err
}
cookie := ntpTime(binary.BigEndian.Uint64(bits))
nm.Hdr.SpoofCookie = cookie
return cookie, nil
}
func (nh NtpHdr) pack(buf *bytes.Buffer) error {
var wire msg
wire.setVersion(nh.Version)
wire.setMode(nh.Mode)
wire.setLeap(nh.LeapIndicator)
wire.Stratum = nh.Stratum
wire.Poll = nh.Poll
wire.Precision = nh.Precision
// wire.RootDelay = ToNtpShortTime()
// wire.RootDispersion = ToNtpShortTime()
// refbuf := make([]byte, 4)
// binary.BigEndian.PutUint32(refbuf, nh.ReferenceID)
// for i, b := range refbuf {
// wire.ReferenceID[i] = b
// }
wire.ReferenceTime = toNtpTime(nh.ReferenceTime)
wire.OriginTime = toNtpTime(nh.OriginTime)
wire.ReceiveTime = toNtpTime(nh.ReceiveTime)
// Use TransmitTime as an anti-spoofing cookie if cookie is
// set otherwise get current time.
if nh.SpoofCookie != 0 {
wire.TransmitTime = nh.SpoofCookie
} else {
wire.TransmitTime = toNtpTime(time.Now())
}
err := binary.Write(buf, binary.BigEndian, wire)
if err != nil {
return err
}
return err
}
func (nh *NtpHdr) unpack(buf *bytes.Reader) error {
var wire msg
err := binary.Read(buf, binary.BigEndian, &wire)
if err != nil {
return err
}
nh.Wire = wire
nh.Version = wire.getVersion()
nh.Mode = wire.getMode()
nh.LeapIndicator = wire.getLeap()
nh.Stratum = wire.Stratum
// TODO nh.Poll = toInterval(wire.Poll)
nh.Precision = wire.Precision
nh.RootDelay = wire.RootDelay.Duration()
nh.RootDispersion = wire.RootDispersion.Duration()
nh.ReferenceID = wire.ReferenceID
nh.ReferenceTime = wire.ReferenceTime.Time()
switch nh.Mode {
case server:
nh.SpoofCookie = wire.OriginTime
nh.TransmitTime = wire.TransmitTime.Time()
case client:
nh.SpoofCookie = wire.TransmitTime
}
nh.ReceiveTime = wire.ReceiveTime.Time()
return nil
}
// msg is an internal representation of an NTP packet.
type msg struct {
LiVnMode uint8 // Leap Indicator (2) + Version (3) + Mode (3)
Stratum uint8
Poll int8
Precision int8
RootDelay ntpTimeShort
RootDispersion ntpTimeShort
ReferenceID uint32
ReferenceTime ntpTime
OriginTime ntpTime
ReceiveTime ntpTime
TransmitTime ntpTime
}
// setVersion sets the NTP protocol version on the message.
func (m *msg) setVersion(v int) {
m.LiVnMode = (m.LiVnMode & 0xc7) | uint8(v)<<3
}
// setMode sets the NTP protocol mode on the message.
func (m *msg) setMode(md mode) {
m.LiVnMode = (m.LiVnMode & 0xf8) | uint8(md)
}
// setLeap modifies the leap indicator on the message.
func (m *msg) setLeap(li LeapIndicator) {
m.LiVnMode = (m.LiVnMode & 0x3f) | uint8(li)<<6
}
// getVersion returns the version value in the message.
func (m *msg) getVersion() int {
return int((m.LiVnMode >> 3) & 0x07)
}
// getMode returns the mode value in the message.
func (m *msg) getMode() mode {
return mode(m.LiVnMode & 0x07)
}
// getLeap returns the leap indicator on the message.
func (m *msg) getLeap() LeapIndicator {
return LeapIndicator((m.LiVnMode >> 6) & 0x03)
}
type ExtHdr struct {
Type uint16
Length uint16
}
func (h ExtHdr) pack(buf *bytes.Buffer) error {
err := binary.Write(buf, binary.BigEndian, h)
return err
}
func (h *ExtHdr) unpack(buf *bytes.Reader) error {
err := binary.Read(buf, binary.BigEndian, h)
return err
}
func (h ExtHdr) Header() ExtHdr { return h }
func (h ExtHdr) string() string {
return fmt.Sprintf(" Extension field type: %v, len: %v\n", h.Type, h.Length)
}
type ExtensionField interface {
Header() ExtHdr
string() string
pack(*bytes.Buffer) error
}
type UniqueIdentifier struct {
ExtHdr
ID []byte
}
func (u UniqueIdentifier) string() string {
return fmt.Sprintf("-- UniqueIdentifier EF\n"+
" ID: %x\n", u.ID)
}
func (u UniqueIdentifier) pack(buf *bytes.Buffer) error {
value := new(bytes.Buffer)
err := binary.Write(value, binary.BigEndian, u.ID)
if err != nil {
return err
}
if value.Len() < 32 {
return fmt.Errorf("UniqueIdentifier.ID < 32 bytes")
}
newlen := (value.Len() + 3) & ^3
padding := make([]byte, newlen-value.Len())
u.ExtHdr.Type = ExtUniqueIdentifier
u.ExtHdr.Length = 4 + uint16(newlen)
err = u.ExtHdr.pack(buf)
if err != nil {
return err
}
_, err = buf.ReadFrom(value)
if err != nil {
return err
}
_, err = buf.Write(padding)
if err != nil {
return err
}
return nil
}
func (u *UniqueIdentifier) unpack(buf *bytes.Reader) error {
if u.ExtHdr.Type != ExtUniqueIdentifier {
return fmt.Errorf("expected unpacked EF header")
}
valueLen := u.ExtHdr.Length - uint16(binary.Size(u.ExtHdr))
id := make([]byte, valueLen)
if err := binary.Read(buf, binary.BigEndian, id); err != nil {
return err
}
u.ID = id
return nil
}
func (u *UniqueIdentifier) Generate() ([]byte, error) {
id := make([]byte, 32)
_, err := rand.Read(id)
if err != nil {
return nil, err
}
u.ID = id
return id, nil
}
type Cookie struct {
ExtHdr
Cookie []byte
}
func (c Cookie) string() string {
return fmt.Sprintf("-- Cookie EF\n"+
" %x\n", c.Cookie)
}
func (c Cookie) pack(buf *bytes.Buffer) error {
value := new(bytes.Buffer)
origlen, err := value.Write(c.Cookie)
if err != nil {
return err
}
// Round up to nearest word boundary
newlen := (origlen + 3) & ^3
padding := make([]byte, newlen-origlen)
c.ExtHdr.Type = ExtCookie
c.ExtHdr.Length = 4 + uint16(newlen)
err = c.ExtHdr.pack(buf)
if err != nil {
return err
}
_, err = buf.ReadFrom(value)
if err != nil {
return err
}
_, err = buf.Write(padding)
if err != nil {
return err
}
return nil
}
type CookiePlaceholder struct {
ExtHdr
Cookie []byte
}
type Key []byte
type Authenticator struct {
ExtHdr
NonceLen uint16
CipherTextLen uint16
Nonce []byte
CipherText []byte
Key Key
}
func (a Authenticator) string() string {
return fmt.Sprintf("-- Authenticator EF\n"+
" NonceLen: %v\n"+
" CipherTextLen: %v\n"+
" Nonce: %x\n"+
" Ciphertext: %x\n"+
" Key: %x\n",
a.NonceLen,
a.CipherTextLen,
a.Nonce,
a.CipherText,
a.Key,
)
}
func (a Authenticator) pack(buf *bytes.Buffer) error {
aessiv, err := siv.NewCMAC(a.Key)
if err != nil {
return err
}
bits := make([]byte, 16)
_, err = rand.Read(bits)
if err != nil {
return err
}
a.Nonce = bits
a.CipherText = aessiv.Seal(nil, a.Nonce, nil, buf.Bytes())
a.CipherTextLen = uint16(len(a.CipherText))
noncebuf := new(bytes.Buffer)
err = binary.Write(noncebuf, binary.BigEndian, a.Nonce)
if err != nil {
return err
}
a.NonceLen = uint16(noncebuf.Len())
cipherbuf := new(bytes.Buffer)
err = binary.Write(cipherbuf, binary.BigEndian, a.CipherText)
if err != nil {
return err
}
a.CipherTextLen = uint16(cipherbuf.Len())
extbuf := new(bytes.Buffer)
err = binary.Write(extbuf, binary.BigEndian, a.NonceLen)
if err != nil {
return err
}
err = binary.Write(extbuf, binary.BigEndian, a.CipherTextLen)
if err != nil {
return err
}
_, err = extbuf.ReadFrom(noncebuf)
if err != nil {
return err
}
noncepadding := make([]byte, (noncebuf.Len()+3) & ^3)
_, err = extbuf.Write(noncepadding)
if err != nil {
return err
}
_, err = extbuf.ReadFrom(cipherbuf)
if err != nil {
return err
}
cipherpadding := make([]byte, (cipherbuf.Len()+3) & ^3)
_, err = extbuf.Write(cipherpadding)
if err != nil {
return err
}
// FIXME Add additionalpadding as described in section 5.6 of nts draft?
a.ExtHdr.Type = ExtAuthenticator
a.ExtHdr.Length = 4 + uint16(extbuf.Len())
err = a.ExtHdr.pack(buf)
if err != nil {
return err
}
_, err = buf.ReadFrom(extbuf)
if err != nil {
return err
}
//_, err = buf.Write(additionalpadding)
//if err != nil {
// return err
//}
return nil
}
func (a *Authenticator) unpack(buf *bytes.Reader) error {
if a.ExtHdr.Type != ExtAuthenticator {
return fmt.Errorf("expected unpacked EF header")
}
// NonceLen, 2
if err := binary.Read(buf, binary.BigEndian, &a.NonceLen); err != nil {
return err
}
// CipherTextlen, 2
if err := binary.Read(buf, binary.BigEndian, &a.CipherTextLen); err != nil {
return err
}
// Nonce
nonce := make([]byte, a.NonceLen)
if err := binary.Read(buf, binary.BigEndian, &nonce); err != nil {
return err
}
a.Nonce = nonce
// Ciphertext
ciphertext := make([]byte, a.CipherTextLen)
if err := binary.Read(buf, binary.BigEndian, ciphertext); err != nil {
return err
}
a.CipherText = ciphertext
return nil
}
// QueryOptions contains the list of configurable options that may be used
// with the QueryWithOptions function.
type QueryOptions struct {
Timeout time.Duration // defaults to 5 seconds
Version int // NTP protocol version, defaults to 4
LocalAddress string // IP address to use for the client address
Port int // Server port, defaults to 123
TTL int // IP TTL to use, defaults to system default
Protocol string // Protocol to use, defaults to udp
NTS bool // Use Network Time Security (NTS)?
C2s Key // Client to server key for NTS.
S2c Key // Server to client key for NTS.
Cookie []byte // Cookie for NTS.
Debug bool
}
// A Response contains time data, some of which is returned by the NTP server
// and some of which is calculated by the client.
type Response struct {
// Time is the transmit time reported by the server just before it
// responded to the client's NTP query.
Time time.Time
// ClockOffset is the estimated offset of the client clock relative to
// the server. Add this to the client's system clock time to obtain a
// more accurate time.
ClockOffset time.Duration
// RTT is the measured round-trip-time delay estimate between the client
// and the server.
RTT time.Duration
// Precision is the reported precision of the server's clock.
Precision time.Duration
// Stratum is the "stratum level" of the server. The smaller the number,
// the closer the server is to the reference clock. Stratum 1 servers are
// attached directly to the reference clock. A stratum value of 0
// indicates the "kiss of death," which typically occurs when the client
// issues too many requests to the server in a short period of time.
Stratum uint8
// ReferenceID is a 32-bit identifier identifying the server or
// reference clock.
ReferenceID uint32
// ReferenceTime is the time when the server's system clock was last
// set or corrected.
ReferenceTime time.Time
// RootDelay is the server's estimated aggregate round-trip-time delay to
// the stratum 1 server.
RootDelay time.Duration
// RootDispersion is the server's estimated maximum measurement error
// relative to the stratum 1 server.
RootDispersion time.Duration
// RootDistance is an estimate of the total synchronization distance
// between the client and the stratum 1 server.
RootDistance time.Duration
// Leap indicates whether a leap second should be added or removed from
// the current month's last minute.
Leap LeapIndicator
// MinError is a lower bound on the error between the client and server
// clocks. When the client and server are not synchronized to the same
// clock, the reported timestamps may appear to violate the principle of
// causality. In other words, the NTP server's response may indicate
// that a message was received before it was sent. In such cases, the
// minimum error may be useful.
MinError time.Duration
// KissCode is a 4-character string describing the reason for a
// "kiss of death" response (stratum = 0). For a list of standard kiss
// codes, see https://tools.ietf.org/html/rfc5905#section-7.4.
KissCode string
// Poll is the maximum interval between successive NTP polling messages.
// It is not relevant for simple NTP clients like this one.
Poll time.Duration
}
// Validate checks if the response is valid for the purposes of time
// synchronization.
func (r *Response) Validate() error {
// Handle invalid stratum values.
if r.Stratum == 0 {
return fmt.Errorf("kiss of death received: %s", r.KissCode)
}
if r.Stratum >= maxStratum {
return errors.New("invalid stratum in response")
}
// Handle invalid leap second indicator.
if r.Leap == LeapNotInSync {
return errors.New("invalid leap second")
}
// Estimate the "freshness" of the time. If it exceeds the maximum
// polling interval (~36 hours), then it cannot be considered "fresh".
freshness := r.Time.Sub(r.ReferenceTime)
if freshness > maxPollInterval {
return errors.New("server clock not fresh")
}
// Calculate the peer synchronization distance, lambda:
// lambda := RootDelay/2 + RootDispersion
// If this value exceeds MAXDISP (16s), then the time is not suitable
// for synchronization purposes.
// https://tools.ietf.org/html/rfc5905#appendix-A.5.1.1.
lambda := r.RootDelay/2 + r.RootDispersion
if lambda > maxDispersion {
return errors.New("invalid dispersion")
}
// If the server's transmit time is before its reference time, the
// response is invalid.
if r.Time.Before(r.ReferenceTime) {
return errors.New("invalid time reported")
}
// nil means the response is valid.
return nil
}
// Query returns a response from the remote NTP server host. It contains
// the time at which the server transmitted the response as well as other
// useful information about the time and the remote server.
func Query(host string) (*Response, error) {
return QueryWithOptions(host, QueryOptions{})
}
// QueryWithOptions performs the same function as Query but allows for the
// customization of several query options.
func QueryWithOptions(host string, opt QueryOptions) (*Response, error) {
m, now, err := getTime(host, opt)
if err != nil {
return nil, err
}
return parseTime(m, now), nil
}
// TimeV returns the current time using information from a remote NTP server.
// On error, it returns the local system time. The version may be 2, 3, or 4.
//
// Deprecated: TimeV is deprecated. Use QueryWithOptions instead.
func TimeV(host string, version int) (time.Time, error) {
m, recvTime, err := getTime(host, QueryOptions{Version: version})
if err != nil {
return time.Now(), err
}
r := parseTime(m, recvTime)
err = r.Validate()
if err != nil {
return time.Now(), err
}
// Use the clock offset to calculate the time.
return time.Now().Add(r.ClockOffset), nil
}
// Time returns the current time using information from a remote NTP server.
// It uses version 4 of the NTP protocol. On error, it returns the local
// system time.
func Time(host string) (time.Time, error) {
return TimeV(host, defaultNtpVersion)
}
// getTime performs the NTP server query and returns the response message
// along with the local system time it was received.
func getTime(host string, opt QueryOptions) (*msg, ntpTime, error) {
if opt.Version == 0 {
opt.Version = defaultNtpVersion
}
if opt.Version < 2 || opt.Version > 4 {
return nil, 0, errors.New("invalid protocol version requested")
}
if opt.Protocol == "" {
opt.Protocol = "udp"
}
// Resolve the remote NTP server address.
raddr, err := net.ResolveUDPAddr(opt.Protocol, net.JoinHostPort(host, "123"))
if err != nil {
return nil, 0, err
}
// Resolve the local address if specified as an option.
var laddr *net.UDPAddr
if opt.LocalAddress != "" {
laddr, err = net.ResolveUDPAddr(opt.Protocol, net.JoinHostPort(opt.LocalAddress, "0"))
if err != nil {
return nil, 0, err
}
}
// Override the port if requested.
if opt.Port != 0 {
raddr.Port = opt.Port
}
// Prepare a "connection" to the remote server.
con, err := net.DialUDP(opt.Protocol, laddr, raddr)
if err != nil {
return nil, 0, err
}
defer con.Close()
// Set a TTL for the packet if requested.
if opt.TTL != 0 {
ipcon := ipv4.NewConn(con)
err = ipcon.SetTTL(opt.TTL)
if err != nil {
return nil, 0, err
}
}
// Set a timeout on the connection.
if opt.Timeout == 0 {
opt.Timeout = defaultTimeout
}
con.SetDeadline(time.Now().Add(opt.Timeout))
var xmitmsg NtpMsg
xmitmsg.Hdr.Version = opt.Version
xmitmsg.Hdr.Mode = client
xmitmsg.Hdr.LeapIndicator = LeapNotInSync
// Keep track of when the messsage was actually transmitted.
// Use TransmitTime in NTP header as an anti-spoofing cookie.
xmitTime := time.Now()
spoofcookie, err := xmitmsg.antiSpoof(xmitTime)
if err != nil {
return nil, 0, err
}
var uqext UniqueIdentifier
if opt.NTS {
// Generate and remember a unique identifier for our packet
_, err = uqext.Generate()
if err != nil {
return nil, 0, err
}
xmitmsg.AddExt(uqext)
var c Cookie
c.Cookie = opt.Cookie
xmitmsg.AddExt(c)
var auth Authenticator
auth.Key = opt.C2s
xmitmsg.AddExt(auth)
}
buf, err := xmitmsg.Pack()
if err != nil {
return nil, 0, err
}
if opt.Debug {
fmt.Printf("Sending: \n")
xmitmsg.String()
fmt.Printf("wire: %x\n", buf)
}
// Transmit the query.
_, err = con.Write(buf.Bytes())
if err != nil {
return nil, 0, err
}
// Receive the response.
readbuf := make([]byte, 64*1024)
n, _, err := con.ReadFromUDP(readbuf)
if err != nil {
return nil, 0, err
}
readbuf = readbuf[:n]
var recv NtpMsg
err = recv.unpack(readbuf, opt.S2c)
if err != nil {
return nil, 0, err
}
if opt.Debug {
fmt.Printf("Received: \n")
recv.String()
fmt.Printf("Received wire: %v\n", recv.Hdr.Wire)
}
// FIXME Workaround for now since code later on works directly on wire format header.
recvMsg := recv.Hdr.Wire
if opt.NTS {
for _, ef := range recv.Extension {
switch ef.Header().Type {
case ExtUniqueIdentifier:
if !bytes.Equal(ef.(UniqueIdentifier).ID, uqext.ID) {
return nil, 0, fmt.Errorf("UniqueIdentifier mismatch!")
}
case ExtAuthenticator:
// TODO handle now decrypted Extension Fields
}
}
}
// Keep track of the time the response was received.
delta := time.Since(xmitTime)
if delta < 0 {
// The local system may have had its clock adjusted since it
// sent the query. In go 1.9 and later, time.Since ensures
// that a monotonic clock is used, so delta can never be less
// than zero. In versions before 1.9, a monotonic clock is
// not used, so we have to check.
return nil, 0, errors.New("client clock ticked backwards")
}
recvTime := toNtpTime(xmitTime.Add(delta))
// Check for invalid fields.
if recv.Hdr.Mode != server {
return nil, 0, errors.New("invalid mode in response")
}