{-# LANGUAGE CPP #-} #include "HsNetDef.h" moduleNetwork.Socket.Name(getPeerName ,getSocketName ,socketPort ,socketPortSafe )whereimportForeign.Marshal.Utils(with)importNetwork.Socket.Imports importNetwork.Socket.Internal importNetwork.Socket.Types -- | Getting peer's socket address.getPeerName ::SocketAddress sa =>Socket ->IOsa getPeerName :: forall sa. SocketAddress sa => Socket -> IO sa getPeerName Socket s =forall sa a. SocketAddress sa => (Ptr sa -> Int -> IO a) -> IO a withNewSocketAddress forall a b. (a -> b) -> a -> b $\Ptr sa ptr Int sz ->forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b with(forall a b. (Integral a, Num b) => a -> b fromIntegralInt sz )forall a b. (a -> b) -> a -> b $\Ptr CInt int_star ->forall r. Socket -> (CInt -> IO r) -> IO r withFdSocket Socket s forall a b. (a -> b) -> a -> b $\CInt fd ->doforall a. (Eq a, Num a) => String -> IO a -> IO () throwSocketErrorIfMinus1Retry_ String "Network.Socket.getPeerName"forall a b. (a -> b) -> a -> b $forall sa. CInt -> Ptr sa -> Ptr CInt -> IO CInt c_getpeername CInt fd Ptr sa ptr Ptr CInt int_star CInt _sz <-forall a. Storable a => Ptr a -> IO a peekPtr CInt int_star forall sa. SocketAddress sa => Ptr sa -> IO sa peekSocketAddress Ptr sa ptr -- | Getting my socket address.getSocketName ::SocketAddress sa =>Socket ->IOsa getSocketName :: forall sa. SocketAddress sa => Socket -> IO sa getSocketName Socket s =forall sa a. SocketAddress sa => (Ptr sa -> Int -> IO a) -> IO a withNewSocketAddress forall a b. (a -> b) -> a -> b $\Ptr sa ptr Int sz ->forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b with(forall a b. (Integral a, Num b) => a -> b fromIntegralInt sz )forall a b. (a -> b) -> a -> b $\Ptr CInt int_star ->forall r. Socket -> (CInt -> IO r) -> IO r withFdSocket Socket s forall a b. (a -> b) -> a -> b $\CInt fd ->doforall a. (Eq a, Num a) => String -> IO a -> IO () throwSocketErrorIfMinus1Retry_ String "Network.Socket.getSocketName"forall a b. (a -> b) -> a -> b $forall sa. CInt -> Ptr sa -> Ptr CInt -> IO CInt c_getsockname CInt fd Ptr sa ptr Ptr CInt int_star forall sa. SocketAddress sa => Ptr sa -> IO sa peekSocketAddress Ptr sa ptr foreignimportCALLCONVunsafe"getpeername"c_getpeername ::CInt->Ptrsa ->PtrCInt->IOCIntforeignimportCALLCONVunsafe"getsockname"c_getsockname ::CInt->Ptrsa ->PtrCInt->IOCInt-- ----------------------------------------------------------------------------- socketPort---- The port number the given socket is currently connected to can be-- determined by calling $port,ドル is generally only useful when bind-- was given $aNY\_PORT$.-- | Getting the port of socket.-- `IOError` is thrown if a port is not available.socketPort ::Socket -- Connected & Bound Socket->IOPortNumber -- Port Number of SocketsocketPort :: Socket -> IO PortNumber socketPort Socket s =doSockAddr sa <-forall sa. SocketAddress sa => Socket -> IO sa getSocketName Socket s caseSockAddr sa ofSockAddrInet PortNumber port HostAddress _->forall (m :: * -> *) a. Monad m => a -> m a returnPortNumber port SockAddrInet6 PortNumber port HostAddress _HostAddress6 _HostAddress _->forall (m :: * -> *) a. Monad m => a -> m a returnPortNumber port SockAddr _->forall a. IOError -> IO a ioErrorforall a b. (a -> b) -> a -> b $String -> IOError userErrorString "Network.Socket.socketPort: AF_UNIX not supported."-- ----------------------------------------------------------------------------- socketPortSafe-- | Getting the port of socket.socketPortSafe ::Socket -- Connected & Bound Socket->IO(MaybePortNumber )-- Port Number of SocketsocketPortSafe :: Socket -> IO (Maybe PortNumber) socketPortSafe Socket s =doSockAddr sa <-forall sa. SocketAddress sa => Socket -> IO sa getSocketName Socket s forall (m :: * -> *) a. Monad m => a -> m a returnforall a b. (a -> b) -> a -> b $caseSockAddr sa ofSockAddrInet PortNumber port HostAddress _->forall a. a -> Maybe a JustPortNumber port SockAddrInet6 PortNumber port HostAddress _HostAddress6 _HostAddress _->forall a. a -> Maybe a JustPortNumber port SockAddr _->forall a. Maybe a Nothing