Kea 2.5.8
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>
12#include <util/str.h>
13#include <iomanip>
14#include <sstream>
15#include <vector>
16#include <string.h>
17
18namespace isc {
19namespace dhcp {
20
21const uint32_t HWAddr::HWADDR_SOURCE_ANY = 0xffffffff;
22const uint32_t HWAddr::HWADDR_SOURCE_UNKNOWN = 0x00000000;
23const uint32_t HWAddr::HWADDR_SOURCE_RAW = 0x00000001;
24const uint32_t HWAddr::HWADDR_SOURCE_DUID = 0x00000002;
25const uint32_t HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL = 0x00000004;
26const uint32_t HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION = 0x00000008;
27const uint32_t HWAddr::HWADDR_SOURCE_REMOTE_ID = 0x00000010;
28const uint32_t HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID = 0x00000020;
29const uint32_t HWAddr::HWADDR_SOURCE_DOCSIS_CMTS = 0x00000040;
30const uint32_t HWAddr::HWADDR_SOURCE_DOCSIS_MODEM = 0x00000080;
31
33 :htype_(HTYPE_ETHER), source_(0) {
34}
35
36HWAddr::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
43HWAddr::HWAddr(const std::vector<uint8_t>& hwaddr, uint16_t htype)
44 :hwaddr_(hwaddr), htype_(htype), source_(0) {
45 if (hwaddr.size() > MAX_HWADDR_LEN) {
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
69HWAddr::fromText(const std::string& text, const uint16_t htype) {
70 std::vector<uint8_t> binary;
72 return (HWAddr(binary, htype));
73}
74
75bool HWAddr::operator==(const HWAddr& other) const {
76 return ((this->htype_ == other.htype_) &&
77 (this->hwaddr_ == other.hwaddr_));
78}
79
80bool HWAddr::operator!=(const HWAddr& other) const {
81 return !(*this == other);
82}
83
84}; // end of isc::dhcp namespace
85}; // end of isc namespace
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
static const uint32_t HWADDR_SOURCE_UNKNOWN
Used when actual origin is not known, e.g.
Definition: hwaddr.h:41
static const uint32_t HWADDR_SOURCE_RAW
Obtained first hand from raw socket (100% reliable).
Definition: hwaddr.h:44
static const uint32_t HWADDR_SOURCE_REMOTE_ID
A relay can insert remote-id.
Definition: hwaddr.h:63
static const uint32_t HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION
Get it from RFC6939 option.
Definition: hwaddr.h:59
static const uint32_t HWADDR_SOURCE_IPV6_LINK_LOCAL
Extracted from IPv6 link-local address.
Definition: hwaddr.h:53
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
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
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
static const uint32_t HWADDR_SOURCE_SUBSCRIBER_ID
A relay can insert a subscriber-id option.
Definition: hwaddr.h:67
static const uint32_t HWADDR_SOURCE_ANY
Not really a type, only used in getMAC() calls.
Definition: hwaddr.h:37
@ HTYPE_ETHER
Ethernet 10Mbps.
Definition: dhcp4.h:56
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
Defines the logger used by the top-level component of kea-lfc.
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
bool operator==(const HWAddr &other) const
Compares two hardware addresses for equality.
Definition: hwaddr.cc:75
static const size_t MAX_HWADDR_LEN
Maximum size of a hardware address.
Definition: hwaddr.h:27
uint16_t htype_
Hardware type.
Definition: hwaddr.h:105
HWAddr()
default constructor
Definition: hwaddr.cc:32
bool operator!=(const HWAddr &other) const
Compares two hardware addresses for inequality.
Definition: hwaddr.cc:80
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
std::vector< uint8_t > hwaddr_
Definition: hwaddr.h:98
std::string toText(bool include_htype=true) const
Returns textual representation of a hardware address (e.g.
Definition: hwaddr.cc:51