summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcpsrv/ip_range.cc
blob: 1ca617948d289f0493c526e8239a2962d33eebef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (C) 2020 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
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <asiolink/addr_utilities.h>
#include <asiolink/io_address.h>
#include <dhcpsrv/ip_range.h>
#include <exceptions/exceptions.h>

using namespace isc::asiolink;

namespace isc {
namespace dhcp {

AddressRange::AddressRange(const IOAddress& start, const IOAddress& end)
    : start_(start), end_(end) {
    // Two IPv4 or two IPv6 addresses are expected as range boundaries.
    if (start_.getFamily() != end_.getFamily()) {
        isc_throw(BadValue, "address range boundaries must have the same type: " << start_
                  << ":" << end_);
    }
    // The start must be lower or equal the end.
    if (end_ < start_) {
        isc_throw(BadValue, "invalid address range boundaries " << start_ << ":" << end_);
    }
}

PrefixRange::PrefixRange(const asiolink::IOAddress& prefix, const uint8_t length, const uint8_t delegated)
    : start_(prefix), end_(IOAddress::IPV6_ZERO_ADDRESS()), prefix_length_(length),
      delegated_length_(delegated) {
    if (!start_.isV6()) {
        isc_throw(BadValue, "IPv6 prefix required for prefix delegation range but "
                  << start_ << " was specified");
    }
    if (delegated_length_ < prefix_length_) {
        isc_throw(BadValue, "delegated length " << static_cast<int>(delegated_length_)
                  << " must not be lower than prefix length " << static_cast<int>(length));
    }
    if ((prefix_length_ > 128) || (delegated_length_ > 128)) {
        isc_throw(BadValue, "delegated length " << static_cast<int>(delegated_length_)
                  << " and prefix length " << static_cast<int>(length)
                  << " must not be greater than 128");
    }
    // Now calculate the last prefix in the range.
    auto prefixes_num = prefixesInRange(prefix_length_, delegated_length_);
    uint64_t addrs_in_prefix = static_cast<uint64_t>(1) << (128 - delegated_length_);
    end_ = offsetAddress(prefix, (prefixes_num - 1) * addrs_in_prefix);
}

PrefixRange::PrefixRange(const asiolink::IOAddress& start, const asiolink::IOAddress& end,
                         const uint8_t delegated)
    : start_(start), end_(end), prefix_length_(0), delegated_length_(delegated) {
    if (!start_.isV6() || !end_.isV6()) {
        isc_throw(BadValue, "IPv6 prefix required for prefix delegation range but "
                  << start_ << ":" << end_ << " was specified");
    }
    // The start must be lower or equal the end.
    if (end_ < start_) {
        isc_throw(BadValue, "invalid address range boundaries " << start_ << ":" << end_);
    }
    if (delegated_length_ > 128) {
        isc_throw(BadValue, "delegated length " << static_cast<int>(delegated_length_)
                  << " must not be greater than 128");
    }
}
 
} // end of namespace isc::dhcp
} // end of namespace isc