Kea  2.3.4-git
pkt4.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef PKT4_H
8 #define PKT4_H
9 
10 #include <asiolink/io_address.h>
11 #include <dhcp/duid.h>
12 #include <util/buffer.h>
13 #include <dhcp/option.h>
14 #include <dhcp/classify.h>
15 #include <dhcp/pkt.h>
16 
17 #include <boost/shared_ptr.hpp>
18 
19 #include <iostream>
20 #include <vector>
21 #include <set>
22 #include <list>
23 
24 #include <time.h>
25 
26 namespace isc {
27 
28 namespace dhcp {
29 
37 class Pkt4 : public Pkt {
38 public:
39 
41  const static size_t MAX_CHADDR_LEN = 16;
42 
44  const static size_t MAX_SNAME_LEN = 64;
45 
47  const static size_t MAX_FILE_LEN = 128;
48 
50  const static size_t DHCPV4_PKT_HDR_LEN = 236;
51 
54  const static uint16_t FLAG_BROADCAST_MASK = 0x8000;
55 
60  Pkt4(uint8_t msg_type, uint32_t transid);
61 
69  Pkt4(const uint8_t* data, size_t len);
70 
79  virtual void pack();
80 
89  virtual void unpack();
90 
101  std::string getLabel() const;
102 
112  static std::string makeLabel(const HWAddrPtr& hwaddr,
113  const ClientIdPtr& client_id,
114  const uint32_t transid);
115 
123  static std::string makeLabel(const HWAddrPtr& hwaddr, const ClientIdPtr& client_id);
124 
130  std::string toText() const;
131 
138  size_t len();
139 
143  void setHops(uint8_t hops) { hops_ = hops; };
144 
148  uint8_t getHops() const { return (hops_); };
149 
150  // Note: There's no need to manipulate OP field directly,
151  // thus no setOp() method. See op_ comment.
152 
156  uint8_t getOp() const { return (op_); };
157 
161  void setSecs(uint16_t secs) { secs_ = secs; };
162 
166  uint16_t getSecs() const { return (secs_); };
167 
171  void setFlags(uint16_t flags) { flags_ = flags; };
172 
176  uint16_t getFlags() const { return (flags_); };
177 
182  getCiaddr() const { return (ciaddr_); };
183 
187  void
188  setCiaddr(const isc::asiolink::IOAddress& ciaddr) { ciaddr_ = ciaddr; };
189 
194  getSiaddr() const { return (siaddr_); };
195 
199  void
200  setSiaddr(const isc::asiolink::IOAddress& siaddr) { siaddr_ = siaddr; };
201 
206  getYiaddr() const { return (yiaddr_); };
207 
211  void
212  setYiaddr(const isc::asiolink::IOAddress& yiaddr) { yiaddr_ = yiaddr; };
213 
218  getGiaddr() const { return (giaddr_); };
219 
223  void
224  setGiaddr(const isc::asiolink::IOAddress& giaddr) { giaddr_ = giaddr; };
225 
232  uint8_t getType() const;
233 
237  void setType(uint8_t type);
238 
249  static const char* getName(const uint8_t type);
250 
256  const char* getName() const;
257 
264  const OptionBuffer
265  getSname() const { return (std::vector<uint8_t>(sname_, &sname_[MAX_SNAME_LEN])); };
266 
271  void setSname(const uint8_t* sname, size_t sname_len);
272 
279  const OptionBuffer
280  getFile() const { return (std::vector<uint8_t>(file_, &file_[MAX_FILE_LEN])); };
281 
286  void
287  setFile(const uint8_t* file, size_t file_len);
288 
300  void setHWAddr(uint8_t htype, uint8_t hlen,
301  const std::vector<uint8_t>& mac_addr);
302 
308  void setHWAddr(const HWAddrPtr& addr);
309 
313  uint8_t
314  getHtype() const;
315 
319  uint8_t
320  getHlen() const;
321 
324  HWAddrPtr getHWAddr() const { return (hwaddr_); }
325 
331  virtual void
332  addOption(const OptionPtr& opt);
333 
344  void setLocalHWAddr(const uint8_t htype, const uint8_t hlen,
345  const std::vector<uint8_t>& mac_addr);
346 
356  void setLocalHWAddr(const HWAddrPtr& addr);
357 
362  return (local_hwaddr_);
363  }
364 
374  std::list<uint16_t>& getDeferredOptions() {
375  return (deferred_options_);
376  }
377 
388  bool isRelayed() const;
389 
394  virtual bool isDhcp4o6() const {
395  return (false);
396  }
397 
398 private:
399 
411  virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
412  const std::vector<uint8_t>& mac_addr,
413  HWAddrPtr& hw_addr);
414 
415 protected:
416 
422  uint8_t
423  DHCPTypeToBootpType(uint8_t dhcpType);
424 
434  return (HWAddrPtr());
435  }
436 
446  return (HWAddrPtr());
447  }
448 
457  return (HWAddrPtr());
458  }
459 
468  return (HWAddrPtr());
469  }
470 
479  return (HWAddrPtr());
480  }
481 
489  return (HWAddrPtr());
490  }
491 
494 
495  // @brief List of deferred option codes
496  std::list<uint16_t> deferred_options_;
497 
505  uint8_t op_;
506 
512 
514  uint8_t hops_;
515 
517  uint16_t secs_;
518 
520  uint16_t flags_;
521 
524 
527 
530 
533 
536 
538  uint8_t file_[MAX_FILE_LEN];
539 
540  // end of real DHCPv4 fields
541 }; // Pkt4 class
542 
544 typedef boost::shared_ptr<Pkt4> Pkt4Ptr;
545 
546 } // isc::dhcp namespace
547 } // isc namespace
548 
549 #endif
uint8_t getHlen() const
Returns hlen field.
Definition: pkt4.cc:577
HWAddrPtr getHWAddr() const
returns hardware address information
Definition: pkt4.h:324
static const size_t MAX_SNAME_LEN
length of the SNAME field in DHCPv4 message
Definition: pkt4.h:44
Pkt4(uint8_t msg_type, uint32_t transid)
Constructor, used in replying to a message.
Definition: pkt4.cc:32
uint8_t hops_
Number of relay agents traversed.
Definition: pkt4.h:514
uint8_t sname_[MAX_SNAME_LEN]
sname field (64 bytes)
Definition: pkt4.h:535
uint16_t flags_
flags
Definition: pkt4.h:520
static std::string makeLabel(const HWAddrPtr &hwaddr, const ClientIdPtr &client_id, const uint32_t transid)
Returns text representation of the given packet identifiers.
Definition: pkt4.cc:397
void setYiaddr(const isc::asiolink::IOAddress &yiaddr)
Sets yiaddr field.
Definition: pkt4.h:212
uint8_t getHtype() const
Returns htype field.
Definition: pkt4.cc:569
virtual HWAddrPtr getMACFromRemoteIdRelayOption()
No-op.
Definition: pkt4.h:488
uint16_t getFlags() const
Returns flags field.
Definition: pkt4.h:176
uint8_t DHCPTypeToBootpType(uint8_t dhcpType)
converts DHCP message type to BOOTP op type
Definition: pkt4.cc:542
isc::asiolink::IOAddress giaddr_
giaddr field (32 bits): Gateway IP address
Definition: pkt4.h:532
const OptionBuffer getSname() const
Returns sname field.
Definition: pkt4.h:265
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
static const size_t MAX_CHADDR_LEN
length of the CHADDR field in DHCPv4 message
Definition: pkt4.h:41
virtual HWAddrPtr getMACFromDocsisCMTS()
No-op.
Definition: pkt4.h:478
const isc::asiolink::IOAddress & getCiaddr() const
Returns ciaddr field.
Definition: pkt4.h:182
virtual bool isDhcp4o6() const
Checks if a DHCPv4 message has been transported over DHCPv6.
Definition: pkt4.h:394
static const size_t MAX_FILE_LEN
length of the FILE field in DHCPv4 message
Definition: pkt4.h:47
Defines elements for storing the names of client classes.
static const size_t DHCPV4_PKT_HDR_LEN
specifies DHCPv4 packet header length (fixed part)
Definition: pkt4.h:50
virtual void unpack()
Parses on-wire form of DHCPv4 packet.
Definition: pkt4.cc:148
Base class for classes representing DHCP messages.
Definition: pkt.h:90
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
const isc::asiolink::IOAddress & getSiaddr() const
Returns siaddr field.
Definition: pkt4.h:194
virtual HWAddrPtr getMACFromIPv6RelayOpt()
No-op.
Definition: pkt4.h:445
void setSiaddr(const isc::asiolink::IOAddress &siaddr)
Sets siaddr field.
Definition: pkt4.h:200
uint8_t getOp() const
Returns op field.
Definition: pkt4.h:156
isc::asiolink::IOAddress siaddr_
siaddr field (32 bits): next server IP address in boot process(e.g.TFTP)
Definition: pkt4.h:529
const OptionBuffer getFile() const
Returns file field.
Definition: pkt4.h:280
const char * getName() const
Returns name of the DHCP message.
Definition: pkt4.cc:358
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
HWAddrPtr hwaddr_
link-layer address and hardware information represents 3 fields: htype (hardware type, 1 byte), hlen (length of the hardware address, up to 16) and chaddr (hardware address field, 16 bytes)
Definition: pkt4.h:511
std::list< uint16_t > & getDeferredOptions()
Returns a reference to option codes which unpacking will be deferred.
Definition: pkt4.h:374
const isc::asiolink::IOAddress & getYiaddr() const
Returns yiaddr field.
Definition: pkt4.h:206
uint8_t op_
message operation code
Definition: pkt4.h:505
virtual HWAddrPtr getMACFromDocsisModem()
No-op.
Definition: pkt4.h:456
void setFlags(uint16_t flags)
Sets flags field.
Definition: pkt4.h:171
uint8_t getType() const
Returns DHCP message type (e.g.
Definition: pkt4.cc:235
void setSname(const uint8_t *sname, size_t sname_len)
Sets sname field.
Definition: pkt4.cc:505
void setCiaddr(const isc::asiolink::IOAddress &ciaddr)
Sets ciaddr field.
Definition: pkt4.h:188
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:544
void setGiaddr(const isc::asiolink::IOAddress &giaddr)
Sets giaddr field.
Definition: pkt4.h:224
isc::asiolink::IOAddress ciaddr_
ciaddr field (32 bits): Client&#39;s IP address
Definition: pkt4.h:523
virtual HWAddrPtr getMACFromSrcLinkLocalAddr()
No-op.
Definition: pkt4.h:433
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition: duid.h:103
const isc::asiolink::IOAddress & getGiaddr() const
Returns giaddr field.
Definition: pkt4.h:218
std::list< uint16_t > deferred_options_
Definition: pkt4.h:496
Defines the logger used by the top-level component of kea-lfc.
Represents DHCPv4 packet.
Definition: pkt4.h:37
void setHWAddr(uint8_t htype, uint8_t hlen, const std::vector< uint8_t > &mac_addr)
Sets hardware address.
Definition: pkt4.cc:455
bool isRelayed() const
Checks if a DHCPv4 message has been relayed.
Definition: pkt4.cc:597
void setType(uint8_t type)
Sets DHCP message type (e.g.
Definition: pkt4.cc:252
std::string getLabel() const
Returns text representation of the primary packet identifiers.
Definition: pkt4.cc:366
void setHops(uint8_t hops)
Sets hops field.
Definition: pkt4.h:143
HWAddrPtr getLocalHWAddr() const
Returns local HW address.
Definition: pkt4.h:361
uint16_t getSecs() const
Returns secs field.
Definition: pkt4.h:166
uint8_t getHops() const
Returns hops field.
Definition: pkt4.h:148
std::string toText() const
Returns text representation of the packet.
Definition: pkt4.cc:420
uint8_t file_[MAX_FILE_LEN]
file field (128 bytes)
Definition: pkt4.h:538
virtual HWAddrPtr getMACFromDUID()
No-op.
Definition: pkt4.h:467
virtual void addOption(const OptionPtr &opt)
Add an option.
Definition: pkt4.cc:586
void setFile(const uint8_t *file, size_t file_len)
Sets file field.
Definition: pkt4.cc:523
static const uint16_t FLAG_BROADCAST_MASK
Mask for the value of flags field in the DHCPv4 message to check whether client requested broadcast r...
Definition: pkt4.h:54
uint16_t secs_
elapsed (number of seconds since beginning of transmission)
Definition: pkt4.h:517
void setSecs(uint16_t secs)
Sets secs field.
Definition: pkt4.h:161
virtual void pack()
Prepares on-wire format of DHCPv4 packet.
Definition: pkt4.cc:71
HWAddrPtr local_hwaddr_
local HW address (dst if receiving packet, src if sending packet)
Definition: pkt4.h:493
void setLocalHWAddr(const uint8_t htype, const uint8_t hlen, const std::vector< uint8_t > &mac_addr)
Sets local HW address.
Definition: pkt4.cc:490
size_t len()
Returns the size of the required buffer to build the packet.
Definition: pkt4.cc:59
isc::asiolink::IOAddress yiaddr_
yiaddr field (32 bits): Client&#39;s IP address ("your"), set by server
Definition: pkt4.h:526