ipaddress 1.2.0
Loading...
Searching...
No Matches
ipv4-address.hpp
Go to the documentation of this file.
1/**
2 * @file ipv4-address.hpp
3 * @brief Provides a set of functions and classes for handling IPv4 addresses
4 * @author Vladimir Shaleev
5 * @copyright MIT License
6 *
7 * Includes functionalities to convert IPv4 addresses to and from various formats,
8 * perform comparisons, and query specific properties of the addresses.
9 * It serves as a foundational component for network applications that require manipulation
10 * and analysis of IPv4 address data.
11 */
12
13#ifndef IPADDRESS_IPV4_ADDRESS_HPP
14#define IPADDRESS_IPV4_ADDRESS_HPP
15
16#include "base-v4.hpp"
17
18namespace IPADDRESS_NAMESPACE {
19
21using ipv6_address = ip_address_base<ipv6_address_base>;
22
23/**
24 * Represents the base class for IPv4 address manipulation.
25 *
26 * This class provides the basic functionalities required for handling IPv4 addresses,
27 * including conversion to and from numeric representations, access to the underlying bytes,
28 * and utility functions that are common across different representations of IPv4 addresses.
29 */
31public:
32 using base_type = typename base_v4<ipv4_address_base>::base_type; /**< The base type for the IPv4 address. */
33 using uint_type = typename base_v4<ipv4_address_base>::uint_type; /**< The unsigned integer type for the IPv4 address. */
34
35 /**
36 * Creates an IPv4 address from an unsigned integer using a template parameter.
37 *
38 * @tparam Ip The unsigned integer representing the IPv4 address.
39 * @return An instance of ip address representing the IPv4 address.
40 */
41 template <uint_type Ip>
43 return from_uint(Ip);
44 }
45
46 /**
47 * Creates an IPv4 address from an unsigned integer.
48 *
49 * @param[in] ip The unsigned integer representing the IPv4 address.
50 * @return An instance of ip address representing the IPv4 address.
51 * @remark Bytes in integer \a ip must be presented in **host byte order**.
52 */
54 return ip_from_uint32(ip);
55 }
56
57 /**
58 * Converts the IPv4 address to an unsigned integer.
59 *
60 * @return The unsigned integer representation of the IPv4 address.
61 * @remark Bytes in integer are presented in **host byte order**.
62 */
64 return ip_to_uint32(_bytes);
65 }
66
67 /**
68 * Provides access to the underlying bytes of the IPv4 address.
69 *
70 * @return A reference to the base type containing the bytes of the IPv4 address.
71 * @remark Retrieves the data representing the IP address in **network byte order** (big-endian).
72 */
74 return _bytes;
75 }
76
77 /**
78 * Retrieves the IPv6-mapped IPv4 address.
79 *
80 * @return An IPv6 address object representing the IPv6-mapped form of the original address.
81 * @see [RFC 4291](https://datatracker.ietf.org/doc/html/rfc4291.html).
82 */
83 IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv6_address ipv6_mapped() const IPADDRESS_NOEXCEPT;
84
85protected:
86 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address_base() IPADDRESS_NOEXCEPT = default;
87
88 IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE explicit ipv4_address_base(const base_type& bytes) IPADDRESS_NOEXCEPT : _bytes(bytes) {
89 }
90
92 lhs._bytes.swap(rhs._bytes);
93 }
94
95 IPADDRESS_NODISCARD static IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE size_t hash(const base_type& bytes) IPADDRESS_NOEXCEPT {
96 return internal::calc_hash(0, size_t(bytes[0]), size_t(bytes[1]), size_t(bytes[2]), size_t(bytes[3]));
97 }
98
100 return lhs._bytes == rhs._bytes;
101 }
102
104 return lhs._bytes < rhs._bytes;
105 }
106
107#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
108
110 return lhs._bytes <=> rhs._bytes;
111 }
112
113#endif // IPADDRESS_HAS_SPACESHIP_OPERATOR
114
115private:
116 template <typename>
117 friend class ip_network_base;
118
119 base_type _bytes{};
120}; // ipv4_address_base
121
122/**
123 * Alias for the base class specialized for IPv4 address manipulation.
124 *
125 * This alias provides a convenient shorthand for the specialized `ip_address_base` class
126 * tailored for IPv4 address handling. It inherits all functionalities from the `ipv4_address_base`
127 * class, allowing for operations such as conversion, comparison, and property querying
128 * specific to IPv4 addresses.
129 */
131
132#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
133
134 /**
135 * User-defined literal for creating an ipv4_address from a fixed string at compile time.
136 *
137 * @tparam FixedString A compile-time fixed string representing the IPv4 address.
138 * @return An ipv4_address object parsed from the fixed string.
139 */
140 IPADDRESS_EXPORT template <fixed_string FixedString>
141 IPADDRESS_NODISCARD IPADDRESS_CONSTEVAL IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4() IPADDRESS_NOEXCEPT {
142 return ipv4_address::parse<FixedString>();
143 }
144
145 /**
146 * User-defined literal for creating an ipv4_address from an unsigned long long integer at compile time.
147 *
148 * @param[in] value An unsigned long long integer representing the IPv4 address in *host byte order*.
149 * @return An ipv4_address object created from the integer.
150 */
151 IPADDRESS_EXPORT IPADDRESS_NODISCARD IPADDRESS_CONSTEVAL IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(unsigned long long value) IPADDRESS_NOEXCEPT {
152 assert(value <= ipv4_address::base_all_ones && "literal integer is too long");
153 return ipv4_address::from_uint(uint32_t(value));
154 }
155
156#else // IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
157
158 /**
159 * User-defined literal for creating an ipv4_address from a string literal.
160 *
161 * @param[in] address A pointer to a character array representing the IPv4 address.
162 * @param[in] size The size of the character array.
163 * @return An ipv4_address object parsed from the string literal.
164 */
165 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(const char* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
166 return internal::parse_ip_from_literal<ipv4_address_base, char, ipv4_address::base_max_string_len>(address, size);
167 }
168
169 /**
170 * User-defined literal for creating an ipv4_address from a wide string literal.
171 *
172 * @param[in] address A pointer to a character array representing the IPv4 address.
173 * @param[in] size The size of the character array.
174 * @return An ipv4_address object parsed from the string literal.
175 */
176 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(const wchar_t* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
177 return internal::parse_ip_from_literal<ipv4_address_base, wchar_t, ipv4_address::base_max_string_len>(address, size);
178 }
179
180 /**
181 * User-defined literal for creating an ipv4_address from a UTF-16 string literal.
182 *
183 * @param[in] address A pointer to a character array representing the IPv4 address.
184 * @param[in] size The size of the character array.
185 * @return An ipv4_address object parsed from the string literal.
186 */
187 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(const char16_t* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
188 return internal::parse_ip_from_literal<ipv4_address_base, char16_t, ipv4_address::base_max_string_len>(address, size);
189 }
190
191 /**
192 * User-defined literal for creating an ipv4_address from a UTF-32 string literal.
193 *
194 * @param[in] address A pointer to a character array representing the IPv4 address.
195 * @param[in] size The size of the character array.
196 * @return An ipv4_address object parsed from the string literal.
197 */
198 IPADDRESS_EXPORT IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(const char32_t* address, size_t size) IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS {
199 return internal::parse_ip_from_literal<ipv4_address_base, char32_t, ipv4_address::base_max_string_len>(address, size);
200 }
201
202 /**
203 * User-defined literal for creating an ipv4_address from an unsigned long long integer.
204 *
205 * @param[in] value An unsigned long long integer representing the IPv4 address in host byte order.
206 * @return An ipv4_address object created from the integer.
207 */
208 IPADDRESS_EXPORT IPADDRESS_NODISCARD IPADDRESS_CONSTEXPR IPADDRESS_FORCE_INLINE ipv4_address operator""_ipv4(unsigned long long value) IPADDRESS_NOEXCEPT {
209 return ipv4_address::from_uint(uint32_t(value));
210 }
211
212#endif // IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
213
214} // namespace IPADDRESS_NAMESPACE
215
216#endif // IPADDRESS_IPV4_ADDRESS_HPP
A template base class for IP address representations.
Definition ip-address-base.hpp:56
Template base class for representing a network of IP addresses.
Definition ip-network-base.hpp:32
Represents the base class for IPv4 address manipulation.
Definition ipv4-address.hpp:30
constexpr inline uint_type to_uint() const noexcept
Converts the IPv4 address to an unsigned integer.
Definition ipv4-address.hpp:63
constexpr inline const base_type & bytes() const noexcept
Provides access to the underlying bytes of the IPv4 address.
Definition ipv4-address.hpp:73
static constexpr inline ip_address_base< ipv4_address_base > from_uint() noexcept
Creates an IPv4 address from an unsigned integer using a template parameter.
Definition ipv4-address.hpp:42
constexpr inline ipv6_address ipv6_mapped() const noexcept
Retrieves the IPv6-mapped IPv4 address.
Definition ipv6-address.hpp:941
static constexpr inline ip_address_base< ipv4_address_base > from_uint(uint_type ip) noexcept
Creates an IPv4 address from an unsigned integer.
Definition ipv4-address.hpp:53
Represents the base class for IPv6 address manipulation.
Definition ipv6-address.hpp:274
#define IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS
Definition config.hpp:96
#define IPADDRESS_EXPORT
Definition config.hpp:45
#define IPADDRESS_NODISCARD
Definition config.hpp:101
#define IPADDRESS_FORCE_INLINE
Definition config.hpp:115
#define IPADDRESS_NAMESPACE
Definition config.hpp:41
#define IPADDRESS_NOEXCEPT
Definition config.hpp:92
#define IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS
Definition config.hpp:105