Kea: hwaddr.cc Source File

| Kea 1.5.0

hwaddr.cc
Go to the documentation of this file.
1 // Copyright (C) 2012-2016 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/strutil.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
51 std::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 (std::vector<uint8_t>::const_iterator it = hwaddr_.begin();
59 it != hwaddr_.end(); ++it) {
60 if (delim) {
61 tmp << ":";
62 }
63 tmp << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(*it);
64 delim = true;
65 }
66 return (tmp.str());
67 }
68
69 HWAddr
70 HWAddr::fromText(const std::string& text, const uint16_t htype) {
71 std::vector<uint8_t> binary;
72 util::str::decodeColonSeparatedHexString(text, binary);
73 return (HWAddr(binary, htype));
74 }
75
76 bool HWAddr::operator==(const HWAddr& other) const {
77 return ((this->htype_ == other.htype_) &&
78 (this->hwaddr_ == other.hwaddr_));
79 }
80
81 bool HWAddr::operator!=(const HWAddr& other) const {
82 return !(*this == other);
83 }
84
85 }; // end of isc::dhcp namespace
86 }; // end of isc namespace
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_ANY
static const uint32_t HWADDR_SOURCE_ANY
Not really a type, only used in getMAC() calls.
Definition: hwaddr.h:37
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::toText
std::string toText(bool include_htype=true) const
Returns textual representation of a hardware address (e.g.
Definition: hwaddr.cc:51
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
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
isc::dhcp::HWAddr::hwaddr_
std::vector< uint8_t > hwaddr_
Definition: hwaddr.h:98
isc::util::str::decodeColonSeparatedHexString
void decodeColonSeparatedHexString(const std::string &hex_string, std::vector< uint8_t > &binary)
Converts a string of hexadecimal digits with colons into a vector.
Definition: strutil.cc:215
dhcp4.h
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
strutil.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::dhcp::HWAddr::operator!=
bool operator!=(const HWAddr &other) const
Compares two hardware addresses for inequality.
Definition: hwaddr.cc:81
isc::dhcp::HWAddr::htype_
uint16_t htype_
Hardware type.
Definition: hwaddr.h:105
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
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_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_CLIENT_ADDR_RELAY_OPTION
static const uint32_t HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION
Get it from RFC6939 option.
Definition: hwaddr.h:59
hwaddr.h
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_REMOTE_ID
static const uint32_t HWADDR_SOURCE_REMOTE_ID
A relay can insert remote-id.
Definition: hwaddr.h:63
isc::dhcp::HTYPE_ETHER
@ HTYPE_ETHER
Ethernet 10Mbps.
Definition: dhcp4.h:56
exceptions.h
isc::dhcp::HWAddr::HWAddr
HWAddr()
default constructor
Definition: hwaddr.cc:32
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::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::operator==
bool operator==(const HWAddr &other) const
Compares two hardware addresses for equality.
Definition: hwaddr.cc:76
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:70