Skip to content

Commit

Permalink
Add protocol version field to Response struct
Browse files Browse the repository at this point in the history
  • Loading branch information
beevik committed Feb 11, 2024
1 parent fd508b2 commit bb6cad6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ntp.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ func (h *header) setLeap(li LeapIndicator) {
h.LiVnMode = (h.LiVnMode & 0x3f) | uint8(li)<<6
}

// getVersion returns the version value in the header.
func (h *header) getVersion() int {
return int((h.LiVnMode >> 3) & 0x7)
}

// getMode returns the mode value in the header.
func (h *header) getMode() mode {
return mode(h.LiVnMode & 0x07)
Expand Down Expand Up @@ -261,6 +266,9 @@ type Response struct {
// Precision is the reported precision of the server's clock.
Precision time.Duration

// Version is the NTP protocol version number reported by the server.
Version int

// 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
Expand Down Expand Up @@ -693,6 +701,7 @@ func generateResponse(h *header, recvTime ntpTime, authErr error) *Response {
ClockOffset: offset(h.OriginTime, h.ReceiveTime, h.TransmitTime, recvTime),
RTT: rtt(h.OriginTime, h.ReceiveTime, h.TransmitTime, recvTime),
Precision: toInterval(h.Precision),
Version: h.getVersion(),
Stratum: h.Stratum,
ReferenceID: h.ReferenceID,
ReferenceTime: h.ReferenceTime.Time(),
Expand Down
1 change: 1 addition & 0 deletions ntp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func logResponse(t *testing.T, r *Response) {
t.Logf("[%s] SystemTime: %s", host, now.Format(timeFormat))
t.Logf("[%s] ~TrueTime: %s", host, now.Add(r.ClockOffset).Format(timeFormat))
t.Logf("[%s] XmitTime: %s", host, r.Time.Format(timeFormat))
t.Logf("[%s] Version: %d", host, r.Version)
t.Logf("[%s] Stratum: %d", host, r.Stratum)
t.Logf("[%s] RefID: %s (0x%08x)", host, r.ReferenceString(), r.ReferenceID)
t.Logf("[%s] RefTime: %s", host, r.ReferenceTime.Format(timeFormat))
Expand Down

0 comments on commit bb6cad6

Please sign in to comment.