PcapPlusPlus
DhcpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_DHCP_LAYER
2 #define PACKETPP_DHCP_LAYER
3 
4 #include <Layer.h>
5 #include <IpAddress.h>
6 #include <MacAddress.h>
7 #include <string.h>
8 
10 
15 namespace pcpp
16 {
17 
22  #pragma pack(push, 1)
23  struct dhcp_header {
25  uint8_t opCode;
27  uint8_t hardwareType;
31  uint8_t hops;
33  uint32_t transactionID;
35  uint16_t secondsElapsed;
37  uint16_t flags;
39  uint32_t clientIpAddress;
41  uint32_t yourIpAddress;
43  uint32_t serverIpAddress;
45  uint32_t gatewayIpAddress;
47  uint8_t clientHardwareAddress[16];
49  uint8_t serverName[64];
51  uint8_t bootFilename[128];
53  uint32_t magicNumber;
54  };
55  #pragma pack(pop)
56 
57 
62  {
67  };
68 
84  DHCP_ACK = 5,
86  DHCP_NAK = 6,
91  };
92 
304  DHCPOPT_URL = 114,
318  DHCPOPT_CCC = 122,
389  };
390 
391 
397  {
398  public:
400  uint8_t opCode;
402  uint8_t len;
404  uint8_t value[];
405 
415  template<typename T>
416  T getValueAs(int valueOffset = 0)
417  {
418  if (getTotalSize() <= 2*sizeof(uint8_t) + valueOffset)
419  return 0;
420  if (getTotalSize() - 2*sizeof(uint8_t) - valueOffset < sizeof(T))
421  return 0;
422 
423  T result;
424  memcpy(&result, value+valueOffset, sizeof(T));
425  return result;
426  }
427 
433  {
434  uint32_t addrAsInt = getValueAs<uint32_t>();
435  return IPv4Address(addrAsInt);
436  }
437 
442  std::string getValueAsString()
443  {
444  if (len < 1)
445  return "";
446 
447  return std::string((char*)value, len);
448  }
449 
458  template<typename T>
459  void setValue(T newValue, int valueOffset = 0)
460  {
461  memcpy(value+valueOffset, &newValue, sizeof(T));
462  }
463 
471  void setValueIpAddr(const IPv4Address& addr, int valueOffset = 0)
472  {
473  setValue<uint32_t>(addr.toInt(), valueOffset);
474  }
475 
484  void setValueString(const std::string& stringValue, int valueOffset = 0)
485  {
486  std::string val = stringValue;
487  if (stringValue.length() > (size_t)len-(size_t)valueOffset)
488  val = stringValue.substr(0, len-valueOffset);
489 
490  memcpy(value+valueOffset, val.c_str(), val.length());
491  }
492 
497  inline size_t getTotalSize() const
498  {
499  if (opCode == (uint8_t)DHCPOPT_END || opCode == (uint8_t)DHCPOPT_PAD)
500  return sizeof(uint8_t);
501 
502  return sizeof(uint8_t)*2 + (size_t)len;
503  }
504 
508  inline uint8_t getLength()
509  {
510  if (opCode == (uint8_t)DHCPOPT_END || opCode == (uint8_t)DHCPOPT_PAD)
511  return 0;
512 
513  return len;
514  }
515 
520 
521  private:
522 
523  // private c'tor which isn't implemented to make this struct impossible to construct
524  DhcpOptionData();
525  };
526 
527 
532  class DhcpLayer : public Layer
533  {
534  public:
542  DhcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
543 
550  DhcpLayer(DhcpMessageType msgType, const MacAddress& clientMacAddr);
551 
555  DhcpLayer();
556 
560  virtual ~DhcpLayer() {}
561 
566  inline dhcp_header* getDhcpHeader() { return (dhcp_header*)m_Data; };
567 
572 
577 
582  void setClientIpAddress(const IPv4Address& addr);
583 
588 
593  void setServerIpAddress(const IPv4Address& addr);
594 
599 
604  void setYourIpAddress(const IPv4Address& addr);
605 
610 
615  void setGatewayIpAddress(const IPv4Address& addr);
616 
622 
628  void setClientHardwareAddress(const MacAddress& addr);
629 
635 
644  bool setMesageType(DhcpMessageType msgType);
645 
651 
660 
667 
671  size_t getOptionsCount();
672 
683  DhcpOptionData* addOption(DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t* optionData);
684 
697  DhcpOptionData* addOptionAfter(DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t* optionData, DhcpOptionTypes prevOption);
698 
704  bool removeOption(DhcpOptionTypes optionType);
705 
710  bool removeAllOptions();
711 
712  // implement abstract methods
713 
717  void parseNextLayer() {}
718 
722  size_t getHeaderLen();
723 
733  void computeCalculateFields();
734 
735  std::string toString();
736 
738 
739  private:
740 
741  size_t m_DhcpOptionsCount;
742 
743  void initDhcpLayer(size_t numOfBytesToAllocate);
744 
745  DhcpOptionData* castPtrToOptionData(uint8_t* ptr);
746 
747  DhcpOptionData* addOptionAt(DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t* optionData, int offset);
748  };
749 }
750 
751 #endif /* PACKETPP_DHCP_LAYER */
Definition: DhcpLayer.h:222
size_t getHeaderLen()
Definition: DhcpLayer.h:360
DhcpOptionData * addOption(DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t *optionData)
uint32_t serverIpAddress
Definition: DhcpLayer.h:43
Definition: DhcpLayer.h:250
Definition: DhcpLayer.h:176
Definition: DhcpLayer.h:170
dhcp_header * getDhcpHeader()
Definition: DhcpLayer.h:566
Definition: DhcpLayer.h:118
DhcpOptionData * getFirstOptionData()
Definition: DhcpLayer.h:78
Definition: DhcpLayer.h:396
uint32_t toInt() const
uint32_t clientIpAddress
Definition: DhcpLayer.h:39
Definition: DhcpLayer.h:162
Definition: DhcpLayer.h:310
uint8_t value[]
Definition: DhcpLayer.h:404
IPv4Address getServerIpAddress()
Definition: DhcpLayer.h:160
void setValueIpAddr(const IPv4Address &addr, int valueOffset=0)
Definition: DhcpLayer.h:471
uint32_t yourIpAddress
Definition: DhcpLayer.h:41
Definition: DhcpLayer.h:178
Definition: DhcpLayer.h:324
Definition: DhcpLayer.h:126
Definition: DhcpLayer.h:152
Definition: DhcpLayer.h:344
uint8_t getLength()
Definition: DhcpLayer.h:508
uint8_t opCode
Definition: DhcpLayer.h:25
MacAddress getClientHardwareAddress()
bool removeOption(DhcpOptionTypes optionType)
Definition: DhcpLayer.h:278
Definition: DhcpLayer.h:110
void setServerIpAddress(const IPv4Address &addr)
IPv4Address getValueAsIpAddr()
Definition: DhcpLayer.h:432
T getValueAs(int valueOffset=0)
Definition: DhcpLayer.h:416
Definition: DhcpLayer.h:260
IPv4Address getClientIpAddress()
Definition: DhcpLayer.h:296
Definition: DhcpLayer.h:140
Definition: DhcpLayer.h:138
Definition: DhcpLayer.h:314
Definition: DhcpLayer.h:334
DhcpOptionData * getOptionData(DhcpOptionTypes option)
Definition: DhcpLayer.h:74
Definition: DhcpLayer.h:216
Definition: DhcpLayer.h:244
std::string getValueAsString()
Definition: DhcpLayer.h:442
Definition: DhcpLayer.h:90
Definition: ProtocolType.h:218
Definition: DhcpLayer.h:352
Definition: DhcpLayer.h:266
Definition: DhcpLayer.h:270
Definition: DhcpLayer.h:226
Definition: DhcpLayer.h:142
Definition: DhcpLayer.h:202
Definition: DhcpLayer.h:242
Definition: DhcpLayer.h:144
Definition: DhcpLayer.h:122
Definition: DhcpLayer.h:150
Definition: DhcpLayer.h:236
Definition: MacAddress.h:21
Definition: DhcpLayer.h:234
void setYourIpAddress(const IPv4Address &addr)
size_t getOptionsCount()
Definition: IpAddress.h:99
Definition: DhcpLayer.h:302
DhcpOptionTypes
Definition: DhcpLayer.h:96
Definition: DhcpLayer.h:172
Definition: DhcpLayer.h:214
Definition: DhcpLayer.h:220
Definition: DhcpLayer.h:364
OsiModelLayer
Definition: ProtocolType.h:203
IPv4Address getGatewayIpAddress()
Definition: DhcpLayer.h:248
bool setMesageType(DhcpMessageType msgType)
Definition: DhcpLayer.h:76
Definition: DhcpLayer.h:370
BootpOpCodes
Definition: DhcpLayer.h:61
OsiModelLayer getOsiModelLayer()
Definition: DhcpLayer.h:737
Definition: DhcpLayer.h:384
void parseNextLayer()
Definition: DhcpLayer.h:717
IPv4Address getYourIpAddress()
Definition: DhcpLayer.h:532
Definition: DhcpLayer.h:366
Definition: Packet.h:26
Definition: DhcpLayer.h:252
Definition: DhcpLayer.h:256
Definition: DhcpLayer.h:286
uint32_t transactionID
Definition: DhcpLayer.h:33
Definition: DhcpLayer.h:320
Definition: DhcpLayer.h:376
DhcpOptionTypes getType()
Definition: DhcpLayer.h:519
Definition: DhcpLayer.h:268
Definition: DhcpLayer.h:136
Definition: DhcpLayer.h:380
Definition: DhcpLayer.h:114
Definition: DhcpLayer.h:372
DhcpMessageType
Definition: DhcpLayer.h:72
Definition: DhcpLayer.h:146
Definition: DhcpLayer.h:66
Definition: DhcpLayer.h:168
Definition: DhcpLayer.h:356
Definition: DhcpLayer.h:298
Definition: DhcpLayer.h:156
Definition: DhcpLayer.h:346
Definition: DhcpLayer.h:112
Definition: DhcpLayer.h:64
DhcpMessageType getMesageType()
Definition: DhcpLayer.h:180
Definition: DhcpLayer.h:218
Definition: DhcpLayer.h:120
size_t getTotalSize() const
Definition: DhcpLayer.h:497
Definition: DhcpLayer.h:378
Definition: DhcpLayer.h:336
Definition: DhcpLayer.h:198
Definition: DhcpLayer.h:132
uint8_t hardwareAddressLength
Definition: DhcpLayer.h:29
Definition: DhcpLayer.h:254
Definition: DhcpLayer.h:128
uint8_t len
Definition: DhcpLayer.h:402
void setValueString(const std::string &stringValue, int valueOffset=0)
Definition: DhcpLayer.h:484
Definition: DhcpLayer.h:354
uint8_t hops
Definition: DhcpLayer.h:31
Definition: DhcpLayer.h:182
Definition: DhcpLayer.h:294
Definition: DhcpLayer.h:290
Definition: Layer.h:52
Definition: DhcpLayer.h:300
Definition: DhcpLayer.h:232
Definition: DhcpLayer.h:166
Definition: DhcpLayer.h:280
Definition: DhcpLayer.h:350
DhcpOptionData * addOptionAfter(DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t *optionData, DhcpOptionTypes prevOption)
uint8_t serverName[64]
Definition: DhcpLayer.h:49
Definition: DhcpLayer.h:86
Definition: DhcpLayer.h:330
Definition: DhcpLayer.h:84
uint8_t clientHardwareAddress[16]
Definition: DhcpLayer.h:47
void setClientHardwareAddress(const MacAddress &addr)
uint8_t opCode
Definition: DhcpLayer.h:400
Definition: DhcpLayer.h:108
Definition: DhcpLayer.h:358
void setClientIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:374
uint32_t gatewayIpAddress
Definition: DhcpLayer.h:45
Definition: DhcpLayer.h:200
Definition: DhcpLayer.h:204
Definition: DhcpLayer.h:82
Definition: DhcpLayer.h:80
Definition: DhcpLayer.h:348
Definition: DhcpLayer.h:174
Definition: DhcpLayer.h:102
BootpOpCodes getOpCode()
Definition: DhcpLayer.h:571
Definition: DhcpLayer.h:284
void setValue(T newValue, int valueOffset=0)
Definition: DhcpLayer.h:459
Definition: DhcpLayer.h:272
Definition: DhcpLayer.h:338
Definition: DhcpLayer.h:326
Definition: DhcpLayer.h:88
DhcpOptionData * getNextOptionData(DhcpOptionData *dhcpOption)
Definition: DhcpLayer.h:23
uint8_t hardwareType
Definition: DhcpLayer.h:27
void setGatewayIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:382
Definition: DhcpLayer.h:264
Definition: DhcpLayer.h:206
Definition: DhcpLayer.h:362
Definition: DhcpLayer.h:246
Definition: DhcpLayer.h:192
Definition: DhcpLayer.h:212
virtual ~DhcpLayer()
Definition: DhcpLayer.h:560
Definition: DhcpLayer.h:106
Definition: DhcpLayer.h:312
Definition: DhcpLayer.h:158
Definition: DhcpLayer.h:238
Definition: DhcpLayer.h:134
Definition: DhcpLayer.h:184
Definition: DhcpLayer.h:208
Definition: DhcpLayer.h:306
Definition: DhcpLayer.h:322
Definition: DhcpLayer.h:154
The main namespace for the PcapPlusPlus lib.
uint16_t flags
Definition: DhcpLayer.h:37
Definition: DhcpLayer.h:194
Definition: DhcpLayer.h:340
Definition: DhcpLayer.h:188
Definition: DhcpLayer.h:196
Definition: DhcpLayer.h:316
Definition: DhcpLayer.h:262
Definition: DhcpLayer.h:100
Definition: DhcpLayer.h:368
Definition: DhcpLayer.h:124
Definition: DhcpLayer.h:190
Definition: DhcpLayer.h:388
Definition: DhcpLayer.h:230
Definition: DhcpLayer.h:292
Definition: DhcpLayer.h:224
Definition: DhcpLayer.h:116
Definition: DhcpLayer.h:328
Definition: DhcpLayer.h:258
Definition: DhcpLayer.h:308
Definition: DhcpLayer.h:304
Definition: DhcpLayer.h:288
Definition: DhcpLayer.h:228
Definition: DhcpLayer.h:98
Definition: DhcpLayer.h:318
void computeCalculateFields()
std::string toString()
bool removeAllOptions()
Definition: DhcpLayer.h:130
Definition: DhcpLayer.h:148
uint16_t secondsElapsed
Definition: DhcpLayer.h:35
Definition: DhcpLayer.h:104
Definition: DhcpLayer.h:240
Definition: DhcpLayer.h:332
uint32_t magicNumber
Definition: DhcpLayer.h:53
Definition: DhcpLayer.h:386
Definition: DhcpLayer.h:282
uint8_t bootFilename[128]
Definition: DhcpLayer.h:51
Definition: DhcpLayer.h:342