PcapPlusPlus  22.05
IPv4Layer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_IPV4_LAYER
2 #define PACKETPP_IPV4_LAYER
3 
4 #include "Layer.h"
5 #include "TLVData.h"
6 #include "IpAddress.h"
7 #include "IPLayer.h"
8 #include <string.h>
9 #include <vector>
10 
12 
17 namespace pcpp
18 {
19 
24 #pragma pack(push, 1)
25  struct iphdr
26  {
27 #if (BYTE_ORDER == LITTLE_ENDIAN)
28 
31  ipVersion:4;
32 #else
33 
34  uint8_t ipVersion:4,
37 #endif
38 
39  uint8_t typeOfService;
41  uint16_t totalLength;
43  uint16_t ipId;
45  uint16_t fragmentOffset;
47  uint8_t timeToLive;
49  uint8_t protocol;
51  uint16_t headerChecksum;
53  uint32_t ipSrc;
55  uint32_t ipDst;
56  /*The options start here. */
57  };
58 #pragma pack(pop)
59 
64  {
107  };
108 
109 
114  {
157  };
158 
159 #define PCPP_IP_DONT_FRAGMENT 0x40
160 #define PCPP_IP_MORE_FRAGMENTS 0x20
161 
167  {
172  {
174  TimestampOnly = 0,
176  TimestampAndIP = 1,
178  TimestampsForPrespecifiedIPs = 2,
180  Unknown = 3
181  };
182 
185 
187  std::vector<uint32_t> timestamps;
188 
190  std::vector<IPv4Address> ipAddresses;
191 
194 
198  void clear()
199  {
201  timestamps.clear();
202  ipAddresses.clear();
203  }
204  };
205 
206 
212  class IPv4Option : public TLVRecord<uint8_t, uint8_t>
213  {
214  public:
215 
220  IPv4Option(uint8_t* optionRawData) : TLVRecord(optionRawData) { }
221 
226 
233  std::vector<IPv4Address> getValueAsIpList() const
234  {
235  std::vector<IPv4Address> res;
236 
237  if (m_Data == NULL)
238  return res;
239 
240  size_t dataSize = getDataSize();
241  if (dataSize < 2)
242  return res;
243 
244  uint8_t valueOffset = (uint8_t)(1);
245 
246  while ((size_t)valueOffset < dataSize)
247  {
248  uint32_t curValue;
249  memcpy(&curValue, m_Data->recordValue + valueOffset, sizeof(uint32_t));
250  if (curValue == 0)
251  break;
252 
253  res.push_back(IPv4Address(curValue));
254 
255  valueOffset += (uint8_t)(4);
256  }
257 
258  return res;
259  }
260 
269  {
271  res.clear();
272 
273  if (m_Data == NULL)
274  return res;
275 
276  if (getIPv4OptionType() != IPV4OPT_Timestamp)
277  return res;
278 
279  size_t dataSize = getDataSize();
280  if (dataSize < 2)
281  return res;
282 
283  res.type = (IPv4TimestampOptionValue::TimestampType)m_Data->recordValue[1];
284 
285  uint8_t valueOffset = (uint8_t)(2);
286  bool readIPAddr = (res.type == IPv4TimestampOptionValue::TimestampAndIP);
287 
288  while ((size_t)valueOffset < dataSize)
289  {
290  uint32_t curValue;
291  memcpy(&curValue, m_Data->recordValue + valueOffset, sizeof(uint32_t));
292  if (curValue == 0)
293  break;
294 
295  if (readIPAddr)
296  res.ipAddresses.push_back(IPv4Address(curValue));
297  else
298  res.timestamps.push_back(curValue);
299 
301  readIPAddr = !readIPAddr;
302 
303  valueOffset += (uint8_t)(4);
304  }
305 
306  return res;
307  }
308 
313  {
314  if (m_Data == NULL)
315  return IPV4OPT_Unknown;
316 
317  return (IPv4OptionTypes)m_Data->recordType;
318  }
319 
320 
321  // implement abstract methods
322 
323  size_t getTotalSize() const
324  {
325  if (m_Data == NULL)
326  return 0;
327 
328  if (getIPv4OptionType() == (uint8_t)IPV4OPT_EndOfOptionsList || m_Data->recordType == (uint8_t)IPV4OPT_NOP)
329  return sizeof(uint8_t);
330 
331  return (size_t)m_Data->recordLen;
332  }
333 
334  size_t getDataSize() const
335  {
336  if (m_Data == NULL)
337  return 0;
338 
339  if (getIPv4OptionType() == (uint8_t)IPV4OPT_EndOfOptionsList || m_Data->recordType == (uint8_t)IPV4OPT_NOP)
340  return (size_t)0;
341 
342  return (size_t)m_Data->recordLen - (2*sizeof(uint8_t));
343  }
344  };
345 
346 
353  {
354  private:
355  bool m_BuilderParamsValid;
356 
357  public:
358 
368  IPv4OptionBuilder(IPv4OptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen) :
369  TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) { m_BuilderParamsValid = true; }
370 
377  IPv4OptionBuilder(IPv4OptionTypes optionType, uint16_t optionValue) :
378  TLVRecordBuilder((uint8_t)optionType, optionValue) { m_BuilderParamsValid = true; }
379 
387  IPv4OptionBuilder(IPv4OptionTypes optionType, const std::vector<IPv4Address>& ipList);
388 
393  IPv4OptionBuilder(const IPv4TimestampOptionValue& timestampValue);
394 
399  IPv4Option build() const;
400  };
401 
402 
407  class IPv4Layer : public Layer, public IPLayer
408  {
409  public:
417  IPv4Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
418 
430  IPv4Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, bool setTotalLenAsDataLen);
431 
432 
436  IPv4Layer();
437 
443  IPv4Layer(const IPv4Address& srcIP, const IPv4Address& dstIP);
444 
448  IPv4Layer(const IPv4Layer& other);
449 
453  IPv4Layer& operator=(const IPv4Layer& other);
454 
459  iphdr* getIPv4Header() const { return (iphdr*)m_Data; }
460 
466  IPAddress getSrcIPAddress() const { return getSrcIPv4Address(); }
467 
472  IPv4Address getSrcIPv4Address() const { return getIPv4Header()->ipSrc; }
473 
478  void setSrcIPv4Address(const IPv4Address& ipAddr) { getIPv4Header()->ipSrc = ipAddr.toInt(); }
479 
485  IPAddress getDstIPAddress() const { return getDstIPv4Address(); }
486 
491  IPv4Address getDstIPv4Address() const { return getIPv4Header()->ipDst; }
492 
497  void setDstIPv4Address(const IPv4Address& ipAddr) { getIPv4Header()->ipDst = ipAddr.toInt(); }
498 
502  bool isFragment() const;
503 
508  bool isFirstFragment() const;
509 
514  bool isLastFragment() const;
515 
519  uint8_t getFragmentFlags() const;
520 
524  uint16_t getFragmentOffset() const;
525 
532  IPv4Option getOption(IPv4OptionTypes option) const;
533 
538  IPv4Option getFirstOption() const;
539 
547  IPv4Option getNextOption(IPv4Option& option) const;
548 
552  size_t getOptionCount() const;
553 
561  IPv4Option addOption(const IPv4OptionBuilder& optionBuilder);
562 
572  IPv4Option addOptionAfter(const IPv4OptionBuilder& optionBuilder, IPv4OptionTypes prevOptionType = IPV4OPT_Unknown);
573 
580  bool removeOption(IPv4OptionTypes option);
581 
586  bool removeAllOptions();
587 
588 
589  // implement abstract methods
590 
605  void parseNextLayer();
606 
610  size_t getHeaderLen() const { return (size_t)((uint16_t)(getIPv4Header()->internetHeaderLength) * 4) + m_TempHeaderExtension; }
611 
619  void computeCalculateFields();
620 
621  std::string toString() const;
622 
624 
631  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
632 
633  private:
634  int m_NumOfTrailingBytes;
635  int m_TempHeaderExtension;
636  TLVRecordReader<IPv4Option> m_OptionReader;
637 
638  void copyLayerData(const IPv4Layer& other);
639  uint8_t* getOptionsBasePtr() const { return m_Data + sizeof(iphdr); }
640  IPv4Option addOptionAt(const IPv4OptionBuilder& optionBuilder, int offset);
641  void adjustOptionsTrailer(size_t totalOptSize);
642  void initLayer();
643  void initLayerInPacket(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, bool setTotalLenAsDataLen);
644  };
645 
646 
647  // implementation of inline methods
648 
649  bool IPv4Layer::isDataValid(const uint8_t* data, size_t dataLen)
650  {
651  const iphdr* hdr = reinterpret_cast<const iphdr*>(data);
652  return dataLen >= sizeof(iphdr) && hdr->ipVersion == 4 && hdr->internetHeaderLength >= 5;
653  }
654 
655 } // namespace pcpp
656 
657 #endif /* PACKETPP_IPV4_LAYER */
Definition: IPv4Layer.h:104
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:263
size_t getTotalSize() const
Definition: IPv4Layer.h:323
Definition: IPv4Layer.h:100
std::vector< uint32_t > timestamps
Definition: IPv4Layer.h:187
Definition: IPv4Layer.h:132
uint16_t ipId
Definition: IPv4Layer.h:43
Definition: IPv4Layer.h:128
Definition: IPv4Layer.h:134
IPv4Option(uint8_t *optionRawData)
Definition: IPv4Layer.h:220
uint8_t timeToLive
Definition: IPv4Layer.h:47
uint8_t internetHeaderLength
Definition: IPv4Layer.h:29
Definition: IPv4Layer.h:72
uint8_t typeOfService
Definition: IPv4Layer.h:39
Definition: IPv4Layer.h:118
Definition: IPv4Layer.h:98
Definition: IPv4Layer.h:140
Definition: TLVData.h:363
Definition: IPv4Layer.h:150
Definition: Layer.h:70
Definition: IPv4Layer.h:142
Definition: IPv4Layer.h:154
Definition: TLVData.h:24
Definition: IPLayer.h:22
IPAddress getDstIPAddress() const
Definition: IPv4Layer.h:485
Definition: Packet.h:26
Definition: IPv4Layer.h:126
IPv4OptionTypes getIPv4OptionType() const
Definition: IPv4Layer.h:312
TimestampType
Definition: IPv4Layer.h:171
void setSrcIPv4Address(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:478
Definition: IPv4Layer.h:116
Definition: IPv4Layer.h:70
std::vector< IPv4Address > getValueAsIpList() const
Definition: IPv4Layer.h:233
Definition: TLVData.h:207
Definition: IPv4Layer.h:92
IPProtocolTypes
Definition: IPv4Layer.h:63
Definition: IPv4Layer.h:82
Definition: ProtocolType.h:270
Definition: IPv4Layer.h:90
Definition: IPv4Layer.h:148
Definition: IPv4Layer.h:352
uint16_t totalLength
Definition: IPv4Layer.h:41
IPv4Address getSrcIPv4Address() const
Definition: IPv4Layer.h:472
Definition: IPv4Layer.h:84
Definition: IPv4Layer.h:25
Definition: IPv4Layer.h:88
Definition: IPv4Layer.h:180
IPv4OptionBuilder(IPv4OptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: IPv4Layer.h:368
Definition: IPv4Layer.h:152
Definition: IPv4Layer.h:120
Definition: IPv4Layer.h:74
Definition: IpAddress.h:27
IPv4TimestampOptionValue getTimestampOptionValue() const
Definition: IPv4Layer.h:268
uint16_t fragmentOffset
Definition: IPv4Layer.h:45
IPAddress getSrcIPAddress() const
Definition: IPv4Layer.h:466
std::vector< IPv4Address > ipAddresses
Definition: IPv4Layer.h:190
Definition: IPv4Layer.h:212
Definition: IPv4Layer.h:138
Definition: IPv4Layer.h:96
Definition: IPv4Layer.h:122
void setDstIPv4Address(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:497
uint8_t ipVersion
Definition: IPv4Layer.h:29
OsiModelLayer getOsiModelLayer() const
Definition: IPv4Layer.h:623
uint8_t protocol
Definition: IPv4Layer.h:49
Definition: IPv4Layer.h:130
Definition: IPv4Layer.h:166
TimestampType type
Definition: IPv4Layer.h:184
Definition: IPv4Layer.h:80
IPv4TimestampOptionValue()
Definition: IPv4Layer.h:193
uint16_t headerChecksum
Definition: IPv4Layer.h:51
~IPv4Option()
Definition: IPv4Layer.h:225
void clear()
Definition: IPv4Layer.h:198
size_t getDataSize() const
Definition: IPv4Layer.h:334
Definition: IPv4Layer.h:94
IPv4OptionBuilder(IPv4OptionTypes optionType, uint16_t optionValue)
Definition: IPv4Layer.h:377
Definition: IPv4Layer.h:102
uint32_t ipDst
Definition: IPv4Layer.h:55
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPv4Layer.h:649
Definition: IPv4Layer.h:86
Definition: IPv4Layer.h:407
IPv4Address getDstIPv4Address() const
Definition: IPv4Layer.h:491
Definition: IpAddress.h:278
Definition: IPv4Layer.h:146
Definition: IPv4Layer.h:106
Definition: IPv4Layer.h:136
Definition: IPv4Layer.h:124
Definition: IPv4Layer.h:66
uint32_t ipSrc
Definition: IPv4Layer.h:53
Definition: IPv4Layer.h:68
Definition: IPv4Layer.h:76
IPv4OptionTypes
Definition: IPv4Layer.h:113
uint32_t toInt() const
Definition: IpAddress.h:156
size_t getHeaderLen() const
Definition: IPv4Layer.h:610
Definition: IPv4Layer.h:78
Definition: IPv4Layer.h:156
Definition: IPv4Layer.h:144
iphdr * getIPv4Header() const
Definition: IPv4Layer.h:459