PcapPlusPlus
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  {
166  public:
167 
172  {
181  };
182 
185 
187  std::vector<uint32_t> timestamps;
188 
190  std::vector<IPv4Address> ipAddresses;
191 
195  void clear()
196  {
198  timestamps.clear();
199  ipAddresses.clear();
200  }
201  };
202 
203 
209  class IPv4Option : public TLVRecord
210  {
211  public:
212 
217  IPv4Option(uint8_t* optionRawData) : TLVRecord(optionRawData) { }
218 
223 
230  std::vector<IPv4Address> getValueAsIpList()
231  {
232  std::vector<IPv4Address> res;
233 
234  if (m_Data == NULL)
235  return res;
236 
237  size_t dataSize = getDataSize();
238  if (dataSize < 2)
239  return res;
240 
241  uint8_t valueOffset = (uint8_t)(1);
242 
243  while (valueOffset < dataSize)
244  {
245  uint32_t curValue;
246  memcpy(&curValue, m_Data->recordValue + valueOffset, sizeof(uint32_t));
247  if (curValue == 0)
248  break;
249 
250  res.push_back(IPv4Address(curValue));
251 
252  valueOffset += (uint8_t)(4);
253  }
254 
255  return res;
256  }
257 
266  {
268  res.clear();
269 
270  if (m_Data == NULL)
271  return res;
272 
274  return res;
275 
276  size_t dataSize = getDataSize();
277  if (dataSize < 2)
278  return res;
279 
281 
282  uint8_t valueOffset = (uint8_t)(2);
283  bool readIPAddr = (res.type == IPv4TimestampOptionValue::TimestampAndIP);
284 
285  while (valueOffset < dataSize)
286  {
287  uint32_t curValue;
288  memcpy(&curValue, m_Data->recordValue + valueOffset, sizeof(uint32_t));
289  if (curValue == 0)
290  break;
291 
292  if (readIPAddr)
293  res.ipAddresses.push_back(IPv4Address(curValue));
294  else
295  res.timestamps.push_back(curValue);
296 
298  readIPAddr = !readIPAddr;
299 
300  valueOffset += (uint8_t)(4);
301  }
302 
303  return res;
304  }
305 
310  {
311  if (m_Data == NULL)
312  return IPV4OPT_Unknown;
313 
314  return (IPv4OptionTypes)m_Data->recordType;
315  }
316 
317 
318  // implement abstract methods
319 
320  size_t getTotalSize() const
321  {
322  if (m_Data == NULL)
323  return 0;
324 
325  if (getIPv4OptionType() == (uint8_t)IPV4OPT_EndOfOtionsList || m_Data->recordType == (uint8_t)IPV4OPT_NOP)
326  return sizeof(uint8_t);
327 
328  return (size_t)m_Data->recordLen;
329  }
330 
331  size_t getDataSize()
332  {
333  if (m_Data == NULL)
334  return 0;
335 
336  if (getIPv4OptionType() == (uint8_t)IPV4OPT_EndOfOtionsList || m_Data->recordType == (uint8_t)IPV4OPT_NOP)
337  return (size_t)0;
338 
339  return (size_t)m_Data->recordLen - (2*sizeof(uint8_t));
340  }
341  };
342 
343 
350  {
351  private:
352  bool m_BuilderParamsValid;
353 
354  public:
355 
365  IPv4OptionBuilder(IPv4OptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen) :
366  TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) { m_BuilderParamsValid = true; }
367 
374  IPv4OptionBuilder(IPv4OptionTypes optionType, uint16_t optionValue) :
375  TLVRecordBuilder((uint8_t)optionType, optionValue) { m_BuilderParamsValid = true; }
376 
384  IPv4OptionBuilder(IPv4OptionTypes optionType, const std::vector<IPv4Address>& ipList);
385 
390  IPv4OptionBuilder(const IPv4TimestampOptionValue& timestampValue);
391 
396  IPv4Option build() const;
397  };
398 
399 
404  class IPv4Layer : public Layer
405  {
406  public:
414  IPv4Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
415 
427  IPv4Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, bool setTotalLenAsDataLen);
428 
429 
433  IPv4Layer();
434 
440  IPv4Layer(const IPv4Address& srcIP, const IPv4Address& dstIP);
441 
445  IPv4Layer(const IPv4Layer& other);
446 
450  IPv4Layer& operator=(const IPv4Layer& other);
451 
456  inline iphdr* getIPv4Header() { return (iphdr*)m_Data; }
457 
462  inline IPv4Address getSrcIpAddress() { return IPv4Address(getIPv4Header()->ipSrc); }
463 
468  inline void setSrcIpAddress(const IPv4Address& ipAddr) { getIPv4Header()->ipSrc = ipAddr.toInt(); }
469 
474  inline IPv4Address getDstIpAddress() { return IPv4Address(getIPv4Header()->ipDst); }
475 
480  inline void setDstIpAddress(const IPv4Address& ipAddr) { getIPv4Header()->ipDst = ipAddr.toInt(); }
481 
485  bool isFragment();
486 
491  bool isFirstFragment();
492 
497  bool isLastFragment();
498 
502  uint8_t getFragmentFlags();
503 
507  uint16_t getFragmentOffset();
508 
516 
522 
531 
535  size_t getOptionCount();
536 
544  IPv4Option addOption(const IPv4OptionBuilder& optionBuilder);
545 
555  IPv4Option addOptionAfter(const IPv4OptionBuilder& optionBuilder, IPv4OptionTypes prevOptionType = IPV4OPT_Unknown);
556 
563  bool removeOption(IPv4OptionTypes option);
564 
569  bool removeAllOptions();
570 
571 
572  // implement abstract methods
573 
577  void parseNextLayer();
578 
582  inline size_t getHeaderLen() { return (size_t)(getIPv4Header()->internetHeaderLength*4) + m_TempHeaderExtension; }
583 
591  void computeCalculateFields();
592 
593  std::string toString();
594 
596 
597  private:
598  int m_NumOfTrailingBytes;
599  int m_TempHeaderExtension;
600  TLVRecordReader<IPv4Option> m_OptionReader;
601 
602  void copyLayerData(const IPv4Layer& other);
603  inline uint8_t* getOptionsBasePtr() { return m_Data + sizeof(iphdr); }
604  IPv4Option addOptionAt(const IPv4OptionBuilder& optionBuilder, int offset);
605  void adjustOptionsTrailer(size_t totalOptSize);
606  void initLayer();
607  void initLayerInPacket(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, bool setTotalLenAsDataLen);
608  };
609 
610 } // namespace pcpp
611 
612 #endif /* PACKETPP_IPV4_LAYER */
Definition: IPv4Layer.h:70
Definition: IPv4Layer.h:98
Definition: IPv4Layer.h:64
Definition: IPv4Layer.h:84
uint8_t recordLen
Definition: TLVData.h:33
Definition: IPv4Layer.h:150
uint8_t recordType
Definition: TLVData.h:31
Definition: IPv4Layer.h:152
Definition: IPv4Layer.h:72
IPv4Layer & operator=(const IPv4Layer &other)
uint8_t typeOfService
Definition: IPv4Layer.h:37
Definition: IPv4Layer.h:82
Definition: IPv4Layer.h:130
bool isFirstFragment()
iphdr * getIPv4Header()
Definition: IPv4Layer.h:456
Definition: TLVData.h:333
Definition: TLVData.h:23
uint32_t toInt() const
~IPv4Option()
Definition: IPv4Layer.h:222
void clear()
Definition: IPv4Layer.h:195
IPv4OptionBuilder(IPv4OptionTypes optionType, uint16_t optionValue)
Definition: IPv4Layer.h:374
uint8_t recordValue[]
Definition: TLVData.h:35
Definition: IPv4Layer.h:78
IPv4Option addOption(const IPv4OptionBuilder &optionBuilder)
Definition: IPv4Layer.h:68
Definition: IPv4Layer.h:126
Definition: IpAddress.h:113
IPv4Option getFirstOption()
uint8_t getFragmentFlags()
Definition: IPv4Layer.h:144
uint32_t ipSrc
Definition: IPv4Layer.h:51
Definition: IPv4Layer.h:138
IPv4Option(uint8_t *optionRawData)
Definition: IPv4Layer.h:217
Definition: Layer.h:70
uint16_t ipId
Definition: IPv4Layer.h:41
Definition: IPv4Layer.h:96
size_t getTotalSize() const
Definition: IPv4Layer.h:320
Definition: IPv4Layer.h:140
Definition: IPv4Layer.h:94
IPv4Option getOption(IPv4OptionTypes option)
Definition: IPv4Layer.h:404
Definition: IPv4Layer.h:134
Definition: Packet.h:26
Definition: IPv4Layer.h:114
Definition: IPv4Layer.h:164
Definition: IPv4Layer.h:80
Definition: TLVData.h:189
Definition: IPv4Layer.h:74
uint8_t internetHeaderLength
Definition: IPv4Layer.h:27
Definition: IPv4Layer.h:128
Definition: IPv4Layer.h:120
uint16_t fragmentOffset
Definition: IPv4Layer.h:43
void parseNextLayer()
void setDstIpAddress(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:480
IPv4Option getNextOption(IPv4Option &option)
OsiModelLayer
Definition: ProtocolType.h:213
Definition: IPv4Layer.h:116
Definition: IPv4Layer.h:66
Definition: ProtocolType.h:220
std::vector< uint32_t > timestamps
Definition: IPv4Layer.h:187
size_t getOptionCount()
Definition: IPv4Layer.h:86
IPv4Address getSrcIpAddress()
Definition: IPv4Layer.h:462
std::vector< IPv4Address > getValueAsIpList()
Definition: IPv4Layer.h:230
uint16_t headerChecksum
Definition: IPv4Layer.h:49
size_t getDataSize()
Definition: IPv4Layer.h:331
uint8_t timeToLive
Definition: IPv4Layer.h:45
IPv4OptionTypes getIPv4OptionType() const
Definition: IPv4Layer.h:309
IPv4Address getDstIpAddress()
Definition: IPv4Layer.h:474
std::vector< IPv4Address > ipAddresses
Definition: IPv4Layer.h:190
Definition: IPv4Layer.h:90
IPv4OptionBuilder(IPv4OptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: IPv4Layer.h:365
void setSrcIpAddress(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:468
uint16_t totalLength
Definition: IPv4Layer.h:39
Definition: IPv4Layer.h:104
Definition: IPv4Layer.h:142
Definition: IPv4Layer.h:154
Definition: IPv4Layer.h:118
TimestampType type
Definition: IPv4Layer.h:184
Definition: IPv4Layer.h:122
uint8_t protocol
Definition: IPv4Layer.h:47
Definition: IPv4Layer.h:349
bool removeOption(IPv4OptionTypes option)
Definition: IPv4Layer.h:24
Definition: IPv4Layer.h:124
uint8_t ipVersion
Definition: IPv4Layer.h:27
Definition: IPv4Layer.h:136
Definition: IPv4Layer.h:180
TimestampType
Definition: IPv4Layer.h:171
IPv4TimestampOptionValue getTimestampOptionValue()
Definition: IPv4Layer.h:265
Definition: IPv4Layer.h:76
uint16_t getFragmentOffset()
Definition: IPv4Layer.h:92
IPv4Option build() const
void computeCalculateFields()
Definition: IPv4Layer.h:88
IPv4Option addOptionAfter(const IPv4OptionBuilder &optionBuilder, IPv4OptionTypes prevOptionType=IPV4OPT_Unknown)
Definition: IPv4Layer.h:148
Definition: IPv4Layer.h:209
Definition: IPv4Layer.h:132
bool isLastFragment()
IPProtocolTypes
Definition: IPv4Layer.h:61
Definition: IPv4Layer.h:100
The main namespace for the PcapPlusPlus lib.
std::string toString()
Definition: IPv4Layer.h:146
size_t getHeaderLen()
Definition: IPv4Layer.h:582
OsiModelLayer getOsiModelLayer()
Definition: IPv4Layer.h:595
uint32_t ipDst
Definition: IPv4Layer.h:53
IPv4OptionTypes
Definition: IPv4Layer.h:111
Definition: IPv4Layer.h:102
bool removeAllOptions()