cfg_subnets4.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2014 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 <exceptions/exceptions.h>
  18. #include <dhcpsrv/subnet.h>
  19. #include <util/optional_value.h>
  20. namespace isc {
  21. namespace dhcp {
  22. /// @brief Exception thrown upon attempt to add subnet with an ID that belongs
  23. /// to the subnet that already exists.
  24. class DuplicateSubnet4ID : public Exception {
  25. public:
  26. DuplicateSubnet4ID(const char* file, size_t line, const char* what) :
  27. isc::Exception(file, line, what) { };
  28. };
  29. /// @brief Holds subnets configured for the DHCPv4 server.
  30. ///
  31. /// This class holds a collection of subnets configured for the DHCPv4 server.
  32. /// It allows for retrieving a subnet for the particular client using various
  33. /// parameters extracted from the DHCPv4 message. These parameters must be
  34. /// assigned to the appropriate members of the @c CfgSubnets4::Selector
  35. /// structure.
  36. ///
  37. /// See @c CfgSubnets4::get documentation for more details on how the subnet
  38. /// is selected for the client.
  39. class CfgSubnets4 {
  40. public:
  41. /// @brief Subnet selector used in @c CfgSubnets4::getSubnet4.
  42. ///
  43. /// This structure holds various parameters extracted from a packet sent
  44. /// by a DHCP client used to select the subnet for the client. Note that
  45. /// data members are optional which means that they may be left unspecified
  46. /// by the caller, if the caller doesn't have access to the relevant
  47. /// information.
  48. struct Selector {
  49. /// @brief ciaddr from the client's message.
  50. util::OptionalValue<asiolink::IOAddress> ciaddr_;
  51. /// @brief giaddr from the client's message.
  52. util::OptionalValue<asiolink::IOAddress> giaddr_;
  53. /// @brief Address on which the message was received.
  54. util::OptionalValue<asiolink::IOAddress> local_address_;
  55. /// @brief Source address of the message.
  56. util::OptionalValue<asiolink::IOAddress> remote_address_;
  57. /// @brief Classes that the client belongs to.
  58. util::OptionalValue<ClientClasses> client_classes_;
  59. /// @brief Name of the interface on which the message was received.
  60. util::OptionalValue<std::string> iface_name_;
  61. /// @brief Default constructor.
  62. ///
  63. /// Sets the default values for the @c Selector.
  64. Selector();
  65. };
  66. /// @brief Adds new subnet to the configuration.
  67. ///
  68. /// @param subnet Pointer to the subnet being added.
  69. ///
  70. /// @throw isc::DuplicateSubnet4ID If the subnet id for the new subnet
  71. /// duplicates id of an existing subnet.
  72. void add(const Subnet4Ptr& subnet);
  73. /// @brief Returns pointer to the collection of all IPv4 subnets.
  74. ///
  75. /// This is used in a hook (subnet4_select), where the hook is able
  76. /// to choose a different subnet. Server code has to offer a list
  77. /// of possible choices (i.e. all subnets).
  78. ///
  79. /// @return A pointer to const Subnet4 collection
  80. const Subnet4Collection* getAll() const {
  81. return (&subnets_);
  82. }
  83. /// @brief Returns pointer to the selected subnet.
  84. ///
  85. /// This method tries to retrieve the subnet for the client using various
  86. /// parameters extracted from the client's message using the following
  87. /// logic.
  88. ///
  89. /// If the giaddr value is found it means that the client's message was
  90. /// relayed. The subnet configuration allows for setting the relay address
  91. /// for each subnet to indicate that the subnet must be assigned when the
  92. /// packet was transmitted over the particular relay. This method first
  93. /// tries to match the giaddr with the relay addresses specified for
  94. /// all subnets. If the relay address for the subnet is equal to the address
  95. /// of the relay through which the message was transmitted, the particular
  96. /// subnet is returned.
  97. ///
  98. /// If the giaddr is not matched with any of the relay addresses in any
  99. /// subnet or the message was not relayed, the method will need to try to
  100. /// match one of the addresses in the client's message with the prefixes
  101. /// of the existing subnets. Depending whether it is a relayed message,
  102. /// message from the renewing client or a new allocation, the server will
  103. /// pick one of the following addresses for this matching:
  104. /// - giaddr - for relayed message
  105. /// - ciaddr - for renewing or rebinding client
  106. /// - source address - for the renewing client which didn't provide ciaddr
  107. /// - address on the local server's interface if this is a new allocation
  108. /// requested by the directly connected client
  109. ///
  110. /// If the address matches with a subnet, the subnet is returned.
  111. ///
  112. /// @param selector Const reference to the selector structure which holds
  113. /// various information extracted from the client's packet which are used
  114. /// to find appropriate subnet.
  115. ///
  116. /// @return Pointer to the selected subnet or NULL if no subnet found.
  117. /// @throw isc::BadValue if the values in the subnet selector are invalid
  118. /// or they are insufficient to select a subnet.
  119. Subnet4Ptr get(const Selector& selector) const;
  120. private:
  121. /// @brief Checks that the IPv4 subnet with the given id already exists.
  122. ///
  123. /// @param subnet Subnet for which this function will check if the other
  124. /// subnet with equal id already exists.
  125. ///
  126. /// @return true if the duplicate subnet exists.
  127. bool isDuplicate(const Subnet4& subnet) const;
  128. /// @brief A container for IPv4 subnets.
  129. Subnet4Collection subnets_;
  130. };
  131. }
  132. }
  133. #endif // CFG_SUBNETS4_H