Boost logo

Boost Users :

Subject: [Boost-users] [ASIO] Resolve hostname to IPv4 or IPv6
From: Alexander Lamaison (awl03_at_[hidden])
Date: 2010-05-29 14:39:50


I'm trying to add IPv6 support to my program without breaking IPv4. What
is the canonical way, if any, to do this win ASIO on Windows.

My previous code to take the hostname of a server and resolve it to and IP
looked like this:

        tcp::resolver resolver(m_io);
        tcp::endpoint endpoint;
        typedef tcp::resolver::query Lookup;
        Lookup query(
                endpoint.protocol(), host, lexical_cast<string>(port),
                Lookup::all_matching | Lookup::numeric_service);

        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::resolver::iterator end;

        error_code error = host_not_found;
        while (error && endpoint_iterator != end)
        {
                m_socket.close();
                m_socket.connect(*endpoint_iterator++, error);
        }
        if (error)
                BOOST_THROW_EXCEPTION(system_error(error));

I originally though that all_matching would mean this code would resolve an
IPv6 address if necessary but it doesn't. So I made the following changes:

        tcp::resolver resolver(m_io);
        typedef tcp::resolver::query Lookup;
        Lookup query(
                tcp::v6(), host_name, lexical_cast<string>(port),
                Lookup::all_matching | Lookup::v4_mapped |
                Lookup::numeric_service);

This works on my test IPv6 setup but is it good enough? There are all
sorts of things I don't understand like mapping and tunnelling so I'm
looking for a best practice for IP-agnostic name resolution.

Many thanks.

Alex Lamaison

--
SFTP for Windows Explorer (http://www.swish-sftp.org)

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net