io_address.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <exceptions/exceptions.h>
  25. namespace isc {
  26. namespace asiolink {
  27. /// Defines length of IPv6 address.
  28. const static size_t V6ADDRESS_LEN = 16;
  29. /// Defines length of IPv4 address.
  30. const static size_t V4ADDRESS_LEN = 4;
  31. /// \brief The \c IOAddress class represents an IP addresses (version
  32. /// agnostic)
  33. ///
  34. /// This class is a wrapper for the ASIO \c ip::address class.
  35. class IOAddress {
  36. public:
  37. ///
  38. /// \name Constructors and Destructor
  39. ///
  40. /// This class is copyable. We use default versions of copy constructor
  41. /// and the assignment operator.
  42. /// We use the default destructor.
  43. //@{
  44. /// \brief Constructor from string.
  45. ///
  46. /// This constructor converts a textual representation of IPv4 and IPv6
  47. /// addresses into an IOAddress object.
  48. /// If \c address_str is not a valid representation of any type of
  49. /// address, an exception of class \c IOError will be thrown.
  50. /// This constructor allocates memory for the object, and if that fails
  51. /// a corresponding standard exception will be thrown.
  52. ///
  53. /// \param address_str Textual representation of address.
  54. IOAddress(const std::string& address_str);
  55. /// \brief Constructor from an ASIO \c ip::address object.
  56. ///
  57. /// This constructor is intended to be used within the wrapper
  58. /// implementation; user applications of the wrapper API won't use it.
  59. ///
  60. /// This constructor never throws an exception.
  61. ///
  62. /// \param asio_address The ASIO \c ip::address to be converted.
  63. IOAddress(const asio::ip::address& asio_address);
  64. //@}
  65. /// @brief Constructor for ip::address_v4 object.
  66. ///
  67. /// This constructor is intented to be used when constructing
  68. /// IPv4 address out of uint32_t type. Passed value must be in
  69. /// network byte order
  70. ///
  71. /// @param v4address IPv4 address represnted by uint32_t
  72. IOAddress(uint32_t v4address);
  73. /// \brief Convert the address to a string.
  74. ///
  75. /// This method is basically expected to be exception free, but
  76. /// generating the string will involve resource allocation,
  77. /// and if it fails the corresponding standard exception will be thrown.
  78. ///
  79. /// \return A string representation of the address.
  80. std::string toText() const;
  81. /// \brief Returns const reference to the underlying address object.
  82. ///
  83. /// This is useful, when access to interface offerted by
  84. // asio::ip::address_v4 and asio::ip::address_v6 is beneficial.
  85. ///
  86. /// \return A const reference to asio::ip::address object
  87. const asio::ip::address& getAddress() const;
  88. /// \brief Returns the address family
  89. ///
  90. /// \return AF_INET for IPv4 or AF_INET6 for IPv6.
  91. short getFamily() const;
  92. /// \brief Creates an address from over wire data.
  93. ///
  94. /// \param family AF_NET for IPv4 or AF_NET6 for IPv6.
  95. /// \param data pointer to first char of data
  96. ///
  97. /// \return Created IOAddress object
  98. static IOAddress
  99. from_bytes(short family, const uint8_t* data);
  100. /// \brief Compare addresses for equality
  101. ///
  102. /// \param other Address to compare against.
  103. ///
  104. /// \return true if addresses are equal, false if not.
  105. bool equals(const IOAddress& other) const {
  106. return (asio_address_ == other.asio_address_);
  107. }
  108. /// \brief Compare addresses for equality
  109. ///
  110. /// \param other Address to compare against.
  111. ///
  112. /// \return true if addresses are equal, false if not.
  113. bool operator==(const IOAddress& other) const {
  114. return equals(other);
  115. }
  116. // \brief Compare addresses for inequality
  117. ///
  118. /// \param other Address to compare against.
  119. ///
  120. /// \return false if addresses are equal, true if not.
  121. bool nequals(const IOAddress& other) const {
  122. return (!equals(other));
  123. }
  124. // \brief Compare addresses for inequality
  125. ///
  126. /// \param other Address to compare against.
  127. ///
  128. /// \return false if addresses are equal, true if not.
  129. bool operator!=(const IOAddress& other) const {
  130. return (nequals(other));
  131. }
  132. /// \brief Converts IPv4 address to uint32_t
  133. ///
  134. /// Will throw BadValue exception if that is not IPv4
  135. /// address.
  136. ///
  137. /// \return uint32_t that represents IPv4 address in
  138. /// network byte order
  139. operator uint32_t () const;
  140. private:
  141. asio::ip::address asio_address_;
  142. };
  143. } // namespace asiolink
  144. } // namespace isc
  145. #endif // __IO_ADDRESS_H