From abe846402638dc34806d8f3e941f882333c766e5 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 2 May 2013 16:48:14 -0400 Subject: [PATCH] work around getAddrInfo segfault on Android For an unknown reason, getAddrInfo currently is segfaulting. Note that in February, I had used warpDebug, which uses getAddrInfo, and it worked. Don't know if my toolchain has changed and broke it, or it's due to having a different Android device now. Anyway, work around it by hardcoding the address to use. --- Utility/WebApp.hs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Utility/WebApp.hs b/Utility/WebApp.hs index 1306b14c3d..440d56fdf6 100644 --- a/Utility/WebApp.hs +++ b/Utility/WebApp.hs @@ -72,7 +72,7 @@ webAppSettings = defaultSettings } {- Binds to a local socket, or if specified, to a socket on the specified - - hostname or address. Selets any free port, unless the hostname ends with + - hostname or address. Selects any free port, unless the hostname ends with - ":port" - - Prefers to bind to the ipv4 address rather than the ipv6 address @@ -80,6 +80,18 @@ webAppSettings = defaultSettings -} getSocket :: Maybe HostName -> IO Socket getSocket h = do +#ifdef __ANDROID__ + -- getAddrInfo currently segfaults on Android. + -- The HostName is ignored by this code. + when (isJust h) $ + error "getSocket with HostName not supported on Android" + addr <- inet_addr "127.0.0.1" + sock <- socket AF_INET Stream defaultProtocol + preparesocket sock + bindSocket sock (SockAddrInet aNY_PORT addr) + use sock + where +#else addrs <- getAddrInfo (Just hints) (Just hostname) port case (partition (\a -> addrFamily a == AF_INET) addrs) of (v4addr:_, _) -> go v4addr @@ -94,12 +106,16 @@ getSocket h = do go' :: Int -> AddrInfo -> IO Socket go' 0 _ = error "unable to bind to local socket" go' n addr = do - r <- tryIO $ bracketOnError (open addr) sClose (use addr) + r <- tryIO $ bracketOnError (open addr) sClose (useaddr addr) either (const $ go' (pred n) addr) return r open addr = socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr) - use addr sock = do - setSocketOption sock ReuseAddr 1 + useaddr addr sock = do + preparesocket sock bindSocket sock (addrAddress addr) + use sock +#endif + preparesocket sock = setSocketOption sock ReuseAddr 1 + use sock = do listen sock maxListenQueue return sock