Kea 1.5.0
subnet.h
Go to the documentation of this file.
1// Copyright (C) 2012-2018 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>
15#include <dhcpsrv/lease.h>
16#include <dhcpsrv/pool.h>
17#include <dhcpsrv/subnet_id.h>
18#include <dhcpsrv/triplet.h>
19
20#include <boost/multi_index/mem_fun.hpp>
21#include <boost/multi_index/indexed_by.hpp>
22#include <boost/multi_index/ordered_index.hpp>
23#include <boost/multi_index/random_access_index.hpp>
24#include <boost/multi_index_container.hpp>
25#include <boost/date_time/posix_time/posix_time.hpp>
26#include <boost/pointer_cast.hpp>
27#include <boost/shared_ptr.hpp>
28#include <cstdint>
29#include <map>
30#include <utility>
31
32namespace isc {
33namespace dhcp {
34
35class Subnet : public virtual data::UserContext, public data::CfgToElement {
36
37 // Assignable network is our friend to allow it to call
38 // @ref Subnet::setSharedNetwork private function.
39 friend class AssignableNetwork;
40
41public:
42
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
87
97 boost::posix_time::ptime getLastAllocatedTime(const Lease::Type& lease_type) const;
98
110 const isc::asiolink::IOAddress& addr);
111
114 SubnetID getID() const { return (id_); }
115
119 std::pair<isc::asiolink::IOAddress, uint8_t> get() const {
120 return (std::make_pair(prefix_, prefix_len_));
121 }
122
145 void addPool(const PoolPtr& pool);
146
151 void delPools(Lease::Type type);
152
171 const PoolPtr getPool(Lease::Type type, const isc::asiolink::IOAddress& addr,
172 bool anypool = true) const;
173
181 const PoolPtr getPool(Lease::Type type,
182 const ClientClasses& client_classes,
183 const isc::asiolink::IOAddress& addr) const;
184
190 return (getPool(type, default_pool()));
191 }
192
198
205 const PoolCollection& getPools(Lease::Type type) const;
206
210 uint64_t getPoolCapacity(Lease::Type type) const;
211
217 uint64_t getPoolCapacity(Lease::Type type,
218 const ClientClasses& client_classes) const;
219
224 virtual std::string toText() const;
225
231 static void resetSubnetID() {
232 static_id_ = 1;
233 }
234
251 template<typename SharedNetworkPtrType>
252 void getSharedNetwork(SharedNetworkPtrType& shared_network) const {
253 shared_network = boost::dynamic_pointer_cast<
254 typename SharedNetworkPtrType::element_type>(shared_network_.lock());
255 }
256
257private:
258
266 void setSharedNetwork(const NetworkPtr& shared_network) {
267 shared_network_ = shared_network;
268 }
269
270public:
271
273 std::string getSharedNetworkName() const {
274 return (shared_network_name_);
275 }
276
289 void setSharedNetworkName(const std::string& shared_network_name) {
290 shared_network_name_ = shared_network_name;
291 }
292
293protected:
301
303 //
316 Subnet(const isc::asiolink::IOAddress& prefix, uint8_t len,
317 const SubnetID id);
318
323 virtual ~Subnet() { };
324
333
343 if (static_id_ == SUBNET_ID_MAX) {
345 }
346
347 return (static_id_++);
348 }
349
358 virtual void checkType(Lease::Type type) const = 0;
359
363 uint64_t sumPoolCapacity(const PoolCollection& pools) const;
364
369 uint64_t sumPoolCapacity(const PoolCollection& pools,
370 const ClientClasses& client_classes) const;
371
380 bool poolOverlaps(const Lease::Type& pool_type, const PoolPtr& pool) const;
381
385 virtual data::ElementPtr toElement() const;
386
395 static std::pair<asiolink::IOAddress, uint8_t>
396 parsePrefixCommon(const std::string& prefix);
397
403
406
409
412
415
417 uint8_t prefix_len_;
418
429
434
439
442 std::map<Lease::Type, boost::posix_time::ptime> last_allocated_time_;
443
445 std::string iface_;
446
449
452};
453
455typedef boost::shared_ptr<Subnet> SubnetPtr;
456
457
458class Subnet4;
459
461typedef boost::shared_ptr<const Subnet4> ConstSubnet4Ptr;
462
464typedef boost::shared_ptr<Subnet4> Subnet4Ptr;
465
471class Subnet4 : public Subnet, public Network4 {
472public:
473
485 Subnet4(const isc::asiolink::IOAddress& prefix, uint8_t length,
486 const Triplet<uint32_t>& t1,
487 const Triplet<uint32_t>& t2,
488 const Triplet<uint32_t>& valid_lifetime,
489 const SubnetID id = 0);
490
503 Subnet4Ptr getNextSubnet(const Subnet4Ptr& first_subnet) const;
504
517 Subnet4Ptr getNextSubnet(const Subnet4Ptr& first_subnet,
518 const ClientClasses& client_classes) const;
519
531 virtual bool
532 clientSupported(const isc::dhcp::ClientClasses& client_classes) const;
533
539 void setSiaddr(const isc::asiolink::IOAddress& siaddr);
540
545
549 void setSname(const std::string& sname);
550
554 const std::string& getSname() const;
555
559 void setFilename(const std::string& filename);
560
564 const std::string& getFilename() const;
565
571 return (dhcp4o6_);
572 }
573
578 const Cfg4o6& get4o6() const {
579 return (dhcp4o6_);
580 }
581
585 virtual data::ElementPtr toElement() const;
586
591 static std::pair<asiolink::IOAddress, uint8_t>
592 parsePrefix(const std::string& prefix);
593
594private:
595
598 virtual isc::asiolink::IOAddress default_pool() const {
599 return (isc::asiolink::IOAddress("0.0.0.0"));
600 }
601
608 virtual void checkType(Lease::Type type) const;
609
612
614 std::string sname_;
615
617 std::string filename_;
618
620 Cfg4o6 dhcp4o6_;
621};
622
623class Subnet6;
624
626typedef boost::shared_ptr<const Subnet6> ConstSubnet6Ptr;
627
629typedef boost::shared_ptr<Subnet6> Subnet6Ptr;
630
636class Subnet6 : public Subnet, public Network6 {
637public:
638
651 Subnet6(const isc::asiolink::IOAddress& prefix, uint8_t length,
652 const Triplet<uint32_t>& t1,
653 const Triplet<uint32_t>& t2,
654 const Triplet<uint32_t>& preferred_lifetime,
655 const Triplet<uint32_t>& valid_lifetime,
656 const SubnetID id = 0);
657
670 Subnet6Ptr getNextSubnet(const Subnet6Ptr& first_subnet) const;
671
684 Subnet6Ptr getNextSubnet(const Subnet6Ptr& first_subnet,
685 const ClientClasses& client_classes) const;
686
698 virtual bool
699 clientSupported(const isc::dhcp::ClientClasses& client_classes) const;
700
704 virtual data::ElementPtr toElement() const;
705
710 static std::pair<asiolink::IOAddress, uint8_t>
711 parsePrefix(const std::string& prefix);
712
713private:
714
717 virtual isc::asiolink::IOAddress default_pool() const {
718 return (isc::asiolink::IOAddress("::"));
719 }
720
727 virtual void checkType(Lease::Type type) const;
728
729};
730
733
734
737
740
743
746
770typedef boost::multi_index_container<
771 // Multi index container holds pointers to the subnets.
773 // The following holds all indexes.
774 boost::multi_index::indexed_by<
775 // First is the random access index allowing for accessing
776 // objects just like we'd do with a vector.
777 boost::multi_index::random_access<
778 boost::multi_index::tag<SubnetRandomAccessIndexTag>
779 >,
780 // Second index allows for searching using subnet identifier.
781 boost::multi_index::ordered_unique<
782 boost::multi_index::tag<SubnetSubnetIdIndexTag>,
783 boost::multi_index::const_mem_fun<Subnet, SubnetID, &Subnet::getID>
784 >,
785 // Third index allows for searching using an output from toText function.
786 boost::multi_index::ordered_unique<
787 boost::multi_index::tag<SubnetPrefixIndexTag>,
788 boost::multi_index::const_mem_fun<Subnet, std::string, &Subnet::toText>
789 >,
790
791 // Fourth index allows for searching using an output from getServerId
792 boost::multi_index::ordered_non_unique<
793 boost::multi_index::tag<SubnetServerIdIndexTag>,
794 boost::multi_index::const_mem_fun<Network4, asiolink::IOAddress,
796 >
797 >
799
822typedef boost::multi_index_container<
823 // Multi index container holds pointers to the subnets.
825 // The following holds all indexes.
826 boost::multi_index::indexed_by<
827 // First is the random access index allowing for accessing
828 // objects just like we'd do with a vector.
829 boost::multi_index::random_access<
830 boost::multi_index::tag<SubnetRandomAccessIndexTag>
831 >,
832 // Second index allows for searching using subnet identifier.
833 boost::multi_index::ordered_unique<
834 boost::multi_index::tag<SubnetSubnetIdIndexTag>,
835 boost::multi_index::const_mem_fun<Subnet, SubnetID, &Subnet::getID>
836 >,
837 // Third index allows for searching using an output from toText function.
838 boost::multi_index::ordered_unique<
839 boost::multi_index::tag<SubnetPrefixIndexTag>,
840 boost::multi_index::const_mem_fun<Subnet, std::string, &Subnet::toText>
841 >
842 >
844
846
847} // end of isc::dhcp namespace
848} // end of isc namespace
849
850#endif // SUBNET_H
Represents a network that can be associated with a subnet.
Container for storing client class names.
Definition: classify.h:43
Specialization of the Network object for DHCPv4 case.
Definition: network.h:366
virtual asiolink::IOAddress getServerId() const
Returns binary representation of the dhcp-server-identifier option (54).
Definition: network.cc:205
Specialization of the Network object for DHCPv6 case.
Definition: network.h:431
A configuration holder for IPv4 subnet.
Definition: subnet.h:471
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:302
Cfg4o6 & get4o6()
Returns DHCP4o6 configuration parameters.
Definition: subnet.h:570
const std::string & getFilename() const
Returns boot file name for this subnet.
Definition: subnet.cc:335
void setSiaddr(const isc::asiolink::IOAddress &siaddr)
Sets siaddr for the Subnet4.
Definition: subnet.cc:312
Subnet4Ptr getNextSubnet(const Subnet4Ptr &first_subnet) const
Returns next subnet within shared network.
Definition: subnet.cc:263
virtual data::ElementPtr toElement() const
Unparse a subnet object.
Definition: subnet.cc:708
const std::string & getSname() const
Returns server hostname for this subnet.
Definition: subnet.cc:328
void setFilename(const std::string &filename)
Sets boot file name for the Subnet4.
Definition: subnet.cc:331
void setSname(const std::string &sname)
Sets server hostname for the Subnet4.
Definition: subnet.cc:324
const Cfg4o6 & get4o6() const
Returns const DHCP4o6 configuration parameters.
Definition: subnet.h:578
isc::asiolink::IOAddress getSiaddr() const
Returns siaddr for this subnet.
Definition: subnet.cc:320
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:742
A configuration holder for IPv6 subnet.
Definition: subnet.h:636
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:680
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:783
Subnet6Ptr getNextSubnet(const Subnet6Ptr &first_subnet) const
Returns next subnet within shared network.
Definition: subnet.cc:642
virtual data::ElementPtr toElement() const
Unparse a subnet object.
Definition: subnet.cc:752
void setSharedNetworkName(const std::string &shared_network_name)
Sets new shared network name.
Definition: subnet.h:289
isc::asiolink::IOAddress prefix_
a prefix of the subnet
Definition: subnet.h:414
SubnetID getID() const
Returns unique ID for that subnet.
Definition: subnet.h:114
uint8_t prefix_len_
a prefix length of the subnet
Definition: subnet.h:417
static void resetSubnetID()
Resets subnet-id counter to its initial value (1)
Definition: subnet.h:231
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:442
virtual data::ElementPtr toElement() const
Unparse a subnet object.
Definition: subnet.cc:691
static SubnetID generateNextID()
returns the next unique Subnet-ID
Definition: subnet.h:342
PoolCollection pools_ta_
collection of IPv6 temporary address pools in that subnet
Definition: subnet.h:408
void getSharedNetwork(SharedNetworkPtrType &shared_network) const
Retrieves pointer to a shared network associated with a subnet.
Definition: subnet.h:252
SubnetID id_
subnet-id
Definition: subnet.h:402
WeakNetworkPtr shared_network_
Pointer to a shared network that subnet belongs to.
Definition: subnet.h:448
PoolPtr getAnyPool(Lease::Type type)
Returns a pool without any address specified.
Definition: subnet.h:189
void addPool(const PoolPtr &pool)
Adds a new pool for the subnet.
Definition: subnet.cc:443
std::string iface_
Name of the network interface (if connected directly)
Definition: subnet.h:445
virtual ~Subnet()
virtual destructor
Definition: subnet.h:323
bool inRange(const isc::asiolink::IOAddress &addr) const
checks if specified address is in range
Definition: subnet.cc:76
isc::asiolink::IOAddress last_allocated_pd_
last allocated IPv6 prefix
Definition: subnet.h:438
virtual std::string toText() const
Returns textual representation of the subnet (e.g.
Definition: subnet.cc:139
void delPools(Lease::Type type)
Deletes all pools of specified type.
Definition: subnet.cc:498
std::pair< isc::asiolink::IOAddress, uint8_t > get() const
Returns subnet parameters (prefix and prefix length)
Definition: subnet.h:119
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:219
std::string getSharedNetworkName() const
Returns shared network name.
Definition: subnet.h:273
PoolCollection pools_
collection of IPv4 or non-temporary IPv6 pools in that subnet
Definition: subnet.h:405
PoolCollection & getPoolsWritable(Lease::Type type)
Returns all pools (non-const variant)
Definition: subnet.cc:357
isc::asiolink::IOAddress getLastAllocated(Lease::Type type) const
returns the last address that was tried from this subnet
Definition: subnet.cc:83
void setLastAllocated(Lease::Type type, const isc::asiolink::IOAddress &addr)
sets the last address that was tried from this subnet
Definition: subnet.cc:113
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:375
isc::asiolink::IOAddress last_allocated_ia_
last allocated address
Definition: subnet.h:428
boost::posix_time::ptime getLastAllocatedTime(const Lease::Type &lease_type) const
Returns the timestamp when the setLastAllocated function was called.
Definition: subnet.cc:101
uint64_t sumPoolCapacity(const PoolCollection &pools) const
Returns a sum of possible leases in all pools.
Definition: subnet.cc:179
std::string shared_network_name_
Shared network name.
Definition: subnet.h:451
bool poolOverlaps(const Lease::Type &pool_type, const PoolPtr &pool) const
Checks if the specified pool overlaps with an existing pool.
Definition: subnet.cc:548
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:411
const PoolCollection & getPools(Lease::Type type) const
Returns all pools (const variant)
Definition: subnet.cc:339
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:146
bool inPool(Lease::Type type, const isc::asiolink::IOAddress &addr) const
checks if the specified address is in pools
Definition: subnet.cc:503
isc::asiolink::IOAddress last_allocated_ta_
last allocated temporary address
Definition: subnet.h:433
This template specifies a parameter value.
Definition: triplet.h:36
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
boost::multi_index_container< Subnet6Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SubnetRandomAccessIndexTag > >, 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 > > > > Subnet6Collection
A collection of Subnet6 objects.
Definition: subnet.h:843
boost::shared_ptr< Subnet > SubnetPtr
A generic pointer to either Subnet4 or Subnet6 object.
Definition: subnet.h:455
boost::shared_ptr< Subnet4 > Subnet4Ptr
A pointer to a Subnet4 object.
Definition: subnet.h:464
boost::shared_ptr< const Subnet6 > ConstSubnet6Ptr
A const pointer to a Subnet6 object.
Definition: subnet.h:626
boost::shared_ptr< const Subnet4 > ConstSubnet4Ptr
A const pointer to a Subnet4 object.
Definition: subnet.h:461
boost::shared_ptr< Subnet6 > Subnet6Ptr
A pointer to a Subnet6 object.
Definition: subnet.h:629
std::vector< PoolPtr > PoolCollection
a container for either IPv4 or IPv6 Pools
Definition: pool.h:408
boost::weak_ptr< Network > WeakNetworkPtr
Weak pointer to the Network object.
Definition: network.h:363
boost::shared_ptr< Pool > PoolPtr
a pointer to either IPv4 or IPv6 Pool
Definition: pool.h:405
uint32_t SubnetID
Unique identifier for a subnet (both v4 and v6)
Definition: lease.h:24
boost::multi_index_container< Subnet4Ptr, boost::multi_index::indexed_by< boost::multi_index::random_access< boost::multi_index::tag< SubnetRandomAccessIndexTag > >, 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 > > > > Subnet4Collection
A collection of Subnet4 objects.
Definition: subnet.h:798
boost::shared_ptr< Network > NetworkPtr
Pointer to the Network object.
Definition: network.h:360
Defines the logger used by the top-level component of kea-dhcp-ddns.
Abstract class for configuration Cfg_* classes.
Base class for user context.
Definition: user_context.h:22
This structure contains information about DHCP4o6 (RFC7341)
Definition: cfg_4o6.h:21
Type
Type of lease or pool.
Definition: lease.h:38
Tag for the index for searching by subnet prefix.
Definition: subnet.h:742
Tag for the random access index.
Definition: subnet.h:736
Tag for the index for searching by server identifier.
Definition: subnet.h:745
Tag for the index for searching by subnet identifier.
Definition: subnet.h:739