ntp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

package ntp implements the NTP protocol as described in RFC 5905.

Index

Constants

View Source
const (
	SizeHeader = 48
	ClientPort = 1023 // Typical Client port number.
	ServerPort = 123  // NTP server port number
	Version4   = 4    // Current NTP Version Number
	MinPoll    = 4    // Minimum poll exponent (16s)
	MaxPoll    = 17   // Maximum poll exponent (~36h)
	MaxDisp    = 16   // Maximum dispersion (16s)
	MaxDist    = 1    // Distance threshold (1s)
	MaxStratum = 16   // Maximum stratum
	MinDispDiv = 200  // Minimum dispersion divisor 1/(200) == 0.005
)

NTP Global Parameters.

Variables

This section is empty.

Functions

func BaseTime

func BaseTime() time.Time

BaseTime returnsS the time that corresponds to the NTP base time. The zero value for Timestamp and Date types corresponds to this time.

func CalculateSystemPrecision

func CalculateSystemPrecision(nowNano func() int64, iters []int64) int8

CalculateSystemPrecision calculates the NTP system precision for a time source. If the time source is nil the default static call to time.Now->[time.Time.UnixNano] is used.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func (*Client) ConnectionID

func (c *Client) ConnectionID() *uint64

func (*Client) Demux

func (c *Client) Demux(carrierData []byte, frameOffset int) error

func (*Client) Encapsulate

func (c *Client) Encapsulate(carrierData []byte, offsetToIP, offsetToFrame int) (int, error)

func (*Client) IsDone

func (c *Client) IsDone() bool

func (*Client) LocalPort

func (c *Client) LocalPort() uint16

func (*Client) Now

func (c *Client) Now() time.Time

Now returns the current time as corrected by NTP protocol.

func (*Client) Offset

func (c *Client) Offset() time.Duration

Offset is a helper method to determine the difference between the Client's clock and the server's clock. Use Client.Now to calculate the server's time.

func (*Client) OffsetUnsynced

func (c *Client) OffsetUnsynced() time.Duration

OffsetUnsynced returns the absolute time offset difference between client and server clock as calculated by the clock synchonization algorithm. It is unsynchonized- the result of OffsetUnsynced will not change with time.

func (*Client) Protocol

func (c *Client) Protocol() uint64

func (*Client) Reset

func (c *Client) Reset(sysprec int8, now func() time.Time)

func (*Client) RoundTripDelay

func (c *Client) RoundTripDelay() time.Duration

func (*Client) ServerStratum

func (c *Client) ServerStratum() Stratum

ServerStratum returns the stratum of the server client synchronized with.

type Date

type Date struct {
	// contains filtered or unexported fields
}

The 128-bit date format is used where sufficient storage and word size are available. It includes a 64-bit signed seconds field spanning 584 billion years and a 64-bit fraction field resolving .05 attosecond (i.e., 0.5e-18).

func (Date) Time

func (d Date) Time() (time.Time, error)

type Frame

type Frame struct {
	// contains filtered or unexported fields
}

Frame encapsulates the raw data of an NTP packet and provides methods for manipulating, validating and retrieving fields and payload data. See RFC5905.

func NewFrame

func NewFrame(buf []byte) (Frame, error)

func (Frame) ClearHeader

func (frm Frame) ClearHeader()

ClearHeader zeros out the header contents.

func (Frame) Flags

func (frm Frame) Flags() (mode Mode, version uint8, lp LeapIndicator)

func (Frame) OriginTime

func (frm Frame) OriginTime() Timestamp

OriginTime is time at the client when the request departed for the server, in NTP timestamp format.

func (Frame) Poll

func (frm Frame) Poll() int8

Poll is 8-bit signed integer representing the maximum interval between successive messages, in log2 seconds. Suggested default limits for minimum and maximum poll intervals are 6 and 10, respectively.

func (Frame) Precision

func (frm Frame) Precision() int8

Precision is 8-bit signed integer representing the precision of the system clock, in log2 seconds. For instance, a value of -18 corresponds to a precision of about one microsecond. The precision can be determined when the service first starts up as the minimum time of several iterations to read the system clock.

func (Frame) ReceiveTime

func (frm Frame) ReceiveTime() Timestamp

ReceiveTime time at the server when the request arrived from the client, in NTP timestamp format.

func (Frame) ReferenceID

func (frm Frame) ReferenceID() *[4]byte

32-bit code identifying the particular server or reference clock. The interpretation depends on the value in the stratum field. For packet stratum 0 (unspecified or invalid), this is a four-character ASCII [RFC1345] string, called the "kiss code", used for debugging and monitoring purposes. For stratum 1 (reference clock), this is a four-octet, left-justified, zero-padded ASCII string assigned to the reference clock. The authoritative list of Reference Identifiers is maintained by IANA; however, any string beginning with the ASCII character "X" is reserved for unregistered experimentation and development.

