100.00% Lines (48/48) 100.00% Functions (17/17)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2026 Vinnie Falco (vinnie.falco@gmail.com)
3 - // Copyright (c) 2026 Michael Vandeberg  
4   // Copyright (c) 2026 Steve Gerbino 3   // Copyright (c) 2026 Steve Gerbino
5   // 4   //
6   // Distributed under the Boost Software License, Version 1.0. (See accompanying 5   // Distributed under the Boost Software License, Version 1.0. (See accompanying
7   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8   // 7   //
9   // Official repository: https://github.com/cppalliance/corosio 8   // Official repository: https://github.com/cppalliance/corosio
10   // 9   //
11   10  
12   #ifndef BOOST_COROSIO_IP_ADDRESS_HPP 11   #ifndef BOOST_COROSIO_IP_ADDRESS_HPP
13   #define BOOST_COROSIO_IP_ADDRESS_HPP 12   #define BOOST_COROSIO_IP_ADDRESS_HPP
14   13  
15   #include <boost/corosio/detail/config.hpp> 14   #include <boost/corosio/detail/config.hpp>
16   #include <boost/corosio/detail/except.hpp> 15   #include <boost/corosio/detail/except.hpp>
17   #include <boost/corosio/family.hpp> 16   #include <boost/corosio/family.hpp>
18   #include <boost/corosio/ipv4_address.hpp> 17   #include <boost/corosio/ipv4_address.hpp>
19   #include <boost/corosio/ipv6_address.hpp> 18   #include <boost/corosio/ipv6_address.hpp>
20   19  
21   #include <boost/capy/results.hpp> 20   #include <boost/capy/results.hpp>
22   21  
23   #include <compare> 22   #include <compare>
24   #include <iosfwd> 23   #include <iosfwd>
25   #include <string> 24   #include <string>
26   #include <string_view> 25   #include <string_view>
27   #include <system_error> 26   #include <system_error>
28   27  
29   namespace boost::corosio { 28   namespace boost::corosio {
30   29  
31   /** A version-independent IP address. 30   /** A version-independent IP address.
32   31  
33   This class holds either an IPv4 or an IPv6 address. Code that works with 32   This class holds either an IPv4 or an IPv6 address. Code that works with
34   both families carries one value instead of branching between @ref 33   both families carries one value instead of branching between @ref
35   ipv4_address and @ref ipv6_address. Family-generic queries such as 34   ipv4_address and @ref ipv6_address. Family-generic queries such as
36   @ref is_loopback dispatch to the held address, and @ref to_v4 / 35   @ref is_loopback dispatch to the held address, and @ref to_v4 /
37   @ref to_v6 recover the family-specific form. 36   @ref to_v6 recover the family-specific form.
38   37  
39   A v4-mapped IPv6 address (`::ffff:a.b.c.d`) is an IPv6-family 38   A v4-mapped IPv6 address (`::ffff:a.b.c.d`) is an IPv6-family
40   value: it does not compare equal to the IPv4 address it maps. 39   value: it does not compare equal to the IPv4 address it maps.
41   To compare across the mapping, normalize both sides with 40   To compare across the mapping, normalize both sides with
42   @ref to_v4 first. 41   @ref to_v4 first.
43   42  
44   @par Thread Safety 43   @par Thread Safety
45   Distinct objects: Safe.@n 44   Distinct objects: Safe.@n
46   Shared objects: Safe. 45   Shared objects: Safe.
47   46  
48   @par Example 47   @par Example
49   @code 48   @code
50   ip_address addr("2001:db8::1"); 49   ip_address addr("2001:db8::1");
51   if (addr.is_loopback()) 50   if (addr.is_loopback())
52   { 51   {
53   // family-generic query, no branching 52   // family-generic query, no branching
54   } 53   }
55   @endcode 54   @endcode
56   55  
57   @see 56   @see
58   @ref ipv4_address, 57   @ref ipv4_address,
59   @ref ipv6_address, 58   @ref ipv6_address,
60 - @ref make_ip_address 59 + @ref make_ip_address.
61   */ 60   */
62   class BOOST_COROSIO_DECL ip_address 61   class BOOST_COROSIO_DECL ip_address
63   { 62   {
64   ipv4_address v4_; 63   ipv4_address v4_;
65   ipv6_address v6_; 64   ipv6_address v6_;
66   corosio::family family_ = corosio::family::v4; 65   corosio::family family_ = corosio::family::v4;
67   66  
68   public: 67   public:
69   /** The number of characters in the longest possible address string. 68   /** The number of characters in the longest possible address string.
70   */ 69   */
71   static constexpr std::size_t max_str_len = ipv6_address::max_str_len; 70   static constexpr std::size_t max_str_len = ipv6_address::max_str_len;
72   71  
73   /** Default constructor. 72   /** Default constructor.
74   73  
75   Constructs the IPv4 unspecified address (0.0.0.0). 74   Constructs the IPv4 unspecified address (0.0.0.0).
76   */ 75   */
HITCBC 77   263433 ip_address() = default; 76   257990 ip_address() = default;
78   77  
79   /** Copy constructor. 78   /** Copy constructor.
80   */ 79   */
81   ip_address(ip_address const&) = default; 80   ip_address(ip_address const&) = default;
82   81  
83   /** Copy assignment. 82   /** Copy assignment.
84   83  
85   @return A reference to this object. 84   @return A reference to this object.
86   */ 85   */
87   ip_address& operator=(ip_address const&) = default; 86   ip_address& operator=(ip_address const&) = default;
88   87  
89   /** Construct from an IPv4 address. 88   /** Construct from an IPv4 address.
90   89  
91   @param addr The address to hold. 90   @param addr The address to hold.
92   */ 91   */
HITCBC 93   34578 ip_address(ipv4_address const& addr) noexcept : v4_(addr) {} 92   34192 ip_address(ipv4_address const& addr) noexcept : v4_(addr) {}
94   93  
95   /** Construct from an IPv6 address. 94   /** Construct from an IPv6 address.
96   95  
97   @param addr The address to hold. 96   @param addr The address to hold.
98   */ 97   */
HITCBC 99   227 ip_address(ipv6_address const& addr) noexcept 98   227 ip_address(ipv6_address const& addr) noexcept
HITCBC 100   454 : v6_(addr) 99   454 : v6_(addr)
HITCBC 101   227 , family_(corosio::family::v6) 100   227 , family_(corosio::family::v6)
102   { 101   {
HITCBC 103   227 } 102   227 }
104   103  
105   /** Construct from a string. 104   /** Construct from a string.
106   105  
107   This function constructs an address from the string `s`, 106   This function constructs an address from the string `s`,
108   which must contain a valid IPv4 or IPv6 address string 107   which must contain a valid IPv4 or IPv6 address string
109   or else an exception is thrown. 108   or else an exception is thrown.
110   109  
111   @par Exception Safety 110   @par Exception Safety
112   Strong guarantee. 111   Strong guarantee.
113   112  
114   @throws std::system_error `errc::invalid_argument` if the input 113   @throws std::system_error `errc::invalid_argument` if the input
115   failed to parse correctly. 114   failed to parse correctly.
116   115  
117   @note For a non-throwing parse function, 116   @note For a non-throwing parse function,
118   use @ref make_ip_address. 117   use @ref make_ip_address.
119   118  
120   @param s The string to parse. 119   @param s The string to parse.
121   120  
122   @see 121   @see
123 - @ref make_ip_address 122 + @ref make_ip_address.
124   */ 123   */
125   explicit ip_address(std::string_view s); 124   explicit ip_address(std::string_view s);
126   125  
127   /** Return the address family. 126   /** Return the address family.
128   127  
129   The portable spelling of the family; @ref is_v4 and 128   The portable spelling of the family; @ref is_v4 and
130   @ref is_v6 are sugar over it. 129   @ref is_v6 are sugar over it.
131   130  
132   @return The family of the held address. 131   @return The family of the held address.
133   */ 132   */
HITCBC 134   637 corosio::family family() const noexcept 133   637 corosio::family family() const noexcept
135   { 134   {
HITCBC 136   637 return family_; 135   637 return family_;
137   } 136   }
138   137  
139   /** Check if the held address is IPv4. 138   /** Check if the held address is IPv4.
140   139  
141   @return `true` if the address is IPv4, `false` if IPv6. 140   @return `true` if the address is IPv4, `false` if IPv6.
142   */ 141   */
HITCBC 143   30323 bool is_v4() const noexcept 142   28969 bool is_v4() const noexcept
144   { 143   {
HITCBC 145   30323 return family_ == corosio::family::v4; 144   28969 return family_ == corosio::family::v4;
146   } 145   }
147   146  
148   /** Check if the held address is IPv6. 147   /** Check if the held address is IPv6.
149   148  
150   @return `true` if the address is IPv6, `false` if IPv4. 149   @return `true` if the address is IPv6, `false` if IPv4.
151   */ 150   */
HITCBC 152   69 bool is_v6() const noexcept 151   69 bool is_v6() const noexcept
153   { 152   {
HITCBC 154   69 return family_ == corosio::family::v6; 153   69 return family_ == corosio::family::v6;
155   } 154   }
156   155  
157   /** Check if the address is a loopback address. 156   /** Check if the address is a loopback address.
158   157  
159   @return `true` if the held address is a loopback 158   @return `true` if the held address is a loopback
160   address of its family. 159   address of its family.
161   */ 160   */
HITCBC 162   13 bool is_loopback() const noexcept 161   13 bool is_loopback() const noexcept
163   { 162   {
HITCBC 164   13 return is_v4() ? v4_.is_loopback() : v6_.is_loopback(); 163   13 return is_v4() ? v4_.is_loopback() : v6_.is_loopback();
165   } 164   }
166   165  
167   /** Check if the address is unspecified. 166   /** Check if the address is unspecified.
168   167  
169   @return `true` if the held address is the unspecified 168   @return `true` if the held address is the unspecified
170   address of its family. 169   address of its family.
171   */ 170   */
HITCBC 172   6 bool is_unspecified() const noexcept 171   6 bool is_unspecified() const noexcept
173   { 172   {
HITCBC 174   6 return is_v4() ? v4_.is_unspecified() : v6_.is_unspecified(); 173   6 return is_v4() ? v4_.is_unspecified() : v6_.is_unspecified();
175   } 174   }
176   175  
177   /** Check if the address is a multicast address. 176   /** Check if the address is a multicast address.
178   177  
179   @return `true` if the held address is a multicast 178   @return `true` if the held address is a multicast
180   address of its family. 179   address of its family.
181   */ 180   */
HITCBC 182   4 bool is_multicast() const noexcept 181   4 bool is_multicast() const noexcept
183   { 182   {
HITCBC 184   4 return is_v4() ? v4_.is_multicast() : v6_.is_multicast(); 183   4 return is_v4() ? v4_.is_multicast() : v6_.is_multicast();
185   } 184   }
186   185  
187   /** Check if the address is a v4-mapped IPv6 address. 186   /** Check if the address is a v4-mapped IPv6 address.
188   187  
189   @return `true` if the address is IPv6 and is an 188   @return `true` if the address is IPv6 and is an
190   IPv4-Mapped IPv6 Address (`::ffff:a.b.c.d`). 189   IPv4-Mapped IPv6 Address (`::ffff:a.b.c.d`).
191   190  
192   @see 191   @see
193 - @ref to_v4 192 + @ref to_v4.
194   */ 193   */
HITCBC 195   3 bool is_v4_mapped() const noexcept 194   3 bool is_v4_mapped() const noexcept
196   { 195   {
HITCBC 197   3 return is_v6() && v6_.is_v4_mapped(); 196   3 return is_v6() && v6_.is_v4_mapped();
198   } 197   }
199   198  
200   /** Convert to an IPv4 address. 199   /** Convert to an IPv4 address.
201   200  
202   Returns the held IPv4 address, or the IPv4 address that a 201   Returns the held IPv4 address, or the IPv4 address that a
203   v4-mapped IPv6 address maps. This makes normalize-then-compare 202   v4-mapped IPv6 address maps. This makes normalize-then-compare
204   a single call when matching addresses across the mapping. 203   a single call when matching addresses across the mapping.
205   204  
206   @throws std::system_error `errc::address_family_not_supported` 205   @throws std::system_error `errc::address_family_not_supported`
207   if the address is IPv6 and not v4-mapped. 206   if the address is IPv6 and not v4-mapped.
208   207  
209   @return The IPv4 form of the address. 208   @return The IPv4 form of the address.
210   209  
211   @see 210   @see
212 - @ref is_v4, @ref is_v4_mapped 211 + @ref is_v4, @ref is_v4_mapped.
213   */ 212   */
HITCBC 214   10726 ipv4_address to_v4() const 213   10272 ipv4_address to_v4() const
215   { 214   {
HITCBC 216   10726 return is_v4() ? v4_ : v6_.to_v4(); 215   10272 return is_v4() ? v4_ : v6_.to_v4();
217   } 216   }
218   217  
219   /** Convert to an IPv6 address. 218   /** Convert to an IPv6 address.
220   219  
221   To map an IPv4 address into IPv6, use the 220   To map an IPv4 address into IPv6, use the
222   `ipv6_address(ipv4_address const&)` constructor instead. 221   `ipv6_address(ipv4_address const&)` constructor instead.
223   222  
224   @throws std::system_error `errc::address_family_not_supported` 223   @throws std::system_error `errc::address_family_not_supported`
225   if the address is IPv4. 224   if the address is IPv4.
226   225  
227   @return The held IPv6 address. 226   @return The held IPv6 address.
228   227  
229   @see 228   @see
230 - @ref is_v6 229 + @ref is_v6.
231   */ 230   */
HITCBC 232   89 ipv6_address to_v6() const 231   89 ipv6_address to_v6() const
233   { 232   {
HITCBC 234   89 if (is_v4()) 233   89 if (is_v4())
HITCBC 235   2 detail::throw_system_error( 234   2 detail::throw_system_error(
HITCBC 236   2 std::make_error_code(std::errc::address_family_not_supported), 235   2 std::make_error_code(std::errc::address_family_not_supported),
237   "address is not IPv6"); 236   "address is not IPv6");
HITCBC 238   87 return v6_; 237   87 return v6_;
239   } 238   }
240   239  
241   /** Return the address as a string. 240   /** Return the address as a string.
242   241  
243   IPv4 addresses format in dotted decimal, IPv6 addresses 242   IPv4 addresses format in dotted decimal, IPv6 addresses
244   in standard notation without surrounding brackets. 243   in standard notation without surrounding brackets.
245   244  
246   @return The address as a string. 245   @return The address as a string.
247   */ 246   */
HITCBC 248   13 std::string to_string() const 247   13 std::string to_string() const
249   { 248   {
HITCBC 250   13 return is_v4() ? v4_.to_string() : v6_.to_string(); 249   13 return is_v4() ? v4_.to_string() : v6_.to_string();
251   } 250   }
252   251  
253   /** Write a string representing the address to a buffer. 252   /** Write a string representing the address to a buffer.
254   253  
255   The resulting buffer is not null-terminated. 254   The resulting buffer is not null-terminated.
256   255  
257   @throws std::length_error `dest_size < ip_address::max_str_len` 256   @throws std::length_error `dest_size < ip_address::max_str_len`
258   257  
259   @param dest The buffer in which to write, 258   @param dest The buffer in which to write,
260   which must have at least `dest_size` space. 259   which must have at least `dest_size` space.
261   260  
262   @param dest_size The size of the output buffer. 261   @param dest_size The size of the output buffer.
263   262  
264   @return The formatted string view. 263   @return The formatted string view.
265   */ 264   */
266   std::string_view to_buffer(char* dest, std::size_t dest_size) const; 265   std::string_view to_buffer(char* dest, std::size_t dest_size) const;
267   266  
268   /** Return true if two addresses are equal. 267   /** Return true if two addresses are equal.
269   268  
270   Addresses are equal if they have the same family and the 269   Addresses are equal if they have the same family and the
271   same value. A v4-mapped IPv6 address is not equal to the 270   same value. A v4-mapped IPv6 address is not equal to the
272   IPv4 address it maps; normalize with @ref ip_address::to_v4 271   IPv4 address it maps; normalize with @ref ip_address::to_v4
273   to compare 272   to compare
274   across the mapping. 273   across the mapping.
275   274  
276   @return `true` if the addresses are equal. 275   @return `true` if the addresses are equal.
277   */ 276   */
HITCBC 278   143 friend bool operator==(ip_address const& a1, ip_address const& a2) noexcept 277   143 friend bool operator==(ip_address const& a1, ip_address const& a2) noexcept
279   { 278   {
HITCBC 280   143 if (a1.family_ != a2.family_) 279   143 if (a1.family_ != a2.family_)
HITCBC 281   8 return false; 280   8 return false;
HITCBC 282   135 return a1.is_v4() ? a1.v4_ == a2.v4_ : a1.v6_ == a2.v6_; 281   135 return a1.is_v4() ? a1.v4_ == a2.v4_ : a1.v6_ == a2.v6_;
283   } 282   }
284   283  
285   /** Order two addresses. 284   /** Order two addresses.
286   285  
287   Establishes a strict total ordering consistent with 286   Establishes a strict total ordering consistent with
288   @ref operator==: addresses are ordered first by family 287   @ref operator==: addresses are ordered first by family
289   (IPv4 before IPv6), then by value. This makes `ip_address` 288   (IPv4 before IPv6), then by value. This makes `ip_address`
290   usable as a key in ordered containers such as `std::map` 289   usable as a key in ordered containers such as `std::map`
291   and `std::set`. 290   and `std::set`.
292   291  
293   @return The relative order of `a1` and `a2`. 292   @return The relative order of `a1` and `a2`.
294   */ 293   */
295   friend std::strong_ordering 294   friend std::strong_ordering
HITCBC 296   33 operator<=>(ip_address const& a1, ip_address const& a2) noexcept 295   33 operator<=>(ip_address const& a1, ip_address const& a2) noexcept
297   { 296   {
HITCBC 298   33 if (a1.family_ != a2.family_) 297   33 if (a1.family_ != a2.family_)
HITCBC 299   11 return a1.is_v4() ? std::strong_ordering::less 298   11 return a1.is_v4() ? std::strong_ordering::less
HITCBC 300   11 : std::strong_ordering::greater; 299   11 : std::strong_ordering::greater;
HITCBC 301   22 return a1.is_v4() ? a1.v4_ <=> a2.v4_ : a1.v6_ <=> a2.v6_; 300   22 return a1.is_v4() ? a1.v4_ <=> a2.v4_ : a1.v6_ <=> a2.v6_;
302   } 301   }
303   302  
304   /** Format the address to an output stream. 303   /** Format the address to an output stream.
305   304  
306   @param os The output stream. 305   @param os The output stream.
307   @param addr The address to format. 306   @param addr The address to format.
308   @return The output stream. 307   @return The output stream.
309   */ 308   */
310   friend BOOST_COROSIO_DECL std::ostream& 309   friend BOOST_COROSIO_DECL std::ostream&
311   operator<<(std::ostream& os, ip_address const& addr); 310   operator<<(std::ostream& os, ip_address const& addr);
312   }; 311   };
313   312  
314   /** Create an IP address from a string. 313   /** Create an IP address from a string.
315   314  
316   This function parses `s` as an IPv4 address in dotted decimal form, or 315   This function parses `s` as an IPv4 address in dotted decimal form, or
317   an IPv6 address in hexadecimal notation. An IPv6 address may carry a 316   an IPv6 address in hexadecimal notation. An IPv6 address may carry a
318   `%zone` suffix: a decimal interface index, or an interface name where 317   `%zone` suffix: a decimal interface index, or an interface name where
319   the platform names interfaces. The string must contain the address 318   the platform names interfaces. The string must contain the address
320   alone: port suffixes, surrounding brackets, and host names are not 319   alone: port suffixes, surrounding brackets, and host names are not
321   accepted. 320   accepted.
322   321  
323   @par Exception Safety 322   @par Exception Safety
324   Throws nothing. 323   Throws nothing.
325   324  
326   @param s The string to parse. 325   @param s The string to parse.
327   @return The error code, empty on success, and the parsed 326   @return The error code, empty on success, and the parsed
328   address — default-constructed on failure. 327   address — default-constructed on failure.
329   */ 328   */
330   [[nodiscard]] BOOST_COROSIO_DECL capy::results<ip_address> 329   [[nodiscard]] BOOST_COROSIO_DECL capy::results<ip_address>
331   make_ip_address(std::string_view s) noexcept; 330   make_ip_address(std::string_view s) noexcept;
332   331  
HITCBC 333   25 inline ip_address::ip_address(std::string_view s) 332   25 inline ip_address::ip_address(std::string_view s)
334   { 333   {
HITCBC 335   25 auto [ec, addr] = make_ip_address(s); 334   25 auto [ec, addr] = make_ip_address(s);
HITCBC 336   25 if (ec) 335   25 if (ec)
HITCBC 337   2 detail::throw_system_error(ec, "invalid IP address"); 336   2 detail::throw_system_error(ec, "invalid IP address");
HITCBC 338   23 *this = addr; 337   23 *this = addr;
HITCBC 339   23 } 338   23 }
340   339  
341   } // namespace boost::corosio 340   } // namespace boost::corosio
342   341  
343   namespace std { 342   namespace std {
344   343  
345   /// Hash support for `boost::corosio::ip_address`. 344   /// Hash support for `boost::corosio::ip_address`.
346   template<> 345   template<>
347   struct hash<boost::corosio::ip_address> 346   struct hash<boost::corosio::ip_address>
348   { 347   {
349   /// Return the hash of `addr`. 348   /// Return the hash of `addr`.
350   std::size_t 349   std::size_t
HITCBC 351   25 operator()(boost::corosio::ip_address const& addr) const noexcept 350   25 operator()(boost::corosio::ip_address const& addr) const noexcept
352   { 351   {
353   // Family-guarded dispatch keeps the throwing conversions 352   // Family-guarded dispatch keeps the throwing conversions
354   // unreachable 353   // unreachable
HITCBC 355   25 return addr.is_v4() 354   25 return addr.is_v4()
HITCBC 356   25 ? hash<boost::corosio::ipv4_address>()(addr.to_v4()) 355   25 ? hash<boost::corosio::ipv4_address>()(addr.to_v4())
HITCBC 357   25 : hash<boost::corosio::ipv6_address>()(addr.to_v6()); 356   25 : hash<boost::corosio::ipv6_address>()(addr.to_v6());
358   } 357   }
359   }; 358   };
360   359  
361   } // namespace std 360   } // namespace std
362   361  
363   #endif 362   #endif