Kea 2.0.1
subnet.h
Go to the documentation of this file.
1// Copyright (C) 2012-2021 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 SUBNET_H
8#define SUBNET_H
9
10#include <asiolink/io_address.h>
11#include <cc/data.h>
12#include <cc/user_context.h>
14#include <dhcpsrv/lease.h>
15#include <dhcpsrv/network.h>
16#include <dhcpsrv/pool.h>
17#include <dhcpsrv/subnet_id.h>
18#include <dhcpsrv/triplet.h>
19#include <boost/multi_index/mem_fun.hpp>
20#include <boost/multi_index/indexed_by.hpp>
21#include <boost/multi_index/ordered_index.hpp>
22#include <boost/multi_index/random_access_index.hpp>
23#include <boost/multi_index_container.hpp>
24#include <boost/date_time/posix_time/posix_time.hpp>
25#include <boost/pointer_cast.hpp>
26#include <boost/scoped_ptr.hpp>
27#include <boost/shared_ptr.hpp>
28#include <cstdint>
29#include <map>
30#include <mutex>
31#include <utility>
32
33namespace isc {
34namespace dhcp {
35
36class Subnet : public virtual Network {
37public:
38
44 bool inRange(const isc::asiolink::IOAddress& addr) const;
45
60 bool inPool(Lease::Type type, const isc::asiolink::IOAddress& addr) const;
61
71 bool inPool(Lease::Type type,
72 const isc::asiolink::IOAddress& addr,
73 const ClientClasses& client_classes) const;
74
89
101 boost::posix_time::ptime
102 getLastAllocatedTime(const Lease::Type& lease_type) const;
103
117 const isc::asiolink::IOAddress& addr);
118
122 SubnetID getID() const { return (id_); }
123
127 std::pair<isc::asiolink::IOAddress, uint8_t> get() const {
128 return (std::make_pair(prefix_, prefix_len_));
129 }
130
153 void addPool(const PoolPtr& pool);
154
160 void delPools(Lease::Type type);
161
180 const PoolPtr getPool(Lease::Type type, const isc::asiolink::IOAddress& addr,
181 bool anypool = true) const;
182
190 const PoolPtr getPool(Lease::Type type,
191 const ClientClasses& client_classes,
192 const isc::asiolink::IOAddress& addr) const;
193
199 return (getPool(type, default_pool()));
200 }
201
207
214 const PoolCollection& getPools(Lease::Type type) const;
215
219 uint64_t getPoolCapacity(Lease::Type type) const;
220
227 uint64_t getPoolCapacity(Lease::Type type,
228 const ClientClasses& client_classes) const;
229
234 virtual std::string toText() const;
235
241 static void resetSubnetID() {
242 static_id_ = 1;
243 }
244
261 template<typename SharedNetworkPtrType>
262 void getSharedNetwork(SharedNetworkPtrType& shared_network) const {
263 shared_network = boost::dynamic_pointer_cast<
264 typename SharedNetworkPtrType::element_type>(parent_network_.lock());
265 }
266
274 void setSharedNetwork(const NetworkPtr& shared_network) {
275 parent_network_ = shared_network;
276 }
277
281 std::string getSharedNetworkName() const {
282 return (shared_network_name_);
283 }
284
297 void setSharedNetworkName(const std::string& shared_network_name) {
298 shared_network_name_ = shared_network_name;
299 }
300
308
309protected:
310
312 //
325 Subnet(const isc::asiolink::IOAddress& prefix, uint8_t len,
326 const SubnetID id);
327
332 virtual ~Subnet() { };
333
342
352 if (static_id_ == SUBNET_ID_MAX) {
354 }
355
356 return (static_id_++);
357 }
358
367 virtual void checkType(Lease::Type type) const = 0;
368
373 uint64_t sumPoolCapacity(const PoolCollection& pools) const;
374
380 uint64_t sumPoolCapacity(const PoolCollection& pools,
381 const ClientClasses& client_classes) const;
382
391 bool poolOverlaps(const Lease::Type& pool_type, const PoolPtr& pool) const;
392
396 virtual data::ElementPtr toElement() const;
397
406 static std::pair<asiolink::IOAddress, uint8_t>
407 parsePrefixCommon(const std::string& prefix);
408
414
417
420
423
426
428 uint8_t prefix_len_;
429
440
445
450
455 std::map<Lease::Type, boost::posix_time::ptime> last_allocated_time_;
456
459
460private:
461
475 isc::asiolink::IOAddress getLastAllocatedInternal(Lease::Type type) const;
476
488 boost::posix_time::ptime
489 getLastAllocatedTimeInternal(const Lease::Type& lease_type) const;
490
505 void setLastAllocatedInternal(Lease::Type type,
506 const isc::asiolink::IOAddress& addr);
507
509 boost::scoped_ptr<std::mutex> mutex_;
510};
511
513typedef boost::shared_ptr<Subnet> SubnetPtr;
514
515
516class Subnet4;
517
519typedef boost::shared_ptr<const Subnet4> ConstSubnet4Ptr;
520
522typedef boost::shared_ptr<Subnet4> Subnet4Ptr;
523
529class Subnet4 : public Subnet, public Network4 {
530public:
531
543 Subnet4(const isc::asiolink::IOAddress& prefix, uint8_t length,
544 const Triplet<uint32_t>& t1,
545 const Triplet<uint32_t>& t2,
546 const Triplet<uint32_t>& valid_lifetime,
547 const SubnetID id = 0);
548
565 static Subnet4Ptr
566 create(const isc::asiolink::IOAddress& prefix, uint8_t length,
567 const Triplet<uint32_t>& t1,
568 const Triplet<uint32_t>& t2,
569 const Triplet<uint32_t>& valid_lifetime,
570 const SubnetID id = 0);
571
584 Subnet4Ptr getNextSubnet(const Subnet4Ptr& first_subnet) const;
585
598 Subnet4Ptr getNextSubnet(const Subnet4Ptr& first_subnet,
599 const ClientClasses& client_classes) const;
600
612 virtual bool
613 clientSupported(const isc::dhcp::ClientClasses& client_classes) const;
614
620 return (dhcp4o6_);
621 }
622
627 const Cfg4o6& get4o6() const {
628 return (dhcp4o6_);
629 }
630
634 virtual data::ElementPtr toElement() const;
635
640 static std::pair<asiolink::IOAddress, uint8_t>
641 parsePrefix(const std::string& prefix);
642
643private:
644
648 virtual isc::asiolink::IOAddress default_pool() const {
649 return (isc::asiolink::IOAddress("0.0.0.0"));
650 }
651
658 virtual void checkType(Lease::Type type) const;
659
661 Cfg4o6 dhcp4o6_;
662};
663
664class Subnet6;
665
667typedef boost::shared_ptr<const Subnet6> ConstSubnet6Ptr;
668
670typedef boost::shared_ptr<Subnet6> Subnet6Ptr;
671
677class Subnet6 : public Subnet, public Network6 {
678public:
679
692 Subnet6(const isc::asiolink::IOAddress& prefix, uint8_t length,
693 const Triplet<uint32_t>& t1,
694 const Triplet<uint32_t>& t2,
695 const Triplet<uint32_t>& preferred_lifetime,
696 const Triplet<uint32_t>& valid_lifetime,
697 const SubnetID id = 0);
698
716 static Subnet6Ptr
717 create(const isc::asiolink::IOAddress& prefix, uint8_t length,
718 const Triplet<uint32_t>& t1,
719 const Triplet<uint32_t>& t2,
720 const Triplet<uint32_t>& preferred_lifetime,
721 const Triplet<uint32_t>& valid_lifetime,
722 const SubnetID id = 0);
723
736 Subnet6Ptr getNextSubnet(const Subnet6Ptr& first_subnet) const;
737
750 Subnet6Ptr getNextSubnet(const Subnet6Ptr& first_subnet,
751 const ClientClasses& client_classes) const;
752
764 virtual bool
765 clientSupported(const isc::dhcp::ClientClasses& client_classes) const;
766
770 virtual data::ElementPtr toElement() const;
771
776 static std::pair<asiolink::IOAddress, uint8_t>
777 parsePrefix(const std::string& prefix);
778
779private:
780
783 virtual isc::asiolink::IOAddress default_pool() const {
784 return (isc::asiolink::IOAddress("::"));
785 }
786
793 virtual void checkType(Lease::Type type) const;
794
795};
796
799
800
803
806
809
812
836typedef boost::multi_index_container<
837 // Multi index container holds pointers to the subnets.
839 // The following holds all indexes.
840 boost::multi_index::indexed_by<
841 // First index allows for searching using subnet identifier.
842 boost::multi_index::ordered_unique<
843 boost::multi_index::tag<SubnetSubnetIdIndexTag>,
844 boost::multi_index::const_mem_fun<Subnet, SubnetID, &Subnet::getID>
845 >,
846 // Second index allows for searching using an output from toText function.
847 boost::multi_index::ordered_unique<
848 boost::multi_index::tag<SubnetPrefixIndexTag>,
849 boost::multi_index::const_mem_fun<Subnet, std::string, &Subnet::toText>
850 >,
851
852 // Third index allows for searching using an output from getServerId.
853 boost::multi_index::ordered_non_unique<
854 boost::multi_index::tag<SubnetServerIdIndexTag>,
855 boost::multi_index::const_mem_fun<Network4, asiolink::IOAddress,
857 >,
858
859 // Forth index allows for searching using subnet modification time.
860 boost::multi_index::ordered_non_unique<
861 boost::multi_index::tag<SubnetModificationTimeIndexTag>,
862 boost::multi_index::const_mem_fun<data::BaseStampedElement,
863 boost::posix_time::ptime,
865 >
866 >
868
891typedef boost::multi_index_container<
892 // Multi index container holds pointers to the subnets.
894 // The following holds all indexes.
895 boost::multi_index::indexed_by<
896 // First index allows for searching using subnet identifier.
897 boost::multi_index::ordered_unique<
898 boost::multi_index::tag<SubnetSubnetIdIndexTag>,
899 boost::multi_index::const_mem_fun<Subnet, SubnetID, &Subnet::getID>
900 >,
901 // Second index allows for searching using an output from toText function.
902 boost::multi_index::ordered_unique<
903 boost::multi_index::tag<SubnetPrefixIndexTag>,
904 boost::multi_index::const_mem_fun<Subnet, std::string, &Subnet::toText>
905 >,
906 // Third index allows for searching using subnet modification time.
907 boost::multi_index::ordered_non_unique<
908 boost::multi_index::tag<SubnetModificationTimeIndexTag>,
909 boost::multi_index::const_mem_fun<data::BaseStampedElement,
910 boost::posix_time::ptime,
912 >
913 >
915
922template<typename ReturnPtrType, typename CollectionType>
924public:
925
933 static ReturnPtrType get(const CollectionType& collection,
934 const SubnetID& subnet_id) {
935 auto& index = collection.template get<SubnetSubnetIdIndexTag>();
936 auto s = index.find(subnet_id);
937 if (s != index.end()) {
938 return (*s);
939 }
940 // No subnet found. Return null pointer.
941 return (ReturnPtrType());
942 }
943};
944
947
950
951
953
954} // end of isc::dhcp namespace
955} // end of isc namespace
956
957#endif // SUBNET_H
This class represents configuration element which is associated with database identifier and the modi...
boost::posix_time::ptime getModificationTime() const
Returns timestamp.
Container for storing client class names.
Definition: classify.h:43
Specialization of the Network object for DHCPv4 case.
Definition: network.h:1138
virtual asiolink::IOAddress getServerId() const
Returns binary representation of the dhcp-server-identifier option (54).
Definition: network.cc:333
Specialization of the Network object for DHCPv6 case.
Definition: network.h:1267
Common interface representing a network to which the DHCP clients are connected.
Definition: network.h:124
WeakNetworkPtr parent_network_
Pointer to another network that this network belongs to.
Definition: network.h:1130
A configuration holder for IPv4 subnet.
Definition: subnet.h:529
Subnet4(const isc::asiolink::IOAddress &prefix, uint8_t length, const Triplet< uint32_t > &t1, const Triplet< uint32_t > &t2, const Triplet< uint32_t > &valid_lifetime, const SubnetID id=0)
Constructor with all parameters.
Definition: subnet.cc:279
virtual bool clientSupported(const isc::dhcp::ClientClasses &client_classes) const
Checks whether this subnet and parent shared network supports the client that belongs to specified cl...
Definition: subnet.cc:347
static Subnet4Ptr create(const isc::asiolink::IOAddress &prefix, uint8_t length, const Triplet< uint32_t > &t1, const Triplet< uint32_t > &t2, const Triplet< uint32_t > &valid_lifetime, const SubnetID id=0)
Factory function creating an instance of the Subnet4.
Definition: subnet.cc:297
Cfg4o6 & get4o6()
Returns DHCP4o6 configuration parameters.
Definition: subnet.h:619
Subnet4Ptr getNextSubnet(const Subnet4Ptr &first_subnet) const
Returns next subnet within shared network.
Definition: subnet.cc:308
virtual data::ElementPtr toElement() const
Unparse a subnet object.
Definition: subnet.cc:738
const Cfg4o6 & get4o6() const
Returns const DHCP4o6 configuration parameters.
Definition: subnet.h:627
static std::pair< asiolink::IOAddress, uint8_t > parsePrefix(const std::string &prefix)
Converts subnet prefix to a pair of prefix/length pair.
Definition: subnet.cc:763
A configuration holder for IPv6 subnet.
Definition: subnet.h:677
virtual bool clientSupported(const isc::dhcp::ClientClasses &client_classes) const
Checks whether this subnet and parent shared network supports the client that belongs to specified cl...
Definition: subnet.cc:710
static Subnet6Ptr create(const isc::asiolink::IOAddress &prefix, uint8_t length, const Triplet< uint32_t > &t1, const Triplet< uint32_t > &t2, const Triplet< uint32_t > &preferred_lifetime, const Triplet< uint32_t > &valid_lifetime, const SubnetID id=0)
Factory function creating an instance of the Subnet4.
Definition: subnet.cc:651
static std::pair< asiolink::IOAddress, uint8_t > parsePrefix(const std::string &prefix)
Converts subnet prefix to a pair of prefix/length pair.
Definition: subnet.cc:804
Subnet6(const isc::asiolink::IOAddress &prefix, uint8_t length, const Triplet< uint32_t > &t1, const Triplet< uint32_t > &t2, const Triplet< uint32_t > &preferred_lifetime, const Triplet< uint32_t > &valid_lifetime, const SubnetID id=0)
Constructor with all parameters.
Definition: subnet.cc:631
Subnet6Ptr getNextSubnet(const Subnet6Ptr &first_subnet) const
Returns next subnet within shared network.
Definition: subnet.cc:672
virtual data::ElementPtr toElement() const
Unparse a subnet object.
Definition: subnet.cc:773
A class containing static convenience methods to fetch the subnets from the containers.
Definition: subnet.h:923
static ReturnPtrType get(const CollectionType &collection, const SubnetID &subnet_id)
Fetches subnets by id.
Definition: subnet.h:933
void setSharedNetworkName(const std::string &shared_network_name)
Sets new shared network name.
Definition: subnet.h:297
isc::asiolink::IOAddress prefix_
a prefix of the subnet.
Definition: subnet.h:425
SubnetID getID() const
Returns unique ID for that subnet.
Definition: subnet.h:122
uint8_t prefix_len_
a prefix length of the subnet.
Definition: subnet.h:428
static void resetSubnetID()
Resets subnet-id counter to its initial value (1).
Definition: subnet.h:241
std::map< Lease::Type, boost::posix_time::ptime > last_allocated_time_
Timestamp indicating when a lease of a specified type has been last allocated from this subnet.
Definition: subnet.h:455
virtual data::ElementPtr toElement() const
Unparse a subnet object.
Definition: subnet.cc:721
static SubnetID generateNextID()
returns the next unique Subnet-ID.
Definition: subnet.h:351
PoolCollection pools_ta_
collection of IPv6 temporary address pools in that subnet.
Definition: subnet.h:419
void getSharedNetwork(SharedNetworkPtrType &shared_network) const
Retrieves pointer to a shared network associated with a subnet.
Definition: subnet.h:262
SubnetID id_
subnet-id
Definition: subnet.h:413
PoolPtr getAnyPool(Lease::Type type)
Returns a pool without any address specified.
Definition: subnet.h:198
void addPool(const PoolPtr &pool)
Adds a new pool for the subnet.
Definition: subnet.cc:461
virtual ~Subnet()
virtual destructor.
Definition: subnet.h:332
bool inRange(const isc::asiolink::IOAddress &addr) const
checks if specified address is in range.
Definition: subnet.cc:83
isc::asiolink::IOAddress last_allocated_pd_
last allocated IPv6 prefix.
Definition: subnet.h:449
virtual std::string toText() const
Returns textual representation of the subnet (e.g.
Definition: subnet.cc:174
void delPools(Lease::Type type)
Deletes all pools of specified type.
Definition: subnet.cc:516
std::pair< isc::asiolink::IOAddress, uint8_t > get() const
Returns subnet parameters (prefix and prefix length).
Definition: subnet.h:127
static std::pair< asiolink::IOAddress, uint8_t > parsePrefixCommon(const std::string &prefix)
Converts subnet prefix to a pair of prefix/length pair.
Definition: subnet.cc:254
std::string getSharedNetworkName() const
Returns shared network name.
Definition: subnet.h:281
PoolCollection pools_
collection of IPv4 or non-temporary IPv6 pools in that subnet.
Definition: subnet.h:416
PoolCollection & getPoolsWritable(Lease::Type type)
Returns all pools (non-const variant).
Definition: subnet.cc:375
isc::asiolink::IOAddress getLastAllocated(Lease::Type type) const
returns the last address that was tried from this subnet.
Definition: subnet.cc:90
void setLastAllocated(Lease::Type type, const isc::asiolink::IOAddress &addr)
sets the last address that was tried from this subnet.
Definition: subnet.cc:138
const PoolPtr getPool(Lease::Type type, const isc::asiolink::IOAddress &addr, bool anypool=true) const
Returns a pool that specified address belongs to.
Definition: subnet.cc:393
isc::asiolink::IOAddress last_allocated_ia_
last allocated address.
Definition: subnet.h:439
Subnet(const isc::asiolink::IOAddress &prefix, uint8_t len, const SubnetID id)
Protected constructor.
Definition: subnet.cc:59
boost::posix_time::ptime getLastAllocatedTime(const Lease::Type &lease_type) const
Returns the timestamp when the setLastAllocated function was called.
Definition: subnet.cc:117
uint64_t sumPoolCapacity(const PoolCollection &pools) const
Returns a sum of possible leases in all pools.
Definition: subnet.cc:214
std::string shared_network_name_
Shared network name.
Definition: subnet.h:458
bool poolOverlaps(const Lease::Type &pool_type, const PoolPtr &pool) const
Checks if the specified pool overlaps with an existing pool.
Definition: subnet.cc:566
virtual isc::asiolink::IOAddress default_pool() const =0
Returns the default address that will be used for pool selection.
PoolCollection pools_pd_
collection of IPv6 prefix pools in that subnet.
Definition: subnet.h:422
const PoolCollection & getPools(Lease::Type type) const
Returns all pools (const variant).
Definition: subnet.cc:357
static SubnetID static_id_
keeps the subnet-id value.
Definition: subnet.h:332
virtual void checkType(Lease::Type type) const =0
Checks if used pool type is valid.
uint64_t getPoolCapacity(Lease::Type type) const
Returns the number of possible leases for specified lease type.
Definition: subnet.cc:181
bool inPool(Lease::Type type, const isc::asiolink::IOAddress &addr) const
checks if the specified address is in pools.
Definition: subnet.cc:521
void setSharedNetwork(const NetworkPtr &shared_network)
Assigns shared network to a subnet.
Definition: subnet.h:274
isc::asiolink::IOAddress last_allocated_ta_
last allocated temporary address.
Definition: subnet.h:444
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
boost::shared_ptr< Subnet > SubnetPtr
A generic pointer to either Subnet4 or Subnet6 object.
Definition: subnet.h:513
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:522
boost::shared_ptr< const Subnet6 > ConstSubnet6Ptr
A const pointer to a Subnet6 object.
Definition: subnet.h:664
boost::shared_ptr< const Subnet4 > ConstSubnet4Ptr
A const pointer to a Subnet4 object.
Definition: subnet.h:516
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition: subnet.h:670
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 > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > > > Subnet6Collection
A collection of Subnet6 objects.
Definition: subnet.h:914
std::vector< PoolPtr > PoolCollection
a container for either IPv4 or IPv6 Pools
Definition: pool.h:508
boost::shared_ptr< Pool > PoolPtr
a pointer to either IPv4 or IPv6 Pool
Definition: pool.h:505
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 > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetServerIdIndexTag >, boost::multi_index::const_mem_fun< Network4, asiolink::IOAddress, &Network4::getServerId > >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< SubnetModificationTimeIndexTag >, boost::multi_index::const_mem_fun< data::BaseStampedElement, boost::posix_time::ptime, &data::BaseStampedElement::getModificationTime > > > > Subnet4Collection
A collection of Subnet4 objects.
Definition: subnet.h:867
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
Definition: lease.h:24
boost::shared_ptr< Network > NetworkPtr
Pointer to the Network object.
Definition: network.h:40
Defines the logger used by the top-level component of kea-lfc.
This structure contains information about DHCP4o6 (RFC7341)
Definition: cfg_4o6.h:22
Type
Type of lease or pool.
Definition: lease.h:50
Tag for the index for searching by subnet modification time.
Definition: subnet.h:811
Tag for the index for searching by subnet prefix.
Definition: subnet.h:805
Tag for the index for searching by server identifier.
Definition: subnet.h:808
Tag for the index for searching by subnet identifier.
Definition: subnet.h:802