PcapPlusPlus  24.09
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 
13 namespace pcpp
14 {
73  class NtpLayer : public Layer
74  {
75  private:
76 #pragma pack(push, 1)
77  struct ntp_header
78  {
79 #if (BYTE_ORDER == LITTLE_ENDIAN)
81  uint8_t mode : 3,
83  version : 3,
86  leapIndicator : 2;
87 #else
90  uint8_t leapIndicator : 2,
92  version : 3,
94  mode : 3;
95 #endif
97  uint8_t stratum;
99  int8_t pollInterval,
101  precision;
103  uint32_t rootDelay,
105  rootDispersion,
108  referenceIdentifier;
110  uint64_t referenceTimestamp,
112  originTimestamp,
114  receiveTimestamp,
116  transmitTimestamp;
117  };
118 #pragma pack(pop)
119 
120 #pragma pack(push, 1)
121  struct ntp_v3_auth
122  {
124  uint32_t keyID;
126  uint8_t dgst[8]; // 64 bit DES based
127  };
128 #pragma pack(pop)
129 
130 #pragma pack(push, 1)
131  struct ntp_v4_auth_md5
132  {
134  uint32_t keyID;
136  uint8_t dgst[16];
137  };
138 #pragma pack(pop)
139 
140 #pragma pack(push, 1)
141  struct ntp_v4_auth_sha1
142  {
144  uint32_t keyID;
146  uint8_t dgst[20];
147  };
148 #pragma pack(pop)
149 
150  ntp_header* getNtpHeader() const
151  {
152  return (ntp_header*)m_Data;
153  }
154 
155  public:
160  {
168  Unknown
169  };
170 
174  enum Mode
175  {
177  Reserved = 0,
191  PrivateUse
192  };
193 
198  enum class ClockSource : uint32_t
199  {
200  // NTPv4
201 
203  GOES = ('G') | ('O' << 8) | ('E' << 16) | ('S' << 24),
205  GPS = ('G') | ('P' << 8) | ('S' << 16),
207  GAL = ('G') | ('A' << 8) | ('L' << 16),
209  PPS = ('P') | ('P' << 8) | ('S' << 16),
211  IRIG = ('I') | ('R' << 8) | ('I' << 16) | ('G' << 24),
213  WWVB = ('W') | ('W' << 8) | ('V' << 16) | ('B' << 24),
215  DCF = ('D') | ('C' << 8) | ('F' << 16),
217  HBG = ('H') | ('B' << 8) | ('G' << 16),
219  MSF = ('M') | ('S' << 8) | ('F' << 16),
221  JJY = ('J') | ('J' << 8) | ('Y' << 16),
223  LORC = ('L') | ('O' << 8) | ('R' << 16) | ('C' << 24),
225  TDF = ('T') | ('D' << 8) | ('F' << 16),
227  CHU = ('C') | ('H' << 8) | ('U' << 16),
229  WWV = ('W') | ('W' << 8) | ('V' << 16),
231  WWVH = ('W') | ('W' << 8) | ('V' << 16) | ('H' << 24),
233  NIST = ('N') | ('I' << 8) | ('S' << 16) | ('T' << 24),
235  ACTS = ('A') | ('C' << 8) | ('T' << 16) | ('S' << 24),
237  USNO = ('U') | ('S' << 8) | ('N' << 16) | ('O' << 24),
239  PTB = ('P') | ('T' << 8) | ('B' << 16),
241  MRS = ('M') | ('R' << 8) | ('S' << 16),
243  XFAC = ('X') | ('F' << 8) | ('A' << 16) | ('C' << 24),
245  STEP = ('S') | ('T' << 8) | ('E' << 16) | ('P' << 24),
247  GOOG = ('G') | ('O' << 8) | ('O' << 16) | ('G' << 24),
250  DCFa = ('D') | ('C' << 8) | ('F' << 16) | ('a' << 24),
253  DCFp = ('D') | ('C' << 8) | ('F' << 16) | ('p' << 24),
256  GPSs = ('G') | ('P' << 8) | ('S' << 16) | ('s' << 24),
259  GPSi = ('G') | ('P' << 8) | ('S' << 16) | ('i' << 24),
262  GLNs = ('G') | ('L' << 8) | ('N' << 16) | ('s' << 24),
265  GLNi = ('G') | ('L' << 8) | ('N' << 16) | ('i' << 24),
267  LCL = ('L') | ('C' << 8) | ('L' << 16),
269  LOCL = ('L') | ('O' << 8) | ('C' << 16) | ('L' << 24),
270 
271  // NTPv3
272 
274  DCN = ('D') | ('C' << 8) | ('N' << 16),
276  TSP = ('T') | ('S' << 8) | ('P' << 16),
278  DTS = ('D') | ('T' << 8) | ('S' << 16),
280  ATOM = ('A') | ('T' << 8) | ('O' << 16) | ('M' << 24),
282  VLF = ('V') | ('L' << 8) | ('F' << 16)
283  };
284 
288  enum class KissODeath : uint32_t
289  {
291  ACST = ('A') | ('C' << 8) | ('S' << 16) | ('T' << 24),
293  AUTH = ('A') | ('U' << 8) | ('T' << 16) | ('H' << 24),
295  AUTO = ('A') | ('U' << 8) | ('T' << 16) | ('O' << 24),
297  BCST = ('B') | ('C' << 8) | ('S' << 16) | ('T' << 24),
299  CRYP = ('C') | ('R' << 8) | ('Y' << 16) | ('P' << 24),
301  DENY = ('D') | ('E' << 8) | ('N' << 16) | ('Y' << 24),
303  DROP = ('D') | ('R' << 8) | ('O' << 16) | ('P' << 24),
305  RSTR = ('R') | ('S' << 8) | ('T' << 16) | ('R' << 24),
307  INIT = ('I') | ('N' << 8) | ('I' << 16) | ('T' << 24),
309  MCST = ('M') | ('C' << 8) | ('S' << 16) | ('T' << 24),
311  NKEY = ('N') | ('K' << 8) | ('E' << 16) | ('Y' << 24),
313  RATE = ('R') | ('A' << 8) | ('T' << 16) | ('E' << 24),
316  RMOT = ('R') | ('M' << 8) | ('O' << 16) | ('T' << 24),
318  STEP = ('S') | ('T' << 8) | ('E' << 16) | ('P' << 24),
319  };
320 
328  NtpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
329  : Layer(data, dataLen, prevLayer, packet, NTP)
330  {}
331 
336 
341 
346 
350  uint8_t getVersion() const;
351 
355  void setVersion(uint8_t val);
356 
360  Mode getMode() const;
361 
365  std::string getModeString() const;
366 
370  void setMode(Mode val);
371 
375  uint8_t getStratum() const;
376 
380  void setStratum(uint8_t val);
381 
385  int8_t getPollInterval() const;
386 
391  void setPollInterval(int8_t val);
392 
396  double getPollIntervalInSecs() const;
397 
401  int8_t getPrecision() const;
402 
407  void setPrecision(int8_t val);
408 
412  double getPrecisionInSecs() const;
413 
417  uint32_t getRootDelay() const;
418 
423  void setRootDelay(uint32_t val);
424 
428  double getRootDelayInSecs() const;
429 
434  void setRootDelayInSecs(double val);
435 
439  uint32_t getRootDispersion() const;
440 
445  void setRootDispersion(uint32_t val);
446 
450  double getRootDispersionInSecs() const;
451 
456  void setRootDispersionInSecs(double val);
457 
461  uint32_t getReferenceIdentifier() const;
462 
468 
474 
480 
485  std::string getReferenceIdentifierString() const;
486 
490  uint64_t getReferenceTimestamp() const;
491 
496  void setReferenceTimestamp(uint64_t val);
497 
502 
507  void setReferenceTimestampInSecs(double val);
508 
513 
517  uint64_t getOriginTimestamp() const;
518 
523  void setOriginTimestamp(uint64_t val);
524 
528  double getOriginTimestampInSecs() const;
529 
534  void setOriginTimestampInSecs(double val);
535 
540 
544  uint64_t getReceiveTimestamp() const;
545 
550  void setReceiveTimestamp(uint64_t val);
551 
556 
561  void setReceiveTimestampInSecs(double val);
562 
567 
571  uint64_t getTransmitTimestamp() const;
572 
577  void setTransmitTimestamp(uint64_t val);
578 
583 
588  void setTransmitTimestampInSecs(double val);
589 
594 
599  uint32_t getKeyID() const;
600 
604  std::string getDigest() const;
605 
612  static double convertFromShortFormat(const uint32_t val);
613 
620  static double convertFromTimestampFormat(const uint64_t val);
621 
628  static uint32_t convertToShortFormat(const double val);
629 
636  static uint64_t convertToTimestampFormat(const double val);
637 
643  static std::string convertToIsoFormat(const double timestamp);
644 
650  static std::string convertToIsoFormat(const uint64_t timestampInNTPformat);
651 
658  static bool isDataValid(const uint8_t* data, size_t dataSize);
659 
664  static bool isNTPPort(uint16_t port)
665  {
666  return port == 123;
667  }
668 
669  // overridden methods
670 
673  {}
674 
678  size_t getHeaderLen() const
679  {
680  return m_DataLen;
681  }
682 
685  {}
686 
691  {
693  }
694 
698  std::string toString() const;
699  };
700 
701 } // namespace pcpp
Definition: IpAddress.h:32
Definition: Layer.h:70
The NTP packet consists of an integral number of 32-bit (4 octet) words in network byte order....
Definition: NtpLayer.h:74
double getTransmitTimestampInSecs() const
LeapIndicator
Definition: NtpLayer.h:160
@ Last61Secs
Last minute of the day has 61 seconds.
Definition: NtpLayer.h:164
@ Last59Secs
Last minute of the day has 59 seconds.
Definition: NtpLayer.h:166
@ NoWarning
Normal, no leap second.
Definition: NtpLayer.h:162
@ Unknown
Unknown (clock unsynchronized)
Definition: NtpLayer.h:168
void setTransmitTimestampInSecs(double val)
uint8_t getStratum() const
std::string getReceiveTimestampAsString()
void setRootDispersion(uint32_t val)
void setVersion(uint8_t val)
std::string getReferenceTimestampAsString()
void setReceiveTimestampInSecs(double val)
void setRootDelayInSecs(double val)
void setStratum(uint8_t val)
static std::string convertToIsoFormat(const uint64_t timestampInNTPformat)
uint64_t getOriginTimestamp() const
uint64_t getReferenceTimestamp() const
size_t getHeaderLen() const
Definition: NtpLayer.h:678
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:199
@ 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.)
void parseNextLayer()
Parses the next layer. NTP is the always last so does nothing for this layer.
Definition: NtpLayer.h:672
Mode getMode() const
NtpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: NtpLayer.h:328
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
Definition: NtpLayer.h:289
@ 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.
std::string getDigest() const
void setReceiveTimestamp(uint64_t val)
static bool isNTPPort(uint16_t port)
Definition: NtpLayer.h:664
void setPollInterval(int8_t val)
LeapIndicator getLeapIndicator() const
int8_t getPrecision() const
uint32_t getReferenceIdentifier() const
void setReferenceIdentifier(KissODeath val)
double getRootDispersionInSecs() const
void setReferenceIdentifier(ClockSource val)
void computeCalculateFields()
Does nothing for this layer.
Definition: NtpLayer.h:684
uint64_t getTransmitTimestamp() const
void setMode(Mode val)
Mode
Definition: NtpLayer.h:175
@ SymActive
Symmetrically active.
Definition: NtpLayer.h:179
@ Client
Client mode.
Definition: NtpLayer.h:183
@ Control
NTP control messages.
Definition: NtpLayer.h:189
@ Broadcast
Broadcasting mode.
Definition: NtpLayer.h:187
@ SymPassive
Symmetrically passive.
Definition: NtpLayer.h:181
@ PrivateUse
Reserved for private use.
Definition: NtpLayer.h:191
@ Server
Server mode.
Definition: NtpLayer.h:185
@ Reserved
Reserved variable.
Definition: NtpLayer.h:177
OsiModelLayer getOsiModelLayer() const
Definition: NtpLayer.h:690
double getPrecisionInSecs() const
double getRootDelayInSecs() const
void setOriginTimestampInSecs(double val)
void setReferenceTimestamp(uint64_t val)
void setLeapIndicator(LeapIndicator val)
uint8_t getVersion() const
std::string getTransmitTimestampAsString()
static std::string convertToIsoFormat(const double timestamp)
uint32_t getRootDispersion() const
std::string toString() const
void setPrecision(int8_t val)
static double convertFromShortFormat(const uint32_t val)
uint32_t getRootDelay() const
Definition: Packet.h:27
The main namespace for the PcapPlusPlus lib.
const ProtocolType NTP
Definition: ProtocolType.h:263
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelApplicationLayer
Definition: ProtocolType.h:368