1 // Copyright (C) 2014-2015,2017 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
CfgMACSource()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
MACSourceFromText(const std::string & name)47 uint32_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
add(uint32_t source)57 void CfgMACSource::add(uint32_t source) {
58 for (CfgMACSources::const_iterator it = mac_sources_.begin();
59 it != mac_sources_.end(); ++it) {
60 if (*it == source) {
61 isc_throw(InvalidParameter, "mac-source parameter " << source
62 << "' specified twice.");
63 }
64 }
65 mac_sources_.push_back(source);
66 }
67
toElement() const68 ElementPtr CfgMACSource::toElement() const {
69 ElementPtr result = Element::createList();
70 for (CfgMACSources::const_iterator source = mac_sources_.cbegin();
71 source != mac_sources_.cend(); ++source) {
72 std::string name;
73 for (unsigned i = 0; i < sizeof(sources)/sizeof(sources[0]); ++i) {
74 if (sources[i].type == *source) {
75 name = sources[i].name;
76 break;
77 }
78 }
79 if (name.empty()) {
80 isc_throw(ToElementError, "invalid MAC source: " << *source);
81 }
82 result->add(Element::create(name));
83 }
84 // @todo check if the list is empty (including a new unit test)
85 return (result);
86 }
87
88 };
89 };
90