@@ -52,10 +52,17 @@ bool Resolver::isCAresUsed()
5252AresResolver::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}
5662AresResolver::LibraryInitializer::~LibraryInitializer ()
5763{
5864 ares_library_cleanup ();
65+ delete hints_;
5966}
6067
6168AresResolver::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
168176void 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)
237255void 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);
0 commit comments