PcapPlusPlus  Next
IcmpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "IPv4Layer.h"
5 #ifdef _MSC_VER
6 # include <Winsock2.h>
7 #else
8 # include <sys/time.h>
9 #endif
10 #include <vector>
11 
13 
18 namespace pcpp
19 {
20 
25 #pragma pack(push, 1)
26  typedef struct icmphdr
27  {
29  uint8_t type;
31  uint8_t code;
33  uint16_t checksum;
34  } icmphdr;
35 #pragma pack(pop)
36 
41  {
73  ICMP_UNSUPPORTED = 255
74  };
75 
81  {
120  };
121 
126 #pragma pack(push, 1)
127  typedef struct icmp_echo_hdr : icmphdr
128  {
130  uint16_t id;
132  uint16_t sequence;
134  uint64_t timestamp;
135  } icmp_echo_hdr;
136 #pragma pack(pop)
137 
142  typedef struct icmp_echo_request
143  {
147  size_t dataLength;
149  uint8_t* data;
151 
157 
162 #pragma pack(push, 1)
164  {
166  uint16_t id;
168  uint16_t sequence;
176 #pragma pack(pop)
177 
183 
188 #pragma pack(push, 1)
190  {
192  uint16_t unused;
194  uint16_t nextHopMTU;
196 #pragma pack(pop)
197 
202 #pragma pack(push, 1)
203  typedef struct icmp_time_exceeded : icmphdr
204  {
206  uint32_t unused;
208 #pragma pack(pop)
209 
215 
220 #pragma pack(push, 1)
221  typedef struct icmp_param_problem : icmphdr
222  {
225  uint8_t pointer;
227  uint8_t unused1;
229  uint16_t unused2;
231 #pragma pack(pop)
232 
238 
243 #pragma pack(push, 1)
244  typedef struct icmp_redirect : icmphdr
245  {
247  uint32_t gatewayAddress;
248  } icmp_redirect;
249 #pragma pack(pop)
250 
255 #pragma pack(push, 1)
257  {
259  uint32_t routerAddress;
263  uint32_t preferenceLevel;
264 
270  void setRouterAddress(IPv4Address addr, uint32_t preference);
271 
276  {
277  return routerAddress;
278  }
279  };
280 #pragma pack(pop)
281 
286 #pragma pack(push, 1)
288  {
296  uint16_t lifetime;
298 #pragma pack(pop)
299 
305  {
308 
317  };
318 
323 #pragma pack(push, 1)
325  {
327  uint16_t id;
329  uint16_t sequence;
331  uint32_t addressMask;
333 #pragma pack(pop)
334 
340 
345 #pragma pack(push, 1)
346  typedef struct icmp_info_request : icmphdr
347  {
349  uint16_t id;
351  uint16_t sequence;
353 #pragma pack(pop)
354 
360 
365  class IcmpLayer : public Layer
366  {
367  private:
368  icmp_echo_request m_EchoData;
369  mutable icmp_router_advertisement m_RouterAdvData;
370 
371  bool cleanIcmpLayer();
372 
373  bool setEchoData(IcmpMessageType echoType, uint16_t id, uint16_t sequence, uint64_t timestamp,
374  const uint8_t* data, size_t dataLen);
375 
376  bool setIpAndL4Layers(IPv4Layer* ipLayer, Layer* l4Layer);
377 
378  public:
386  // cppcheck-suppress uninitMemberVar
387  IcmpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
388  : Layer(data, dataLen, prevLayer, packet, ICMP)
389  {}
390 
396 
397  ~IcmpLayer() override = default;
398 
405  {
406  return reinterpret_cast<icmphdr*>(m_Data);
407  }
408 
413 
419  {
420  return getMessageType() == type;
421  }
422 
427 
438  icmp_echo_request* setEchoRequestData(uint16_t id, uint16_t sequence, uint64_t timestamp, const uint8_t* data,
439  size_t dataLen);
440 
445 
456  icmp_echo_reply* setEchoReplyData(uint16_t id, uint16_t sequence, uint64_t timestamp, const uint8_t* data,
457  size_t dataLen);
458 
463 
472  icmp_timestamp_request* setTimestampRequestData(uint16_t id, uint16_t sequence, timeval originateTimestamp);
473 
478 
489  icmp_timestamp_reply* setTimestampReplyData(uint16_t id, uint16_t sequence, timeval originateTimestamp,
490  timeval receiveTimestamp, timeval transmitTimestamp);
491 
498 
512  IPv4Layer* ipHeader, Layer* l4Header);
513 
520 
532 
539 
553  icmp_redirect* setRedirectData(uint8_t code, IPv4Address gatewayAddress, IPv4Layer* ipHeader, Layer* l4Header);
554 
560 
572  uint8_t code, uint16_t lifetimeInSeconds,
573  const std::vector<icmp_router_address_structure>& routerAddresses);
574 
580 
588 
595 
608  icmp_time_exceeded* setTimeExceededData(uint8_t code, IPv4Layer* ipHeader, Layer* l4Header);
609 
614 
629  icmp_param_problem* setParamProblemData(uint8_t code, uint8_t errorOctetPointer, IPv4Layer* ipHeader,
630  Layer* l4Header);
631 
637 
646  icmp_address_mask_request* setAddressMaskRequestData(uint16_t id, uint16_t sequence, IPv4Address mask);
647 
652 
661  icmp_address_mask_reply* setAddressMaskReplyData(uint16_t id, uint16_t sequence, IPv4Address mask);
662 
668 
676  icmp_info_request* setInfoRequestData(uint16_t id, uint16_t sequence);
677 
683 
691  icmp_info_reply* setInfoReplyData(uint16_t id, uint16_t sequence);
692 
699  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
700 
701  // implement abstract methods
702 
708  void parseNextLayer() override;
709 
715  size_t getHeaderLen() const override;
716 
720  void computeCalculateFields() override;
721 
722  std::string toString() const override;
723 
725  {
726  return OsiModelNetworkLayer;
727  }
728  };
729 
730  // implementation of inline methods
731 
732  bool IcmpLayer::isDataValid(const uint8_t* data, size_t dataLen)
733  {
734  if (dataLen < sizeof(icmphdr))
735  return false;
736 
737  uint8_t type = data[0];
738 
739  // ICMP_ECHO_REQUEST, ICMP_ECHO_REPLY, ICMP_ROUTER_SOL, ICMP_INFO_REQUEST, ICMP_INFO_REPLY
740  if (type == 8 || type == 0 || type == 10 || type == 15 || type == 16)
741  return true;
742 
743  // ICMP_TIMESTAMP_REQUEST, ICMP_TIMESTAMP_REPLY
744  if (type == 13 || type == 14)
745  return dataLen >= sizeof(icmp_timestamp_request);
746 
747  // ICMP_ADDRESS_MASK_REPLY, ICMP_ADDRESS_MASK_REQUEST
748  if (type == 17 || type == 18)
749  return dataLen >= sizeof(icmp_address_mask_request);
750 
751  // ICMP_DEST_UNREACHABLE
752  if (type == 3)
753  return dataLen >= sizeof(icmp_destination_unreachable);
754 
755  // ICMP_REDIRECT
756  if (type == 5)
757  return dataLen >= sizeof(icmp_redirect);
758 
759  // ICMP_TIME_EXCEEDED, ICMP_SOURCE_QUENCH
760  if (type == 4 || type == 11)
761  return dataLen >= sizeof(icmp_time_exceeded);
762 
763  // ICMP_PARAM_PROBLEM
764  if (type == 12)
765  return dataLen >= sizeof(icmp_param_problem);
766 
767  // ICMP_ROUTER_ADV
768  if (type == 9)
769  return dataLen >= sizeof(icmp_router_advertisement_hdr);
770 
771  return false;
772  }
773 
774 } // namespace pcpp
Definition: IpAddress.h:32
Definition: IPv4Layer.h:452
Definition: IcmpLayer.h:366
icmp_router_solicitation * getRouterSolicitationData()
icmp_address_mask_request * setAddressMaskRequestData(uint16_t id, uint16_t sequence, IPv4Address mask)
icmp_echo_request * getEchoRequestData()
icmp_info_reply * getInfoReplyData()
icmp_info_request * setInfoRequestData(uint16_t id, uint16_t sequence)
icmp_echo_reply * getEchoReplyData()
size_t getHeaderLen() const override
icmp_source_quench * getSourceQuenchdata()
icmp_echo_reply * setEchoReplyData(uint16_t id, uint16_t sequence, uint64_t timestamp, const uint8_t *data, size_t dataLen)
icmp_echo_request * setEchoRequestData(uint16_t id, uint16_t sequence, uint64_t timestamp, const uint8_t *data, size_t dataLen)
IcmpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: IcmpLayer.h:387
icmp_router_solicitation * setRouterSolicitationData()
icmp_address_mask_reply * setAddressMaskReplyData(uint16_t id, uint16_t sequence, IPv4Address mask)
icmp_router_advertisement * setRouterAdvertisementData(uint8_t code, uint16_t lifetimeInSeconds, const std::vector< icmp_router_address_structure > &routerAddresses)
icmp_redirect * getRedirectData()
icmp_address_mask_request * getAddressMaskRequestData()
icmp_address_mask_reply * getAddressMaskReplyData()
icmp_timestamp_request * getTimestampRequestData()
bool isMessageOfType(IcmpMessageType type) const
Definition: IcmpLayer.h:418
icmp_redirect * setRedirectData(uint8_t code, IPv4Address gatewayAddress, IPv4Layer *ipHeader, Layer *l4Header)
icmp_timestamp_reply * getTimestampReplyData()
icmp_destination_unreachable * setDestUnreachableData(IcmpDestUnreachableCodes code, uint16_t nextHopMTU, IPv4Layer *ipHeader, Layer *l4Header)
icmphdr * getIcmpHeader() const
Definition: IcmpLayer.h:404
icmp_timestamp_request * setTimestampRequestData(uint16_t id, uint16_t sequence, timeval originateTimestamp)
icmp_router_advertisement * getRouterAdvertisementData() const
icmp_info_request * getInfoRequestData()
icmp_destination_unreachable * getDestUnreachableData()
icmp_time_exceeded * setTimeExceededData(uint8_t code, IPv4Layer *ipHeader, Layer *l4Header)
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IcmpLayer.h:732
icmp_param_problem * setParamProblemData(uint8_t code, uint8_t errorOctetPointer, IPv4Layer *ipHeader, Layer *l4Header)
void parseNextLayer() override
icmp_time_exceeded * getTimeExceededData()
icmp_param_problem * getParamProblemData()
void computeCalculateFields() override
icmp_timestamp_reply * setTimestampReplyData(uint16_t id, uint16_t sequence, timeval originateTimestamp, timeval receiveTimestamp, timeval transmitTimestamp)
icmp_info_reply * setInfoReplyData(uint16_t id, uint16_t sequence)
std::string toString() const override
IcmpMessageType getMessageType() const
icmp_source_quench * setSourceQuenchdata(IPv4Layer *ipHeader, Layer *l4Header)
OsiModelLayer getOsiModelLayer() const override
Definition: IcmpLayer.h:724
Definition: Layer.h:69
Definition: Packet.h:27
The main namespace for the PcapPlusPlus lib.
icmp_address_mask_request icmp_address_mask_reply
Definition: IcmpLayer.h:339
icmphdr icmp_router_solicitation
Definition: IcmpLayer.h:237
icmp_info_request icmp_info_reply
Definition: IcmpLayer.h:359
IcmpMessageType
Definition: IcmpLayer.h:41
@ ICMP_DEST_UNREACHABLE
Definition: IcmpLayer.h:45
@ ICMP_TIMESTAMP_REPLY
Definition: IcmpLayer.h:63
@ ICMP_TIME_EXCEEDED
Definition: IcmpLayer.h:57
@ ICMP_ECHO_REQUEST
Definition: IcmpLayer.h:51
@ ICMP_TIMESTAMP_REQUEST
Definition: IcmpLayer.h:61
@ ICMP_UNSUPPORTED
Definition: IcmpLayer.h:73
@ ICMP_PARAM_PROBLEM
Definition: IcmpLayer.h:59
@ ICMP_ADDRESS_MASK_REPLY
Definition: IcmpLayer.h:71
@ ICMP_ROUTER_SOL
Definition: IcmpLayer.h:55
@ ICMP_ECHO_REPLY
Definition: IcmpLayer.h:43
@ ICMP_INFO_REQUEST
Definition: IcmpLayer.h:65
@ ICMP_ADDRESS_MASK_REQUEST
Definition: IcmpLayer.h:69
@ ICMP_SOURCE_QUENCH
Definition: IcmpLayer.h:47
@ ICMP_REDIRECT
Definition: IcmpLayer.h:49
@ ICMP_ROUTER_ADV
Definition: IcmpLayer.h:53
@ ICMP_INFO_REPLY
Definition: IcmpLayer.h:67
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelNetworkLayer
Definition: ProtocolType.h:370
icmp_timestamp_request icmp_timestamp_reply
Definition: IcmpLayer.h:182
icmp_time_exceeded icmp_source_quench
Definition: IcmpLayer.h:214
IcmpDestUnreachableCodes
Definition: IcmpLayer.h:81
@ IcmpDestinationNetworkProhibited
Definition: IcmpLayer.h:101
@ IcmpSourceHostIsolated
Definition: IcmpLayer.h:99
@ IcmpPrecedenceCutoff
Definition: IcmpLayer.h:119
@ IcmpHostPrecedenceViolation
Definition: IcmpLayer.h:115
@ IcmpPortUnreachable
Definition: IcmpLayer.h:89
@ IcmpSourceRouteFailed
Definition: IcmpLayer.h:93
@ IcmpHostUnreachableForTypeOfService
Definition: IcmpLayer.h:107
@ IcmpDestinationHostProhibited
Definition: IcmpLayer.h:103
@ IcmpDestinationHostUnknown
Definition: IcmpLayer.h:97
@ IcmpProtocolUnreachable
Definition: IcmpLayer.h:87
@ IcmpCommunicationProhibited
Definition: IcmpLayer.h:111
@ IcmpDestinationNetworkUnknown
Definition: IcmpLayer.h:95
@ IcmpNetworkUnreachable
Definition: IcmpLayer.h:83
@ IcmpNetworkUnreachableForTypeOfService
Definition: IcmpLayer.h:105
@ IcmpHostUnreachable
Definition: IcmpLayer.h:85
@ IcmpDatagramTooBig
Definition: IcmpLayer.h:91
const ProtocolType ICMP
Definition: ProtocolType.h:88
icmp_echo_request icmp_echo_reply
Definition: IcmpLayer.h:156
Definition: IcmpLayer.h:325
uint16_t id
Definition: IcmpLayer.h:327
uint16_t sequence
Definition: IcmpLayer.h:329
uint32_t addressMask
Definition: IcmpLayer.h:331
Definition: IcmpLayer.h:190
uint16_t unused
Definition: IcmpLayer.h:192
uint16_t nextHopMTU
Definition: IcmpLayer.h:194
Definition: IcmpLayer.h:128
uint64_t timestamp
Definition: IcmpLayer.h:134
uint16_t id
Definition: IcmpLayer.h:130
uint16_t sequence
Definition: IcmpLayer.h:132
Definition: IcmpLayer.h:143
icmp_echo_hdr * header
Definition: IcmpLayer.h:145
size_t dataLength
Definition: IcmpLayer.h:147
uint8_t * data
Definition: IcmpLayer.h:149
Definition: IcmpLayer.h:347
uint16_t sequence
Definition: IcmpLayer.h:351
uint16_t id
Definition: IcmpLayer.h:349
Definition: IcmpLayer.h:222
uint8_t unused1
Definition: IcmpLayer.h:227
uint8_t pointer
Definition: IcmpLayer.h:225
uint16_t unused2
Definition: IcmpLayer.h:229
Definition: IcmpLayer.h:245
uint32_t gatewayAddress
Definition: IcmpLayer.h:247
Definition: IcmpLayer.h:257
void setRouterAddress(IPv4Address addr, uint32_t preference)
uint32_t routerAddress
Definition: IcmpLayer.h:259
IPv4Address getAddress() const
Definition: IcmpLayer.h:275
uint32_t preferenceLevel
Definition: IcmpLayer.h:263
Definition: IcmpLayer.h:288
uint8_t addressEntrySize
Definition: IcmpLayer.h:294
uint16_t lifetime
Definition: IcmpLayer.h:296
uint8_t advertisementCount
Definition: IcmpLayer.h:291
Definition: IcmpLayer.h:305
icmp_router_advertisement_hdr * header
Definition: IcmpLayer.h:307
icmp_router_address_structure * getRouterAddress(int index) const
Definition: IcmpLayer.h:204
uint32_t unused
Definition: IcmpLayer.h:206
Definition: IcmpLayer.h:164
uint16_t id
Definition: IcmpLayer.h:166
uint32_t transmitTimestamp
Definition: IcmpLayer.h:174
uint16_t sequence
Definition: IcmpLayer.h:168
uint32_t receiveTimestamp
Definition: IcmpLayer.h:172
uint32_t originateTimestamp
Definition: IcmpLayer.h:170
Definition: IcmpLayer.h:27
uint8_t code
Definition: IcmpLayer.h:31
uint8_t type
Definition: IcmpLayer.h:29
uint16_t checksum
Definition: IcmpLayer.h:33