Boost C++ Libraries: Ticket #8503: Cannot connect to localhost without global connectivity https://svn.boost.org/trac10/ticket/8503 <p> On Windows, if there are no network cards installed or they are all disabled, attempts to resolve localhost fail. The problem is that by default, the default value for the basic_resolver_query constructor's resolve_flags parameter is address_configured, which is AI_ADDRCONFIG on Windows. However, as implemented AI_ADDRCONFIG is actually detrimental, particularly for resolution of loopback and link-local addresses. </p> <p> The rationale from the Google Chrome source code explains this. Here is the relevant snippet from SystemHostResolverCall in <a class="ext-link" href="http://src.chromium.org/viewvc/chrome/trunk/src/net/dns/host_resolver_proc.cc"><span class="icon">​</span>host_resolver_proc.cc</a>: </p> <pre class="wiki">#if defined(OS_WIN) // DO NOT USE AI_ADDRCONFIG ON WINDOWS. // // The following comment in &lt;winsock2.h&gt; is the best documentation I found // on AI_ADDRCONFIG for Windows: // Flags used in "hints" argument to getaddrinfo() // - AI_ADDRCONFIG is supported starting with Vista // - default is AI_ADDRCONFIG ON whether the flag is set or not // because the performance penalty in not having ADDRCONFIG in // the multi-protocol stack environment is severe; // this defaulting may be disabled by specifying the AI_ALL flag, // in that case AI_ADDRCONFIG must be EXPLICITLY specified to // enable ADDRCONFIG behavior // // Not only is AI_ADDRCONFIG unnecessary, but it can be harmful. If the // computer is not connected to a network, AI_ADDRCONFIG causes getaddrinfo // to fail with WSANO_DATA (11004) for "localhost", probably because of the // following note on AI_ADDRCONFIG in the MSDN getaddrinfo page: // The IPv4 or IPv6 loopback address is not considered a valid global // address. // See http://crbug.com/5234. // // OpenBSD does not support it, either. hints.ai_flags = 0; #else hints.ai_flags = AI_ADDRCONFIG; #endif </pre><p> The developers of <a class="ext-link" href="http://xcb.freedesktop.org/"><span class="icon">​</span>XCB</a> arrived at a similar conclusion. See their discussion on <a class="ext-link" href="http://thread.gmane.org/gmane.comp.freedesktop.xcb/6973"><span class="icon">​</span>Bug: Can’t connect to localhost without global connectivity</a>. </p> <p> Code using ASIO can work around this problem, but doing so would require platform-specific code and intricate knowledge of poorly documented platform-specific behavior. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8503 Trac 1.4.3 chris_kohlhoff Fri, 24 May 2013 03:07:59 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/8503#comment:1 https://svn.boost.org/trac10/ticket/8503#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> This behaviour is not specific to Windows (e.g. I've seen it on Linux too). It looks like AI_ADDRCONFIG is behaving as POSIX intended. </p> <p> If you don't want to use the AI_ADDRCONFIG option, please explicitly specify the flags when constructing the query. You can do this in portable code. </p> Ticket Edward Brey <Edward@…> Fri, 24 May 2013 13:10:44 GMT <link>https://svn.boost.org/trac10/ticket/8503#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8503#comment:2</guid> <description> <p> I realize that it is in theory possible to override the default when creating a query. Sometimes, however, that option gets abstracted away. I ran into this problem when using the <a class="ext-link" href="https://github.com/zaphoyd/websocketpp"><span class="icon">​</span>WebSocket++</a> library, which in <a class="ext-link" href="https://github.com/zaphoyd/websocketpp/blob/experimental/websocketpp/transport/asio/endpoint.hpp"><span class="icon">​</span>endpoint.hpp</a> creates a query using the default options without exposing the options to users of the library. Of course, exposing the options would be one resolution to the <a class="ext-link" href="https://github.com/zaphoyd/websocketpp/issues/244"><span class="icon">​</span>open issue</a>, but it's nice to keep the API smaller if there is a "it just works" solution. </p> <p> Am I missing something about the virtues of AI_ADDRCONFIG? If 99% of the time, it's best to override the default and turn the flag off, isn't keeping AI_ADDRCONFIG as the default likely to trip up unsuspecting developers and lead to a lot of "Why doesn't this work on localhost?" headaches like I experienced. </p> <p> As computers become more mobile, this gotcha will grow more frequent. I encountered it simply by turning on airplane mode on my laptop (a commonly used Windows 8 feature). </p> </description> <category>Ticket</category> </item> </channel> </rss>