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 "IpAddress.h"
6 #include <string.h>
7 #include <vector>
8 
10 
15 namespace pcpp
16 {
17 
22 #pragma pack(push, 1)
23  struct iphdr {
24 #if (BYTE_ORDER == LITTLE_ENDIAN)
25 
28  ipVersion:4;
29 #else
30 
31  uint8_t ipVersion:4,
34 #endif
35 
36  uint8_t typeOfService;
38  uint16_t totalLength;
40  uint16_t ipId;
42  uint16_t fragmentOffset;
44  uint8_t timeToLive;
46  uint8_t protocol;
48  uint16_t headerChecksum;
50  uint32_t ipSrc;
52  uint32_t ipDst;
53  /*The options start here. */
54  };
55 #pragma pack(pop)
56 
61  {
104  };
105 
106 
111  {
154  };
155 
156 #define PCPP_IP_DONT_FRAGMENT 0x40
157 #define PCPP_IP_MORE_FRAGMENTS 0x20
158 
164  {
165  public:
166 
171  {
180  };
181 
184 
186  std::vector<uint32_t> timestamps;
187 
189  std::vector<IPv4Address> ipAddresses;
190 
194  void clear()
195  {
197  timestamps.clear();
198  ipAddresses.clear();
199  }
200  };
201 
202 
208  {
209  public:
211  uint8_t opCode;
213  uint8_t len;
215  uint8_t value[];
216 
226  template<typename T>
227  T getValueAs(int valueOffset = 0)
228  {
229  if (getTotalSize() <= 2*sizeof(uint8_t) + valueOffset)
230  return 0;
231  if (getTotalSize() - 2*sizeof(uint8_t) - valueOffset < sizeof(T))
232  return 0;
233 
234  T result;
235  memcpy(&result, value+valueOffset, sizeof(T));
236  return result;
237  }
238 
245  std::vector<IPv4Address> getValueAsIpList()
246  {
247  std::vector<IPv4Address> res;
248 
249  size_t dataSize = getDataSize();
250  if (dataSize < 2)
251  return res;
252 
253  uint8_t valueOffset = (uint8_t)(1);
254 
255  while (valueOffset < dataSize)
256  {
257  uint32_t curValue;
258  memcpy(&curValue, value+valueOffset, sizeof(uint32_t));
259  if (curValue == 0)
260  break;
261 
262  res.push_back(IPv4Address(curValue));
263 
264  valueOffset += (uint8_t)(4);
265  }
266 
267  return res;
268  }
269 
278  {
280  res.clear();
281 
282  if (getType() != IPV4OPT_Timestamp)
283  return res;
284 
285  size_t dataSize = getDataSize();
286  if (dataSize < 2)
287  return res;
288 
290 
291  uint8_t valueOffset = (uint8_t)(2);
292  bool readIPAddr = (res.type == IPv4TimestampOptionValue::TimestampAndIP);
293 
294  while (valueOffset < dataSize)
295  {
296  uint32_t curValue;
297  memcpy(&curValue, value+valueOffset, sizeof(uint32_t));
298  if (curValue == 0)
299  break;
300 
301  if (readIPAddr)
302  res.ipAddresses.push_back(IPv4Address(curValue));
303  else
304  res.timestamps.push_back(curValue);
305 
307  readIPAddr = !readIPAddr;
308 
309  valueOffset += (uint8_t)(4);
310  }
311 
312  return res;
313  }
314 
319  size_t getTotalSize() const
320  {
321  if (opCode == (uint8_t)IPV4OPT_EndOfOtionsList || opCode == (uint8_t)IPV4OPT_NOP)
322  return sizeof(uint8_t);
323 
324  return (size_t)len;
325  }
326 
330  size_t getDataSize()
331  {
332  if (opCode == (uint8_t)IPV4OPT_EndOfOtionsList || opCode == (uint8_t)IPV4OPT_NOP)
333  return (size_t)0;
334 
335  return (size_t)len - (2*sizeof(uint8_t));
336  }
337 
342 
343  private:
344 
345  // private c'tor which isn't implemented to make this struct impossible to construct
346  IPv4OptionData();
347  };
348 
349 
350 
355  class IPv4Layer : public Layer
356  {
357  public:
365  IPv4Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
366 
376  IPv4Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, bool setTotalLenAsDataLen);
377 
378 
382  IPv4Layer();
383 
389  IPv4Layer(const IPv4Address& srcIP, const IPv4Address& dstIP);
390 
394  IPv4Layer(const IPv4Layer& other);
395 
399  IPv4Layer& operator=(const IPv4Layer& other);
400 
405  inline iphdr* getIPv4Header() { return (iphdr*)m_Data; };
406 
411  inline IPv4Address getSrcIpAddress() { return IPv4Address(getIPv4Header()->ipSrc); }
412 
417  inline void setSrcIpAddress(const IPv4Address& ipAddr) { getIPv4Header()->ipSrc = ipAddr.toInt(); }
418 
423  inline IPv4Address getDstIpAddress() { return IPv4Address(getIPv4Header()->ipDst); }
424 
429  inline void setDstIpAddress(const IPv4Address& ipAddr) { getIPv4Header()->ipDst = ipAddr.toInt(); }
430 
434  bool isFragment();
435 
440  bool isFirstFragment();
441 
446  bool isLastFragment();
447 
451  uint8_t getFragmentFlags();
452 
456  uint16_t getFragmentOffset();
457 
464 
470 
479 
483  size_t getOptionsCount();
484 
494  IPv4OptionData* addOption(IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t* optionData);
495 
505  IPv4OptionData* addOption(IPv4OptionTypes optionType, const std::vector<IPv4Address>& ipList);
506 
517 
529  IPv4OptionData* addOptionAfter(IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t* optionData, IPv4OptionTypes prevOption = IPV4OPT_Unknown);
530 
542  IPv4OptionData* addOptionAfter(IPv4OptionTypes optionType, const std::vector<IPv4Address>& ipList, IPv4OptionTypes prevOption = IPV4OPT_Unknown);
543 
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  size_t m_OptionCount;
599  int m_NumOfTrailingBytes;
600  int m_TempHeaderExtension;
601 
602  void copyLayerData(const IPv4Layer& other);
603  IPv4OptionData* castPtrToOptionData(uint8_t* ptr);
604  IPv4OptionData* addOptionAt(IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t* optionData, int offset);
605  void adjustOptionsTrailer(size_t totalOptSize);
606  void buildIPListOptionData(const std::vector<IPv4Address>& ipList, uint8_t** optionData, int& optionDataLength);
607  void buildTimestampOptionData(const IPv4TimestampOptionValue& timestampVal, uint8_t** optionData, int& optionDataLength);
608  void incOptionCount(int val);
609  void setOptionCount(int val);
610  void initLayer();
611  void initLayerInPacket(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, bool setTotalLenAsDataLen);
612  };
613 
614 } // namespace pcpp
615 
616 #endif /* PACKETPP_IPV4_LAYER */
uint8_t opCode
Definition: IPv4Layer.h:211
Definition: IPv4Layer.h:91
Definition: IPv4Layer.h:97
Definition: IPv4Layer.h:87
std::vector< IPv4Address > ipAddresses
Definition: IPv4Layer.h:189
Definition: IPv4Layer.h:75
Definition: IPv4Layer.h:67
std::vector< uint32_t > timestamps
Definition: IPv4Layer.h:186
uint8_t ipVersion
Definition: IPv4Layer.h:26
Definition: IPv4Layer.h:151
Definition: IPv4Layer.h:163
uint32_t toInt() const
Definition: IPv4Layer.h:89
T getValueAs(int valueOffset=0)
Definition: IPv4Layer.h:227
IPv4OptionTypes getType()
Definition: IPv4Layer.h:341
Definition: IPv4Layer.h:63
IPv4OptionData * addTimestampOption(const IPv4TimestampOptionValue &timestampValue)
bool isLastFragment()
Definition: IPv4Layer.h:125
Definition: IPv4Layer.h:139
void computeCalculateFields()
uint32_t ipSrc
Definition: IPv4Layer.h:50
Definition: IPv4Layer.h:355
uint8_t typeOfService
Definition: IPv4Layer.h:36
Definition: Packet.h:26
Definition: IPv4Layer.h:23
The main namespace for the PcapPlusPlus lib.
void setDstIpAddress(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:429
size_t getOptionsCount()
IPv4OptionData * addOptionAfter(IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t *optionData, IPv4OptionTypes prevOption=IPV4OPT_Unknown)
bool removeOption(IPv4OptionTypes option)
iphdr * getIPv4Header()
Definition: IPv4Layer.h:405
Definition: IPv4Layer.h:69
Definition: IpAddress.h:113
std::string toString()
uint16_t ipId
Definition: IPv4Layer.h:40
void clear()
Definition: IPv4Layer.h:194
Definition: IPv4Layer.h:85
size_t getHeaderLen()
Definition: IPv4Layer.h:582
Definition: IPv4Layer.h:103
Definition: IPv4Layer.h:179
IPv4OptionData * addOption(IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t *optionData)
IPv4OptionData * getOptionData(IPv4OptionTypes option)
Definition: IPv4Layer.h:77
bool isFirstFragment()
OsiModelLayer
Definition: ProtocolType.h:208
Definition: IPv4Layer.h:119
Definition: IPv4Layer.h:95
Definition: IPv4Layer.h:141
Definition: IPv4Layer.h:101
Definition: IPv4Layer.h:73
uint8_t internetHeaderLength
Definition: IPv4Layer.h:26
uint16_t fragmentOffset
Definition: IPv4Layer.h:42
Definition: IPv4Layer.h:143
size_t getTotalSize() const
Definition: IPv4Layer.h:319
IPv4Address getSrcIpAddress()
Definition: IPv4Layer.h:411
Definition: IPv4Layer.h:81
IPv4Address getDstIpAddress()
Definition: IPv4Layer.h:423
uint16_t headerChecksum
Definition: IPv4Layer.h:48
uint8_t value[]
Definition: IPv4Layer.h:215
void parseNextLayer()
Definition: IPv4Layer.h:113
Definition: IPv4Layer.h:135
uint8_t timeToLive
Definition: IPv4Layer.h:44
Definition: IPv4Layer.h:147
Definition: IPv4Layer.h:121
bool removeAllOptions()
IPProtocolTypes
Definition: IPv4Layer.h:60
IPv4OptionData * getFirstOptionData()
Definition: IPv4Layer.h:137
Definition: IPv4Layer.h:131
uint8_t protocol
Definition: IPv4Layer.h:46
Definition: Layer.h:70
TimestampType type
Definition: IPv4Layer.h:183
Definition: IPv4Layer.h:79
Definition: IPv4Layer.h:127
Definition: IPv4Layer.h:133
Definition: ProtocolType.h:215
Definition: IPv4Layer.h:99
TimestampType
Definition: IPv4Layer.h:170
IPv4TimestampOptionValue getTimestampOptionValue()
Definition: IPv4Layer.h:277
Definition: IPv4Layer.h:83
OsiModelLayer getOsiModelLayer()
Definition: IPv4Layer.h:595
Definition: IPv4Layer.h:93
std::vector< IPv4Address > getValueAsIpList()
Definition: IPv4Layer.h:245
Definition: IPv4Layer.h:145
void setSrcIpAddress(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:417
IPv4OptionData * addTimestampOptionAfter(const IPv4TimestampOptionValue &timestampValue, IPv4OptionTypes prevOption=IPV4OPT_Unknown)
IPv4OptionData * getNextOptionData(IPv4OptionData *option)
uint16_t getFragmentOffset()
Definition: IPv4Layer.h:117
Definition: IPv4Layer.h:207
Definition: IPv4Layer.h:153
uint32_t ipDst
Definition: IPv4Layer.h:52
IPv4Layer & operator=(const IPv4Layer &other)
uint8_t len
Definition: IPv4Layer.h:213
uint8_t getFragmentFlags()
Definition: IPv4Layer.h:115
Definition: IPv4Layer.h:71
Definition: IPv4Layer.h:65
Definition: IPv4Layer.h:149
size_t getDataSize()
Definition: IPv4Layer.h:330
IPv4OptionTypes
Definition: IPv4Layer.h:110
Definition: IPv4Layer.h:129
Definition: IPv4Layer.h:123
uint16_t totalLength
Definition: IPv4Layer.h:38