Author Topic: gethostbyname (used in GetMyIP) failing on iOS  (Read 23175 times)

GaiamarkGames

  • Not-a-newbie
  • *
  • Posts: 13
  • Karma: 0
    • View Profile
gethostbyname (used in GetMyIP) failing on iOS
« on: January 24, 2014, 04:29:07 PM »
I recently upgraded to 4.08 from 4.051 and noticed on my three iOS devices (6.x and 7.x) that gethostbyname called in RakNetSocket2_Windows_Linux.cpp returns NULL, which breaks the RakAssert - this is called in RakPeer::Startup.  Looking online I see others that are experiencing that behavior in iOS 6.x+.  For a solution, I found a StackOverflow article (http://stackoverflow.com/questions/14082992/gethostbyname-failed-in-ios6) and also tried taking the code from GetMyIP_Windows_Linux_IPV4And6 and modifying it slightly to work with GetMyIP_Windows_Linux_IPV4 - both of those solutions have worked for me.  Has anybody else seen or reported this issue?


Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: gethostbyname (used in GetMyIP) failing on iOS
« Reply #1 on: March 24, 2014, 11:21:20 PM »
Would you be able to post the fix you made? I could integrate that into the next version.

anchor

  • Not-a-newbie
  • *
  • Posts: 3
  • Karma: 0
    • View Profile
Re: gethostbyname (used in GetMyIP) failing on iOS
« Reply #2 on: April 16, 2014, 03:04:11 PM »
in the GetMyIP_Windows_Linux_IPV4() function:
the gethostbyname() returns NULL, so the local IP addresses are unknown.

tested on ipad3 [OS Version: iOS 6.1.2 (10B146)] and ipad3 [iOS 6.1.3 (10B329)].
(raknet 4.0802)
« Last Edit: April 17, 2014, 01:19:38 PM by anchor »

GaiamarkGames

  • Not-a-newbie
  • *
  • Posts: 13
  • Karma: 0
    • View Profile
Re: gethostbyname (used in GetMyIP) failing on iOS
« Reply #3 on: July 11, 2014, 12:21:26 AM »
Would you be able to post the fix you made? I could integrate that into the next version.

Just saw that you replied to this a couple months after I posted :)

First I moved PrepareAddrInfoHints2 outside of the RAKNET_SUPPORT_IPV6 define block.  Then I made this change to GetMyIP_Windows_Linux_IPV4:

Code: [Select]
void GetMyIP_Windows_Linux_IPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
{
int idx=0;
char ac[ 80 ];
int err = gethostname( ac, sizeof( ac ) );
((void)err); // err gives an unused warning in Release
RakAssert(err != -1);

struct addrinfo hints;
struct addrinfo *servinfo=0, *aip;  // will point to the results
PrepareAddrInfoHints2(&hints);
getaddrinfo(ac, "", &hints, &servinfo);

for (idx=0, aip = servinfo; aip != NULL && idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; aip = aip->ai_next, idx++)
{
if (aip->ai_family == AF_INET)
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)aip->ai_addr;
memcpy(&addresses[idx].address.addr4.sin_addr, &ipv4->sin_addr, sizeof(struct in_addr));
}
}

freeaddrinfo(servinfo); // free the linked-list

while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS)
{
addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS;
idx++;
}

}

Looking at it again I probably could have made a third method that would get called by both GetMyIP_Windows_Linux_IPV4And6 and GetMyIP_Windows_Linux_IPV4 but this is what I have now.
« Last Edit: July 11, 2014, 12:24:08 AM by GaiamarkGames »