12#include <boost/foreach.hpp>
15using namespace boost::posix_time;
27 : transid_(transid), iface_(
""), ifindex_(
UNSET_IFINDEX), local_addr_(local_addr),
28 remote_addr_(remote_addr), local_port_(local_port),
29 remote_port_(remote_port), buffer_out_(0), copy_retrieved_options_(false) {
35 : transid_(0), iface_(
""), ifindex_(
UNSET_IFINDEX), local_addr_(local_addr),
36 remote_addr_(remote_addr), local_port_(local_port),
37 remote_port_(remote_port), buffer_out_(0), copy_retrieved_options_(false) {
50 for (
auto const& option :
options_) {
51 options.emplace(std::make_pair(option.second->getType(), option.second->clone()));
58 options_.insert(std::pair<int, OptionPtr>(opt->getType(), opt));
75 OptionPtr option_copy = x->second->clone();
76 x->second = option_copy;
85 std::pair<OptionCollection::const_iterator,
86 OptionCollection::const_iterator> range =
options_.equal_range(opt_type);
94 std::pair<OptionCollection::iterator,
95 OptionCollection::iterator> range =
options_.equal_range(opt_type);
100 BOOST_FOREACH(
auto& opt_it, range) {
101 OptionPtr option_copy = opt_it.second->clone();
102 opt_it.second = option_copy;
111 auto const& x =
options_.find(type);
155 timestamp_ = boost::posix_time::microsec_clock::universal_time();
159 if (!
data_.empty()) {
166 const std::vector<uint8_t>& hw_addr) {
180Pkt::setHWAddrMember(
const uint8_t htype,
const uint8_t,
181 const std::vector<uint8_t>& hw_addr,
183 storage.reset(
new HWAddr(hw_addr, htype));
288 std::vector<uint8_t> bin = addr.
toBytes();
291 if ((bin.size() == isc::asiolink::V6ADDRESS_LEN) &&
293 (bin[0] == 0xfe) && (bin[1] == 0x80) &&
296 ((bin[8] & 3) == 2) &&
298 (bin[11] == 0xff) && (bin[12] == 0xfe)) {
301 bin.erase(bin.begin(), bin.begin() + 8);
304 bin.erase(bin.begin() + 3, bin.begin() + 5);
315 hwtype = iface->getHWType();
318 mac.reset(
new HWAddr(bin, hwtype));
329 events_.push_back(
PktEvent(label, timestamp));
334 for (
auto& event : events_) {
335 if (event.label_ == label) {
336 event.timestamp_ = timestamp;
341 events_.push_back(
PktEvent(label, timestamp));
346 time_t time_t_secs = tv.tv_sec;
347 ptime timestamp = from_time_t(time_t_secs);
348 time_duration usecs(0, 0, 0, tv.tv_usec);
355 for (
auto const& event : events_) {
356 if (event.label_ == label) {
357 return (event.timestamp_);
371 std::stringstream oss;
373 oss <<
"Event log: " << std::endl;
376 bool first_pass =
true;
377 boost::posix_time::ptime beg_time;
378 boost::posix_time::ptime prev_time;
379 for (
auto const& event : events_) {
381 oss << (first_pass ?
"" :
", ") << event.timestamp_ <<
" : " << event.label_;
383 oss <<
event.timestamp_ <<
" : " <<
event.label_;
386 beg_time =
event.timestamp_;
388 oss <<
" elapsed: " <<
event.timestamp_ - prev_time << std::endl;
391 prev_time =
event.timestamp_;
398 oss <<
"total elapsed: " << prev_time - beg_time;
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
The IOAddress class represents an IP addresses (version agnostic)
std::vector< uint8_t > toBytes() const
Return address as set of bytes.
bool isV6LinkLocal() const
checks whether and address is IPv6 and is link-local
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
void insert(const ClientClass &class_name)
Insert an element.
IfacePtr getIface(const unsigned int ifindex)
Returns interface specified interface index.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
Describes an event during the life cycle of a packet.
static const std::string BUFFER_READ
Event that marks when a packet is read from the socket buffer by application.
static const std::string SOCKET_RECEIVED
Event that marks when a packet is placed in the socket buffer by the kernel.
static const std::string RESPONSE_SENT
Event that marks when a packet is been written to the socket by application.
static boost::posix_time::ptime & EMPTY_TIME()
Fetch an empty timestamp, used for logic comparisons.
bool delOption(uint16_t type)
Attempts to delete first suboption of requested type.
virtual HWAddrPtr getMACFromDocsisModem()=0
Attempts to extract MAC/Hardware address from DOCSIS options inserted by the modem itself.
isc::dhcp::OptionCollection getOptions(const uint16_t type)
Returns all instances of specified type.
virtual HWAddrPtr getMACFromDocsisCMTS()=0
Attempts to extract MAC/Hardware address from DOCSIS options inserted by the CMTS (the relay agent)
void addClass(const isc::dhcp::ClientClass &client_class)
Adds a specified class to the packet.
void addAdditionalClass(const isc::dhcp::ClientClass &client_class)
Adds a specified class to the packet's additional class list.
void repack()
Copies content of input buffer to output buffer.
virtual HWAddrPtr getMACFromRemoteIdRelayOption()=0
Attempts to obtain MAC address from remote-id relay option.
OptionBuffer data_
Unparsed data (in received packets).
HWAddrPtr getRemoteHWAddr() const
Returns the remote HW address obtained from raw sockets.
virtual HWAddrPtr getMACFromSrcLinkLocalAddr()=0
Attempts to obtain MAC address from source link-local IPv6 address.
ClientClasses classes_
Classes this packet belongs to.
void setPktEvent(const std::string &label, const boost::posix_time::ptime ×tamp=PktEvent::now())
Updates (or adds) an event in the event stack.
virtual size_t len()=0
Returns packet size in binary format.
isc::dhcp::OptionCollection options_
Collection of options present in this message.
isc::util::OutputBuffer buffer_out_
Output buffer (used during message transmission)
virtual HWAddrPtr getMACFromDUID()=0
Attempts to obtain MAC address from DUID-LL or DUID-LLT.
SubClassRelationContainer subclasses_
SubClasses this packet belongs to.
void addPktEvent(const std::string &label, const boost::posix_time::ptime ×tamp=PktEvent::now())
Adds an event to the end of the event stack.
void clearPktEvents()
Discards contents of the packet event stack.
OptionCollection getNonCopiedOptions(const uint16_t opt_type) const
Returns all option instances of specified type without copying.
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.
boost::posix_time::ptime getPktEventTime(const std::string &label) const
Fetches the timestamp for a given event in the stack.
OptionCollection cloneOptions()
Clones all options so that they can be safely modified.
OptionPtr getOption(const uint16_t type)
Returns the first option of specified type.
void setRemoteHWAddr(const HWAddrPtr &hw_addr)
Sets remote hardware address.
void addSubClass(const isc::dhcp::ClientClass &class_def, const isc::dhcp::ClientClass &subclass)
Adds a specified subclass to the packet.
bool inClass(const isc::dhcp::ClientClass &client_class)
Checks whether a client belongs to a given class.
virtual HWAddrPtr getMACFromIPv6RelayOpt()=0
Attempts to obtain MAC address from relay option client-linklayer-addr.
boost::posix_time::ptime timestamp_
packet timestamp
HWAddrPtr getMAC(uint32_t hw_addr_src)
Returns MAC address.
ClientClasses additional_classes_
Classes to be evaluated during additional class evaluation.
void updateTimestamp()
Update packet timestamp.
bool copy_retrieved_options_
Indicates if a copy of the retrieved option should be returned when Pkt::getOption is called.
std::string iface_
Name of the network interface the packet was received/to be sent over.
std::string dumpPktEvents(bool verbose=false) const
Creates a dump of the stack contents to a string for logging.
OptionPtr getNonCopiedOption(const uint16_t type) const
Returns the first option of specified type without copying.
HWAddrPtr getMACFromIPv6(const isc::asiolink::IOAddress &addr)
Attempts to convert IPv6 address into MAC.
virtual void addOption(const OptionPtr &opt)
Adds an option to this packet.
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
static const uint32_t HWADDR_SOURCE_RAW
Obtained first hand from raw socket (100% reliable).
static const uint32_t HWADDR_SOURCE_REMOTE_ID
A relay can insert remote-id.
static const uint32_t HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION
Get it from RFC6939 option.
static const uint32_t HWADDR_SOURCE_IPV6_LINK_LOCAL
Extracted from IPv6 link-local address.
static const uint32_t HWADDR_SOURCE_DOCSIS_MODEM
A cable modem (acting as DHCP client) that supports DOCSIS standard can insert DOCSIS options that co...
static const uint32_t HWADDR_SOURCE_DUID
Extracted from DUID-LL or DUID-LLT (not 100% reliable as the client can send fake DUID).
static const uint32_t HWADDR_SOURCE_DOCSIS_CMTS
A CMTS (acting as DHCP relay agent) that supports DOCSIS standard can insert DOCSIS options that cont...
std::string ClientClass
Defines a single class name.
boost::shared_ptr< Iface > IfacePtr
Type definition for the pointer to an Iface object.
std::multimap< unsigned int, OptionPtr > OptionCollection
A collection of DHCP (v4 or v6) options.
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
constexpr unsigned int UNSET_IFINDEX
A value used to signal that the interface index was not set.
boost::shared_ptr< Option > OptionPtr
Defines the logger used by the top-level component of kea-lfc.
Hardware type that represents information from DHCPv4 packet.
Defines a subclass to template class relation.