io_address.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <asio/ip/address.hpp>
  21. #include <functional>
  22. #include <string>
  23. #include <exceptions/exceptions.h>
  24. namespace isc {
  25. namespace asiolink {
  26. /// \brief The \c IOAddress class represents an IP addresses (version
  27. /// agnostic)
  28. ///
  29. /// This class is a wrapper for the ASIO \c ip::address class.
  30. class IOAddress {
  31. public:
  32. ///
  33. /// \name Constructors and Destructor
  34. ///
  35. /// This class is copyable. We use default versions of copy constructor
  36. /// and the assignment operator.
  37. /// We use the default destructor.
  38. //@{
  39. /// \brief Constructor from string.
  40. ///
  41. /// This constructor converts a textual representation of IPv4 and IPv6
  42. /// addresses into an IOAddress object.
  43. /// If \c address_str is not a valid representation of any type of
  44. /// address, an exception of class \c IOError will be thrown.
  45. /// This constructor allocates memory for the object, and if that fails
  46. /// a corresponding standard exception will be thrown.
  47. ///
  48. /// \param address_str Textual representation of address.
  49. IOAddress(const std::string& address_str);
  50. /// \brief Constructor from an ASIO \c ip::address object.
  51. ///
  52. /// This constructor is intended to be used within the wrapper
  53. /// implementation; user applications of the wrapper API won't use it.
  54. ///
  55. /// This constructor never throws an exception.
  56. ///
  57. /// \param asio_address The ASIO \c ip::address to be converted.
  58. IOAddress(const asio::ip::address& asio_address);
  59. //@}
  60. /// \brief Convert the address to a string.
  61. ///
  62. /// This method is basically expected to be exception free, but
  63. /// generating the string will involve resource allocation,
  64. /// and if it fails the corresponding standard exception will be thrown.
  65. ///
  66. /// \return A string representation of the address.
  67. std::string toText() const;
  68. /// \brief Returns the address family
  69. ///
  70. /// \return AF_INET for IPv4 or AF_INET6 for IPv6.
  71. short getFamily() const;
  72. /// \brief Compare addresses for equality
  73. ///
  74. /// \param other Address to compare against.
  75. ///
  76. /// \return true if addresses are equal, false if not.
  77. bool equals(const IOAddress& other) const {
  78. return (asio_address_ == other.asio_address_);
  79. }
  80. /// \brief Compare addresses for equality
  81. ///
  82. /// \param other Address to compare against.
  83. ///
  84. /// \return true if addresses are equal, false if not.
  85. bool operator==(const IOAddress& other) const {
  86. return equals(other);
  87. }
  88. // \brief Compare addresses for inequality
  89. ///
  90. /// \param other Address to compare against.
  91. ///
  92. /// \return false if addresses are equal, true if not.
  93. bool nequals(const IOAddress& other) const {
  94. return (!equals(other));
  95. }
  96. // \brief Compare addresses for inequality
  97. ///
  98. /// \param other Address to compare against.
  99. ///
  100. /// \return false if addresses are equal, true if not.
  101. bool operator!=(const IOAddress& other) const {
  102. return (nequals(other));
  103. }
  104. private:
  105. asio::ip::address asio_address_;
  106. };
  107. } // namespace asiolink
  108. } // namespace isc
  109. #endif // __IO_ADDRESS_H