| Kea 3.0.2-git

hwaddr.cc
Go to the documentation of this file.
1// Copyright (C) 2012-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#include <config.h>
8
9#include <dhcp/hwaddr.h>
10#include <dhcp/dhcp4.h>
11#include <exceptions/exceptions.h>
12#include <util/str.h>
13#include <iomanip>
14#include <sstream>
15#include <vector>
16#include <string.h>
17
18 namespace isc {
19 namespace dhcp {
20
21 const uint32_t HWAddr::HWADDR_SOURCE_ANY = 0xffffffff;
22 const uint32_t HWAddr::HWADDR_SOURCE_UNKNOWN = 0x00000000;
23 const uint32_t HWAddr::HWADDR_SOURCE_RAW = 0x00000001;
24 const uint32_t HWAddr::HWADDR_SOURCE_DUID = 0x00000002;
25 const uint32_t HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL = 0x00000004;
26 const uint32_t HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION = 0x00000008;
27 const uint32_t HWAddr::HWADDR_SOURCE_REMOTE_ID = 0x00000010;
28 const uint32_t HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID = 0x00000020;
29 const uint32_t HWAddr::HWADDR_SOURCE_DOCSIS_CMTS = 0x00000040;
30 const uint32_t HWAddr::HWADDR_SOURCE_DOCSIS_MODEM = 0x00000080;
31
32 HWAddr::HWAddr()
33 :htype_(HTYPE_ETHER), source_(0) {
34}
35
36 HWAddr::HWAddr(const uint8_t* hwaddr, size_t len, uint16_t htype)
37 :hwaddr_(hwaddr, hwaddr + len), htype_(htype), source_(0) {
38 if (len > MAX_HWADDR_LEN) {
39 isc_throw(isc::BadValue, "hwaddr length exceeds MAX_HWADDR_LEN");
40 }
41}
42
43 HWAddr::HWAddr(const std::vector<uint8_t>& hwaddr, uint16_t htype)
44 :hwaddr_(hwaddr), htype_(htype), source_(0) {
45 if (hwaddr.size() > MAX_HWADDR_LEN) {
46 isc_throw(isc::BadValue,
47 "address vector size exceeds MAX_HWADDR_LEN");
48 }
49}
50
51std::string HWAddr::toText(bool include_htype) const {
52 std::stringstream tmp;
53 if (include_htype) {
54 tmp << "hwtype=" << static_cast<unsigned int>(htype_) << " ";
55 }
56 tmp << std::hex;
57 bool delim = false;
58 for (auto const& it : hwaddr_) {
59 if (delim) {
60 tmp << ":";
61 }
62 tmp << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(it);
63 delim = true;
64 }
65 return (tmp.str());
66}
67
68 HWAddr
69 HWAddr::fromText(const std::string& text, const uint16_t htype) {
70 std::vector<uint8_t> binary;
71 util::str::decodeColonSeparatedHexString(text, binary);
72 return (HWAddr(binary, htype));
73}
74
75 bool HWAddr::operator==(const HWAddr& other) const {
76 return ((this->htype_ == other.htype_) &&
77 (this->hwaddr_ == other.hwaddr_));
78}
79
80 bool HWAddr::operator!=(const HWAddr& other) const {
81 return !(*this == other);
82}
83
84} // end of isc::dhcp namespace
85} // end of isc namespace
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
dhcp4.h
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::dhcp::HWAddr::HWADDR_SOURCE_UNKNOWN
static const uint32_t HWADDR_SOURCE_UNKNOWN
Used when actual origin is not known, e.g.
Definition hwaddr.h:41
isc::dhcp::HWAddr::HWADDR_SOURCE_RAW
static const uint32_t HWADDR_SOURCE_RAW
Obtained first hand from raw socket (100% reliable).
Definition hwaddr.h:44
isc::dhcp::HWAddr::HWADDR_SOURCE_REMOTE_ID
static const uint32_t HWADDR_SOURCE_REMOTE_ID
A relay can insert remote-id.
Definition hwaddr.h:63
isc::dhcp::HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION
static const uint32_t HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION
Get it from RFC6939 option.
Definition hwaddr.h:59
isc::dhcp::HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL
static const uint32_t HWADDR_SOURCE_IPV6_LINK_LOCAL
Extracted from IPv6 link-local address.
Definition hwaddr.h:53
isc::dhcp::HWAddr::HWADDR_SOURCE_DOCSIS_MODEM
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...
Definition hwaddr.h:79
isc::dhcp::HWAddr::HWADDR_SOURCE_DUID
static const uint32_t HWADDR_SOURCE_DUID
Extracted from DUID-LL or DUID-LLT (not 100% reliable as the client can send fake DUID).
Definition hwaddr.h:48
isc::dhcp::HWAddr::HWADDR_SOURCE_DOCSIS_CMTS
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...
Definition hwaddr.h:73
isc::dhcp::HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID
static const uint32_t HWADDR_SOURCE_SUBSCRIBER_ID
A relay can insert a subscriber-id option.
Definition hwaddr.h:67
isc::dhcp::HWAddr::HWADDR_SOURCE_ANY
static const uint32_t HWADDR_SOURCE_ANY
Not really a type, only used in getMAC() calls.
Definition hwaddr.h:37
hwaddr.h
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::HTYPE_ETHER
@ HTYPE_ETHER
Ethernet 10Mbps.
Definition dhcp4.h:56
isc::util::str::decodeColonSeparatedHexString
void decodeColonSeparatedHexString(const string &hex_string, vector< uint8_t > &binary)
Converts a string of hexadecimal digits with colons into a vector.
Definition str.cc:158
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
str.h
isc::dhcp::HWAddr
Hardware type that represents information from DHCPv4 packet.
Definition hwaddr.h:20
isc::dhcp::HWAddr::operator==
bool operator==(const HWAddr &other) const
Compares two hardware addresses for equality.
Definition hwaddr.cc:75
isc::dhcp::HWAddr::MAX_HWADDR_LEN
static const size_t MAX_HWADDR_LEN
Maximum size of a hardware address.
Definition hwaddr.h:27
isc::dhcp::HWAddr::htype_
uint16_t htype_
Hardware type.
Definition hwaddr.h:105
isc::dhcp::HWAddr::HWAddr
HWAddr()
default constructor
Definition hwaddr.cc:32
isc::dhcp::HWAddr::operator!=
bool operator!=(const HWAddr &other) const
Compares two hardware addresses for inequality.
Definition hwaddr.cc:80
isc::dhcp::HWAddr::fromText
static HWAddr fromText(const std::string &text, const uint16_t htype=HTYPE_ETHER)
Creates instance of the hardware address from textual format.
Definition hwaddr.cc:69
isc::dhcp::HWAddr::hwaddr_
std::vector< uint8_t > hwaddr_
Definition hwaddr.h:98
isc::dhcp::HWAddr::source_
uint32_t source_
Hardware address source.
Definition hwaddr.h:113
isc::dhcp::HWAddr::toText
std::string toText(bool include_htype=true) const
Returns textual representation of a hardware address (e.g.
Definition hwaddr.cc:51