Kea  1.9.9-git
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 
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) {
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;
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
static const uint32_t HWADDR_SOURCE_ANY
Not really a type, only used in getMAC() calls.
Definition: hwaddr.h:37
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_UNKNOWN
Used when actual origin is not known, e.g.
Definition: hwaddr.h:41
static const size_t MAX_HWADDR_LEN
Maximum size of a hardware address.
Definition: hwaddr.h:27
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
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
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static const uint32_t HWADDR_SOURCE_SUBSCRIBER_ID
A relay can insert a subscriber-id option.
Definition: hwaddr.h:67
uint16_t htype_
Hardware type.
Definition: hwaddr.h:105
Ethernet 10Mbps.
Definition: dhcp4.h:56
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_CMTS
A CMTS (acting as DHCP relay agent) that supports DOCSIS standard can insert DOCSIS options that cont...
Definition: hwaddr.h:73
Defines the logger used by the top-level component of kea-dhcp-ddns.
static const uint32_t HWADDR_SOURCE_RAW
Obtained first hand from raw socket (100% reliable).
Definition: hwaddr.h:44
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
HWAddr()
default constructor
Definition: hwaddr.cc:32
bool operator!=(const HWAddr &other) const
Compares two hardware addresses for inequality.
Definition: hwaddr.cc:81
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
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:76
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