io_address.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright (C) 2010 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 IO_ADDRESS_H
  15. #define IO_ADDRESS_H 1
  16. // IMPORTANT NOTE: only very few ASIO headers files can be included in
  17. // this file. In particular, asio.hpp should never be included here.
  18. // See the description of the namespace below.
  19. #include <unistd.h> // for some network system calls
  20. #include <stdint.h> // for uint32_t
  21. #include <asio/ip/address.hpp>
  22. #include <functional>
  23. #include <string>
  24. #include <vector>
  25. #include <exceptions/exceptions.h>
  26. namespace isc {
  27. namespace asiolink {
  28. /// Defines length of IPv6 address.
  29. const static size_t V6ADDRESS_LEN = 16;
  30. /// Defines length of IPv4 address.
  31. const static size_t V4ADDRESS_LEN = 4;
  32. /// \brief The \c IOAddress class represents an IP addresses (version
  33. /// agnostic)
  34. ///
  35. /// This class is a wrapper for the ASIO \c ip::address class.
  36. class IOAddress {
  37. public:
  38. ///
  39. /// \name Constructors and Destructor
  40. ///
  41. /// This class is copyable. We use default versions of copy constructor
  42. /// and the assignment operator.
  43. /// We use the default destructor.
  44. //@{
  45. /// \brief Constructor from string.
  46. ///
  47. /// This constructor converts a textual representation of IPv4 and IPv6
  48. /// addresses into an IOAddress object.
  49. /// If \c address_str is not a valid representation of any type of
  50. /// address, an exception of class \c IOError will be thrown.
  51. /// This constructor allocates memory for the object, and if that fails
  52. /// a corresponding standard exception will be thrown.
  53. ///
  54. /// \param address_str Textual representation of address.
  55. IOAddress(const std::string& address_str);
  56. /// \brief Constructor from an ASIO \c ip::address object.
  57. ///
  58. /// This constructor is intended to be used within the wrapper
  59. /// implementation; user applications of the wrapper API won't use it.
  60. ///
  61. /// This constructor never throws an exception.
  62. ///
  63. /// \param asio_address The ASIO \c ip::address to be converted.
  64. IOAddress(const asio::ip::address& asio_address);
  65. //@}
  66. /// @brief Constructor for ip::address_v4 object.
  67. ///
  68. /// This constructor is intented to be used when constructing
  69. /// IPv4 address out of uint32_t type. Passed value must be in
  70. /// network byte order
  71. ///
  72. /// @param v4address IPv4 address represnted by uint32_t
  73. IOAddress(uint32_t v4address);
  74. /// \brief Convert the address to a string.
  75. ///
  76. /// This method is basically expected to be exception free, but
  77. /// generating the string will involve resource allocation,
  78. /// and if it fails the corresponding standard exception will be thrown.
  79. ///
  80. /// \return A string representation of the address.
  81. std::string toText() const;
  82. /// \brief Returns const reference to the underlying address object.
  83. ///
  84. /// This is useful, when access to interface offerted by
  85. // asio::ip::address_v4 and asio::ip::address_v6 is beneficial.
  86. ///
  87. /// \return A const reference to asio::ip::address object
  88. const asio::ip::address& getAddress() const;
  89. /// \brief Returns the address family
  90. ///
  91. /// \return AF_INET for IPv4 or AF_INET6 for IPv6.
  92. short getFamily() const;
  93. /// \brief Convenience function to check for an IPv4 address
  94. ///
  95. /// \return true if the address is a V4 address
  96. bool isV4() const {
  97. return (asio_address_.is_v4());
  98. }
  99. /// \brief Convenience function to check for an IPv6 address
  100. ///
  101. /// \return true if the address is a V6 address
  102. bool isV6() const {
  103. return (asio_address_.is_v6());
  104. }
  105. /// \brief Creates an address from over wire data.
  106. ///
  107. /// \param family AF_NET for IPv4 or AF_NET6 for IPv6.
  108. /// \param data pointer to first char of data
  109. ///
  110. /// \return Created IOAddress object
  111. static IOAddress fromBytes(short family, const uint8_t* data);
  112. /// \brief Return address as set of bytes
  113. ///
  114. /// \return Contents of the address as a set of bytes in network-byte
  115. /// order.
  116. std::vector<uint8_t> toBytes() const;
  117. /// \brief Compare addresses for equality
  118. ///
  119. /// \param other Address to compare against.
  120. ///
  121. /// \return true if addresses are equal, false if not.
  122. bool equals(const IOAddress& other) const {
  123. return (asio_address_ == other.asio_address_);
  124. }
  125. /// \brief Compare addresses for equality
  126. ///
  127. /// \param other Address to compare against.
  128. ///
  129. /// \return true if addresses are equal, false if not.
  130. bool operator==(const IOAddress& other) const {
  131. return equals(other);
  132. }
  133. /// \brief Compare addresses for inequality
  134. ///
  135. /// \param other Address to compare against.
  136. ///
  137. /// \return false if addresses are equal, true if not.
  138. bool nequals(const IOAddress& other) const {
  139. return (!equals(other));
  140. }
  141. /// \brief Checks if one address is smaller than the other
  142. ///
  143. /// \param other Address to compare against.
  144. ///
  145. /// \return true if this address is smaller than the other address.
  146. ///
  147. /// It is useful for comparing which address is bigger.
  148. /// Operations within one protocol family are obvious.
  149. /// Comparisons between v4 and v6 will allways return v4
  150. /// being smaller. This follows boost::asio::ip implementation
  151. bool lessThan(const IOAddress& other) const {
  152. if (this->getFamily() == other.getFamily()) {
  153. if (this->getFamily() == AF_INET6) {
  154. return (this->asio_address_.to_v6() < other.asio_address_.to_v6());
  155. } else {
  156. return (this->asio_address_.to_v4() < other.asio_address_.to_v4());
  157. }
  158. }
  159. return (this->getFamily() < other.getFamily());
  160. }
  161. /// \brief Checks if one address is smaller or equal than the other
  162. ///
  163. /// \param other Address to compare against.
  164. ///
  165. /// \return true if this address is smaller than the other address.
  166. bool smallerEqual(const IOAddress& other) const {
  167. if (equals(other)) {
  168. return (true);
  169. }
  170. return (lessThan(other));
  171. }
  172. /// \brief Checks if one address is smaller than the other
  173. ///
  174. /// \param other Address to compare against.
  175. ///
  176. /// See \ref smaller_than method for details.
  177. bool operator<(const IOAddress& other) const {
  178. return (lessThan(other));
  179. }
  180. /// \brief Checks if one address is smaller or equal than the other
  181. ///
  182. /// \param other Address to compare against.
  183. ///
  184. /// See \ref smaller_equal method for details.
  185. bool operator<=(const IOAddress& other) const {
  186. return (smallerEqual(other));
  187. }
  188. /// \brief Compare addresses for inequality
  189. ///
  190. /// \param other Address to compare against.
  191. ///
  192. /// \return false if addresses are equal, true if not.
  193. bool operator!=(const IOAddress& other) const {
  194. return (nequals(other));
  195. }
  196. /// \brief Converts IPv4 address to uint32_t
  197. ///
  198. /// Will throw BadValue exception if that is not IPv4
  199. /// address.
  200. ///
  201. /// \return uint32_t that represents IPv4 address in
  202. /// network byte order
  203. operator uint32_t () const;
  204. private:
  205. asio::ip::address asio_address_;
  206. };
  207. } // namespace asiolink
  208. } // namespace isc
  209. #endif // IO_ADDRESS_H