Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if available use ares_getaddrinfo instead of ares_gethostbyname #365

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading