cfg_subnets4.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef CFG_SUBNETS4_H
  15. #define CFG_SUBNETS4_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcpsrv/subnet.h>
  18. #include <dhcpsrv/subnet_selector.h>
  19. #include <boost/shared_ptr.hpp>
  20. namespace isc {
  21. namespace dhcp {
  22. /// @brief Holds subnets configured for the DHCPv4 server.
  23. ///
  24. /// This class holds a collection of subnets configured for the DHCPv4 server.
  25. /// It allows for retrieving a subnet for the particular client using various
  26. /// parameters extracted from the DHCPv4 message. These parameters must be
  27. /// assigned to the appropriate members of the @c CfgSubnets4::Selector
  28. /// structure.
  29. ///
  30. /// See @c CfgSubnets4::selectSubnet documentation for more details on how the
  31. /// subnet is selected for the client.
  32. class CfgSubnets4 {
  33. public:
  34. /// @brief Adds new subnet to the configuration.
  35. ///
  36. /// @param subnet Pointer to the subnet being added.
  37. ///
  38. /// @throw isc::DuplicateSubnetID If the subnet id for the new subnet
  39. /// duplicates id of an existing subnet.
  40. void add(const Subnet4Ptr& subnet);
  41. /// @brief Returns pointer to the collection of all IPv4 subnets.
  42. ///
  43. /// This is used in a hook (subnet4_select), where the hook is able
  44. /// to choose a different subnet. Server code has to offer a list
  45. /// of possible choices (i.e. all subnets).
  46. ///
  47. /// @return A pointer to const Subnet4 collection
  48. const Subnet4Collection* getAll() const {
  49. return (&subnets_);
  50. }
  51. /// @brief Returns pointer to the selected subnet.
  52. ///
  53. /// This method tries to retrieve the subnet for the client using various
  54. /// parameters extracted from the client's message using the following
  55. /// logic.
  56. ///
  57. /// First when link select suboption of relay agent information option
  58. /// or subnet select option in this order exists the address is used
  59. ///
  60. /// If the giaddr value is set in the selector it means that the client's
  61. /// message was relayed. The subnet configuration allows for setting the
  62. /// relay address for each subnet to indicate that the subnet must be
  63. /// assigned when the packet was transmitted over the particular relay.
  64. /// This method first tries to match the giaddr with the relay addresses
  65. /// specified for all subnets. If the relay address for the subnet is equal
  66. /// to the address of the relay through which the message was transmitted,
  67. /// the particular subnet is returned.
  68. ///
  69. /// If the giaddr is not matched with any of the relay addresses in any
  70. /// subnet or the message was not relayed, the method will need to try to
  71. /// match one of the addresses in the client's message with the prefixes
  72. /// of the existing subnets. Depending whether it is a relayed message,
  73. /// message from the renewing client or a new allocation, the server will
  74. /// pick one of the following addresses for this matching:
  75. /// - giaddr - for relayed message
  76. /// - ciaddr - for renewing or rebinding client
  77. /// - source address - for the renewing client which didn't provide ciaddr
  78. /// - address on the local server's interface if this is a new allocation
  79. /// requested by the directly connected client
  80. ///
  81. /// If the address matches with a subnet, the subnet is returned.
  82. ///
  83. /// @todo This method requires performance improvement! It currently
  84. /// iterates over all existing subnets (possibly a couple of times)
  85. /// to find the one which fulfils the search criteria. The subnet storage
  86. /// is implemented as a simple STL vector which precludes fast searches
  87. /// using specific keys. Hence, full scan is required. To improve the
  88. /// search performance a different container type is required, e.g.
  89. /// multi-index container, or something of a similar functionality.
  90. ///
  91. /// @param selector Const reference to the selector structure which holds
  92. /// various information extracted from the client's packet which are used
  93. /// to find appropriate subnet.
  94. ///
  95. /// @return Pointer to the selected subnet or NULL if no subnet found.
  96. /// @throw isc::BadValue if the values in the subnet selector are invalid
  97. /// or they are insufficient to select a subnet.
  98. Subnet4Ptr selectSubnet(const SubnetSelector& selector) const;
  99. /// @brief Returns pointer to a subnet if provided address is in its range.
  100. ///
  101. /// This method returns a pointer to the subnet if the address passed in
  102. /// parameter is in range with this subnet. This is mainly used for unit
  103. /// testing. This method is also called by the
  104. /// @c selectSubnet(SubnetSelector).
  105. ///
  106. /// @todo This method requires performance improvement! It currently
  107. /// iterates over all existing subnets to find the one which fulfils
  108. /// the search criteria. The subnet storage is implemented as a simple
  109. /// STL vector which precludes fast searches using specific keys.
  110. /// Hence, full scan is required. To improve the search performance a
  111. /// different container type is required, e.g. multi-index container,
  112. /// or something of a similar functionality.
  113. ///
  114. /// @param address Address for which the subnet is searched.
  115. /// @param client_classes Optional parameter specifying the classes that
  116. /// the client belongs to.
  117. ///
  118. /// @return Pointer to the selected subnet or NULL if no subnet found.
  119. Subnet4Ptr selectSubnet(const asiolink::IOAddress& address,
  120. const ClientClasses& client_classes
  121. = ClientClasses()) const;
  122. /// @brief Updates statistics.
  123. ///
  124. /// This method updates statistics that are affected by the newly committed
  125. /// configuration. In particular, it updates the number of available addresses
  126. /// in each subnet. Other statistics may be added in the future. In general,
  127. /// these are statistics that are dependant only on configuration, so they are
  128. /// not expected to change until the next reconfiguration event.
  129. void updateStatistics();
  130. /// @brief Removes statistics.
  131. ///
  132. /// During commitment of a new configuration, we need to get rid of the old
  133. /// statistics for the old configuration. In particular, we need to remove
  134. /// anything related to subnets, as there may be fewer subnets in the new
  135. /// configuration and also subnet-ids may change.
  136. void removeStatistics();
  137. private:
  138. /// @brief Checks that the IPv4 subnet with the given id already exists.
  139. ///
  140. /// @param subnet Subnet for which this function will check if the other
  141. /// subnet with equal id already exists.
  142. ///
  143. /// @return true if the duplicate subnet exists.
  144. bool isDuplicate(const Subnet4& subnet) const;
  145. /// @brief A container for IPv4 subnets.
  146. Subnet4Collection subnets_;
  147. };
  148. /// @name Pointer to the @c CfgSubnets4 objects.
  149. //@{
  150. /// @brief Non-const pointer.
  151. typedef boost::shared_ptr<CfgSubnets4> CfgSubnets4Ptr;
  152. /// @brief Const pointer.
  153. typedef boost::shared_ptr<const CfgSubnets4> ConstCfgSubnets4Ptr;
  154. //@}
  155. }
  156. }
  157. #endif // CFG_SUBNETS4_H