diff options
Diffstat (limited to 'src/lib/dhcpsrv/pool.cc')
-rw-r--r-- | src/lib/dhcpsrv/pool.cc | 111 |
1 files changed, 109 insertions, 2 deletions
diff --git a/src/lib/dhcpsrv/pool.cc b/src/lib/dhcpsrv/pool.cc index 2991d82082..8b815f6666 100644 --- a/src/lib/dhcpsrv/pool.cc +++ b/src/lib/dhcpsrv/pool.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -12,6 +12,7 @@ #include <sstream> using namespace isc::asiolink; +using namespace isc::data; namespace isc { namespace dhcp { @@ -76,6 +77,46 @@ Pool4::Pool4( const isc::asiolink::IOAddress& prefix, uint8_t prefix_len) capacity_ = addrsInRange(prefix, last_); } +data::ElementPtr +Pool::toElement() const { + // Prepare the map + ElementPtr map = Element::createMap(); + + // Set user-context + ConstElementPtr context = getContext(); + if (!isNull(context)) { + map->set("user-context", context); + } + + // Set pool options + ConstCfgOptionPtr opts = getCfgOption(); + map->set("option-data", opts->toElement()); + return (map); +} + +data::ElementPtr +Pool4::toElement() const { + // Prepare the map + ElementPtr map = Pool::toElement(); + + // Set pool + const IOAddress& first = getFirstAddress(); + const IOAddress& last = getLastAddress(); + std::string range = first.toText() + "-" + last.toText(); + + // Try to output a prefix (vs a range) + int prefix_len = prefixLengthFromRange(first, last); + if (prefix_len >= 0) { + std::ostringstream oss; + oss << first.toText() << "/" << prefix_len; + range = oss.str(); + } + + map->set("pool", Element::create(range)); + return (map); +} + + Pool6::Pool6(Lease::Type type, const isc::asiolink::IOAddress& first, const isc::asiolink::IOAddress& last) : Pool(type, first, last), prefix_len_(128), pd_exclude_option_() { @@ -172,7 +213,7 @@ Pool6::Pool6(const asiolink::IOAddress& prefix, const uint8_t prefix_len, } /// @todo Check that the prefixes actually match. Theoretically, a - /// user could specify a prefix which sets insgnificant bits. We should + /// user could specify a prefix which sets insignificant bits. We should /// clear insignificant bits based on the prefix length but this /// should be considered a part of the IOAddress class, perhaps and /// requires a bit of work (mainly in terms of testing). @@ -237,6 +278,72 @@ Pool6::init(const Lease::Type& type, } } +data::ElementPtr +Pool6::toElement() const { + // Prepare the map + ElementPtr map = Pool::toElement(); + + switch (getType()) { + case Lease::TYPE_NA: { + const IOAddress& first = getFirstAddress(); + const IOAddress& last = getLastAddress(); + std::string range = first.toText() + "-" + last.toText(); + + // Try to output a prefix (vs a range) + int prefix_len = prefixLengthFromRange(first, last); + if (prefix_len >= 0) { + std::ostringstream oss; + oss << first.toText() << "/" << prefix_len; + range = oss.str(); + } + + map->set("pool", Element::create(range)); + break; + } + case Lease::TYPE_PD: { + // Set prefix + const IOAddress& prefix = getFirstAddress(); + map->set("prefix", Element::create(prefix.toText())); + + // Set prefix-len (get it from min - max) + const IOAddress& last = getLastAddress(); + int prefix_len = prefixLengthFromRange(prefix, last); + if (prefix_len < 0) { + // The pool is bad: give up + isc_throw(ToElementError, "invalid prefix range " + << prefix.toText() << "-" << last.toText()); + } + + // Set delegated-len + uint8_t len = getLength(); + map->set("delegated-len", Element::create(static_cast<int>(len))); + + // Set excluded prefix + const Option6PDExcludePtr& xopt = getPrefixExcludeOption(); + if (xopt) { + const IOAddress& xprefix = xopt->getExcludedPrefix(prefix, len); + map->set("excluded-prefix", Element::create(xprefix.toText())); + + uint8_t xlen = xopt->getExcludedPrefixLength(); + map->set("excluded-prefix-len", + Element::create(static_cast<int>(xlen))); + } else { + map->set("excluded-prefix", Element::create(std::string("::"))); + map->set("excluded-prefix-len", Element::create(0)); + } + + break; + } + default: + isc_throw(ToElementError, "Lease type: " << getType() + << ", unsupported for Pool6"); + break; + } + + return (map); +} + + std::string Pool6::toText() const { std::ostringstream s; |