[ASIO] Resolve hostname to IPv4 or IPv6
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)
On Sat, 29 May 2010 19:39:50 +0100, Alexander Lamaison wrote:
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. ...
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.
Turns out, this does indeed break IPv4 when run from an OS such as Windows 2000 that doesn't have IPv6. How can I resolve a hostname in both situations? Many thanks. Alex Lamaison -- SFTP for Windows Explorer (http://www.swish-sftp.org)
participants (1)
-
Alexander Lamaison