11 #if defined(__MINGW64__) || defined(_WIN32)
12 #define EAI_SYSTEM EAI_FAIL
19 namespace Serialization {
25 if(endpoint.address().is_v4()) {
26 auto v4_bytes = endpoint.address().to_v4().to_bytes();
30 auto v6_bytes = endpoint.address().to_v6().to_bytes();
39 asio::ip::address_v4::bytes_type v4_bytes;
41 bufp, remainp, v4_bytes.size());
42 std::memcpy(v4_bytes.data(), bytes, v4_bytes.size());
46 asio::ip::address_v6::bytes_type v6_bytes;
48 bufp, remainp, v6_bytes.size());
49 std::memcpy(v6_bytes.data(), bytes, v6_bytes.size());
56 for(
auto& endpoint : endpoints)
63 for(
auto& endpoint : endpoints)
67 void decode(
const uint8_t** bufp,
size_t* remainp,
73 for(uint32_t i=0; i<sz; ++i)
87 for(
auto& endpoint : endpoints)
88 out << endpoint <<
',';
95 return std::find(endpoints_in.cbegin(), endpoints_in.cend(), e1)
96 != endpoints_in.cend();
100 const EndPoints& endpoints_in) noexcept {
101 for(
auto& endpoint : endpoints) {
110 if(endpoints1.size() != endpoints2.size())
112 auto it = endpoints2.cbegin();
113 for(; it != endpoints2.cend(); ++it) {
117 return it == endpoints2.cend();
122 s.reserve(23 * endpoints.
size());
123 for(
auto& endpoint : endpoints) {
124 s.append(endpoint.address().to_string());
129 std::hash<std::string> hasher;
134 return std::hash<EndPoint>()(endpoint);
142 struct sockaddr_in sa;
143 return inet_pton(AF_INET,
str.c_str(), &(sa.sin_addr));
148 struct sockaddr_in6 sa;
149 return inet_pton(AF_INET6,
str.c_str(), &(sa.sin6_addr));
154 const std::string& host,
161 for(
auto& addr : addrs) {
162 auto at = addr.find_first_of(
"-");
163 if(at != std::string::npos) {
164 ip = addr.substr(0, at);
170 endpoints.
emplace_back(asio::ip::make_address(ip.c_str()), port );
173 }
else if(!host.empty()) {
174 std::string hostname;
175 auto hostname_cfg = host;
176 auto at = host.find_first_of(
":");
177 if(at != std::string::npos) {
178 hostname = host.substr(0, at);
184 addrinfo hints, *result, *rp;
185 memset(&hints, 0,
sizeof(
struct addrinfo));
187 hints.ai_family = AF_UNSPEC;
188 hints.ai_socktype = SOCK_STREAM;
189 hints.ai_protocol = 0;
193 int err = getaddrinfo(hostname.c_str(),
nullptr, &hints, &result);
196 SWC_THROWF(err,
"Bad addr-info for host: %s", hostname.c_str());
199 for (rp = result; rp !=
nullptr; rp = rp->ai_next) {
200 char c_addr[INET6_ADDRSTRLEN];
201 const char * s =
nullptr;
202 switch(rp->ai_family) {
204 s = inet_ntop(AF_INET,
205 &
reinterpret_cast<struct sockaddr_in*
>(rp->ai_addr)->sin_addr,
206 c_addr, INET_ADDRSTRLEN);
210 s = inet_ntop(AF_INET6,
211 &
reinterpret_cast<struct sockaddr_in6*
>(rp->ai_addr)->sin6_addr,
212 c_addr, INET6_ADDRSTRLEN);
223 endpoints.
emplace_back(asio::ip::make_address(c_addr), port);
225 freeaddrinfo(result);
227 SWC_THROWF(err,
"Bad IP for host: %s", hostname.c_str());
230 if(srv && endpoints.
empty()) {
231 endpoints.
emplace_back(asio::ip::make_address(
"::"), defaul_port);
239 sort(nets, endpoints, sorted);
248 for(
auto& net : nets) {
249 for(
auto& endpoint : endpoints) {
250 if((net.is_v4 && endpoint.address().is_v4() &&
252 (!net.is_v4 && endpoint.address().is_v6() &&
258 for(
auto& endpoint : endpoints) {
265 Networks& nets, asio::error_code& ec) {
267 for(
auto& net : networks) {
268 if(net.find_first_of(
":") == std::string::npos)
278 asio::error_code& ec) {
279 for(
auto& net : networks) {
280 if(net.find_first_of(
":") == std::string::npos)
281 nets_v4.
push_back(asio::ip::make_network_v4(net, ec));
283 nets_v6.
push_back(asio::ip::make_network_v6(net, ec));
291 if(gethostname(hostname,
sizeof(hostname)) == -1) {
296 addrinfo hints, *result, *rp;
297 memset(&hints, 0,
sizeof(
struct addrinfo));
298 hints.ai_family = AF_UNSPEC;
299 hints.ai_socktype = SOCK_STREAM;
300 hints.ai_protocol = 0;
304 if((err = getaddrinfo(hostname,
nullptr, &hints, &result))) {
310 for (rp = result; rp !=
nullptr; rp = rp->ai_next) {
311 char c_addr[INET6_ADDRSTRLEN];
312 const char * s =
nullptr;
314 switch(rp->ai_family) {
316 s = inet_ntop(AF_INET,
317 &
reinterpret_cast<struct sockaddr_in*
>(rp->ai_addr)->sin_addr,
318 c_addr, INET_ADDRSTRLEN);
321 asio::ip::make_network_v4(
322 asio::ip::make_address_v4(c_addr, ec), 32));
326 s = inet_ntop(AF_INET6,
327 &
reinterpret_cast<struct sockaddr_in6*
>(rp->ai_addr)->sin6_addr,
328 c_addr, INET6_ADDRSTRLEN);
331 asio::ip::make_network_v6(
332 asio::ip::make_address_v6(c_addr, ec), 128));
351 freeaddrinfo(result);
357 if(endpoint.address().is_v4()) {
358 for(
auto& net : nets_v4)
363 if(endpoint.address().is_v6()) {
364 for(
auto& net : nets_v6) {
374 const asio::ip::network_v4& net) noexcept {
375 return endpoint.address().to_v4() == net.address() ||
376 asio::ip::make_network_v4(endpoint.address().to_v4(), 32)
381 const asio::ip::network_v6& net) noexcept {
382 return endpoint.address().to_v6() == net.address() ||
383 asio::ip::make_network_v6(endpoint.address().to_v6(), 128)