PcapPlusPlus  24.09
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  virtual ~IcmpLayer()
398  {}
399 
406  {
407  return (icmphdr*)m_Data;
408  }
409 
414 
420  {
421  return getMessageType() == type;
422  }
423 
428 
439  icmp_echo_request* setEchoRequestData(uint16_t id, uint16_t sequence, uint64_t timestamp, const uint8_t* data,
440  size_t dataLen);
441 
446 
457  icmp_echo_reply* setEchoReplyData(uint16_t id, uint16_t sequence, uint64_t timestamp, const uint8_t* data,
458  size_t dataLen);
459 
464 
473  icmp_timestamp_request* setTimestampRequestData(uint16_t id, uint16_t sequence, timeval originateTimestamp);
474 
479 
490  icmp_timestamp_reply* setTimestampReplyData(uint16_t id, uint16_t sequence, timeval originateTimestamp,
491  timeval receiveTimestamp, timeval transmitTimestamp);
492 
499 
513  IPv4Layer* ipHeader, Layer* l4Header);
514 
521 
533 
540 
554  icmp_redirect* setRedirectData(uint8_t code, IPv4Address gatewayAddress, IPv4Layer* ipHeader, Layer* l4Header);
555 
561 
573  uint8_t code, uint16_t lifetimeInSeconds,
574  const std::vector<icmp_router_address_structure>& routerAddresses);
575 
581 
589 
596 
609  icmp_time_exceeded* setTimeExceededData(uint8_t code, IPv4Layer* ipHeader, Layer* l4Header);
610 
615 
630  icmp_param_problem* setParamProblemData(uint8_t code, uint8_t errorOctetPointer, IPv4Layer* ipHeader,
631  Layer* l4Header);
632 
638 
647  icmp_address_mask_request* setAddressMaskRequestData(uint16_t id, uint16_t sequence, IPv4Address mask);
648 
653 
662  icmp_address_mask_reply* setAddressMaskReplyData(uint16_t id, uint16_t sequence, IPv4Address mask);
663 
669 
677  icmp_info_request* setInfoRequestData(uint16_t id, uint16_t sequence);
678 
684 
692  icmp_info_reply* setInfoReplyData(uint16_t id, uint16_t sequence);
693 
700  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
701 
702  // implement abstract methods
703 
710 
716  size_t getHeaderLen() const;
717 
722 
723  std::string toString() const;
724 
726  {
727  return OsiModelNetworkLayer;
728  }
729  };
730 
731  // implementation of inline methods
732 
733  bool IcmpLayer::isDataValid(const uint8_t* data, size_t dataLen)
734  {
735  if (dataLen < sizeof(icmphdr))
736  return false;
737 
738  uint8_t type = data[0];
739 
740  // ICMP_ECHO_REQUEST, ICMP_ECHO_REPLY, ICMP_ROUTER_SOL, ICMP_INFO_REQUEST, ICMP_INFO_REPLY
741  if (type == 8 || type == 0 || type == 10 || type == 15 || type == 16)
742  return true;
743 
744  // ICMP_TIMESTAMP_REQUEST, ICMP_TIMESTAMP_REPLY
745  if (type == 13 || type == 14)
746  return dataLen >= sizeof(icmp_timestamp_request);
747 
748  // ICMP_ADDRESS_MASK_REPLY, ICMP_ADDRESS_MASK_REQUEST
749  if (type == 17 || type == 18)
750  return dataLen >= sizeof(icmp_address_mask_request);
751 
752  // ICMP_DEST_UNREACHABLE
753  if (type == 3)
754  return dataLen >= sizeof(icmp_destination_unreachable);
755 
756  // ICMP_REDIRECT
757  if (type == 5)
758  return dataLen >= sizeof(icmp_redirect);
759 
760  // ICMP_TIME_EXCEEDED, ICMP_SOURCE_QUENCH
761  if (type == 4 || type == 11)
762  return dataLen >= sizeof(icmp_time_exceeded);
763 
764  // ICMP_PARAM_PROBLEM
765  if (type == 12)
766  return dataLen >= sizeof(icmp_param_problem);
767 
768  // ICMP_ROUTER_ADV
769  if (type == 9)
770  return dataLen >= sizeof(icmp_router_advertisement_hdr);
771 
772  return false;
773  }
774 
775 } // namespace pcpp
Definition: IpAddress.h:32
Definition: IPv4Layer.h:453
Definition: IcmpLayer.h:366
icmp_router_solicitation * getRouterSolicitationData()
icmp_address_mask_request * setAddressMaskRequestData(uint16_t id, uint16_t sequence, IPv4Address mask)
void parseNextLayer()
icmp_echo_request * getEchoRequestData()
std::string toString() const
icmp_info_reply * getInfoReplyData()
icmp_info_request * setInfoRequestData(uint16_t id, uint16_t sequence)
icmp_echo_reply * getEchoReplyData()
size_t getHeaderLen() const
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)
void computeCalculateFields()
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:419
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:405
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:733
icmp_param_problem * setParamProblemData(uint8_t code, uint8_t errorOctetPointer, IPv4Layer *ipHeader, Layer *l4Header)
icmp_time_exceeded * getTimeExceededData()
icmp_param_problem * getParamProblemData()
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)
IcmpMessageType getMessageType() const
icmp_source_quench * setSourceQuenchdata(IPv4Layer *ipHeader, Layer *l4Header)
OsiModelLayer getOsiModelLayer() const
Definition: IcmpLayer.h:725
Definition: Layer.h:70
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:354
@ OsiModelNetworkLayer
Definition: ProtocolType.h:360
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