PcapPlusPlus  21.11
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  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->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->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
478  return sizeof(uint8_t);
479 
480  return sizeof(uint8_t) * 2 + (size_t)m_Data->recordLen;
481  }
482 
483  size_t getDataSize() const
484  {
485  if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
486  return 0;
487 
488  return m_Data->recordLen;
489  }
490  };
491 
492 
499  {
500  public:
501 
509  DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen) :
510  TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) { }
511 
518  DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue) :
519  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
520 
527  DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue) :
528  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
529 
536  DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue) :
537  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
538 
545  DhcpOptionBuilder(DhcpOptionTypes optionType, const IPv4Address& optionValue) :
546  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
547 
554  DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string& optionValue) :
555  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
556 
562  TLVRecordBuilder(other) { }
563 
570  {
571  TLVRecordBuilder::operator=(other);
572  return *this;
573  }
574 
579  DhcpOption build() const;
580  };
581 
582 
583 
588  class DhcpLayer : public Layer
589  {
590  public:
591 
599  DhcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
600 
607  DhcpLayer(DhcpMessageType msgType, const MacAddress& clientMacAddr);
608 
612  DhcpLayer();
613 
617  virtual ~DhcpLayer() {}
618 
623  dhcp_header* getDhcpHeader() const { return (dhcp_header*)m_Data; }
624 
628  BootpOpCodes getOpCode() const { return (BootpOpCodes)getDhcpHeader()->opCode; }
629 
633  IPv4Address getClientIpAddress() const { return getDhcpHeader()->clientIpAddress; }
634 
639  void setClientIpAddress(const IPv4Address& addr) { getDhcpHeader()->clientIpAddress = addr.toInt(); }
640 
644  IPv4Address getServerIpAddress() const { return getDhcpHeader()->serverIpAddress; }
645 
650  void setServerIpAddress(const IPv4Address& addr) { getDhcpHeader()->serverIpAddress = addr.toInt(); }
651 
655  IPv4Address getYourIpAddress() const { return getDhcpHeader()->yourIpAddress; }
656 
661  void setYourIpAddress(const IPv4Address& addr) { getDhcpHeader()->yourIpAddress = addr.toInt(); }
662 
666  IPv4Address getGatewayIpAddress() const { return getDhcpHeader()->gatewayIpAddress; }
667 
672  void setGatewayIpAddress(const IPv4Address& addr) { getDhcpHeader()->gatewayIpAddress = addr.toInt(); }
673 
678  MacAddress getClientHardwareAddress() const;
679 
685  void setClientHardwareAddress(const MacAddress& addr);
686 
691  DhcpMessageType getMesageType() const;
692 
701  bool setMesageType(DhcpMessageType msgType);
702 
707  DhcpOption getFirstOptionData() const;
708 
716  DhcpOption getNextOptionData(DhcpOption dhcpOption) const;
717 
724  DhcpOption getOptionData(DhcpOptionTypes option) const;
725 
729  size_t getOptionsCount() const;
730 
737  DhcpOption addOption(const DhcpOptionBuilder& optionBuilder);
738 
746  DhcpOption addOptionAfter(const DhcpOptionBuilder& optionBuilder, DhcpOptionTypes prevOption);
747 
753  bool removeOption(DhcpOptionTypes optionType);
754 
759  bool removeAllOptions();
760 
761  // implement abstract methods
762 
766  void parseNextLayer() {}
767 
771  size_t getHeaderLen() const { return m_DataLen; }
772 
782  void computeCalculateFields();
783 
784  std::string toString() const;
785 
787 
788  private:
789 
790  uint8_t* getOptionsBasePtr() const { return m_Data + sizeof(dhcp_header); }
791 
792  TLVRecordReader<DhcpOption> m_OptionReader;
793 
794  void initDhcpLayer(size_t numOfBytesToAllocate);
795 
796  DhcpOption addOptionAt(const DhcpOptionBuilder& optionBuilder, int offset);
797  };
798 }
799 
800 #endif /* PACKETPP_DHCP_LAYER */
Definition: DhcpLayer.h:156
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:258
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:518
Definition: DhcpLayer.h:144
DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue)
Definition: DhcpLayer.h:527
Definition: DhcpLayer.h:370
Definition: DhcpLayer.h:378
Definition: DhcpLayer.h:83
Definition: DhcpLayer.h:386
Definition: DhcpLayer.h:91
Definition: ProtocolType.h:273
Definition: DhcpLayer.h:158
DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: DhcpLayer.h:509
uint8_t opCode
Definition: DhcpLayer.h:27
Definition: DhcpLayer.h:282
Definition: TLVData.h:363
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:672
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:666
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:661
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:207
void parseNextLayer()
Definition: DhcpLayer.h:766
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:588
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:655
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:628
Definition: DhcpLayer.h:208
Definition: DhcpLayer.h:294
DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string &optionValue)
Definition: DhcpLayer.h:554
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:639
dhcp_header * getDhcpHeader() const
Definition: DhcpLayer.h:623
Definition: IpAddress.h:27
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:536
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:771
Definition: DhcpLayer.h:108
void setServerIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:650
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:545
Definition: DhcpLayer.h:138
IPv4Address getClientIpAddress() const
Definition: DhcpLayer.h:633
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:561
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:498
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:483
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:786
virtual ~DhcpLayer()
Definition: DhcpLayer.h:617
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:569
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:156
IPv4Address getServerIpAddress() const
Definition: DhcpLayer.h:644
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