Kea  2.3.9
pkt.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2023 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 <limits>
21 #include <utility>
22 
23 namespace isc {
24 
25 namespace dhcp {
26 
30 constexpr unsigned int UNSET_IFINDEX = std::numeric_limits<unsigned int>::max();
31 
45 template<typename PktType>
47 public:
48 
50  typedef boost::shared_ptr<PktType> PktTypePtr;
51 
59  const PktTypePtr& pkt2 = PktTypePtr())
60  : pkts_(pkt1, pkt2) {
61  if (pkt1) {
62  pkt1->setCopyRetrievedOptions(true);
63  }
64  if (pkt2) {
65  pkt2->setCopyRetrievedOptions(true);
66  }
67  }
68 
73  if (pkts_.first) {
74  pkts_.first->setCopyRetrievedOptions(false);
75  }
76  if (pkts_.second) {
77  pkts_.second->setCopyRetrievedOptions(false);
78  }
79  }
80 
81 private:
82 
84  std::pair<PktTypePtr, PktTypePtr> pkts_;
85 };
86 
97 protected:
98 
111  Pkt(uint32_t transid, const isc::asiolink::IOAddress& local_addr,
112  const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
113  uint16_t remote_port);
114 
128  Pkt(const uint8_t* buf, uint32_t len,
129  const isc::asiolink::IOAddress& local_addr,
130  const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
131  uint16_t remote_port);
132 
133 public:
134 
150  virtual void pack() = 0;
151 
166  virtual void unpack() = 0;
167 
181  return (buffer_out_);
182  }
183 
191  virtual void addOption(const OptionPtr& opt);
192 
201  bool delOption(uint16_t type);
202 
210  virtual std::string getLabel() const {
211  isc_throw(NotImplemented, "Pkt::getLabel()");
212  }
213 
223  virtual std::string toText() const = 0;
224 
235  virtual size_t len() = 0;
236 
244  virtual uint8_t getType() const = 0;
245 
253  virtual void setType(uint8_t type) = 0;
254 
264  virtual const char* getName() const = 0;
265 
269  void setTransid(uint32_t transid) {
270  transid_ = transid;
271  }
272 
276  uint32_t getTransid() const {
277  return (transid_);
278  }
279 
284  bool inClass(const isc::dhcp::ClientClass& client_class);
285 
294  void addClass(const isc::dhcp::ClientClass& client_class,
295  bool required = false);
296 
305  void addSubClass(const isc::dhcp::ClientClass& class_def,
306  const isc::dhcp::ClientClass& subclass);
307 
315  const ClientClasses& getClasses(bool required = false) const {
316  return (!required ? classes_ : required_classes_);
317  }
318 
329  return (subclasses_);
330  }
331 
341 
342 protected:
343 
355  OptionPtr getNonCopiedOption(const uint16_t type) const;
356 
369  OptionCollection getNonCopiedOptions(const uint16_t opt_type) const;
370 
371 public:
372 
383  OptionPtr getOption(const uint16_t type);
384 
392  isc::dhcp::OptionCollection getOptions(const uint16_t type);
393 
418  virtual void setCopyRetrievedOptions(const bool copy) {
420  }
421 
427  bool isCopyRetrievedOptions() const {
428  return (copy_retrieved_options_);
429  }
430 
437  void updateTimestamp();
438 
445  const boost::posix_time::ptime& getTimestamp() const {
446  return timestamp_;
447  }
448 
453  void setTimestamp(boost::posix_time::ptime& timestamp) {
454  timestamp_ = timestamp;
455  }
456 
463  void repack();
464 
469  remote_addr_ = remote;
470  }
471 
476  return (remote_addr_);
477  }
478 
483  local_addr_ = local;
484  }
485 
490  return (local_addr_);
491  }
492 
499  void setLocalPort(uint16_t local) {
500  local_port_ = local;
501  }
502 
509  uint16_t getLocalPort() const {
510  return (local_port_);
511  }
512 
519  void setRemotePort(uint16_t remote) {
520  remote_port_ = remote;
521  }
522 
526  uint16_t getRemotePort() const {
527  return (remote_port_);
528  }
529 
533  void setIndex(const unsigned int ifindex) {
534  ifindex_ = ifindex;
535  }
536 
538  void resetIndex() {
540  }
541 
545  int getIndex() const {
546  return (ifindex_);
547  }
548 
552  bool indexSet() const {
553  return (ifindex_ != UNSET_IFINDEX);
554  }
555 
562  std::string getIface() const {
563  return (iface_);
564  }
565 
572  void setIface(const std::string& iface) {
573  iface_ = iface;
574  }
575 
587  void setRemoteHWAddr(const HWAddrPtr& hw_addr);
588 
607  void setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
608  const std::vector<uint8_t>& hw_addr);
609 
614  return (remote_hwaddr_);
615  }
616 
634  HWAddrPtr getMAC(uint32_t hw_addr_src);
635 
641  virtual ~Pkt() {
642  }
643 
651 
659 
667 
677 
678 protected:
679 
700 
716 
729  virtual HWAddrPtr getMACFromDUID() = 0;
730 
744 
758  HWAddrPtr
760 
773 
786 
788  uint32_t transid_;
789 
791  std::string iface_;
792 
798  unsigned int ifindex_;
799 
805 
811 
813  uint16_t local_port_;
814 
816  uint16_t remote_port_;
817 
827 
833 
835  boost::posix_time::ptime timestamp_;
836 
837  // remote HW address (src if receiving packet, dst if sending packet)
839 
840 private:
841 
853  virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
854  const std::vector<uint8_t>& hw_addr,
855  HWAddrPtr& storage);
856 };
857 
859 typedef boost::shared_ptr<isc::dhcp::Pkt> PktPtr;
860 
874 public:
875 
881  ScopedSubOptionsCopy(const OptionPtr& opt) : option_(opt) {
882  if (opt) {
883  options_ = opt->getMutableOptions();
884  }
885  }
886 
891  if (option_) {
892  option_->getMutableOptions() = options_;
893  }
894  }
895 
896 private:
897 
899  OptionPtr option_;
900 
902  OptionCollection options_;
903 };
904 
917 template<typename PktType>
919 public:
920 
926  ScopedPktOptionsCopy(PktType& pkt) : pkt_(pkt), options_(pkt.options_) {
927  }
928 
933  pkt_.options_ = options_;
934  }
935 
936 private:
937 
939  PktType& pkt_;
940 
942  OptionCollection options_;
943 };
944 
945 } // end of namespace isc::dhcp
946 } // end of namespace isc
947 
948 #endif
Defines elements for storing the names of client classes.
A generic exception that is thrown when a function is not implemented.
Container for storing client class names.
Definition: classify.h:108
Base class for classes representing DHCP messages.
Definition: pkt.h:96
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: pkt.cc:95
virtual HWAddrPtr getMACFromDocsisModem()=0
Attempts to extract MAC/Hardware address from DOCSIS options inserted by the modem itself.
virtual uint8_t getType() const =0
Returns message type (e.g.
isc::dhcp::OptionCollection getOptions(const uint16_t type)
Returns all instances of specified type.
Definition: pkt.cc:75
isc::asiolink::IOAddress remote_addr_
Remote IP address.
Definition: pkt.h:810
virtual HWAddrPtr getMACFromDocsisCMTS()=0
Attempts to extract MAC/Hardware address from DOCSIS options inserted by the CMTS (the relay agent)
virtual void setType(uint8_t type)=0
Sets message type (e.g.
ClientClasses required_classes_
Classes which are required to be evaluated.
Definition: pkt.h:658
const SubClassRelationContainer & getSubClassesRelations() const
Returns the class set including template classes associated with subclasses.
Definition: pkt.h:328
bool isCopyRetrievedOptions() const
Returns whether the copying of retrieved options is enabled.
Definition: pkt.h:427
uint16_t local_port_
local TDP or UDP port
Definition: pkt.h:813
int getIndex() const
Returns interface index.
Definition: pkt.h:545
const ClientClasses & getClasses(bool required=false) const
Returns the class set.
Definition: pkt.h:315
void resetIndex()
Resets interface index to negative value.
Definition: pkt.h:538
uint32_t transid_
Transaction-id (32 bits for v4, 24 bits for v6)
Definition: pkt.h:788
void repack()
Copies content of input buffer to output buffer.
Definition: pkt.cc:136
isc::asiolink::IOAddress local_addr_
Local IP (v4 or v6) address.
Definition: pkt.h:804
virtual void pack()=0
Prepares on-wire format of DHCP (either v4 or v6) packet.
const isc::asiolink::IOAddress & getLocalAddr() const
Returns local IP address.
Definition: pkt.h:489
const boost::posix_time::ptime & getTimestamp() const
Returns packet timestamp.
Definition: pkt.h:445
virtual HWAddrPtr getMACFromRemoteIdRelayOption()=0
Attempts to obtain MAC address from remote-id relay option.
virtual void unpack()=0
Parses on-wire form of DHCP (either v4 or v6) packet.
OptionBuffer data_
Unparsed data (in received packets).
Definition: pkt.h:340
HWAddrPtr getRemoteHWAddr() const
Returns the remote HW address obtained from raw sockets.
Definition: pkt.h:613
virtual HWAddrPtr getMACFromSrcLinkLocalAddr()=0
Attempts to obtain MAC address from source link-local IPv6 address.
ClientClasses classes_
Classes this packet belongs to.
Definition: pkt.h:650
virtual size_t len()=0
Returns packet size in binary format.
void setIface(const std::string &iface)
Sets interface name.
Definition: pkt.h:572
virtual const char * getName() const =0
Returns name of the DHCP message.
bool indexSet() const
Checks if interface index has been set.
Definition: pkt.h:552
uint16_t getLocalPort() const
Returns local UDP (and soon TCP) port.
Definition: pkt.h:509
void setLocalPort(uint16_t local)
Sets local UDP (and soon TCP) port.
Definition: pkt.h:499
uint16_t remote_port_
remote TCP or UDP port
Definition: pkt.h:816
void setIndex(const unsigned int ifindex)
Sets interface index.
Definition: pkt.h:533
HWAddrPtr remote_hwaddr_
Definition: pkt.h:838
uint32_t getTransid() const
Returns value of transaction-id field.
Definition: pkt.h:276
isc::dhcp::OptionCollection options_
Collection of options present in this message.
Definition: pkt.h:676
isc::util::OutputBuffer buffer_out_
Output buffer (used during message transmission)
Definition: pkt.h:826
std::string getIface() const
Returns interface name.
Definition: pkt.h:562
virtual HWAddrPtr getMACFromDUID()=0
Attempts to obtain MAC address from DUID-LL or DUID-LLT.
virtual void setCopyRetrievedOptions(const bool copy)
Controls whether the option retrieved by the Pkt::getOption should be copied before being returned.
Definition: pkt.h:418
SubClassRelationContainer subclasses_
SubClasses this packet belongs to.
Definition: pkt.h:666
OptionCollection getNonCopiedOptions(const uint16_t opt_type) const
Returns all option instances of specified type without copying.
Definition: pkt.cc:68
void setTimestamp(boost::posix_time::ptime &timestamp)
Set packet timestamp.
Definition: pkt.h:453
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
uint16_t getRemotePort() const
Returns remote port.
Definition: pkt.h:526
OptionPtr getOption(const uint16_t type)
Returns the first option of specified type.
Definition: pkt.cc:55
void setRemoteHWAddr(const HWAddrPtr &hw_addr)
Sets remote hardware address.
Definition: pkt.cc:149
void addSubClass(const isc::dhcp::ClientClass &class_def, const isc::dhcp::ClientClass &subclass)
Adds a specified subclass to the packet.
Definition: pkt.cc:120
bool inClass(const isc::dhcp::ClientClass &client_class)
Checks whether a client belongs to a given class.
Definition: pkt.cc:106
virtual HWAddrPtr getMACFromIPv6RelayOpt()=0
Attempts to obtain MAC address from relay option client-linklayer-addr.
unsigned int ifindex_
Interface index.
Definition: pkt.h:798
boost::posix_time::ptime timestamp_
packet timestamp
Definition: pkt.h:835
void setTransid(uint32_t transid)
Sets transaction-id value.
Definition: pkt.h:269
HWAddrPtr getMAC(uint32_t hw_addr_src)
Returns MAC address.
Definition: pkt.cc:165
virtual std::string toText() const =0
Returns text representation of the packet.
void updateTimestamp()
Update packet timestamp.
Definition: pkt.cc:132
isc::util::OutputBuffer & getBuffer()
Returns reference to output buffer.
Definition: pkt.h:180
bool copy_retrieved_options_
Indicates if a copy of the retrieved option should be returned when Pkt::getOption is called.
Definition: pkt.h:832
std::string iface_
Name of the network interface the packet was received/to be sent over.
Definition: pkt.h:791
void addClass(const isc::dhcp::ClientClass &client_class, bool required=false)
Adds a specified class to the packet.
Definition: pkt.cc:111
OptionPtr getNonCopiedOption(const uint16_t type) const
Returns the first option of specified type without copying.
Definition: pkt.cc:46
void setRemoteAddr(const isc::asiolink::IOAddress &remote)
Sets remote IP address.
Definition: pkt.h:468
const isc::asiolink::IOAddress & getRemoteAddr() const
Returns remote IP address.
Definition: pkt.h:475
HWAddrPtr getMACFromIPv6(const isc::asiolink::IOAddress &addr)
Attempts to convert IPv6 address into MAC.
Definition: pkt.cc:262
void setLocalAddr(const isc::asiolink::IOAddress &local)
Sets local IP address.
Definition: pkt.h:482
void setRemotePort(uint16_t remote)
Sets remote UDP (and soon TCP) port.
Definition: pkt.h:519
virtual std::string getLabel() const
Returns text representation primary packet identifiers.
Definition: pkt.h:210
virtual void addOption(const OptionPtr &opt)
Adds an option to this packet.
Definition: pkt.cc:41
virtual ~Pkt()
Virtual destructor.
Definition: pkt.h:641
RAII object enabling copying options retrieved from the packet.
Definition: pkt.h:46
ScopedEnableOptionsCopy(const PktTypePtr &pkt1, const PktTypePtr &pkt2=PktTypePtr())
Constructor.
Definition: pkt.h:58
boost::shared_ptr< PktType > PktTypePtr
Pointer to an encapsulated packet.
Definition: pkt.h:50
~ScopedEnableOptionsCopy()
Destructor.
Definition: pkt.h:72
RAII object enabling duplication of the stored options and restoring the original options on destruct...
Definition: pkt.h:918
~ScopedPktOptionsCopy()
Destructor.
Definition: pkt.h:932
ScopedPktOptionsCopy(PktType &pkt)
Constructor.
Definition: pkt.h:926
RAII object enabling duplication of the stored options and restoring the original options on destruct...
Definition: pkt.h:873
ScopedSubOptionsCopy(const OptionPtr &opt)
Constructor.
Definition: pkt.h:881
~ScopedSubOptionsCopy()
Destructor.
Definition: pkt.h:890
Base class for classes which need to be associated with a CalloutHandle object.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1395
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition: pkt.h:859
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:40
boost::multi_index_container< SubClassRelation, boost::multi_index::indexed_by< boost::multi_index::sequenced< boost::multi_index::tag< TemplateClassSequenceTag > >, boost::multi_index::hashed_unique< boost::multi_index::tag< TemplateClassNameTag >, boost::multi_index::member< SubClassRelation, ClientClass, &SubClassRelation::class_def_ > > > > SubClassRelationContainer
the subclass multi-index.
Definition: classify.h:102
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
constexpr unsigned int UNSET_IFINDEX
A value used to signal that the interface index was not set.
Definition: pkt.h:30
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
Defines the logger used by the top-level component of kea-lfc.