results from Boost.Asio resolver differ

boost, boost-asio, c++, getaddrinfo, sockets

Solution

You can limit resolver to IPv4 only: ip::tcp::resolver::query(ip::tcp::v4(), host, service)

Problem

I have a canned reproducer invoking `boost::asio::ip::tcp::resolver::resolve()` on `localhost` once every 5 seconds. It counts the number of endpoints returned and compares that value against the previous iteration. ``` #include <boost/asio.hpp> #include <iostream> int main(int argc, char *argv[]) { if ( argc < 3 ) { std::cerr << argv[0] << " host port" << std::endl; exit( EXIT_FAILURE ); } const char* host = argv[1]; const char* service = argv[2]; boost::asio::io_service io_service; boost::asio::ip::tcp::resolver resolver( io_service ); size_t previous = 0; while ( true ) { boost::asio::ip::tcp::resolver::iterator i( resolver.resolve( boost::asio::ip::tcp::resolver::query( host, service ) ) ); size_t count( 0 ); while ( i != boost::asio::ip::tcp::resolver::iterator() ) { std::cout << i->endpoint() << std::endl; ++i; ++count; } std::cout << "got " << count << " addresses" << std::endl; if ( previous == 0 ) { previous = count; } assert( count == previous ); sleep( 5 ); } } ``` sample session ``` ~> time ./addrinfo_asio localhost 80 ... 127.0.0.1:80 got 1 addresses [::1]:80 127.0.0.1:80 got 2 addresses addrinfo_asio: addrinfo_asio.cc:35: int main(int, char**): Assertion `count == previous' failed. Aborted (core dumped) real 216m20.515s user 0m0.181s sys 0m0.193s ~> ``` You can see it found one endpoint (127.0.0.1:80) for about 3.5 hours, then found two (127.0.0.1:80 and [::1]:80). I'm wondering - why the endpoint count changes from one, to two? - what could cause it? Resolving both ipv4 and ipv6 addresses is intentional, I do not want to limit the query to just ipv4. I realize this behavior is likely not specific to asio, I also have a reproducer invoking `getaddrinfo` directly that exhibits the same behavior. My platform is ppc64 RHEL 6.2 if that is relevant. I have not tried reproducing elsewhere.

Original source