@@ -52,10 +52,17 @@ bool Resolver::isCAresUsed()
52
52
AresResolver::LibraryInitializer::LibraryInitializer ()
53
53
{
54
54
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 ;
55
61
}
56
62
AresResolver::LibraryInitializer::~LibraryInitializer ()
57
63
{
58
64
ares_library_cleanup ();
65
+ delete hints_;
59
66
}
60
67
61
68
AresResolver::LibraryInitializer AresResolver::libraryInitializer_;
@@ -124,11 +131,12 @@ void AresResolver::resolveInLoop(const std::string& hostname,
124
131
#endif
125
132
init ();
126
133
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);
132
140
struct timeval tv;
133
141
struct timeval * tvp = ares_timeout (ctx_, NULL , &tv);
134
142
double timeout = getSeconds (tvp);
@@ -166,23 +174,33 @@ void AresResolver::onTimer()
166
174
}
167
175
168
176
void AresResolver::onQueryResult (int status,
169
- struct hostent * result,
177
+ struct ares_addrinfo * result,
170
178
const std::string& hostname,
171
179
const ResolverResultsCallback& callback)
172
180
{
173
181
LOG_TRACE << " onQueryResult " << status;
174
182
auto inets_ptr = std::make_shared<std::vector<trantor::InetAddress>>();
175
183
if (result)
176
184
{
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 )
179
187
{
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
+ }
186
204
}
187
205
}
188
206
if (inets_ptr->empty ())
@@ -237,7 +255,7 @@ void AresResolver::onSockStateChange(int sockfd, bool read, bool write)
237
255
void AresResolver::ares_hostcallback_ (void * data,
238
256
int status,
239
257
int timeouts,
240
- struct hostent * hostent)
258
+ struct ares_addrinfo * hostent)
241
259
{
242
260
(void )timeouts;
243
261
QueryData* query = static_cast <QueryData*>(data);
0 commit comments