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