Skip to content

Commit d72df63

Browse files
committed
net: Use GetLocalAddresses in Discover
This has the same code, it's unnecessary to duplicate it.
1 parent e020304 commit d72df63

File tree

2 files changed

+4
-40
lines changed

2 files changed

+4
-40
lines changed

src/net.cpp

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <random.h>
2929
#include <scheduler.h>
3030
#include <util/fs.h>
31+
#include <util/netif.h>
3132
#include <util/sock.h>
3233
#include <util/strencodings.h>
3334
#include <util/thread.h>
@@ -3118,46 +3119,10 @@ void Discover()
31183119
if (!fDiscover)
31193120
return;
31203121

3121-
#ifdef WIN32
3122-
// Get local host IP
3123-
char pszHostName[256] = "";
3124-
if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
3125-
{
3126-
const std::vector<CNetAddr> addresses{LookupHost(pszHostName, 0, true)};
3127-
for (const CNetAddr& addr : addresses)
3128-
{
3129-
if (AddLocal(addr, LOCAL_IF))
3130-
LogPrintf("%s: %s - %s\n", __func__, pszHostName, addr.ToStringAddr());
3131-
}
3122+
for (const CNetAddr &addr: GetLocalAddresses()) {
3123+
if (AddLocal(addr, LOCAL_IF))
3124+
LogPrintf("%s: %s\n", __func__, addr.ToStringAddr());
31323125
}
3133-
#elif (HAVE_DECL_GETIFADDRS && HAVE_DECL_FREEIFADDRS)
3134-
// Get local host ip
3135-
struct ifaddrs* myaddrs;
3136-
if (getifaddrs(&myaddrs) == 0)
3137-
{
3138-
for (struct ifaddrs* ifa = myaddrs; ifa != nullptr; ifa = ifa->ifa_next)
3139-
{
3140-
if (ifa->ifa_addr == nullptr) continue;
3141-
if ((ifa->ifa_flags & IFF_UP) == 0) continue;
3142-
if ((ifa->ifa_flags & IFF_LOOPBACK) != 0) continue;
3143-
if (ifa->ifa_addr->sa_family == AF_INET)
3144-
{
3145-
struct sockaddr_in* s4 = (struct sockaddr_in*)(ifa->ifa_addr);
3146-
CNetAddr addr(s4->sin_addr);
3147-
if (AddLocal(addr, LOCAL_IF))
3148-
LogPrintf("%s: IPv4 %s: %s\n", __func__, ifa->ifa_name, addr.ToStringAddr());
3149-
}
3150-
else if (ifa->ifa_addr->sa_family == AF_INET6)
3151-
{
3152-
struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
3153-
CNetAddr addr(s6->sin6_addr);
3154-
if (AddLocal(addr, LOCAL_IF))
3155-
LogPrintf("%s: IPv6 %s: %s\n", __func__, ifa->ifa_name, addr.ToStringAddr());
3156-
}
3157-
}
3158-
freeifaddrs(myaddrs);
3159-
}
3160-
#endif
31613126
}
31623127

31633128
void CConnman::SetNetworkActive(bool active)

src/util/netif.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
//! Returns std::nullopt if it cannot be found, or there is no support for this OS.
1414
std::optional<CNetAddr> QueryDefaultGateway(Network network);
1515

16-
// TODO share with Discover()
1716
//! Return all local non-loopback IPv4 and IPv6 network addresses.
1817
std::vector<CNetAddr> GetLocalAddresses();
1918

0 commit comments

Comments
 (0)