PcapPlusPlus  23.09
NtpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_NTP_LAYER
2 #define PACKETPP_NTP_LAYER
3 
4 #include "Logger.h"
5 #include "Layer.h"
6 #include "IpAddress.h"
7 
9 
14 namespace pcpp
15 {
73  class NtpLayer : public Layer
74  {
75  private:
76 #pragma pack(push, 1)
77  struct ntp_header
78  {
79 #if (BYTE_ORDER == LITTLE_ENDIAN)
80  uint8_t mode:3,
83  version:3,
85  leapIndicator:2;
86 #else
87  uint8_t leapIndicator:2,
90  version:3,
92  mode:3;
93 #endif
94  uint8_t stratum;
97  int8_t pollInterval,
99  precision;
101  uint32_t rootDelay,
103  rootDispersion,
105  referenceIdentifier;
107  uint64_t referenceTimestamp,
109  originTimestamp,
111  receiveTimestamp,
113  transmitTimestamp;
114  };
115 #pragma pack(pop)
116 
117 #pragma pack(push, 1)
118  struct ntp_v3_auth
119  {
121  uint32_t keyID;
123  uint8_t dgst[8]; // 64 bit DES based
124  };
125 #pragma pack(pop)
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 
137 #pragma pack(push, 1)
138  struct ntp_v4_auth_sha1
139  {
141  uint32_t keyID;
143  uint8_t dgst[20];
144  };
145 #pragma pack(pop)
146 
147  ntp_header *getNtpHeader() const { return (ntp_header *)m_Data; }
148 
149  public:
154  {
163  };
164 
168  enum Mode
169  {
171  Reserved = 0,
186  };
187 
192  enum class ClockSource : uint32_t
193  {
194  // NTPv4
195 
197  GOES = ('G') | ('O' << 8) | ('E' << 16) | ('S' << 24),
199  GPS = ('G') | ('P' << 8) | ('S' << 16),
201  GAL = ('G') | ('A' << 8) | ('L' << 16),
203  PPS = ('P') | ('P' << 8) | ('S' << 16),
205  IRIG = ('I') | ('R' << 8) | ('I' << 16) | ('G' << 24),
207  WWVB = ('W') | ('W' << 8) | ('V' << 16) | ('B' << 24),
209  DCF = ('D') | ('C' << 8) | ('F' << 16),
211  HBG = ('H') | ('B' << 8) | ('G' << 16),
213  MSF = ('M') | ('S' << 8) | ('F' << 16),
215  JJY = ('J') | ('J' << 8) | ('Y' << 16),
217  LORC = ('L') | ('O' << 8) | ('R' << 16) | ('C' << 24),
219  TDF = ('T') | ('D' << 8) | ('F' << 16),
221  CHU = ('C') | ('H' << 8) | ('U' << 16),
223  WWV = ('W') | ('W' << 8) | ('V' << 16),
225  WWVH = ('W') | ('W' << 8) | ('V' << 16) | ('H' << 24),
227  NIST = ('N') | ('I' << 8) | ('S' << 16) | ('T' << 24),
229  ACTS = ('A') | ('C' << 8) | ('T' << 16) | ('S' << 24),
231  USNO = ('U') | ('S' << 8) | ('N' << 16) | ('O' << 24),
233  PTB = ('P') | ('T' << 8) | ('B' << 16),
235  MRS = ('M') | ('R' << 8) | ('S' << 16),
237  XFAC = ('X') | ('F' << 8) | ('A' << 16) | ('C' << 24),
239  STEP = ('S') | ('T' << 8) | ('E' << 16) | ('P' << 24),
241  GOOG = ('G') | ('O' << 8) | ('O' << 16) | ('G' << 24),
243  DCFa = ('D') | ('C' << 8) | ('F' << 16) | ('a' << 24),
245  DCFp = ('D') | ('C' << 8) | ('F' << 16) | ('p' << 24),
247  GPSs = ('G') | ('P' << 8) | ('S' << 16) | ('s' << 24),
249  GPSi = ('G') | ('P' << 8) | ('S' << 16) | ('i' << 24),
251  GLNs = ('G') | ('L' << 8) | ('N' << 16) | ('s' << 24),
253  GLNi = ('G') | ('L' << 8) | ('N' << 16) | ('i' << 24),
255  LCL = ('L') | ('C' << 8) | ('L' << 16),
257  LOCL = ('L') | ('O' << 8) | ('C' << 16) | ('L' << 24),
258 
259  // NTPv3
260 
262  DCN = ('D') | ('C' << 8) | ('N' << 16),
264  TSP = ('T') | ('S' << 8) | ('P' << 16),
266  DTS = ('D') | ('T' << 8) | ('S' << 16),
268  ATOM = ('A') | ('T' << 8) | ('O' << 16) | ('M' << 24),
270  VLF = ('V') | ('L' << 8) | ('F' << 16)
271 
272  };
273 
277  enum class KissODeath : uint32_t
278  {
280  ACST = ('A') | ('C' << 8) | ('S' << 16) | ('T' << 24),
282  AUTH = ('A') | ('U' << 8) | ('T' << 16) | ('H' << 24),
284  AUTO = ('A') | ('U' << 8) | ('T' << 16) | ('O' << 24),
286  BCST = ('B') | ('C' << 8) | ('S' << 16) | ('T' << 24),
288  CRYP = ('C') | ('R' << 8) | ('Y' << 16) | ('P' << 24),
290  DENY = ('D') | ('E' << 8) | ('N' << 16) | ('Y' << 24),
292  DROP = ('D') | ('R' << 8) | ('O' << 16) | ('P' << 24),
294  RSTR = ('R') | ('S' << 8) | ('T' << 16) | ('R' << 24),
296  INIT = ('I') | ('N' << 8) | ('I' << 16) | ('T' << 24),
298  MCST = ('M') | ('C' << 8) | ('S' << 16) | ('T' << 24),
300  NKEY = ('N') | ('K' << 8) | ('E' << 16) | ('Y' << 24),
302  RATE = ('R') | ('A' << 8) | ('T' << 16) | ('E' << 24),
304  RMOT = ('R') | ('M' << 8) | ('O' << 16) | ('T' << 24),
306  STEP = ('S') | ('T' << 8) | ('E' << 16) | ('P' << 24),
307  };
308 
316  NtpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = NTP; }
317 
321  NtpLayer();
322 
327 
332 
336  uint8_t getVersion() const;
337 
341  void setVersion(uint8_t val);
342 
346  Mode getMode() const;
347 
351  std::string getModeString() const;
352 
356  void setMode(Mode val);
357 
361  uint8_t getStratum() const;
362 
366  void setStratum(uint8_t val);
367 
371  int8_t getPollInterval() const;
372 
377  void setPollInterval(int8_t val);
378 
382  double getPollIntervalInSecs() const;
383 
387  int8_t getPrecision() const;
388 
393  void setPrecision(int8_t val);
394 
398  double getPrecisionInSecs() const;
399 
403  uint32_t getRootDelay() const;
404 
409  void setRootDelay(uint32_t val);
410 
414  double getRootDelayInSecs() const;
415 
420  void setRootDelayInSecs(double val);
421 
425  uint32_t getRootDispersion() const;
426 
431  void setRootDispersion(uint32_t val);
432 
436  double getRootDispersionInSecs() const;
437 
442  void setRootDispersionInSecs(double val);
443 
447  uint32_t getReferenceIdentifier() const;
448 
454 
460 
466 
471  std::string getReferenceIdentifierString() const;
472 
476  uint64_t getReferenceTimestamp() const;
477 
482  void setReferenceTimestamp(uint64_t val);
483 
487  double getReferenceTimestampInSecs() const;
488 
493  void setReferenceTimestampInSecs(double val);
494 
498  std::string getReferenceTimestampAsString();
499 
503  uint64_t getOriginTimestamp() const;
504 
509  void setOriginTimestamp(uint64_t val);
510 
514  double getOriginTimestampInSecs() const;
515 
520  void setOriginTimestampInSecs(double val);
521 
525  std::string getOriginTimestampAsString();
526 
530  uint64_t getReceiveTimestamp() const;
531 
536  void setReceiveTimestamp(uint64_t val);
537 
541  double getReceiveTimestampInSecs() const;
542 
547  void setReceiveTimestampInSecs(double val);
548 
552  std::string getReceiveTimestampAsString();
553 
557  uint64_t getTransmitTimestamp() const;
558 
563  void setTransmitTimestamp(uint64_t val);
564 
568  double getTransmitTimestampInSecs() const;
569 
574  void setTransmitTimestampInSecs(double val);
575 
579  std::string getTransmitTimestampAsString();
580 
584  uint32_t getKeyID() const;
585 
589  std::string getDigest() const;
590 
597  static double convertFromShortFormat(const uint32_t val);
598 
605  static double convertFromTimestampFormat(const uint64_t val);
606 
613  static uint32_t convertToShortFormat(const double val);
614 
621  static uint64_t convertToTimestampFormat(const double val);
622 
628  static std::string convertToIsoFormat(const double timestamp);
629 
635  static std::string convertToIsoFormat(const uint64_t timestampInNTPformat);
636 
643  static bool isDataValid(const uint8_t *data, size_t dataSize);
644 
649  static bool isNTPPort(uint16_t port) { return port == 123; }
650 
651  // overridden methods
652 
654  void parseNextLayer() {}
655 
659  size_t getHeaderLen() const { return m_DataLen; }
660 
663 
668 
672  std::string toString() const;
673  };
674 
675 } // namespace pcpp
676 
677 #endif /* PACKETPP_NTP_LAYER */
size_t getHeaderLen() const
Definition: NtpLayer.h:659
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
int8_t getPollInterval() const
Unknown (clock unsynchronized)
Definition: NtpLayer.h:162
double getPollIntervalInSecs() const
void setOriginTimestampInSecs(double val)
void parseNextLayer()
Parses the next layer. NTP is the always last so does nothing for this layer.
Definition: NtpLayer.h:654
void setReferenceIdentifier(IPv4Address val)
KissODeath
Definition: NtpLayer.h:277
void setRootDispersionInSecs(double val)
Last minute of the day has 59 seconds.
Definition: NtpLayer.h:160
uint8_t getStratum() const
Definition: ProtocolType.h:348
LeapIndicator getLeapIndicator() const
uint32_t getRootDelay() const
double getRootDelayInSecs() const
Definition: Layer.h:70
NTP control messages.
Definition: NtpLayer.h:183
static std::string convertToIsoFormat(const double timestamp)
Definition: Packet.h:26
void setPrecision(int8_t val)
double getRootDispersionInSecs() const
std::string getOriginTimestampAsString()
void setOriginTimestamp(uint64_t val)
NtpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: NtpLayer.h:316
void setTransmitTimestamp(uint64_t val)
static double convertFromTimestampFormat(const uint64_t val)
The NTP packet consists of an integral number of 32-bit (4 octet) words in network byte order...
Definition: NtpLayer.h:73
static uint32_t convertToShortFormat(const double val)
uint32_t getReferenceIdentifier() const
Broadcasting mode.
Definition: NtpLayer.h:181
uint64_t getOriginTimestamp() const
double getReceiveTimestampInSecs() const
void setPollInterval(int8_t val)
void setLeapIndicator(LeapIndicator val)
Last minute of the day has 61 seconds.
Definition: NtpLayer.h:158
std::string getReferenceIdentifierString() const
std::string getReferenceTimestampAsString()
Mode
Definition: NtpLayer.h:168
void setReferenceTimestamp(uint64_t val)
void setStratum(uint8_t val)
uint64_t getReferenceTimestamp() const
std::string getTransmitTimestampAsString()
std::string getModeString() const
double getReferenceTimestampInSecs() const
Definition: IpAddress.h:42
double getTransmitTimestampInSecs() const
uint64_t getReceiveTimestamp() const
uint32_t getKeyID() const
LeapIndicator
Definition: NtpLayer.h:153
Symmetrically active.
Definition: NtpLayer.h:173
void setVersion(uint8_t val)
void setRootDelayInSecs(double val)
Symmetrically passive.
Definition: NtpLayer.h:175
void setRootDispersion(uint32_t val)
void setReferenceTimestampInSecs(double val)
void setReceiveTimestamp(uint64_t val)
Reserved variable.
Definition: NtpLayer.h:171
void setMode(Mode val)
std::string getDigest() const
std::string getReceiveTimestampAsString()
uint32_t getRootDispersion() const
void setReceiveTimestampInSecs(double val)
void setRootDelay(uint32_t val)
OsiModelLayer getOsiModelLayer() const
Definition: NtpLayer.h:667
std::string toString() const
double getPrecisionInSecs() const
Server mode.
Definition: NtpLayer.h:179
Reserved for private use.
Definition: NtpLayer.h:185
uint64_t getTransmitTimestamp() const
const ProtocolType NTP
Definition: ProtocolType.h:258
static bool isNTPPort(uint16_t port)
Definition: NtpLayer.h:649
Normal, no leap second.
Definition: NtpLayer.h:156
static double convertFromShortFormat(const uint32_t val)
void computeCalculateFields()
Does nothing for this layer.
Definition: NtpLayer.h:662
static bool isDataValid(const uint8_t *data, size_t dataSize)
Client mode.
Definition: NtpLayer.h:177
Mode getMode() const
static uint64_t convertToTimestampFormat(const double val)
double getOriginTimestampInSecs() const
int8_t getPrecision() const
uint8_t getVersion() const
ClockSource
Definition: NtpLayer.h:192
void setTransmitTimestampInSecs(double val)