Kea  1.5.0
pkt.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2018 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 PKT_H
8 #define PKT_H
9 
10 #include <asiolink/io_address.h>
11 #include <util/buffer.h>
12 #include <dhcp/option.h>
13 #include <dhcp/hwaddr.h>
14 #include <dhcp/classify.h>
16 
17 #include <boost/date_time/posix_time/posix_time.hpp>
18 #include <boost/shared_ptr.hpp>
19 
20 #include <utility>
21 
22 namespace isc {
23 
24 namespace dhcp {
25 
39 template<typename PktType>
41 public:
42 
44  typedef boost::shared_ptr<PktType> PktTypePtr;
45 
53  const PktTypePtr& pkt2 = PktTypePtr())
54  : pkts_(pkt1, pkt2) {
55  if (pkt1) {
56  pkt1->setCopyRetrievedOptions(true);
57  }
58  if (pkt2) {
59  pkt2->setCopyRetrievedOptions(true);
60  }
61  }
62 
67  if (pkts_.first) {
68  pkts_.first->setCopyRetrievedOptions(false);
69  }
70  if (pkts_.second) {
71  pkts_.second->setCopyRetrievedOptions(false);
72  }
73  }
74 
75 private:
76 
78  std::pair<PktTypePtr, PktTypePtr> pkts_;
79 };
80 
91 protected:
92 
105  Pkt(uint32_t transid, const isc::asiolink::IOAddress& local_addr,
106  const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
107  uint16_t remote_port);
108 
122  Pkt(const uint8_t* buf, uint32_t len,
123  const isc::asiolink::IOAddress& local_addr,
124  const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
125  uint16_t remote_port);
126 
127 public:
128 
144  virtual void pack() = 0;
145 
160  virtual void unpack() = 0;
161 
175 
183  virtual void addOption(const OptionPtr& opt);
184 
193  bool delOption(uint16_t type);
194 
202  virtual std::string getLabel() const {
203  isc_throw(NotImplemented, "Pkt::getLabel()");
204  }
205 
215  virtual std::string toText() const = 0;
216 
227  virtual size_t len() = 0;
228 
236  virtual uint8_t getType() const = 0;
237 
245  virtual void setType(uint8_t type) = 0;
246 
256  virtual const char* getName() const = 0;
257 
261  void setTransid(uint32_t transid) { transid_ = transid; }
262 
266  uint32_t getTransid() const { return (transid_); };
267 
272  bool inClass(const isc::dhcp::ClientClass& client_class);
273 
290  void addClass(const isc::dhcp::ClientClass& client_class,
291  bool required = false);
292 
300  const ClientClasses& getClasses(bool required = false) const {
301  return (!required ? classes_ : required_classes_);
302  }
303 
313 
314 protected:
315 
327  OptionPtr getNonCopiedOption(const uint16_t type) const;
328 
329 public:
330 
342  OptionPtr getOption(const uint16_t type);
343 
368  virtual void setCopyRetrievedOptions(const bool copy) {
370  }
371 
377  bool isCopyRetrievedOptions() const {
378  return (copy_retrieved_options_);
379  }
380 
387  void updateTimestamp();
388 
395  const boost::posix_time::ptime& getTimestamp() const {
396  return timestamp_;
397  }
398 
405  void repack();
406 
411  remote_addr_ = remote;
412  }
413 
418  return (remote_addr_);
419  }
420 
425  local_addr_ = local;
426  }
427 
432  return (local_addr_);
433  }
434 
441  void setLocalPort(uint16_t local) {
442  local_port_ = local;
443  }
444 
451  uint16_t getLocalPort() const {
452  return (local_port_);
453  }
454 
461  void setRemotePort(uint16_t remote) {
462  remote_port_ = remote;
463  }
464 
468  uint16_t getRemotePort() const {
469  return (remote_port_);
470  }
471 
475  void setIndex(uint32_t ifindex) {
476  ifindex_ = ifindex;
477  };
478 
480  void resetIndex() {
481  ifindex_ = -1;
482  }
483 
487  uint32_t getIndex() const {
488  return (ifindex_);
489  };
490 
494  bool indexSet() const {
495  return (ifindex_ >= 0);
496  }
497 
504  std::string getIface() const { return (iface_); };
505 
512  void setIface(const std::string& iface ) { iface_ = iface; };
513 
525  void setRemoteHWAddr(const HWAddrPtr& hw_addr);
526 
545  void setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
546  const std::vector<uint8_t>& hw_addr);
547 
552  return (remote_hwaddr_);
553  }
554 
572  HWAddrPtr getMAC(uint32_t hw_addr_src);
573 
579  virtual ~Pkt() {
580  }
581 
589 
597 
607 
608 protected:
609 
630 
646 
659  virtual HWAddrPtr getMACFromDUID() = 0;
660 
674 
688  HWAddrPtr
690 
703 
716 
718  uint32_t transid_;
719 
721  std::string iface_;
722 
728  int64_t ifindex_;
729 
735 
741 
743  uint16_t local_port_;
744 
746  uint16_t remote_port_;
747 
757 
763 
765  boost::posix_time::ptime timestamp_;
766 
767  // remote HW address (src if receiving packet, dst if sending packet)
769 
770 private:
771 
783  virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
784  const std::vector<uint8_t>& hw_addr,
785  HWAddrPtr& storage);
786 };
787 
788 }; // namespace isc::dhcp
789 }; // namespace isc
790 
791 #endif
isc::dhcp::Pkt::local_addr_
isc::asiolink::IOAddress local_addr_
Local IP (v4 or v6) address.
Definition: pkt.h:734
isc::dhcp::Pkt::isCopyRetrievedOptions
bool isCopyRetrievedOptions() const
Returns whether the copying of retrieved options is enabled.
Definition: pkt.h:377
isc::dhcp::Pkt::setType
virtual void setType(uint8_t type)=0
Sets message type (e.g.
isc::dhcp::OptionBuffer
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:25
isc::dhcp::Pkt::getMACFromRemoteIdRelayOption
virtual HWAddrPtr getMACFromRemoteIdRelayOption()=0
Attempts to obtain MAC address from remote-id relay option.
isc::dhcp::Pkt::getTransid
uint32_t getTransid() const
Returns value of transaction-id field.
Definition: pkt.h:266
isc::data::copy
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1114
isc::dhcp::Pkt::inClass
bool inClass(const isc::dhcp::ClientClass &client_class)
Checks whether a client belongs to a given class.
Definition: pkt.cc:95
isc::dhcp::Pkt::getClasses
const ClientClasses & getClasses(bool required=false) const
Returns the class set.
Definition: pkt.h:300
isc::dhcp::Pkt::getIndex
uint32_t getIndex() const
Returns interface index.
Definition: pkt.h:487
isc::dhcp::Pkt::addOption
virtual void addOption(const OptionPtr &opt)
Adds an option to this packet.
Definition: pkt.cc:56
io_address.h
isc::dhcp::Pkt::setRemotePort
void setRemotePort(uint16_t remote)
Sets remote UDP (and soon TCP) port.
Definition: pkt.h:461
isc::dhcp::Pkt::getTimestamp
const boost::posix_time::ptime & getTimestamp() const
Returns packet timestamp.
Definition: pkt.h:395
classify.h
Defines elements for storing the names of client classes.
isc::dhcp::Pkt::setRemoteHWAddr
void setRemoteHWAddr(const HWAddrPtr &hw_addr)
Sets remote hardware address.
Definition: pkt.cc:125
isc::dhcp::Pkt::getRemoteAddr
const isc::asiolink::IOAddress & getRemoteAddr() const
Returns remote IP address.
Definition: pkt.h:417
isc::dhcp::Pkt::unpack
virtual void unpack()=0
Parses on-wire form of DHCP (either v4 or v6) packet.
isc::dhcp::Pkt::getName
virtual const char * getName() const =0
Returns name of the DHCP message.
isc::dhcp::Pkt::setIface
void setIface(const std::string &iface)
Sets interface name.
Definition: pkt.h:512
isc::dhcp::Pkt::setRemoteAddr
void setRemoteAddr(const isc::asiolink::IOAddress &remote)
Sets remote IP address.
Definition: pkt.h:410
isc::dhcp::ScopedEnableOptionsCopy
RAII object enabling copying options retrieved from the packet.
Definition: pkt.h:40
isc::dhcp::Pkt::required_classes_
ClientClasses required_classes_
Classes which are required to be evaluated.
Definition: pkt.h:596
isc::dhcp::Pkt::options_
isc::dhcp::OptionCollection options_
Collection of options present in this message.
Definition: pkt.h:606
isc::dhcp::Pkt::classes_
ClientClasses classes_
Classes this packet belongs to.
Definition: pkt.h:588
isc::dhcp::Pkt::setIndex
void setIndex(uint32_t ifindex)
Sets interface index.
Definition: pkt.h:475
isc::dhcp::Pkt::updateTimestamp
void updateTimestamp()
Update packet timestamp.
Definition: pkt.cc:108
isc::dhcp::Pkt::getBuffer
isc::util::OutputBuffer & getBuffer()
Returns reference to output buffer.
Definition: pkt.h:174
isc::dhcp::Pkt::getIface
std::string getIface() const
Returns interface name.
Definition: pkt.h:504
isc::dhcp::Pkt::getMACFromSrcLinkLocalAddr
virtual HWAddrPtr getMACFromSrcLinkLocalAddr()=0
Attempts to obtain MAC address from source link-local IPv6 address.
isc::dhcp::OptionCollection
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:41
isc::dhcp::Pkt::getRemoteHWAddr
HWAddrPtr getRemoteHWAddr() const
Returns the remote HW address obtained from raw sockets.
Definition: pkt.h:551
isc::dhcp::Pkt::getNonCopiedOption
OptionPtr getNonCopiedOption(const uint16_t type) const
Returns the first option of specified type without copying.
Definition: pkt.cc:61
isc::dhcp::Pkt::getMACFromIPv6
HWAddrPtr getMACFromIPv6(const isc::asiolink::IOAddress &addr)
Attempts to convert IPv6 address into MAC.
Definition: pkt.cc:239
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::dhcp::HWAddrPtr
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
isc::dhcp::Pkt::copy_retrieved_options_
bool copy_retrieved_options_
Indicates if a copy of the retrieved option should be returned when Pkt::getOption is called.
Definition: pkt.h:762
isc::dhcp::Pkt::ifindex_
int64_t ifindex_
Interface index.
Definition: pkt.h:728
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::dhcp::Pkt::setLocalPort
void setLocalPort(uint16_t local)
Sets local UDP (and soon TCP) port.
Definition: pkt.h:441
isc::dhcp::Pkt::toText
virtual std::string toText() const =0
Returns text representation of the packet.
isc::dhcp::ScopedEnableOptionsCopy::ScopedEnableOptionsCopy
ScopedEnableOptionsCopy(const PktTypePtr &pkt1, const PktTypePtr &pkt2=PktTypePtr())
Constructor.
Definition: pkt.h:52
isc::dhcp::Pkt::addClass
void addClass(const isc::dhcp::ClientClass &client_class, bool required=false)
Adds packet to a specified class.
Definition: pkt.cc:100
isc::dhcp::Pkt
Base class for classes representing DHCP messages.
Definition: pkt.h:90
isc::dhcp::Pkt::data_
OptionBuffer data_
Unparsed data (in received packets).
Definition: pkt.h:312
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
isc::dhcp::Pkt::remote_hwaddr_
HWAddrPtr remote_hwaddr_
Definition: pkt.h:768
callout_handle_associate.h
isc::dhcp::Pkt::buffer_out_
isc::util::OutputBuffer buffer_out_
Output buffer (used during message transmission)
Definition: pkt.h:756
isc::dhcp::Pkt::getMAC
HWAddrPtr getMAC(uint32_t hw_addr_src)
Returns MAC address.
Definition: pkt.cc:142
isc::dhcp::Pkt::setLocalAddr
void setLocalAddr(const isc::asiolink::IOAddress &local)
Sets local IP address.
Definition: pkt.h:424
isc::dhcp::Pkt::remote_addr_
isc::asiolink::IOAddress remote_addr_
Remote IP address.
Definition: pkt.h:740
isc::dhcp::ScopedEnableOptionsCopy::~ScopedEnableOptionsCopy
~ScopedEnableOptionsCopy()
Destructor.
Definition: pkt.h:66
isc::dhcp::Pkt::getLocalAddr
const isc::asiolink::IOAddress & getLocalAddr() const
Returns local IP address.
Definition: pkt.h:431
isc::dhcp::Pkt::~Pkt
virtual ~Pkt()
Virtual destructor.
Definition: pkt.h:579
buffer.h
isc::dhcp::Pkt::timestamp_
boost::posix_time::ptime timestamp_
packet timestamp
Definition: pkt.h:765
hwaddr.h
isc::dhcp::Pkt::Pkt
Pkt(uint32_t transid, const isc::asiolink::IOAddress &local_addr, const isc::asiolink::IOAddress &remote_addr, uint16_t local_port, uint16_t remote_port)
Constructor.
Definition: pkt.cc:17
isc::dhcp::Pkt::setTransid
void setTransid(uint32_t transid)
Sets transaction-id value.
Definition: pkt.h:261
isc::dhcp::Pkt::getMACFromDUID
virtual HWAddrPtr getMACFromDUID()=0
Attempts to obtain MAC address from DUID-LL or DUID-LLT.
isc::dhcp::ClientClass
std::string ClientClass
Defines a single class name.
Definition: classify.h:37
isc::dhcp::Pkt::getMACFromDocsisCMTS
virtual HWAddrPtr getMACFromDocsisCMTS()=0
Attempts to extract MAC/Hardware address from DOCSIS options inserted by the CMTS (the relay agent)
isc::dhcp::Pkt::local_port_
uint16_t local_port_
local TDP or UDP port
Definition: pkt.h:743
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
isc::dhcp::Pkt::pack
virtual void pack()=0
Prepares on-wire format of DHCP (either v4 or v6) packet.
isc::dhcp::Pkt::getMACFromIPv6RelayOpt
virtual HWAddrPtr getMACFromIPv6RelayOpt()=0
Attempts to obtain MAC address from relay option client-linklayer-addr.
isc::dhcp::Pkt::setCopyRetrievedOptions
virtual void setCopyRetrievedOptions(const bool copy)
Controls whether the option retrieved by the Pkt::getOption should be copied before being returned.
Definition: pkt.h:368
isc::dhcp::Pkt::repack
void repack()
Copies content of input buffer to output buffer.
Definition: pkt.cc:112
isc::dhcp::Pkt::getLocalPort
uint16_t getLocalPort() const
Returns local UDP (and soon TCP) port.
Definition: pkt.h:451
isc::dhcp::ScopedEnableOptionsCopy::PktTypePtr
boost::shared_ptr< PktType > PktTypePtr
Pointer to an encapsulated packet.
Definition: pkt.h:44
isc::dhcp::Pkt::getMACFromDocsisModem
virtual HWAddrPtr getMACFromDocsisModem()=0
Attempts to extract MAC/Hardware address from DOCSIS options inserted by the modem itself.
isc::dhcp::Pkt::getLabel
virtual std::string getLabel() const
Returns text representation primary packet identifiers.
Definition: pkt.h:202
isc::NotImplemented
A generic exception that is thrown when a function is not implemented.
Definition: exceptions/exceptions.h:165
option.h
isc::dhcp::Pkt::transid_
uint32_t transid_
Transaction-id (32 bits for v4, 24 bits for v6)
Definition: pkt.h:718
isc::dhcp::Pkt::iface_
std::string iface_
Name of the network interface the packet was received/to be sent over.
Definition: pkt.h:721
isc::dhcp::Pkt::remote_port_
uint16_t remote_port_
remote TCP or UDP port
Definition: pkt.h:746
isc::dhcp::Pkt::getOption
OptionPtr getOption(const uint16_t type)
Returns the first option of specified type.
Definition: pkt.cc:70
isc::dhcp::Pkt::resetIndex
void resetIndex()
Resets interface index to negative value.
Definition: pkt.h:480
isc::dhcp::Pkt::indexSet
bool indexSet() const
Checks if interface index has been set.
Definition: pkt.h:494
isc::dhcp::Pkt::delOption
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: pkt.cc:83
isc::dhcp::Pkt::len
virtual size_t len()=0
Returns packet size in binary format.
isc::dhcp::Pkt::getRemotePort
uint16_t getRemotePort() const
Returns remote port.
Definition: pkt.h:468
isc::dhcp::Pkt::getType
virtual uint8_t getType() const =0
Returns message type (e.g.
isc::dhcp::ClientClasses
Container for storing client class names.
Definition: classify.h:43
isc::hooks::CalloutHandleAssociate
Base class for classes which need to be associated with a CalloutHandle object.
Definition: callout_handle_associate.h:37