Skip to content

Commit

Permalink
Further cleanups on AresHandler
Browse files Browse the repository at this point in the history
Assume that we're not using a 15+ year old c-ares and remove the checks
for versions older than 1.7.0. On autotools systems, check for the
existance of ares_getaddrinfo. Assume we have it on Windows and macOS.
  • Loading branch information
blast007 committed Nov 26, 2024
1 parent 9edff79 commit 504e4b3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 39 deletions.
2 changes: 1 addition & 1 deletion MSVC/build/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
#define HAVE_STD__MIN 1
#define HAVE_STD__MAX 1

#define HAVE_ARES_LIBRARY_INIT 1
#define HAVE_ARES_GETADDRINFO 1

#ifndef DEBUG_TRACE
#define DEBUG_TRACE
Expand Down
4 changes: 2 additions & 2 deletions Xcode/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
/* libm includes acosf */
#define HAVE_ACOSF 1

/* Define if libcares includes ares_library_init. */
#define HAVE_ARES_LIBRARY_INIT 1
/* Define if libcares includes ares_getaddrinfo */
#define HAVE_ARES_GETADDRINFO 1

/* libm includes asinf */
#define HAVE_ASINF 1
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM([[
]])],[AC_MSG_RESULT([yes])],[AC_MSG_FAILURE([working c-ares library was not found])])
LIBS="$PRELIBS"

AC_CHECK_LIB(cares, ares_library_init, AC_DEFINE([HAVE_ARES_LIBRARY_INIT],[1],[Define if libcares includes ares_library_init.]))
AC_CHECK_LIB(cares, ares_getaddrinfo, AC_DEFINE([HAVE_ARES_GETADDRINFO],[1],[Define if libcares includes ares_getaddrinfo.]))



Expand Down
15 changes: 4 additions & 11 deletions include/AresHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,15 @@ class AresHandler
return status;
};
private:
#if ARES_VERSION_MAJOR > 1 || ARES_VERSION_MINOR >= 16
static void staticCallback1(void *arg, int status,
#if HAVE_ARES_GETADDRINFO
static void staticCallbackAddrInfo(void *arg, int status,
int timeout, struct ares_addrinfo *result);
void callbackAddrInfo(int status, 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);
#else
static void staticCallback(void *arg, int statusCallback,
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
37 changes: 13 additions & 24 deletions src/net/AresHandler.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,16 @@ bool AresHandler::globalInit()
{
if (!globallyInited)
{
#ifdef HAVE_ARES_LIBRARY_INIT
if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS)
#endif
globallyInited = true;
}
return globallyInited;
}

void AresHandler::globalShutdown()
{
#ifdef HAVE_ARES_LIBRARY_INIT
if (globallyInited)
ares_library_cleanup();
#endif
}

void AresHandler::queryHostname(const struct sockaddr *clientAddr)
Expand Down Expand Up @@ -121,37 +117,21 @@ 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
#if HAVE_ARES_GETADDRINFO
struct ares_addrinfo_hints hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;

ares_getaddrinfo(aresChannel, name, NULL, &hints, staticCallback1,
ares_getaddrinfo(aresChannel, name, NULL, &hints, staticCallbackAddrInfo,
(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)
#else
void AresHandler::staticCallback(void *arg, int callbackStatus,
struct hostent *hostent)
#endif
{
((AresHandler *)arg)->callback(callbackStatus, hostent);
}
Expand Down Expand Up @@ -182,8 +162,17 @@ 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)
#if HAVE_ARES_GETADDRINFO
void AresHandler::staticCallbackAddrInfo(void *arg, int status,
int, struct ares_addrinfo *result)
{
if (status == ARES_EDESTRUCTION)
return;

((AresHandler *)arg)->callbackAddrInfo(status, result);
}

void AresHandler::callbackAddrInfo(int callbackStatus, struct ares_addrinfo *result)
{
const std::lock_guard<std::mutex> lock(callback_mutex);

Expand Down

0 comments on commit 504e4b3

Please sign in to comment.