cfg_mac_source.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include <config.h>
  7. #include <dhcpsrv/cfg_mac_source.h>
  8. #include <exceptions/exceptions.h>
  9. #include <dhcp/hwaddr.h>
  10. using namespace isc::data;
  11. namespace {
  12. using namespace isc::dhcp;
  13. struct {
  14. const char * name;
  15. uint32_t type;
  16. } sources[] = {
  17. { "any", HWAddr::HWADDR_SOURCE_ANY },
  18. { "raw", HWAddr::HWADDR_SOURCE_RAW },
  19. { "duid", HWAddr::HWADDR_SOURCE_DUID },
  20. { "ipv6-link-local", HWAddr::HWADDR_SOURCE_IPV6_LINK_LOCAL },
  21. { "client-link-addr-option", HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION },
  22. { "rfc6939", HWAddr::HWADDR_SOURCE_CLIENT_ADDR_RELAY_OPTION },
  23. { "remote-id", HWAddr::HWADDR_SOURCE_REMOTE_ID },
  24. { "rfc4649", HWAddr::HWADDR_SOURCE_REMOTE_ID },
  25. { "subscriber-id", HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID },
  26. { "rfc4580", HWAddr::HWADDR_SOURCE_SUBSCRIBER_ID },
  27. { "docsis-cmts", HWAddr::HWADDR_SOURCE_DOCSIS_CMTS },
  28. { "docsis-modem", HWAddr::HWADDR_SOURCE_DOCSIS_MODEM }
  29. };
  30. };
  31. namespace isc {
  32. namespace dhcp {
  33. CfgMACSource::CfgMACSource() {
  34. // By default, use any hardware source that is available.
  35. mac_sources_.push_back(HWAddr::HWADDR_SOURCE_ANY);
  36. }
  37. uint32_t CfgMACSource::MACSourceFromText(const std::string& name) {
  38. for (unsigned i = 0; i < sizeof(sources)/sizeof(sources[0]); ++i) {
  39. if (name.compare(sources[i].name) == 0) {
  40. return (sources[i].type);
  41. }
  42. }
  43. isc_throw(BadValue, "Can't convert '" << name << "' to any known MAC source.");
  44. }
  45. void CfgMACSource::add(uint32_t source) {
  46. for (CfgMACSources::const_iterator it = mac_sources_.begin();
  47. it != mac_sources_.end(); ++it) {
  48. if (*it == source) {
  49. isc_throw(InvalidParameter, "mac-source parameter " << source
  50. << "' specified twice.");
  51. }
  52. }
  53. mac_sources_.push_back(source);
  54. }
  55. ElementPtr CfgMACSource::toElement() const {
  56. ElementPtr result = Element::createList();
  57. for (CfgMACSources::const_iterator source = mac_sources_.cbegin();
  58. source != mac_sources_.cend(); ++source) {
  59. std::string name;
  60. for (unsigned i = 0; i < sizeof(sources)/sizeof(sources[0]); ++i) {
  61. if (sources[i].type == *source) {
  62. name = sources[i].name;
  63. break;
  64. }
  65. }
  66. if (name.empty()) {
  67. isc_throw(ToElementError, "invalid MAC source: " << *source);
  68. }
  69. result->add(Element::create(name));
  70. }
  71. // @todo check if the list is empty (including a new unit test)
  72. return (result);
  73. }
  74. };
  75. };