@@ -18,6 +18,7 @@ import (
1818 "time"
1919)
2020
21+ // NTPv5 errors. Will move to ntp.go once NTPv5 is finalized.
2122var (
2223 ErrInvalidDraftID = errors .New ("invalid draft ID value in response" )
2324 ErrInvalidExtensionField = errors .New ("invalid extension field in response" )
@@ -186,7 +187,7 @@ func (m *messageV5) getLeap() LeapIndicator {
186187
187188// queryV5 performs an NTPv5 time query using the provided connection.
188189func queryV5 (conn net.Conn , opt * QueryOptions ) (* Response , error ) {
189- // Generate a random client cookie if not set by the caller .
190+ // Generate a random client cookie. The response cookie needs to match .
190191 clientCookie , err := randUint64 ()
191192 if err != nil {
192193 return nil , err
@@ -258,7 +259,7 @@ func queryV5(conn net.Conn, opt *QueryOptions) (*Response, error) {
258259 return nil , err
259260 }
260261
261- // Compare cookies .
262+ // Validate the client cookie .
262263 if m .ClientCookie != clientCookie {
263264 return nil , ErrServerResponseMismatch
264265 }
@@ -339,7 +340,7 @@ func queryV5(conn net.Conn, opt *QueryOptions) (*Response, error) {
339340
340341 case extServerInfo :
341342 bits := binary .BigEndian .Uint16 (body [0 :2 ])
342- for v := 3 ; v <= 5 ; v ++ {
343+ for v := 1 ; v <= 5 ; v ++ {
343344 if bits & (1 << uint16 (v - 1 )) != 0 {
344345 r .SupportedVersions = append (r .SupportedVersions , v )
345346 }
@@ -397,7 +398,7 @@ func queryV5(conn net.Conn, opt *QueryOptions) (*Response, error) {
397398 }
398399 }
399400
400- offset += paddedLen (xlen )
401+ offset += padlen (xlen )
401402 curr = recvBuf [offset :]
402403 }
403404
@@ -535,13 +536,10 @@ func parseV5Response(data []byte) (*messageV5, error) {
535536}
536537
537538func writeExtDraftID (buf * bytes.Buffer ) {
538- valueLenPadded := paddedLen (len (draftID ))
539- totalLen := 4 + len (draftID ) // Confirm that length shouldn't include pad!
540-
541539 binary .Write (buf , binary .BigEndian , extDraftID )
542- binary .Write (buf , binary .BigEndian , uint16 (totalLen ))
540+ binary .Write (buf , binary .BigEndian , uint16 (4 + len ( draftID ) ))
543541 buf .Write ([]byte (draftID ))
544- buf .Write (padBytes [: valueLenPadded - len (draftID )] )
542+ buf .Write (pad ( len (draftID )) )
545543}
546544
547545func writeExtRefIDRequest (buf * bytes.Buffer , req ReferenceIDRequest ) {
@@ -592,6 +590,10 @@ func writeExtSecondaryTimestamp(buf *bytes.Buffer, timescale Timescale) {
592590
593591var padBytes = make ([]byte , 4 )
594592
595- func paddedLen (len int ) int {
593+ func padlen (len int ) int {
596594 return (len + 3 ) & ^ 3
597595}
596+
597+ func pad (len int ) []byte {
598+ return padBytes [:padlen (len )- len ]
599+ }
0 commit comments