| Kea 3.0.2-git

cfg_mac_source.cc
Go to the documentation of this file.
1// Copyright (C) 2014-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#include <dhcpsrv/cfg_mac_source.h>
9#include <exceptions/exceptions.h>
10#include <dhcp/hwaddr.h>
11
12 using namespace isc::data;
13
14 namespace {
15
16 using namespace isc::dhcp;
17
18 struct {
19 const char * name;
20 uint32_t type;
21} sources[] = {
22 { "any", HWAddr::HWADDR_SOURCE_ANY },
23 { "raw", HWAddr::HWADDR_SOURCE_RAW },
24 { "duid", HWAddr::HWADDR_SOURCE_DUID },
25 { "ipv6-link-local", HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL },
26 { "client-link-addr-option", HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION },
27 { "rfc6939", HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION },
28 { "remote-id", HWAddr::HWADDR_SOURCE_REMOTE_ID },
29 { "rfc4649", HWAddr::HWADDR_SOURCE_REMOTE_ID },
30 { "subscriber-id", HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID },
31 { "rfc4580", HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID },
32 { "docsis-cmts", HWAddr::HWADDR_SOURCE_DOCSIS_CMTS },
33 { "docsis-modem", HWAddr::HWADDR_SOURCE_DOCSIS_MODEM }
34};
35
36};
37
38 namespace isc {
39 namespace dhcp {
40
41 CfgMACSource::CfgMACSource() {
42
43 // By default, use any hardware source that is available.
44 mac_sources_.push_back(HWAddr::HWADDR_SOURCE_ANY);
45}
46
47uint32_t CfgMACSource::MACSourceFromText(const std::string& name) {
48 for (unsigned i = 0; i < sizeof(sources)/sizeof(sources[0]); ++i) {
49 if (name.compare(sources[i].name) == 0) {
50 return (sources[i].type);
51 }
52 }
53
54 isc_throw(BadValue, "Can't convert '" << name << "' to any known MAC source.");
55}
56
57 void CfgMACSource::add(uint32_t source) {
58 for (auto const& it : mac_sources_) {
59 if (it == source) {
60 isc_throw(InvalidParameter, "mac-source parameter " << source
61 << "' specified twice.");
62 }
63 }
64 mac_sources_.push_back(source);
65}
66
67 ElementPtr CfgMACSource::toElement() const {
68 ElementPtr result = Element::createList();
69 for (auto const& source : mac_sources_) {
70 std::string name;
71 for (unsigned i = 0; i < sizeof(sources)/sizeof(sources[0]); ++i) {
72 if (sources[i].type == source) {
73 name = sources[i].name;
74 break;
75 }
76 }
77 if (name.empty()) {
78 isc_throw(ToElementError, "invalid MAC source: " << source);
79 }
80 result->add(Element::create(name));
81 }
82 // @todo check if the list is empty (including a new unit test)
83 return (result);
84}
85
86}
87}
cfg_mac_source.h
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::InvalidParameter
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
Definition exceptions/exceptions.h:124
isc::ToElementError
Cannot unparse error.
Definition cfg_to_element.h:19
isc::data::Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
isc::data::Element::createList
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
isc::dhcp::CfgMACSource::add
void add(uint32_t source)
Adds additional MAC/hardware address acquisition.
Definition cfg_mac_source.cc:57
isc::dhcp::CfgMACSource::toElement
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition cfg_mac_source.cc:67
isc::dhcp::CfgMACSource::CfgMACSource
CfgMACSource()
Default constructor.
Definition cfg_mac_source.cc:41
isc::dhcp::CfgMACSource::mac_sources_
CfgMACSources mac_sources_
Actual MAC sources storage.
Definition cfg_mac_source.h:79
isc::dhcp::CfgMACSource::MACSourceFromText
static uint32_t MACSourceFromText(const std::string &name)
Attempts to convert known hardware address sources to uint32_t.
Definition cfg_mac_source.cc:47
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_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::data
Definition base_stamped_element.cc:12
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148