added new method rsGetHostByName to use gethostbyname_r for re-entrant calls. Updated dnsresolver and extaddrfinder to use it. Suggestion by GuessWho #125

This commit is contained in:
csoler 2015-10-07 21:24:31 -04:00
parent f1f722a767
commit e776effc0d
5 changed files with 90 additions and 39 deletions

View file

@ -26,6 +26,7 @@
#include "util/rsnet.h"
#include "util/rsthreads.h"
#include "util/rsstring.h"
#include "util/rsmemory.h"
#ifdef WINDOWS_SYS
#else
@ -72,6 +73,36 @@ void sockaddr_clear(struct sockaddr_in *addr)
addr->sin_family = AF_INET;
}
bool rsGetHostByName(const std::string& hostname, in_addr& returned_addr)
{
RsTemporaryMemory mem(8192) ;
if(!mem)
{
std::cerr << __PRETTY_FUNCTION__ << ": Cannot allocate memory!" << std::endl;
return false; // Do something.
}
int error = 0;
struct hostent pHost;
struct hostent *result;
if(gethostbyname_r(hostname.c_str(), &pHost, (char*)(unsigned char*)mem, mem.size(), &result, &error) != 0)
{
std::cerr << __PRETTY_FUNCTION__ << ": cannot call gethostname_r. Internal error reported. Check buffer size." << std::endl;
return false ;
}
if(!result)
{
std::cerr << __PRETTY_FUNCTION__ << ": gethostname_r returned null result." << std::endl;
return false ;
}
// Use contents of result.
returned_addr.s_addr = *(unsigned long*) (result->h_addr);
return true ;
}
bool isValidNet(const struct in_addr *addr)
{