0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 08:12:37 +01:00

ircd::net::addrs: Split into translation unit.

This commit is contained in:
Jason Volk 2020-02-26 11:20:00 -08:00
parent 393ff914a3
commit 38aa9b6d0c
3 changed files with 132 additions and 126 deletions

View file

@ -156,6 +156,7 @@ libircd_la_SOURCES += db_port.cc
libircd_la_SOURCES += db_env.cc libircd_la_SOURCES += db_env.cc
libircd_la_SOURCES += db.cc libircd_la_SOURCES += db.cc
libircd_la_SOURCES += net.cc libircd_la_SOURCES += net.cc
libircd_la_SOURCES += net_addrs.cc
libircd_la_SOURCES += net_dns.cc libircd_la_SOURCES += net_dns.cc
libircd_la_SOURCES += net_dns_cache.cc libircd_la_SOURCES += net_dns_cache.cc
libircd_la_SOURCES += net_dns_resolver.cc libircd_la_SOURCES += net_dns_resolver.cc
@ -209,6 +210,7 @@ magic.lo: AM_CPPFLAGS := @MAGIC_CPPFLAGS@ ${AM_CPPFLAGS}
endif endif
mods.lo: AM_CPPFLAGS := ${ASIO_UNIT_CPPFLAGS} ${AM_CPPFLAGS} mods.lo: AM_CPPFLAGS := ${ASIO_UNIT_CPPFLAGS} ${AM_CPPFLAGS}
net.lo: AM_CPPFLAGS := ${ASIO_UNIT_CPPFLAGS} ${AM_CPPFLAGS} net.lo: AM_CPPFLAGS := ${ASIO_UNIT_CPPFLAGS} ${AM_CPPFLAGS}
net_addrs.lo: AM_CPPFLAGS := ${ASIO_UNIT_CPPFLAGS} ${AM_CPPFLAGS}
net_dns.lo: AM_CPPFLAGS := ${ASIO_UNIT_CPPFLAGS} ${AM_CPPFLAGS} net_dns.lo: AM_CPPFLAGS := ${ASIO_UNIT_CPPFLAGS} ${AM_CPPFLAGS}
net_dns_resolver.lo: AM_CPPFLAGS := ${ASIO_UNIT_CPPFLAGS} ${AM_CPPFLAGS} net_dns_resolver.lo: AM_CPPFLAGS := ${ASIO_UNIT_CPPFLAGS} ${AM_CPPFLAGS}
openssl.lo: AM_CPPFLAGS := @SSL_CPPFLAGS@ @CRYPTO_CPPFLAGS@ ${AM_CPPFLAGS} openssl.lo: AM_CPPFLAGS := @SSL_CPPFLAGS@ @CRYPTO_CPPFLAGS@ ${AM_CPPFLAGS}

View file

