subnet.cc 6.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <asiolink/io_address.h>
16 17 18
#include <dhcpsrv/addr_utilities.h>
#include <dhcpsrv/subnet.h>

19
#include <sstream>
20 21 22 23 24 25 26 27 28 29 30

using namespace isc::asiolink;

namespace isc {
namespace dhcp {

Subnet::Subnet(const isc::asiolink::IOAddress& prefix, uint8_t len,
               const Triplet<uint32_t>& t1,
               const Triplet<uint32_t>& t2,
               const Triplet<uint32_t>& valid_lifetime)
    :id_(getNextID()), prefix_(prefix), prefix_len_(len), t1_(t1),
31 32
     t2_(t2), valid_(valid_lifetime),
     last_allocated_(lastAddrInPrefix(prefix, len)) {
33 34
    if ((prefix.isV6() && len > 128) ||
        (prefix.isV4() && len > 32)) {
35 36 37 38 39 40 41 42 43 44 45
        isc_throw(BadValue, "Invalid prefix length specified for subnet: " << len);
    }
}

bool Subnet::inRange(const isc::asiolink::IOAddress& addr) const {
    IOAddress first = firstAddrInPrefix(prefix_, prefix_len_);
    IOAddress last = lastAddrInPrefix(prefix_, prefix_len_);

    return ((first <= addr) && (addr <= last));
}

46
void
47
Subnet::addOption(OptionPtr& option, bool persistent /* = false */) {
48 49 50 51 52 53 54 55 56
    validateOption(option);
    options_.push_back(OptionDescriptor(option, persistent));
}

void
Subnet::delOptions() {
    options_.clear();
}

57
std::string Subnet::toText() const {
58 59 60 61 62
    std::stringstream tmp;
    tmp << prefix_.toText() << "/" << static_cast<unsigned int>(prefix_len_);
    return (tmp.str());
}

63 64 65 66 67
Subnet4::Subnet4(const isc::asiolink::IOAddress& prefix, uint8_t length,
                 const Triplet<uint32_t>& t1,
                 const Triplet<uint32_t>& t2,
                 const Triplet<uint32_t>& valid_lifetime)
    :Subnet(prefix, length, t1, t2, valid_lifetime) {
68
    if (!prefix.isV4()) {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        isc_throw(BadValue, "Non IPv4 prefix " << prefix.toText()
                  << " specified in subnet4");
    }
}

void Subnet4::addPool4(const Pool4Ptr& pool) {
    IOAddress first_addr = pool->getFirstAddress();
    IOAddress last_addr = pool->getLastAddress();

    if (!inRange(first_addr) || !inRange(last_addr)) {
        isc_throw(BadValue, "Pool4 (" << first_addr.toText() << "-" << last_addr.toText()
                  << " does not belong in this (" << prefix_ << "/" << prefix_len_
                  << ") subnet4");
    }

    /// @todo: Check that pools do not overlap

    pools_.push_back(pool);
}

Pool4Ptr Subnet4::getPool4(const isc::asiolink::IOAddress& hint /* = IOAddress("::")*/ ) {
    Pool4Ptr candidate;
    for (Pool4Collection::iterator pool = pools_.begin(); pool != pools_.end(); ++pool) {

        // if we won't find anything better, then let's just use the first pool
        if (!candidate) {
            candidate = *pool;
        }

        // if the client provided a pool and there's a pool that hint is valid in,
        // then let's use that pool
        if ((*pool)->inRange(hint)) {
            return (*pool);
        }
    }
    return (candidate);
}

107
void
108
Subnet4::validateOption(const OptionPtr& option) const {
109 110 111
    if (!option) {
        isc_throw(isc::BadValue, "option configured for subnet must not be NULL");
    } else if (option->getUniverse() != Option::V4) {
112
        isc_throw(isc::BadValue, "expected V4 option to be added to the subnet");
113 114 115
    }
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
bool Subnet4::inPool(const isc::asiolink::IOAddress& addr) const {

    // Let's start with checking if it even belongs to that subnet.
    if (!inRange(addr)) {
        return (false);
    }

    for (Pool4Collection::const_iterator pool = pools_.begin(); pool != pools_.end(); ++pool) {
        if ((*pool)->inRange(addr)) {
            return (true);
        }
    }
    // there's no pool that address belongs to
    return (false);
}

132 133 134 135 136 137 138
Subnet6::Subnet6(const isc::asiolink::IOAddress& prefix, uint8_t length,
                 const Triplet<uint32_t>& t1,
                 const Triplet<uint32_t>& t2,
                 const Triplet<uint32_t>& preferred_lifetime,
                 const Triplet<uint32_t>& valid_lifetime)
    :Subnet(prefix, length, t1, t2, valid_lifetime),
     preferred_(preferred_lifetime){
139
    if (!prefix.isV6()) {
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        isc_throw(BadValue, "Non IPv6 prefix " << prefix.toText()
                  << " specified in subnet6");
    }
}

void Subnet6::addPool6(const Pool6Ptr& pool) {
    IOAddress first_addr = pool->getFirstAddress();
    IOAddress last_addr = pool->getLastAddress();

    if (!inRange(first_addr) || !inRange(last_addr)) {
        isc_throw(BadValue, "Pool6 (" << first_addr.toText() << "-" << last_addr.toText()
                  << " does not belong in this (" << prefix_ << "/" << prefix_len_
                  << ") subnet6");
    }

    /// @todo: Check that pools do not overlap

    pools_.push_back(pool);
}

Pool6Ptr Subnet6::getPool6(const isc::asiolink::IOAddress& hint /* = IOAddress("::")*/ ) {
    Pool6Ptr candidate;
    for (Pool6Collection::iterator pool = pools_.begin(); pool != pools_.end(); ++pool) {

        // if we won't find anything better, then let's just use the first pool
        if (!candidate) {
            candidate = *pool;
        }

        // if the client provided a pool and there's a pool that hint is valid in,
        // then let's use that pool
        if ((*pool)->inRange(hint)) {
            return (*pool);
        }
    }
    return (candidate);
}

178
void
179
Subnet6::validateOption(const OptionPtr& option) const {
180 181 182
    if (!option) {
        isc_throw(isc::BadValue, "option configured for subnet must not be NULL");
    } else if (option->getUniverse() != Option::V6) {
183
        isc_throw(isc::BadValue, "expected V6 option to be added to the subnet");
184 185
    }
}
186

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
bool Subnet6::inPool(const isc::asiolink::IOAddress& addr) const {

    // Let's start with checking if it even belongs to that subnet.
    if (!inRange(addr)) {
        return (false);
    }

    for (Pool6Collection::const_iterator pool = pools_.begin(); pool != pools_.end(); ++pool) {
        if ((*pool)->inRange(addr)) {
            return (true);
        }
    }
    // there's no pool that address belongs to
    return (false);
}

203 204
} // end of isc::dhcp namespace
} // end of isc namespace