Show / Hide Table of Contents

Class NtpPacket

Represents RFC4330 SNTP packet used for communication to and from a network time server.

Inheritance
object
NtpPacket
Namespace: LiteNetLib.Utils
Assembly: LiteNetLib.dll
Syntax
public class NtpPacket
Remarks

Most applications should just use the CorrectionOffset property.

The same data structure represents both request and reply packets. Request and reply differ in which properties are set and to what values.

The only real property is Bytes. All other properties read from and write to the underlying byte array with the exception of DestinationTimestamp, which is not part of the packet on network and it is instead set locally after receiving the packet.

Copied from GuerrillaNtp project with permission from Robert Vazan (@robertvazan) under MIT license, see https://github.com/RevenantX/LiteNetLib/pull/236

Constructors

NtpPacket()

Initializes default request packet.

Declaration
public NtpPacket()
Remarks

Properties Mode and VersionNumber are set appropriately for request packet. Property TransmitTimestamp is set to UtcNow.

Properties

Bytes

Gets RFC4330-encoded SNTP packet.

Declaration
public byte[] Bytes { get; }
Property Value
Type Description
byte[]

Byte array containing RFC4330-encoded SNTP packet. It is at least 48 bytes long.

Remarks

This is the only real property. All other properties except DestinationTimestamp read from or write to this byte array.

CorrectionOffset

Gets the offset that should be added to local time to synchronize it with server time.

Declaration
public TimeSpan CorrectionOffset { get; }
Property Value
Type Description
TimeSpan

Time difference between server and client. It should be added to local time to get server time. It is calculated from timestamps in the packet as 0.5 * ((t1 - t0) - (t3 - t2)) where t0 is OriginTimestamp, t1 is ReceiveTimestamp, t2 is TransmitTimestamp, and t3 is DestinationTimestamp. This property throws an exception in request packets.

DestinationTimestamp

Gets or sets the time of reception of response SNTP packet on the client.

Declaration
public DateTime? DestinationTimestamp { get; }
Property Value
Type Description
DateTime?

Time of reception of response SNTP packet on the client. It is null in request packets.

Remarks

This property is not part of the protocol and has to be set when reply packet is received.

See Also
CorrectionOffset
RoundTripTime

LeapIndicator

Gets the leap second indicator.

Declaration
public NtpLeapIndicator LeapIndicator { get; }
Property Value
Type Description
NtpLeapIndicator

Leap second warning, if any. Special value AlarmCondition indicates unsynchronized server clock. Default is NoWarning.

Remarks

Only servers fill in this property. Clients can consult this property for possible leap second warning.

Mode

Gets or sets SNTP packet mode, i.e. whether this is client or server packet.

Declaration
public NtpMode Mode { get; }
Property Value
Type Description
NtpMode

SNTP packet mode. Default is Client in newly created packets. Server reply should have this property set to Server.

OriginTimestamp

Gets or sets the time when the client sent its request.

Declaration
public DateTime? OriginTimestamp { get; }
Property Value
Type Description
DateTime?

This property is null in request packets. In reply packets, it is the time when the client sent its request. Servers copy this value from TransmitTimestamp that they find in received request packet.

See Also
CorrectionOffset
RoundTripTime

Poll

Gets server's preferred polling interval.

Declaration
public int Poll { get; }
Property Value
Type Description
int

Polling interval in log2 seconds, e.g. 4 stands for 16s and 17 means 131,072s.

Precision

Gets the precision of server clock.

Declaration
public int Precision { get; }
Property Value
Type Description
int

Clock precision in log2 seconds, e.g. -20 for microsecond precision.

ReceiveTimestamp

Gets or sets the time when the request was received by the server.

Declaration
public DateTime? ReceiveTimestamp { get; }
Property Value
Type Description
DateTime?

This property is null in request packets. In reply packets, it is the time when the server received client request.

See Also
CorrectionOffset
RoundTripTime

ReferenceId

Gets the ID of the time source used by the server or Kiss-o'-Death code sent by the server.

Declaration
public uint ReferenceId { get; }
Property Value
Type Description
uint

ID of server's time source or Kiss-o'-Death code. Purpose of this property depends on value of Stratum property.

Stratum 1 servers write here one of several special values that describe the kind of hardware clock they use.

Stratum 2 and lower servers set this property to IPv4 address of their upstream server. If upstream server has IPv6 address, the address is hashed, because it doesn't fit in this property.

When server sets Stratum to special value 0, this property contains so called kiss code that instructs the client to stop querying the server.

ReferenceTimestamp

Gets or sets the time when the server clock was last set or corrected.

Declaration
public DateTime? ReferenceTimestamp { get; }
Property Value
Type Description
DateTime?

Time when the server clock was last set or corrected or null when not specified.

Remarks

This Property is usually set only by servers. It usually lags server's current time by several minutes, so don't use this property for time synchronization.

RootDelay

Gets the total round-trip delay from the server to the reference clock.

Declaration
public TimeSpan RootDelay { get; }
Property Value
Type Description
TimeSpan

Round-trip delay to the reference clock. Normally a positive value smaller than one second.

RootDispersion

Gets the estimated error in time reported by the server.

Declaration
public TimeSpan RootDispersion { get; }
Property Value
Type Description
TimeSpan

Estimated error in time reported by the server. Normally a positive value smaller than one second.

RoundTripTime

Gets the round-trip time to the server.

Declaration
public TimeSpan RoundTripTime { get; }
Property Value
Type Description
TimeSpan

Time the request spent traveling to the server plus the time the reply spent traveling back. This is calculated from timestamps in the packet as (t1 - t0) + (t3 - t2) where t0 is OriginTimestamp, t1 is ReceiveTimestamp, t2 is TransmitTimestamp, and t3 is DestinationTimestamp. This property throws an exception in request packets.

Stratum

Gets server's distance from the reference clock.

Declaration
public int Stratum { get; }
Property Value
Type Description
int

Distance from the reference clock. This property is set only in server reply packets. Servers connected directly to reference clock hardware set this property to 1. Statum number is incremented by 1 on every hop down the NTP server hierarchy.

Special value 0 indicates that this packet is a Kiss-o'-Death message with kiss code stored in ReferenceId.

TransmitTimestamp

Gets or sets the time when the packet was sent.

Declaration
public DateTime? TransmitTimestamp { get; }
Property Value
Type Description
DateTime?

Time when the packet was sent. It should never be null. Default value is UtcNow.

Remarks

This property must be set by both clients and servers.

See Also
CorrectionOffset
RoundTripTime

VersionNumber

Gets or sets protocol version number.

Declaration
public int VersionNumber { get; }
Property Value
Type Description
int

SNTP protocol version. Default is 4, which is the latest version at the time of this writing.

Remarks

In request packets, clients should leave this property at default value 4. Servers usually reply with the same protocol version.

Methods

FromServerResponse(byte[], DateTime)

Initializes packet from data received from a server.

Declaration
public static NtpPacket FromServerResponse(byte[] bytes, DateTime destinationTimestamp)
Parameters
Type Name Description
byte[] bytes

Data received from the server.

DateTime destinationTimestamp

Utc time of reception of response SNTP packet on the client.

Returns
Type Description
NtpPacket
In this article
Back to top Generated by DocFX