Skip to content

Commit 639e0e6

Browse files
committed
[#1415] Moved address range to common place
1 parent 0ef972a commit 639e0e6

File tree

7 files changed

+101
-27
lines changed

7 files changed

+101
-27
lines changed

src/lib/dhcpsrv/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ CLEANFILES += *.csv
6363

6464
lib_LTLIBRARIES = libkea-dhcpsrv.la
6565
libkea_dhcpsrv_la_SOURCES =
66+
libkea_dhcpsrv_la_SOURCES += address_range.h address_range.cc
6667
libkea_dhcpsrv_la_SOURCES += alloc_engine.cc alloc_engine.h
6768
libkea_dhcpsrv_la_SOURCES += alloc_engine_log.cc alloc_engine_log.h
6869
libkea_dhcpsrv_la_SOURCES += alloc_engine_messages.h alloc_engine_messages.cc

src/lib/dhcpsrv/address_range.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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>
8+
#include <asiolink/io_address.h>
9+
#include <dhcpsrv/address_range.h>
10+
#include <exceptions/exceptions.h>
11+
12+
using namespace isc::asiolink;
13+
14+
namespace isc {
15+
namespace dhcp {
16+
17+
AddressRange::AddressRange(const IOAddress& start, const IOAddress& end)
18+
: start_(start), end_(end) {
19+
// The start must be lower or equal the end.
20+
if (end_ < start_) {
21+
isc_throw(BadValue, "invalid address range boundaries " << start_ << ":" << end_);
22+
}
23+
// Two IPv4 or two IPv6 addresses are expected as range boundaries.
24+
if (start_.getFamily() != end_.getFamily()) {
25+
isc_throw(BadValue, "address range boundaries must have the same type: " << start_
26+
<< ":" << end_);
27+
}
28+
}
29+
30+
} // end of namespace isc::dhcp
31+
} // end of namespace isc

src/lib/dhcpsrv/address_range.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
#ifndef ADDRESS_RANGE_H
8+
#define ADDRESS_RANGE_H
9+
10+
#include <asiolink/io_address.h>
11+
12+
namespace isc {
13+
namespace dhcp {
14+
15+
/// @brief Structure representing IP address range.
16+
struct AddressRange {
17+
/// IP address denoting the start of the address range.
18+
asiolink::IOAddress start_;
19+
/// IP address denoting the end of the address range.
20+
asiolink::IOAddress end_;
21+
22+
/// @brief Constructor.
23+
///
24+
/// @param start beginning of the address range.
25+
/// @param end end of the address range.
26+
/// @throw BadValue if the @c start is greater than the end or
27+
/// specified boundaries do not belong to the same family.
28+
AddressRange(const asiolink::IOAddress& start, const asiolink::IOAddress& end);
29+
};
30+
31+
} // end of namespace isc::dhcp
32+
} // end of namespace isc
33+
34+
#endif // ADDRESS_RANGE_H

src/lib/dhcpsrv/free_lease_queue.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,6 @@ using namespace isc::asiolink;
2020
namespace isc {
2121
namespace dhcp {
2222

23-
FreeLeaseQueue::Range::Range(const IOAddress& start, const IOAddress& end)
24-
: start_(start), end_(end) {
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-
// Two IPv4 or two IPv6 addresses are expected as range boundaries.
30-
if (start_.getFamily() != end_.getFamily()) {
31-
isc_throw(BadValue, "address range boundaries must have the same type: " << start_
32-
<< ":" << end_);
33-
}
34-
}
35-
3623
FreeLeaseQueue::FreeLeaseQueue()
3724
: containers_() {
3825
}

src/lib/dhcpsrv/free_lease_queue.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define FREE_LEASE_QUEUE_H
99

1010
#include <asiolink/io_address.h>
11+
#include <dhcpsrv/address_range.h>
1112

1213
#include <boost/multi_index_container.hpp>
1314
#include <boost/multi_index/hashed_index.hpp>
@@ -74,20 +75,7 @@ class FreeLeaseQueue {
7475
public:
7576

7677
/// @brief Structure representing address range in the @c FreeLeaseQueue.
77-
struct Range {
78-
/// IP address denoting the start of the address range.
79-
asiolink::IOAddress start_;
80-
/// IP address denoting the end of the address range.
81-
asiolink::IOAddress end_;
82-
83-
/// @brief Constructor.
84-
///
85-
/// @param start beginning of the address range.
86-
/// @param end end of the address range.
87-
/// @throw BadValue if the @c start is greater than the end or
88-
/// specified boundaries do not belong to the same family.
89-
Range(const asiolink::IOAddress& start, const asiolink::IOAddress& end);
90-
};
78+
typedef AddressRange Range;
9179

9280
/// @brief Constructor.
9381
FreeLeaseQueue();

src/lib/dhcpsrv/tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ libco3_la_LDFLAGS = -avoid-version -export-dynamic -module -rpath /nowhere
5757
TESTS += libdhcpsrv_unittests
5858

5959
libdhcpsrv_unittests_SOURCES = run_unittests.cc
60+
libdhcpsrv_unittests_SOURCES += address_range_unittest.cc
6061
libdhcpsrv_unittests_SOURCES += alloc_engine_utils.cc alloc_engine_utils.h
6162
libdhcpsrv_unittests_SOURCES += alloc_engine_expiration_unittest.cc
6263
libdhcpsrv_unittests_SOURCES += alloc_engine_hooks_unittest.cc
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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>
8+
#include <dhcpsrv/address_range.h>
9+
10+
#include <gtest/gtest.h>
11+
12+
using namespace isc;
13+
using namespace isc::asiolink;
14+
using namespace isc::dhcp;
15+
16+
namespace {
17+
18+
// This test verifies that an exception is thrown upon an attempt to
19+
// create an address range from invalid values and that no exception
20+
// is thrown when the values are correct.
21+
TEST(AddressRangeTest, constructorWithInvalidValues) {
22+
// The start address must be lower or equal the end address.
23+
EXPECT_THROW(AddressRange(IOAddress("192.0.2.100"), IOAddress("192.0.2.99")),
24+
BadValue);
25+
// The start and end address must be of the same family.
26+
EXPECT_THROW(AddressRange(IOAddress("192.0.2.100"), IOAddress("2001:db8:1::1")),
27+
BadValue);
28+
// It is allowed to create address range with a single IP address.
29+
EXPECT_NO_THROW(AddressRange(IOAddress("192.0.2.100"), IOAddress("192.0.2.100")));
30+
}
31+
32+
} // end of anonymous namespace

0 commit comments

Comments
 (0)