mirror of
https://github.com/celisej567/mcpe.git
synced 2026-09-11 20:11:18 +03:00
109
thirdparty/raknet/RakNetSocket2_Windows_Linux.cpp
vendored
109
thirdparty/raknet/RakNetSocket2_Windows_Linux.cpp
vendored
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
|
||||
#include "EmptyHeader.h"
|
||||
#include "../../compat/PlatformDefinitions.h"
|
||||
|
||||
#ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS
|
||||
|
||||
@@ -17,6 +18,97 @@
|
||||
|
||||
#if !defined(WINDOWS_STORE_RT) && !defined(__native_client__)
|
||||
|
||||
#if MC_TARGET_OS_IOS
|
||||
|
||||
// Lifed from SLikeNet because *just* iOS 6 was bitching and moaning 25% of the time
|
||||
// iOS 6 SDK makes phones hate this for some reason
|
||||
// gethostbyname didn't return NULL on iPhone 5 running iOS 6.1.4 on iOS 5 SDK,
|
||||
// but did return NULL when running on the same hardware on iOS 6.1 SDK
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/types.h> // used for getifaddrs()
|
||||
#include <ifaddrs.h> // used for getifaddrs()
|
||||
#endif // _WIN32
|
||||
|
||||
#include <string> // used for std::string
|
||||
|
||||
// based on https://stackoverflow.com/questions/212528/get-the-ip-address-of-the-machine#265978
|
||||
void GetMyIP_Linux(SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS], const bool includeIPv6)
|
||||
{
|
||||
struct ifaddrs* pAddresses = nullptr;
|
||||
|
||||
// #med - add error check to getifaddrs()-call
|
||||
getifaddrs(&pAddresses);
|
||||
|
||||
struct ifaddrs* pCurAdapter = pAddresses;
|
||||
size_t outAddressIndex = 0;
|
||||
while ((pCurAdapter != nullptr) && (outAddressIndex < MAXIMUM_NUMBER_OF_INTERNAL_IDS)) {
|
||||
// skip interfaces which don't have any address assigned (according to the manual, this would only apply for BSD, but still we'd check for null here just in case)
|
||||
if (pCurAdapter->ifa_addr != nullptr) {
|
||||
// skip loopback adapters
|
||||
if ((pCurAdapter->ifa_flags & IFF_LOOPBACK) == 0) {
|
||||
if (pCurAdapter->ifa_addr->sa_family == AF_INET) {
|
||||
const sockaddr_in* const curSocketAddress = reinterpret_cast<const sockaddr_in*>(pCurAdapter->ifa_addr);
|
||||
|
||||
char buffer[INET_ADDRSTRLEN] = { 0 };
|
||||
// #med - add return value check
|
||||
inet_ntop(AF_INET, &(curSocketAddress->sin_addr), buffer, INET_ADDRSTRLEN);
|
||||
|
||||
const std::string ipv4String(buffer);
|
||||
|
||||
// #med - review, is this really necessary?
|
||||
// skip source only addresses (aka: 0.0.0.0/8 - see RFC1700 p.4)
|
||||
if (ipv4String.find("0.") != 0) {
|
||||
// store the adapter's address
|
||||
addresses[outAddressIndex++].address.addr4 = *curSocketAddress;
|
||||
}
|
||||
}
|
||||
#if RAKNET_SUPPORT_IPV6 == 1
|
||||
else if (includeIPv6 && pCurAdapter->ifa_addr->sa_family == AF_INET6) {
|
||||
const sockaddr_in6* const curSocketAddress = reinterpret_cast<const sockaddr_in6*>(pCurAdapter->ifa_addr);
|
||||
|
||||
char buffer[INET6_ADDRSTRLEN] = { 0 };
|
||||
// #med - add return value check
|
||||
inet_ntop(AF_INET6, &(curSocketAddress->sin6_addr), buffer, INET6_ADDRSTRLEN);
|
||||
|
||||
const std::string ipv6String(buffer);
|
||||
|
||||
// detect and skip non-external addresses
|
||||
bool isLocal = false;
|
||||
bool isSpecial = false;
|
||||
if (ipv6String.find("fe") == 0) {
|
||||
const char c = ipv6String[2];
|
||||
if (c == '8' || c == '9' || c == 'a' || c == 'b') {
|
||||
isLocal = true;
|
||||
}
|
||||
}
|
||||
else if (ipv6String.find("2001:0:") == 0) {
|
||||
isSpecial = true;
|
||||
}
|
||||
|
||||
if (!(isLocal || isSpecial)) {
|
||||
// store the adapter's address
|
||||
addresses[outAddressIndex++].address.addr6 = *curSocketAddress;
|
||||
}
|
||||
}
|
||||
#endif // RAKNET_SUPPORT_IPV6 == 1
|
||||
// else skip the address (neither IPv4 nor IPv6 address)
|
||||
}
|
||||
}
|
||||
|
||||
pCurAdapter = pCurAdapter->ifa_next;
|
||||
}
|
||||
|
||||
if (pAddresses != nullptr)
|
||||
freeifaddrs(pAddresses);
|
||||
|
||||
while (outAddressIndex < MAXIMUM_NUMBER_OF_INTERNAL_IDS) {
|
||||
addresses[outAddressIndex++] = UNASSIGNED_SYSTEM_ADDRESS;
|
||||
}
|
||||
}
|
||||
|
||||
#else // !MC_TARGET_OS_IOS
|
||||
|
||||
#if RAKNET_SUPPORT_IPV6==1
|
||||
|
||||
void PrepareAddrInfoHints2(addrinfo *hints)
|
||||
@@ -82,6 +174,7 @@ void GetMyIP_Windows_Linux_IPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTER
|
||||
|
||||
if ( phe == 0 )
|
||||
{
|
||||
printf("gethostbyname hostname: %s, errno: %d\n", ac, h_errno);
|
||||
RakAssert(phe!=0);
|
||||
return ;
|
||||
}
|
||||
@@ -103,13 +196,23 @@ void GetMyIP_Windows_Linux_IPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTER
|
||||
|
||||
#endif // RAKNET_SUPPORT_IPV6==1
|
||||
|
||||
#endif // MC_TARGET_OS_IOS
|
||||
|
||||
|
||||
void GetMyIP_Windows_Linux( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
|
||||
{
|
||||
#if RAKNET_SUPPORT_IPV6==1
|
||||
GetMyIP_Windows_Linux_IPV4And6(addresses);
|
||||
#if MC_TARGET_OS_IOS
|
||||
GetMyIP_Linux(addresses, true);
|
||||
#else
|
||||
GetMyIP_Windows_Linux_IPV4And6(addresses);
|
||||
#endif
|
||||
#else
|
||||
GetMyIP_Windows_Linux_IPV4(addresses);
|
||||
#if MC_TARGET_OS_IOS
|
||||
GetMyIP_Linux(addresses, false);
|
||||
#else
|
||||
GetMyIP_Windows_Linux_IPV4(addresses);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -118,4 +221,4 @@ void GetMyIP_Windows_Linux( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_I
|
||||
|
||||
#endif // file header
|
||||
|
||||
#endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS
|
||||
#endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS
|
||||
Reference in New Issue
Block a user