8 #include "event2/util.h"
12 #pragma comment(lib, "IPHLPAPI.lib")
13 #elif !defined(ANDROID)
14 #include <sys/types.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
30 switch (a_Addr.lpSockaddr->sa_family)
34 auto sin =
reinterpret_cast<const sockaddr_in *
>(a_Addr.lpSockaddr);
35 evutil_inet_ntop(a_Addr.lpSockaddr->sa_family, &(sin->sin_addr), IP,
sizeof(IP));
40 auto sin =
reinterpret_cast<const sockaddr_in6 *
>(a_Addr.lpSockaddr);
41 evutil_inet_ntop(a_Addr.lpSockaddr->sa_family, &(sin->sin6_addr), IP,
sizeof(IP));
53 #elif !defined(ANDROID)
57 switch (InterfaceAddress->ifa_addr->sa_family)
61 char AddressBuffer[INET_ADDRSTRLEN];
62 sockaddr_in InternetSocket;
64 std::memcpy(&InternetSocket, InterfaceAddress->ifa_addr,
sizeof(InternetSocket));
65 inet_ntop(AF_INET, &InternetSocket.sin_addr, AddressBuffer, INET_ADDRSTRLEN);
70 char AddressBuffer[INET6_ADDRSTRLEN];
71 sockaddr_in6 InternetSocket;
73 std::memcpy(&InternetSocket, InterfaceAddress->ifa_addr,
sizeof(InternetSocket));
74 inet_ntop(AF_INET6, &InternetSocket.sin6_addr, AddressBuffer, INET6_ADDRSTRLEN);
79 LOG(
"Unknown address family: %i", InterfaceAddress->ifa_addr->sa_family);
99 PIP_ADAPTER_ADDRESSES pAddresses =
reinterpret_cast<PIP_ADAPTER_ADDRESSES
>(&buffer);
100 ULONG outBufLen =
sizeof(buffer);
101 DWORD dwRetVal = GetAdaptersAddresses(
103 GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
nullptr,
104 pAddresses, &outBufLen
106 if (dwRetVal != ERROR_SUCCESS)
108 LOG(
"GetAdaptersAddresses() failed: %u", dwRetVal);
113 for (
auto pCurrAddresses = pAddresses; pCurrAddresses !=
nullptr; pCurrAddresses = pCurrAddresses->Next)
115 if (pCurrAddresses->OperStatus != IfOperStatusUp)
122 for (
auto pUnicast = pCurrAddresses->FirstUnicastAddress; pUnicast !=
nullptr; pUnicast = pUnicast->Next)
125 if (!Address.empty())
127 res.push_back(Address);
132 #elif !defined(ANDROID)
134 struct ifaddrs * ifAddrStruct =
nullptr;
135 getifaddrs(&ifAddrStruct);
137 for (
auto ifa = ifAddrStruct; ifa !=
nullptr; ifa = ifa->ifa_next)
139 if (ifa->ifa_addr ==
nullptr)
145 if (!Address.empty())
147 res.emplace_back(Address);
151 if (ifAddrStruct !=
nullptr)
153 freeifaddrs(ifAddrStruct);
#define KiB
Allows arithmetic expressions like "32 KiB" (but consider using parenthesis around it,...
void LOG(std::string_view a_Format, const Args &... args)
static AString PrintAddress(ifaddrs *InterfaceAddress)
std::vector< AString > AStringVector
static AStringVector EnumLocalIPAddresses(void)
Returns all local IP addresses for network interfaces currently available.