Kea  2.1.7-git
ip_range.cc
Go to the documentation of this file.
1 // Copyright (C) 2020 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 
7 #include <config.h>
9 #include <asiolink/io_address.h>
10 #include <dhcpsrv/ip_range.h>
11 #include <exceptions/exceptions.h>
12 
13 using namespace isc::asiolink;
14 
15 namespace isc {
16 namespace dhcp {
17 
18 AddressRange::AddressRange(const IOAddress& start, const IOAddress& end)
19  : start_(start), end_(end) {
20  // Two IPv4 or two IPv6 addresses are expected as range boundaries.
21  if (start_.getFamily() != end_.getFamily()) {
22  isc_throw(BadValue, "address range boundaries must have the same type: " << start_
23  << ":" << end_);
24  }
25  // The start must be lower or equal the end.
26  if (end_ < start_) {
27  isc_throw(BadValue, "invalid address range boundaries " << start_ << ":" << end_);
28  }
29 }
30 
31 PrefixRange::PrefixRange(const asiolink::IOAddress& prefix, const uint8_t length, const uint8_t delegated)
32  : start_(prefix), end_(IOAddress::IPV6_ZERO_ADDRESS()), prefix_length_(length),
33  delegated_length_(delegated) {
34  if (!start_.isV6()) {
35  isc_throw(BadValue, "IPv6 prefix required for prefix delegation range but "
36  << start_ << " was specified");
37  }
39  isc_throw(BadValue, "delegated length " << static_cast<int>(delegated_length_)
40  << " must not be lower than prefix length " << static_cast<int>(length));
41  }
42  if ((prefix_length_ > 128) || (delegated_length_ > 128)) {
43  isc_throw(BadValue, "delegated length " << static_cast<int>(delegated_length_)
44  << " and prefix length " << static_cast<int>(length)
45  << " must not be greater than 128");
46  }
47  // Now calculate the last prefix in the range.
48  auto prefixes_num = prefixesInRange(prefix_length_, delegated_length_);
49  uint64_t addrs_in_prefix = static_cast<uint64_t>(1) << (128 - delegated_length_);
50  end_ = offsetAddress(prefix, (prefixes_num - 1) * addrs_in_prefix);
51 }
52 
54  const uint8_t delegated)
55  : start_(start), end_(end), prefix_length_(0), delegated_length_(delegated) {
56  if (!start_.isV6() || !end_.isV6()) {
57  isc_throw(BadValue, "IPv6 prefix required for prefix delegation range but "
58  << start_ << ":" << end_ << " was specified");
59  }
60  // The start must be lower or equal the end.
61  if (end_ < start_) {
62  isc_throw(BadValue, "invalid address range boundaries " << start_ << ":" << end_);
63  }
64  if (delegated_length_ > 128) {
65  isc_throw(BadValue, "delegated length " << static_cast<int>(delegated_length_)
66  << " must not be greater than 128");
67  }
68 }
69 
70 } // end of namespace isc::dhcp
71 } // end of namespace isc
asiolink::IOAddress end_
IP address denoting the end of the address range.
Definition: ip_range.h:20
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
PrefixRange(const asiolink::IOAddress &prefix, const uint8_t length, const uint8_t delegated)
Constructor.
Definition: ip_range.cc:31
Defines the logger used by the top-level component of kea-lfc.
asiolink::IOAddress end_
IP address denoting the first address within the last prefix in the prefix range. ...
Definition: ip_range.h:37
asiolink::IOAddress start_
IP address denoting the start of the address range.
Definition: ip_range.h:18
uint8_t prefix_length_
Prefix length.
Definition: ip_range.h:39
asiolink::IOAddress start_
IP address denoting the start of the prefix range.
Definition: ip_range.h:34
uint8_t delegated_length_
Delegated prefix length.
Definition: ip_range.h:41