Kea 2.2.0
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
22namespace isc {
23
24namespace dhcp {
25
39template<typename PktType>
41public:
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
75private:
76
78 std::pair<PktTypePtr, PktTypePtr> pkts_;
79};
80
91protected:
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
127public:
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
314protected:
315
327 OptionPtr getNonCopiedOption(const uint16_t type) const;
328
329public:
330
342 OptionPtr getOption(const uint16_t type);
343
368 virtual void setCopyRetrievedOptions(const bool copy) {
370 }
371
379 }
380
387 void updateTimestamp();
388
395 const boost::posix_time::ptime& getTimestamp() const {
396 return timestamp_;
397 }
398
403 void setTimestamp(boost::posix_time::ptime& timestamp) {
404 timestamp_ = timestamp;
405 }
406
413 void repack();
414
419 remote_addr_ = remote;
420 }
421
426 return (remote_addr_);
427 }
428
433 local_addr_ = local;
434 }
435
440 return (local_addr_);
441 }
442
449 void setLocalPort(uint16_t local) {
450 local_port_ = local;
451 }
452
459 uint16_t getLocalPort() const {
460 return (local_port_);
461 }
462
469 void setRemotePort(uint16_t remote) {
470 remote_port_ = remote;
471 }
472
476 uint16_t getRemotePort() const {
477 return (remote_port_);
478 }
479
483 void setIndex(int ifindex) {
484 ifindex_ = ifindex;
485 };
486
488 void resetIndex() {
489 ifindex_ = -1;
490 }
491
495 int getIndex() const {
496 return (ifindex_);
497 };
498
502 bool indexSet() const {
503 return (ifindex_ >= 0);
504 }
505
512 std::string getIface() const { return (iface_); };
513
520 void setIface(const std::string& iface) { iface_ = iface; };
521
533 void setRemoteHWAddr(const HWAddrPtr& hw_addr);
534
553 void setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
554 const std::vector<uint8_t>& hw_addr);
555
560 return (remote_hwaddr_);
561 }
562
580 HWAddrPtr getMAC(uint32_t hw_addr_src);
581
587 virtual ~Pkt() {
588 }
589
597
605
615
616protected:
617
638
654
668
682
698
711
724
726 uint32_t transid_;
727
729 std::string iface_;
730
736 int64_t ifindex_;
737
743
749
751 uint16_t local_port_;
752
754 uint16_t remote_port_;
755
765
771
773 boost::posix_time::ptime timestamp_;
774
775 // remote HW address (src if receiving packet, dst if sending packet)
777
778private:
779
791 virtual void setHWAddrMember(const uint8_t htype, const uint8_t hlen,
792 const std::vector<uint8_t>& hw_addr,
793 HWAddrPtr& storage);
794};
795
797typedef boost::shared_ptr<isc::dhcp::Pkt> PktPtr;
798
812public:
813
819 ScopedSubOptionsCopy(const OptionPtr& opt) : option_(opt) {
820 if (opt) {
821 options_ = opt->getMutableOptions();
822 }
823 }
824
829 if (option_) {
830 option_->getMutableOptions() = options_;
831 }
832 }
833
834private:
835
837 OptionPtr option_;
838
840 OptionCollection options_;
841};
842
855template<typename PktType>
857public:
858
864 ScopedPktOptionsCopy(PktType& pkt) : pkt_(pkt), options_(pkt.options_) {
865 }
866
871 pkt_.options_ = options_;
872 }
873
874private:
875
877 PktType& pkt_;
878
880 OptionCollection options_;
881};
882
883} // end of namespace isc::dhcp
884} // end of namespace isc
885
886#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:70
Base class for classes representing DHCP messages.
Definition: pkt.h:90
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
Definition: pkt.cc:68
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.
void setIndex(int ifindex)
Sets interface index.
Definition: pkt.h:483
isc::asiolink::IOAddress remote_addr_
Remote IP address.
Definition: pkt.h:748
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:604
bool isCopyRetrievedOptions() const
Returns whether the copying of retrieved options is enabled.
Definition: pkt.h:377
uint16_t local_port_
local TDP or UDP port
Definition: pkt.h:751
int getIndex() const
Returns interface index.
Definition: pkt.h:495
void resetIndex()
Resets interface index to negative value.
Definition: pkt.h:488
uint32_t transid_
Transaction-id (32 bits for v4, 24 bits for v6)
Definition: pkt.h:726
const ClientClasses & getClasses(bool required=false) const
Returns the class set.
Definition: pkt.h:300
void repack()
Copies content of input buffer to output buffer.
Definition: pkt.cc:100
isc::asiolink::IOAddress local_addr_
Local IP (v4 or v6) address.
Definition: pkt.h:742
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.
int64_t ifindex_
Interface index.
Definition: pkt.h:736
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:312
const isc::asiolink::IOAddress & getLocalAddr() const
Returns local IP address.
Definition: pkt.h:439
HWAddrPtr getRemoteHWAddr() const
Returns the remote HW address obtained from raw sockets.
Definition: pkt.h:559
virtual HWAddrPtr getMACFromSrcLinkLocalAddr()=0
Attempts to obtain MAC address from source link-local IPv6 address.
const isc::asiolink::IOAddress & getRemoteAddr() const
Returns remote IP address.
Definition: pkt.h:425
ClientClasses classes_
Classes this packet belongs to.
Definition: pkt.h:596
virtual size_t len()=0
Returns packet size in binary format.
void setIface(const std::string &iface)
Sets interface name.
Definition: pkt.h:520
bool indexSet() const
Checks if interface index has been set.
Definition: pkt.h:502
isc::util::OutputBuffer & getBuffer()
Returns reference to output buffer.
Definition: pkt.h:174
uint16_t getLocalPort() const
Returns local UDP (and soon TCP) port.
Definition: pkt.h:459
void setLocalPort(uint16_t local)
Sets local UDP (and soon TCP) port.
Definition: pkt.h:449
uint16_t remote_port_
remote TCP or UDP port
Definition: pkt.h:754
HWAddrPtr remote_hwaddr_
Definition: pkt.h:776
uint32_t getTransid() const
Returns value of transaction-id field.
Definition: pkt.h:266
isc::dhcp::OptionCollection options_
Collection of options present in this message.
Definition: pkt.h:614
isc::util::OutputBuffer buffer_out_
Output buffer (used during message transmission)
Definition: pkt.h:764
std::string getIface() const
Returns interface name.
Definition: pkt.h:512
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:368
const boost::posix_time::ptime & getTimestamp() const
Returns packet timestamp.
Definition: pkt.h:395
void setTimestamp(boost::posix_time::ptime &timestamp)
Set packet timestamp.
Definition: pkt.h:403
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:476
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:113
bool inClass(const isc::dhcp::ClientClass &client_class)
Checks whether a client belongs to a given class.
Definition: pkt.cc:79
virtual HWAddrPtr getMACFromIPv6RelayOpt()=0
Attempts to obtain MAC address from relay option client-linklayer-addr.
boost::posix_time::ptime timestamp_
packet timestamp
Definition: pkt.h:773
void setTransid(uint32_t transid)
Sets transaction-id value.
Definition: pkt.h:261
HWAddrPtr getMAC(uint32_t hw_addr_src)
Returns MAC address.
Definition: pkt.cc:129
virtual std::string toText() const =0
Returns text representation of the packet.
void updateTimestamp()
Update packet timestamp.
Definition: pkt.cc:96
bool copy_retrieved_options_
Indicates if a copy of the retrieved option should be returned when Pkt::getOption is called.
Definition: pkt.h:770
std::string iface_
Name of the network interface the packet was received/to be sent over.
Definition: pkt.h:729
void addClass(const isc::dhcp::ClientClass &client_class, bool required=false)
Adds packet to a specified class.
Definition: pkt.cc:84
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:418
HWAddrPtr getMACFromIPv6(const isc::asiolink::IOAddress &addr)
Attempts to convert IPv6 address into MAC.
Definition: pkt.cc:226
void setLocalAddr(const isc::asiolink::IOAddress &local)
Sets local IP address.
Definition: pkt.h:432
void setRemotePort(uint16_t remote)
Sets remote UDP (and soon TCP) port.
Definition: pkt.h:469
virtual std::string getLabel() const
Returns text representation primary packet identifiers.
Definition: pkt.h:202
virtual void addOption(const OptionPtr &opt)
Adds an option to this packet.
Definition: pkt.cc:41
virtual ~Pkt()
Virtual destructor.
Definition: pkt.h:587
RAII object enabling copying options retrieved from the packet.
Definition: pkt.h:40
ScopedEnableOptionsCopy(const PktTypePtr &pkt1, const PktTypePtr &pkt2=PktTypePtr())
Constructor.
Definition: pkt.h:52
boost::shared_ptr< PktType > PktTypePtr
Pointer to an encapsulated packet.
Definition: pkt.h:44
~ScopedEnableOptionsCopy()
Destructor.
Definition: pkt.h:66
RAII object enabling duplication of the stored options and restoring the original options on destruct...
Definition: pkt.h:856
~ScopedPktOptionsCopy()
Destructor.
Definition: pkt.h:870
ScopedPktOptionsCopy(PktType &pkt)
Constructor.
Definition: pkt.h:864
RAII object enabling duplication of the stored options and restoring the original options on destruct...
Definition: pkt.h:811
ScopedSubOptionsCopy(const OptionPtr &opt)
Constructor.
Definition: pkt.h:819
~ScopedSubOptionsCopy()
Destructor.
Definition: pkt.h:828
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:1360
boost::shared_ptr< isc::dhcp::Pkt > PktPtr
A pointer to either Pkt4 or Pkt6 packet.
Definition: pkt.h:797
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::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
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.