Welcome to CPJNSNTPClient, a collection of freeware C++ classes to encapsulate the SNTP protocol.
Features |
Usage |
Copyright |
History |
API Reference |
Contacting the Author |
v1.25 (27 April 2022)
v1.24 (3 May 2020)
v1.23 (12 March 2020)
v1.22 (20 December 2019)
v1.21 (30 October 2019)
v1.20 (28 October 2019)
v1.19 (16 September 2019)
v1.18 (1 June 2019)
v1.17 (19 August 2018)
v1.16 (18 January 2016)
v1.15 (6 April 2015)
v1.14 (16 March 2009)
v1.13 (11 July 2008)
v1.12 (5 July 2006)
v1.11 (12 September 2005)
v1.10 (10 July 2005)
18 May 2005
V1.09 (4 April 2005)
V1.08 (17 March 2005)
V1.07 (21 September 2003)
V1.06 (13 March 2003)
V1.05 (25 August 2002)
V1.04 (29 March 2001)
V1.03 (23 July 2000)
V1.02 (25 June 2000)
V1.01 (16 November 1998)
V1.0 (8 August 1998)
The API consists of the classes:
CPJNNTPTime
This is an encapsulation of a time instance as used in the SNTP protocol. This consists of a 64 bit unsigned integer with the top 32 bits containing the number of seconds since 1st January 1900 and the lower 32 bits contain the fraction of seconds.
CPJNNTPTime
AsSYSTEMTIME
AsFILETIME
operator CPJNNTPTimePacket
operator unsigned __int64
Seconds
SecondsAsDouble
Milliseconds
Fraction
GetCurrentTime
MsToNtpFraction
NtpFractionToMs
CPJNNTPServerResponse
This is a simple encapsulation of the information retrieved from the SNTP server. It contains:
m_nLeapIndicator
m_nStratum
m_OriginateTime
m_ReceiveTime
m_TransmitTime
m_DestinationTime
m_RoundTripDelay
m_LocalClockOffset
CPJNSNTPClient
The actual class to call to perform a time lookup is CPJNSNTPClient and it consists of:
CPJNSNTPClient
GetServerTime
GetTimeout
SetTimeout
SetClientTime
CPJNNTPTime();
CPJNNTPTime(const CPJNNTPTime& time);
CPJNNTPTime(CPJNNTPTime&& time);
CPJNNTPTime(const CPJNNTPTimePacket& packet);
CPJNNTPTime(const SYSTEMTIME& st);
Parameters
time Another CPJNNTPTime instance.
packet An CPJNNTPTimePacket instance. This is a simple struct representing the data as sent over the wire.
st A Win32 SDK SYSTEMTIME instance.
Remarks
Standard C++ constructor.
SYSTEMTIME AsSYSTEMTIME() const;
Remarks
Returns a SDK SYSTEMTIME representation of the NTP time.
FILETIME AsFILETIME() const;
Remarks
Returns a SDK FILETIME representation of the NTP time.
CPJNNTPTime::operator CPJNNTPTimePacket
operator CPJNNTPTimePacket() const;
Remarks
Returns a CPJNNTPTimePacket representation of an NTP time. This structure is the actual value which gets transmitted to the SNTP server.
CPJNNTPTime::operator unsigned __int64
operator unsigned __int64() const;
Remarks
Returns an unsigned int64 representation of the NTP time.
DWORD Seconds() const;
Remarks
Returns the total number of seconds which this NTP time represents
double SecondsAsDouble() const;
Remarks
Returns the total number of seconds and fractions of a second which this NTP time represents
DWORD Milliseconds() const;
Remarks
Returns the fractional part of seconds which this NTP time represents as milliseconds
DWORD Fraction() const;
Remarks
Returns the fractional part of seconds which this NTP time represents.
static CPJNNTPTime GetCurrentTime();
Remarks
Returns an NTP time instance which represents the current UTC time of the machine.
static DWORD MsToNtpFraction(WORD wMilliSeconds);
Remarks
Converts a count of milliseconds to an NTP fractional.
static WORD NtpFractionToMs(DWORD dwFraction);
Remarks
Converts an NTP fractional to a count of milliseconds.
PJNNTPServerResponse::m_nLeapIndicator
int m_nLeapIndicator;
Remarks
This value will contain one of the following values:
0: no warning
1: last minute in day has 61 seconds
2: last minute has 59 seconds
3: clock not synchronized
PJNNTPServerResponse::m_nStratum
int m_nStratum;
Remarks
This value will contain the stratum level of the server. It will be one of the following values:
0: unspecified or unavailable
1: primary reference (e.g., radio clock)
2-15: secondary reference (via NTP or SNTP)
16-255: reserved
PJNNTPServerResponse::m_nOriginateTime
CPJNNTPTime m_OriginateTime;
Remarks
This is the client time when the request was sent from the client to the SNTP server.
PJNNTPServerResponse::m_ReceiveTime
CPJNNTPTime m_ReceiveTime;
Remarks
This is the server time when the request was received by the SNTP server from the client.
PJNNTPServerResponse::m_TransmitTime
CPJNNTPTime m_TransmitTime;
Remarks
This is the server time when the server sent the request back to the client.
PJNNTPServerResponse::m_DestinationTime
CPJNNTPTime m_DestinationTime;
Remarks
This is the client time when the reply was received by the client.
PJNNTPServerResponse::m_RoundTripDelay
double m_RoundTripDelay;
Remarks
This is the round trip time in seconds for the NTP request. It is calculated as:
m_RoundTripDelay = (m_DestinationTime - m_OriginateTime) - (m_ReceiveTime - m_TransmitTime);
PJNNTPServerResponse::m_LocalClockOffset
double m_LocalClockOffset;
Remarks
This is the local clock offset relative to the server. This is calculated as:
LocalClockOffset = ((m_ReceiveTime - m_OriginateTime) + (m_TransmitTime - m_DestinationTime)) / 2.
CPJNSNTPClient::CPJNSNTPClient
CPJNSNTPClient();
Remarks
Standard C++ constructor which initializes the timeout to a default value of 5 seconds.
void GetServerTime(LPCTSTR pszHostName, NtpServerResponse& response, int nPort = 123);
Parameters
pszHostName The network address of the SNTP server to connect to: a machine name such as “ntp.maths.tcd.ie”, or a dotted number such as “128.56.22.8” will both work.
response Upon a successful call to this function, this will contain all the information relating to the SNTP server.
nPort This is the port number of which to make a SNTP request. The default value is 123.
Remarks
This performs the actual SNTP query and returns the relevant information back in the response structure. You are then free to decide if you want to set the local time using the values retrieved. Please note that this method can and will throw CWSocketException exceptions and client code should be prepared to handle this.
DWORD GetTimeout() const;
Return Value
The current timeout to use for socket calls which will block while doing the SNTP query.
void SetTimeout(DWORD dwTimeout);
Parameters
dwTimeout The new timeout to use for socket calls which will block while doing the SNTP query.
bool SetClientTime(const SYSTEMTIME& st, bool bEnableSetTimePrivilege = true);
Parameters
st The time to be set.
bEnableSetTimePrivlege true to call the AdjustTokenPrivileges API to enable the SE_SYSTEMTIME_NAME privilege prior to setting the time.
Return Value
If the function succeeds, the return value is true. If the function fails, the return value is false. To get extended error information, call GetLastError.
Remarks
Given a time, this function will set the client system time. Internally it looks after setting privileges which is required on NT because of its security mode..
PJ Naughter
Email: pjna@naughter.com
Web: http://www.naughter.com
27 April 2022