1 //
2 // ip/address.hpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_IP_ADDRESS_HPP
12 #define ASIO_IP_ADDRESS_HPP
13 
14 
15 #include "asio/detail/config.hpp"
16 #include <string>
17 #include "asio/error_code.hpp"
18 #include "asio/ip/address_v4.hpp"
19 #include "asio/ip/address_v6.hpp"
20 
21 
22 #include "asio/detail/push_options.hpp"
23 
24 namespace asio {
25 namespace ip {
26 
27 /// Implements version-independent IP addresses.
28 /**
29  * The asio::ip::address class provides the ability to use either IP
30  * version 4 or version 6 addresses.
31  *
32  * @par Thread Safety
33  * @e Distinct @e objects: Safe.@n
34  * @e Shared @e objects: Unsafe.
35  */
36 class address
37 {
38 public:
39   /// Default constructor.
40   ASIO_DECL address();
41 
42   /// Construct an address from an IPv4 address.
43   ASIO_DECL address(const asio::ip::address_v4& ipv4_address);
44 
45   /// Construct an address from an IPv6 address.
46   ASIO_DECL address(const asio::ip::address_v6& ipv6_address);
47 
48   /// Copy constructor.
49   ASIO_DECL address(const address& other);
50 
51   /// Move constructor.
52   ASIO_DECL address(address&& other);
53 
54   /// Assign from another address.
55   ASIO_DECL address& operator=(const address& other);
56 
57   /// Move-assign from another address.
58   ASIO_DECL address& operator=(address&& other);
59 
60   /// Assign from an IPv4 address.
61   ASIO_DECL address& operator=(
62       const asio::ip::address_v4& ipv4_address);
63 
64   /// Assign from an IPv6 address.
65   ASIO_DECL address& operator=(
66       const asio::ip::address_v6& ipv6_address);
67 
68   /// Get whether the address is an IP version 4 address.
is_v4() const69   bool is_v4() const
70   {
71     return type_ == ipv4;
72   }
73 
74   /// Get whether the address is an IP version 6 address.
is_v6() const75   bool is_v6() const
76   {
77     return type_ == ipv6;
78   }
79 
80   /// Get the address as an IP version 4 address.
81   ASIO_DECL asio::ip::address_v4 to_v4() const;
82 
83   /// Get the address as an IP version 6 address.
84   ASIO_DECL asio::ip::address_v6 to_v6() const;
85 
86   /// Get the address as a string in dotted decimal format.
87   ASIO_DECL std::string to_string() const;
88 
89   /// Get the address as a string in dotted decimal format.
90   ASIO_DECL std::string to_string(asio::error_code& ec) const;
91 
92   /// Create an address from an IPv4 address string in dotted decimal form,
93   /// or from an IPv6 address in hexadecimal notation.
94   ASIO_DECL static address from_string(const char* str);
95 
96   /// Create an address from an IPv4 address string in dotted decimal form,
97   /// or from an IPv6 address in hexadecimal notation.
98   ASIO_DECL static address from_string(
99       const char* str, asio::error_code& ec);
100 
101   /// Create an address from an IPv4 address string in dotted decimal form,
102   /// or from an IPv6 address in hexadecimal notation.
103   ASIO_DECL static address from_string(const std::string& str);
104 
105   /// Create an address from an IPv4 address string in dotted decimal form,
106   /// or from an IPv6 address in hexadecimal notation.
107   ASIO_DECL static address from_string(
108       const std::string& str, asio::error_code& ec);
109 
110   /// Determine whether the address is a loopback address.
111   ASIO_DECL bool is_loopback() const;
112 
113   /// Determine whether the address is unspecified.
114   ASIO_DECL bool is_unspecified() const;
115 
116   /// Determine whether the address is a multicast address.
117   ASIO_DECL bool is_multicast() const;
118 
119   /// Compare two addresses for equality.
120   ASIO_DECL friend bool operator==(const address& a1, const address& a2);
121 
122   /// Compare two addresses for inequality.
operator !=(const address & a1,const address & a2)123   friend bool operator!=(const address& a1, const address& a2)
124   {
125     return !(a1 == a2);
126   }
127 
128   /// Compare addresses for ordering.
129   ASIO_DECL friend bool operator<(const address& a1, const address& a2);
130 
131   /// Compare addresses for ordering.
operator >(const address & a1,const address & a2)132   friend bool operator>(const address& a1, const address& a2)
133   {
134     return a2 < a1;
135   }
136 
137   /// Compare addresses for ordering.
operator <=(const address & a1,const address & a2)138   friend bool operator<=(const address& a1, const address& a2)
139   {
140     return !(a2 < a1);
141   }
142 
143   /// Compare addresses for ordering.
operator >=(const address & a1,const address & a2)144   friend bool operator>=(const address& a1, const address& a2)
145   {
146     return !(a1 < a2);
147   }
148 
149 private:
150   // The type of the address.
151   enum { ipv4, ipv6 } type_;
152 
153   // The underlying IPv4 address.
154   asio::ip::address_v4 ipv4_address_;
155 
156   // The underlying IPv6 address.
157   asio::ip::address_v6 ipv6_address_;
158 };
159 
160 
161 } // namespace ip
162 } // namespace asio
163 
164 #include "asio/detail/pop_options.hpp"
165 
166 #include "asio/ip/impl/address.hpp"
167 # include "asio/ip/impl/address.ipp"
168 
169 #endif // ASIO_IP_ADDRESS_HPP
170