Skip to content

Commit bd3e8d8

Browse files
authored
Fix build on latest c-ares (#331)
1 parent c922801 commit bd3e8d8

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

.github/workflows/macos-clang.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ jobs:
1616
link: [ 'STATIC', 'SHARED' ]
1717
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
1818
build-type: ['Debug', 'Release']
19-
tls-provider: ['', 'openssl', 'botan']
19+
# Botan needs std::ranges but clang on macOS doesn't support it yet
20+
#tls-provider: ['', 'openssl', 'botan']
21+
tls-provider: ['', 'openssl']
2022

2123
steps:
2224
- name: Install dependencies

trantor/net/inner/AresResolver.cc

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ bool Resolver::isCAresUsed()
5252
AresResolver::LibraryInitializer::LibraryInitializer()
5353
{
5454
ares_library_init(ARES_LIB_INIT_ALL);
55+
56+
hints_ = new ares_addrinfo_hints;
57+
hints_->ai_flags = 0;
58+
hints_->ai_family = AF_INET;
59+
hints_->ai_socktype = 0;
60+
hints_->ai_protocol = 0;
5561
}
5662
AresResolver::LibraryInitializer::~LibraryInitializer()
5763
{
5864
ares_library_cleanup();
65+
delete hints_;
5966
}
6067

6168
AresResolver::LibraryInitializer AresResolver::libraryInitializer_;
@@ -124,11 +131,12 @@ void AresResolver::resolveInLoop(const std::string& hostname,
124131
#endif
125132
init();
126133
QueryData* queryData = new QueryData(this, cb, hostname);
127-
ares_gethostbyname(ctx_,
128-
hostname.c_str(),
129-
AF_INET,
130-
&AresResolver::ares_hostcallback_,
131-
queryData);
134+
ares_getaddrinfo(ctx_,
135+
hostname.c_str(),
136+
NULL,
137+
libraryInitializer_.hints_,
138+
&AresResolver::ares_hostcallback_,
139+
queryData);
132140
struct timeval tv;
133141
struct timeval* tvp = ares_timeout(ctx_, NULL, &tv);
134142
double timeout = getSeconds(tvp);
@@ -166,23 +174,33 @@ void AresResolver::onTimer()
166174
}
167175

168176
void AresResolver::onQueryResult(int status,
169-
struct hostent* result,
177+
struct ares_addrinfo* result,
170178
const std::string& hostname,
171179
const ResolverResultsCallback& callback)
172180
{
173181
LOG_TRACE << "onQueryResult " << status;
174182
auto inets_ptr = std::make_shared<std::vector<trantor::InetAddress>>();
175183
if (result)
176184
{
177-
auto pptr = (struct in_addr**)result->h_addr_list;
178-
for (; *pptr != nullptr; pptr++)
185+
auto pptr = (struct ares_addrinfo_node*)result->nodes;
186+
for (; pptr != NULL; pptr = pptr->ai_next)
179187
{
180-
struct sockaddr_in addr;
181-
memset(&addr, 0, sizeof addr);
182-
addr.sin_family = AF_INET;
183-
addr.sin_port = 0;
184-
addr.sin_addr = *reinterpret_cast<in_addr*>(*pptr);
185-
inets_ptr->emplace_back(trantor::InetAddress{addr});
188+
trantor::InetAddress inet;
189+
if (pptr->ai_family == AF_INET)
190+
{
191+
struct sockaddr_in* addr4 = (struct sockaddr_in*)pptr->ai_addr;
192+
inets_ptr->emplace_back(trantor::InetAddress{*addr4});
193+
}
194+
else if (pptr->ai_family == AF_INET6)
195+
{
196+
struct sockaddr_in6* addr6 =
197+
(struct sockaddr_in6*)pptr->ai_addr;
198+
inets_ptr->emplace_back(trantor::InetAddress{*addr6});
199+
}
200+
else
201+
{
202+
// TODO: Handle unknown family?
203+
}
186204
}
187205
}
188206
if (inets_ptr->empty())
@@ -237,7 +255,7 @@ void AresResolver::onSockStateChange(int sockfd, bool read, bool write)
237255
void AresResolver::ares_hostcallback_(void* data,
238256
int status,
239257
int timeouts,
240-
struct hostent* hostent)
258+
struct ares_addrinfo* hostent)
241259
{
242260
(void)timeouts;
243261
QueryData* query = static_cast<QueryData*>(data);

trantor/net/inner/AresResolver.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515

1616
extern "C"
1717
{
18-
struct hostent;
18+
struct ares_addrinfo;
1919
struct ares_channeldata;
20+
struct ares_addrinfo_hints;
2021
using ares_channel = struct ares_channeldata*;
2122
}
2223
namespace trantor
@@ -157,7 +158,7 @@ class AresResolver : public Resolver,
157158
void onRead(int sockfd);
158159
void onTimer();
159160
void onQueryResult(int status,
160-
struct hostent* result,
161+
struct ares_addrinfo* result,
161162
const std::string& hostname,
162163
const ResolverResultsCallback& callback);
163164
void onSockCreate(int sockfd, int type);
@@ -166,7 +167,7 @@ class AresResolver : public Resolver,
166167
static void ares_hostcallback_(void* data,
167168
int status,
168169
int timeouts,
169-
struct hostent* hostent);
170+
struct ares_addrinfo* hostent);
170171
#ifdef _WIN32
171172
static int ares_sock_createcallback_(SOCKET sockfd, int type, void* data);
172173
#else
@@ -184,6 +185,7 @@ class AresResolver : public Resolver,
184185
{
185186
LibraryInitializer();
186187
~LibraryInitializer();
188+
ares_addrinfo_hints* hints_;
187189
};
188190
static LibraryInitializer libraryInitializer_;
189191
};

0 commit comments

Comments
 (0)