14#include <fmt/format.h>
15#include <userver/utils/fmt_compat.hpp>
17#include <userver/logging/log_helper_fwd.hpp>
19USERVER_NAMESPACE_BEGIN
26 using std::runtime_error::runtime_error;
39 "Your socket subsystem looks broken, please contact support chat.");
46 Sockaddr() { ::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));
62 static_assert(
sizeof(T) <=
sizeof(data_),
"Invalid socket address type");
63 return reinterpret_cast<T*>(&data_);
71 static_assert(
sizeof(T) <=
sizeof(data_),
"Invalid socket address type");
72 return reinterpret_cast<
const T*>(&data_);
76 struct sockaddr*
Data() {
return As<
struct sockaddr>(); }
79 const struct sockaddr*
Data()
const {
return As<
struct sockaddr>(); }
85 socklen_t
Capacity()
const {
return sizeof(data_); }
108 const auto res = AddrlenImpl(domain);
111 throw AddrException(fmt::format(
"Unexpected address family {}",
112 static_cast<
int>(domain)));
119 static constexpr socklen_t AddrlenImpl(
AddrDomain domain)
noexcept {
122 return sizeof(
struct sockaddr);
124 return sizeof(
struct sockaddr_in);
126 return sizeof(
struct sockaddr_in6);
128 return sizeof(
struct sockaddr_un);
135 struct sockaddr sa_any;
136 struct sockaddr_in sa_inet;
137 struct sockaddr_in6 sa_inet6;
138 struct sockaddr_un sa_unix;
151struct fmt::formatter<USERVER_NAMESPACE::engine::io::Sockaddr> {
152 static constexpr auto parse(format_parse_context&);
154 template <
typename FormatContext>
155 auto format(
const USERVER_NAMESPACE::engine::io::Sockaddr& sa,
159inline constexpr auto fmt::formatter<
160 USERVER_NAMESPACE::engine::io::Sockaddr>::parse(format_parse_context& ctx) {
161 const auto* it = ctx.begin();
162 if (it != ctx.end() && *it !=
'}') {
163 throw format_error(
"invalid Sockaddr format");
168template <
typename FormatContext>
169inline auto fmt::formatter<USERVER_NAMESPACE::engine::io::Sockaddr>::format(
170 const USERVER_NAMESPACE::engine::io::Sockaddr& sa,
172 switch (sa.Domain()) {
173 case USERVER_NAMESPACE::engine::io::AddrDomain::kInet:
174 return fmt::format_to(ctx.out(),
"{}:{}", sa.PrimaryAddressString(),
177 case USERVER_NAMESPACE::engine::io::AddrDomain::kInet6:
178 return fmt::format_to(ctx.out(),
"[{}]:{}", sa.PrimaryAddressString(),
182 return fmt::format_to(ctx.out(),
"{}", sa.PrimaryAddressString());