Kea 2.5.8
pkt.h
Go to the documentation of this file.
1// Copyright (C) 2014-2024 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
23namespace isc {
24
25namespace dhcp {
26
30constexpr unsigned int UNSET_IFINDEX = std::numeric_limits<unsigned int>::max();
31
45template<typename PktType>
47public:
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
81private:
82
84 std::pair<PktTypePtr, PktTypePtr> pkts_;
85};
86
87
89class PktEvent {
90public:
93 static const std::string SOCKET_RECEIVED;
94
97 static const std::string BUFFER_READ;
98
101 static const std::string RESPONSE_SENT;
102
107 PktEvent(const std::string& label, boost::posix_time::ptime timestamp)
108 : label_(label), timestamp_(timestamp) {
109 }
110
112 ~PktEvent() = default;
113
117 static boost::posix_time::ptime now() {
118 return (boost::posix_time::microsec_clock::universal_time());
119 }
120
124 static boost::posix_time::ptime& EMPTY_TIME() {
125 static boost::posix_time::ptime empty_time;
126 return (empty_time);
127 }
128
132 static boost::posix_time::ptime& MIN_TIME() {
133 static auto min_time = boost::posix_time::ptime(boost::posix_time::min_date_time);
134 return (min_time);
135 }
136
140 static boost::posix_time::ptime& MAX_TIME() {
141 static auto max_time = boost::posix_time::ptime(boost::posix_time::max_date_time);
142 return (max_time);
143 }
144
146 std::string label_;
147
149 boost::posix_time::ptime timestamp_;
150};
151
162protected:
163
176 Pkt(uint32_t transid, const isc::asiolink::IOAddress& local_addr,
177 const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
178 uint16_t remote_port);
179
193 Pkt(const uint8_t* buf, uint32_t len,
194 const isc::asiolink::IOAddress& local_addr,
195 const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
196 uint16_t remote_port);
197
198public:
199
215 virtual void pack() = 0;
216
231 virtual void unpack() = 0;
232
246 return (buffer_out_);
247 }
248
256 virtual void addOption(const OptionPtr& opt);
257
266 bool delOption(uint16_t type);
267
275 virtual std::string getLabel() const {
276 isc_throw(NotImplemented, "Pkt::getLabel()");
277 }
278
288 virtual std::string toText() const = 0;
289
300 virtual size_t len() = 0;
301
309 virtual uint8_t getType() const = 0;
310
318 virtual void setType(uint8_t type) = 0;
319
329 virtual const char* getName() const = 0;
330
334 void setTransid(uint32_t transid) {
335 transid_ = transid;
336 }
337
341 uint32_t getTransid() const {
342 return (transid_);
343 }
344
349 bool inClass(const isc::dhcp::ClientClass& client_class);
350
359 void addClass(const isc::dhcp::ClientClass& client_class,
360 bool required = false);
361
370 void addSubClass(const isc::dhcp::ClientClass& class_def,
371 const isc::dhcp::ClientClass& subclass);
372
380 const ClientClasses& getClasses(bool required = false) const {
381 return (!required ? classes_ : required_classes_);
382 }
383
393 return (subclasses_);
394 }
395
405
406protected:
407
419 OptionPtr getNonCopiedOption(const uint16_t type) const;
420
433 OptionCollection getNonCopiedOptions(const uint16_t opt_type) const;
434
435public:
436
441
452 OptionPtr getOption(const uint16_t type);
453
461 isc::dhcp::OptionCollection getOptions(const uint16_t type);
462
487 virtual void setCopyRetrievedOptions(const bool copy) {
489 }
490
498 }
499
506 void updateTimestamp();
507
514 const boost::posix_time::ptime& getTimestamp() const {
515 return timestamp_;
516 }
517
521 void setTimestamp(boost::posix_time::ptime& timestamp) {
522 timestamp_ = timestamp;
523 }
524
530 void addPktEvent(const std::string& label,
531 const boost::posix_time::ptime& timestamp = PktEvent::now());
532
539 void addPktEvent(const std::string& label, const struct timeval& timestamp);
540
550 void setPktEvent(const std::string& label,
551 const boost::posix_time::ptime& timestamp = PktEvent::now());
552
556 void clearPktEvents();
557
562 boost::posix_time::ptime getPktEventTime(const std::string& label) const;
563
567 const std::list<PktEvent>& getPktEvents() {
568 return (events_);
569 }
570
575 std::string dumpPktEvents(bool verbose = false) const;
576
583 void repack();
584
589 remote_addr_ = remote;
590 }
591
596 return (remote_addr_);
597 }
598
603 local_addr_ = local;
604 }
605
610 return (local_addr_);
611 }
612
619 void setLocalPort(uint16_t local) {
620 local_port_ = local;
621 }
622
629 uint16_t getLocalPort() const {
630 return (local_port_);
631 }
632
639 void setRemotePort(uint16_t remote) {
640 remote_port_ = remote;
641 }
642
646 uint16_t getRemotePort() const {
647 return (remote_port_);
648 }
649
653 void setIndex(const unsigned int ifindex) {
654 ifindex_ = ifindex;
655 }
656
658 void resetIndex() {
660 }
661
665 int getIndex() const {
666 return (ifindex_);
667 }
668
672 bool indexSet() const {
673 return (ifindex_ != UNSET_IFINDEX);
674 }
675
682 std::string getIface() const {
683 return (iface_);
684 }
685
692 void setIface(const std::string& iface) {
693 iface_ = iface;
694 }
695
707 void setRemoteHWAddr(const HWAddrPtr& hw_addr);
708
727 void setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
728 const std::vector<uint8_t>& hw_addr);
729
734 return (remote_hwaddr_);
735 }
736
754 HWAddrPtr getMAC(uint32_t hw_addr_src);
755
761 virtual ~Pkt() {
762 }
763
771
779
787
797
798protected:
799
820
836
850
864
880
893
906
908 uint32_t transid_;
909
911 std::string iface_;
912
918 unsigned int ifindex_;
919
925
931
933 uint16_t local_port_;
934
936 uint16_t remote_port_;
937
947
953
955 boost::posix_time::ptime timestamp_;
956
957 // remote HW address (src if receiving packet, dst if sending packet)
959
960private:
961
973 virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
974 const std::vector<uint8_t>& hw_addr,
975 HWAddrPtr& storage);
976
978 std::list<PktEvent> events_;
979};
980
982typedef boost::shared_ptr<isc::dhcp::Pkt> PktPtr;
983
997public:
998
1004 ScopedSubOptionsCopy(const OptionPtr& opt) : option_(opt) {
1005 if (opt) {
1006 options_ = opt->getMutableOptions();
1007 }
1008 }
1009
1014 if (option_) {
1015 option_->getMutableOptions() = options_;
1016 }
1017 }
1018
1019private:
1020
1022 OptionPtr option_;
1023
1025 OptionCollection options_;
1026};
1027
1040template<typename PktType>
1042public:
1043
1049 ScopedPktOptionsCopy(PktType& pkt) : pkt_(pkt), options_(pkt.options_) {
1050 pkt_.options_ = pkt_.cloneOptions();
1051 }
1052
1057 pkt_.options_ = options_;
1058 }
1059
1060private:
1061
1063 PktType& pkt_;
1064
1066 OptionCollection options_;
1067};
1068
1069} // end of namespace isc::dhcp
1070} // end of namespace isc
1071
1072#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
Describes an event during the life cycle of a packet.
Definition: pkt.h:89
~PktEvent()=default
Destructor.
static boost::posix_time::ptime now()
Fetch the current UTC system time, microsecond precision.
Definition: pkt.h:117
std::string label_
Label identifying this event.
Definition: pkt.h:146
PktEvent(const std::string &label, boost::posix_time::ptime timestamp)
Constructor.
Definition: pkt.h:107
static boost::posix_time::ptime & MAX_TIME()
Fetches the maximum timestamp.
Definition: pkt.h:140
static const std::string BUFFER_READ
Event that marks when a packet is read from the socket buffer by application.
Definition: pkt.h:97
boost::posix_time::ptime timestamp_
Timestamp at which the event occurred.
Definition: pkt.h:149
static const std::string SOCKET_RECEIVED
Event that marks when a packet is placed in the socket buffer by the kernel.
Definition: pkt.h:93
static const std::string RESPONSE_SENT
Event that marks when a packet is been written to the socket by application.
Definition: pkt.h:101
static boost::posix_time::ptime & MIN_TIME()
Fetches the minimum timestamp.
Definition: pkt.h:132
static boost::posix_time::ptime & EMPTY_TIME()
Fetch an empty timestamp, used for logic comparisons.
Definition: pkt.h:124
Base class for classes representing DHCP messages.
Definition: pkt.h:161
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: pkt.cc:110
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:91
isc::asiolink::IOAddress remote_addr_
Remote IP address.
Definition: pkt.h:930
virtual HWAddrPtr getMACFromDocsisCMTS()=0
Attempts to extract MAC/Hardware address from DOCSIS options inserted by the CMTS (the relay agent)
virtual const char * getName() const =0
Returns name of the DHCP message.
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:778
bool isCopyRetrievedOptions() const
Returns whether the copying of retrieved options is enabled.
Definition: pkt.h:496
uint16_t local_port_
local TDP or UDP port
Definition: pkt.h:933
int getIndex() const
Returns interface index.
Definition: pkt.h:665
const SubClassRelationContainer & getSubClassesRelations() const
Returns the class set including template classes associated with subclasses.
Definition: pkt.h:392
void resetIndex()
Resets interface index to negative value.
Definition: pkt.h:658
uint32_t transid_
Transaction-id (32 bits for v4, 24 bits for v6)
Definition: pkt.h:908
const ClientClasses & getClasses(bool required=false) const
Returns the class set.
Definition: pkt.h:380
void repack()
Copies content of input buffer to output buffer.
Definition: pkt.cc:151
isc::asiolink::IOAddress local_addr_
Local IP (v4 or v6) address.
Definition: pkt.h:924
virtual void pack()=0
Prepares on-wire format of DHCP (either v4 or v6) packet.
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:404
const isc::asiolink::IOAddress & getLocalAddr() const
Returns local IP address.
Definition: pkt.h:609
HWAddrPtr getRemoteHWAddr() const
Returns the remote HW address obtained from raw sockets.
Definition: pkt.h:733
virtual HWAddrPtr getMACFromSrcLinkLocalAddr()=0
Attempts to obtain MAC address from source link-local IPv6 address.
const std::list< PktEvent > & getPktEvents()
Fetches the current event stack contents.
Definition: pkt.h:567
const isc::asiolink::IOAddress & getRemoteAddr() const
Returns remote IP address.
Definition: pkt.h:595
ClientClasses classes_
Classes this packet belongs to.
Definition: pkt.h:770
void setPktEvent(const std::string &label, const boost::posix_time::ptime &timestamp=PktEvent::now())
Updates (or adds) an event in the event stack.
Definition: pkt.cc:326
virtual size_t len()=0
Returns packet size in binary format.
void setIface(const std::string &iface)
Sets interface name.
Definition: pkt.h:692
bool indexSet() const
Checks if interface index has been set.
Definition: pkt.h:672
isc::util::OutputBuffer & getBuffer()
Returns reference to output buffer.
Definition: pkt.h:245
uint16_t getLocalPort() const
Returns local UDP (and soon TCP) port.
Definition: pkt.h:629
void setLocalPort(uint16_t local)
Sets local UDP (and soon TCP) port.
Definition: pkt.h:619
uint16_t remote_port_
remote TCP or UDP port
Definition: pkt.h:936
void setIndex(const unsigned int ifindex)
Sets interface index.
Definition: pkt.h:653
HWAddrPtr remote_hwaddr_
Definition: pkt.h:958
uint32_t getTransid() const
Returns value of transaction-id field.
Definition: pkt.h:341
isc::dhcp::OptionCollection options_
Collection of options present in this message.
Definition: pkt.h:796
isc::util::OutputBuffer buffer_out_
Output buffer (used during message transmission)
Definition: pkt.h:946
std::string getIface() const
Returns interface name.
Definition: pkt.h:682
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:487
const boost::posix_time::ptime & getTimestamp() const
Returns packet timestamp.
Definition: pkt.h:514
SubClassRelationContainer subclasses_
SubClasses this packet belongs to.
Definition: pkt.h:786
void addPktEvent(const std::string &label, const boost::posix_time::ptime &timestamp=PktEvent::now())
Adds an event to the end of the event stack.
Definition: pkt.cc:321
void clearPktEvents()
Discards contents of the packet event stack.
Definition: pkt.cc:358
OptionCollection getNonCopiedOptions(const uint16_t opt_type) const
Returns all option instances of specified type without copying.
Definition: pkt.cc:84
void setTimestamp(boost::posix_time::ptime &timestamp)
Set socket receive timestamp.
Definition: pkt.h:521
boost::posix_time::ptime getPktEventTime(const std::string &label) const
Fetches the timestamp for a given event in the stack.
Definition: pkt.cc:347
OptionCollection cloneOptions()
Clones all options so that they can be safely modified.
Definition: pkt.cc:48
uint16_t getRemotePort() const
Returns remote port.
Definition: pkt.h:646
OptionPtr getOption(const uint16_t type)
Returns the first option of specified type.
Definition: pkt.cc:71
void setRemoteHWAddr(const HWAddrPtr &hw_addr)
Sets remote hardware address.
Definition: pkt.cc:164
void addSubClass(const isc::dhcp::ClientClass &class_def, const isc::dhcp::ClientClass &subclass)
Adds a specified subclass to the packet.
Definition: pkt.cc:135
bool inClass(const isc::dhcp::ClientClass &client_class)
Checks whether a client belongs to a given class.
Definition: pkt.cc:121
virtual HWAddrPtr getMACFromIPv6RelayOpt()=0
Attempts to obtain MAC address from relay option client-linklayer-addr.
unsigned int ifindex_
Interface index.
Definition: pkt.h:918
boost::posix_time::ptime timestamp_
packet timestamp
Definition: pkt.h:955
void setTransid(uint32_t transid)
Sets transaction-id value.
Definition: pkt.h:334
HWAddrPtr getMAC(uint32_t hw_addr_src)
Returns MAC address.
Definition: pkt.cc:180
virtual std::string toText() const =0
Returns text representation of the packet.
void updateTimestamp()
Update packet timestamp.
Definition: pkt.cc:147
bool copy_retrieved_options_
Indicates if a copy of the retrieved option should be returned when Pkt::getOption is called.
Definition: pkt.h:952
std::string iface_
Name of the network interface the packet was received/to be sent over.
Definition: pkt.h:911
std::string dumpPktEvents(bool verbose=false) const
Creates a dump of the stack contents to a string for logging.
Definition: pkt.cc:363
void addClass(const isc::dhcp::ClientClass &client_class, bool required=false)
Adds a specified class to the packet.
Definition: pkt.cc:126
OptionPtr getNonCopiedOption(const uint16_t type) const
Returns the first option of specified type without copying.
Definition: pkt.cc:62
void setRemoteAddr(const isc::asiolink::IOAddress &remote)
Sets remote IP address.
Definition: pkt.h:588
HWAddrPtr getMACFromIPv6(const isc::asiolink::IOAddress &addr)
Attempts to convert IPv6 address into MAC.
Definition: pkt.cc:277
void setLocalAddr(const isc::asiolink::IOAddress &local)
Sets local IP address.
Definition: pkt.h:602
void setRemotePort(uint16_t remote)
Sets remote UDP (and soon TCP) port.
Definition: pkt.h:639
virtual std::string getLabel() const
Returns text representation primary packet identifiers.
Definition: pkt.h:275
virtual void addOption(const OptionPtr &opt)
Adds an option to this packet.
Definition: pkt.cc:57
virtual ~Pkt()
Virtual destructor.
Definition: pkt.h:761
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:1041
~ScopedPktOptionsCopy()
Destructor.
Definition: pkt.h:1056
ScopedPktOptionsCopy(PktType &pkt)
Constructor.
Definition: pkt.h:1049
RAII object enabling duplication of the stored options and restoring the original options on destruct...
Definition: pkt.h:996
ScopedSubOptionsCopy(const OptionPtr &opt)
Constructor.
Definition: pkt.h:1004
~ScopedSubOptionsCopy()
Destructor.
Definition: pkt.h:1013
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:343
#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:1420
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition: pkt.h:982
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:37
Defines the logger used by the top-level component of kea-lfc.