Kea  2.1.7-git
shared_network.h
Go to the documentation of this file.
1 // Copyright (C) 2017-2022 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 
29 
32 
35 
38 
42 
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 
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  Subnet4Ptr getNextSubnet(const Subnet4Ptr& first_subnet,
159  const SubnetID& current_subnet) const;
160 
184  Subnet4Ptr getPreferredSubnet(const Subnet4Ptr& selected_subnet) const;
185 
194  static
195  bool subnetsIncludeMatchClientId(const Subnet4Ptr& first_subnet,
196  const ClientClasses& client_classes);
197 
201  virtual data::ElementPtr toElement() const;
202 
203 private:
204 
206  std::string name_;
207 
209  Subnet4SimpleCollection subnets_;
210 };
211 
217 typedef boost::multi_index_container<
218  // Multi index container holds pointers to the shared networks.
220  boost::multi_index::indexed_by<
221  // First is the random access index allowing for accessing objects
222  // just like we'd do with vector.
223  boost::multi_index::random_access<
224  boost::multi_index::tag<SharedNetworkRandomAccessIndexTag>
225  >,
226  // Second index allows for access by shared network id.
227  boost::multi_index::hashed_non_unique<
228  boost::multi_index::tag<SharedNetworkIdIndexTag>,
229  boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
231  >,
232  // Third index allows for access by shared network's name.
233  boost::multi_index::ordered_unique<
234  boost::multi_index::tag<SharedNetworkNameIndexTag>,
235  boost::multi_index::const_mem_fun<SharedNetwork4, std::string,
237  >,
238  // Fourth index allows for access by server identifier specified for the
239  // network.
240  boost::multi_index::ordered_non_unique<
241  boost::multi_index::tag<SharedNetworkServerIdIndexTag>,
242  boost::multi_index::const_mem_fun<Network4, asiolink::IOAddress,
244  >,
245  // Fifth index allows for searching using subnet modification time.
246  boost::multi_index::ordered_non_unique<
247  boost::multi_index::tag<SharedNetworkModificationTimeIndexTag>,
248  boost::multi_index::const_mem_fun<data::BaseStampedElement,
249  boost::posix_time::ptime,
251  >
252  >
254 
256 
258 typedef boost::shared_ptr<SharedNetwork6> SharedNetwork6Ptr;
259 
263 class SharedNetwork6 : public virtual Network6,
264  public boost::enable_shared_from_this<SharedNetwork6> {
265 public:
266 
270  explicit SharedNetwork6(const std::string& name)
271  : name_(name), subnets_() {
272  }
273 
284  static SharedNetwork6Ptr create(const std::string& name);
285 
287  std::string getName() const {
288  return (name_);
289  }
290 
294  void setName(const std::string& name) {
295  name_ = name;
296  }
297 
307  void add(const Subnet6Ptr& subnet);
308 
320  bool replace(const Subnet6Ptr& subnet);
321 
327  void del(const SubnetID& subnet_id);
328 
330  void delAll();
331 
335  return (&subnets_);
336  }
337 
344  Subnet6Ptr getSubnet(const SubnetID& subnet_id) const;
345 
352  Subnet6Ptr getSubnet(const std::string& subnet_prefix) const;
353 
368  Subnet6Ptr getNextSubnet(const Subnet6Ptr& first_subnet,
369  const SubnetID& current_subnet) const;
370 
393  Subnet6Ptr getPreferredSubnet(const Subnet6Ptr& selected_subnet,
394  const Lease::Type& lease_type) const;
395 
399  virtual data::ElementPtr toElement() const;
400 
401 private:
402 
404  std::string name_;
405 
407  Subnet6SimpleCollection subnets_;
408 };
409 
415 typedef boost::multi_index_container<
416  // Multi index container holds pointers to the shared networks.
418  boost::multi_index::indexed_by<
419  // First is the random access index allowing for accessing objects
420  // just like we'd do with vector.
421  boost::multi_index::random_access<
422  boost::multi_index::tag<SharedNetworkRandomAccessIndexTag>
423  >,
424  // Second index allows for access by shared network id.
425  boost::multi_index::hashed_non_unique<
426  boost::multi_index::tag<SharedNetworkIdIndexTag>,
427  boost::multi_index::const_mem_fun<data::BaseStampedElement, uint64_t,
428  &data::BaseStampedElement::getId>
429  >,
430  // Third index allows for access by shared network's name.
431  boost::multi_index::ordered_unique<
432  boost::multi_index::tag<SharedNetworkNameIndexTag>,
433  boost::multi_index::const_mem_fun<SharedNetwork6, std::string,
435  >,
436  // Fourth index allows for searching using subnet modification time.
437  boost::multi_index::ordered_non_unique<
438  boost::multi_index::tag<SharedNetworkModificationTimeIndexTag>,
439  boost::multi_index::const_mem_fun<data::BaseStampedElement,
440  boost::posix_time::ptime,
442  >
443  >
445 
453 template<typename ReturnPtrType, typename CollectionType>
455 public:
456 
464  static ReturnPtrType get(const CollectionType& collection, const std::string& name) {
465  auto& index = collection.template get<SharedNetworkNameIndexTag>();
466  auto sn = index.find(name);
467  if (sn != index.end()) {
468  return (*sn);
469  }
470  // No network found. Return null pointer.
471  return (ReturnPtrType());
472  }
473 };
474 
477 
480 
481 } // end of namespace isc::dhcp
482 } // end of namespace isc
483 
484 #endif // SHARED_NETWORK_H
void setName(const std::string &name)
Sets new name for the shared network.
A class containing static convenience methods to fetch the shared networks from the containers...
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:524
A tag for accessing index by id.
std::string getName() const
Returns a name of the shared network.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
const Subnet4SimpleCollection * getAllSubnets() const
Returns a pointer to the collection of subnets within this shared network.
Shared network holding IPv4 subnets.
Specialization of the Network object for DHCPv6 case.
Definition: network.h:1283
A tag for accessing index by shared network name.
SharedNetwork4(const std::string &name)
Constructor.
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:837
boost::posix_time::ptime getModificationTime() const
Returns timestamp.
const Subnet6SimpleCollection * getAllSubnets() const
Returns a pointer to the collection of subnets within this shared network.
uint64_t getId() const
Returns element&#39;s database identifier.
std::string getName() const
Returns a name of the shared network.
boost::shared_ptr< SharedNetwork6 > SharedNetwork6Ptr
Pointer to SharedNetwork6 object.
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:917
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.
boost::shared_ptr< SharedNetwork4 > SharedNetwork4Ptr
Pointer to SharedNetwork4 object.
A tag for accessing index by server identifier.
Defines the logger used by the top-level component of kea-lfc.
A tag for accessing random access index.
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.
const Name & name_
Definition: dns/message.cc:693
Type
Type of lease or pool.
Definition: lease.h:45
This class represents configuration element which is associated with database identifier and the modi...
virtual asiolink::IOAddress getServerId() const
Returns binary representation of the dhcp-server-identifier option (54).
Definition: network.cc:329
Shared network holding IPv6 subnets.
Specialization of the Network object for DHCPv4 case.
Definition: network.h:1151
Tag for the index for searching by shared network modification time.
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition: subnet.h:672
SharedNetwork6(const std::string &name)
Constructor.
Container for storing client class names.
Definition: classify.h:68
void setName(const std::string &name)
Sets new name for the shared network.
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition: subnet_id.h:24