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>
37#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 1400000)
39std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
42 auto sock{
CreateSock(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)};
57 const size_t dst_data_len = family == AF_INET ? 4 : 16;
59 request.hdr.nlmsg_type = RTM_GETROUTE;
60 request.hdr.nlmsg_flags = NLM_F_REQUEST;
65 request.hdr.nlmsg_flags |= NLM_F_DUMP;
67 request.hdr.nlmsg_len = NLMSG_LENGTH(
sizeof(rtmsg) +
sizeof(nlattr) + dst_data_len);
68 request.hdr.nlmsg_seq = 0;
69 request.data.rtm_family = family;
70 request.data.rtm_dst_len = 0;
75 request.data.rtm_flags = RTM_F_PREFIX;
77 request.dst_hdr.nla_type = RTA_DST;
78 request.dst_hdr.nla_len =
sizeof(nlattr) + dst_data_len;
80 if (sock->Send(&request, request.hdr.nlmsg_len, 0) !=
static_cast<ssize_t
>(request.hdr.nlmsg_len)) {
89 recv_result = sock->Recv(response,
sizeof(response), 0);
90 }
while (recv_result < 0 && (errno == EINTR || errno == EAGAIN));
91 if (recv_result < 0) {
96 for (nlmsghdr* hdr = (nlmsghdr*)response; NLMSG_OK(hdr, recv_result); hdr = NLMSG_NEXT(hdr, recv_result)) {
97 rtmsg* r = (rtmsg*)NLMSG_DATA(hdr);
98 int remaining_len = RTM_PAYLOAD(hdr);
101 rtattr *rta_gateway =
nullptr;
103 for (rtattr* attr = RTM_RTA(r); RTA_OK(attr, remaining_len); attr = RTA_NEXT(attr, remaining_len)) {
104 if (attr->rta_type == RTA_GATEWAY) {
106 }
else if (attr->rta_type == RTA_OIF &&
sizeof(
int) == RTA_PAYLOAD(attr)) {
107 std::memcpy(&scope_id, RTA_DATA(attr),
sizeof(scope_id));
112 if (rta_gateway !=
nullptr) {
113 if (family == AF_INET &&
sizeof(in_addr) == RTA_PAYLOAD(rta_gateway)) {
115 std::memcpy(&gw, RTA_DATA(rta_gateway),
sizeof(gw));
117 }
else if (family == AF_INET6 &&
sizeof(in6_addr) == RTA_PAYLOAD(rta_gateway)) {
119 std::memcpy(&gw, RTA_DATA(rta_gateway),
sizeof(gw));
130std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
132 NET_LUID interface_luid = {};
133 SOCKADDR_INET destination_address = {};
134 MIB_IPFORWARD_ROW2 best_route = {};
135 SOCKADDR_INET best_source_address = {};
136 DWORD best_if_idx = 0;
140 destination_address.si_family = family;
141 status = GetBestInterfaceEx((sockaddr*)&destination_address, &best_if_idx);
142 if (status != NO_ERROR) {
149 status = GetBestRoute2(&interface_luid, best_if_idx,
nullptr, &destination_address, 0, &best_route, &best_source_address);
150 if (status != NO_ERROR) {
156 Assume(best_route.NextHop.si_family == family);
157 if (family == AF_INET) {
158 return CNetAddr(best_route.NextHop.Ipv4.sin_addr);
159 }
else if(family == AF_INET6) {
160 return CNetAddr(best_route.NextHop.Ipv6.sin6_addr, best_route.InterfaceIndex);
165#elif defined(__APPLE__)
167#define ROUNDUP32(a) \
168 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
170std::optional<CNetAddr> FromSockAddr(
const struct sockaddr* addr)
174 if (!(addr->sa_family == AF_INET && addr->sa_len ==
sizeof(
struct sockaddr_in)) &&
175 !(addr->sa_family == AF_INET6 && addr->sa_len ==
sizeof(
struct sockaddr_in6))) {
188std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
191 int mib[] = {CTL_NET, PF_ROUTE, 0, family, NET_RT_FLAGS, RTF_GATEWAY};
194 if (sysctl(mib,
sizeof(mib) /
sizeof(
int),
nullptr, &l,
nullptr, 0) < 0) {
198 std::vector<std::byte> buf(l);
199 if (sysctl(mib,
sizeof(mib) /
sizeof(
int), buf.data(), &l,
nullptr, 0) < 0) {
204 for (
size_t msg_pos = 0; msg_pos < buf.size(); ) {
205 if ((msg_pos +
sizeof(rt_msghdr)) > buf.size())
return std::nullopt;
206 const struct rt_msghdr* rt = (
const struct rt_msghdr*)(buf.data() + msg_pos);
207 const size_t next_msg_pos = msg_pos + rt->rtm_msglen;
208 if (rt->rtm_msglen <
sizeof(rt_msghdr) || next_msg_pos > buf.size())
return std::nullopt;
211 size_t sa_pos = msg_pos +
sizeof(
struct rt_msghdr);
212 std::optional<CNetAddr> dst, gateway;
213 for (
int i = 0; i < RTAX_MAX; i++) {
214 if (rt->rtm_addrs & (1 << i)) {
216 if ((sa_pos + 2) > next_msg_pos)
return std::nullopt;
217 const struct sockaddr* sa = (
const struct sockaddr*)(buf.data() + sa_pos);
218 if ((sa_pos + sa->sa_len) > next_msg_pos)
return std::nullopt;
220 dst = FromSockAddr(sa);
221 }
else if (i == RTAX_GATEWAY) {
222 gateway = FromSockAddr(sa);
226 sa_pos += ROUNDUP32(sa->sa_len);
230 if (dst && gateway && dst->IsBindAny()) {
234 msg_pos = next_msg_pos;
242std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t)
264 std::optional<CNetAddr>
ret = QueryDefaultGatewayImpl(family);
268 if (
ret && !
ret->IsBindAny()) {
277 std::vector<CNetAddr> addresses;
279 char pszHostName[256] =
"";
280 if (gethostname(pszHostName,
sizeof(pszHostName)) !=
SOCKET_ERROR) {
283#elif (HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS)
284 struct ifaddrs* myaddrs;
285 if (getifaddrs(&myaddrs) == 0) {
286 for (
struct ifaddrs* ifa = myaddrs; ifa !=
nullptr; ifa = ifa->ifa_next)
288 if (ifa->ifa_addr ==
nullptr)
continue;
289 if ((ifa->ifa_flags & IFF_UP) == 0)
continue;
290 if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
continue;
291 if (ifa->ifa_addr->sa_family == AF_INET) {
292 struct sockaddr_in* s4 = (
struct sockaddr_in*)(ifa->ifa_addr);
293 addresses.emplace_back(s4->sin_addr);
294 }
else if (ifa->ifa_addr->sa_family == AF_INET6) {
295 struct sockaddr_in6* s6 = (
struct sockaddr_in6*)(ifa->ifa_addr);
296 addresses.emplace_back(s6->sin6_addr);
299 freeifaddrs(myaddrs);
#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)
#define LogPrintLevel(category, level,...)
std::vector< CNetAddr > LookupHost(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
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.