14#include <fmt/format.h>
15#include <userver/utils/fmt_compat.hpp>
17#include <userver/logging/log_helper_fwd.hpp>
19USERVER_NAMESPACE_BEGIN
39 "Your socket subsystem looks broken, please contact support chat.");
46 Sockaddr()
noexcept { ::memset(&data_, 0,
sizeof(data_)); }
52 const auto* sockaddr =
reinterpret_cast<
const struct sockaddr*>(data);
53 const auto domain =
static_cast<
AddrDomain>(sockaddr->sa_family);
54 ::memcpy(&data_, data, Sockaddr::Addrlen(domain));
76 static_assert(
sizeof(T) <=
sizeof(data_),
"Invalid socket address type");
77 return reinterpret_cast<T*>(&data_);
85 static_assert(
sizeof(T) <=
sizeof(data_),
"Invalid socket address type");
86 return reinterpret_cast<
const T*>(&data_);
90 struct sockaddr*
Data() {
return As<
struct sockaddr>(); }
93 const struct sockaddr*
Data()
const {
return As<
struct sockaddr>(); }
99 socklen_t
Capacity()
const {
return sizeof(data_); }
122 const auto res = AddrlenImpl(domain);
125 throw AddrException(fmt::format(
"Unexpected address family {}",
126 static_cast<
int>(domain)));
133 static constexpr socklen_t AddrlenImpl(
AddrDomain domain)
noexcept {
136 return sizeof(
struct sockaddr);
138 return sizeof(
struct sockaddr_in);
140 return sizeof(
struct sockaddr_in6);
142 return sizeof(
struct sockaddr_un);
149 struct sockaddr sa_any;
150 struct sockaddr_in sa_inet;
151 struct sockaddr_in6 sa_inet6;
152 struct sockaddr_un sa_unix;
165struct fmt::formatter<USERVER_NAMESPACE::engine::io::Sockaddr> {
166 static constexpr auto parse(format_parse_context&);
168 template <
typename FormatContext>
169 auto format(
const USERVER_NAMESPACE::engine::io::Sockaddr& sa,
173inline constexpr auto fmt::formatter<
174 USERVER_NAMESPACE::engine::io::Sockaddr>::parse(format_parse_context& ctx) {
175 const auto* it = ctx.begin();
176 if (it != ctx.end() && *it !=
'}') {
177 throw format_error(
"invalid Sockaddr format");
182template <
typename FormatContext>
183inline auto fmt::formatter<USERVER_NAMESPACE::engine::io::Sockaddr>::format(
184 const USERVER_NAMESPACE::engine::io::Sockaddr& sa,
186 switch (sa.Domain()) {
187 case USERVER_NAMESPACE::engine::io::AddrDomain::kInet:
188 return fmt::format_to(ctx.out(),
"{}:{}", sa.PrimaryAddressString(),
191 case USERVER_NAMESPACE::engine::io::AddrDomain::kInet6:
192 return fmt::format_to(ctx.out(),
"[{}]:{}", sa.PrimaryAddressString(),
196 return fmt::format_to(ctx.out(),
"{}", sa.PrimaryAddressString());