cfg_iface.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 IFACE_CFG_H
  15. #define IFACE_CFG_H
  16. #include <asiolink/io_address.h>
  17. #include <map>
  18. #include <set>
  19. namespace isc {
  20. namespace dhcp {
  21. /// @brief Exception thrown when duplicated interface names specified.
  22. class DuplicateIfaceName : public Exception {
  23. public:
  24. DuplicateIfaceName(const char* file, size_t line, const char* what) :
  25. isc::Exception(file, line, what) { };
  26. };
  27. /// @brief Exception thrown when specified interface name is invalid.
  28. class InvalidIfaceName : public Exception {
  29. public:
  30. InvalidIfaceName(const char* file, size_t line, const char* what) :
  31. isc::Exception(file, line, what) { };
  32. };
  33. /// @brief Exception thrown when specified interface doesn't exist in a system.
  34. class NoSuchIface : public Exception {
  35. public:
  36. NoSuchIface(const char* file, size_t line, const char* what) :
  37. isc::Exception(file, line, what) { };
  38. };
  39. /// @brief Exception thrown when specified unicast address is not assigned
  40. /// to the interface specified.
  41. class NoSuchAddress : public Exception {
  42. public:
  43. NoSuchAddress(const char* file, size_t line, const char* what) :
  44. isc::Exception(file, line, what) { };
  45. };
  46. /// @brief Represents selection of interfaces for DHCP server.
  47. ///
  48. /// This class manages selection of interfaces on which the DHCP server is
  49. /// listening to queries. The interfaces are selected in the server
  50. /// configuration by their names or by the pairs of interface names and unicast
  51. /// addresses (e.g. eth0/2001:db8:1::1). The latter format is only accepted when
  52. /// IPv6 configuration is in use.
  53. ///
  54. /// This class also accepts "wildcard" interface name which, if specified,
  55. /// instructs the server to listen on all available interfaces.
  56. ///
  57. /// Once interfaces have been specified the sockets (either IPv4 or IPv6)
  58. /// can be opened by calling @c CfgIface::openSockets function.
  59. class CfgIface {
  60. public:
  61. /// @brief Keyword used to enable all interfaces.
  62. ///
  63. /// This keyword can be used instead of the interface name to specify
  64. /// that DHCP server should listen on all interfaces.
  65. static const char* ALL_IFACES_KEYWORD;
  66. /// @brief Protocol family: IPv4 or IPv6.
  67. ///
  68. /// Depending on the family specified, the IPv4 or IPv6 sockets are
  69. /// opened.
  70. enum Family {
  71. V4, V6
  72. };
  73. /// @brief Constructor.
  74. CfgIface();
  75. /// @brief Convenience function which closes all open sockets.
  76. void closeSockets() const;
  77. /// @brief Tries to open sockets on selected interfaces.
  78. ///
  79. /// This function opens sockets bound to link-local address as well as
  80. /// sockets bound to unicast address. See @c CfgIface::use function
  81. /// documentation for details how to specify interfaces and unicast
  82. /// addresses to bind the sockets to.
  83. ///
  84. /// @param family Address family (v4 or v6).
  85. /// @param port Port number to be used to bind sockets to.
  86. /// @param use_bcast A boolean flag which indicates if the broadcast
  87. /// traffic should be received through the socket. This parameter is
  88. /// ignored for IPv6.
  89. void openSockets(const Family& family, const uint16_t port,
  90. const bool use_bcast = true) const;
  91. /// @brief Puts the interface configuration into default state.
  92. ///
  93. /// This function removes interface names from the set.
  94. void reset();
  95. /// @brief Select interface to be used to receive DHCP traffic.
  96. ///
  97. /// This function controls the selection of the interface on which the
  98. /// DHCP queries should be received by the server. The interface name
  99. /// passed as the argument of this function may appear in one of the following
  100. /// formats:
  101. /// - interface-name, e.g. eth0
  102. /// - interface-name/unicast-address, e.g. eth0/2001:db8:1::1 (V6 only)
  103. ///
  104. /// Extraneous spaces surrounding the interface name and/or unicast address
  105. /// are accepted. For example: eth0 / 2001:db8:1::1 will be accepted.
  106. ///
  107. /// When only interface name is specified (without an address) it is allowed
  108. /// to use the "wildcard" interface name (*) which indicates that the server
  109. /// should open sockets on all interfaces. When IPv6 is in use, the sockets
  110. /// will be bound to the link local addresses. Wildcard interface names are
  111. /// not allowed when specifying a unicast address. For example:
  112. /// */2001:db8:1::1 is not allowed.
  113. ///
  114. /// @param family Address family (v4 or v6).
  115. /// @param iface_name Explicit interface name, a wildcard name (*) of
  116. /// the interface(s) or the pair of iterface/unicast-address to be used
  117. /// to receive DHCP traffic.
  118. ///
  119. /// @throw InvalidIfaceName If the interface name is incorrect, e.g. empty.
  120. /// @throw NoSuchIface If the specified interface is not present.
  121. /// @throw NoSuchAddress If the specified unicast address is not assigned
  122. /// to the interface.
  123. /// @throw DuplicateIfaceName If the interface is already selected, i.e.
  124. /// @throw IOError when specified unicast address is invalid.
  125. /// @c CfgIface::use has been already called for this interface.
  126. void use(const Family& family, const std::string& iface_name);
  127. private:
  128. /// @brief Selects or deselects interfaces.
  129. ///
  130. /// This function selects all interfaces to receive DHCP traffic or
  131. /// deselects all interfaces so as none of them receives a DHCP traffic.
  132. ///
  133. /// @param family Address family (v4 or v6).
  134. /// @param inactive A boolean value which indicates if all interfaces
  135. /// (except loopback) should be selected or deselected.
  136. /// @param loopback_inactive A boolean value which indicates if loopback
  137. /// interface should be selected or deselected.
  138. /// should be deselected/inactive (true) or selected/active (false).
  139. void setState(const Family& family, const bool inactive,
  140. const bool loopback_inactive) const;
  141. /// @brief Error handler for executed when opening a socket fail.
  142. ///
  143. /// A pointer to this function is passed to the @c IfaceMgr::openSockets4
  144. /// or @c IfaceMgr::openSockets6. These functions call this handler when
  145. /// they fail to open a socket. The handler logs an error passed in the
  146. /// parameter.
  147. ///
  148. /// @param errmsg Error message being logged by the function.
  149. static void socketOpenErrorHandler(const std::string& errmsg);
  150. /// @brief Protocol family.
  151. Family family_;
  152. /// @brief Represents a set of interface names.
  153. typedef std::set<std::string> IfaceSet;
  154. /// @brief A set of interface names specified by the user.
  155. IfaceSet iface_set_;
  156. /// @brief A map of interfaces and unicast addresses.
  157. typedef std::map<std::string, asiolink::IOAddress> UnicastMap;
  158. /// @brief A map which holds the pairs of interface names and unicast
  159. /// addresses for which the unicast sockets should be opened.
  160. ///
  161. /// This is only used for V6 family.
  162. UnicastMap unicast_map_;
  163. /// @brief A booolean value which indicates that the wildcard interface name
  164. /// has been specified (*).
  165. bool wildcard_used_;
  166. };
  167. }
  168. }
  169. #endif // IFACE_CFG_H