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 
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  {
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 
275  return res;
276 
277  size_t dataSize = getDataSize();
278  if (dataSize < 2)
279  return res;
280 
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 
464 
469  void setSrcIpAddress(const IPv4Address& ipAddr) { getIPv4Header()->ipSrc = ipAddr.toInt(); }
470 
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 */
pcpp::IPv4Layer::isFragment
bool isFragment() const
pcpp::PACKETPP_IPPROTO_EGP
@ PACKETPP_IPPROTO_EGP
Definition: IPv4Layer.h:76
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:233
pcpp::TLVRecord
Definition: TLVData.h:23
pcpp::IPV4OPT_Unknown
@ IPV4OPT_Unknown
Definition: IPv4Layer.h:154
pcpp::PACKETPP_IPPROTO_IGMP
@ PACKETPP_IPPROTO_IGMP
Definition: IPv4Layer.h:70
pcpp::IPv4Address
Definition: IpAddress.h:26
pcpp::Packet
Definition: Packet.h:26
pcpp::IPv4TimestampOptionValue::ipAddresses
std::vector< IPv4Address > ipAddresses
Definition: IPv4Layer.h:188
pcpp::IPV4OPT_NOP
@ IPV4OPT_NOP
Definition: IPv4Layer.h:116
pcpp::IPv4TimestampOptionValue::TimestampsForPrespecifiedIPs
@ TimestampsForPrespecifiedIPs
Definition: IPv4Layer.h:176
pcpp::IPv4Layer::getFirstOption
IPv4Option getFirstOption() const
pcpp::PACKETPP_IPPROTO_ICMPV6
@ PACKETPP_IPPROTO_ICMPV6
Definition: IPv4Layer.h:96
pcpp::PACKETPP_IPPROTO_NONE
@ PACKETPP_IPPROTO_NONE
Definition: IPv4Layer.h:98
pcpp::iphdr::fragmentOffset
uint16_t fragmentOffset
Definition: IPv4Layer.h:43
pcpp::IPv4OptionTypes
IPv4OptionTypes
Definition: IPv4Layer.h:111
pcpp::IPv4Option::getTotalSize
size_t getTotalSize() const
Definition: IPv4Layer.h:321
pcpp::IPv4Layer::removeOption
bool removeOption(IPv4OptionTypes option)
pcpp::IPV4OPT_Security
@ IPV4OPT_Security
Definition: IPv4Layer.h:130
pcpp::IPV4OPT_StreamID
@ IPV4OPT_StreamID
Definition: IPv4Layer.h:138
pcpp::IPv4TimestampOptionValue::TimestampType
TimestampType
Definition: IPv4Layer.h:169
pcpp::IPV4OPT_Timestamp
@ IPV4OPT_Timestamp
Definition: IPv4Layer.h:126
pcpp::IPv4Layer::toString
std::string toString() const
pcpp::IPV4OPT_SelectiveDirectedBroadcast
@ IPV4OPT_SelectiveDirectedBroadcast
Definition: IPv4Layer.h:148
pcpp::IPV4OPT_UpstreamMulticastPkt
@ IPV4OPT_UpstreamMulticastPkt
Definition: IPv4Layer.h:152
Layer.h
pcpp::IPv4TimestampOptionValue::clear
void clear()
Definition: IPv4Layer.h:196
pcpp::TLVRecord::TLVRawData::recordType
uint8_t recordType
Definition: TLVData.h:31
pcpp::IPv4Layer::setDstIpAddress
void setDstIpAddress(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:481
pcpp::IPv4Layer::getOptionCount
size_t getOptionCount() const
pcpp::IPv4Layer::getOsiModelLayer
OsiModelLayer getOsiModelLayer() const
Definition: IPv4Layer.h:596
pcpp::IPv4TimestampOptionValue
Definition: IPv4Layer.h:164
pcpp::iphdr::protocol
uint8_t protocol
Definition: IPv4Layer.h:47
pcpp::IPV4OPT_StrictSourceRoute
@ IPV4OPT_StrictSourceRoute
Definition: IPv4Layer.h:140
pcpp::IPv4Layer::isLastFragment
bool isLastFragment() const
pcpp::IPv4Option::getValueAsIpList
std::vector< IPv4Address > getValueAsIpList() const
Definition: IPv4Layer.h:231
pcpp::IPv4Layer
Definition: IPv4Layer.h:405
pcpp::IPV4OPT_QuickStart
@ IPV4OPT_QuickStart
Definition: IPv4Layer.h:124
pcpp::iphdr::headerChecksum
uint16_t headerChecksum
Definition: IPv4Layer.h:49
pcpp::IPv4Layer::getFragmentOffset
uint16_t getFragmentOffset() const
pcpp::IPv4Layer::computeCalculateFields
void computeCalculateFields()
pcpp::IPv4OptionBuilder::IPv4OptionBuilder
IPv4OptionBuilder(IPv4OptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: IPv4Layer.h:366
pcpp::OsiModelNetworkLayer
@ OsiModelNetworkLayer
Definition: ProtocolType.h:240
pcpp::PACKETPP_IPPROTO_UDP
@ PACKETPP_IPPROTO_UDP
Definition: IPv4Layer.h:80
pcpp::IPV4OPT_RouterAlert
@ IPV4OPT_RouterAlert
Definition: IPv4Layer.h:146
pcpp::IPv4TimestampOptionValue::timestamps
std::vector< uint32_t > timestamps
Definition: IPv4Layer.h:185
pcpp::IPV4OPT_EndOfOptionsList
@ IPV4OPT_EndOfOptionsList
Definition: IPv4Layer.h:114
pcpp::IPv4TimestampOptionValue::type
TimestampType type
Definition: IPv4Layer.h:182
pcpp::PACKETPP_IPPROTO_ICMP
@ PACKETPP_IPPROTO_ICMP
Definition: IPv4Layer.h:68
TLVData.h
pcpp::IPv4Option::getIPv4OptionType
IPv4OptionTypes getIPv4OptionType() const
Definition: IPv4Layer.h:310
pcpp::PACKETPP_IPPROTO_ROUTING
@ PACKETPP_IPPROTO_ROUTING
Definition: IPv4Layer.h:86
pcpp::IPv4TimestampOptionValue::Unknown
@ Unknown
Definition: IPv4Layer.h:178
pcpp::IPv4Layer::parseNextLayer
void parseNextLayer()
pcpp::PACKETPP_IPPROTO_GRE
@ PACKETPP_IPPROTO_GRE
Definition: IPv4Layer.h:90
pcpp::IPv4Layer::isDataValid
static bool isDataValid(const uint8_t *data, size_t dataLen)
Definition: IPv4Layer.h:622
pcpp::IPv4TimestampOptionValue::TimestampAndIP
@ TimestampAndIP
Definition: IPv4Layer.h:174
pcpp::IPv4Layer::getDstIpAddress
IPv4Address getDstIpAddress() const
Definition: IPv4Layer.h:475
pcpp::IPV4OPT_RecordRoute
@ IPV4OPT_RecordRoute
Definition: IPv4Layer.h:118
pcpp::PACKETPP_IPPROTO_FRAGMENT
@ PACKETPP_IPPROTO_FRAGMENT
Definition: IPv4Layer.h:88
pcpp::IPv4Option::getDataSize
size_t getDataSize() const
Definition: IPv4Layer.h:332
pcpp::iphdr::ipSrc
uint32_t ipSrc
Definition: IPv4Layer.h:51
pcpp::IPProtocolTypes
IPProtocolTypes
Definition: IPv4Layer.h:61
pcpp::IPv4Layer::getHeaderLen
size_t getHeaderLen() const
Definition: IPv4Layer.h:583
pcpp::IPv4Layer::getOption
IPv4Option getOption(IPv4OptionTypes option) const
pcpp::PACKETPP_IPPROTO_IDP
@ PACKETPP_IPPROTO_IDP
Definition: IPv4Layer.h:82
pcpp::IPv4Address::toInt
uint32_t toInt() const
Definition: IpAddress.h:125
pcpp::IPv4OptionBuilder
Definition: IPv4Layer.h:350
pcpp::PACKETPP_IPPROTO_AH
@ PACKETPP_IPPROTO_AH
Definition: IPv4Layer.h:94
pcpp::IPV4OPT_DynamicPacketState
@ IPV4OPT_DynamicPacketState
Definition: IPv4Layer.h:150
pcpp::IPv4TimestampOptionValue::TimestampOnly
@ TimestampOnly
Definition: IPv4Layer.h:172
pcpp::IPV4OPT_MTUProbe
@ IPV4OPT_MTUProbe
Definition: IPv4Layer.h:120
pcpp::PACKETPP_IPPROTO_IPIP
@ PACKETPP_IPPROTO_IPIP
Definition: IPv4Layer.h:72
pcpp::IPv4Option::IPv4Option
IPv4Option(uint8_t *optionRawData)
Definition: IPv4Layer.h:218
pcpp::iphdr
Definition: IPv4Layer.h:24
pcpp::IPV4OPT_Traceroute
@ IPV4OPT_Traceroute
Definition: IPv4Layer.h:128
pcpp::IPv4Layer::addOption
IPv4Option addOption(const IPv4OptionBuilder &optionBuilder)
pcpp::iphdr::totalLength
uint16_t totalLength
Definition: IPv4Layer.h:39
pcpp::IPv4Option::~IPv4Option
~IPv4Option()
Definition: IPv4Layer.h:223
pcpp::IPv4TimestampOptionValue::IPv4TimestampOptionValue
IPv4TimestampOptionValue()
Definition: IPv4Layer.h:191
pcpp::IPv4Layer::isFirstFragment
bool isFirstFragment() const
pcpp::IPv4Layer::getFragmentFlags
uint8_t getFragmentFlags() const
pcpp::IPv4Option
Definition: IPv4Layer.h:210
IpAddress.h
pcpp::IPV4OPT_CommercialSecurity
@ IPV4OPT_CommercialSecurity
Definition: IPv4Layer.h:136
pcpp::IPv4Layer::getIPv4Header
iphdr * getIPv4Header() const
Definition: IPv4Layer.h:457
pcpp::iphdr::internetHeaderLength
uint8_t internetHeaderLength
Definition: IPv4Layer.h:27
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::TLVRecordBuilder
Definition: TLVData.h:351
pcpp::IPv4Layer::operator=
IPv4Layer & operator=(const IPv4Layer &other)
pcpp::iphdr::ipDst
uint32_t ipDst
Definition: IPv4Layer.h:53
pcpp::PACKETPP_IPPROTO_PUP
@ PACKETPP_IPPROTO_PUP
Definition: IPv4Layer.h:78
pcpp::PACKETPP_IPPROTO_DSTOPTS
@ PACKETPP_IPPROTO_DSTOPTS
Definition: IPv4Layer.h:100
pcpp::Layer
Definition: Layer.h:70
pcpp::IPv4Layer::removeAllOptions
bool removeAllOptions()
pcpp::TLVRecordReader
Definition: TLVData.h:197
pcpp::iphdr::timeToLive
uint8_t timeToLive
Definition: IPv4Layer.h:45
pcpp::iphdr::typeOfService
uint8_t typeOfService
Definition: IPv4Layer.h:37
pcpp::TLVRecord::TLVRawData::recordValue
uint8_t recordValue[]
Definition: TLVData.h:35
pcpp::PACKETPP_IPPROTO_ESP
@ PACKETPP_IPPROTO_ESP
Definition: IPv4Layer.h:92
pcpp::PACKETPP_IPPROTO_IPV6
@ PACKETPP_IPPROTO_IPV6
Definition: IPv4Layer.h:84
pcpp::IPv4Option::getTimestampOptionValue
IPv4TimestampOptionValue getTimestampOptionValue() const
Definition: IPv4Layer.h:266
pcpp::PACKETPP_IPPROTO_IP
@ PACKETPP_IPPROTO_IP
Definition: IPv4Layer.h:64
pcpp::iphdr::ipId
uint16_t ipId
Definition: IPv4Layer.h:41
pcpp::IPv4Layer::setSrcIpAddress
void setSrcIpAddress(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:469
pcpp::IPV4OPT_AddressExtension
@ IPV4OPT_AddressExtension
Definition: IPv4Layer.h:144
pcpp::PACKETPP_IPPROTO_HOPOPTS
@ PACKETPP_IPPROTO_HOPOPTS
Definition: IPv4Layer.h:66
pcpp::PACKETPP_IPPROTO_TCP
@ PACKETPP_IPPROTO_TCP
Definition: IPv4Layer.h:74
pcpp::IPv4OptionBuilder::IPv4OptionBuilder
IPv4OptionBuilder(IPv4OptionTypes optionType, uint16_t optionValue)
Definition: IPv4Layer.h:375
pcpp::PACKETPP_IPPROTO_MAX
@ PACKETPP_IPPROTO_MAX
Definition: IPv4Layer.h:104
pcpp::IPV4OPT_ExtendedSecurity
@ IPV4OPT_ExtendedSecurity
Definition: IPv4Layer.h:134
pcpp::TLVRecord::TLVRawData::recordLen
uint8_t recordLen
Definition: TLVData.h:33
pcpp::IPV4OPT_ExtendedInternetProtocol
@ IPV4OPT_ExtendedInternetProtocol
Definition: IPv4Layer.h:142
pcpp::IPV4OPT_MTUReply
@ IPV4OPT_MTUReply
Definition: IPv4Layer.h:122
pcpp::IPv4Layer::getNextOption
IPv4Option getNextOption(IPv4Option &option) const
pcpp::PACKETPP_IPPROTO_RAW
@ PACKETPP_IPPROTO_RAW
Definition: IPv4Layer.h:102
pcpp::IPv4Layer::IPv4Layer
IPv4Layer()
pcpp::IPv4Layer::getSrcIpAddress
IPv4Address getSrcIpAddress() const
Definition: IPv4Layer.h:463
pcpp::IPv4Layer::addOptionAfter
IPv4Option addOptionAfter(const IPv4OptionBuilder &optionBuilder, IPv4OptionTypes prevOptionType=IPV4OPT_Unknown)
pcpp::IPv4OptionBuilder::build
IPv4Option build() const
pcpp::iphdr::ipVersion
uint8_t ipVersion
Definition: IPv4Layer.h:29
pcpp::IPV4OPT_LooseSourceRoute
@ IPV4OPT_LooseSourceRoute
Definition: IPv4Layer.h:132