Kea  1.5.0
pkt4.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2017 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/option.h>
12 #include <dhcp/duid.h>
13 #include <util/buffer.h>
14 #include <dhcp/option.h>
15 #include <dhcp/classify.h>
16 #include <dhcp/pkt.h>
17 
18 #include <boost/shared_ptr.hpp>
19 
20 #include <iostream>
21 #include <vector>
22 #include <set>
23 #include <list>
24 
25 #include <time.h>
26 
27 namespace isc {
28 
29 namespace dhcp {
30 
38 class Pkt4 : public Pkt {
39 public:
40 
42  const static size_t MAX_CHADDR_LEN = 16;
43 
45  const static size_t MAX_SNAME_LEN = 64;
46 
48  const static size_t MAX_FILE_LEN = 128;
49 
51  const static size_t DHCPV4_PKT_HDR_LEN = 236;
52 
55  const static uint16_t FLAG_BROADCAST_MASK = 0x8000;
56 
61  Pkt4(uint8_t msg_type, uint32_t transid);
62 
70  Pkt4(const uint8_t* data, size_t len);
71 
80  virtual void pack();
81 
90  virtual void unpack();
91 
102  std::string getLabel() const;
103 
113  static std::string makeLabel(const HWAddrPtr& hwaddr,
114  const ClientIdPtr& client_id,
115  const uint32_t transid);
116 
124  static std::string makeLabel(const HWAddrPtr& hwaddr, const ClientIdPtr& client_id);
125 
131  std::string toText() const;
132 
139  size_t len();
140 
144  void setHops(uint8_t hops) { hops_ = hops; };
145 
149  uint8_t getHops() const { return (hops_); };
150 
151  // Note: There's no need to manipulate OP field directly,
152  // thus no setOp() method. See op_ comment.
153 
157  uint8_t getOp() const { return (op_); };
158 
162  void setSecs(uint16_t secs) { secs_ = secs; };
163 
167  uint16_t getSecs() const { return (secs_); };
168 
172  void setFlags(uint16_t flags) { flags_ = flags; };
173 
177  uint16_t getFlags() const { return (flags_); };
178 
183  getCiaddr() const { return (ciaddr_); };
184 
188  void
189  setCiaddr(const isc::asiolink::IOAddress& ciaddr) { ciaddr_ = ciaddr; };
190 
191 
196  getSiaddr() const { return (siaddr_); };
197 
201  void
202  setSiaddr(const isc::asiolink::IOAddress& siaddr) { siaddr_ = siaddr; };
203 
208  getYiaddr() const { return (yiaddr_); };
209 
213  void
214  setYiaddr(const isc::asiolink::IOAddress& yiaddr) { yiaddr_ = yiaddr; };
215 
220  getGiaddr() const { return (giaddr_); };
221 
225  void
226  setGiaddr(const isc::asiolink::IOAddress& giaddr) { giaddr_ = giaddr; };
227 
234  uint8_t getType() const;
235 
239  void setType(uint8_t type);
240 
251  static const char* getName(const uint8_t type);
252 
258  const char* getName() const;
259 
266  const OptionBuffer
267  getSname() const { return (std::vector<uint8_t>(sname_, &sname_[MAX_SNAME_LEN])); };
268 
273  void setSname(const uint8_t* sname, size_t sname_len);
274 
281  const OptionBuffer
282  getFile() const { return (std::vector<uint8_t>(file_, &file_[MAX_FILE_LEN])); };
283 
288  void
289  setFile(const uint8_t* file, size_t file_len);
290 
302  void setHWAddr(uint8_t htype, uint8_t hlen,
303  const std::vector<uint8_t>& mac_addr);
304 
310  void setHWAddr(const HWAddrPtr& addr);
311 
315  uint8_t
316  getHtype() const;
317 
321  uint8_t
322  getHlen() const;
323 
326  HWAddrPtr getHWAddr() const { return (hwaddr_); }
327 
333  virtual void
334  addOption(const OptionPtr& opt);
335 
346  void setLocalHWAddr(const uint8_t htype, const uint8_t hlen,
347  const std::vector<uint8_t>& mac_addr);
348 
358  void setLocalHWAddr(const HWAddrPtr& addr);
359 
364  return (local_hwaddr_);
365  }
366 
376  std::list<uint16_t>& getDeferredOptions() {
377  return (deferred_options_);
378  }
379 
390  bool isRelayed() const;
391 
396  virtual bool isDhcp4o6() const {
397  return (false);
398  }
399 
400 private:
401 
413  virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
414  const std::vector<uint8_t>& mac_addr,
415  HWAddrPtr& hw_addr);
416 
417 protected:
418 
424  uint8_t
425  DHCPTypeToBootpType(uint8_t dhcpType);
426 
436  return (HWAddrPtr());
437  }
438 
448  return (HWAddrPtr());
449  }
450 
459  return (HWAddrPtr());
460  }
461 
470  return (HWAddrPtr());
471  }
472 
481  return (HWAddrPtr());
482  }
483 
491  return(HWAddrPtr());
492  }
493 
496 
497  // @brief List of deferred option codes
498  std::list<uint16_t> deferred_options_;
499 
507  uint8_t op_;
508 
514 
516  uint8_t hops_;
517 
519  uint16_t secs_;
520 
522  uint16_t flags_;
523 
526 
529 
532 
535 
538 
540  uint8_t file_[MAX_FILE_LEN];
541 
542  // end of real DHCPv4 fields
543 }; // Pkt4 class
544 
546 typedef boost::shared_ptr<Pkt4> Pkt4Ptr;
547 
548 } // isc::dhcp namespace
549 
550 } // isc namespace
551 
552 #endif
isc::dhcp::Pkt4::getLabel
std::string getLabel() const
Returns text representation of the primary packet identifiers.
Definition: pkt4.cc:329
isc::dhcp::Pkt4::setFile
void setFile(const uint8_t *file, size_t file_len)
Sets file field.
Definition: pkt4.cc:487
isc::dhcp::OptionBuffer
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:25
isc::dhcp::Pkt4::flags_
uint16_t flags_
flags
Definition: pkt4.h:522
isc::dhcp::Pkt4::setYiaddr
void setYiaddr(const isc::asiolink::IOAddress &yiaddr)
Sets yiaddr field.
Definition: pkt4.h:214
isc::dhcp::Pkt4::local_hwaddr_
HWAddrPtr local_hwaddr_
local HW address (dst if receiving packet, src if sending packet)
Definition: pkt4.h:495
isc::dhcp::Pkt4::unpack
virtual void unpack()
Parses on-wire form of DHCPv4 packet.
Definition: pkt4.cc:150
isc::dhcp::Pkt4::getSecs
uint16_t getSecs() const
Returns secs field.
Definition: pkt4.h:167
io_address.h
duid.h
isc::dhcp::Pkt4::setType
void setType(uint8_t type)
Sets DHCP message type (e.g.
Definition: pkt4.cc:247
classify.h
Defines elements for storing the names of client classes.
isc::dhcp::Pkt4::getMACFromDocsisModem
virtual HWAddrPtr getMACFromDocsisModem()
No-op.
Definition: pkt4.h:458
isc::dhcp::Pkt4::getCiaddr
const isc::asiolink::IOAddress & getCiaddr() const
Returns ciaddr field.
Definition: pkt4.h:183
isc::dhcp::Pkt4::makeLabel
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:360
isc::dhcp::Pkt4::setHops
void setHops(uint8_t hops)
Sets hops field.
Definition: pkt4.h:144
isc::dhcp::Pkt4::isRelayed
bool isRelayed() const
Checks if a DHCPv4 message has been relayed.
Definition: pkt4.cc:561
isc::dhcp::Pkt4::getGiaddr
const isc::asiolink::IOAddress & getGiaddr() const
Returns giaddr field.
Definition: pkt4.h:220
isc::dhcp::Pkt4::siaddr_
isc::asiolink::IOAddress siaddr_
siaddr field (32 bits): next server IP address in boot process(e.g.TFTP)
Definition: pkt4.h:531
isc::dhcp::Pkt4::file_
uint8_t file_[MAX_FILE_LEN]
file field (128 bytes)
Definition: pkt4.h:540
isc::dhcp::Pkt4::getFlags
uint16_t getFlags() const
Returns flags field.
Definition: pkt4.h:177
isc::dhcp::Pkt4::hops_
uint8_t hops_
Number of relay agents traversed.
Definition: pkt4.h:516
isc::dhcp::Pkt4::addOption
virtual void addOption(const OptionPtr &opt)
Add an option.
Definition: pkt4.cc:550
isc::dhcp::Pkt4::toText
std::string toText() const
Returns text representation of the packet.
Definition: pkt4.cc:383
isc::dhcp::Pkt4::pack
virtual void pack()
Prepares on-wire format of DHCPv4 packet.
Definition: pkt4.cc:89
isc::dhcp::Pkt4::getHtype
uint8_t getHtype() const
Returns htype field.
Definition: pkt4.cc:533
isc::dhcp::Pkt4::setSecs
void setSecs(uint16_t secs)
Sets secs field.
Definition: pkt4.h:162
isc::dhcp::Pkt4::getMACFromDocsisCMTS
virtual HWAddrPtr getMACFromDocsisCMTS()
No-op.
Definition: pkt4.h:480
isc::dhcp::Pkt4::getMACFromDUID
virtual HWAddrPtr getMACFromDUID()
No-op.
Definition: pkt4.h:469
isc::dhcp::Pkt4::getYiaddr
const isc::asiolink::IOAddress & getYiaddr() const
Returns yiaddr field.
Definition: pkt4.h:208
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::dhcp::Pkt4::getHops
uint8_t getHops() const
Returns hops field.
Definition: pkt4.h:149
isc::dhcp::HWAddrPtr
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
isc::dhcp::Pkt4Ptr
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:546
isc::dhcp::Pkt4::giaddr_
isc::asiolink::IOAddress giaddr_
giaddr field (32 bits): Gateway IP address
Definition: pkt4.h:534
isc::dhcp::Pkt4::getLocalHWAddr
HWAddrPtr getLocalHWAddr() const
Returns local HW address.
Definition: pkt4.h:363
isc::dhcp::Pkt4::isDhcp4o6
virtual bool isDhcp4o6() const
Checks if a DHCPv4 message has been transported over DHCPv6.
Definition: pkt4.h:396
isc::dhcp::Pkt4::setGiaddr
void setGiaddr(const isc::asiolink::IOAddress &giaddr)
Sets giaddr field.
Definition: pkt4.h:226
isc::dhcp::Pkt4::ciaddr_
isc::asiolink::IOAddress ciaddr_
ciaddr field (32 bits): Client's IP address
Definition: pkt4.h:525
isc::dhcp::Pkt4::MAX_SNAME_LEN
static const size_t MAX_SNAME_LEN
length of the SNAME field in DHCPv4 message
Definition: pkt4.h:45
isc::dhcp::Pkt
Base class for classes representing DHCP messages.
Definition: pkt.h:90
isc::dhcp::Pkt4::getOp
uint8_t getOp() const
Returns op field.
Definition: pkt4.h:157
isc::dhcp::Pkt4::setCiaddr
void setCiaddr(const isc::asiolink::IOAddress &ciaddr)
Sets ciaddr field.
Definition: pkt4.h:189
pkt.h
isc::dhcp::Pkt4::getType
uint8_t getType() const
Returns DHCP message type (e.g.
Definition: pkt4.cc:230
isc::dhcp::Pkt4::MAX_CHADDR_LEN
static const size_t MAX_CHADDR_LEN
length of the CHADDR field in DHCPv4 message
Definition: pkt4.h:42
isc::dhcp::Pkt4::getName
const char * getName() const
Returns name of the DHCP message.
Definition: pkt4.cc:321
isc::dhcp::Pkt4::setFlags
void setFlags(uint16_t flags)
Sets flags field.
Definition: pkt4.h:172
isc::dhcp::Pkt4
Represents DHCPv4 packet.
Definition: pkt4.h:38
isc::dhcp::Pkt4::setSname
void setSname(const uint8_t *sname, size_t sname_len)
Sets sname field.
Definition: pkt4.cc:469
isc::dhcp::Pkt4::FLAG_BROADCAST_MASK
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:55
buffer.h
isc::dhcp::Pkt4::yiaddr_
isc::asiolink::IOAddress yiaddr_
yiaddr field (32 bits): Client's IP address ("your"), set by server
Definition: pkt4.h:528
isc::dhcp::Pkt4::getHWAddr
HWAddrPtr getHWAddr() const
returns hardware address information
Definition: pkt4.h:326
isc::dhcp::Pkt4::getSname
const OptionBuffer getSname() const
Returns sname field.
Definition: pkt4.h:267
isc::dhcp::Pkt4::MAX_FILE_LEN
static const size_t MAX_FILE_LEN
length of the FILE field in DHCPv4 message
Definition: pkt4.h:48
isc::dhcp::ClientIdPtr
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition: duid.h:103
isc::dhcp::Pkt4::deferred_options_
std::list< uint16_t > deferred_options_
Definition: pkt4.h:498
isc::dhcp::Pkt4::getHlen
uint8_t getHlen() const
Returns hlen field.
Definition: pkt4.cc:541
isc::dhcp::Pkt4::len
size_t len()
Returns the size of the required buffer to build the packet.
Definition: pkt4.cc:75
isc::dhcp::Pkt4::getMACFromRemoteIdRelayOption
virtual HWAddrPtr getMACFromRemoteIdRelayOption()
No-op.
Definition: pkt4.h:490
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
isc::dhcp::Pkt4::Pkt4
Pkt4(uint8_t msg_type, uint32_t transid)
Constructor, used in replying to a message.
Definition: pkt4.cc:32
isc::dhcp::Pkt4::setLocalHWAddr
void setLocalHWAddr(const uint8_t htype, const uint8_t hlen, const std::vector< uint8_t > &mac_addr)
Sets local HW address.
Definition: pkt4.cc:454
isc::dhcp::Pkt4::setSiaddr
void setSiaddr(const isc::asiolink::IOAddress &siaddr)
Sets siaddr field.
Definition: pkt4.h:202
isc::dhcp::Pkt4::hwaddr_
HWAddrPtr hwaddr_
link-layer address and hardware information represents 3 fields: htype (hardware type,...
Definition: pkt4.h:513
isc::dhcp::Pkt4::getMACFromSrcLinkLocalAddr
virtual HWAddrPtr getMACFromSrcLinkLocalAddr()
No-op.
Definition: pkt4.h:435
isc::dhcp::Pkt4::getDeferredOptions
std::list< uint16_t > & getDeferredOptions()
Returns a reference to option codes which unpacking will be deferred.
Definition: pkt4.h:376
option.h
isc::dhcp::Pkt4::op_
uint8_t op_
message operation code
Definition: pkt4.h:507
isc::dhcp::Pkt4::setHWAddr
void setHWAddr(uint8_t htype, uint8_t hlen, const std::vector< uint8_t > &mac_addr)
Sets hardware address.
Definition: pkt4.cc:419
isc::dhcp::Pkt4::DHCPTypeToBootpType
uint8_t DHCPTypeToBootpType(uint8_t dhcpType)
converts DHCP message type to BOOTP op type
Definition: pkt4.cc:506
isc::dhcp::Pkt4::DHCPV4_PKT_HDR_LEN
static const size_t DHCPV4_PKT_HDR_LEN
specifies DHCPv4 packet header length (fixed part)
Definition: pkt4.h:51
isc::dhcp::Pkt4::getSiaddr
const isc::asiolink::IOAddress & getSiaddr() const
Returns siaddr field.
Definition: pkt4.h:196
isc::dhcp::Pkt4::getFile
const OptionBuffer getFile() const
Returns file field.
Definition: pkt4.h:282
isc::dhcp::Pkt4::sname_
uint8_t sname_[MAX_SNAME_LEN]
sname field (64 bytes)
Definition: pkt4.h:537
isc::dhcp::Pkt4::getMACFromIPv6RelayOpt
virtual HWAddrPtr getMACFromIPv6RelayOpt()
No-op.
Definition: pkt4.h:447
isc::dhcp::Pkt4::secs_
uint16_t secs_
elapsed (number of seconds since beginning of transmission)
Definition: pkt4.h:519