| Kea 3.0.2-git

shared_network.h
Go to the documentation of this file.
1// Copyright (C) 2017-2024 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 SHARED_NETWORK_H
8#define SHARED_NETWORK_H
9
10#include <asiolink/io_address.h>
11#include <cc/data.h>
12#include <dhcpsrv/subnet.h>
13#include <dhcpsrv/subnet_id.h>
14#include <boost/enable_shared_from_this.hpp>
15#include <boost/multi_index/mem_fun.hpp>
16#include <boost/multi_index/hashed_index.hpp>
17#include <boost/multi_index/indexed_by.hpp>
18#include <boost/multi_index/ordered_index.hpp>
19#include <boost/multi_index/random_access_index.hpp>
20#include <boost/multi_index_container.hpp>
21#include <boost/shared_ptr.hpp>
22#include <string>
23
24 namespace isc {
25 namespace dhcp {
26
28 struct SharedNetworkRandomAccessIndexTag { };
29
31 struct SharedNetworkIdIndexTag { };
32
34 struct SharedNetworkNameIndexTag { };
35
37 struct SharedNetworkServerIdIndexTag { };
38
41 struct SharedNetworkModificationTimeIndexTag { };
42
43 class SharedNetwork4;
44
46 typedef boost::shared_ptr<SharedNetwork4> SharedNetwork4Ptr;
47
51 class SharedNetwork4 : public virtual Network4,
52 public boost::enable_shared_from_this<SharedNetwork4> {
53 public:
54
60 explicit SharedNetwork4(const std::string& name)
61 : name_(name), subnets_() {
62 }
63
74 static SharedNetwork4Ptr create(const std::string& name);
75
77 std::string getName() const {
78 return (name_);
79 }
80
84 void setName(const std::string& name) {
85 name_ = name;
86 }
87
97 void add(const Subnet4Ptr& subnet);
98
110 bool replace(const Subnet4Ptr& subnet);
111
117 void del(const SubnetID& subnet_id);
118
120 void delAll();
121
124 const Subnet4SimpleCollection* getAllSubnets() const {
125 return (&subnets_);
126 }
127
134 Subnet4Ptr getSubnet(const SubnetID& subnet_id) const;
135
142 Subnet4Ptr getSubnet(const std::string& subnet_prefix) const;
143
158 ConstSubnet4Ptr getNextSubnet(const ConstSubnet4Ptr& first_subnet,
159 const SubnetID& current_subnet) const;
160
184 ConstSubnet4Ptr getPreferredSubnet(const ConstSubnet4Ptr& selected_subnet) const;
185
194 static
195 bool subnetsIncludeMatchClientId(const ConstSubnet4Ptr& first_subnet,
196 const ClientClasses& client_classes);
197
201 virtual data::ElementPtr toElement() const;
202
206 virtual std::string getLabel() const {
207 std::stringstream ss;
208 ss << "shared-network " << name_;
209 return (ss.str());
210 }
211
212 private:
213
215 std::string name_;
216
218 Subnet4SimpleCollection subnets_;
219};
220
226 typedef boost::multi_index_container<
227 // Multi index container holds pointers to the shared networks.
228 SharedNetwork4Ptr,
229 boost::multi_index::indexed_by<
230 // First is the random access index allowing for accessing objects
231 // just like we'd do with vector.
232 boost::multi_index::random_access<
233 boost::multi_index::tag<SharedNetworkRandomAccessIndexTag>
234 >,
235 // Second index allows for access by shared network id.
236 boost::multi_index::hashed_non_unique<
237 boost::multi_index::tag<SharedNetworkIdIndexTag>,
238 boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
239 &data::BaseStampedElement::getId>
240 >,
241 // Third index allows for access by shared network's name.
242 boost::multi_index::ordered_unique<
243 boost::multi_index::tag<SharedNetworkNameIndexTag>,
244 boost::multi_index::const_mem_fun<SharedNetwork4, std::string,
245 &SharedNetwork4::getName>
246 >,
247 // Fourth index allows for access by server identifier specified for the
248 // network.
249 boost::multi_index::ordered_non_unique<
250 boost::multi_index::tag<SharedNetworkServerIdIndexTag>,
251 boost::multi_index::const_mem_fun<Network4, asiolink::IOAddress,
252 &Network4::getServerId>
253 >,
254 // Fifth index allows for searching using subnet modification time.
255 boost::multi_index::ordered_non_unique<
256 boost::multi_index::tag<SharedNetworkModificationTimeIndexTag>,
257 boost::multi_index::const_mem_fun<data::BaseStampedElement,
258 boost::posix_time::ptime,
259 &data::BaseStampedElement::getModificationTime>
260 >
261 >
262> SharedNetwork4Collection;
263
264 class SharedNetwork6;
265
267 typedef boost::shared_ptr<SharedNetwork6> SharedNetwork6Ptr;
268
272 class SharedNetwork6 : public virtual Network6,
273 public boost::enable_shared_from_this<SharedNetwork6> {
274 public:
275
279 explicit SharedNetwork6(const std::string& name)
280 : name_(name), subnets_() {
281 }
282
293 static SharedNetwork6Ptr create(const std::string& name);
294
296 std::string getName() const {
297 return (name_);
298 }
299
303 void setName(const std::string& name) {
304 name_ = name;
305 }
306
316 void add(const Subnet6Ptr& subnet);
317
329 bool replace(const Subnet6Ptr& subnet);
330
336 void del(const SubnetID& subnet_id);
337
339 void delAll();
340
343 const Subnet6SimpleCollection* getAllSubnets() const {
344 return (&subnets_);
345 }
346
353 Subnet6Ptr getSubnet(const SubnetID& subnet_id) const;
354
361 Subnet6Ptr getSubnet(const std::string& subnet_prefix) const;
362
377 ConstSubnet6Ptr getNextSubnet(const ConstSubnet6Ptr& first_subnet,
378 const SubnetID& current_subnet) const;
379
402 ConstSubnet6Ptr getPreferredSubnet(const ConstSubnet6Ptr& selected_subnet,
403 const Lease::Type& lease_type) const;
404
408 virtual data::ElementPtr toElement() const;
409
413 virtual std::string getLabel() const {
414 std::stringstream ss;
415 ss << "shared-network " << name_;
416 return (ss.str());
417 }
418
419 private:
420
422 std::string name_;
423
425 Subnet6SimpleCollection subnets_;
426};
427
433 typedef boost::multi_index_container<
434 // Multi index container holds pointers to the shared networks.
435 SharedNetwork6Ptr,
436 boost::multi_index::indexed_by<
437 // First is the random access index allowing for accessing objects
438 // just like we'd do with vector.
439 boost::multi_index::random_access<
440 boost::multi_index::tag<SharedNetworkRandomAccessIndexTag>
441 >,
442 // Second index allows for access by shared network id.
443 boost::multi_index::hashed_non_unique<
444 boost::multi_index::tag<SharedNetworkIdIndexTag>,
445 boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
446 &data::BaseStampedElement::getId>
447 >,
448 // Third index allows for access by shared network's name.
449 boost::multi_index::ordered_unique<
450 boost::multi_index::tag<SharedNetworkNameIndexTag>,
451 boost::multi_index::const_mem_fun<SharedNetwork6, std::string,
452 &SharedNetwork6::getName>
453 >,
454 // Fourth index allows for searching using subnet modification time.
455 boost::multi_index::ordered_non_unique<
456 boost::multi_index::tag<SharedNetworkModificationTimeIndexTag>,
457 boost::multi_index::const_mem_fun<data::BaseStampedElement,
458 boost::posix_time::ptime,
459 &data::BaseStampedElement::getModificationTime>
460 >
461 >
462> SharedNetwork6Collection;
463
471 template<typename ReturnPtrType, typename CollectionType>
472 class SharedNetworkFetcher {
473 public:
474
482 static ReturnPtrType get(const CollectionType& collection, const std::string& name) {
483 auto& index = collection.template get<SharedNetworkNameIndexTag>();
484 auto sn = index.find(name);
485 if (sn != index.end()) {
486 return (*sn);
487 }
488 // No network found. Return null pointer.
489 return (ReturnPtrType());
490 }
491};
492
494 using SharedNetworkFetcher4 = SharedNetworkFetcher<SharedNetwork4Ptr, SharedNetwork4Collection>;
495
497 using SharedNetworkFetcher6 = SharedNetworkFetcher<SharedNetwork6Ptr, SharedNetwork6Collection>;
498
499} // end of namespace isc::dhcp
500} // end of namespace isc
501
502#endif // SHARED_NETWORK_H
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic).
Definition io_address.h:45
isc::data::BaseStampedElement
This class represents configuration element which is associated with database identifier and the modi...
Definition base_stamped_element.h:30
isc::data::BaseStampedElement::getModificationTime
boost::posix_time::ptime getModificationTime() const
Returns timestamp.
Definition base_stamped_element.h:61
isc::data::BaseStampedElement::getId
uint64_t getId() const
Returns element's database identifier.
Definition base_stamped_element.h:46
isc::dhcp::ClientClasses
Container for storing client class names.
Definition classify.h:109
isc::dhcp::Network4::getServerId
virtual asiolink::IOAddress getServerId() const
Returns binary representation of the dhcp-server-identifier option (54).
Definition network.cc:339
isc::dhcp::Network4::Network4
Network4()
Constructor.
Definition network.h:1329
isc::dhcp::Network6::Network6
Network6()
Constructor.
Definition network.h:1490
isc::dhcp::SharedNetwork4
Shared network holding IPv4 subnets.
Definition shared_network.h:52
isc::dhcp::SharedNetwork4::getName
std::string getName() const
Returns a name of the shared network.
Definition shared_network.h:77
isc::dhcp::SharedNetwork4::getAllSubnets
const Subnet4SimpleCollection * getAllSubnets() const
Returns a pointer to the collection of subnets within this shared network.
Definition shared_network.h:124
isc::dhcp::SharedNetwork4::replace
bool replace(const Subnet4Ptr &subnet)
Replaces IPv4 subnet in a shared network.
Definition shared_network.cc:353
isc::dhcp::SharedNetwork4::create
static SharedNetwork4Ptr create(const std::string &name)
Factory function creating an instance of the SharedNetwork4.
Definition shared_network.cc:340
isc::dhcp::SharedNetwork4::setName
void setName(const std::string &name)
Sets new name for the shared network.
Definition shared_network.h:84
isc::dhcp::SharedNetwork4::getNextSubnet
ConstSubnet4Ptr getNextSubnet(const ConstSubnet4Ptr &first_subnet, const SubnetID &current_subnet) const
Retrieves next available IPv4 subnet within shared network.
Definition shared_network.cc:399
isc::dhcp::SharedNetwork4::getPreferredSubnet
ConstSubnet4Ptr getPreferredSubnet(const ConstSubnet4Ptr &selected_subnet) const
Attempts to find a subnet which is more likely to include available leases than selected subnet.
Definition shared_network.cc:405
isc::dhcp::SharedNetwork4::toElement
virtual data::ElementPtr toElement() const
Unparses shared network object.
Definition shared_network.cc:422
isc::dhcp::SharedNetwork4::getLabel
virtual std::string getLabel() const
Generates an identifying label for logging.
Definition shared_network.h:206
isc::dhcp::SharedNetwork4::add
void add(const Subnet4Ptr &subnet)
Adds IPv4 subnet to a shared network.
Definition shared_network.cc:345
isc::dhcp::SharedNetwork4::del
void del(const SubnetID &subnet_id)
Removes subnet from a shared network.
Definition shared_network.cc:373
isc::dhcp::SharedNetwork4::getSubnet
Subnet4Ptr getSubnet(const SubnetID &subnet_id) const
Returns a subnet for a specified subnet id.
Definition shared_network.cc:389
isc::dhcp::SharedNetwork4::delAll
void delAll()
Removes all subnets from a shared network.
Definition shared_network.cc:380
isc::dhcp::SharedNetwork4::SharedNetwork4
SharedNetwork4(const std::string &name)
Constructor.
Definition shared_network.h:60
isc::dhcp::SharedNetwork4::subnetsIncludeMatchClientId
static bool subnetsIncludeMatchClientId(const ConstSubnet4Ptr &first_subnet, const ClientClasses &client_classes)
Checks if the shared network includes a subnet with the match client ID flag set to true.
Definition shared_network.cc:410
isc::dhcp::SharedNetwork6
Shared network holding IPv6 subnets.
Definition shared_network.h:273
isc::dhcp::SharedNetwork6::setName
void setName(const std::string &name)
Sets new name for the shared network.
Definition shared_network.h:303
isc::dhcp::SharedNetwork6::create
static SharedNetwork6Ptr create(const std::string &name)
Factory function creating an instance of the SharedNetwork6.
Definition shared_network.cc:441
isc::dhcp::SharedNetwork6::getPreferredSubnet
ConstSubnet6Ptr getPreferredSubnet(const ConstSubnet6Ptr &selected_subnet, const Lease::Type &lease_type) const
Attempts to find a subnet which is more likely to include available leases than selected subnet.
Definition shared_network.cc:505
isc::dhcp::SharedNetwork6::getSubnet
Subnet6Ptr getSubnet(const SubnetID &subnet_id) const
Returns a subnet for a specified subnet id.
Definition shared_network.cc:489
isc::dhcp::SharedNetwork6::SharedNetwork6
SharedNetwork6(const std::string &name)
Constructor.
Definition shared_network.h:279
isc::dhcp::SharedNetwork6::getAllSubnets
const Subnet6SimpleCollection * getAllSubnets() const
Returns a pointer to the collection of subnets within this shared network.
Definition shared_network.h:343
isc::dhcp::SharedNetwork6::getLabel
virtual std::string getLabel() const
Generates an identifying label for logging.
Definition shared_network.h:413
isc::dhcp::SharedNetwork6::del
void del(const SubnetID &subnet_id)
Removes subnet from a shared network.
Definition shared_network.cc:474
isc::dhcp::SharedNetwork6::add
void add(const Subnet6Ptr &subnet)
Adds IPv6 subnet to a shared network.
Definition shared_network.cc:446
isc::dhcp::SharedNetwork6::getNextSubnet
ConstSubnet6Ptr getNextSubnet(const ConstSubnet6Ptr &first_subnet, const SubnetID &current_subnet) const
Retrieves next available IPv6 subnet within shared network.
Definition shared_network.cc:499
isc::dhcp::SharedNetwork6::toElement
virtual data::ElementPtr toElement() const
Unparses shared network object.
Definition shared_network.cc:511
isc::dhcp::SharedNetwork6::replace
bool replace(const Subnet6Ptr &subnet)
Replaces IPv6 subnet in a shared network.
Definition shared_network.cc:454
isc::dhcp::SharedNetwork6::getName
std::string getName() const
Returns a name of the shared network.
Definition shared_network.h:296
isc::dhcp::SharedNetwork6::delAll
void delAll()
Removes all subnets from a shared network.
Definition shared_network.cc:481
isc::dhcp::SharedNetworkFetcher
A class containing static convenience methods to fetch the shared networks from the containers.
Definition shared_network.h:472
isc::dhcp::SharedNetworkFetcher::get
static ReturnPtrType get(const CollectionType &collection, const std::string &name)
Fetches shared network by name.
Definition shared_network.h:482
data.h
io_address.h
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::Subnet4Ptr
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition subnet.h:461
isc::dhcp::SharedNetworkFetcher4
SharedNetworkFetcher< SharedNetwork4Ptr, SharedNetwork4Collection > SharedNetworkFetcher4
Type of the SharedNetworkFetcher used for IPv4.
Definition shared_network.h:494
isc::dhcp::SharedNetwork6Collection
boost::multi_index_container< SharedNetwork6Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SharedNetworkRandomAccessIndexTag > >, boost::multi_index::hashed_non_unique< boost::multi_index::tag< SharedNetworkIdIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, uint64_t, &data::BaseStampedElement::getId > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SharedNetworkNameIndexTag >, boost::multi_index::const_mem_fun< SharedNetwork6, std::string, &SharedNetwork6::getName > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SharedNetworkModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > > > SharedNetwork6Collection
Multi index container holding shared networks.
Definition shared_network.h:462
isc::dhcp::ConstSubnet6Ptr
boost::shared_ptr< const Subnet6 > ConstSubnet6Ptr
A const pointer to a Subnet6 object.
Definition subnet.h:623
isc::dhcp::ConstSubnet4Ptr
boost::shared_ptr< const Subnet4 > ConstSubnet4Ptr
A const pointer to a Subnet4 object.
Definition subnet.h:458
isc::dhcp::Subnet6Ptr
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition subnet.h:626
isc::dhcp::Subnet4SimpleCollection
boost::multi_index_container< Subnet4Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > > > > Subnet4SimpleCollection
A simple collection of Subnet4 objects.
Definition subnet.h:810
isc::dhcp::SharedNetwork6Ptr
boost::shared_ptr< SharedNetwork6 > SharedNetwork6Ptr
Pointer to SharedNetwork6 object.
Definition shared_network.h:267
isc::dhcp::SubnetID
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
isc::dhcp::Subnet6SimpleCollection
boost::multi_index_container< Subnet6Ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetSubnetIdIndexTag >, boost::multi_index::const_mem_fun< Subnet, SubnetID, &Subnet::getID > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SubnetPrefixIndexTag >, boost::multi_index::const_mem_fun< Subnet, std::string, &Subnet::toText > > > > Subnet6SimpleCollection
A simple collection of Subnet6 objects.
Definition subnet.h:890
isc::dhcp::SharedNetwork4Collection
boost::multi_index_container< SharedNetwork4Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SharedNetworkRandomAccessIndexTag > >, boost::multi_index::hashed_non_unique< boost::multi_index::tag< SharedNetworkIdIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, uint64_t, &data::BaseStampedElement::getId > >, boost::multi_index::ordered_unique< boost::multi_index::tag< SharedNetworkNameIndexTag >, boost::multi_index::const_mem_fun< SharedNetwork4, std::string, &SharedNetwork4::getName > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SharedNetworkServerIdIndexTag >, boost::multi_index::const_mem_fun< Network4, asiolink::IOAddress, &Network4::getServerId > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SharedNetworkModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > > > SharedNetwork4Collection
Multi index container holding shared networks.
Definition shared_network.h:262
isc::dhcp::SharedNetwork4Ptr
boost::shared_ptr< SharedNetwork4 > SharedNetwork4Ptr
Pointer to SharedNetwork4 object.
Definition shared_network.h:46
isc::dhcp::SharedNetworkFetcher6
SharedNetworkFetcher< SharedNetwork6Ptr, SharedNetwork6Collection > SharedNetworkFetcher6
Type of the SharedNetworkFetcher used for IPv6.
Definition shared_network.h:497
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::dhcp::Lease::Type
Type
Type of lease or pool.
Definition lease.h:46
isc::dhcp::SharedNetworkIdIndexTag
A tag for accessing index by id.
Definition shared_network.h:31
isc::dhcp::SharedNetworkModificationTimeIndexTag
Tag for the index for searching by shared network modification time.
Definition shared_network.h:41
isc::dhcp::SharedNetworkNameIndexTag
A tag for accessing index by shared network name.
Definition shared_network.h:34
isc::dhcp::SharedNetworkRandomAccessIndexTag
A tag for accessing random access index.
Definition shared_network.h:28
isc::dhcp::SharedNetworkServerIdIndexTag
A tag for accessing index by server identifier.
Definition shared_network.h:37
subnet.h
subnet_id.h