Skip to content

Commit

Permalink
GetLocalAddresses function
Browse files Browse the repository at this point in the history
  • Loading branch information
laanwj committed Apr 30, 2024
1 parent c418f2b commit a263748
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions src/ipv6-pinhole-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,43 @@ bool OpenPinhole(const sockaddr_in6 &gateway, const struct in6_addr &my_addr, ui
return true;
}

// TODO share with Discover()
//! Return potentially routable local IPv4 and IPv6 network addresses.
std::vector<CNetAddr> GetLocalAddresses()
{
std::vector<CNetAddr> addresses;
#ifdef WIN32
char pszHostName[256] = "";
if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
{
addresses = LookupHost(pszHostName, 0, true);
}
#elif (HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS)
struct ifaddrs* myaddrs;
if (getifaddrs(&myaddrs) == 0)
{
for (struct ifaddrs* ifa = myaddrs; ifa != nullptr; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == nullptr) continue;
if ((ifa->ifa_flags & IFF_UP) == 0) continue;
if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) continue;
if (ifa->ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
addresses.emplace_back(s4->sin_addr);
}
else if (ifa->ifa_addr->sa_family == AF_INET6)
{
struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
addresses.emplace_back(s6->sin6_addr);
}
}
freeifaddrs(myaddrs);
}
#endif
return addresses;
}

int main(int argc, char **argv)
{
// Needed to get LogPrintf working.
Expand All @@ -221,8 +258,6 @@ int main(int argc, char **argv)
LogInstance().EnableCategory(BCLog::NET);
LogInstance().SetCategoryLogLevel("net", "trace");

std::vector<struct in6_addr> my_addrs;

LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "*************************************************\n");
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "PCP IPv6 pinholing test\n");
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "*************************************************\n");
Expand All @@ -233,27 +268,15 @@ int main(int argc, char **argv)
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, " nc -v6 <addr> 1234\n");
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "\n");

struct ifaddrs* myaddrs;
if (getifaddrs(&myaddrs) == 0)
{
for (struct ifaddrs* ifa = myaddrs; ifa != nullptr; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == nullptr) continue;
if ((ifa->ifa_flags & IFF_UP) == 0) continue;
if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) continue;
if (ifa->ifa_addr->sa_family == AF_INET6)
{
struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
if ((s6->sin6_addr.s6_addr[0] & 0xe0) != 0x20) { // Not gLobal unicast.
continue;
}
my_addrs.push_back(s6->sin6_addr);
}
}
freeifaddrs(myaddrs);
// Collect routable local IPv6 addresses.
std::vector<struct in6_addr> my_addrs;
for (const auto &net_addr: GetLocalAddresses()) {
struct in6_addr addr;
if (net_addr.IsRoutable() && net_addr.GetIn6Addr(&addr))
my_addrs.push_back(addr);
}
if (my_addrs.empty()) {
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp6: Did not find any IPv6 address\n");
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp6: Did not find any routable local IPv6 address\n");
exit(1);
}
std::optional<sockaddr_in6> gateway = FindIPv6DefaultGateway();
Expand Down

0 comments on commit a263748

Please sign in to comment.