File: Network.hs

package info (click to toggle)
haskell-aws 0.24.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 868 kB
  • sloc: haskell: 9,593; makefile: 2
file content (20 lines) | stat: -rw-r--r-- 677 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 h = do
  sock <- getProtocolNumber "tcp" >>= socket AF_INET Stream
  addr <- (addrAddress . head) `fmap` getAddrInfo (Just (defaultHints { addrFlags = [ AI_PASSIVE ] } )) (Just h) (Just "80")
  case addr of
    remote@(SockAddrInet _ _) -> do
      v <- catch (timeout 100000 (connect sock remote) >>= return . isJust)
                 (\(_ :: SomeException) -> return False)
      close sock
      return v
    _ -> return False