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 "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  uint32_t addrAsInt = getValueAs<uint32_t>();
420  return IPv4Address(addrAsInt);
421  }
422 
430  void setValueIpAddr(const IPv4Address& addr, int valueOffset = 0)
431  {
432  setValue<uint32_t>(addr.toInt(), valueOffset);
433  }
434 
442  std::string getValueAsString(int valueOffset = 0)
443  {
444  if (m_Data->recordLen - valueOffset < 1)
445  return "";
446 
447  return std::string((char*)m_Data->recordValue + valueOffset, m_Data->recordLen - valueOffset);
448  }
449 
458  void setValueString(const std::string& stringValue, int valueOffset = 0)
459  {
460  std::string val = stringValue;
461  if (stringValue.length() > (size_t)m_Data->recordLen-(size_t)valueOffset)
462  val = stringValue.substr(0, m_Data->recordLen-valueOffset);
463 
464  memcpy(m_Data->recordValue+valueOffset, val.c_str(), val.length());
465  }
466 
467 
468  // implement abstract methods
469 
470  size_t getTotalSize() const
471  {
472  if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
473  return sizeof(uint8_t);
474 
475  return sizeof(uint8_t)*2 + (size_t)m_Data->recordLen;
476  }
477 
478  size_t getDataSize()
479  {
480  if (m_Data->recordType == (uint8_t)DHCPOPT_END || m_Data->recordType == (uint8_t)DHCPOPT_PAD)
481  return 0;
482 
483  return m_Data->recordLen;
484  }
485  };
486 
487 
494  {
495  public:
496 
504  DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t* optionValue, uint8_t optionValueLen) :
505  TLVRecordBuilder((uint8_t)optionType, optionValue, optionValueLen) { }
506 
513  DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue) :
514  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
515 
522  DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue) :
523  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
524 
531  DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue) :
532  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
533 
540  DhcpOptionBuilder(DhcpOptionTypes optionType, const IPv4Address& optionValue) :
541  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
542 
549  DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string& optionValue) :
550  TLVRecordBuilder((uint8_t)optionType, optionValue) { }
551 
557  TLVRecordBuilder(other) { }
558 
563  DhcpOption build() const;
564  };
565 
566 
567 
572  class DhcpLayer : public Layer
573  {
574  public:
575 
583  DhcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
584 
591  DhcpLayer(DhcpMessageType msgType, const MacAddress& clientMacAddr);
592 
596  DhcpLayer();
597 
601  virtual ~DhcpLayer() {}
602 
607  inline dhcp_header* getDhcpHeader() { return (dhcp_header*)m_Data; }
608 
613 
618 
623  void setClientIpAddress(const IPv4Address& addr);
624 
629 
634  void setServerIpAddress(const IPv4Address& addr);
635 
640 
645  void setYourIpAddress(const IPv4Address& addr);
646 
651 
656  void setGatewayIpAddress(const IPv4Address& addr);
657 
663 
669  void setClientHardwareAddress(const MacAddress& addr);
670 
676 
685  bool setMesageType(DhcpMessageType msgType);
686 
692 
701 
709 
713  size_t getOptionsCount();
714 
721  DhcpOption addOption(const DhcpOptionBuilder& optionBuilder);
722 
730  DhcpOption addOptionAfter(const DhcpOptionBuilder& optionBuilder, DhcpOptionTypes prevOption);
731 
737  bool removeOption(DhcpOptionTypes optionType);
738 
743  bool removeAllOptions();
744 
745  // implement abstract methods
746 
750  void parseNextLayer() {}
751 
755  size_t getHeaderLen();
756 
766  void computeCalculateFields();
767 
768  std::string toString();
769 
771 
772  private:
773 
774  inline uint8_t* getOptionsBasePtr() { return m_Data + sizeof(dhcp_header); }
775 
776  TLVRecordReader<DhcpOption> m_OptionReader;
777 
778  void initDhcpLayer(size_t numOfBytesToAllocate);
779 
780  DhcpOption addOptionAt(const DhcpOptionBuilder& optionBuilder, int offset);
781  };
782 }
783 
784 #endif /* PACKETPP_DHCP_LAYER */
Definition: DhcpLayer.h:379
DhcpOption build() const
Definition: DhcpLayer.h:129
uint8_t recordLen
Definition: TLVData.h:33
Definition: DhcpLayer.h:103
Definition: DhcpLayer.h:339
Definition: DhcpLayer.h:77
Definition: DhcpLayer.h:121
Definition: DhcpLayer.h:305
Definition: DhcpLayer.h:385
Definition: DhcpLayer.h:207
DhcpOptionBuilder(DhcpOptionTypes optionType, uint32_t optionValue)
Definition: DhcpLayer.h:531
void setValueIpAddr(const IPv4Address &addr, int valueOffset=0)
Definition: DhcpLayer.h:430
Definition: DhcpLayer.h:343
uint8_t recordType
Definition: TLVData.h:31
Definition: DhcpLayer.h:233
Definition: DhcpLayer.h:301
virtual ~DhcpOption()
Definition: DhcpLayer.h:411
Definition: DhcpLayer.h:371
Definition: DhcpLayer.h:85
Definition: DhcpLayer.h:363
Definition: DhcpLayer.h:245
Definition: DhcpLayer.h:119
Definition: DhcpLayer.h:313
bool removeAllOptions()
Definition: DhcpLayer.h:359
Definition: DhcpLayer.h:67
Definition: DhcpLayer.h:357
uint32_t gatewayIpAddress
Definition: DhcpLayer.h:46
Definition: TLVData.h:333
OsiModelLayer getOsiModelLayer()
Definition: DhcpLayer.h:770
Definition: DhcpLayer.h:115
Definition: DhcpLayer.h:137
size_t getHeaderLen()
Definition: DhcpLayer.h:197
Definition: TLVData.h:23
uint8_t hardwareType
Definition: DhcpLayer.h:28
uint32_t toInt() const
DhcpMessageType getMesageType()
Definition: DhcpLayer.h:325
Definition: DhcpLayer.h:293
DhcpOptionBuilder(DhcpOptionTypes optionType, const std::string &optionValue)
Definition: DhcpLayer.h:549
Definition: DhcpLayer.h:189
DhcpOption addOption(const DhcpOptionBuilder &optionBuilder)
Definition: DhcpLayer.h:255
Definition: DhcpLayer.h:299
Definition: DhcpLayer.h:157
Definition: DhcpLayer.h:285
DhcpOption getNextOptionData(DhcpOption dhcpOption)
Definition: DhcpLayer.h:349
uint8_t recordValue[]
Definition: TLVData.h:35
Definition: DhcpLayer.h:181
Definition: DhcpLayer.h:279
Definition: DhcpLayer.h:389
Definition: DhcpLayer.h:125
Definition: DhcpLayer.h:175
size_t getTotalSize() const
Definition: DhcpLayer.h:470
Definition: IpAddress.h:113
Definition: DhcpLayer.h:387
Definition: DhcpLayer.h:215
Definition: DhcpLayer.h:247
Definition: DhcpLayer.h:229
Definition: DhcpLayer.h:223
Definition: DhcpLayer.h:319
Definition: DhcpLayer.h:167
Definition: DhcpLayer.h:89
Definition: DhcpLayer.h:183
Definition: DhcpLayer.h:351
Definition: DhcpLayer.h:398
Definition: DhcpLayer.h:287
Definition: MacAddress.h:21
Definition: DhcpLayer.h:331
Definition: DhcpLayer.h:381
size_t getOptionsCount()
Definition: DhcpLayer.h:135
Definition: DhcpLayer.h:323
Definition: DhcpLayer.h:101
Definition: Layer.h:70
uint8_t bootFilename[128]
Definition: DhcpLayer.h:52
Definition: DhcpLayer.h:317
uint32_t transactionID
Definition: DhcpLayer.h:34
Definition: DhcpLayer.h:191
Definition: DhcpLayer.h:151
DhcpOption(uint8_t *optionRawData)
Definition: DhcpLayer.h:406
Definition: DhcpLayer.h:109
uint32_t magicNumber
Definition: DhcpLayer.h:54
Definition: DhcpLayer.h:169
Definition: DhcpLayer.h:231
Definition: DhcpLayer.h:345
IPv4Address getClientIpAddress()
Definition: DhcpLayer.h:205
Definition: DhcpLayer.h:113
Definition: DhcpLayer.h:237
Definition: DhcpLayer.h:341
Definition: DhcpLayer.h:329
Definition: DhcpLayer.h:369
IPv4Address getGatewayIpAddress()
uint16_t secondsElapsed
Definition: DhcpLayer.h:36
Definition: DhcpLayer.h:133
virtual ~DhcpLayer()
Definition: DhcpLayer.h:601
Definition: Packet.h:26
void setGatewayIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:365
Definition: DhcpLayer.h:149
uint32_t clientIpAddress
Definition: DhcpLayer.h:40
void setYourIpAddress(const IPv4Address &addr)
DhcpOption getFirstOptionData()
Definition: DhcpLayer.h:297
Definition: DhcpLayer.h:99
Definition: DhcpLayer.h:383
Definition: DhcpLayer.h:333
Definition: DhcpLayer.h:107
Definition: DhcpLayer.h:261
DhcpOptionBuilder(const DhcpOptionBuilder &other)
Definition: DhcpLayer.h:556
Definition: DhcpLayer.h:281
Definition: DhcpLayer.h:159
size_t getDataSize()
Definition: DhcpLayer.h:478
Definition: DhcpLayer.h:209
Definition: DhcpLayer.h:141
bool removeOption(DhcpOptionTypes optionType)
Definition: ProtocolType.h:228
Definition: DhcpLayer.h:315
Definition: DhcpLayer.h:79
IPv4Address getValueAsIpAddr()
Definition: DhcpLayer.h:417
Definition: DhcpLayer.h:177
DhcpOptionTypes
Definition: DhcpLayer.h:97
DhcpOptionBuilder(DhcpOptionTypes optionType, uint8_t optionValue)
Definition: DhcpLayer.h:513
Definition: DhcpLayer.h:271
OsiModelLayer
Definition: ProtocolType.h:213
Definition: DhcpLayer.h:87
Definition: DhcpLayer.h:311
Definition: DhcpLayer.h:267
Definition: DhcpLayer.h:373
Definition: DhcpLayer.h:145
Definition: DhcpLayer.h:147
Definition: DhcpLayer.h:123
Definition: DhcpLayer.h:309
void parseNextLayer()
Definition: DhcpLayer.h:750
uint8_t opCode
Definition: DhcpLayer.h:26
Definition: DhcpLayer.h:263
Definition: DhcpLayer.h:201
Definition: DhcpLayer.h:65
Definition: DhcpLayer.h:259
Definition: DhcpLayer.h:303
Definition: DhcpLayer.h:83
Definition: DhcpLayer.h:572
Definition: DhcpLayer.h:337
uint16_t flags
Definition: DhcpLayer.h:38
uint8_t serverName[64]
Definition: DhcpLayer.h:50
Definition: DhcpLayer.h:117
Definition: DhcpLayer.h:24
Definition: DhcpLayer.h:235
Definition: DhcpLayer.h:241
Definition: DhcpLayer.h:273
bool setMesageType(DhcpMessageType msgType)
Definition: DhcpLayer.h:327
void setClientIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:105
Definition: DhcpLayer.h:295
uint8_t clientHardwareAddress[16]
Definition: DhcpLayer.h:48
Definition: DhcpLayer.h:193
Definition: DhcpLayer.h:171
Definition: DhcpLayer.h:213
Definition: DhcpLayer.h:361
Definition: DhcpLayer.h:367
void setServerIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:257
BootpOpCodes getOpCode()
Definition: DhcpLayer.h:612
Definition: DhcpLayer.h:217
uint8_t hardwareAddressLength
Definition: DhcpLayer.h:30
Definition: DhcpLayer.h:377
BootpOpCodes
Definition: DhcpLayer.h:62
DhcpOptionBuilder(DhcpOptionTypes optionType, const uint8_t *optionValue, uint8_t optionValueLen)
Definition: DhcpLayer.h:504
Definition: DhcpLayer.h:243
Definition: DhcpLayer.h:493
Definition: DhcpLayer.h:163
Definition: DhcpLayer.h:155
Definition: DhcpLayer.h:127
Definition: DhcpLayer.h:195
Definition: DhcpLayer.h:143
DhcpOptionBuilder(DhcpOptionTypes optionType, const IPv4Address &optionValue)
Definition: DhcpLayer.h:540
Definition: DhcpLayer.h:81
Definition: DhcpLayer.h:179
Definition: DhcpLayer.h:347
DhcpOption getOptionData(DhcpOptionTypes option)
Definition: DhcpLayer.h:153
Definition: DhcpLayer.h:225
DhcpOptionBuilder(DhcpOptionTypes optionType, uint16_t optionValue)
Definition: DhcpLayer.h:522
uint32_t yourIpAddress
Definition: DhcpLayer.h:42
Definition: DhcpLayer.h:219
void setValueString(const std::string &stringValue, int valueOffset=0)
Definition: DhcpLayer.h:458
Definition: DhcpLayer.h:289
Definition: DhcpLayer.h:335
Definition: DhcpLayer.h:265
Definition: DhcpLayer.h:173
Definition: DhcpLayer.h:131
Definition: DhcpLayer.h:199
Definition: DhcpLayer.h:221
Definition: DhcpLayer.h:111
Definition: DhcpLayer.h:283
Definition: DhcpLayer.h:355
Definition: DhcpLayer.h:249
void setClientHardwareAddress(const MacAddress &addr)
Definition: DhcpLayer.h:91
DhcpOption addOptionAfter(const DhcpOptionBuilder &optionBuilder, DhcpOptionTypes prevOption)
Definition: DhcpLayer.h:291
dhcp_header * getDhcpHeader()
Definition: DhcpLayer.h:607
Definition: DhcpLayer.h:251
Definition: DhcpLayer.h:353
Definition: DhcpLayer.h:139
Definition: DhcpLayer.h:227
Definition: DhcpLayer.h:75
Definition: DhcpLayer.h:203
Definition: DhcpLayer.h:239
std::string toString()
Definition: DhcpLayer.h:269
void computeCalculateFields()
The main namespace for the PcapPlusPlus lib.
std::string getValueAsString(int valueOffset=0)
Definition: DhcpLayer.h:442
DhcpMessageType
Definition: DhcpLayer.h:73
Definition: DhcpLayer.h:185
uint8_t hops
Definition: DhcpLayer.h:32
IPv4Address getServerIpAddress()
Definition: DhcpLayer.h:307
IPv4Address getYourIpAddress()
uint32_t serverIpAddress
Definition: DhcpLayer.h:44
Definition: DhcpLayer.h:253
Definition: DhcpLayer.h:321
Definition: DhcpLayer.h:161
Definition: DhcpLayer.h:375
MacAddress getClientHardwareAddress()