PcapPlusPlus  22.05
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 
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  DCFa = ('D') | ('C' << 8) | ('F' << 16) | ('a' << 24),
237  DCFp = ('D') | ('C' << 8) | ('F' << 16) | ('p' << 24),
239  GPSs = ('G') | ('P' << 8) | ('S' << 16) | ('s' << 24),
241  GPSi = ('G') | ('P' << 8) | ('S' << 16) | ('i' << 24),
243  GLNs = ('G') | ('L' << 8) | ('N' << 16) | ('s' << 24),
245  GLNi = ('G') | ('L' << 8) | ('N' << 16) | ('i' << 24),
247  LCL = ('L') | ('C' << 8) | ('L' << 16),
249  LOCL = ('L') | ('O' << 8) | ('C' << 16) | ('L' << 24),
250 
251  // NTPv3
252 
254  DCN = ('D') | ('C' << 8) | ('N' << 16),
256  TSP = ('T') | ('S' << 8) | ('P' << 16),
258  DTS = ('D') | ('T' << 8) | ('S' << 16),
260  ATOM = ('A') | ('T' << 8) | ('O' << 16) | ('M' << 24),
262  VLF = ('V') | ('L' << 8) | ('F' << 16)
263 
264  };
265 
273  NtpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = NTP; }
274 
278  NtpLayer();
279 
284 
289 
293  uint8_t getVersion() const;
294 
298  void setVersion(uint8_t val);
299 
303  Mode getMode() const;
304 
308  std::string getModeString() const;
309 
313  void setMode(Mode val);
314 
318  uint8_t getStratum() const;
319 
323  void setStratum(uint8_t val);
324 
328  int8_t getPollInterval() const;
329 
334  void setPollInterval(int8_t val);
335 
339  double getPollIntervalInSecs() const;
340 
344  int8_t getPrecision() const;
345 
350  void setPrecision(int8_t val);
351 
355  double getPrecisionInSecs() const;
356 
360  uint32_t getRootDelay() const;
361 
366  void setRootDelay(uint32_t val);
367 
371  double getRootDelayInSecs() const;
372 
377  void setRootDelayInSecs(double val);
378 
382  uint32_t getRootDispersion() const;
383 
388  void setRootDispersion(uint32_t val);
389 
393  double getRootDispersionInSecs() const;
394 
399  void setRootDispersionInSecs(double val);
400 
404  uint32_t getReferenceIdentifier() const;
405 
411 
417 
422  std::string getReferenceIdentifierString() const;
423 
427  uint64_t getReferenceTimestamp() const;
428 
433  void setReferenceTimestamp(uint64_t val);
434 
438  double getReferenceTimestampInSecs() const;
439 
444  void setReferenceTimestampInSecs(double val);
445 
449  std::string getReferenceTimestampAsString();
450 
454  uint64_t getOriginTimestamp() const;
455 
460  void setOriginTimestamp(uint64_t val);
461 
465  double getOriginTimestampInSecs() const;
466 
471  void setOriginTimestampInSecs(double val);
472 
476  std::string getOriginTimestampAsString();
477 
481  uint64_t getReceiveTimestamp() const;
482 
487  void setReceiveTimestamp(uint64_t val);
488 
492  double getReceiveTimestampInSecs() const;
493 
498  void setReceiveTimestampInSecs(double val);
499 
503  std::string getReceiveTimestampAsString();
504 
508  uint64_t getTransmitTimestamp() const;
509 
514  void setTransmitTimestamp(uint64_t val);
515 
519  double getTransmitTimestampInSecs() const;
520 
525  void setTransmitTimestampInSecs(double val);
526 
530  std::string getTransmitTimestampAsString();
531 
535  uint32_t getKeyID() const;
536 
540  std::string getDigest() const;
541 
548  static double convertFromShortFormat(const uint32_t val);
549 
556  static double convertFromTimestampFormat(const uint64_t val);
557 
564  static uint32_t convertToShortFormat(const double val);
565 
572  static uint64_t convertToTimestampFormat(const double val);
573 
579  static std::string convertToIsoFormat(const double timestamp);
580 
586  static std::string convertToIsoFormat(const uint64_t timestampInNTPformat);
587 
594  static bool isDataValid(const uint8_t *data, size_t dataSize);
595 
600  static bool isNTPPort(uint16_t port) { return port == 123; }
601 
602  // overridden methods
603 
605  void parseNextLayer() {}
606 
610  size_t getHeaderLen() const { return m_DataLen; }
611 
614 
619 
623  std::string toString() const;
624  };
625 
626 } // namespace pcpp
627 
628 #endif /* PACKETPP_NTP_LAYER */
HF Radio CHU Ottawa, Ontario.
Definition: NtpLayer.h:221
size_t getHeaderLen() const
Definition: NtpLayer.h:610
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:263
NIST telephone modem.
Definition: NtpLayer.h:227
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:605
void setReferenceIdentifier(IPv4Address val)
void setRootDispersionInSecs(double val)
Last minute of the day has 59 seconds.
Definition: NtpLayer.h:160
uint8_t getStratum() const
Definition: ProtocolType.h:278
LeapIndicator getLeapIndicator() const
Inter-Range Instrumentation Group.
Definition: NtpLayer.h:205
uint32_t getRootDelay() const
double getRootDelayInSecs() const
Definition: Layer.h:70
Meinberg GPS/GLONASS (with interrupt based access) (Ref: https://www.meinbergglobal.com/english/info/ntp-refid.htm)
Definition: NtpLayer.h:245
NTP control messages.
Definition: NtpLayer.h:183
MF Radio Allouis, FR 162 kHz.
Definition: NtpLayer.h:219
static std::string convertToIsoFormat(const double timestamp)
HF Radio WWVH Kauai, HI.
Definition: NtpLayer.h:225
Definition: Packet.h:26
void setPrecision(int8_t val)
double getRootDispersionInSecs() const
std::string getOriginTimestampAsString()
LF Radio MSF Anthorn, UK 60 kHz.
Definition: NtpLayer.h:213
USNO telephone modem.
Definition: NtpLayer.h:231
void setOriginTimestamp(uint64_t val)
LF Radio DCF77 Mainflingen, DE 77.5 kHz.
Definition: NtpLayer.h:209
NtpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: NtpLayer.h:273
Global Position System.
Definition: NtpLayer.h:199
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)
HF Radio WWV Ft. Collins, CO.
Definition: NtpLayer.h:223
void setLeapIndicator(LeapIndicator val)
Last minute of the day has 61 seconds.
Definition: NtpLayer.h:158
std::string getReferenceIdentifierString() const
std::string getReferenceTimestampAsString()
Meinberg DCF77 with phase modulation)/pseudo random phase modulation (Ref: https://www.meinbergglobal.com/english/info/ntp-refid.htm)
Definition: NtpLayer.h:237
Generic pulse-per-second.
Definition: NtpLayer.h:203
Mode
Definition: NtpLayer.h:168
void setReferenceTimestamp(uint64_t val)
void setStratum(uint8_t val)
uint64_t getReferenceTimestamp() const
std::string getTransmitTimestampAsString()
ClockSource
Definition: NtpLayer.h:192
std::string getModeString() const
double getReferenceTimestampInSecs() const
Definition: IpAddress.h:27
Meinberg Undisciplined local clock (Ref: https://www.meinbergglobal.com/english/info/ntp-refid.htm)
Definition: NtpLayer.h:249
LF Radio WWVB Ft. Collins, CO 60 kHz.
Definition: NtpLayer.h:207
Meinberg GPS (with shared memory access) (Ref: https://www.meinbergglobal.com/english/info/ntp-refid...
Definition: NtpLayer.h:239
VLF radio (OMEGA, etc.)
Definition: NtpLayer.h:262
European telephone modem.
Definition: NtpLayer.h:233
DCN routing protocol.
Definition: NtpLayer.h:254
double getTransmitTimestampInSecs() const
Meinberg DCF77 with amplitude modulation (Ref: https://www.meinbergglobal.com/english/info/ntp-refid...
Definition: NtpLayer.h:235
uint64_t getReceiveTimestamp() const
uint32_t getKeyID() const
LeapIndicator
Definition: NtpLayer.h:153
Meinberg GPS (with interrupt based access) (Ref: https://www.meinbergglobal.com/english/info/ntp-refi...
Definition: NtpLayer.h:241
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)
NIST telephone modem.
Definition: NtpLayer.h:229
void setReferenceTimestampInSecs(double val)
Meinberg GPS/GLONASS (with shared memory access) (Ref: https://www.meinbergglobal.com/english/info/ntp-refid.htm)
Definition: NtpLayer.h:243
Digital Time Service.
Definition: NtpLayer.h:258
Meinberg Undisciplined local clock (Ref: https://www.meinbergglobal.com/english/info/ntp-refid.htm)
Definition: NtpLayer.h:247
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)
MF Radio LORAN C station, 100 kHz.
Definition: NtpLayer.h:217
void setRootDelay(uint32_t val)
OsiModelLayer getOsiModelLayer() const
Definition: NtpLayer.h:618
std::string toString() const
double getPrecisionInSecs() const
Server mode.
Definition: NtpLayer.h:179
LF Radio HBG Prangins, HB 75 kHz.
Definition: NtpLayer.h:211
Reserved for private use.
Definition: NtpLayer.h:185
uint64_t getTransmitTimestamp() const
Geosynchronous Orbit Environment Satellite.
Definition: NtpLayer.h:197
const ProtocolType NTP
Definition: ProtocolType.h:258
Atomic clock (calibrated)
Definition: NtpLayer.h:260
static bool isNTPPort(uint16_t port)
Definition: NtpLayer.h:600
Normal, no leap second.
Definition: NtpLayer.h:156
LF Radio JJY Fukushima, JP 40 kHz, Saga, JP 60 kHz.
Definition: NtpLayer.h:215
static double convertFromShortFormat(const uint32_t val)
void computeCalculateFields()
Does nothing for this layer.
Definition: NtpLayer.h:613
TSP time protocol.
Definition: NtpLayer.h:256
static bool isDataValid(const uint8_t *data, size_t dataSize)
Client mode.
Definition: NtpLayer.h:177
Galileo Positioning System.
Definition: NtpLayer.h:201
Mode getMode() const
static uint64_t convertToTimestampFormat(const double val)
double getOriginTimestampInSecs() const
int8_t getPrecision() const
uint8_t getVersion() const
void setTransmitTimestampInSecs(double val)