diff options
author | Tomek Mrugalski <tomasz@isc.org> | 2017-04-22 12:28:53 +0200 |
---|---|---|
committer | Tomek Mrugalski <tomasz@isc.org> | 2017-04-22 12:28:53 +0200 |
commit | 7d00bc89e740d3f6ebbb552b5a085b6fdfecf598 (patch) | |
tree | 73e68fbc768c1ccd201ddcc8ef1fd6595b203fd0 /src/lib/dhcpsrv/host.cc | |
parent | [5208a] setTestHostDataSource() added. (diff) | |
download | kea-7d00bc89e740d3f6ebbb552b5a085b6fdfecf598.tar.xz kea-7d00bc89e740d3f6ebbb552b5a085b6fdfecf598.zip |
[5208a] toElement() moved from CfgHosts to Host
Diffstat (limited to 'src/lib/dhcpsrv/host.cc')
-rw-r--r-- | src/lib/dhcpsrv/host.cc | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/lib/dhcpsrv/host.cc b/src/lib/dhcpsrv/host.cc index ea0215b723..2ceb3d8d93 100644 --- a/src/lib/dhcpsrv/host.cc +++ b/src/lib/dhcpsrv/host.cc @@ -9,9 +9,13 @@ #include <dhcpsrv/host.h> #include <util/encode/hex.h> #include <util/strutil.h> +#include <asiolink/io_address.h> #include <exceptions/exceptions.h> #include <sstream> +using namespace isc::data; +using namespace isc::asiolink; + namespace isc { namespace dhcp { @@ -400,6 +404,126 @@ Host::setBootFileName(const std::string& boot_file_name) { boot_file_name_ = boot_file_name; } +ElementPtr +Host::toElement4() const { + + // Get the subnet ID + SubnetID subnet_id = getIPv4SubnetID(); + // Prepare the map + ElementPtr map = Element::createMap(); + // Set the identifier + Host::IdentifierType id_type = getIdentifierType(); + if (id_type == Host::IDENT_HWADDR) { + HWAddrPtr hwaddr = getHWAddress(); + map->set("hw-address", Element::create(hwaddr->toText(false))); + } else if (id_type == Host::IDENT_DUID) { + DuidPtr duid = getDuid(); + map->set("duid", Element::create(duid->toText())); + } else if (id_type == Host::IDENT_CIRCUIT_ID) { + const std::vector<uint8_t>& bin = getIdentifier(); + std::string circuit_id = util::encode::encodeHex(bin); + map->set("circuit-id", Element::create(circuit_id)); + } else if (id_type == Host::IDENT_CLIENT_ID) { + const std::vector<uint8_t>& bin = getIdentifier(); + std::string client_id = util::encode::encodeHex(bin); + map->set("client-id", Element::create(client_id)); + } else if (id_type == Host::IDENT_FLEX) { + const std::vector<uint8_t>& bin = getIdentifier(); + std::string flex = util::encode::encodeHex(bin); + map->set("flex-id", Element::create(flex)); + } else { + isc_throw(ToElementError, "invalid identifier type: " << id_type); + } + // Set the reservation + const IOAddress& address = getIPv4Reservation(); + map->set("ip-address", Element::create(address.toText())); + // Set the hostname + const std::string& hostname = getHostname(); + map->set("hostname", Element::create(hostname)); + // Set next-server + const IOAddress& next_server = getNextServer(); + map->set("next-server", Element::create(next_server.toText())); + // Set server-hostname + const std::string& server_hostname = getServerHostname(); + map->set("server-hostname", Element::create(server_hostname)); + // Set boot-file-name + const std::string& boot_file_name = getBootFileName(); + map->set("boot-file-name", Element::create(boot_file_name)); + // Set client-classes + const ClientClasses& cclasses = getClientClasses4(); + ElementPtr classes = Element::createList(); + for (ClientClasses::const_iterator cclass = cclasses.cbegin(); + cclass != cclasses.end(); ++cclass) { + classes->add(Element::create(*cclass)); + } + map->set("client-classes", classes); + // Set option-data + ConstCfgOptionPtr opts = getCfgOption4(); + map->set("option-data", opts->toElement()); + + return (map); +} + +ElementPtr +Host::toElement6() const { + // Get the subnet ID + SubnetID subnet_id = getIPv6SubnetID(); + // Prepare the map + ElementPtr map = Element::createMap(); + // Set the identifier + Host::IdentifierType id_type = getIdentifierType(); + if (id_type == Host::IDENT_HWADDR) { + HWAddrPtr hwaddr = getHWAddress(); + map->set("hw-address", Element::create(hwaddr->toText(false))); + } else if (id_type == Host::IDENT_DUID) { + DuidPtr duid = getDuid(); + map->set("duid", Element::create(duid->toText())); + } else if (id_type == Host::IDENT_CIRCUIT_ID) { + isc_throw(ToElementError, "unexpected circuit-id DUID type"); + } else if (id_type == Host::IDENT_CLIENT_ID) { + isc_throw(ToElementError, "unexpected client-id DUID type"); + } else if (id_type == Host::IDENT_FLEX) { + const std::vector<uint8_t>& bin = getIdentifier(); + std::string flex = util::encode::encodeHex(bin); + map->set("flex-id", Element::create(flex)); + } else { + isc_throw(ToElementError, "invalid DUID type: " << id_type); + } + // Set reservations (ip-addresses) + IPv6ResrvRange na_resv = getIPv6Reservations(IPv6Resrv::TYPE_NA); + ElementPtr resvs = Element::createList(); + for (IPv6ResrvIterator resv = na_resv.first; + resv != na_resv.second; ++resv) { + resvs->add(Element::create(resv->second.toText())); + } + map->set("ip-addresses", resvs); + // Set reservations (prefixes) + IPv6ResrvRange pd_resv = getIPv6Reservations(IPv6Resrv::TYPE_PD); + resvs = Element::createList(); + for (IPv6ResrvIterator resv = pd_resv.first; + resv != pd_resv.second; ++resv) { + resvs->add(Element::create(resv->second.toText())); + } + map->set("prefixes", resvs); + // Set the hostname + const std::string& hostname = getHostname(); + map->set("hostname", Element::create(hostname)); + // Set client-classes + const ClientClasses& cclasses = getClientClasses6(); + ElementPtr classes = Element::createList(); + for (ClientClasses::const_iterator cclass = cclasses.cbegin(); + cclass != cclasses.end(); ++cclass) { + classes->add(Element::create(*cclass)); + } + map->set("client-classes", classes); + + // Set option-data + ConstCfgOptionPtr opts = getCfgOption6(); + map->set("option-data", opts->toElement()); + + return (map); +} + std::string Host::toText() const { std::ostringstream s; |