4 #include "fly/net/ipv4_address.hpp"
5 #include "fly/net/ipv6_address.hpp"
6 #include "fly/net/socket/concepts.hpp"
7 #include "fly/net/socket/types.hpp"
8 #include "fly/types/string/lexer.hpp"
9 #include "fly/types/string/string.hpp"
15 #include <type_traits>
26 template <IPAddress IPAddressType>
30 using address_type = IPAddressType;
62 static constexpr
bool is_ipv4();
67 static constexpr
bool is_ipv6();
79 static constexpr std::optional<Endpoint>
from_string(std::string_view endpoint);
84 constexpr
const IPAddressType &
address()
const;
89 constexpr port_type
port()
const;
91 #if defined(FLY_MACOS)
96 constexpr
bool operator==(
const Endpoint &endpoint)
const;
97 constexpr
bool operator!=(
const Endpoint &endpoint)
const;
98 constexpr
bool operator<(
const Endpoint &endpoint)
const;
99 constexpr
bool operator<=(
const Endpoint &endpoint)
const;
100 constexpr
bool operator>(
const Endpoint &endpoint)
const;
101 constexpr
bool operator>=(
const Endpoint &endpoint)
const;
111 IPAddressType m_address {};
112 port_type m_port {0};
116 template <IPAddress IPAddressType>
124 template <IPAddress IPAddressType>
132 template <IPAddress IPAddressType>
135 return std::is_same_v<IPAddressType, IPv4Address>;
139 template <IPAddress IPAddressType>
142 return std::is_same_v<IPAddressType, IPv6Address>;
146 template <IPAddress IPAddressType>
147 constexpr std::optional<Endpoint<IPAddressType>>
150 constexpr
const auto s_max16 =
151 static_cast<std::size_t
>(std::numeric_limits<std::uint16_t>::max());
152 constexpr
const auto s_colon =
':';
154 const std::size_t separator = endpoint.find_last_of(s_colon);
156 if ((separator == std::string_view::npos) || (separator == 0) || (separator == endpoint.size()))
161 auto address_view = endpoint.substr(0, separator);
162 auto port_view = endpoint.substr(separator + 1);
164 if constexpr (is_ipv6())
166 constexpr
const auto s_left_bracket =
'[';
167 constexpr
const auto s_right_bracket =
']';
169 if (address_view.starts_with(s_left_bracket) && address_view.ends_with(s_right_bracket))
171 address_view = address_view.substr(1, address_view.size() - 2);
181 auto address = IPAddressType::from_string(std::move(address_view));
184 if (!address || !port || (*port > s_max16) || lexer.
peek())
189 return Endpoint(*std::move(address),
static_cast<port_type
>(*port));
193 template <IPAddress IPAddressType>
200 template <IPAddress IPAddressType>
206 #if defined(FLY_MACOS)
209 template <IPAddress IPAddressType>
212 return (m_address == endpoint.m_address) && (m_port == endpoint.m_port);
216 template <IPAddress IPAddressType>
217 constexpr
bool Endpoint<IPAddressType>::operator!=(
const Endpoint<IPAddressType> &endpoint)
const
219 return !(*
this == endpoint);
223 template <IPAddress IPAddressType>
224 constexpr
bool Endpoint<IPAddressType>::operator<(
const Endpoint<IPAddressType> &endpoint)
const
226 if (m_address < endpoint.m_address)
231 return m_port < endpoint.m_port;
235 template <IPAddress IPAddressType>
236 constexpr
bool Endpoint<IPAddressType>::operator<=(
const Endpoint<IPAddressType> &endpoint)
const
238 return !(endpoint < *
this);
242 template <IPAddress IPAddressType>
243 constexpr
bool Endpoint<IPAddressType>::operator>(
const Endpoint<IPAddressType> &endpoint)
const
245 return !(*
this <= endpoint);
249 template <IPAddress IPAddressType>
250 constexpr
bool Endpoint<IPAddressType>::operator>=(
const Endpoint<IPAddressType> &endpoint)
const
252 return !(*
this < endpoint);
271 template <
typename FormatContext>
290 template <
typename FormatContext>
constexpr std::optional< std::uintmax_t > consume_number()
Definition: lexer.hpp:227
constexpr std::optional< CharType > peek(std::size_t offset=0)
Definition: lexer.hpp:190
static void format_to(OutputIterator output, FormatString< ParameterTypes... > &&fmt, ParameterTypes &&...parameters)
Definition: string.hpp:939
Definition: endpoint.hpp:28
static constexpr bool is_ipv4()
Definition: endpoint.hpp:133
auto operator<=>(const Endpoint &) const =default
static constexpr std::optional< Endpoint > from_string(std::string_view endpoint)
Definition: endpoint.hpp:148
static constexpr bool is_ipv6()
Definition: endpoint.hpp:140
constexpr port_type port() const
Definition: endpoint.hpp:201
constexpr const IPAddressType & address() const
Definition: endpoint.hpp:194