PcapPlusPlus  Next
NtpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Logger.h"
4 #include "Layer.h"
5 #include "IpAddress.h"
6 
8 
11 namespace pcpp
12 {
68  class NtpLayer : public Layer
69  {
70  private:
71 #pragma pack(push, 1)
72  struct ntp_header
73  {
74 #if (BYTE_ORDER == LITTLE_ENDIAN)
76  uint8_t mode : 3,
78  version : 3,
81  leapIndicator : 2;
82 #else
85  uint8_t leapIndicator : 2,
87  version : 3,
89  mode : 3;
90 #endif
92  uint8_t stratum;
94  int8_t pollInterval,
96  precision;
98  uint32_t rootDelay,
100  rootDispersion,
103  referenceIdentifier;
105  uint64_t referenceTimestamp,
107  originTimestamp,
109  receiveTimestamp,
111  transmitTimestamp;
112  };
113 #pragma pack(pop)
114  static_assert(sizeof(ntp_header) == 48, "ntp_header size is not 48 bytes");
115 
116 #pragma pack(push, 1)
117  struct ntp_v3_auth
118  {
120  uint32_t keyID;
122  uint8_t dgst[8]; // 64 bit DES based
123  };
124 #pragma pack(pop)
125  static_assert(sizeof(ntp_v3_auth) == 12, "ntp_v3_auth size is not 12 bytes");
126 
127 #pragma pack(push, 1)
128  struct ntp_v4_auth_md5
129  {
131  uint32_t keyID;
133  uint8_t dgst[16];
134  };
135 #pragma pack(pop)
136  static_assert(sizeof(ntp_v4_auth_md5) == 20, "ntp_v4_auth_md5 size is not 20 bytes");
137 
138 #pragma pack(push, 1)
139  struct ntp_v4_auth_sha1
140  {
142  uint32_t keyID;
144  uint8_t dgst[20];
145  };
146 #pragma pack(pop)
147  static_assert(sizeof(ntp_v4_auth_sha1) == 24, "ntp_v4_auth_sha1 size is not 24 bytes");
148 
149  ntp_header* getNtpHeader() const
150  {
151  return reinterpret_cast<ntp_header*>(m_Data);
152  }
153 
154  public:
157  {
165  Unknown
166  };
167 
169  enum Mode
170  {
172  Reserved = 0,
186  PrivateUse
187  };
188 
191  enum class ClockSource : uint32_t
192  {
193  // NTPv4
194 
196  GOES = ('G') | ('O' << 8) | ('E' << 16) | ('S' << 24),
198  GPS = ('G') | ('P' << 8) | ('S' << 16),
200  GAL = ('G') | ('A' << 8) | ('L' << 16),
202  PPS = ('P') | ('P' << 8) | ('S' << 16),
204  IRIG = ('I') | ('R' << 8) | ('I' << 16) | ('G' << 24),
206  WWVB = ('W') | ('W' << 8) | ('V' << 16) | ('B' << 24),
208  DCF = ('D') | ('C' << 8) | ('F' << 16),
210  HBG = ('H') | ('B' << 8) | ('G' << 16),
212  MSF = ('M') | ('S' << 8) | ('F' << 16),
214  JJY = ('J') | ('J' << 8) | ('Y' << 16),
216  LORC = ('L') | ('O' << 8) | ('R' << 16) | ('C' << 24),
218  TDF = ('T') | ('D' << 8) | ('F' << 16),
220  CHU = ('C') | ('H' << 8) | ('U' << 16),
222  WWV = ('W') | ('W' << 8) | ('V' << 16),
224  WWVH = ('W') | ('W' << 8) | ('V' << 16) | ('H' << 24),
226  NIST = ('N') | ('I' << 8) | ('S' << 16) | ('T' << 24),
228  ACTS = ('A') | ('C' << 8) | ('T' << 16) | ('S' << 24),
230  USNO = ('U') | ('S' << 8) | ('N' << 16) | ('O' << 24),
232  PTB = ('P') | ('T' << 8) | ('B' << 16),
234  MRS = ('M') | ('R' << 8) | ('S' << 16),
236  XFAC = ('X') | ('F' << 8) | ('A' << 16) | ('C' << 24),
238  STEP = ('S') | ('T' << 8) | ('E' << 16) | ('P' << 24),
240  GOOG = ('G') | ('O' << 8) | ('O' << 16) | ('G' << 24),
243  DCFa = ('D') | ('C' << 8) | ('F' << 16) | ('a' << 24),
246  DCFp = ('D') | ('C' << 8) | ('F' << 16) | ('p' << 24),
249  GPSs = ('G') | ('P' << 8) | ('S' << 16) | ('s' << 24),
252  GPSi = ('G') | ('P' << 8) | ('S' << 16) | ('i' << 24),
255  GLNs = ('G') | ('L' << 8) | ('N' << 16) | ('s' << 24),
258  GLNi = ('G') | ('L' << 8) | ('N' << 16) | ('i' << 24),
260  LCL = ('L') | ('C' << 8) | ('L' << 16),
262  LOCL = ('L') | ('O' << 8) | ('C' << 16) | ('L' << 24),
263 
264  // NTPv3
265 
267  DCN = ('D') | ('C' << 8) | ('N' << 16),
269  TSP = ('T') | ('S' << 8) | ('P' << 16),
271  DTS = ('D') | ('T' << 8) | ('S' << 16),
273  ATOM = ('A') | ('T' << 8) | ('O' << 16) | ('M' << 24),
275  VLF = ('V') | ('L' << 8) | ('F' << 16)
276  };
277 
279  enum class KissODeath : uint32_t
280  {
282  ACST = ('A') | ('C' << 8) | ('S' << 16) | ('T' << 24),
284  AUTH = ('A') | ('U' << 8) | ('T' << 16) | ('H' << 24),
286  AUTO = ('A') | ('U' << 8) | ('T' << 16) | ('O' << 24),
288  BCST = ('B') | ('C' << 8) | ('S' << 16) | ('T' << 24),
290  CRYP = ('C') | ('R' << 8) | ('Y' << 16) | ('P' << 24),
292  DENY = ('D') | ('E' << 8) | ('N' << 16) | ('Y' << 24),
294  DROP = ('D') | ('R' << 8) | ('O' << 16) | ('P' << 24),
296  RSTR = ('R') | ('S' << 8) | ('T' << 16) | ('R' << 24),
298  INIT = ('I') | ('N' << 8) | ('I' << 16) | ('T' << 24),
300  MCST = ('M') | ('C' << 8) | ('S' << 16) | ('T' << 24),
302  NKEY = ('N') | ('K' << 8) | ('E' << 16) | ('Y' << 24),
304  RATE = ('R') | ('A' << 8) | ('T' << 16) | ('E' << 24),
307  RMOT = ('R') | ('M' << 8) | ('O' << 16) | ('T' << 24),
309  STEP = ('S') | ('T' << 8) | ('E' << 16) | ('P' << 24),
310  };
311 
317  NtpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
318  : Layer(data, dataLen, prevLayer, packet, NTP)
319  {}
320 
323 
326 
329 
331  uint8_t getVersion() const;
332 
334  void setVersion(uint8_t val);
335 
337  Mode getMode() const;
338 
340  std::string getModeString() const;
341 
343  void setMode(Mode val);
344 
346  uint8_t getStratum() const;
347 
349  void setStratum(uint8_t val);
350 
352  int8_t getPollInterval() const;
353 
356  void setPollInterval(int8_t val);
357 
359  double getPollIntervalInSecs() const;
360 
362  int8_t getPrecision() const;
363 
366  void setPrecision(int8_t val);
367 
369  double getPrecisionInSecs() const;
370 
372  uint32_t getRootDelay() const;
373 
376  void setRootDelay(uint32_t val);
377 
379  double getRootDelayInSecs() const;
380 
383  void setRootDelayInSecs(double val);
384 
386  uint32_t getRootDispersion() const;
387 
390  void setRootDispersion(uint32_t val);
391 
393  double getRootDispersionInSecs() const;
394 
397  void setRootDispersionInSecs(double val);
398 
400  uint32_t getReferenceIdentifier() const;
401 
405 
409 
413 
416  std::string getReferenceIdentifierString() const;
417 
419  uint64_t getReferenceTimestamp() const;
420 
423  void setReferenceTimestamp(uint64_t val);
424 
427 
430  void setReferenceTimestampInSecs(double val);
431 
434 
436  uint64_t getOriginTimestamp() const;
437 
440  void setOriginTimestamp(uint64_t val);
441 
443  double getOriginTimestampInSecs() const;
444 
447  void setOriginTimestampInSecs(double val);
448 
451 
453  uint64_t getReceiveTimestamp() const;
454 
457  void setReceiveTimestamp(uint64_t val);
458 
461 
464  void setReceiveTimestampInSecs(double val);
465 
468 
470  uint64_t getTransmitTimestamp() const;
471 
474  void setTransmitTimestamp(uint64_t val);
475 
478 
481  void setTransmitTimestampInSecs(double val);
482 
485 
488  uint32_t getKeyID() const;
489 
491  std::string getDigest() const;
492 
496  static double convertFromShortFormat(const uint32_t val);
497 
501  static double convertFromTimestampFormat(const uint64_t val);
502 
506  static uint32_t convertToShortFormat(const double val);
507 
511  static uint64_t convertToTimestampFormat(const double val);
512 
516  static std::string convertToIsoFormat(const double timestamp);
517 
521  static std::string convertToIsoFormat(const uint64_t timestampInNTPformat);
522 
527  static bool isDataValid(const uint8_t* data, size_t dataSize);
528 
531  static bool isNTPPort(uint16_t port)
532  {
533  return port == 123;
534  }
535 
536  // overridden methods
537 
539  void parseNextLayer() override
540  {}
541 
543  size_t getHeaderLen() const override
544  {
545  return m_DataLen;
546  }
547 
549  void computeCalculateFields() override
550  {}
551 
554  {
556  }
557 
559  std::string toString() const override;
560  };
561 } // namespace pcpp
Definition: IpAddress.h:28
Definition: Layer.h:60
The NTP packet consists of an integral number of 32-bit (4 octet) words in network byte order....
Definition: NtpLayer.h:69
double getTransmitTimestampInSecs() const
LeapIndicator
Warning of an impending leap second to be inserted or deleted in the last minute of the current month...
Definition: NtpLayer.h:157
@ Last61Secs
Last minute of the day has 61 seconds.
Definition: NtpLayer.h:161
@ Last59Secs
Last minute of the day has 59 seconds.
Definition: NtpLayer.h:163
@ NoWarning
Normal, no leap second.
Definition: NtpLayer.h:159
@ Unknown
Unknown (clock unsynchronized)
Definition: NtpLayer.h:165
void setTransmitTimestampInSecs(double val)
uint8_t getStratum() const
std::string getReceiveTimestampAsString()
void setRootDispersion(uint32_t val)
void setVersion(uint8_t val)
Set the version of NTP.
std::string getReferenceTimestampAsString()
void setReceiveTimestampInSecs(double val)
void setRootDelayInSecs(double val)
void setStratum(uint8_t val)
Set the value of stratum.
static std::string convertToIsoFormat(const uint64_t timestampInNTPformat)
uint64_t getOriginTimestamp() const
uint64_t getReferenceTimestamp() const
uint64_t getReceiveTimestamp() const
uint32_t getKeyID() const
static bool isDataValid(const uint8_t *data, size_t dataSize)
void setReferenceIdentifier(IPv4Address val)
static uint32_t convertToShortFormat(const double val)
void setRootDispersionInSecs(double val)
double getReferenceTimestampInSecs() const
std::string getOriginTimestampAsString()
static double convertFromTimestampFormat(const uint64_t val)
double getReceiveTimestampInSecs() const
void setReferenceTimestampInSecs(double val)
int8_t getPollInterval() const
double getPollIntervalInSecs() const
double getOriginTimestampInSecs() const
ClockSource
Definition: NtpLayer.h:192
@ HBG
LF Radio HBG Prangins, HB 75 kHz.
@ DCF
LF Radio DCF77 Mainflingen, DE 77.5 kHz.
@ ACTS
NIST telephone modem.
@ MRS
Multi Reference Sources.
@ IRIG
Inter-Range Instrumentation Group.
@ PPS
Generic pulse-per-second.
@ GOES
Geosynchronous Orbit Environment Satellite.
@ USNO
USNO telephone modem.
@ XFAC
Inter Face Association Changed.
@ WWV
HF Radio WWV Ft. Collins, CO.
@ MSF
LF Radio MSF Anthorn, UK 60 kHz.
@ STEP
Step time change.
@ JJY
LF Radio JJY Fukushima, JP 40 kHz, Saga, JP 60 kHz.
@ ATOM
Atomic clock (calibrated)
@ GPS
Global Position System.
@ LORC
MF Radio LORAN C station, 100 kHz.
@ WWVH
HF Radio WWVH Kauai, HI.
@ GAL
Galileo Positioning System.
@ DTS
Digital Time Service.
@ NIST
NIST telephone modem.
@ LCL
Meinberg Undisciplined local clock (Ref: https://www.meinbergglobal.com/english/info/ntp-refid....
@ CHU
HF Radio CHU Ottawa, Ontario.
@ LOCL
Meinberg Undisciplined local clock (Ref: https://www.meinbergglobal.com/english/info/ntp-refid....
@ DCN
DCN routing protocol.
@ PTB
European telephone modem.
@ TSP
TSP time protocol.
@ TDF
MF Radio Allouis, FR 162 kHz.
@ GOOG
Google Refid used by Google NTP servers as time4.google.com.
@ WWVB
LF Radio WWVB Ft. Collins, CO 60 kHz.
@ VLF
VLF radio (OMEGA, etc.)
Mode getMode() const
NtpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: NtpLayer.h:317
std::string getReferenceIdentifierString() const
void setTransmitTimestamp(uint64_t val)
void setRootDelay(uint32_t val)
static uint64_t convertToTimestampFormat(const double val)
std::string getModeString() const
void setOriginTimestamp(uint64_t val)
KissODeath
32-bit Kiss of Death (KoD) codes
Definition: NtpLayer.h:280
@ NKEY
No key found. Either the key was never installed or is not trusted.
@ RSTR
Access denied due to local policy.
@ DENY
Access denied by remote server.
@ CRYP
Cryptographic authentication or identification failed.
@ BCST
The association belongs to a broadcast server.
@ STEP
A step change in system time has occurred, but the association has not yet resynchronized.
@ RATE
Rate exceeded. The server has temporarily denied access because the client exceeded the rate threshol...
@ MCST
The association belongs to a manycast server.
@ ACST
The association belongs to a anycast server.
@ DROP
Lost peer in symmetric mode.
@ AUTO
Autokey sequence failed.
@ AUTH
Server authentication failed.
@ INIT
The association has not yet synchronized for the first time.
NtpLayer()
Empty c'tor.
std::string getDigest() const
void setReceiveTimestamp(uint64_t val)
static bool isNTPPort(uint16_t port)
Definition: NtpLayer.h:531
void setPollInterval(int8_t val)
void parseNextLayer() override
Parses the next layer. NTP is the always last so does nothing for this layer.
Definition: NtpLayer.h:539
LeapIndicator getLeapIndicator() const
OsiModelLayer getOsiModelLayer() const override
Definition: NtpLayer.h:553
int8_t getPrecision() const
uint32_t getReferenceIdentifier() const
void setReferenceIdentifier(KissODeath val)
size_t getHeaderLen() const override
Definition: NtpLayer.h:543
double getRootDispersionInSecs() const
void setReferenceIdentifier(ClockSource val)
uint64_t getTransmitTimestamp() const
void setMode(Mode val)
Set the mode.
Mode
Representing the NTP association modes.
Definition: NtpLayer.h:170
@ SymActive
Symmetrically active.
Definition: NtpLayer.h:174
@ Client
Client mode.
Definition: NtpLayer.h:178
@ Control
NTP control messages.
Definition: NtpLayer.h:184
@ Broadcast
Broadcasting mode.
Definition: NtpLayer.h:182
@ SymPassive
Symmetrically passive.
Definition: NtpLayer.h:176
@ PrivateUse
Reserved for private use.
Definition: NtpLayer.h:186
@ Server
Server mode.
Definition: NtpLayer.h:180
@ Reserved
Reserved variable.
Definition: NtpLayer.h:172
double getPrecisionInSecs() const
double getRootDelayInSecs() const
void setOriginTimestampInSecs(double val)
void setReferenceTimestamp(uint64_t val)
void setLeapIndicator(LeapIndicator val)
Set the leap indicator.
uint8_t getVersion() const
std::string toString() const override
std::string getTransmitTimestampAsString()
static std::string convertToIsoFormat(const double timestamp)
uint32_t getRootDispersion() const
void setPrecision(int8_t val)
static double convertFromShortFormat(const uint32_t val)
uint32_t getRootDelay() const
void computeCalculateFields() override
Does nothing for this layer.
Definition: NtpLayer.h:549
Definition: Packet.h:22
The main namespace for the PcapPlusPlus lib.
const ProtocolType NTP
Network Time (NTP) Protocol.
Definition: ProtocolType.h:161
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:225
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:239