module Network.Utils (niceSocketsDo, connectTCP, connectTCPAddr,
listenTCPAddr, showSockAddr)
where
import Network
import Network.Socket
import Network.BSD
import System.IO
#if !(defined(mingw32_HOST_OS) || defined(mingw32_TARGET_OS) || defined(__MINGW32__))
import qualified System.Posix.Signals
#endif
niceSocketsDo :: IO a -> IO a
niceSocketsDo func = do
#if !(defined(mingw32_HOST_OS) || defined(mingw32_TARGET_OS) || defined(__MINGW32__))
System.Posix.Signals.installHandler
System.Posix.Signals.sigPIPE
System.Posix.Signals.Ignore
Nothing
#endif
withSocketsDo func
connectTCP :: HostName -> PortNumber -> IO Socket
connectTCP host port = do
he <- getHostByName host
connectTCPAddr (SockAddrInet port (hostAddress he))
connectTCPAddr :: SockAddr -> IO Socket
connectTCPAddr addr = do
proto <- getProtocolNumber "tcp"
s <- socket AF_INET Stream proto
connect s addr
return s
listenTCPAddr :: SockAddr -> Int -> IO Socket
listenTCPAddr addr queuelen = do
proto <- getProtocolNumber "tcp"
s <- socket AF_INET Stream proto
bindSocket s addr
listen s queuelen
return s
showSockAddr :: SockAddr -> IO String
#if !(defined(mingw32_HOST_OS) || defined(mingw32_TARGET_OS) || defined(__MINGW32__))
showSockAddr (SockAddrUnix x) = return $ "UNIX socket at " ++ x
#endif
showSockAddr (SockAddrInet port host) =
do h <- inet_ntoa host
return $ "IPv4 host " ++ h ++ ", port " ++ (show port)