TSharedRef<FInternetAddr> FSocketSubsystemIOS::GetLocalHostAddr(FOutputDevice& Out, bool& bCanBindAll) { TSharedRef<FInternetAddr> HostAddr = CreateInternetAddr(); HostAddr->SetAnyAddress(); ifaddrs* Interfaces = NULL; int32 WifiAddress; bool bWasWifiSet = false; int32 CellAddress; bool bWasCellSet = false; // get all of the addresses if (getifaddrs(&Interfaces) == 0) { // Loop through linked list of interfaces for (ifaddrs* Travel = Interfaces; Travel != NULL; Travel = Travel->ifa_next) { if (Travel->ifa_addr->sa_family == AF_INET) { if (strcmp(Travel->ifa_name, "en0") == 0) { WifiAddress = ((sockaddr_in*)Travel->ifa_addr)->sin_addr.s_addr; bWasWifiSet = true; // this is the best, no need to go on break; } else if (strcmp(Travel->ifa_name, "pdp_ip0") == 0) { CellAddress = ((sockaddr_in*)Travel->ifa_addr)->sin_addr.s_addr; bWasCellSet = true; } } } // Free memory freeifaddrs(Interfaces); if (bWasWifiSet) { HostAddr->SetIp(WifiAddress); UE_LOG(LogIOS, Log, TEXT("Host addr is WIFI: %s"), *HostAddr->ToString(false)); } else if (bWasCellSet) { HostAddr->SetIp(CellAddress); UE_LOG(LogIOS, Log, TEXT("Host addr is CELL: %s"), *HostAddr->ToString(false)); } else { UE_LOG(LogIOS, Log, TEXT("Host addr is INVALID")); } } // return the newly created address return HostAddr; }
TSharedRef<FInternetAddr> FSocketSubsystemLinux::GetLocalHostAddr(FOutputDevice& Out, bool& bCanBindAll) { // get parent address first TSharedRef<FInternetAddr> Addr = FSocketSubsystemBSD::GetLocalHostAddr(Out, bCanBindAll); // If the address is not a loopback one (or none), return it. uint32 ParentIp = 0; Addr->GetIp(ParentIp); // will return in host order if (ParentIp != 0 && (ParentIp & 0xff000000) != 0x7f000000) { return Addr; } // If superclass got the address from command line, honor that override TCHAR Home[256]=TEXT(""); if (FParse::Value(FCommandLine::Get(),TEXT("MULTIHOME="),Home,ARRAY_COUNT(Home))) { TSharedRef<FInternetAddr> TempAddr = CreateInternetAddr(); bool bIsValid = false; TempAddr->SetIp(Home, bIsValid); if (bIsValid) { return Addr; } } // we need to go deeper... (see http://unixhelp.ed.ac.uk/CGI/man-cgi?netdevice+7) int TempSocket = socket(PF_INET, SOCK_STREAM, 0); if (TempSocket) { ifreq IfReqs[8]; ifconf IfConfig; FMemory::Memzero(IfConfig); IfConfig.ifc_req = IfReqs; IfConfig.ifc_len = sizeof(IfReqs); int Result = ioctl(TempSocket, SIOCGIFCONF, &IfConfig); if (Result == 0) { for (int32 IdxReq = 0; IdxReq < ARRAY_COUNT(IfReqs); ++IdxReq) { // grab the first non-loobpack one which is up int ResultFlags = ioctl(TempSocket, SIOCGIFFLAGS, &IfReqs[IdxReq]); if (ResultFlags == 0 && (IfReqs[IdxReq].ifr_flags & IFF_UP) && (IfReqs[IdxReq].ifr_flags & IFF_LOOPBACK) == 0) { int32 NetworkAddr = reinterpret_cast<sockaddr_in *>(&IfReqs[IdxReq].ifr_addr)->sin_addr.s_addr; Addr->SetIp(ntohl(NetworkAddr)); break; } } } else { int ErrNo = errno; UE_LOG(LogSockets, Warning, TEXT("ioctl( ,SIOGCIFCONF, ) failed, errno=%d (%s)"), ErrNo, ANSI_TO_TCHAR(strerror(ErrNo))); } close(TempSocket); } return Addr; }