Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:29

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     ipv4_address&
0086     operator=(
0087         ipv4_address const&) = default;
0088 
0089     //
0090     //---
0091     //
0092 
0093     /** Construct from an unsigned integer.
0094 
0095         This function constructs an address from
0096         the unsigned integer `u`, where the most
0097         significant byte forms the first octet
0098         of the resulting address.
0099 
0100         @param u The integer to construct from.
0101     */
0102     BOOST_URL_DECL
0103     explicit
0104     ipv4_address(
0105         uint_type u) noexcept;
0106 
0107     /** Construct from an array of bytes.
0108 
0109         This function constructs an address
0110         from the array in `bytes`, which is
0111         interpreted in big-endian.
0112 
0113         @param bytes The value to construct from.
0114     */
0115     BOOST_URL_DECL
0116     explicit
0117     ipv4_address(
0118         bytes_type const& bytes) noexcept;
0119 
0120     /** Construct from a string.
0121 
0122         This function constructs an address from
0123         the string `s`, which must contain a valid
0124         IPv4 address string or else an exception
0125         is thrown.
0126 
0127         @note For a non-throwing parse function,
0128         use @ref parse_ipv4_address.
0129 
0130         @par Exception Safety
0131         Exceptions thrown on invalid input.
0132 
0133         @throw system_error
0134         The input failed to parse correctly.
0135 
0136         @param s The string to parse.
0137 
0138         @par Specification
0139         @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
0140             >3.2.2. Host (rfc3986)</a>
0141 
0142         @see
0143             @ref parse_ipv4_address.
0144     */
0145     BOOST_URL_DECL
0146     explicit
0147     ipv4_address(
0148         core::string_view s);
0149 
0150     /** Return the address as bytes, in network byte order.
0151     */
0152     BOOST_URL_DECL
0153     bytes_type
0154     to_bytes() const noexcept;
0155 
0156     /** Return the address as an unsigned integer.
0157     */
0158     BOOST_URL_DECL
0159     uint_type
0160     to_uint() const noexcept;
0161 
0162     /** Return the address as a string in dotted decimal format
0163 
0164         When called with no arguments, the
0165         return type is `std::string`.
0166         Otherwise, the return type and style
0167         of output is determined by which string
0168         token is passed.
0169 
0170         @par Example
0171         @code
0172         assert( ipv4_address(0x01020304).to_string() == "1.2.3.4" );
0173         @endcode
0174 
0175         @par Complexity
0176         Constant.
0177 
0178         @par Exception Safety
0179         Strong guarantee.
0180         Calls to allocate may throw.
0181         String tokens may throw exceptions.
0182 
0183         @return The return type of the string token.
0184         If the token parameter is omitted, then
0185         a new `std::string` is returned.
0186         Otherwise, the function return type
0187         is the result type of the token.
0188 
0189         @param token An optional string token.
0190 
0191         @par Specification
0192         @li <a href="https://datatracker.ietf.org/doc/html/rfc4291#section-2.2">
0193             2.2. Text Representation of Addresses (rfc4291)</a>
0194     */
0195     template<BOOST_URL_STRTOK_TPARAM>
0196     BOOST_URL_STRTOK_RETURN
0197     to_string(
0198         BOOST_URL_STRTOK_ARG(token)) const
0199     {
0200         to_string_impl(token);
0201         return token.result();
0202     }
0203 
0204     /** Write a dotted decimal string representing the address to a buffer
0205 
0206         The resulting buffer is not null-terminated.
0207 
0208         @throw std::length_error `dest_size < ipv4_address::max_str_len`
0209 
0210         @return The formatted string
0211 
0212         @param dest The buffer in which to write,
0213         which must have at least `dest_size` space.
0214 
0215         @param dest_size The size of the output buffer.
0216     */
0217     BOOST_URL_DECL
0218     core::string_view
0219     to_buffer(
0220         char* dest,
0221         std::size_t dest_size) const;
0222 
0223     /** Return true if the address is a loopback address
0224     */
0225     BOOST_URL_DECL
0226     bool
0227     is_loopback() const noexcept;
0228 
0229     /** Return true if the address is unspecified
0230     */
0231     BOOST_URL_DECL
0232     bool
0233     is_unspecified() const noexcept;
0234 
0235     /** Return true if the address is a multicast address
0236     */
0237     BOOST_URL_DECL
0238     bool
0239     is_multicast() const noexcept;
0240 
0241     /** Return true if two addresses are equal
0242     */
0243     friend
0244     bool
0245     operator==(
0246         ipv4_address const& a1,
0247         ipv4_address const& a2) noexcept
0248     {
0249         return a1.addr_ == a2.addr_;
0250     }
0251 
0252     /** Return true if two addresses are not equal
0253     */
0254     friend
0255     bool
0256     operator!=(
0257         ipv4_address const& a1,
0258         ipv4_address const& a2) noexcept
0259     {
0260         return a1.addr_ != a2.addr_;
0261     }
0262 
0263     /** Return an address object that represents any address
0264     */
0265     static
0266     ipv4_address
0267     any() noexcept
0268     {
0269         return ipv4_address();
0270     }
0271 
0272     /** Return an address object that represents the loopback address
0273     */
0274     static
0275     ipv4_address
0276     loopback() noexcept
0277     {
0278         return ipv4_address(0x7F000001);
0279     }
0280 
0281     /** Return an address object that represents the broadcast address
0282     */
0283     static
0284     ipv4_address
0285     broadcast() noexcept
0286     {
0287         return ipv4_address(0xFFFFFFFF);
0288     }
0289 
0290     // hidden friend
0291     friend
0292     std::ostream&
0293     operator<<(
0294         std::ostream& os,
0295         ipv4_address const& addr)
0296     {
0297         char buf[ipv4_address::max_str_len];
0298         os << addr.to_buffer(buf, sizeof(buf));
0299         return os;
0300     }
0301 
0302 private:
0303     friend class ipv6_address;
0304 
0305     BOOST_URL_DECL
0306     std::size_t
0307     print_impl(
0308         char* dest) const noexcept;
0309 
0310     BOOST_URL_DECL
0311     void
0312     to_string_impl(
0313         string_token::arg& t) const;
0314 
0315     uint_type addr_ = 0;
0316 };
0317 
0318 /** Format the address to an output stream.
0319 
0320     IPv4 addresses written to output streams
0321     are written in their dotted decimal format.
0322 
0323     @param os The output stream.
0324 
0325     @param addr The address to format.
0326 */
0327 std::ostream&
0328 operator<<(
0329     std::ostream& os,
0330     ipv4_address const& addr);
0331 
0332 //------------------------------------------------
0333 
0334 /** Return an IPv4 address from an IP address string in dotted decimal form
0335 */
0336 BOOST_URL_DECL
0337 system::result<ipv4_address>
0338 parse_ipv4_address(
0339     core::string_view s) noexcept;
0340 
0341 } // urls
0342 } // boost
0343 
0344 #endif