Skip to content

Commit 8be8a82

Browse files
committed
Improve comments and minor refactors
1 parent 5ad77b2 commit 8be8a82

5 files changed

Lines changed: 24 additions & 29 deletions

File tree

auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ func calcCMAC_AES(payload, key []byte) []byte {
115115
xor(cmac, payload)
116116
xor(cmac, k1)
117117
} else {
118-
xor(cmac, pad(payload))
118+
xor(cmac, padblock(payload))
119119
xor(cmac, k2)
120120
}
121121
c.Encrypt(cmac, cmac)
122122

123123
return cmac
124124
}
125125

126-
func pad(block []byte) []byte {
126+
func padblock(block []byte) []byte {
127127
pad := make([]byte, 16-len(block))
128128
pad[0] = 0x80
129129
return append(block, pad...)

ntp.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,12 @@ type QueryOptions struct {
177177
// of the response. Used only in NTPv5.
178178
RequestReferenceID ReferenceIDRequest
179179

180-
// RequestSupportedVersions indicates whether to request which versions of
181-
// the NTP protocol are supported by the server. When used with NTPv5, the
180+
// RequestSupportedVersions indicates whether to request the versions of
181+
// the NTP protocol supported by the server. When used with NTPv5, the
182182
// response will list all supported versions. When used with NTPv3 or
183-
// NTPv4, the response's supported versions will include the version used
184-
// in the query as well as 5 if the server supports it. The response's
185-
// ReferenceTime field will be invalid in NTPv3 and NTPv4 if this option
186-
// is set.
183+
// NTPv4, the response's ReferenceTime value will be invalid, and the
184+
// supported version list will include the version used in the query as
185+
// well as 5 if the server supports it.
187186
RequestSupportedVersions bool
188187

189188
// RequestCorrection indicates whether to request delay corrections from
@@ -216,13 +215,9 @@ type QueryOptions struct {
216215
// is guaranteed to include a port number.
217216
Dialer func(localAddress, remoteAddress string) (net.Conn, error)
218217

219-
// Dial is a callback used to override the default UDP network dialer.
220-
//
221218
// DEPRECATED. Use Dialer instead.
222219
Dial func(laddr string, lport int, raddr string, rport int) (net.Conn, error)
223220

224-
// Port indicates the port used to reach the remote NTP server.
225-
//
226221
// DEPRECATED. Embed the port number in the query address string instead.
227222
Port int
228223
}
@@ -370,10 +365,6 @@ type Response struct {
370365
// Used only in NTPv5.
371366
ServerCookie uint64
372367

373-
// Time is the time the server transmitted this response, measured using
374-
// its own clock. You should not use this value for time synchronization
375-
// purposes. Add ClockOffset to your system clock instead.
376-
//
377368
// DEPRECATED. Use Timestamps.ServerXmit instead.
378369
Time time.Time
379370

ntp4.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,14 @@ func queryV4(conn net.Conn, opt *QueryOptions) (*Response, error) {
198198
Time: m.TransmitTime.TimeV4(),
199199
}
200200

201-
// If NTPv5 support was requested, check for it.
201+
// If supported versions were requested, check the response for an answer.
202202
if opt.RequestSupportedVersions {
203+
// Always include the version used in the query.
203204
r.SupportedVersions = []int{opt.Version}
204205

205206
// If the server responded to the NTPv5 support request in its
206-
// ReferenceTime field, add version 5 and clear the ReferenceTime.
207+
// ReferenceTime field, add version 5 to the list and invalidate the
208+
// ReferenceTime.
207209
if m.ReferenceTime == v5sentinel {
208210
r.SupportedVersions = append(r.SupportedVersions, 5)
209211
r.ReferenceTime = ntpEra0

ntp5.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919
)
2020

21+
// NTPv5 errors. Will move to ntp.go once NTPv5 is finalized.
2122
var (
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.
188189
func 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

537538
func 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

547545
func writeExtRefIDRequest(buf *bytes.Buffer, req ReferenceIDRequest) {
@@ -592,6 +590,10 @@ func writeExtSecondaryTimestamp(buf *bytes.Buffer, timescale Timescale) {
592590

593591
var 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+
}

ntp5_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func TestOfflineV5DraftIDExtension(t *testing.T) {
264264
xlen := binary.BigEndian.Uint16(data[2:4])
265265

266266
assert.Equal(t, extDraftID, xtype)
267-
assert.Equal(t, buf.Len(), paddedLen(int(xlen)), "Length should include header")
267+
assert.Equal(t, buf.Len(), padlen(int(xlen)), "Length should include header")
268268

269269
assert.True(t, bytes.Contains(data[4:], []byte(draftID)), "Extension should contain draft ID string")
270270
}

0 commit comments

Comments
 (0)