@ -8,8 +8,6 @@
// copyright notice and this permission notice is present in all copies. The // copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file. // full license for this software is available in the LICENSE file.
#include <RB_INC_IFADDRS_H
namespace ircd::net namespace ircd::net
{ {
ctx::dock dock; ctx::dock dock;
@ -748,130 +746,6 @@ ircd::net::open(socket &socket,
connector({}, opts.hostport, opts.ipport); connector({}, opts.hostport, opts.ipport);
} }
///////////////////////////////////////////////////////////////////////////////
//
// net/addrs.h
//
#ifdef HAVE_IFADDRS_H
bool
ircd::net::addrs::has_usable_ipv6_interface()
try
{
return !for_each([](const addr &a)
{
if(a.family != AF_INET6)
return true;
if(a.scope_id != 0) // global scope
return true;
if(~a.flags & IFF_UP) // not up
return true;
if(a.flags & IFF_LOOPBACK) // not usable
return true;
// return false to break
return false;
});
}
catch(const std::exception &e)
{
log::error
{
log, "Failed to check for usable IPv6 interfaces :%s",
e.what()
};
return false;
}
#else
bool
ircd::net::addrs::has_usable_ipv6_interface()
{
return false;
}
#endif
#ifdef HAVE_IFADDRS_H
bool
__attribute__((optimize(0))) //XXX: trouble
ircd::net::addrs::for_each(const closure &closure)
{
return for_each(raw_closure{[&closure]
(const struct ::ifaddrs *const &ifa)
{
addr a;
a.name = ifa->ifa_name;
a.flags = ifa->ifa_flags;
if(ifa->ifa_addr) switch(ifa->ifa_addr->sa_family)
{
case AF_INET6:
{
const auto sin(reinterpret_cast<const struct ::sockaddr_in6 *>(ifa->ifa_addr));
const auto ip(reinterpret_cast<const uint128_t *>(sin->sin6_addr.s6_addr));
a.family = sin->sin6_family;
a.scope_id = sin->sin6_scope_id;
a.flowinfo = sin->sin6_flowinfo;
a.address =
{
ntoh(*ip), sin->sin6_port
};
break;
}
case AF_INET:
{
const auto &sin(reinterpret_cast<const struct ::sockaddr_in *>(ifa->ifa_addr));
a.family = sin->sin_family;
a.address =
{
ntoh(sin->sin_addr.s_addr), sin->sin_port
};
break;
}
default:
return true;
}
return closure(a);
}});
}
#else
bool
ircd::net::addrs::for_each(const closure &closure)
{
return true;
}
#endif
#ifdef HAVE_IFADDRS_H
bool
ircd::net::addrs::for_each(const raw_closure &closure)
{
struct ::ifaddrs *ifap_;
syscall(::getifaddrs, &ifap_);
const custom_ptr<struct ::ifaddrs> ifap
{
ifap_, ::freeifaddrs
};
for(auto ifa(ifap.get()); ifa; ifa = ifa->ifa_next)
if(!closure(ifa))
return false;
return true;
}
#else
bool
ircd::net::addrs::for_each(const raw_closure &closure)
{
return true;
}
#endif
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// //
// net/sopts.h // net/sopts.h

130
ircd/net_addrs.cc Normal file
View file

@ -0,0 +1,130 @@
// The Construct
//
// Copyright (C) The Construct Developers, Authors & Contributors
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#include <RB_INC_IFADDRS_H
#ifdef HAVE_IFADDRS_H
bool
ircd::net::addrs::has_usable_ipv6_interface()
try
{
return !for_each([](const addr &a)
{
if(a.family != AF_INET6)
return true;
if(a.scope_id != 0) // global scope
return true;
if(~a.flags & IFF_UP) // not up
return true;
if(a.flags & IFF_LOOPBACK) // not usable
return true;
// return false to break
return false;
});
}
catch(const std::exception &e)
{
log::error
{
log, "Failed to check for usable IPv6 interfaces :%s",
e.what()
};
return false;
}
#else
bool
ircd::net::addrs::has_usable_ipv6_interface()
{
return false;
}
#endif
#ifdef HAVE_IFADDRS_H
bool
__attribute__((optimize(0))) //XXX: trouble
ircd::net::addrs::for_each(const closure &closure)
{
return for_each(raw_closure{[&closure]
(const struct ::ifaddrs *const &ifa)
{
addr a;
a.name = ifa->ifa_name;
a.flags = ifa->ifa_flags;
if(ifa->ifa_addr) switch(ifa->ifa_addr->sa_family)
{
case AF_INET6:
{
const auto sin(reinterpret_cast<const struct ::sockaddr_in6 *>(ifa->ifa_addr));
const auto ip(reinterpret_cast<const uint128_t *>(sin->sin6_addr.s6_addr));
a.family = sin->sin6_family;
a.scope_id = sin->sin6_scope_id;
a.flowinfo = sin->sin6_flowinfo;
a.address =
{
ntoh(*ip), sin->sin6_port
};
break;
}
case AF_INET:
{
const auto &sin(reinterpret_cast<const struct ::sockaddr_in *>(ifa->ifa_addr));
a.family = sin->sin_family;
a.address =
{
ntoh(sin->sin_addr.s_addr), sin->sin_port
};
break;
}
default:
return true;
}
return closure(a);
}});
}
#else
bool
ircd::net::addrs::for_each(const closure &closure)
{
return true;
}
#endif
#ifdef HAVE_IFADDRS_H
bool
ircd::net::addrs::for_each(const raw_closure &closure)
{
struct ::ifaddrs *ifap_;
syscall(::getifaddrs, &ifap_);
const custom_ptr<struct ::ifaddrs> ifap
{
ifap_, ::freeifaddrs
};
for(auto ifa(ifap.get()); ifa; ifa = ifa->ifa_next)
if(!closure(ifa))
return false;
return true;
}
#else
bool
ircd::net::addrs::for_each(const raw_closure &closure)
{
return true;
}
#endif