{-# LANGUAGE CPP #-}
module Network.Socket.SockAddr (
getPeerName
, getSocketName
, connect
, bind
, accept
, sendBufTo
, recvBufFrom
, sendBufMsg
, recvBufMsg
) where
import Control.Exception (try, throwIO, IOException)
import System.Directory (removeFile)
import System.IO.Error (isAlreadyInUseError, isDoesNotExistError)
import qualified Network.Socket.Buffer as G
import qualified Network.Socket.Name as G
import qualified Network.Socket.Syscall as G
import Network.Socket.Flag
import Network.Socket.Imports
#if !defined(mingw32_HOST_OS)
import Network.Socket.Posix.Cmsg
#else
import Network.Socket.Win32.Cmsg
#endif
import Network.Socket.Types
getPeerName :: Socket -> IO SockAddr
getPeerName :: Socket -> IO SockAddr
getPeerName = Socket -> IO SockAddr
forall sa. SocketAddress sa => Socket -> IO sa
G.getPeerName
getSocketName :: Socket -> IO SockAddr
getSocketName :: Socket -> IO SockAddr
getSocketName = Socket -> IO SockAddr
forall sa. SocketAddress sa => Socket -> IO sa
G.getSocketName
connect :: Socket -> SockAddr -> IO ()
connect :: Socket -> SockAddr -> IO ()
connect = Socket -> SockAddr -> IO ()
forall sa. SocketAddress sa => Socket -> sa -> IO ()
G.connect
bind :: Socket -> SockAddr -> IO ()
bind :: Socket -> SockAddr -> IO ()
bind Socket
s SockAddr
a = case SockAddr
a of
SockAddrUnix String
p -> do
Either IOError ()
res <- IO () -> IO (Either IOError ())
forall e a. Exception e => IO a -> IO (Either e a)
try (Socket -> SockAddr -> IO ()
forall sa. SocketAddress sa => Socket -> sa -> IO ()
G.bind Socket
s SockAddr
a)
case Either IOError ()
res of
Right () -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Left IOError
e | Bool -> Bool
not (IOError -> Bool
isAlreadyInUseError IOError
e) -> IOError -> IO ()
forall e a. Exception e => e -> IO a
throwIO (IOError
e :: IOException)
Left IOError
e | Bool
otherwise -> do
Either IOError ()
res2 <- IO () -> IO (Either IOError ())
forall e a. Exception e => IO a -> IO (Either e a)
try (Socket -> SockAddr -> IO ()
forall sa. SocketAddress sa => Socket -> sa -> IO ()
G.connect Socket
s SockAddr
a)
case Either IOError ()
res2 of
Right () -> Socket -> IO ()
close Socket
s IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IOError -> IO ()
forall e a. Exception e => e -> IO a
throwIO IOError
e
Left IOError
e2 | Bool -> Bool
not (IOError -> Bool
isDoesNotExistError IOError
e2) -> IOError -> IO ()
forall e a. Exception e => e -> IO a
throwIO (IOError
e2 :: IOException)
Either IOError ()
_ -> do
IO (Either IOError ()) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO () -> IO (Either IOError ())
forall e a. Exception e => IO a -> IO (Either e a)
try (IO () -> IO (Either IOError ()))
-> IO () -> IO (Either IOError ())
forall a b. (a -> b) -> a -> b
$ String -> IO ()
removeFile String
p :: IO (Either IOError ()))
Socket -> SockAddr -> IO ()
forall sa. SocketAddress sa => Socket -> sa -> IO ()
G.bind Socket
s SockAddr
a
SockAddr
_ -> Socket -> SockAddr -> IO ()
forall sa. SocketAddress sa => Socket -> sa -> IO ()
G.bind Socket
s SockAddr
a
accept :: Socket -> IO (Socket, SockAddr)
accept :: Socket -> IO (Socket, SockAddr)
accept = Socket -> IO (Socket, SockAddr)
forall sa. SocketAddress sa => Socket -> IO (Socket, sa)
G.accept
sendBufTo :: Socket -> Ptr a -> Int -> SockAddr -> IO Int
sendBufTo :: Socket -> Ptr a -> Int -> SockAddr -> IO Int
sendBufTo = Socket -> Ptr a -> Int -> SockAddr -> IO Int
forall sa a.
SocketAddress sa =>
Socket -> Ptr a -> Int -> sa -> IO Int
G.sendBufTo
recvBufFrom :: Socket -> Ptr a -> Int -> IO (Int, SockAddr)
recvBufFrom :: Socket -> Ptr a -> Int -> IO (Int, SockAddr)
recvBufFrom = Socket -> Ptr a -> Int -> IO (Int, SockAddr)
forall sa a.
SocketAddress sa =>
Socket -> Ptr a -> Int -> IO (Int, sa)
G.recvBufFrom
sendBufMsg :: Socket
-> SockAddr
-> [(Ptr Word8,Int)]
-> [Cmsg]
-> MsgFlag
-> IO Int
sendBufMsg :: Socket
-> SockAddr -> [(Ptr Word8, Int)] -> [Cmsg] -> MsgFlag -> IO Int
sendBufMsg = Socket
-> SockAddr -> [(Ptr Word8, Int)] -> [Cmsg] -> MsgFlag -> IO Int
forall sa.
SocketAddress sa =>
Socket -> sa -> [(Ptr Word8, Int)] -> [Cmsg] -> MsgFlag -> IO Int
G.sendBufMsg
recvBufMsg :: Socket
-> [(Ptr Word8,Int)]
-> Int
-> MsgFlag
-> IO (SockAddr,Int,[Cmsg],MsgFlag)
recvBufMsg :: Socket
-> [(Ptr Word8, Int)]
-> Int
-> MsgFlag
-> IO (SockAddr, Int, [Cmsg], MsgFlag)
recvBufMsg = Socket
-> [(Ptr Word8, Int)]
-> Int
-> MsgFlag
-> IO (SockAddr, Int, [Cmsg], MsgFlag)
forall sa.
SocketAddress sa =>
Socket
-> [(Ptr Word8, Int)]
-> Int
-> MsgFlag
-> IO (sa, Int, [Cmsg], MsgFlag)
G.recvBufMsg