-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathName.hs
More file actions
77 lines (67 loc) · 2.51 KB
/
Name.hs
File metadata and controls
77 lines (67 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{-# LANGUAGE CPP #-}
#include "HsNetDef.h"
module Network.Socket.Name (
getPeerName
, getSocketName
, socketPort
, socketPortSafe
) where
import Foreign.Marshal.Utils (with)
import Network.Socket.Imports
import Network.Socket.Internal
import Network.Socket.Types
-- | Getting peer's socket address.
getPeerName :: SocketAddress sa => Socket -> IO sa
#ifdef HAVE_GETPEERNAME
getPeerName s =
withNewSocketAddress $ \ptr sz ->
with (fromIntegral sz) $ \int_star -> withFdSocket s $ \fd -> do
throwSocketErrorIfMinus1Retry_ "Network.Socket.getPeerName" $
c_getpeername fd ptr int_star
_sz <- peek int_star
peekSocketAddress ptr
#else
getPeerName _ = unsupported "getPeerName"
{-# WARNING getPeerName "operation will throw 'IOError' \"unsupported operation\"" #-}
#endif
-- | Getting my socket address.
getSocketName :: SocketAddress sa => Socket -> IO sa
getSocketName s =
withNewSocketAddress $ \ptr sz ->
with (fromIntegral sz) $ \int_star -> withFdSocket s $ \fd -> do
throwSocketErrorIfMinus1Retry_ "Network.Socket.getSocketName" $
c_getsockname fd ptr int_star
peekSocketAddress ptr
#ifdef HAVE_GETPEERNAME
foreign import CALLCONV unsafe "getpeername"
c_getpeername :: CInt -> Ptr sa -> Ptr CInt -> IO CInt
#endif
foreign import CALLCONV unsafe "getsockname"
c_getsockname :: CInt -> Ptr sa -> Ptr CInt -> IO CInt
-- ---------------------------------------------------------------------------
-- 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
-> IO PortNumber -- Port Number of Socket
socketPort s = do
sa <- getSocketName s
case sa of
SockAddrInet port _ -> return port
SockAddrInet6 port _ _ _ -> return port
_ -> ioError $ userError "Network.Socket.socketPort: AF_UNIX not supported."
-- ---------------------------------------------------------------------------
-- socketPortSafe
-- | Getting the port of socket.
socketPortSafe :: Socket -- Connected & Bound Socket
-> IO (Maybe PortNumber) -- Port Number of Socket
socketPortSafe s = do
sa <- getSocketName s
return $ case sa of
SockAddrInet port _ -> Just port
SockAddrInet6 port _ _ _ -> Just port
_ -> Nothing