PcapPlusPlus  20.08+
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 <string.h>
8 #include <vector>
9 
11 
16 namespace pcpp
17 {
18 
23 #pragma pack(push, 1)
24  struct iphdr {
25 #if (BYTE_ORDER == LITTLE_ENDIAN)
26 
29  ipVersion:4;
30 #else
31 
32  uint8_t ipVersion:4,
35 #endif
36 
37  uint8_t typeOfService;
39  uint16_t totalLength;
41  uint16_t ipId;
43  uint16_t fragmentOffset;
45  uint8_t timeToLive;
47  uint8_t protocol;
49  uint16_t headerChecksum;
51  uint32_t ipSrc;
53  uint32_t ipDst;
54  /*The options start here. */
55  };
56 #pragma pack(pop)
57 
62  {
105  };
106 
107 
112  {
155  };
156 
157 #define PCPP_IP_DONT_FRAGMENT 0x40
158 #define PCPP_IP_MORE_FRAGMENTS 0x20
159 
165  {
170  {
172  TimestampOnly = 0,
174  TimestampAndIP = 1,
176  TimestampsForPrespecifiedIPs = 2,
178  Unknown = 3
179  };
180 
183 
185  std::vector<uint32_t> timestamps;
186 
188  std::vector<IPv4Address> ipAddresses;
189 
192 
196  void clear()
197  {
199  timestamps.clear();
200  ipAddresses.clear();
201  }
202  };
203 
204 
210  class IPv4Option : public TLVRecord
211  {
212  public:
213 
218  IPv4Option(uint8_t* optionRawData) : TLVRecord(optionRawData) { }
219 
224 
231  std::vector<IPv4Address> getValueAsIpList() const
232  {
233  std::vector<IPv4Address> res;
234 
235  if (m_Data == NULL)
236  return res;
237 
238  size_t dataSize = getDataSize();
239  if (dataSize < 2)
240  return res;
241 
242  uint8_t valueOffset = (uint8_t)(1);
243 
244  while ((size_t)valueOffset < dataSize)
245  {
246  uint32_t curValue;
247  memcpy(&curValue, m_Data->recordValue + valueOffset, sizeof(uint32_t));
248  if (curValue == 0)
249  break;
250 
251  res.push_back(IPv4Address(curValue));
252 
253  valueOffset += (uint8_t)(4);
254  }
255 
256  return res;
257  }
258 
267  {
269  res.clear();
270 
271  if (m_Data == NULL)
272  return res;
273 
274  if (getIPv4OptionType() != IPV4OPT_Timestamp)
275  return res;
276 
277  size_t dataSize = getDataSize();
278  if (dataSize < 2)
279  return res;
280 
281  res.type = (IPv4TimestampOptionValue::TimestampType)m_Data->recordValue[1];
282 
283  uint8_t valueOffset = (uint8_t)(2);
284  bool readIPAddr = (res.type == IPv4TimestampOptionValue::TimestampAndIP);
285 
286  while ((size_t)valueOffset < dataSize)
287  {
288  uint32_t curValue;
289  memcpy(&curValue, m_Data->recordValue + valueOffset, sizeof(uint32_t));
290  if (curValue == 0)
291  break;
292 
293  if (readIPAddr)
294  res.ipAddresses.push_back(IPv4Address(curValue));
295  else
296  res.timestamps.push_back(curValue);
297 
299  readIPAddr = !readIPAddr;
300 
301  valueOffset += (uint8_t)(4);
302  }
303 
304  return res;
305  }
306 
311  {
312  if (m_Data == NULL)
313  return IPV4OPT_Unknown;
314 
315  return (IPv4OptionTypes)m_Data->recordType;
316  }
317 
318 
319  // implement abstract methods
320 
321  size_t getTotalSize() const
322  {
323  if (m_Data == NULL)
324  return 0;
325 
326  if (getIPv4OptionType() == (uint8_t)IPV4OPT_EndOfOptionsList || m_Data->recordType == (uint8_t)IPV4OPT_NOP)
327  return sizeof(uint8_t);
328 
329  return (size_t)m_Data->recordLen;
330  }
331 
332  size_t getDataSize() const
333  {
334  if (m_Data == NULL)
335  return 0;
336 
337  if (getIPv4OptionType() == (uint8_t)IPV4OPT_EndOfOptionsList || m_Data->recordType == (uint8_t)IPV4OPT_NOP)
338  return (size_t)0;
339 
340  return (size_t)m_Data->recordLen - (2*sizeof(uint8_t));
341  }
342  };
343 
344 
351  {
352  private:
353  bool m_BuilderParamsValid;
354 
355  public:
356 
366  IPv4OptionBuilder(IPv4OptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen) :
367  TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) { m_BuilderParamsValid = true; }
368 
375  IPv4OptionBuilder(IPv4OptionTypes optionType, uint16_t optionValue) :
376  TLVRecordBuilder((uint8_t)optionType, optionValue) { m_BuilderParamsValid = true; }
377 
385  IPv4OptionBuilder(IPv4OptionTypes optionType, const std::vector<IPv4Address>& ipList);
386 
391  IPv4OptionBuilder(const IPv4TimestampOptionValue& timestampValue);
392 
397  IPv4Option build() const;
398  };
399 
400 
405  class IPv4Layer : public Layer
406  {
407  public:
415  IPv4Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
416 
428  IPv4Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, bool setTotalLenAsDataLen);
429 
430 
434  IPv4Layer();
435 
441  IPv4Layer(const IPv4Address& srcIP, const IPv4Address& dstIP);
442 
446  IPv4Layer(const IPv4Layer& other);
447 
451  IPv4Layer& operator=(const IPv4Layer& other);
452 
457  iphdr* getIPv4Header() const { return (iphdr*)m_Data; }
458 
463  IPv4Address getSrcIpAddress() const { return getIPv4Header()->ipSrc; }
464 
469  void setSrcIpAddress(const IPv4Address& ipAddr) { getIPv4Header()->ipSrc = ipAddr.toInt(); }
470 
475  IPv4Address getDstIpAddress() const { return getIPv4Header()->ipDst; }
476 
481  void setDstIpAddress(const IPv4Address& ipAddr) { getIPv4Header()->ipDst = ipAddr.toInt(); }
482 
486  bool isFragment() const;
487 
492  bool isFirstFragment() const;
493 
498  bool isLastFragment() const;
499 
503  uint8_t getFragmentFlags() const;
504 
508  uint16_t getFragmentOffset() const;
509 
516  IPv4Option getOption(IPv4OptionTypes option) const;
517 
522  IPv4Option getFirstOption() const;
523 
531  IPv4Option getNextOption(IPv4Option& option) const;
532 
536  size_t getOptionCount() const;
537 
545  IPv4Option addOption(const IPv4OptionBuilder& optionBuilder);
546 
556  IPv4Option addOptionAfter(const IPv4OptionBuilder& optionBuilder, IPv4OptionTypes prevOptionType = IPV4OPT_Unknown);
557 
564  bool removeOption(IPv4OptionTypes option);
565 
570  bool removeAllOptions();
571 
572 
573  // implement abstract methods
574 
578  void parseNextLayer();
579 
583  size_t getHeaderLen() const { return (size_t)((uint16_t)(getIPv4Header()->internetHeaderLength) * 4) + m_TempHeaderExtension; }
584 
592  void computeCalculateFields();
593 
594  std::string toString() const;
595 
597 
604  static inline bool isDataValid(const uint8_t* data, size_t dataLen);
605 
606  private:
607  int m_NumOfTrailingBytes;
608  int m_TempHeaderExtension;
609  TLVRecordReader<IPv4Option> m_OptionReader;
610 
611  void copyLayerData(const IPv4Layer& other);
612  uint8_t* getOptionsBasePtr() const { return m_Data + sizeof(iphdr); }
613  IPv4Option addOptionAt(const IPv4OptionBuilder& optionBuilder, int offset);
614  void adjustOptionsTrailer(size_t totalOptSize);
615  void initLayer();
616  void initLayerInPacket(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, bool setTotalLenAsDataLen);
617  };
618 
619 
620  // implementation of inline methods
621 
622  bool IPv4Layer::isDataValid(const uint8_t* data, size_t dataLen)
623  {
624  const iphdr* hdr = reinterpret_cast<const iphdr*>(data);
625  return dataLen >= sizeof(iphdr) && hdr->ipVersion == 4 && hdr->internetHeaderLength >= 5;
626  }
627 
628 } // namespace pcpp
629 
630 #endif /* PACKETPP_IPV4_LAYER */
Definition: IPv4Layer.h:102
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:238
size_t getTotalSize() const
Definition: IPv4Layer.h:321
Definition: IPv4Layer.h:98
std::vector< uint32_t > timestamps
Definition: IPv4Layer.h:185
Definition: IPv4Layer.h:130
uint16_t ipId
Definition: IPv4Layer.h:41
Definition: IPv4Layer.h:126
Definition: IPv4Layer.h:132
IPv4Option(uint8_t *optionRawData)
Definition: IPv4Layer.h:218
uint8_t timeToLive
Definition: IPv4Layer.h:45
void setSrcIpAddress(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:469
uint8_t internetHeaderLength
Definition: IPv4Layer.h:27
Definition: IPv4Layer.h:70
uint8_t typeOfService
Definition: IPv4Layer.h:37
Definition: IPv4Layer.h:116
void setDstIpAddress(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:481
Definition: IPv4Layer.h:96
Definition: IPv4Layer.h:138
Definition: TLVData.h:351
Definition: IPv4Layer.h:148
Definition: Layer.h:70
Definition: IPv4Layer.h:140
Definition: IPv4Layer.h:152
Definition: TLVData.h:23
Definition: Packet.h:26
Definition: IPv4Layer.h:124
IPv4OptionTypes getIPv4OptionType() const
Definition: IPv4Layer.h:310
TimestampType
Definition: IPv4Layer.h:169
Definition: IPv4Layer.h:114
Definition: IPv4Layer.h:68
std::vector< IPv4Address > getValueAsIpList() const
Definition: IPv4Layer.h:231
Definition: TLVData.h:197
Definition: IPv4Layer.h:90
IPProtocolTypes
Definition: IPv4Layer.h:61
Definition: IPv4Layer.h:80
Definition: ProtocolType.h:245
Definition: IPv4Layer.h:88
Definition: IPv4Layer.h:146
Definition: IPv4Layer.h:350
uint16_t totalLength
Definition: IPv4Layer.h:39
IPv4Address getSrcIpAddress() const
Definition: IPv4Layer.h:463
Definition: IPv4Layer.h:82
Definition: IPv4Layer.h:24
Definition: IPv4Layer.h:86
Definition: IPv4Layer.h:178
IPv4Address getDstIpAddress() const
Definition: IPv4Layer.h:475
IPv4OptionBuilder(IPv4OptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: IPv4Layer.h:366
Definition: IPv4Layer.h:150
Definition: IPv4Layer.h:118
Definition: IPv4Layer.h:72
Definition: IpAddress.h:26
IPv4TimestampOptionValue getTimestampOptionValue() const
Definition: IPv4Layer.h:266
uint16_t fragmentOffset
Definition: IPv4Layer.h:43
std::vector< IPv4Address > ipAddresses
Definition: IPv4Layer.h:188
Definition: IPv4Layer.h:210
Definition: IPv4Layer.h:136
Definition: IPv4Layer.h:94
Definition: IPv4Layer.h:120
uint8_t ipVersion
Definition: IPv4Layer.h:27
OsiModelLayer getOsiModelLayer() const
Definition: IPv4Layer.h:596
uint8_t protocol
Definition: IPv4Layer.h:47
Definition: IPv4Layer.h:128
Definition: IPv4Layer.h:164
TimestampType type
Definition: IPv4Layer.h:182
Definition: IPv4Layer.h:78
IPv4TimestampOptionValue()
Definition: IPv4Layer.h:191
uint16_t headerChecksum
Definition: IPv4Layer.h:49
~IPv4Option()
Definition: IPv4Layer.h:223
void clear()
Definition: IPv4Layer.h:196
size_t getDataSize() const
Definition: IPv4Layer.h:332
Definition: IPv4Layer.h:92
IPv4OptionBuilder(IPv4OptionTypes optionType, uint16_t optionValue)
Definition: IPv4Layer.h:375
Definition: IPv4Layer.h:100
uint32_t ipDst
Definition: IPv4Layer.h:53
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPv4Layer.h:622
Definition: IPv4Layer.h:84
Definition: IPv4Layer.h:405
Definition: IPv4Layer.h:144
Definition: IPv4Layer.h:104
Definition: IPv4Layer.h:134
Definition: IPv4Layer.h:122
Definition: IPv4Layer.h:64
uint32_t ipSrc
Definition: IPv4Layer.h:51
Definition: IPv4Layer.h:66
Definition: IPv4Layer.h:74
IPv4OptionTypes
Definition: IPv4Layer.h:111
uint32_t toInt() const
Definition: IpAddress.h:125
size_t getHeaderLen() const
Definition: IPv4Layer.h:583
Definition: IPv4Layer.h:76
Definition: IPv4Layer.h:154
Definition: IPv4Layer.h:142
iphdr * getIPv4Header() const
Definition: IPv4Layer.h:457