diff options
author | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2015-03-08 17:54:44 +0100 |
---|---|---|
committer | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2015-03-08 17:54:44 +0100 |
commit | 20da3d34a51afb15145c9a02710a7a627163cd23 (patch) | |
tree | 9779effb0246d3e59b27cb7a2f7314e6f747b600 /src/OSSupport | |
parent | Network: Replaced magic number with named constant. (diff) | |
download | cuberite-20da3d34a51afb15145c9a02710a7a627163cd23.tar cuberite-20da3d34a51afb15145c9a02710a7a627163cd23.tar.gz cuberite-20da3d34a51afb15145c9a02710a7a627163cd23.tar.bz2 cuberite-20da3d34a51afb15145c9a02710a7a627163cd23.tar.lz cuberite-20da3d34a51afb15145c9a02710a7a627163cd23.tar.xz cuberite-20da3d34a51afb15145c9a02710a7a627163cd23.tar.zst cuberite-20da3d34a51afb15145c9a02710a7a627163cd23.zip |
Diffstat (limited to '')
-rw-r--r-- | src/OSSupport/NetworkInterfaceEnum.cpp | 58 |
1 files changed, 54 insertions, 4 deletions
diff --git a/src/OSSupport/NetworkInterfaceEnum.cpp b/src/OSSupport/NetworkInterfaceEnum.cpp index 38d946393..94edd3bfb 100644 --- a/src/OSSupport/NetworkInterfaceEnum.cpp +++ b/src/OSSupport/NetworkInterfaceEnum.cpp @@ -10,6 +10,10 @@ #include <IPHlpApi.h> #pragma comment(lib, "IPHLPAPI.lib") #else // _WIN32 + #include <sys/types.h> + #include <ifaddrs.h> + #include <netinet/in.h> + #include <arpa/inet.h> #endif // else _WIN32 @@ -29,7 +33,7 @@ public: { printf(" %s\n", ip.c_str()); } - printf("done.\n"); + printf("Done.\n"); } } g_EnumIPAddressTest; @@ -65,10 +69,36 @@ static AString PrintAddress(SOCKET_ADDRESS & a_Addr) break; } } - return AString(IP); + return IP; } -#endif // _WIN32 +#else // _WIN32 + +static AString PrintAddress(ifaddrs * InterfaceAddress) +{ + switch (InterfaceAddress->ifa_addr->sa_family) + { + case AF_INET: + { // IPv4 + char AddressBuffer[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &(reinterpret_cast<struct sockaddr_in *>(reinterpret_cast<void *>(InterfaceAddress->ifa_addr))->sin_addr), AddressBuffer, INET_ADDRSTRLEN); + return AddressBuffer; + } + case AF_INET6: + { // IPv6 + char AddressBuffer[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, &(reinterpret_cast<struct sockaddr_in6 *>(reinterpret_cast<void *>(InterfaceAddress->ifa_addr))->sin6_addr), AddressBuffer, INET6_ADDRSTRLEN); + return AddressBuffer; + } + default: + { + LOG("Unknown address family: %i", InterfaceAddress->ifa_addr->sa_family); + return ""; + } + } +} + +#endif // else _WIN32 @@ -117,7 +147,27 @@ AStringVector cNetwork::EnumLocalIPAddresses(void) #else // _WIN32 - // TODO: Enumerate network interfaces on Linux + struct ifaddrs * ifAddrStruct = nullptr; + getifaddrs(&ifAddrStruct); + + for (auto ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) + { + if (ifa->ifa_addr == nullptr) + { + continue; + } + + auto Address = PrintAddress(ifa); + if (!Address.empty()) + { + res.emplace_back(Address); + } + } + + if (ifAddrStruct != nullptr) + { + freeifaddrs(ifAddrStruct); + } #endif // else _WIN32 |