protocol

package
v0.0.0-...-9e743c2 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package protocol implements ntp packet and basic functions to work with. It provides quick and transparent translation between 48 bytes and simply accessible struct in the most efficient way.

Index

Constants

View Source
const ControlHeaderSizeBytes = 32

ControlHeaderSizeBytes is a buffer to read packet header with Kernel timestamps

View Source
const NanosecondsToUnix = int64(2_208_988_800_000_000_000)

NanosecondsToUnix is the difference between the start of NTP Era 0 and the Unix epoch in nanoseconds Jan-1 1900 00:00:00 UTC (start of NTP epoch Era 0) and Jan-1 1970 00:00:00 UTC (start of Unix epoch) Formula is 70 * (365 + 17) * 86400 (17 leap days)

View Source
const PacketSizeBytes = 48

PacketSizeBytes sets the size of NTP packet

Variables

This section is empty.

Functions

func CorrectTime

func CorrectTime(clientReceiveTime time.Time, offset int64) time.Time

CorrectTime returns the correct time based on computed offset

func Offset

func Offset(originTime, serverReceiveTime, serverTransmitTime, clientReceiveTime time.Time) int64

Offset uses NTP algorithm for clock offset

func RoundTripDelay

func RoundTripDelay(originTime, serverReceiveTime, serverTransmitTime, clientReceiveTime time.Time) int64

RoundTripDelay uses NTP algorithm for roundtrip network delay

func Time

func Time(t time.Time) (seconds uint32, fractions uint32)

Time is converting Unix time to sec and frac NTP format

func Unix

func Unix(seconds, fractions uint32) time.Time

Unix is converting NTP seconds and fractions into Unix time

Types

type Packet

type Packet struct {
	Settings       uint8  // leap year indicator, version number and mode
	Stratum        uint8  // stratum
	Poll           int8   // poll. Power of 2
	Precision      int8   // precision. Power of 2
	RootDelay      uint32 // total delay to the reference clock
	RootDispersion uint32 // total dispersion to the reference clock
	ReferenceID    uint32 // identifier of server or a reference clock
	RefTimeSec     uint32 // last time local clock was updated sec
	RefTimeFrac    uint32 // last time local clock was updated frac
	OrigTimeSec    uint32 // client time sec
	OrigTimeFrac   uint32 // client time frac
	RxTimeSec      uint32 // receive time sec
	RxTimeFrac     uint32 // receive time frac
	TxTimeSec      uint32 // transmit time sec
	TxTimeFrac     uint32 // transmit time frac
}

Packet is an NTPv4 packet

http://seriot.ch/ntp.php https://tools.ietf.org/html/rfc958

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

0 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|LI | VN  |Mode |    Stratum     |     Poll      |  Precision   |

4 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|                         Root Delay                            |

8 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|                         Root Dispersion                       |

12+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|                          Reference ID                         |

16+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|                                                               |
+                     Reference Timestamp (64)                  +
|                                                               |

24+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|                                                               |
+                      Origin Timestamp (64)                    +
|                                                               |

32+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|                                                               |
+                      Receive Timestamp (64)                   +
|                                                               |

40+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|                                                               |
+                      Transmit Timestamp (64)                  +
|                                                               |

48+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

0 1 2 3 4 5 6 7

+-+-+-+-+-+-+-+-+ |LI | VN |Mode | +-+-+-+-+-+-+-+-+

0 1 1 0 0 0 1 1

Setting = LI | VN |Mode. Client request example: 00 011 011 (or 0x1B) | | +-- client mode (3) | + ----- version (3) + -------- leap year indicator, 0 no warning

func BytesToPacket

func BytesToPacket(ntpPacketBytes []byte) (*Packet, error)

BytesToPacket converts []bytes to Packet

func ReadNTPPacket

func ReadNTPPacket(conn *net.UDPConn) (ntp *Packet, remAddr net.Addr, err error)

ReadNTPPacket reads incoming NTP packet

func (*Packet) Bytes

func (p *Packet) Bytes() ([]byte, error)

Bytes converts Packet to []bytes

func (*Packet) UnmarshalBinary

func (p *Packet) UnmarshalBinary(b []byte) error

UnmarshalBinary fills the Packet from []bytes

func (*Packet) ValidSettingsFormat

func (p *Packet) ValidSettingsFormat() bool

ValidSettingsFormat verifies that LI | VN |Mode fields are set correctly check the first byte,include: LN:must be 0 or 3 VN:must be 1,2,3 or 4 Mode:must be 3

Jump to

Keyboard shortcuts

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