5#include <bitcoin-build-config.h>
16#include <linux/rtnetlink.h>
17#elif defined(__FreeBSD__)
19#if __FreeBSD_version >= 1400000
21#define typeof __typeof
22#include <netlink/netlink.h>
23#include <netlink/netlink_route.h>
27#elif defined(__APPLE__)
29#include <sys/sysctl.h>
50 switch (addr->sa_family) {
68#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 1400000)
102 request.hdr.nlmsg_seq = 0;
103 request.data.rtm_family =
family;
104 request.data.rtm_dst_len = 0;
111 request.dst_hdr.nla_type =
RTA_DST;
114 if (sock->Send(&request, request.hdr.nlmsg_len, 0) !=
static_cast<ssize_t>(request.hdr.nlmsg_len)) {
159 if (r->rtm_dst_len != 0) {
215 LogError(
"Could not get best route for default route for interface index %d: %s\n",
229#elif defined(__APPLE__)
231#define ROUNDUP32(a) \
232 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
241 if (
sysctl(
mib,
sizeof(
mib) /
sizeof(
int),
nullptr, &
l,
nullptr, 0) < 0) {
245 std::vector<std::byte> buf(
l);
246 if (
sysctl(
mib,
sizeof(
mib) /
sizeof(
int), buf.data(), &
l,
nullptr, 0) < 0) {
260 for (
int i = 0; i <
RTAX_MAX; i++) {
261 if (
rt->rtm_addrs & (1 << i)) {
315 if (
ret && !
ret->IsBindAny()) {
328 std::vector<std::byte>
out_buf(15000, {});
368#elif defined(HAVE_IFADDRS)
373 if (
ifa->ifa_addr ==
nullptr)
continue;
374 if ((
ifa->ifa_flags &
IFF_UP) == 0)
continue;
377 if (std::optional<CNetAddr> addr =
FromSockAddr(
ifa->ifa_addr, std::nullopt)) {
#define Assume(val)
Assume is the identity function.
A combination of a network address (CNetAddr) and a (TCP) port.
bool SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
Set CService from a network sockaddr.
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
Socket factory.
std::vector< CNetAddr > GetLocalAddresses()
Return all local non-loopback IPv4 and IPv6 network addresses.
std::optional< CNetAddr > QueryDefaultGateway(Network network)
Query the OS for the default gateway for network.
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
std::string SysErrorString(int err)
Return system error string from errno value.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.