![]() |
|
|||
File indexing completed on 2025-09-17 08:52:42
0001 // 0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) 0003 // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com) 0004 // 0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying 0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 0007 // 0008 // Official repository: https://github.com/boostorg/url 0009 // 0010 0011 #ifndef BOOST_URL_IPV4_ADDRESS_HPP 0012 #define BOOST_URL_IPV4_ADDRESS_HPP 0013 0014 #include <boost/url/detail/config.hpp> 0015 #include <boost/url/error.hpp> 0016 #include <boost/url/error_types.hpp> 0017 #include <boost/core/detail/string_view.hpp> 0018 #include <boost/url/grammar/string_token.hpp> 0019 #include <string> 0020 #include <array> 0021 #include <cstdint> 0022 #include <iosfwd> 0023 0024 namespace boost { 0025 namespace urls { 0026 0027 /** An IP version 4 style address. 0028 0029 Objects of this type are used to construct, 0030 parse, and manipulate IP version 6 addresses. 0031 0032 @par BNF 0033 @code 0034 IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet 0035 0036 dec-octet = DIGIT ; 0-9 0037 / %x31-39 DIGIT ; 10-99 0038 / "1" 2DIGIT ; 100-199 0039 / "2" %x30-34 DIGIT ; 200-249 0040 / "25" %x30-35 ; 250-255 0041 @endcode 0042 0043 @par Specification 0044 @li <a href="https://en.wikipedia.org/wiki/IPv4" 0045 >IPv4 (Wikipedia)</a> 0046 @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2" 0047 >3.2.2. Host (rfc3986)</a> 0048 0049 @see 0050 @ref parse_ipv4_address, 0051 @ref ipv6_address. 0052 */ 0053 class ipv4_address 0054 { 0055 public: 0056 /** The number of characters in the longest possible IPv4 string. 0057 0058 The longest ipv4 address string is "255.255.255.255". 0059 */ 0060 static 0061 constexpr 0062 std::size_t max_str_len = 15; 0063 0064 /** The type used to represent an address as an unsigned integer 0065 */ 0066 using uint_type = 0067 std::uint_least32_t; 0068 0069 /** The type used to represent an address as an array of bytes 0070 */ 0071 using bytes_type = 0072 std::array<unsigned char, 4>; 0073 0074 /** Constructor. 0075 */ 0076 ipv4_address() = default; 0077 0078 /** Constructor. 0079 */ 0080 ipv4_address( 0081 ipv4_address const&) = default; 0082 0083 /** Copy Assignment. 0084 0085 @param other The object to copy. 0086 @return A reference to this object. 0087 */ 0088 ipv4_address& 0089 operator=( 0090 ipv4_address const& other) = default; 0091 0092 // 0093 //--- 0094 // 0095 0096 /** Construct from an unsigned integer. 0097 0098 This function constructs an address from 0099 the unsigned integer `u`, where the most 0100 significant byte forms the first octet 0101 of the resulting address. 0102 0103 @param u The integer to construct from. 0104 */ 0105 BOOST_URL_DECL 0106 explicit 0107 ipv4_address( 0108 uint_type u) noexcept; 0109 0110 /** Construct from an array of bytes. 0111 0112 This function constructs an address 0113 from the array in `bytes`, which is 0114 interpreted in big-endian. 0115 0116 @param bytes The value to construct from. 0117 */ 0118 BOOST_URL_DECL 0119 explicit 0120 ipv4_address( 0121 bytes_type const& bytes) noexcept; 0122 0123 /** Construct from a string. 0124 0125 This function constructs an address from 0126 the string `s`, which must contain a valid 0127 IPv4 address string or else an exception 0128 is thrown. 0129 0130 @note For a non-throwing parse function, 0131 use @ref parse_ipv4_address. 0132 0133 @par Exception Safety 0134 Exceptions thrown on invalid input. 0135 0136 @throw system_error The input failed to parse correctly. 0137 0138 @param s The string to parse. 0139 0140 @par Specification 0141 @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2" 0142 >3.2.2. Host (rfc3986)</a> 0143 0144 @see 0145 @ref parse_ipv4_address. 0146 */ 0147 BOOST_URL_DECL 0148 explicit 0149 ipv4_address( 0150 core::string_view s); 0151 0152 /** Return the address as bytes, in network byte order. 0153 0154 @return The address as an array of bytes. 0155 */ 0156 BOOST_URL_DECL 0157 bytes_type 0158 to_bytes() const noexcept; 0159 0160 /** Return the address as an unsigned integer. 0161 0162 @return The address as an unsigned integer. 0163 */ 0164 BOOST_URL_DECL 0165 uint_type 0166 to_uint() const noexcept; 0167 0168 /** Return the address as a string in dotted decimal format 0169 0170 When called with no arguments, the 0171 return type is `std::string`. 0172 Otherwise, the return type and style 0173 of output is determined by which string 0174 token is passed. 0175 0176 @par Example 0177 @code 0178 assert( ipv4_address(0x01020304).to_string() == "1.2.3.4" ); 0179 @endcode 0180 0181 @par Complexity 0182 Constant. 0183 0184 @par Exception Safety 0185 Strong guarantee. 0186 Calls to allocate may throw. 0187 String tokens may throw exceptions. 0188 0189 @return The return type of the string token. 0190 If the token parameter is omitted, then 0191 a new `std::string` is returned. 0192 Otherwise, the function return type 0193 is the result type of the token. 0194 0195 @param token An optional string token. 0196 0197 @par Specification 0198 @li <a href="https://datatracker.ietf.org/doc/html/rfc4291#section-2.2"> 0199 2.2. Text Representation of Addresses (rfc4291)</a> 0200 */ 0201 template<BOOST_URL_STRTOK_TPARAM> 0202 BOOST_URL_STRTOK_RETURN 0203 to_string(StringToken&& token = {}) const 0204 { 0205 to_string_impl(token); 0206 return token.result(); 0207 } 0208 0209 /** Write a dotted decimal string representing the address to a buffer 0210 0211 The resulting buffer is not null-terminated. 0212 0213 @throw std::length_error `dest_size < ipv4_address::max_str_len` 0214 0215 @return The formatted string 0216 0217 @param dest The buffer in which to write, 0218 which must have at least `dest_size` space. 0219 0220 @param dest_size The size of the output buffer. 0221 */ 0222 BOOST_URL_DECL 0223 core::string_view 0224 to_buffer( 0225 char* dest, 0226 std::size_t dest_size) const; 0227 0228 /** Return true if the address is a loopback address 0229 0230 @return `true` if the address is a loopback address 0231 */ 0232 BOOST_URL_DECL 0233 bool 0234 is_loopback() const noexcept; 0235 0236 /** Return true if the address is unspecified 0237 0238 @return `true` if the address is unspecified 0239 */ 0240 BOOST_URL_DECL 0241 bool 0242 is_unspecified() const noexcept; 0243 0244 /** Return true if the address is a multicast address 0245 0246 @return `true` if the address is a multicast address, 0247 */ 0248 BOOST_URL_DECL 0249 bool 0250 is_multicast() const noexcept; 0251 0252 /** Return true if two addresses are equal 0253 0254 @param a1 The first address to compare. 0255 @param a2 The second address to compare. 0256 @return `true` if the addresses are equal, otherwise `false`. 0257 0258 */ 0259 friend 0260 bool 0261 operator==( 0262 ipv4_address const& a1, 0263 ipv4_address const& a2) noexcept 0264 { 0265 return a1.addr_ == a2.addr_; 0266 } 0267 0268 /** Return true if two addresses are not equal 0269 0270 @param a1 The first address to compare. 0271 @param a2 The second address to compare. 0272 @return `true` if the addresses are not equal, otherwise `false`. 0273 */ 0274 friend 0275 bool 0276 operator!=( 0277 ipv4_address const& a1, 0278 ipv4_address const& a2) noexcept 0279 { 0280 return a1.addr_ != a2.addr_; 0281 } 0282 0283 /** Return an address object that represents any address 0284 0285 @return The any address. 0286 */ 0287 static 0288 ipv4_address 0289 any() noexcept 0290 { 0291 return ipv4_address(); 0292 } 0293 0294 /** Return an address object that represents the loopback address 0295 0296 @return The loopback address. 0297 */ 0298 static 0299 ipv4_address 0300 loopback() noexcept 0301 { 0302 return ipv4_address(0x7F000001); 0303 } 0304 0305 /** Return an address object that represents the broadcast address 0306 0307 @return The broadcast address. 0308 */ 0309 static 0310 ipv4_address 0311 broadcast() noexcept 0312 { 0313 return ipv4_address(0xFFFFFFFF); 0314 } 0315 0316 /** Format the address to an output stream. 0317 0318 IPv4 addresses written to output streams 0319 are written in their dotted decimal format. 0320 0321 @param os The output stream. 0322 0323 @param addr The address to format. 0324 */ 0325 friend 0326 std::ostream& 0327 operator<<( 0328 std::ostream& os, 0329 ipv4_address const& addr) 0330 { 0331 char buf[ipv4_address::max_str_len]; 0332 os << addr.to_buffer(buf, sizeof(buf)); 0333 return os; 0334 } 0335 0336 private: 0337 friend class ipv6_address; 0338 0339 BOOST_URL_DECL 0340 std::size_t 0341 print_impl( 0342 char* dest) const noexcept; 0343 0344 BOOST_URL_DECL 0345 void 0346 to_string_impl( 0347 string_token::arg& t) const; 0348 0349 uint_type addr_ = 0; 0350 }; 0351 0352 /** Format the address to an output stream. 0353 0354 IPv4 addresses written to output streams 0355 are written in their dotted decimal format. 0356 0357 @param os The output stream. 0358 @param addr The address to format. 0359 @return The output stream. 0360 */ 0361 std::ostream& 0362 operator<<( 0363 std::ostream& os, 0364 ipv4_address const& addr); 0365 0366 //------------------------------------------------ 0367 0368 /** Return an IPv4 address from an IP address string in dotted decimal form 0369 0370 @param s The string to parse. 0371 @return The parsed address, or an error code. 0372 */ 0373 BOOST_URL_DECL 0374 system::result<ipv4_address> 0375 parse_ipv4_address( 0376 core::string_view s) noexcept; 0377 0378 } // urls 0379 } // boost 0380 0381 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |