    module Aws.Network where

    import Data.Maybe
    import Control.Exception
    import Network.BSD (getProtocolNumber)
    import Network.Socket
    import System.Timeout

    -- Make a good guess if a host is reachable.
    hostAvailable :: String -> IO Bool
    hostAvailable :: ServiceName -> IO Bool
    hostAvailable ServiceName
    h = do
      sock <- ServiceName -> IO ProtocolNumber
    getProtocolNumber ServiceName
    "tcp" IO ProtocolNumber -> (ProtocolNumber -> IO Socket) -> IO Socket
    forall a b. IO a -> (a -> IO b) -> IO b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Family -> SocketType -> ProtocolNumber -> IO Socket
    socket Family
    AF_INET SocketType
    Stream
      addr <- (addrAddress . head) `fmap` getAddrInfo (Just (defaultHints { addrFlags = [ AI_PASSIVE ] } )) (Just h) (Just "80")
      case addr of
        remote :: SockAddr
    remote@(SockAddrInet PortNumber
    _ HostAddress
    _) -> do
          v <- IO Bool -> (SomeException -> IO Bool) -> IO Bool
    forall e a. Exception e => IO a -> (e -> IO a) -> IO a
    catch (Int -> IO () -> IO (Maybe ())
    forall a. Int -> IO a -> IO (Maybe a)
    timeout Int
    100000 (Socket -> SockAddr -> IO ()
    connect Socket
    sock SockAddr
    remote) IO (Maybe ()) -> (Maybe () -> IO Bool) -> IO Bool
    forall a b. IO a -> (a -> IO b) -> IO b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Bool -> IO Bool
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Bool -> IO Bool) -> (Maybe () -> Bool) -> Maybe () -> IO Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Maybe () -> Bool
    forall a. Maybe a -> Bool
    isJust)
                     (\(SomeException
    _ :: SomeException) -> Bool -> IO Bool
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False)
          close sock
          return v
        SockAddr
    _ -> Bool -> IO Bool
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False