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 "IpAddress.h"
6 #include "MacAddress.h"
7 #include <string.h>
8 
10 
15 namespace pcpp
16 {
17 
22  #pragma pack(push, 1)
23  struct dhcp_header {
25  uint8_t opCode;
27  uint8_t hardwareType;
31  uint8_t hops;
33  uint32_t transactionID;
35  uint16_t secondsElapsed;
37  uint16_t flags;
39  uint32_t clientIpAddress;
41  uint32_t yourIpAddress;
43  uint32_t serverIpAddress;
45  uint32_t gatewayIpAddress;
47  uint8_t clientHardwareAddress[16];
49  uint8_t serverName[64];
51  uint8_t bootFilename[128];
53  uint32_t magicNumber;
54  };
55  #pragma pack(pop)
56 
57 
62  {
67  };
68 
84  DHCP_ACK = 5,
86  DHCP_NAK = 6,
91  };
92 
304  DHCPOPT_URL = 114,
318  DHCPOPT_CCC = 122,
389  };
390 
391 
397  {
398  public:
400  uint8_t opCode;
402  uint8_t len;
404  uint8_t value[];
405 
415  template<typename T>
416  T getValueAs(int valueOffset = 0)
417  {
418  if (getTotalSize() <= 2*sizeof(uint8_t) + valueOffset)
419  return 0;
420  if (getTotalSize() - 2*sizeof(uint8_t) - valueOffset < sizeof(T))
421  return 0;
422 
423  T result;
424  memcpy(&result, value+valueOffset, sizeof(T));
425  return result;
426  }
427 
433  {
434  uint32_t addrAsInt = getValueAs<uint32_t>();
435  return IPv4Address(addrAsInt);
436  }
437 
445  std::string getValueAsString(int valueOffset = 0)
446  {
447  if (len-valueOffset < 1)
448  return "";
449 
450  return std::string((char*)value+valueOffset, len-valueOffset);
451  }
452 
461  template<typename T>
462  void setValue(T newValue, int valueOffset = 0)
463  {
464  memcpy(value+valueOffset, &newValue, sizeof(T));
465  }
466 
474  void setValueIpAddr(const IPv4Address& addr, int valueOffset = 0)
475  {
476  setValue<uint32_t>(addr.toInt(), valueOffset);
477  }
478 
487  void setValueString(const std::string& stringValue, int valueOffset = 0)
488  {
489  std::string val = stringValue;
490  if (stringValue.length() > (size_t)len-(size_t)valueOffset)
491  val = stringValue.substr(0, len-valueOffset);
492 
493  memcpy(value+valueOffset, val.c_str(), val.length());
494  }
495 
500  inline size_t getTotalSize() const
501  {
502  if (opCode == (uint8_t)DHCPOPT_END || opCode == (uint8_t)DHCPOPT_PAD)
503  return sizeof(uint8_t);
504 
505  return sizeof(uint8_t)*2 + (size_t)len;
506  }
507 
511  inline uint8_t getLength()
512  {
513  if (opCode == (uint8_t)DHCPOPT_END || opCode == (uint8_t)DHCPOPT_PAD)
514  return 0;
515 
516  return len;
517  }
518 
523 
524  private:
525 
526  // private c'tor which isn't implemented to make this struct impossible to construct
527  DhcpOptionData();
528  };
529 
530 
535  class DhcpLayer : public Layer
536  {
537  public:
545  DhcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
546 
553  DhcpLayer(DhcpMessageType msgType, const MacAddress& clientMacAddr);
554 
558  DhcpLayer();
559 
563  virtual ~DhcpLayer() {}
564 
569  inline dhcp_header* getDhcpHeader() { return (dhcp_header*)m_Data; };
570 
575 
580 
585  void setClientIpAddress(const IPv4Address& addr);
586 
591 
596  void setServerIpAddress(const IPv4Address& addr);
597 
602 
607  void setYourIpAddress(const IPv4Address& addr);
608 
613 
618  void setGatewayIpAddress(const IPv4Address& addr);
619 
625 
631  void setClientHardwareAddress(const MacAddress& addr);
632 
638 
647  bool setMesageType(DhcpMessageType msgType);
648 
654 
663 
670 
674  size_t getOptionsCount();
675 
686  DhcpOptionData* addOption(DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t* optionData);
687 
700  DhcpOptionData* addOptionAfter(DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t* optionData, DhcpOptionTypes prevOption);
701 
707  bool removeOption(DhcpOptionTypes optionType);
708 
713  bool removeAllOptions();
714 
715  // implement abstract methods
716 
720  void parseNextLayer() {}
721 
725  size_t getHeaderLen();
726 
736  void computeCalculateFields();
737 
738  std::string toString();
739 
741 
742  private:
743 
744  size_t m_DhcpOptionsCount;
745 
746  void initDhcpLayer(size_t numOfBytesToAllocate);
747 
748  DhcpOptionData* castPtrToOptionData(uint8_t* ptr);
749 
750  DhcpOptionData* addOptionAt(DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t* optionData, int offset);
751  };
752 }
753 
754 #endif /* PACKETPP_DHCP_LAYER */
Definition: DhcpLayer.h:326
Definition: DhcpLayer.h:348
DhcpOptionData * addOptionAfter(DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t *optionData, DhcpOptionTypes prevOption)
Definition: DhcpLayer.h:128
Definition: DhcpLayer.h:322
Definition: DhcpLayer.h:126
Definition: DhcpLayer.h:138
Definition: DhcpLayer.h:184
Definition: DhcpLayer.h:174
IPv4Address getClientIpAddress()
Definition: DhcpLayer.h:112
Definition: DhcpLayer.h:396
Definition: DhcpLayer.h:82
Definition: DhcpLayer.h:362
Definition: DhcpLayer.h:98
Definition: DhcpLayer.h:350
void setValueString(const std::string &stringValue, int valueOffset=0)
Definition: DhcpLayer.h:487
void setClientIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:324
uint32_t toInt() const
void computeCalculateFields()
Definition: DhcpLayer.h:80
DhcpOptionData * getFirstOptionData()
Definition: DhcpLayer.h:100
DhcpOptionTypes getType()
Definition: DhcpLayer.h:522
Definition: DhcpLayer.h:206
Definition: DhcpLayer.h:66
Definition: DhcpLayer.h:382
Definition: DhcpLayer.h:132
Definition: DhcpLayer.h:214
Definition: DhcpLayer.h:304
Definition: DhcpLayer.h:246
Definition: DhcpLayer.h:198
void setGatewayIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:23
Definition: DhcpLayer.h:344
Definition: DhcpLayer.h:328
uint32_t clientIpAddress
Definition: DhcpLayer.h:39
Definition: DhcpLayer.h:196
Definition: Packet.h:26
Definition: DhcpLayer.h:260
Definition: DhcpLayer.h:368
Definition: DhcpLayer.h:152
Definition: DhcpLayer.h:150
Definition: ProtocolType.h:223
Definition: DhcpLayer.h:342
The main namespace for the PcapPlusPlus lib.
Definition: DhcpLayer.h:148
void setYourIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:136
Definition: DhcpLayer.h:298
Definition: DhcpLayer.h:216
Definition: DhcpLayer.h:114
Definition: DhcpLayer.h:192
Definition: IpAddress.h:113
void setServerIpAddress(const IPv4Address &addr)
Definition: DhcpLayer.h:332
Definition: DhcpLayer.h:202
std::string getValueAsString(int valueOffset=0)
Definition: DhcpLayer.h:445
Definition: DhcpLayer.h:300
Definition: DhcpLayer.h:228
Definition: DhcpLayer.h:535
Definition: DhcpLayer.h:262
Definition: DhcpLayer.h:116
Definition: DhcpLayer.h:102
Definition: DhcpLayer.h:330
Definition: DhcpLayer.h:190
Definition: DhcpLayer.h:168
Definition: DhcpLayer.h:130
Definition: DhcpLayer.h:314
OsiModelLayer
Definition: ProtocolType.h:208
Definition: DhcpLayer.h:286
Definition: DhcpLayer.h:144
Definition: DhcpLayer.h:122
uint32_t gatewayIpAddress
Definition: DhcpLayer.h:45
Definition: DhcpLayer.h:336
Definition: DhcpLayer.h:270
Definition: DhcpLayer.h:170
DhcpOptionData * getNextOptionData(DhcpOptionData *dhcpOption)
Definition: DhcpLayer.h:384
Definition: DhcpLayer.h:224
Definition: DhcpLayer.h:318
Definition: DhcpLayer.h:218
uint8_t hardwareAddressLength
Definition: DhcpLayer.h:29
DhcpOptionTypes
Definition: DhcpLayer.h:96
Definition: DhcpLayer.h:172
IPv4Address getServerIpAddress()
Definition: DhcpLayer.h:188
uint8_t value[]
Definition: DhcpLayer.h:404
Definition: DhcpLayer.h:146
Definition: DhcpLayer.h:220
Definition: DhcpLayer.h:306
Definition: DhcpLayer.h:252
Definition: DhcpLayer.h:312
Definition: DhcpLayer.h:240
Definition: DhcpLayer.h:238
Definition: DhcpLayer.h:134
Definition: DhcpLayer.h:156
Definition: DhcpLayer.h:142
Definition: DhcpLayer.h:234
MacAddress getClientHardwareAddress()
Definition: DhcpLayer.h:320
Definition: DhcpLayer.h:204
Definition: DhcpLayer.h:376
Definition: DhcpLayer.h:380
uint8_t clientHardwareAddress[16]
Definition: DhcpLayer.h:47
Definition: DhcpLayer.h:160
Definition: DhcpLayer.h:364
Definition: DhcpLayer.h:366
Definition: DhcpLayer.h:282
uint8_t serverName[64]
Definition: DhcpLayer.h:49
Definition: DhcpLayer.h:288
Definition: DhcpLayer.h:372
DhcpMessageType
Definition: DhcpLayer.h:72
Definition: DhcpLayer.h:386
DhcpOptionData * getOptionData(DhcpOptionTypes option)
OsiModelLayer getOsiModelLayer()
Definition: DhcpLayer.h:740
size_t getHeaderLen()
Definition: DhcpLayer.h:182
Definition: DhcpLayer.h:250
Definition: DhcpLayer.h:230
Definition: DhcpLayer.h:378
Definition: DhcpLayer.h:258
Definition: DhcpLayer.h:124
Definition: DhcpLayer.h:268
Definition: DhcpLayer.h:294
Definition: DhcpLayer.h:108
Definition: DhcpLayer.h:162
Definition: DhcpLayer.h:200
uint8_t len
Definition: DhcpLayer.h:402
Definition: DhcpLayer.h:280
IPv4Address getValueAsIpAddr()
Definition: DhcpLayer.h:432
uint16_t secondsElapsed
Definition: DhcpLayer.h:35
uint16_t flags
Definition: DhcpLayer.h:37
size_t getTotalSize() const
Definition: DhcpLayer.h:500
std::string toString()
bool removeAllOptions()
Definition: DhcpLayer.h:158
Definition: DhcpLayer.h:166
Definition: DhcpLayer.h:254
Definition: DhcpLayer.h:84
uint32_t magicNumber
Definition: DhcpLayer.h:53
Definition: MacAddress.h:21
uint32_t yourIpAddress
Definition: DhcpLayer.h:41
Definition: DhcpLayer.h:346
Definition: DhcpLayer.h:176
void parseNextLayer()
Definition: DhcpLayer.h:720
Definition: DhcpLayer.h:226
Definition: DhcpLayer.h:110
Definition: Layer.h:70
Definition: DhcpLayer.h:388
uint8_t opCode
Definition: DhcpLayer.h:25
Definition: DhcpLayer.h:178
Definition: DhcpLayer.h:236
Definition: DhcpLayer.h:222
Definition: DhcpLayer.h:78
Definition: DhcpLayer.h:232
Definition: DhcpLayer.h:272
Definition: DhcpLayer.h:64
Definition: DhcpLayer.h:358
uint8_t bootFilename[128]
Definition: DhcpLayer.h:51
virtual ~DhcpLayer()
Definition: DhcpLayer.h:563
uint32_t transactionID
Definition: DhcpLayer.h:33
Definition: DhcpLayer.h:86
Definition: DhcpLayer.h:106
Definition: DhcpLayer.h:244
Definition: DhcpLayer.h:308
BootpOpCodes
Definition: DhcpLayer.h:61
void setClientHardwareAddress(const MacAddress &addr)
uint32_t serverIpAddress
Definition: DhcpLayer.h:43
Definition: DhcpLayer.h:118
dhcp_header * getDhcpHeader()
Definition: DhcpLayer.h:569
void setValueIpAddr(const IPv4Address &addr, int valueOffset=0)
Definition: DhcpLayer.h:474
Definition: DhcpLayer.h:140
Definition: DhcpLayer.h:266
Definition: DhcpLayer.h:90
Definition: DhcpLayer.h:154
bool removeOption(DhcpOptionTypes optionType)
Definition: DhcpLayer.h:302
Definition: DhcpLayer.h:292
IPv4Address getYourIpAddress()
T getValueAs(int valueOffset=0)
Definition: DhcpLayer.h:416
Definition: DhcpLayer.h:316
Definition: DhcpLayer.h:284
Definition: DhcpLayer.h:354
IPv4Address getGatewayIpAddress()
Definition: DhcpLayer.h:296
bool setMesageType(DhcpMessageType msgType)
Definition: DhcpLayer.h:360
Definition: DhcpLayer.h:278
Definition: DhcpLayer.h:310
Definition: DhcpLayer.h:212
Definition: DhcpLayer.h:194
uint8_t getLength()
Definition: DhcpLayer.h:511
uint8_t opCode
Definition: DhcpLayer.h:400
DhcpMessageType getMesageType()
Definition: DhcpLayer.h:256
Definition: DhcpLayer.h:338
Definition: DhcpLayer.h:340
void setValue(T newValue, int valueOffset=0)
Definition: DhcpLayer.h:462
Definition: DhcpLayer.h:356
BootpOpCodes getOpCode()
Definition: DhcpLayer.h:574
Definition: DhcpLayer.h:180
size_t getOptionsCount()
DhcpOptionData * addOption(DhcpOptionTypes optionType, uint16_t optionLen, const uint8_t *optionData)
Definition: DhcpLayer.h:352
Definition: DhcpLayer.h:248
Definition: DhcpLayer.h:76
Definition: DhcpLayer.h:374
Definition: DhcpLayer.h:88
Definition: DhcpLayer.h:334
Definition: DhcpLayer.h:264
Definition: DhcpLayer.h:208
Definition: DhcpLayer.h:370
Definition: DhcpLayer.h:120
Definition: DhcpLayer.h:290
uint8_t hardwareType
Definition: DhcpLayer.h:27
Definition: DhcpLayer.h:104
Definition: DhcpLayer.h:242
Definition: DhcpLayer.h:74
uint8_t hops
Definition: DhcpLayer.h:31