func (Frame) ReferenceTime

func (frm Frame) ReferenceTime() Timestamp

ReferenceTime is when the system clock was last set or corrected, in NTP timestamp format.

func (Frame) RootDelay

func (frm Frame) RootDelay() Short

Total round-trip delay to the reference clock, in NTP short format.

func (Frame) RootDispersion

func (frm Frame) RootDispersion() Short

Total dispersion to the reference clock, in NTP short format.

func (Frame) SetFlags

func (frm Frame) SetFlags(mode Mode, version uint8, lp LeapIndicator)

func (Frame) SetOriginTime

func (frm Frame) SetOriginTime(ot Timestamp)

func (Frame) SetPoll

func (frm Frame) SetPoll(Poll int8)

func (Frame) SetPrecision

func (frm Frame) SetPrecision(Precision int8)

func (Frame) SetReceiveTime

func (frm Frame) SetReceiveTime(rt Timestamp)

func (Frame) SetReferenceTime

func (frm Frame) SetReferenceTime(rt Timestamp)

func (Frame) SetRootDelay

func (frm Frame) SetRootDelay(rd Short)

func (Frame) SetRootDispersion

func (frm Frame) SetRootDispersion(rd Short)

func (Frame) SetStratum

func (frm Frame) SetStratum(stratum Stratum)

func (Frame) SetTransmitTime

func (frm Frame) SetTransmitTime(rt Timestamp)

func (Frame) Stratum

func (frm Frame) Stratum() Stratum

func (Frame) TransmitTime

func (frm Frame) TransmitTime() Timestamp

TransmitTime at the server when the response left for the client, in NTP timestamp format.

type LeapIndicator

type LeapIndicator uint8

LeapIndicator represents the leap second indicator. It indicates whether there is no warning, an extra second (61 seconds in the last minute), or a missing second (59 seconds in the last minute).

const (
	LeapNoWarning    LeapIndicator = iota // no warning
	LeapLastMinute61                      // last minute 61
	LeapLastMinute59                      // last minute 59
)

type Mode

type Mode uint8

Mode represents the mode of the NTP message. It can be undefined, symmetric active, symmetric passive, client, server, broadcast, NTP control message, or private use.

const (
	ModeSymmetricActive   Mode // symmetric active
	ModeSymmetricPassive       // symmetric passive
	ModeClient                 // client
	ModeServer                 // server
	ModeBroadcast              // broadcast
	ModeNTPControlMessage      // control message
	ModePrivateUse             // private use
)

type Short

type Short uint32

func (Short) Fractions

func (t Short) Fractions() uint16

func (Short) Seconds

func (t Short) Seconds() uint16

type Stratum

type Stratum uint8

Stratum represents the stratum level of the NTP server.

const (
	// If the Stratum field is 0, which implies unspecified or invalid, the
	// Reference Identifier field can be used to convey messages useful for
	// status reporting and access control.  These are called Kiss-o'-Death
	// (KoD) packets and the ASCII messages they convey are called kiss codes.
	StratumUnspecified Stratum = 0  // unspecified
	StratumPrimary     Stratum = 1  // primary
	StratumUnsync      Stratum = 16 // unsynchronized
)

func (Stratum) IsSecondary

func (s Stratum) IsSecondary() bool

func (Stratum) String

func (s Stratum) String() string

String returns a human readable representation of the Stratum.

type Timestamp

type Timestamp struct {
	// contains filtered or unexported fields
}

Timestamp format is used in packet headers and other places with limited word size. It includes a 32-bit unsigned seconds field spanning 136 years and a 32-bit fraction field resolving 232 picoseconds. The 32-bit short format is used in delay and dispersion header fields where the full resolution and range of the other formats are not justified. It includes a 16-bit unsigned seconds field and a 16-bit fraction field.

func TimestampFromTime

func TimestampFromTime(t time.Time) (Timestamp, error)

func TimestampFromUint64

func TimestampFromUint64(ts uint64) Timestamp

func (Timestamp) Add

func (t Timestamp) Add(d time.Duration) Timestamp

func (Timestamp) Fractions

func (t Timestamp) Fractions() uint32

func (Timestamp) IsZero

func (t Timestamp) IsZero() bool

IsZero reports whether t represents the zero time instant.

func (Timestamp) Put

func (t Timestamp) Put(b []byte)

func (Timestamp) Seconds

func (t Timestamp) Seconds() uint32

func (Timestamp) Sub

func (t Timestamp) Sub(v Timestamp) time.Duration

func (Timestamp) Time

func (t Timestamp) Time() time.Time

Jump to

Keyboard shortcuts

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