ntp

package
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

NTP v4 (RFC 5905) http://tools.ietf.org/html/rfc5905

Index

Constants

View Source
const JAN_1970 = 2208988800

JAN_1970 is the Unix Epoch in NTP Seconds (1970 - 1900)

Variables

This section is empty.

Functions

func Precision

func Precision() int8

Precision represents the accuracy of times reported by Now() for use in Header.Precision. The return value is log2 seconds.

Types

type Header struct {
	LeapIndicator      LeapIndicator // 2 bits
	VersionNumber      VersionNumber // 3 bits
	Mode               Mode          // 3 bits
	Stratum            uint8
	Poll               int8
	Precision          int8
	RootDelay          Short
	RootDispersion     Short
	ReferenceId        [4]byte
	ReferenceTimestamp Timestamp
	OriginTimestamp    Timestamp
	ReceiveTimestamp   Timestamp
	TransmitTimestamp  Timestamp
}

NTP Packet Header Format

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|LI | VN  |Mode |    Stratum    |     Poll      |   Precision   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                         Root Delay                            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                         Root Dispersion                       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                          Reference ID                         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
+                     Reference Timestamp (64)                  +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
+                      Origin Timestamp (64)                    +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
+                      Receive Timestamp (64)                   +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
+                      Transmit Timestamp (64)                  +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
.                                                               .
.                    Extension Field 1 (variable)               .
.                                                               .
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
.                                                               .
.                    Extension Field 2 (variable)               .
.                                                               .
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                          Key Identifier                       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
|                            dgst (128)                         |
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

func (*Header) MarshalBinary

func (h *Header) MarshalBinary() ([]byte, error)

func (*Header) UnmarshalBinary

func (h *Header) UnmarshalBinary(data []byte) error

type LeapIndicator

type LeapIndicator byte

LeapIndicator (LI): 2-bit integer warning of an impending leap second to be inserted or deleted in the last minute of the current month.

const (
	LEAP_NONE   LeapIndicator = iota
	LEAP_ADD                  // last minute of the day has 61 seconds
	LEAP_SUB                  // last minute of the day has 59 seconds
	LEAP_NOSYNC               // unknown (clock unsynchronized)
)

func (LeapIndicator) String

func (i LeapIndicator) String() string

type Mode

type Mode byte

Mode: 3-bit integer representing the association mode.

const (
	MODE_RESERVED Mode = iota
	MODE_SYMMETRIC_ACTIVE
	MODE_SYMMETRIC_PASSIVE
	MODE_CLIENT
	MODE_SERVER
	MODE_BROADCAST
	MODE_CONTROL // NTP control message
	MODE_PRIVATE // reserved for private use
)

func (Mode) String

func (i Mode) String() string

type Server

type Server struct {
	net.PacketConn
	// contains filtered or unexported fields
}

A simple NTP server intended for testing. It can serve time at some offset from the real time and adjust for a single leap second.

func NewServer

func NewServer(addr string) (*Server, error)

Create a NTP server that listens on the given address.

func (*Server) Accept

func (s *Server) Accept() (*ServerReq, error)

Accept a single NTP request.

func (*Server) Respond

func (s *Server) Respond(r *ServerReq)

Respond to a single NTP request.

func (*Server) Serve

func (s *Server) Serve()

Serve NTP requests forever.

func (*Server) SetLeapSecond

func (s *Server) SetLeapSecond(second time.Time, direction LeapIndicator)

Must be exactly midnight on the first day of the month. This is the first time that is always valid after the leap has occurred for both adding and removing a second. This is the same way leap seconds are officially listed. https://www.ietf.org/timezones/data/leap-seconds.list

func (*Server) SetTime

func (s *Server) SetTime(now time.Time)

Adjust the internal time offset to begin serving based on the given time.

func (*Server) UpdateOffset

func (s *Server) UpdateOffset(now time.Time) (time.Duration, LeapIndicator)

Get the current offset between real time and the server's time, adjusting for a leap second as needed. now is real time, not server time.

type ServerReq

type ServerReq struct {
	Client   net.Addr
	Received time.Time
	Packet   []byte
}

type Short

type Short struct {
	Seconds  uint16
	Fraction uint16
}

NTP Short Format

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Seconds              |           Fraction            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

type Timestamp

type Timestamp struct {
	Seconds  uint32
	Fraction uint32
}

NTP Timestamp Format

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                            Seconds                            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                            Fraction                           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

func NewTimestamp

func NewTimestamp(t time.Time) Timestamp

NewTimestamp converts from Go's Time to NTP's 64-bit Timestamp format.

func Now

func Now() Timestamp

Now gets the current NTP time in the 64-bit Timestamp format.

type VersionNumber

type VersionNumber byte

VersionNumber (VN): 3-bit integer representing the NTP version number, currently 4.

const NTPv4 VersionNumber = 4

func (VersionNumber) String

func (i VersionNumber) String() string

Notes

Bugs

  • Since our clock source is UTC instead of TAI or some type of monotonic clock, trying to use this server during a real leap second will lead to incorrect results. Timekeeping sucks.

  • We doesn't account for the possibility of UpdateOffset behaving differently for the transmit time instead of the received time. No idea what the correct behavior is.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL