PcapPlusPlus  22.05
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 
10 #ifndef PCPP_DEPRECATED
11 #if defined(__GNUC__) || defined(__clang__)
12 #define PCPP_DEPRECATED __attribute__((deprecated))
13 #elif defined(_MSC_VER)
14 #define PCPP_DEPRECATED __declspec(deprecated)
15 #else
16 #pragma message("WARNING: DEPRECATED feature is not implemented for this compiler")
17 #define PCPP_DEPRECATED
18 #endif
19 #endif
20 
22 
27 namespace pcpp
28 {
29 
34  #pragma pack(push, 1)
35  struct dhcp_header
36  {
38  uint8_t opCode;
40  uint8_t hardwareType;
44  uint8_t hops;
46  uint32_t transactionID;
48  uint16_t secondsElapsed;
50  uint16_t flags;
52  uint32_t clientIpAddress;
54  uint32_t yourIpAddress;
56  uint32_t serverIpAddress;
58  uint32_t gatewayIpAddress;
60  uint8_t clientHardwareAddress[16];
62  uint8_t serverName[64];
64  uint8_t bootFilename[128];
66  uint32_t magicNumber;
67  };
68  #pragma pack(pop)
69 
70 
75  {
80  };
81 
86  {
98  DHCP_ACK = 5,
100  DHCP_NAK = 6,
105  };
106 
111  {
319  DHCPOPT_URL = 114,
333  DHCPOPT_CCC = 122,
404  };
405 
406 
412  class DhcpOption : public TLVRecord<uint8_t, uint8_t>
413  {
414  public:
415 
420  DhcpOption(uint8_t* optionRawData) : TLVRecord(optionRawData) { }
421 
425  virtual ~DhcpOption() { }
426 
432  {
433  return getValueAs<uint32_t>();
434  }
435 
443  void setValueIpAddr(const IPv4Address& addr, int valueOffset = 0)
444  {
445  setValue<uint32_t>(addr.toInt(), valueOffset);
446  }
447 
455  std::string getValueAsString(int valueOffset = 0) const
456  {
457  if (m_Data->recordLen - valueOffset < 1)
458  return "";
459 
460  return std::string((const char*)m_Data->recordValue + valueOffset, (int)m_Data->recordLen - valueOffset);
461  }
462 
471  void setValueString(const std::string& stringValue, int valueOffset = 0)
472  {
473  // calculate the maximum length of the destination buffer
474  size_t len = (size_t)m_Data->recordLen - (size_t)valueOffset;
475 
476  // use the length of input string if a buffer is large enough for whole string
477  if (stringValue.length() < len)
478  len = stringValue.length();
479 
480  memcpy(m_Data->recordValue + valueOffset, stringValue.data(), len);
481  }
482 
483 
484  // implement abstract methods
485 
486  size_t getTotalSize() const
487  {
488  if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
489  return sizeof(uint8_t);
490 
491  return sizeof(uint8_t) * 2 + (size_t)m_Data->recordLen;
492  }
493 
494  size_t getDataSize() const
495  {
496  if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
497  return 0;
498 
499  return m_Data->recordLen;
500  }
501  };
502 
503 
510  {
511  public:
512 
520  DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen) :
521  TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) { }
522 
529  DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue) :
530  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
531 
538  DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue) :
539  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
540 
547  DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue) :
548  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
549 
556  DhcpOptionBuilder(DhcpOptionTypes optionType, const IPv4Address& optionValue) :
557  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
558 
565  DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string& optionValue) :
566  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
567 
573  TLVRecordBuilder(other) { }
574 
581  {
582  TLVRecordBuilder::operator=(other);
583  return *this;
584  }
585 
590  DhcpOption build() const;
591  };
592 
593 
594 
599  class DhcpLayer : public Layer
600  {
601  public:
602 
610  DhcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
611 
618  DhcpLayer(DhcpMessageType msgType, const MacAddress& clientMacAddr);
619 
623  DhcpLayer();
624 
628  virtual ~DhcpLayer() {}
629 
634  dhcp_header* getDhcpHeader() const { return (dhcp_header*)m_Data; }
635 
639  BootpOpCodes getOpCode() const { return (BootpOpCodes)getDhcpHeader()->opCode; }
640 
644  IPv4Address getClientIpAddress() const { return getDhcpHeader()->clientIpAddress; }
645 
650  void setClientIpAddress(const IPv4Address& addr) { getDhcpHeader()->clientIpAddress = addr.toInt(); }
651 
655  IPv4Address getServerIpAddress() const { return getDhcpHeader()->serverIpAddress; }
656 
661  void setServerIpAddress(const IPv4Address& addr) { getDhcpHeader()->serverIpAddress = addr.toInt(); }
662 
666  IPv4Address getYourIpAddress() const { return getDhcpHeader()->yourIpAddress; }
667 
672  void setYourIpAddress(const IPv4Address& addr) { getDhcpHeader()->yourIpAddress = addr.toInt(); }
673 
677  IPv4Address getGatewayIpAddress() const { return getDhcpHeader()->gatewayIpAddress; }
678 
683  void setGatewayIpAddress(const IPv4Address& addr) { getDhcpHeader()->gatewayIpAddress = addr.toInt(); }
684 
689  MacAddress getClientHardwareAddress() const;
690 
696  void setClientHardwareAddress(const MacAddress& addr);
697 
701  PCPP_DEPRECATED DhcpMessageType getMesageType() const { return getMessageType(); };
702 
707  DhcpMessageType getMessageType() const;
708 
712  bool setMesageType(DhcpMessageType msgType) { return setMessageType(msgType); };
713 
722  bool setMessageType(DhcpMessageType msgType);
723 
728  DhcpOption getFirstOptionData() const;
729 
737  DhcpOption getNextOptionData(DhcpOption dhcpOption) const;
738 
745  DhcpOption getOptionData(DhcpOptionTypes option) const;
746 
750  size_t getOptionsCount() const;
751 
758  DhcpOption addOption(const DhcpOptionBuilder& optionBuilder);
759 
767  DhcpOption addOptionAfter(const DhcpOptionBuilder& optionBuilder, DhcpOptionTypes prevOption);
768 
774  bool removeOption(DhcpOptionTypes optionType);
775 
780  bool removeAllOptions();
781 
782  // implement abstract methods
783 
787  void parseNextLayer() {}
788 
792  size_t getHeaderLen() const { return m_DataLen; }
793 
803  void computeCalculateFields();
804 
805  std::string toString() const;
806 
808 
809  private:
810 
811  uint8_t* getOptionsBasePtr() const { return m_Data + sizeof(dhcp_header); }
812 
813  TLVRecordReader<DhcpOption> m_OptionReader;
814 
815  void initDhcpLayer(size_t numOfBytesToAllocate);
816 
817  DhcpOption addOptionAt(const DhcpOptionBuilder& optionBuilder, int offset);
818  };
819 }
820 
821 #endif /* PACKETPP_DHCP_LAYER */
Definition: DhcpLayer.h:167
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:263
Definition: DhcpLayer.h:287
Definition: DhcpLayer.h:311
Definition: DhcpLayer.h:337
Definition: DhcpLayer.h:183
Definition: DhcpLayer.h:343
Definition: DhcpLayer.h:241
IPv4Address getValueAsIpAddr() const
Definition: DhcpLayer.h:431
Definition: DhcpLayer.h:227
Definition: DhcpLayer.h:157
uint32_t magicNumber
Definition: DhcpLayer.h:66
Definition: DhcpLayer.h:275
Definition: DhcpLayer.h:349
std::string getValueAsString(int valueOffset=0) const
Definition: DhcpLayer.h:455
Definition: DhcpLayer.h:313
Definition: DhcpLayer.h:121
Definition: DhcpLayer.h:371
Definition: DhcpLayer.h:221
Definition: DhcpLayer.h:373
Definition: DhcpLayer.h:277
DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue)
Definition: DhcpLayer.h:529
Definition: DhcpLayer.h:155
DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue)
Definition: DhcpLayer.h:538
Definition: DhcpLayer.h:381
Definition: DhcpLayer.h:389
Definition: DhcpLayer.h:94
Definition: DhcpLayer.h:397
Definition: DhcpLayer.h:102
Definition: ProtocolType.h:278
Definition: DhcpLayer.h:169
DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: DhcpLayer.h:520
uint8_t opCode
Definition: DhcpLayer.h:38
Definition: DhcpLayer.h:293
Definition: TLVData.h:363
Definition: DhcpLayer.h:79
Definition: Layer.h:70
Definition: DhcpLayer.h:141
Definition: DhcpLayer.h:331
uint32_t gatewayIpAddress
Definition: DhcpLayer.h:58
Definition: DhcpLayer.h:377
Definition: TLVData.h:24
Definition: DhcpLayer.h:231
void setGatewayIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:683
Definition: DhcpLayer.h:239
Definition: DhcpLayer.h:98
Definition: Packet.h:26
Definition: DhcpLayer.h:329
Definition: DhcpLayer.h:217
uint32_t yourIpAddress
Definition: DhcpLayer.h:54
IPv4Address getGatewayIpAddress() const
Definition: DhcpLayer.h:677
Definition: DhcpLayer.h:412
Definition: DhcpLayer.h:369
uint8_t serverName[64]
Definition: DhcpLayer.h:62
Definition: DhcpLayer.h:399
Definition: DhcpLayer.h:161
void setYourIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:672
Definition: DhcpLayer.h:355
Definition: DhcpLayer.h:131
Definition: DhcpLayer.h:333
Definition: DhcpLayer.h:357
uint8_t hops
Definition: DhcpLayer.h:44
Definition: DhcpLayer.h:395
Definition: DhcpLayer.h:325
Definition: DhcpLayer.h:257
Definition: DhcpLayer.h:261
void setValueIpAddr(const IPv4Address &addr, int valueOffset=0)
Definition: DhcpLayer.h:443
Definition: DhcpLayer.h:137
Definition: DhcpLayer.h:92
Definition: DhcpLayer.h:279
Definition: DhcpLayer.h:393
DhcpOptionTypes
Definition: DhcpLayer.h:110
Definition: DhcpLayer.h:347
Definition: DhcpLayer.h:375
Definition: DhcpLayer.h:365
Definition: DhcpLayer.h:295
Definition: DhcpLayer.h:245
Definition: TLVData.h:207
void parseNextLayer()
Definition: DhcpLayer.h:787
Definition: DhcpLayer.h:193
Definition: DhcpLayer.h:299
Definition: DhcpLayer.h:281
size_t getTotalSize() const
Definition: DhcpLayer.h:486
Definition: DhcpLayer.h:171
Definition: DhcpLayer.h:88
Definition: DhcpLayer.h:599
Definition: DhcpLayer.h:165
Definition: DhcpLayer.h:159
Definition: DhcpLayer.h:163
Definition: DhcpLayer.h:403
Definition: DhcpLayer.h:215
Definition: DhcpLayer.h:187
uint16_t flags
Definition: DhcpLayer.h:50
Definition: DhcpLayer.h:255
IPv4Address getYourIpAddress() const
Definition: DhcpLayer.h:666
Definition: DhcpLayer.h:271
Definition: DhcpLayer.h:367
Definition: DhcpLayer.h:125
Definition: DhcpLayer.h:387
Definition: DhcpLayer.h:35
BootpOpCodes getOpCode() const
Definition: DhcpLayer.h:639
Definition: DhcpLayer.h:219
Definition: DhcpLayer.h:305
DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string &optionValue)
Definition: DhcpLayer.h:565
Definition: DhcpLayer.h:213
Definition: DhcpLayer.h:385
BootpOpCodes
Definition: DhcpLayer.h:74
Definition: DhcpLayer.h:249
Definition: DhcpLayer.h:173
uint16_t secondsElapsed
Definition: DhcpLayer.h:48
void setClientIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:650
dhcp_header * getDhcpHeader() const
Definition: DhcpLayer.h:634
Definition: IpAddress.h:27
Definition: DhcpLayer.h:283
Definition: DhcpLayer.h:117
Definition: DhcpLayer.h:135
virtual ~DhcpOption()
Definition: DhcpLayer.h:425
DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue)
Definition: DhcpLayer.h:547
Definition: DhcpLayer.h:309
Definition: DhcpLayer.h:153
Definition: DhcpLayer.h:209
Definition: DhcpLayer.h:115
Definition: DhcpLayer.h:269
size_t getHeaderLen() const
Definition: DhcpLayer.h:792
Definition: DhcpLayer.h:119
void setServerIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:661
Definition: DhcpLayer.h:143
Definition: DhcpLayer.h:323
Definition: DhcpLayer.h:253
Definition: DhcpLayer.h:363
Definition: DhcpLayer.h:211
Definition: DhcpLayer.h:237
Definition: DhcpLayer.h:383
bool setMesageType(DhcpMessageType msgType)
Definition: DhcpLayer.h:712
Definition: DhcpLayer.h:199
DhcpMessageType
Definition: DhcpLayer.h:85
Definition: DhcpLayer.h:195
Definition: DhcpLayer.h:339
DhcpOptionBuilder(DhcpOptionTypes optionType, const IPv4Address &optionValue)
Definition: DhcpLayer.h:556
Definition: DhcpLayer.h:149
IPv4Address getClientIpAddress() const
Definition: DhcpLayer.h:644
Definition: DhcpLayer.h:235
Definition: DhcpLayer.h:263
Definition: DhcpLayer.h:401
Definition: DhcpLayer.h:77
uint32_t clientIpAddress
Definition: DhcpLayer.h:52
DhcpOptionBuilder(const DhcpOptionBuilder &other)
Definition: DhcpLayer.h:572
Definition: DhcpLayer.h:243
Definition: DhcpLayer.h:151
Definition: DhcpLayer.h:133
Definition: DhcpLayer.h:317
Definition: DhcpLayer.h:189
Definition: DhcpLayer.h:203
Definition: DhcpLayer.h:351
Definition: DhcpLayer.h:335
uint8_t hardwareAddressLength
Definition: DhcpLayer.h:42
Definition: DhcpLayer.h:223
Definition: DhcpLayer.h:96
uint8_t hardwareType
Definition: DhcpLayer.h:40
Definition: DhcpLayer.h:307
Definition: DhcpLayer.h:509
Definition: DhcpLayer.h:359
Definition: DhcpLayer.h:301
Definition: DhcpLayer.h:251
Definition: DhcpLayer.h:129
uint32_t serverIpAddress
Definition: DhcpLayer.h:56
uint32_t transactionID
Definition: DhcpLayer.h:46
Definition: DhcpLayer.h:379
Definition: DhcpLayer.h:139
Definition: DhcpLayer.h:321
Definition: DhcpLayer.h:259
uint8_t bootFilename[128]
Definition: DhcpLayer.h:64
size_t getDataSize() const
Definition: DhcpLayer.h:494
Definition: DhcpLayer.h:391
Definition: DhcpLayer.h:123
Definition: DhcpLayer.h:267
Definition: DhcpLayer.h:191
Definition: DhcpLayer.h:327
Definition: DhcpLayer.h:147
Definition: DhcpLayer.h:145
Definition: DhcpLayer.h:273
Definition: DhcpLayer.h:229
OsiModelLayer getOsiModelLayer() const
Definition: DhcpLayer.h:807
virtual ~DhcpLayer()
Definition: DhcpLayer.h:628
Definition: DhcpLayer.h:104
void setValueString(const std::string &stringValue, int valueOffset=0)
Definition: DhcpLayer.h:471
Definition: DhcpLayer.h:181
Definition: DhcpLayer.h:205
DhcpOptionBuilder & operator=(const DhcpOptionBuilder &other)
Definition: DhcpLayer.h:580
Definition: DhcpLayer.h:233
Definition: DhcpLayer.h:100
DhcpOption(uint8_t *optionRawData)
Definition: DhcpLayer.h:420
Definition: DhcpLayer.h:265
Definition: DhcpLayer.h:315
Definition: DhcpLayer.h:185
Definition: DhcpLayer.h:90
Definition: DhcpLayer.h:127
Definition: DhcpLayer.h:175
Definition: MacAddress.h:28
Definition: DhcpLayer.h:345
Definition: DhcpLayer.h:303
Definition: DhcpLayer.h:207
Definition: DhcpLayer.h:247
Definition: DhcpLayer.h:353
Definition: DhcpLayer.h:197
uint32_t toInt() const
Definition: IpAddress.h:156
IPv4Address getServerIpAddress() const
Definition: DhcpLayer.h:655
Definition: DhcpLayer.h:297
Definition: DhcpLayer.h:113
Definition: DhcpLayer.h:285
Definition: DhcpLayer.h:319
Definition: DhcpLayer.h:177
Definition: DhcpLayer.h:361
uint8_t clientHardwareAddress[16]
Definition: DhcpLayer.h:60
Definition: DhcpLayer.h:341