PcapPlusPlus  23.09
IcmpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_ICMP_LAYER
2 #define PACKETPP_ICMP_LAYER
3 
4 #include "Layer.h"
5 #include "IPv4Layer.h"
6 #ifdef _MSC_VER
7 #include <Winsock2.h>
8 #else
9 #include <sys/time.h>
10 #endif
11 #include <vector>
12 
13 
15 
20 namespace pcpp
21 {
22 
27 #pragma pack(push, 1)
28  typedef struct icmphdr
29  {
31  uint8_t type;
33  uint8_t code;
35  uint16_t checksum;
36  } icmphdr;
37 #pragma pack(pop)
38 
43  {
76  };
77 
83  {
122  };
123 
124 
129 #pragma pack(push, 1)
130  typedef struct icmp_echo_hdr : icmphdr
131  {
133  uint16_t id;
135  uint16_t sequence;
137  uint64_t timestamp;
138  } icmp_echo_hdr;
139 #pragma pack(pop)
140 
141 
146  typedef struct icmp_echo_request
147  {
151  size_t dataLength;
153  uint8_t* data;
155 
156 
162 
163 
168 #pragma pack(push, 1)
170  {
172  uint16_t id;
174  uint16_t sequence;
182 #pragma pack(pop)
183 
184 
190 
191 
196 #pragma pack(push, 1)
198  {
200  uint16_t unused;
202  uint16_t nextHopMTU;
204 #pragma pack(pop)
205 
206 
211 #pragma pack(push, 1)
212  typedef struct icmp_time_exceeded : icmphdr
213  {
215  uint32_t unused;
217 #pragma pack(pop)
218 
219 
225 
226 
231 #pragma pack(push, 1)
232  typedef struct icmp_param_problem : icmphdr
233  {
235  uint8_t pointer;
237  uint8_t unused1;
239  uint16_t unused2;
241 #pragma pack(pop)
242 
243 
249 
254 #pragma pack(push, 1)
255  typedef struct icmp_redirect : icmphdr
256  {
258  uint32_t gatewayAddress;
259  } icmp_redirect;
260 #pragma pack(pop)
261 
262 
267 #pragma pack(push, 1)
269  {
271  uint32_t routerAddress;
275  uint32_t preferenceLevel;
276 
282  void setRouterAddress(IPv4Address addr, uint32_t preference);
283 
287  IPv4Address getAddress() const { return routerAddress; }
288  };
289 #pragma pack(pop)
290 
291 
296 #pragma pack(push, 1)
298  {
305  uint16_t lifetime;
307 #pragma pack(pop)
308 
309 
315  {
318 
325  icmp_router_address_structure* getRouterAddress(int index) const;
326  };
327 
328 
333 #pragma pack(push, 1)
335  {
337  uint16_t id;
339  uint16_t sequence;
341  uint32_t addressMask;
343 #pragma pack(pop)
344 
345 
351 
352 
357 #pragma pack(push, 1)
358  typedef struct icmp_info_request : icmphdr
359  {
361  uint16_t id;
363  uint16_t sequence;
365 #pragma pack(pop)
366 
367 
373 
374 
379  class IcmpLayer : public Layer
380  {
381  private:
382  icmp_echo_request m_EchoData;
383  mutable icmp_router_advertisement m_RouterAdvData;
384 
385  bool cleanIcmpLayer();
386 
387  bool setEchoData(IcmpMessageType echoType, uint16_t id, uint16_t sequence, uint64_t timestamp, const uint8_t* data, size_t dataLen);
388 
389  bool setIpAndL4Layers(IPv4Layer* ipLayer, Layer* l4Layer);
390 
391  public:
399  // cppcheck-suppress uninitMemberVar
400  IcmpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = ICMP; }
401 
406  IcmpLayer();
407 
408  virtual ~IcmpLayer() {}
409 
414  icmphdr* getIcmpHeader() const { return (icmphdr*)m_Data; }
415 
419  IcmpMessageType getMessageType() const;
420 
425  bool isMessageOfType(IcmpMessageType type) const { return getMessageType() == type; }
426 
430  icmp_echo_request* getEchoRequestData();
431 
442  icmp_echo_request* setEchoRequestData(uint16_t id, uint16_t sequence, uint64_t timestamp, const uint8_t* data, size_t dataLen);
443 
447  icmp_echo_reply* getEchoReplyData();
448 
459  icmp_echo_reply* setEchoReplyData(uint16_t id, uint16_t sequence, uint64_t timestamp, const uint8_t* data, size_t dataLen);
460 
464  icmp_timestamp_request* getTimestampRequestData();
465 
474  icmp_timestamp_request* setTimestampRequestData(uint16_t id, uint16_t sequence, timeval originateTimestamp);
475 
479  icmp_timestamp_reply* getTimestampReplyData();
480 
491  icmp_timestamp_reply* setTimestampReplyData(uint16_t id, uint16_t sequence,
492  timeval originateTimestamp, timeval receiveTimestamp, timeval transmitTimestamp);
493 
499  icmp_destination_unreachable* getDestUnreachableData();
500 
512  icmp_destination_unreachable* setDestUnreachableData(IcmpDestUnreachableCodes code, uint16_t nextHopMTU, IPv4Layer* ipHeader, Layer* l4Header);
513 
519  icmp_source_quench* getSourceQuenchdata();
520 
530  icmp_source_quench* setSourceQuenchdata(IPv4Layer* ipHeader, Layer* l4Header);
531 
537  icmp_redirect* getRedirectData();
538 
550  icmp_redirect* setRedirectData(uint8_t code, IPv4Address gatewayAddress, IPv4Layer* ipHeader, Layer* l4Header);
551 
555  icmp_router_advertisement* getRouterAdvertisementData() const;
556 
565  icmp_router_advertisement* setRouterAdvertisementData(uint8_t code, uint16_t lifetimeInSeconds, const std::vector<icmp_router_address_structure>& routerAddresses);
566 
570  icmp_router_solicitation* getRouterSolicitationData();
571 
578  icmp_router_solicitation* setRouterSolicitationData();
579 
585  icmp_time_exceeded* getTimeExceededData();
586 
597  icmp_time_exceeded* setTimeExceededData(uint8_t code, IPv4Layer* ipHeader, Layer* l4Header);
598 
602  icmp_param_problem* getParamProblemData();
603 
615  icmp_param_problem* setParamProblemData(uint8_t code, uint8_t errorOctetPointer, IPv4Layer* ipHeader, Layer* l4Header);
616 
620  icmp_address_mask_request* getAddressMaskRequestData();
621 
630  icmp_address_mask_request* setAddressMaskRequestData(uint16_t id, uint16_t sequence, IPv4Address mask);
631 
635  icmp_address_mask_reply* getAddressMaskReplyData();
636 
645  icmp_address_mask_reply* setAddressMaskReplyData(uint16_t id, uint16_t sequence, IPv4Address mask);
646 
650  icmp_info_request* getInfoRequestData();
651 
659  icmp_info_request* setInfoRequestData(uint16_t id, uint16_t sequence);
660 
664  icmp_info_reply* getInfoReplyData();
665 
673  icmp_info_reply* setInfoReplyData(uint16_t id, uint16_t sequence);
674 
681  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
682 
683  // implement abstract methods
684 
690  void parseNextLayer();
691 
697  size_t getHeaderLen() const;
698 
702  void computeCalculateFields();
703 
704  std::string toString() const;
705 
707  };
708 
709  // implementation of inline methods
710 
711  bool IcmpLayer::isDataValid(const uint8_t* data, size_t dataLen)
712  {
713  if (dataLen < sizeof(icmphdr))
714  return false;
715 
716  uint8_t type = data[0];
717 
718  // ICMP_ECHO_REQUEST, ICMP_ECHO_REPLY, ICMP_ROUTER_SOL, ICMP_INFO_REQUEST, ICMP_INFO_REPLY
719  if (type == 8 || type == 0 || type == 10 || type == 15 || type == 16)
720  return true;
721 
722  // ICMP_TIMESTAMP_REQUEST, ICMP_TIMESTAMP_REPLY
723  if (type == 13 || type == 14)
724  return dataLen >= sizeof(icmp_timestamp_request);
725 
726  // ICMP_ADDRESS_MASK_REPLY, ICMP_ADDRESS_MASK_REQUEST
727  if (type == 17 || type == 18)
728  return dataLen >= sizeof(icmp_address_mask_request);
729 
730  // ICMP_DEST_UNREACHABLE
731  if (type == 3)
732  return dataLen >= sizeof(icmp_destination_unreachable);
733 
734  // ICMP_REDIRECT
735  if (type == 5)
736  return dataLen >= sizeof(icmp_redirect);
737 
738  // ICMP_TIME_EXCEEDED, ICMP_SOURCE_QUENCH
739  if (type == 4 || type == 11)
740  return dataLen >= sizeof(icmp_time_exceeded);
741 
742  // ICMP_PARAM_PROBLEM
743  if (type == 12)
744  return dataLen >= sizeof(icmp_param_problem);
745 
746  // ICMP_ROUTER_ADV
747  if (type == 9)
748  return dataLen >= sizeof(icmp_router_advertisement_hdr);
749 
750  return false;
751  }
752 
753 } // namespace pcpp
754 
755 #endif /* PACKETPP_ICMP_LAYER */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
uint32_t routerAddress
Definition: IcmpLayer.h:271
Definition: IcmpLayer.h:89
icmp_info_request icmp_info_reply
Definition: IcmpLayer.h:372
Definition: IcmpLayer.h:379
uint16_t nextHopMTU
Definition: IcmpLayer.h:202
IPv4Address getAddress() const
Definition: IcmpLayer.h:287
icmp_timestamp_request icmp_timestamp_reply
Definition: IcmpLayer.h:189
Definition: IcmpLayer.h:53
Definition: IcmpLayer.h:297
icmp_router_advertisement_hdr * header
Definition: IcmpLayer.h:317
Definition: IcmpLayer.h:169
icmphdr icmp_router_solicitation
Definition: IcmpLayer.h:248
icmp_time_exceeded icmp_source_quench
Definition: IcmpLayer.h:224
Definition: IcmpLayer.h:28
IcmpDestUnreachableCodes
Definition: IcmpLayer.h:82
uint16_t id
Definition: IcmpLayer.h:172
Definition: Layer.h:70
uint8_t advertisementCount
Definition: IcmpLayer.h:300
Definition: IcmpLayer.h:63
Definition: IcmpLayer.h:69
Definition: IcmpLayer.h:57
Definition: Packet.h:26
Definition: IcmpLayer.h:59
uint16_t sequence
Definition: IcmpLayer.h:174
icmp_echo_hdr * header
Definition: IcmpLayer.h:149
uint8_t type
Definition: IcmpLayer.h:31
Definition: IcmpLayer.h:212
Definition: IcmpLayer.h:358
uint32_t preferenceLevel
Definition: IcmpLayer.h:275
Definition: IcmpLayer.h:334
Definition: IcmpLayer.h:49
uint32_t transmitTimestamp
Definition: IcmpLayer.h:180
uint8_t unused1
Definition: IcmpLayer.h:237
uint16_t id
Definition: IcmpLayer.h:133
Definition: ProtocolType.h:340
uint32_t receiveTimestamp
Definition: IcmpLayer.h:178
uint32_t originateTimestamp
Definition: IcmpLayer.h:176
icmp_echo_request icmp_echo_reply
Definition: IcmpLayer.h:161
uint16_t id
Definition: IcmpLayer.h:361
Definition: IcmpLayer.h:73
Definition: IcmpLayer.h:314
Definition: IcmpLayer.h:146
uint32_t addressMask
Definition: IcmpLayer.h:341
Definition: IcmpLayer.h:85
Definition: IcmpLayer.h:93
Definition: IcmpLayer.h:75
uint64_t timestamp
Definition: IcmpLayer.h:137
bool isMessageOfType(IcmpMessageType type) const
Definition: IcmpLayer.h:425
Definition: IcmpLayer.h:95
Definition: IpAddress.h:42
Definition: IcmpLayer.h:130
Definition: IcmpLayer.h:232
uint16_t lifetime
Definition: IcmpLayer.h:305
Definition: IcmpLayer.h:91
uint16_t sequence
Definition: IcmpLayer.h:135
Definition: IcmpLayer.h:47
Definition: IcmpLayer.h:65
IcmpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: IcmpLayer.h:400
Definition: IcmpLayer.h:113
Definition: IcmpLayer.h:67
uint16_t id
Definition: IcmpLayer.h:337
uint16_t unused
Definition: IcmpLayer.h:200
Definition: IcmpLayer.h:197
const ProtocolType ICMP
Definition: ProtocolType.h:83
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IcmpLayer.h:711
size_t dataLength
Definition: IcmpLayer.h:151
uint32_t unused
Definition: IcmpLayer.h:215
uint8_t * data
Definition: IcmpLayer.h:153
Definition: IcmpLayer.h:71
Definition: IcmpLayer.h:87
Definition: IcmpLayer.h:97
Definition: IcmpLayer.h:255
Definition: IcmpLayer.h:101
IcmpMessageType
Definition: IcmpLayer.h:42
Definition: IcmpLayer.h:45
uint32_t gatewayAddress
Definition: IcmpLayer.h:258
Definition: IcmpLayer.h:121
Definition: IPv4Layer.h:409
uint16_t sequence
Definition: IcmpLayer.h:339
uint16_t checksum
Definition: IcmpLayer.h:35
Definition: IcmpLayer.h:99
Definition: IcmpLayer.h:55
Definition: IcmpLayer.h:117
uint16_t sequence
Definition: IcmpLayer.h:363
Definition: IcmpLayer.h:268
icmphdr * getIcmpHeader() const
Definition: IcmpLayer.h:414
uint16_t unused2
Definition: IcmpLayer.h:239
Definition: IcmpLayer.h:103
uint8_t addressEntrySize
Definition: IcmpLayer.h:303
uint8_t code
Definition: IcmpLayer.h:33
Definition: IcmpLayer.h:51
icmp_address_mask_request icmp_address_mask_reply
Definition: IcmpLayer.h:350
Definition: IcmpLayer.h:61
uint8_t pointer
Definition: IcmpLayer.h:235
OsiModelLayer getOsiModelLayer() const
Definition: IcmpLayer.h:706
Definition: IcmpLayer.h:105