Kea 3.3.3
pkt.h
Go to the documentation of this file.
1// Copyright (C) 2014-2026 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#include <vector>
23
24namespace isc {
25
26namespace dhcp {
27
31constexpr unsigned int UNSET_IFINDEX = std::numeric_limits<unsigned int>::max();
32
46template<typename PktType>
48public:
49
51 typedef boost::shared_ptr<PktType> PktTypePtr;
52
60 const PktTypePtr& pkt2 = PktTypePtr())
61 : pkts_(pkt1, pkt2) {
62 if (pkt1) {
63 pkt1->setCopyRetrievedOptions(true);
64 }
65 if (pkt2) {
66 pkt2->setCopyRetrievedOptions(true);
67 }
68 }
69
74 if (pkts_.first) {
75 pkts_.first->setCopyRetrievedOptions(false);
76 }
77 if (pkts_.second) {
78 pkts_.second->setCopyRetrievedOptions(false);
79 }
80 }
81
82private:
83
85 std::pair<PktTypePtr, PktTypePtr> pkts_;
86};
87
88
90class PktEvent {
91public:
94 static const std::string SOCKET_RECEIVED;
95
98 static const std::string BUFFER_READ;
99
102 static const std::string RESPONSE_SENT;
103
108 PktEvent(const std::string& label, boost::posix_time::ptime timestamp)
109 : label_(label), timestamp_(timestamp) {
110 }
111
113 ~PktEvent() = default;
114
118 static boost::posix_time::ptime now() {
119 return (boost::posix_time::microsec_clock::universal_time());
120 }
121
125 static boost::posix_time::ptime& EMPTY_TIME() {
126 static boost::posix_time::ptime empty_time;
127 return (empty_time);
128 }
129
133 static boost::posix_time::ptime& MIN_TIME() {
134 static auto min_time = boost::posix_time::ptime(boost::posix_time::min_date_time);
135 return (min_time);
136 }
137
141 static boost::posix_time::ptime& MAX_TIME() {
142 static auto max_time = boost::posix_time::ptime(boost::posix_time::max_date_time);
143 return (max_time);
144 }
145
147 std::string label_;
148
150 boost::posix_time::ptime timestamp_;
151};
152
163protected:
164
177 Pkt(uint32_t transid, const isc::asiolink::IOAddress& local_addr,
178 const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
179 uint16_t remote_port);
180
194 Pkt(const uint8_t* buf, uint32_t len,
195 const isc::asiolink::IOAddress& local_addr,
196 const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
197 uint16_t remote_port);
198
199public:
200
216 virtual void pack() = 0;
217
232 virtual void unpack() = 0;
233
249
257 virtual void addOption(const OptionPtr& opt);
258
267 bool delOption(uint16_t type);
268
276 virtual std::string getLabel() const {
277 isc_throw(NotImplemented, "Pkt::getLabel()");
278 }
279
290 virtual std::string toText(bool verbose = false) const = 0;
291
302 virtual size_t len() = 0;
303
311 virtual uint8_t getType() const = 0;
312
320 virtual void setType(uint8_t type) = 0;
321
331 virtual const char* getName() const = 0;
332
336 void setTransid(uint32_t transid) {
337 transid_ = transid;
338 }
339
343 uint32_t getTransid() const {
344 return (transid_);
345 }
346
351 bool inClass(const isc::dhcp::ClientClass& client_class);
352
360 void addClass(const isc::dhcp::ClientClass& client_class);
361
369 void addAdditionalClass(const isc::dhcp::ClientClass& client_class);
370
379 void addSubClass(const isc::dhcp::ClientClass& class_def,
380 const isc::dhcp::ClientClass& subclass);
381
386 const ClientClasses& getClasses() const {
387 return (classes_);
388 }
389
395 return (additional_classes_);
396 }
397
407
412 void copyClasses(const Pkt& other);
413
423
424protected:
425
437 OptionPtr getNonCopiedOption(const uint16_t type) const;
438
451 OptionCollection getNonCopiedOptions(const uint16_t opt_type) const;
452
453public:
454
459
470 OptionPtr getOption(const uint16_t type);
471
479 isc::dhcp::OptionCollection getOptions(const uint16_t type);
480
505 virtual void setCopyRetrievedOptions(const bool copy) {
507 }
508
516 }
517
524 void updateTimestamp();
525
532 const boost::posix_time::ptime& getTimestamp() const {
533 return timestamp_;
534 }
535
539 void setTimestamp(boost::posix_time::ptime& timestamp) {
540 timestamp_ = timestamp;
541 }
542
548 void addPktEvent(const std::string& label,
549 const boost::posix_time::ptime& timestamp = PktEvent::now());
550
557 void addPktEvent(const std::string& label, const struct timeval& timestamp);
558
568 void setPktEvent(const std::string& label,
569 const boost::posix_time::ptime& timestamp = PktEvent::now());
570
574 void clearPktEvents();
575
580 boost::posix_time::ptime getPktEventTime(const std::string& label) const;
581
585 const std::list<PktEvent>& getPktEvents() {
586 return (events_);
587 }
588
593 std::string dumpPktEvents(bool verbose = false) const;
594
601 void repack();
602
607 remote_addr_ = remote;
608 }
609
614 return (remote_addr_);
615 }
616
621 local_addr_ = local;
622 }
623
628 return (local_addr_);
629 }
630
637 void setLocalPort(uint16_t local) {
638 local_port_ = local;
639 }
640
647 uint16_t getLocalPort() const {
648 return (local_port_);
649 }
650
657 void setRemotePort(uint16_t remote) {
658 remote_port_ = remote;
659 }
660
664 uint16_t getRemotePort() const {
665 return (remote_port_);
666 }
667
671 void setIndex(const unsigned int ifindex) {
672 ifindex_ = ifindex;
673 }
674
676 void resetIndex() {
678 }
679
683 int getIndex() const {
684 return (ifindex_);
685 }
686
690 bool indexSet() const {
691 return (ifindex_ != UNSET_IFINDEX);
692 }
693
700 std::string getIface() const {
701 return (iface_);
702 }
703
710 void setIface(const std::string& iface) {
711 iface_ = iface;
712 }
713
725 void setRemoteHWAddr(const HWAddrPtr& hw_addr);
726
745 void setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
746 const std::vector<uint8_t>& hw_addr);
747
752 return (remote_hwaddr_);
753 }
754
772 HWAddrPtr getMAC(uint32_t hw_addr_src);
773
779 virtual ~Pkt() {
780 }
781
789
803
811
821
822protected:
823
844
860
874
888
904
917
930
932 uint32_t transid_;
933
935 std::string iface_;
936
942 unsigned int ifindex_;
943
949
955
957 uint16_t local_port_;
958
960 uint16_t remote_port_;
961
971
977
979 boost::posix_time::ptime timestamp_;
980
981 // remote HW address (src if receiving packet, dst if sending packet)
983
984private:
985
997 virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
998 const std::vector<uint8_t>& hw_addr,
999 HWAddrPtr& storage);
1000
1002 std::list<PktEvent> events_;
1003};
1004
1006typedef boost::shared_ptr<isc::dhcp::Pkt> PktPtr;
1007
1021public:
1022
1028 ScopedSubOptionsCopy(const OptionPtr& opt) : option_(opt) {
1029 if (opt) {
1030 options_ = opt->getMutableOptions();
1031 }
1032 }
1033
1038 if (option_) {
1039 option_->getMutableOptions() = options_;
1040 }
1041 }
1042
1043private:
1044
1046 OptionPtr option_;
1047
1049 OptionCollection options_;
1050};
1051
1064template<typename PktType>
1066public:
1067
1073 ScopedPktOptionsCopy(PktType& pkt) : pkt_(pkt), options_(pkt.options_) {
1074 pkt_.options_ = pkt_.cloneOptions();
1075 }
1076
1081 pkt_.options_ = options_;
1082 }
1083
1084private:
1085
1087 PktType& pkt_;
1088
1090 OptionCollection options_;
1091};
1092
1093} // end of namespace isc::dhcp
1094} // end of namespace isc
1095
1096#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:111
~PktEvent()=default
Destructor.
static boost::posix_time::ptime now()
Fetch the current UTC system time, microsecond precision.
Definition pkt.h:118
std::string label_
Label identifying this event.
Definition pkt.h:147
PktEvent(const std::string &label, boost::posix_time::ptime timestamp)
Constructor.
Definition pkt.h:108
static boost::posix_time::ptime & MAX_TIME()
Fetches the maximum timestamp.
Definition pkt.h:141
static const std::string BUFFER_READ
Event that marks when a packet is read from the socket buffer by application.
Definition pkt.h:98
boost::posix_time::ptime timestamp_
Timestamp at which the event occurred.
Definition pkt.h:150
static const std::string SOCKET_RECEIVED
Event that marks when a packet is placed in the socket buffer by the kernel.
Definition pkt.h:94
static const std::string RESPONSE_SENT
Event that marks when a packet is been written to the socket by application.
Definition pkt.h:102
static boost::posix_time::ptime & MIN_TIME()
Fetches the minimum timestamp.
Definition pkt.h:133
static boost::posix_time::ptime & EMPTY_TIME()
Fetch an empty timestamp, used for logic comparisons.
Definition pkt.h:125
Base class for classes representing DHCP messages.
Definition pkt.h:162
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 std::string toText(bool verbose=false) const =0
Returns text representation of the packet.
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:954
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.
bool isCopyRetrievedOptions() const
Returns whether the copying of retrieved options is enabled.
Definition pkt.h:514
void addClass(const isc::dhcp::ClientClass &client_class)
Adds a specified class to the packet.
Definition pkt.cc:126
uint16_t local_port_
local TDP or UDP port
Definition pkt.h:957
int getIndex() const
Returns interface index.
Definition pkt.h:683
const SubClassRelationContainer & getSubClassesRelations() const
Returns the class set including template classes associated with subclasses.
Definition pkt.h:404
void resetIndex()
Resets interface index to negative value.
Definition pkt.h:676
uint32_t transid_
Transaction-id (32 bits for v4, 24 bits for v6).
Definition pkt.h:932
void addAdditionalClass(const isc::dhcp::ClientClass &client_class)
Adds a specified class to the packet's additional class list.
Definition pkt.cc:134
void repack()
Copies content of input buffer to output buffer.
Definition pkt.cc:167
isc::asiolink::IOAddress local_addr_
Local IP (v4 or v6) address.
Definition pkt.h:948
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.
const ClientClasses & getClasses() const
Returns the class set.
Definition pkt.h:386
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:422
const isc::asiolink::IOAddress & getLocalAddr() const
Returns local IP address.
Definition pkt.h:627
HWAddrPtr getRemoteHWAddr() const
Returns the remote HW address obtained from raw sockets.
Definition pkt.h:751
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:585
const isc::asiolink::IOAddress & getRemoteAddr() const
Returns remote IP address.
Definition pkt.h:613
ClientClasses classes_
Classes this packet belongs to.
Definition pkt.h:788
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:342
virtual size_t len()=0
Returns packet size in binary format.
void setIface(const std::string &iface)
Sets interface name.
Definition pkt.h:710
bool indexSet() const
Checks if interface index has been set.
Definition pkt.h:690
isc::util::OutputBuffer & getBuffer()
Returns reference to output buffer.
Definition pkt.h:246
uint16_t getLocalPort() const
Returns local UDP (and soon TCP) port.
Definition pkt.h:647
void setLocalPort(uint16_t local)
Sets local UDP (and soon TCP) port.
Definition pkt.h:637
const ClientClasses & getAdditionalClasses() const
Returns the additional class list.
Definition pkt.h:394
uint16_t remote_port_
remote TCP or UDP port
Definition pkt.h:960
void setIndex(const unsigned int ifindex)
Sets interface index.
Definition pkt.h:671
HWAddrPtr remote_hwaddr_
Definition pkt.h:982
uint32_t getTransid() const
Returns value of transaction-id field.
Definition pkt.h:343
isc::dhcp::OptionCollection options_
Collection of options present in this message.
Definition pkt.h:820
isc::util::OutputBuffer buffer_out_
Output buffer (used during message transmission).
Definition pkt.h:970
std::string getIface() const
Returns interface name.
Definition pkt.h:700
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:505
const boost::posix_time::ptime & getTimestamp() const
Returns packet timestamp.
Definition pkt.h:532
SubClassRelationContainer subclasses_
SubClasses this packet belongs to.
Definition pkt.h:810
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:337
void clearPktEvents()
Discards contents of the packet event stack.
Definition pkt.cc:374
void copyClasses(const Pkt &other)
Simple copy of classes from another packet.
Definition pkt.cc:154
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:539
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:24
boost::posix_time::ptime getPktEventTime(const std::string &label) const
Fetches the timestamp for a given event in the stack.
Definition pkt.cc:363
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:664
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:180
void addSubClass(const isc::dhcp::ClientClass &class_def, const isc::dhcp::ClientClass &subclass)
Adds a specified subclass to the packet.
Definition pkt.cc:142
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:942
boost::posix_time::ptime timestamp_
packet timestamp
Definition pkt.h:979
void setTransid(uint32_t transid)
Sets transaction-id value.
Definition pkt.h:336
HWAddrPtr getMAC(uint32_t hw_addr_src)
Returns MAC address.
Definition pkt.cc:196
ClientClasses additional_classes_
Classes to be evaluated during additional class evaluation.
Definition pkt.h:802
void updateTimestamp()
Update packet timestamp.
Definition pkt.cc:163
bool copy_retrieved_options_
Indicates if a copy of the retrieved option should be returned when Pkt::getOption is called.
Definition pkt.h:976
std::string iface_
Name of the network interface the packet was received/to be sent over.
Definition pkt.h:935
std::string dumpPktEvents(bool verbose=false) const
Creates a dump of the stack contents to a string for logging.
Definition pkt.cc:379
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:606
HWAddrPtr getMACFromIPv6(const isc::asiolink::IOAddress &addr)
Attempts to convert IPv6 address into MAC.
Definition pkt.cc:293
void setLocalAddr(const isc::asiolink::IOAddress &local)
Sets local IP address.
Definition pkt.h:620
void setRemotePort(uint16_t remote)
Sets remote UDP (and soon TCP) port.
Definition pkt.h:657
virtual std::string getLabel() const
Returns text representation primary packet identifiers.
Definition pkt.h:276
virtual void addOption(const OptionPtr &opt)
Adds an option to this packet.
Definition pkt.cc:57
virtual ~Pkt()
Virtual destructor.
Definition pkt.h:779
ScopedEnableOptionsCopy(const PktTypePtr &pkt1, const PktTypePtr &pkt2=PktTypePtr())
Constructor.
Definition pkt.h:59
boost::shared_ptr< PktType > PktTypePtr
Pointer to an encapsulated packet.
Definition pkt.h:51
~ScopedEnableOptionsCopy()
Destructor.
Definition pkt.h:73
~ScopedPktOptionsCopy()
Destructor.
Definition pkt.h:1080
ScopedPktOptionsCopy(PktType &pkt)
Constructor.
Definition pkt.h:1073
ScopedSubOptionsCopy(const OptionPtr &opt)
Constructor.
Definition pkt.h:1028
~ScopedSubOptionsCopy()
Destructor.
Definition pkt.h:1037
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:355
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
ElementPtr copy(ConstElementPtr from, unsigned level)
Copy the data up to a nesting level.
Definition data.cc:1543
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition pkt.h:1006
std::string ClientClass
Defines a single class name.
Definition classify.h:45
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
Definition option.h:41
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:105
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition hwaddr.h:166
constexpr unsigned int UNSET_IFINDEX
A value used to signal that the interface index was not set.
Definition pkt.h:31
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:25
boost::shared_ptr< Option > OptionPtr
Definition option.h:38
Defines the logger used by the top-level component of kea-lfc.