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
- func CorrectTime(clientReceiveTime time.Time, offset int64) time.Time
- func Offset(originTime, serverReceiveTime, serverTransmitTime, clientReceiveTime time.Time) int64
- func RoundTripDelay(originTime, serverReceiveTime, serverTransmitTime, clientReceiveTime time.Time) int64
- func Time(t time.Time) (seconds uint32, fractions uint32)
- func Unix(seconds, fractions uint32) time.Time
- type Packet
Constants ¶
const ControlHeaderSizeBytes = 32
ControlHeaderSizeBytes is a buffer to read packet header with Kernel timestamps
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)
const PacketSizeBytes = 48
PacketSizeBytes sets the size of NTP packet
Variables ¶
This section is empty.
Functions ¶
func CorrectTime ¶
CorrectTime returns the correct time based on computed offset
func RoundTripDelay ¶
func RoundTripDelay(originTime, serverReceiveTime, serverTransmitTime, clientReceiveTime time.Time) int64
RoundTripDelay uses NTP algorithm for roundtrip network delay
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 ¶
BytesToPacket converts []bytes to Packet
func ReadNTPPacket ¶
ReadNTPPacket reads incoming NTP packet
func (*Packet) UnmarshalBinary ¶
UnmarshalBinary fills the Packet from []bytes
func (*Packet) ValidSettingsFormat ¶
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