From 467844d7d3f703f99fcde1f951f33efda5e90074 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 18 Sep 2012 17:19:41 -0400 Subject: [PATCH] prefer ipv4 localhost when both are available The webapp can only run on one of ipv4 and ipv6, no both. Some web browsers may not support ipv6, so ipv4 is the safe choice. The actual problem I ran into with it only listening to ipv6 was that Utility.Url.exists was failing to connect to it. I doubt that haskell's HTTP library is ipv4 only. More likely, it was only trying one address, and tried ipv4 first. --- Utility/WebApp.hs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Utility/WebApp.hs b/Utility/WebApp.hs index 883f4d183d..21a5761bcb 100644 --- a/Utility/WebApp.hs +++ b/Utility/WebApp.hs @@ -62,15 +62,19 @@ runWebApp app observer = do observer =<< socketPort sock {- Binds to a local socket, selecting any free port. + - + - Prefers to bind to the ipv4 address rather than the ipv6 address + - of localhost, if it's available. - - As a (very weak) form of security, only connections from - localhost are accepted. -} localSocket :: IO Socket localSocket = do addrs <- getAddrInfo (Just hints) (Just localhost) Nothing - case addrs of - [] -> error "unable to bind to a local socket" - (addr:_) -> go addr + case (partition (\a -> addrFamily a == AF_INET) addrs) of + (v4addr:_, _) -> go v4addr + (_, v6addr:_) -> go v6addr + _ -> error "unable to bind to a local socket" where hints = defaultHints { addrFlags = [AI_ADDRCONFIG]