PcapPlusPlus  21.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 
11 
16 namespace pcpp
17 {
18 
23  #pragma pack(push, 1)
24  struct dhcp_header {
26  uint8_t opCode;
28  uint8_t hardwareType;
32  uint8_t hops;
34  uint32_t transactionID;
36  uint16_t secondsElapsed;
38  uint16_t flags;
40  uint32_t clientIpAddress;
42  uint32_t yourIpAddress;
44  uint32_t serverIpAddress;
46  uint32_t gatewayIpAddress;
48  uint8_t clientHardwareAddress[16];
50  uint8_t serverName[64];
52  uint8_t bootFilename[128];
54  uint32_t magicNumber;
55  };
56  #pragma pack(pop)
57 
58 
63  {
68  };
69 
85  DHCP_ACK = 5,
87  DHCP_NAK = 6,
92  };
93 
305  DHCPOPT_URL = 114,
319  DHCPOPT_CCC = 122,
390  };
391 
392 
398  class DhcpOption : public TLVRecord
399  {
400  public:
401 
406  DhcpOption(uint8_t* optionRawData) : TLVRecord(optionRawData) { }
407 
411  virtual ~DhcpOption() { }
412 
418  {
419  return getValueAs<uint32_t>();
420  }
421 
429  void setValueIpAddr(const IPv4Address& addr, int valueOffset = 0)
430  {
431  setValue<uint32_t>(addr.toInt(), valueOffset);
432  }
433 
441  std::string getValueAsString(int valueOffset = 0) const
442  {
443  if (m_Data->recordLen - valueOffset < 1)
444  return "";
445 
446  return std::string((const char*)m_Data->recordValue + valueOffset, (int)m_Data->recordLen - valueOffset);
447  }
448 
457  void setValueString(const std::string& stringValue, int valueOffset = 0)
458  {
459  // calculate the maximum length of the destination buffer
460  size_t len = (size_t)m_Data->recordLen - (size_t)valueOffset;
461 
462  // use the length of input string if a buffer is large enough for whole string
463  if (stringValue.length() < len)
464  len = stringValue.length();
465 
466  memcpy(m_Data->recordValue + valueOffset, stringValue.data(), len);
467  }
468 
469 
470  // implement abstract methods
471 
472  size_t getTotalSize() const
473  {
474  if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
475  return sizeof(uint8_t);
476 
477  return sizeof(uint8_t) * 2 + (size_t)m_Data->recordLen;
478  }
479 
480  size_t getDataSize() const
481  {
482  if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
483  return 0;
484 
485  return m_Data->recordLen;
486  }
487  };
488 
489 
496  {
497  public:
498 
506  DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen) :
507  TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) { }
508 
515  DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue) :
516  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
517 
524  DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue) :
525  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
526 
533  DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue) :
534  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
535 
542  DhcpOptionBuilder(DhcpOptionTypes optionType, const IPv4Address& optionValue) :
543  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
544 
551  DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string& optionValue) :
552  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
553 
559  TLVRecordBuilder(other) { }
560 
566  {
567  TLVRecordBuilder::operator=(other);
568  return *this;
569  }
570 
575  DhcpOption build() const;
576  };
577 
578 
579 
584  class DhcpLayer : public Layer
585  {
586  public:
587 
595  DhcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
596 
603  DhcpLayer(DhcpMessageType msgType, const MacAddress& clientMacAddr);
604 
608  DhcpLayer();
609 
613  virtual ~DhcpLayer() {}
614 
619  dhcp_header* getDhcpHeader() const { return (dhcp_header*)m_Data; }
620 
624  BootpOpCodes getOpCode() const { return (BootpOpCodes)getDhcpHeader()->opCode; }
625 
629  IPv4Address getClientIpAddress() const { return getDhcpHeader()->clientIpAddress; }
630 
635  void setClientIpAddress(const IPv4Address& addr) { getDhcpHeader()->clientIpAddress = addr.toInt(); }
636 
640  IPv4Address getServerIpAddress() const { return getDhcpHeader()->serverIpAddress; }
641 
646  void setServerIpAddress(const IPv4Address& addr) { getDhcpHeader()->serverIpAddress = addr.toInt(); }
647 
651  IPv4Address getYourIpAddress() const { return getDhcpHeader()->yourIpAddress; }
652 
657  void setYourIpAddress(const IPv4Address& addr) { getDhcpHeader()->yourIpAddress = addr.toInt(); }
658 
662  IPv4Address getGatewayIpAddress() const { return getDhcpHeader()->gatewayIpAddress; }
663 
668  void setGatewayIpAddress(const IPv4Address& addr) { getDhcpHeader()->gatewayIpAddress = addr.toInt(); }
669 
674  MacAddress getClientHardwareAddress() const;
675 
681  void setClientHardwareAddress(const MacAddress& addr);
682 
687  DhcpMessageType getMesageType() const;
688 
697  bool setMesageType(DhcpMessageType msgType);
698 
703  DhcpOption getFirstOptionData() const;
704 
712  DhcpOption getNextOptionData(DhcpOption dhcpOption) const;
713 
720  DhcpOption getOptionData(DhcpOptionTypes option) const;
721 
725  size_t getOptionsCount() const;
726 
733  DhcpOption addOption(const DhcpOptionBuilder& optionBuilder);
734 
742  DhcpOption addOptionAfter(const DhcpOptionBuilder& optionBuilder, DhcpOptionTypes prevOption);
743 
749  bool removeOption(DhcpOptionTypes optionType);
750 
755  bool removeAllOptions();
756 
757  // implement abstract methods
758 
762  void parseNextLayer() {}
763 
767  size_t getHeaderLen() const { return m_DataLen; }
768 
778  void computeCalculateFields();
779 
780  std::string toString() const;
781 
783 
784  private:
785 
786  uint8_t* getOptionsBasePtr() const { return m_Data + sizeof(dhcp_header); }
787 
788  TLVRecordReader<DhcpOption> m_OptionReader;
789 
790  void initDhcpLayer(size_t numOfBytesToAllocate);
791 
792  DhcpOption addOptionAt(const DhcpOptionBuilder& optionBuilder, int offset);
793  };
794 }
795 
796 #endif /* PACKETPP_DHCP_LAYER */
Definition: DhcpLayer.h:153
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:253
Definition: DhcpLayer.h:273
Definition: DhcpLayer.h:297
Definition: DhcpLayer.h:323
Definition: DhcpLayer.h:169
Definition: DhcpLayer.h:329
Definition: DhcpLayer.h:227
IPv4Address getValueAsIpAddr() const
Definition: DhcpLayer.h:417
Definition: DhcpLayer.h:213
Definition: DhcpLayer.h:143
uint32_t magicNumber
Definition: DhcpLayer.h:54
Definition: DhcpLayer.h:261
Definition: DhcpLayer.h:335
std::string getValueAsString(int valueOffset=0) const
Definition: DhcpLayer.h:441
Definition: DhcpLayer.h:299
Definition: DhcpLayer.h:107
Definition: DhcpLayer.h:357
Definition: DhcpLayer.h:207
Definition: DhcpLayer.h:359
Definition: DhcpLayer.h:263
DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue)
Definition: DhcpLayer.h:515
Definition: DhcpLayer.h:141
DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue)
Definition: DhcpLayer.h:524
Definition: DhcpLayer.h:367
Definition: DhcpLayer.h:375
Definition: DhcpLayer.h:81
Definition: DhcpLayer.h:383
Definition: DhcpLayer.h:89
Definition: ProtocolType.h:268
Definition: DhcpLayer.h:155
DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: DhcpLayer.h:506
uint8_t opCode
Definition: DhcpLayer.h:26
Definition: DhcpLayer.h:279
Definition: TLVData.h:351
Definition: DhcpLayer.h:67
Definition: Layer.h:70
Definition: DhcpLayer.h:127
Definition: DhcpLayer.h:317
uint32_t gatewayIpAddress
Definition: DhcpLayer.h:46
Definition: DhcpLayer.h:363
Definition: TLVData.h:23
Definition: DhcpLayer.h:217
void setGatewayIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:668
Definition: DhcpLayer.h:225
Definition: DhcpLayer.h:85
Definition: Packet.h:26
Definition: DhcpLayer.h:315
Definition: DhcpLayer.h:203
uint32_t yourIpAddress
Definition: DhcpLayer.h:42
IPv4Address getGatewayIpAddress() const
Definition: DhcpLayer.h:662
Definition: DhcpLayer.h:398
Definition: DhcpLayer.h:355
uint8_t serverName[64]
Definition: DhcpLayer.h:50
Definition: DhcpLayer.h:385
Definition: DhcpLayer.h:147
void setYourIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:657
Definition: DhcpLayer.h:341
Definition: DhcpLayer.h:117
Definition: DhcpLayer.h:319
Definition: DhcpLayer.h:343
uint8_t hops
Definition: DhcpLayer.h:32
Definition: DhcpLayer.h:381
Definition: DhcpLayer.h:311
Definition: DhcpLayer.h:243
Definition: DhcpLayer.h:247
void setValueIpAddr(const IPv4Address &addr, int valueOffset=0)
Definition: DhcpLayer.h:429
Definition: DhcpLayer.h:123
Definition: DhcpLayer.h:79
Definition: DhcpLayer.h:265
Definition: DhcpLayer.h:379
DhcpOptionTypes
Definition: DhcpLayer.h:97
Definition: DhcpLayer.h:333
Definition: DhcpLayer.h:361
Definition: DhcpLayer.h:351
Definition: DhcpLayer.h:281
Definition: DhcpLayer.h:231
Definition: TLVData.h:197
void parseNextLayer()
Definition: DhcpLayer.h:762
Definition: DhcpLayer.h:179
Definition: DhcpLayer.h:285
Definition: DhcpLayer.h:267
size_t getTotalSize() const
Definition: DhcpLayer.h:472
Definition: DhcpLayer.h:157
Definition: DhcpLayer.h:75
Definition: DhcpLayer.h:584
Definition: DhcpLayer.h:151
Definition: DhcpLayer.h:145
Definition: DhcpLayer.h:149
Definition: DhcpLayer.h:389
Definition: DhcpLayer.h:201
Definition: DhcpLayer.h:173
uint16_t flags
Definition: DhcpLayer.h:38
Definition: DhcpLayer.h:241
IPv4Address getYourIpAddress() const
Definition: DhcpLayer.h:651
Definition: DhcpLayer.h:257
Definition: DhcpLayer.h:353
Definition: DhcpLayer.h:111
Definition: DhcpLayer.h:373
Definition: DhcpLayer.h:24
BootpOpCodes getOpCode() const
Definition: DhcpLayer.h:624
Definition: DhcpLayer.h:205
Definition: DhcpLayer.h:291
DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string &optionValue)
Definition: DhcpLayer.h:551
Definition: DhcpLayer.h:199
Definition: DhcpLayer.h:371
BootpOpCodes
Definition: DhcpLayer.h:62
Definition: DhcpLayer.h:235
Definition: DhcpLayer.h:159
uint16_t secondsElapsed
Definition: DhcpLayer.h:36
void setClientIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:635
dhcp_header * getDhcpHeader() const
Definition: DhcpLayer.h:619
Definition: IpAddress.h:26
Definition: DhcpLayer.h:269
Definition: DhcpLayer.h:103
Definition: DhcpLayer.h:121
virtual ~DhcpOption()
Definition: DhcpLayer.h:411
DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue)
Definition: DhcpLayer.h:533
Definition: DhcpLayer.h:295
Definition: DhcpLayer.h:139
Definition: DhcpLayer.h:195
Definition: DhcpLayer.h:101
Definition: DhcpLayer.h:255
size_t getHeaderLen() const
Definition: DhcpLayer.h:767
Definition: DhcpLayer.h:105
void setServerIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:646
Definition: DhcpLayer.h:129
Definition: DhcpLayer.h:309
Definition: DhcpLayer.h:239
Definition: DhcpLayer.h:349
Definition: DhcpLayer.h:197
Definition: DhcpLayer.h:223
Definition: DhcpLayer.h:369
Definition: DhcpLayer.h:185
DhcpMessageType
Definition: DhcpLayer.h:73
Definition: DhcpLayer.h:181
Definition: DhcpLayer.h:325
DhcpOptionBuilder(DhcpOptionTypes optionType, const IPv4Address &optionValue)
Definition: DhcpLayer.h:542
Definition: DhcpLayer.h:135
IPv4Address getClientIpAddress() const
Definition: DhcpLayer.h:629
Definition: DhcpLayer.h:221
Definition: DhcpLayer.h:249
Definition: DhcpLayer.h:387
Definition: DhcpLayer.h:65
uint32_t clientIpAddress
Definition: DhcpLayer.h:40
DhcpOptionBuilder(const DhcpOptionBuilder &other)
Definition: DhcpLayer.h:558
Definition: DhcpLayer.h:229
Definition: DhcpLayer.h:137
Definition: DhcpLayer.h:119
Definition: DhcpLayer.h:303
Definition: DhcpLayer.h:175
Definition: DhcpLayer.h:189
Definition: DhcpLayer.h:337
Definition: DhcpLayer.h:321
uint8_t hardwareAddressLength
Definition: DhcpLayer.h:30
Definition: DhcpLayer.h:209
Definition: DhcpLayer.h:83
uint8_t hardwareType
Definition: DhcpLayer.h:28
Definition: DhcpLayer.h:293
Definition: DhcpLayer.h:495
Definition: DhcpLayer.h:345
Definition: DhcpLayer.h:287
Definition: DhcpLayer.h:237
Definition: DhcpLayer.h:115
uint32_t serverIpAddress
Definition: DhcpLayer.h:44
uint32_t transactionID
Definition: DhcpLayer.h:34
Definition: DhcpLayer.h:365
Definition: DhcpLayer.h:125
Definition: DhcpLayer.h:307
Definition: DhcpLayer.h:245
uint8_t bootFilename[128]
Definition: DhcpLayer.h:52
size_t getDataSize() const
Definition: DhcpLayer.h:480
Definition: DhcpLayer.h:377
Definition: DhcpLayer.h:109
Definition: DhcpLayer.h:253
Definition: DhcpLayer.h:177
Definition: DhcpLayer.h:313
Definition: DhcpLayer.h:133
Definition: DhcpLayer.h:131
Definition: DhcpLayer.h:259
Definition: DhcpLayer.h:215
OsiModelLayer getOsiModelLayer() const
Definition: DhcpLayer.h:782
virtual ~DhcpLayer()
Definition: DhcpLayer.h:613
Definition: DhcpLayer.h:91
void setValueString(const std::string &stringValue, int valueOffset=0)
Definition: DhcpLayer.h:457
Definition: DhcpLayer.h:167
Definition: DhcpLayer.h:191
DhcpOptionBuilder & operator=(const DhcpOptionBuilder &other)
Definition: DhcpLayer.h:565
Definition: DhcpLayer.h:219
Definition: DhcpLayer.h:87
DhcpOption(uint8_t *optionRawData)
Definition: DhcpLayer.h:406
Definition: DhcpLayer.h:251
Definition: DhcpLayer.h:301
Definition: DhcpLayer.h:171
Definition: DhcpLayer.h:77
Definition: DhcpLayer.h:113
Definition: DhcpLayer.h:161
Definition: MacAddress.h:27
Definition: DhcpLayer.h:331
Definition: DhcpLayer.h:289
Definition: DhcpLayer.h:193
Definition: DhcpLayer.h:233
Definition: DhcpLayer.h:339
Definition: DhcpLayer.h:183
uint32_t toInt() const
Definition: IpAddress.h:149
IPv4Address getServerIpAddress() const
Definition: DhcpLayer.h:640
Definition: DhcpLayer.h:283
Definition: DhcpLayer.h:99
Definition: DhcpLayer.h:271
Definition: DhcpLayer.h:305
Definition: DhcpLayer.h:163
Definition: DhcpLayer.h:347
uint8_t clientHardwareAddress[16]
Definition: DhcpLayer.h:48
Definition: DhcpLayer.h:327