Kea  2.3.5-git
pkt.h
Go to the documentation of this file.
1 // Copyright (C) 2014-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 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 
52  ScopedEnableOptionsCopy(const PktTypePtr& pkt1,
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  return (buffer_out_);
176  }
177 
185  virtual void addOption(const OptionPtr& opt);
186 
195  bool delOption(uint16_t type);
196 
204  virtual std::string getLabel() const {
205  isc_throw(NotImplemented, "Pkt::getLabel()");
206  }
207 
217  virtual std::string toText() const = 0;
218 
229  virtual size_t len() = 0;
230 
238  virtual uint8_t getType() const = 0;
239 
247  virtual void setType(uint8_t type) = 0;
248 
258  virtual const char* getName() const = 0;
259 
263  void setTransid(uint32_t transid) {
264  transid_ = transid;
265  }
266 
270  uint32_t getTransid() const {
271  return (transid_);
272  }
273 
278  bool inClass(const isc::dhcp::ClientClass& client_class);
279 
288  void addClass(const isc::dhcp::ClientClass& client_class,
289  bool required = false);
290 
299  void addSubClass(const isc::dhcp::ClientClass& class_def,
300  const isc::dhcp::ClientClass& subclass);
301 
309  const ClientClasses& getClasses(bool required = false) const {
310  return (!required ? classes_ : required_classes_);
311  }
312 
323  return (subclasses_);
324  }
325 
335 
336 protected:
337 
349  OptionPtr getNonCopiedOption(const uint16_t type) const;
350 
363  OptionCollection getNonCopiedOptions(const uint16_t opt_type) const;
364 
365 public:
366 
377  OptionPtr getOption(const uint16_t type);
378 
386  isc::dhcp::OptionCollection getOptions(const uint16_t type);
387 
412  virtual void setCopyRetrievedOptions(const bool copy) {
413  copy_retrieved_options_ = copy;
414  }
415 
421  bool isCopyRetrievedOptions() const {
422  return (copy_retrieved_options_);
423  }
424 
431  void updateTimestamp();
432 
439  const boost::posix_time::ptime& getTimestamp() const {
440  return timestamp_;
441  }
442 
447  void setTimestamp(boost::posix_time::ptime& timestamp) {
448  timestamp_ = timestamp;
449  }
450 
457  void repack();
458 
463  remote_addr_ = remote;
464  }
465 
470  return (remote_addr_);
471  }
472 
477  local_addr_ = local;
478  }
479 
484  return (local_addr_);
485  }
486 
493  void setLocalPort(uint16_t local) {
494  local_port_ = local;
495  }
496 
503  uint16_t getLocalPort() const {
504  return (local_port_);
505  }
506 
513  void setRemotePort(uint16_t remote) {
514  remote_port_ = remote;
515  }
516 
520  uint16_t getRemotePort() const {
521  return (remote_port_);
522  }
523 
527  void setIndex(int ifindex) {
528  ifindex_ = ifindex;
529  }
530 
532  void resetIndex() {
533  ifindex_ = -1;
534  }
535 
539  int getIndex() const {
540  return (ifindex_);
541  }
542 
546  bool indexSet() const {
547  return (ifindex_ >= 0);
548  }
549 
556  std::string getIface() const {
557  return (iface_);
558  }
559 
566  void setIface(const std::string& iface) {
567  iface_ = iface;
568  }
569 
581  void setRemoteHWAddr(const HWAddrPtr& hw_addr);
582 
601  void setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
602  const std::vector<uint8_t>& hw_addr);
603 
608  return (remote_hwaddr_);
609  }
610 
628  HWAddrPtr getMAC(uint32_t hw_addr_src);
629 
635  virtual ~Pkt() {
636  }
637 
645 
653 
661 
671 
672 protected:
673 
693  virtual HWAddrPtr getMACFromSrcLinkLocalAddr() = 0;
694 
709  virtual HWAddrPtr getMACFromIPv6RelayOpt() = 0;
710 
723  virtual HWAddrPtr getMACFromDUID() = 0;
724 
737  virtual HWAddrPtr getMACFromRemoteIdRelayOption() = 0;
738 
752  HWAddrPtr
753  getMACFromIPv6(const isc::asiolink::IOAddress& addr);
754 
766  virtual HWAddrPtr getMACFromDocsisModem() = 0;
767 
779  virtual HWAddrPtr getMACFromDocsisCMTS() = 0;
780 
782  uint32_t transid_;
783 
785  std::string iface_;
786 
792  int64_t ifindex_;
793 
799 
805 
807  uint16_t local_port_;
808 
810  uint16_t remote_port_;
811 
821 
827 
829  boost::posix_time::ptime timestamp_;
830 
831  // remote HW address (src if receiving packet, dst if sending packet)
833 
834 private:
835 
847  virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
848  const std::vector<uint8_t>& hw_addr,
849  HWAddrPtr& storage);
850 };
851 
853 typedef boost::shared_ptr<isc::dhcp::Pkt> PktPtr;
854 
868 public:
869 
875  ScopedSubOptionsCopy(const OptionPtr& opt) : option_(opt) {
876  if (opt) {
877  options_ = opt->getMutableOptions();
878  }
879  }
880 
885  if (option_) {
886  option_->getMutableOptions() = options_;
887  }
888  }
889 
890 private:
891 
893  OptionPtr option_;
894 
896  OptionCollection options_;
897 };
898 
911 template<typename PktType>
913 public:
914 
920  ScopedPktOptionsCopy(PktType& pkt) : pkt_(pkt), options_(pkt.options_) {
921  }
922 
927  pkt_.options_ = options_;
928  }
929 
930 private:
931 
933  PktType& pkt_;
934 
936  OptionCollection options_;
937 };
938 
939 } // end of namespace isc::dhcp
940 } // end of namespace isc
941 
942 #endif
isc::asiolink::IOAddress local_addr_
Local IP (v4 or v6) address.
Definition: pkt.h:798
int64_t ifindex_
Interface index.
Definition: pkt.h:792
virtual ~Pkt()
Virtual destructor.
Definition: pkt.h:635
OptionBuffer data_
Unparsed data (in received packets).
Definition: pkt.h:334
A generic exception that is thrown when a function is not implemented.
const ClientClasses & getClasses(bool required=false) const
Returns the class set.
Definition: pkt.h:309
std::string iface_
Name of the network interface the packet was received/to be sent over.
Definition: pkt.h:785
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
uint32_t transid_
Transaction-id (32 bits for v4, 24 bits for v6)
Definition: pkt.h:782
Defines elements for storing the names of client classes.
Base class for classes representing DHCP messages.
Definition: pkt.h:90
virtual std::string getLabel() const
Returns text representation primary packet identifiers.
Definition: pkt.h:204
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
void setTransid(uint32_t transid)
Sets transaction-id value.
Definition: pkt.h:263
uint16_t getRemotePort() const
Returns remote port.
Definition: pkt.h:520
virtual void setCopyRetrievedOptions(const bool copy)
Controls whether the option retrieved by the Pkt::getOption should be copied before being returned...
Definition: pkt.h:412
void setIface(const std::string &iface)
Sets interface name.
Definition: pkt.h:566
RAII object enabling copying options retrieved from the packet.
Definition: pkt.h:40
std::string getIface() const
Returns interface name.
Definition: pkt.h:556
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
~ScopedPktOptionsCopy()
Destructor.
Definition: pkt.h:926
void setRemoteAddr(const isc::asiolink::IOAddress &remote)
Sets remote IP address.
Definition: pkt.h:462
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
ClientClasses classes_
Classes this packet belongs to.
Definition: pkt.h:644
const boost::posix_time::ptime & getTimestamp() const
Returns packet timestamp.
Definition: pkt.h:439
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1360
Base class for classes which need to be associated with a CalloutHandle object.
const SubClassRelationContainer & getSubClassesRelations() const
Returns the class set including template classes associated with subclasses.
Definition: pkt.h:322
ScopedSubOptionsCopy(const OptionPtr &opt)
Constructor.
Definition: pkt.h:875
void setLocalAddr(const isc::asiolink::IOAddress &local)
Sets local IP address.
Definition: pkt.h:476
void setRemotePort(uint16_t remote)
Sets remote UDP (and soon TCP) port.
Definition: pkt.h:513
void setLocalPort(uint16_t local)
Sets local UDP (and soon TCP) port.
Definition: pkt.h:493
void setIndex(int ifindex)
Sets interface index.
Definition: pkt.h:527
SubClassRelationContainer subclasses_
SubClasses this packet belongs to.
Definition: pkt.h:660
void resetIndex()
Resets interface index to negative value.
Definition: pkt.h:532
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
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition: option.h:40
isc::util::OutputBuffer & getBuffer()
Returns reference to output buffer.
Definition: pkt.h:174
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition: pkt.h:853
RAII object enabling duplication of the stored options and restoring the original options on destruct...
Definition: pkt.h:912
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
~ScopedEnableOptionsCopy()
Destructor.
Definition: pkt.h:66
void setTimestamp(boost::posix_time::ptime &timestamp)
Set packet timestamp.
Definition: pkt.h:447
bool copy_retrieved_options_
Indicates if a copy of the retrieved option should be returned when Pkt::getOption is called...
Definition: pkt.h:826
uint16_t remote_port_
remote TCP or UDP port
Definition: pkt.h:810
const isc::asiolink::IOAddress & getRemoteAddr() const
Returns remote IP address.
Definition: pkt.h:469
ScopedEnableOptionsCopy(const PktTypePtr &pkt1, const PktTypePtr &pkt2=PktTypePtr())
Constructor.
Definition: pkt.h:52
Defines the logger used by the top-level component of kea-lfc.
~ScopedSubOptionsCopy()
Destructor.
Definition: pkt.h:884
HWAddrPtr remote_hwaddr_
Definition: pkt.h:832
uint16_t local_port_
local TDP or UDP port
Definition: pkt.h:807
uint16_t getLocalPort() const
Returns local UDP (and soon TCP) port.
Definition: pkt.h:503
bool isCopyRetrievedOptions() const
Returns whether the copying of retrieved options is enabled.
Definition: pkt.h:421
isc::asiolink::IOAddress remote_addr_
Remote IP address.
Definition: pkt.h:804
int getIndex() const
Returns interface index.
Definition: pkt.h:539
ScopedPktOptionsCopy(PktType &pkt)
Constructor.
Definition: pkt.h:920
std::string ClientClass
Defines a single class name.
Definition: classify.h:42
isc::util::OutputBuffer buffer_out_
Output buffer (used during message transmission)
Definition: pkt.h:820
boost::shared_ptr< PktType > PktTypePtr
Pointer to an encapsulated packet.
Definition: pkt.h:44
const isc::asiolink::IOAddress & getLocalAddr() const
Returns local IP address.
Definition: pkt.h:483
HWAddrPtr getRemoteHWAddr() const
Returns the remote HW address obtained from raw sockets.
Definition: pkt.h:607
RAII object enabling duplication of the stored options and restoring the original options on destruct...
Definition: pkt.h:867
ClientClasses required_classes_
Classes which are required to be evaluated.
Definition: pkt.h:652
uint32_t getTransid() const
Returns value of transaction-id field.
Definition: pkt.h:270
isc::dhcp::OptionCollection options_
Collection of options present in this message.
Definition: pkt.h:670
Container for storing client class names.
Definition: classify.h:108
boost::posix_time::ptime timestamp_
packet timestamp
Definition: pkt.h:829
bool indexSet() const
Checks if interface index has been set.
Definition: pkt.h:546