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) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = IPv4; m_OptionCount = -1; m_NumOfTrailingBytes = 0; m_TempHeaderExtension = 0; }
366 
370  IPv4Layer();
371 
377  IPv4Layer(const IPv4Address& srcIP, const IPv4Address& dstIP);
378 
382  IPv4Layer(const IPv4Layer& other);
383 
387  IPv4Layer& operator=(const IPv4Layer& other);
388 
393  inline iphdr* getIPv4Header() { return (iphdr*)m_Data; };
394 
399  inline IPv4Address getSrcIpAddress() { return IPv4Address(getIPv4Header()->ipSrc); }
400 
405  inline void setSrcIpAddress(const IPv4Address& ipAddr) { getIPv4Header()->ipSrc = ipAddr.toInt(); }
406 
411  inline IPv4Address getDstIpAddress() { return IPv4Address(getIPv4Header()->ipDst); }
412 
417  inline void setDstIpAddress(const IPv4Address& ipAddr) { getIPv4Header()->ipDst = ipAddr.toInt(); }
418 
422  bool isFragment();
423 
428  bool isFirstFragment();
429 
434  bool isLastFragment();
435 
439  uint8_t getFragmentFlags();
440 
444  uint16_t getFragmentOffset();
445 
452 
458 
467 
471  size_t getOptionsCount();
472 
482  IPv4OptionData* addOption(IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t* optionData);
483 
493  IPv4OptionData* addOption(IPv4OptionTypes optionType, const std::vector<IPv4Address>& ipList);
494 
505 
517  IPv4OptionData* addOptionAfter(IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t* optionData, IPv4OptionTypes prevOption = IPV4OPT_Unknown);
518 
530  IPv4OptionData* addOptionAfter(IPv4OptionTypes optionType, const std::vector<IPv4Address>& ipList, IPv4OptionTypes prevOption = IPV4OPT_Unknown);
531 
544 
551  bool removeOption(IPv4OptionTypes option);
552 
557  bool removeAllOptions();
558 
559 
560  // implement abstract methods
561 
565  void parseNextLayer();
566 
570  inline size_t getHeaderLen() { return (size_t)(getIPv4Header()->internetHeaderLength*4) + m_TempHeaderExtension; }
571 
579  void computeCalculateFields();
580 
581  std::string toString();
582 
584 
585  private:
586  size_t m_OptionCount;
587  int m_NumOfTrailingBytes;
588  int m_TempHeaderExtension;
589 
590  void copyLayerData(const IPv4Layer& other);
591  IPv4OptionData* castPtrToOptionData(uint8_t* ptr);
592  IPv4OptionData* addOptionAt(IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t* optionData, int offset);
593  void adjustOptionsTrailer(size_t totalOptSize);
594  void buildIPListOptionData(const std::vector<IPv4Address>& ipList, uint8_t** optionData, int& optionDataLength);
595  void buildTimestampOptionData(const IPv4TimestampOptionValue& timestampVal, uint8_t** optionData, int& optionDataLength);
596  void incOptionCount(int val);
597  void setOptionCount(int val);
598  void initLayer();
599  };
600 
601 } // namespace pcpp
602 
603 #endif /* PACKETPP_IPV4_LAYER */
Definition: IPv4Layer.h:71
Definition: IPv4Layer.h:131
Definition: IPv4Layer.h:99
T getValueAs(int valueOffset=0)
Definition: IPv4Layer.h:227
Definition: IPv4Layer.h:143
Definition: IPv4Layer.h:137
Definition: IPv4Layer.h:103
void clear()
Definition: IPv4Layer.h:194
uint32_t toInt() const
Definition: IPv4Layer.h:81
Definition: IPv4Layer.h:123
uint16_t totalLength
Definition: IPv4Layer.h:38
Definition: IPv4Layer.h:95
IPv4OptionData * getNextOptionData(IPv4OptionData *option)
size_t getTotalSize() const
Definition: IPv4Layer.h:319
bool isLastFragment()
uint8_t value[]
Definition: IPv4Layer.h:215
std::vector< IPv4Address > getValueAsIpList()
Definition: IPv4Layer.h:245
Definition: ProtocolType.h:31
Definition: IPv4Layer.h:135
uint8_t timeToLive
Definition: IPv4Layer.h:44
IPv4OptionTypes
Definition: IPv4Layer.h:110
uint16_t fragmentOffset
Definition: IPv4Layer.h:42
Definition: IPv4Layer.h:127
IPv4OptionData * addTimestampOption(const IPv4TimestampOptionValue &timestampValue)
uint8_t ipVersion
Definition: IPv4Layer.h:26
Definition: IPv4Layer.h:153
std::string toString()
std::vector< uint32_t > timestamps
Definition: IPv4Layer.h:186
TimestampType type
Definition: IPv4Layer.h:183
Definition: IPv4Layer.h:115
Definition: IPv4Layer.h:101
Definition: ProtocolType.h:210
bool isFirstFragment()
iphdr * getIPv4Header()
Definition: IPv4Layer.h:393
Definition: IPv4Layer.h:65
IPv4Layer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: IPv4Layer.h:365
Definition: IpAddress.h:99
Definition: IPv4Layer.h:63
IPv4OptionData * getFirstOptionData()
Definition: IPv4Layer.h:83
Definition: IPv4Layer.h:147
Definition: IPv4Layer.h:93
Definition: IPv4Layer.h:125
uint8_t internetHeaderLength
Definition: IPv4Layer.h:26
Definition: IPv4Layer.h:75
Definition: IPv4Layer.h:141
Definition: IPv4Layer.h:113
OsiModelLayer
Definition: ProtocolType.h:203
Definition: IPv4Layer.h:87
IPv4OptionData * getOptionData(IPv4OptionTypes option)
Definition: IPv4Layer.h:121
uint32_t ipDst
Definition: IPv4Layer.h:52
OsiModelLayer getOsiModelLayer()
Definition: IPv4Layer.h:583
Definition: IPv4Layer.h:149
uint8_t protocol
Definition: IPv4Layer.h:46
Definition: Packet.h:26
size_t getOptionsCount()
Definition: IPv4Layer.h:97
void setSrcIpAddress(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:405
IPv4Address getDstIpAddress()
Definition: IPv4Layer.h:411
Definition: IPv4Layer.h:77
IPv4OptionData * addTimestampOptionAfter(const IPv4TimestampOptionValue &timestampValue, IPv4OptionTypes prevOption=IPV4OPT_Unknown)
Definition: IPv4Layer.h:179
bool removeAllOptions()
std::vector< IPv4Address > ipAddresses
Definition: IPv4Layer.h:189
Definition: IPv4Layer.h:151
Definition: Layer.h:52
uint8_t typeOfService
Definition: IPv4Layer.h:36
IPv4OptionData * addOptionAfter(IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t *optionData, IPv4OptionTypes prevOption=IPV4OPT_Unknown)
bool removeOption(IPv4OptionTypes option)
Definition: IPv4Layer.h:145
uint16_t headerChecksum
Definition: IPv4Layer.h:48
TimestampType
Definition: IPv4Layer.h:170
Definition: IPv4Layer.h:23
Definition: IPv4Layer.h:139
uint8_t len
Definition: IPv4Layer.h:213
Definition: IPv4Layer.h:67
IPv4Layer & operator=(const IPv4Layer &other)
uint32_t ipSrc
Definition: IPv4Layer.h:50
Definition: IPv4Layer.h:133
Definition: IPv4Layer.h:163
IPProtocolTypes
Definition: IPv4Layer.h:60
void computeCalculateFields()
Definition: IPv4Layer.h:91
Definition: IPv4Layer.h:89
Definition: IPv4Layer.h:355
Definition: IPv4Layer.h:79
IPv4OptionData * addOption(IPv4OptionTypes optionType, uint8_t optionDataLength, const uint8_t *optionData)
IPv4Address getSrcIpAddress()
Definition: IPv4Layer.h:399
Definition: IPv4Layer.h:117
size_t getDataSize()
Definition: IPv4Layer.h:330
Definition: IPv4Layer.h:129
uint16_t getFragmentOffset()
Definition: IPv4Layer.h:119
The main namespace for the PcapPlusPlus lib.
IPv4TimestampOptionValue getTimestampOptionValue()
Definition: IPv4Layer.h:277
IPv4OptionTypes getType()
Definition: IPv4Layer.h:341
Definition: IPv4Layer.h:69
void setDstIpAddress(const IPv4Address &ipAddr)
Definition: IPv4Layer.h:417
uint16_t ipId
Definition: IPv4Layer.h:40
void parseNextLayer()
uint8_t opCode
Definition: IPv4Layer.h:211
Definition: IPv4Layer.h:85
uint8_t getFragmentFlags()
Definition: IPv4Layer.h:73
size_t getHeaderLen()
Definition: IPv4Layer.h:570
Definition: IPv4Layer.h:207