Skip to content

Commit

Permalink
if available use ares_getaddrinfo instead of ares_gethostbyname
Browse files Browse the repository at this point in the history
  • Loading branch information
atupone committed Nov 10, 2024
1 parent f48c953 commit 9edff79
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/AresHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class AresHandler
return status;
};
private:
#if ARES_VERSION_MAJOR > 1 || ARES_VERSION_MINOR >= 16
static void staticCallback1(void *arg, int status,
int timeout, struct ares_addrinfo *result);
#endif

#if ARES_VERSION_MAJOR >= 1 && ARES_VERSION_MINOR >= 5
static void staticCallback(void *arg, int statusCallback, int timeouts,
struct hostent *hostent);
Expand All @@ -62,6 +67,9 @@ class AresHandler
struct hostent *hostent);
#endif
void callback(int status, struct hostent *hostent);
#if ARES_VERSION_MAJOR > 1 || ARES_VERSION_MINOR >= 16
void callback1(int status, struct ares_addrinfo *result);
#endif
int index;

std::string hostName;
Expand Down
42 changes: 42 additions & 0 deletions src/net/AresHandler.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,31 @@ void AresHandler::queryHost(const char *name)

// launch the asynchronous query to look up this hostname
status = HbNPending;

#if ARES_VERSION_MAJOR > 1 || ARES_VERSION_MINOR >= 16
struct ares_addrinfo_hints hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;

ares_getaddrinfo(aresChannel, name, NULL, &hints, staticCallback1,
(void *)this);
#else
ares_gethostbyname(aresChannel, name, AF_INET, staticCallback,
(void *)this);
#endif
}

#if ARES_VERSION_MAJOR > 1 || ARES_VERSION_MINOR >= 16
void AresHandler::staticCallback1(void *arg, int status,
int, struct ares_addrinfo *result)
{
if (status == ARES_EDESTRUCTION)
return;

((AresHandler *)arg)->callback1(status, result);
}
#endif

#if ARES_VERSION_MAJOR > 1 || ARES_VERSION_MINOR >= 5
void AresHandler::staticCallback(void *arg, int callbackStatus,
int, struct hostent *hostent)
Expand Down Expand Up @@ -161,6 +182,27 @@ void AresHandler::callback(int callbackStatus, struct hostent *hostent)
}
}

#if ARES_VERSION_MAJOR > 1 || ARES_VERSION_MINOR >= 16
void AresHandler::callback1(int callbackStatus, struct ares_addrinfo *result)
{
const std::lock_guard<std::mutex> lock(callback_mutex);

if (callbackStatus != ARES_SUCCESS)
{
logDebugMessage(1,"Player [%d] failed to resolve: error %d\n", index,
callbackStatus);
status = Failed;
}
else if (status == HbNPending)
{
memcpy(&hostAddress,
&((sockaddr_in *)result->nodes->ai_addr)->sin_addr,
sizeof(hostAddress));
status = HbNSucceeded;
}
}
#endif

const char *AresHandler::getHostname()
{
if (!callback_mutex.try_lock())
Expand Down

0 comments on commit 9edff79

Please sign in to comment.