PcapPlusPlus  23.09
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 "TLVData.h"
6 #include "IpAddress.h"
7 #include "MacAddress.h"
8 #include <string.h>
9 
11 
16 namespace pcpp
17 {
18 
23  #pragma pack(push, 1)
24  struct dhcp_header
25  {
27  uint8_t opCode;
29  uint8_t hardwareType;
33  uint8_t hops;
35  uint32_t transactionID;
37  uint16_t secondsElapsed;
39  uint16_t flags;
41  uint32_t clientIpAddress;
43  uint32_t yourIpAddress;
45  uint32_t serverIpAddress;
47  uint32_t gatewayIpAddress;
49  uint8_t clientHardwareAddress[16];
51  uint8_t serverName[64];
53  uint8_t bootFilename[128];
55  uint32_t magicNumber;
56  };
57  #pragma pack(pop)
58 
59 
64  {
69  };
70 
75  {
87  DHCP_ACK = 5,
89  DHCP_NAK = 6,
94  };
95 
100  {
308  DHCPOPT_URL = 114,
322  DHCPOPT_CCC = 122,
393  };
394 
395 
401  class DhcpOption : public TLVRecord<uint8_t, uint8_t>
402  {
403  public:
404 
409  explicit DhcpOption(uint8_t* optionRawData) : TLVRecord(optionRawData) { }
410 
414  virtual ~DhcpOption() { }
415 
421  {
422  return getValueAs<uint32_t>();
423  }
424 
432  void setValueIpAddr(const IPv4Address& addr, int valueOffset = 0)
433  {
434  setValue<uint32_t>(addr.toInt(), valueOffset);
435  }
436 
444  std::string getValueAsString(int valueOffset = 0) const
445  {
446  if (m_Data == nullptr || m_Data->recordLen - valueOffset < 1)
447  return "";
448 
449  return std::string((const char*)m_Data->recordValue + valueOffset, (int)m_Data->recordLen - valueOffset);
450  }
451 
460  void setValueString(const std::string& stringValue, int valueOffset = 0)
461  {
462  // calculate the maximum length of the destination buffer
463  size_t len = (size_t)m_Data->recordLen - (size_t)valueOffset;
464 
465  // use the length of input string if a buffer is large enough for whole string
466  if (stringValue.length() < len)
467  len = stringValue.length();
468 
469  memcpy(m_Data->recordValue + valueOffset, stringValue.data(), len);
470  }
471 
472 
473  // implement abstract methods
474 
475  size_t getTotalSize() const
476  {
477  if (m_Data == nullptr)
478  return 0;
479 
480  if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
481  return sizeof(uint8_t);
482 
483  return sizeof(uint8_t) * 2 + (size_t)m_Data->recordLen;
484  }
485 
486  size_t getDataSize() const
487  {
488  if (m_Data == nullptr)
489  return 0;
490 
491  if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
492  return 0;
493 
494  return m_Data->recordLen;
495  }
496  };
497 
498 
505  {
506  public:
507 
515  DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen) :
516  TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) { }
517 
524  DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue) :
525  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
526 
533  DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue) :
534  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
535 
542  DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue) :
543  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
544 
551  DhcpOptionBuilder(DhcpOptionTypes optionType, const IPv4Address& optionValue) :
552  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
553 
560  DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string& optionValue) :
561  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
562 
568  TLVRecordBuilder(other) { }
569 
576  {
577  TLVRecordBuilder::operator=(other);
578  return *this;
579  }
580 
585  DhcpOption build() const;
586  };
587 
588 
589 
594  class DhcpLayer : public Layer
595  {
596  public:
597 
605  DhcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
606 
613  DhcpLayer(DhcpMessageType msgType, const MacAddress& clientMacAddr);
614 
618  DhcpLayer();
619 
623  virtual ~DhcpLayer() {}
624 
629  dhcp_header* getDhcpHeader() const { return (dhcp_header*)m_Data; }
630 
634  BootpOpCodes getOpCode() const { return (BootpOpCodes)getDhcpHeader()->opCode; }
635 
639  IPv4Address getClientIpAddress() const { return getDhcpHeader()->clientIpAddress; }
640 
645  void setClientIpAddress(const IPv4Address& addr) { getDhcpHeader()->clientIpAddress = addr.toInt(); }
646 
650  IPv4Address getServerIpAddress() const { return getDhcpHeader()->serverIpAddress; }
651 
656  void setServerIpAddress(const IPv4Address& addr) { getDhcpHeader()->serverIpAddress = addr.toInt(); }
657 
661  IPv4Address getYourIpAddress() const { return getDhcpHeader()->yourIpAddress; }
662 
667  void setYourIpAddress(const IPv4Address& addr) { getDhcpHeader()->yourIpAddress = addr.toInt(); }
668 
672  IPv4Address getGatewayIpAddress() const { return getDhcpHeader()->gatewayIpAddress; }
673 
678  void setGatewayIpAddress(const IPv4Address& addr) { getDhcpHeader()->gatewayIpAddress = addr.toInt(); }
679 
684  MacAddress getClientHardwareAddress() const;
685 
691  void setClientHardwareAddress(const MacAddress& addr);
692 
697  DhcpMessageType getMessageType() const;
698 
707  bool setMessageType(DhcpMessageType msgType);
708 
713  DhcpOption getFirstOptionData() const;
714 
722  DhcpOption getNextOptionData(DhcpOption dhcpOption) const;
723 
730  DhcpOption getOptionData(DhcpOptionTypes option) const;
731 
735  size_t getOptionsCount() const;
736 
743  DhcpOption addOption(const DhcpOptionBuilder& optionBuilder);
744 
752  DhcpOption addOptionAfter(const DhcpOptionBuilder& optionBuilder, DhcpOptionTypes prevOption);
753 
759  bool removeOption(DhcpOptionTypes optionType);
760 
765  bool removeAllOptions();
766 
767  // implement abstract methods
768 
772  void parseNextLayer() {}
773 
777  size_t getHeaderLen() const { return m_DataLen; }
778 
788  void computeCalculateFields();
789 
790  std::string toString() const;
791 
793 
794  private:
795 
796  uint8_t* getOptionsBasePtr() const { return m_Data + sizeof(dhcp_header); }
797 
798  TLVRecordReader<DhcpOption> m_OptionReader;
799 
800  void initDhcpLayer(size_t numOfBytesToAllocate);
801 
802  DhcpOption addOptionAt(const DhcpOptionBuilder& optionBuilder, int offset);
803  };
804 }
805 
806 #endif /* PACKETPP_DHCP_LAYER */
Definition: DhcpLayer.h:156
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
Definition: DhcpLayer.h:276
Definition: DhcpLayer.h:300
Definition: DhcpLayer.h:326
Definition: DhcpLayer.h:172
Definition: DhcpLayer.h:332
Definition: DhcpLayer.h:230
IPv4Address getValueAsIpAddr() const
Definition: DhcpLayer.h:420
Definition: DhcpLayer.h:216
Definition: DhcpLayer.h:146
uint32_t magicNumber
Definition: DhcpLayer.h:55
Definition: DhcpLayer.h:264
Definition: DhcpLayer.h:338
std::string getValueAsString(int valueOffset=0) const
Definition: DhcpLayer.h:444
Definition: DhcpLayer.h:302
Definition: DhcpLayer.h:110
Definition: DhcpLayer.h:360
Definition: DhcpLayer.h:210
Definition: DhcpLayer.h:362
Definition: DhcpLayer.h:266
DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue)
Definition: DhcpLayer.h:524
Definition: DhcpLayer.h:144
DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue)
Definition: DhcpLayer.h:533
Definition: DhcpLayer.h:370
Definition: DhcpLayer.h:378
Definition: DhcpLayer.h:83
Definition: DhcpLayer.h:386
Definition: DhcpLayer.h:91
Definition: ProtocolType.h:348
Definition: DhcpLayer.h:158
DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: DhcpLayer.h:515
uint8_t opCode
Definition: DhcpLayer.h:27
Definition: DhcpLayer.h:282
Definition: TLVData.h:376
Definition: DhcpLayer.h:68
Definition: Layer.h:70
Definition: DhcpLayer.h:130
Definition: DhcpLayer.h:320
uint32_t gatewayIpAddress
Definition: DhcpLayer.h:47
Definition: DhcpLayer.h:366
Definition: TLVData.h:24
Definition: DhcpLayer.h:220
void setGatewayIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:678
Definition: DhcpLayer.h:228
Definition: DhcpLayer.h:87
Definition: Packet.h:26
Definition: DhcpLayer.h:318
Definition: DhcpLayer.h:206
uint32_t yourIpAddress
Definition: DhcpLayer.h:43
IPv4Address getGatewayIpAddress() const
Definition: DhcpLayer.h:672
Definition: DhcpLayer.h:401
Definition: DhcpLayer.h:358
uint8_t serverName[64]
Definition: DhcpLayer.h:51
Definition: DhcpLayer.h:388
Definition: DhcpLayer.h:150
void setYourIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:667
Definition: DhcpLayer.h:344
Definition: DhcpLayer.h:120
Definition: DhcpLayer.h:322
Definition: DhcpLayer.h:346
uint8_t hops
Definition: DhcpLayer.h:33
Definition: DhcpLayer.h:384
Definition: DhcpLayer.h:314
Definition: DhcpLayer.h:246
Definition: DhcpLayer.h:250
void setValueIpAddr(const IPv4Address &addr, int valueOffset=0)
Definition: DhcpLayer.h:432
Definition: DhcpLayer.h:126
Definition: DhcpLayer.h:81
Definition: DhcpLayer.h:268
Definition: DhcpLayer.h:382
DhcpOptionTypes
Definition: DhcpLayer.h:99
Definition: DhcpLayer.h:336
Definition: DhcpLayer.h:364
Definition: DhcpLayer.h:354
Definition: DhcpLayer.h:284
Definition: DhcpLayer.h:234
Definition: TLVData.h:217
void parseNextLayer()
Definition: DhcpLayer.h:772
Definition: DhcpLayer.h:182
Definition: DhcpLayer.h:288
Definition: DhcpLayer.h:270
size_t getTotalSize() const
Definition: DhcpLayer.h:475
Definition: DhcpLayer.h:160
Definition: DhcpLayer.h:77
Definition: DhcpLayer.h:594
Definition: DhcpLayer.h:154
Definition: DhcpLayer.h:148
Definition: DhcpLayer.h:152
Definition: DhcpLayer.h:392
Definition: DhcpLayer.h:204
Definition: DhcpLayer.h:176
uint16_t flags
Definition: DhcpLayer.h:39
Definition: DhcpLayer.h:244
IPv4Address getYourIpAddress() const
Definition: DhcpLayer.h:661
Definition: DhcpLayer.h:260
Definition: DhcpLayer.h:356
Definition: DhcpLayer.h:114
Definition: DhcpLayer.h:376
Definition: DhcpLayer.h:24
BootpOpCodes getOpCode() const
Definition: DhcpLayer.h:634
Definition: DhcpLayer.h:208
Definition: DhcpLayer.h:294
DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string &optionValue)
Definition: DhcpLayer.h:560
Definition: DhcpLayer.h:202
Definition: DhcpLayer.h:374
BootpOpCodes
Definition: DhcpLayer.h:63
Definition: DhcpLayer.h:238
Definition: DhcpLayer.h:162
uint16_t secondsElapsed
Definition: DhcpLayer.h:37
void setClientIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:645
dhcp_header * getDhcpHeader() const
Definition: DhcpLayer.h:629
Definition: IpAddress.h:42
Definition: DhcpLayer.h:272
Definition: DhcpLayer.h:106
Definition: DhcpLayer.h:124
virtual ~DhcpOption()
Definition: DhcpLayer.h:414
DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue)
Definition: DhcpLayer.h:542
Definition: DhcpLayer.h:298
Definition: DhcpLayer.h:142
Definition: DhcpLayer.h:198
Definition: DhcpLayer.h:104
Definition: DhcpLayer.h:258
size_t getHeaderLen() const
Definition: DhcpLayer.h:777
Definition: DhcpLayer.h:108
void setServerIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:656
Definition: DhcpLayer.h:132
Definition: DhcpLayer.h:312
Definition: DhcpLayer.h:242
Definition: DhcpLayer.h:352
Definition: DhcpLayer.h:200
Definition: DhcpLayer.h:226
Definition: DhcpLayer.h:372
Definition: DhcpLayer.h:188
DhcpMessageType
Definition: DhcpLayer.h:74
Definition: DhcpLayer.h:184
Definition: DhcpLayer.h:328
DhcpOptionBuilder(DhcpOptionTypes optionType, const IPv4Address &optionValue)
Definition: DhcpLayer.h:551
Definition: DhcpLayer.h:138
IPv4Address getClientIpAddress() const
Definition: DhcpLayer.h:639
Definition: DhcpLayer.h:224
Definition: DhcpLayer.h:252
Definition: DhcpLayer.h:390
Definition: DhcpLayer.h:66
uint32_t clientIpAddress
Definition: DhcpLayer.h:41
DhcpOptionBuilder(const DhcpOptionBuilder &other)
Definition: DhcpLayer.h:567
Definition: DhcpLayer.h:232
Definition: DhcpLayer.h:140
Definition: DhcpLayer.h:122
Definition: DhcpLayer.h:306
Definition: DhcpLayer.h:178
Definition: DhcpLayer.h:192
Definition: DhcpLayer.h:340
Definition: DhcpLayer.h:324
uint8_t hardwareAddressLength
Definition: DhcpLayer.h:31
Definition: DhcpLayer.h:212
Definition: DhcpLayer.h:85
uint8_t hardwareType
Definition: DhcpLayer.h:29
Definition: DhcpLayer.h:296
Definition: DhcpLayer.h:504
Definition: DhcpLayer.h:348
Definition: DhcpLayer.h:290
Definition: DhcpLayer.h:240
Definition: DhcpLayer.h:118
uint32_t serverIpAddress
Definition: DhcpLayer.h:45
uint32_t transactionID
Definition: DhcpLayer.h:35
Definition: DhcpLayer.h:368
Definition: DhcpLayer.h:128
Definition: DhcpLayer.h:310
Definition: DhcpLayer.h:248
uint8_t bootFilename[128]
Definition: DhcpLayer.h:53
size_t getDataSize() const
Definition: DhcpLayer.h:486
Definition: DhcpLayer.h:380
Definition: DhcpLayer.h:112
Definition: DhcpLayer.h:256
Definition: DhcpLayer.h:180
Definition: DhcpLayer.h:316
Definition: DhcpLayer.h:136
Definition: DhcpLayer.h:134
Definition: DhcpLayer.h:262
Definition: DhcpLayer.h:218
OsiModelLayer getOsiModelLayer() const
Definition: DhcpLayer.h:792
virtual ~DhcpLayer()
Definition: DhcpLayer.h:623
Definition: DhcpLayer.h:93
void setValueString(const std::string &stringValue, int valueOffset=0)
Definition: DhcpLayer.h:460
Definition: DhcpLayer.h:170
Definition: DhcpLayer.h:194
DhcpOptionBuilder & operator=(const DhcpOptionBuilder &other)
Definition: DhcpLayer.h:575
Definition: DhcpLayer.h:222
Definition: DhcpLayer.h:89
DhcpOption(uint8_t *optionRawData)
Definition: DhcpLayer.h:409
Definition: DhcpLayer.h:254
Definition: DhcpLayer.h:304
Definition: DhcpLayer.h:174
Definition: DhcpLayer.h:79
Definition: DhcpLayer.h:116
Definition: DhcpLayer.h:164
Definition: MacAddress.h:28
Definition: DhcpLayer.h:334
Definition: DhcpLayer.h:292
Definition: DhcpLayer.h:196
Definition: DhcpLayer.h:236
Definition: DhcpLayer.h:342
Definition: DhcpLayer.h:186
uint32_t toInt() const
Definition: IpAddress.h:181
IPv4Address getServerIpAddress() const
Definition: DhcpLayer.h:650
Definition: DhcpLayer.h:286
Definition: DhcpLayer.h:102
Definition: DhcpLayer.h:274
Definition: DhcpLayer.h:308
Definition: DhcpLayer.h:166
Definition: DhcpLayer.h:350
uint8_t clientHardwareAddress[16]
Definition: DhcpLayer.h:49
Definition: DhcpLayer.h:330