diff options
Diffstat (limited to 'Presence/GetHostByAddr.hs')
-rw-r--r-- | Presence/GetHostByAddr.hs | 77 |
1 files changed, 0 insertions, 77 deletions
diff --git a/Presence/GetHostByAddr.hs b/Presence/GetHostByAddr.hs deleted file mode 100644 index 45bca5e9..00000000 --- a/Presence/GetHostByAddr.hs +++ /dev/null | |||
@@ -1,77 +0,0 @@ | |||
1 | {-# LANGUAGE ForeignFunctionInterface #-} | ||
2 | module GetHostByAddr where | ||
3 | |||
4 | import Network.BSD | ||
5 | import Foreign.Ptr | ||
6 | import Foreign.C.Types | ||
7 | import Foreign.Storable (Storable(..)) | ||
8 | import Foreign.Marshal.Utils (with) | ||
9 | import Foreign.Marshal.Alloc | ||
10 | import Control.Concurrent | ||
11 | import System.IO.Unsafe | ||
12 | import System.IO.Error (ioeSetErrorString, mkIOError) | ||
13 | import Network.Socket | ||
14 | import GHC.IO.Exception | ||
15 | |||
16 | |||
17 | throwNoSuchThingIfNull :: String -> String -> IO (Ptr a) -> IO (Ptr a) | ||
18 | throwNoSuchThingIfNull loc desc act = do | ||
19 | ptr <- act | ||
20 | if (ptr == nullPtr) | ||
21 | then ioError (ioeSetErrorString (mkIOError NoSuchThing loc Nothing Nothing) desc) | ||
22 | else return ptr | ||
23 | |||
24 | {-# NOINLINE lock #-} | ||
25 | lock :: MVar () | ||
26 | lock = unsafePerformIO $ newMVar () | ||
27 | |||
28 | withLock :: IO a -> IO a | ||
29 | withLock act = withMVar lock (\_ -> act) | ||
30 | |||
31 | trySysCall :: IO a -> IO a | ||
32 | trySysCall act = act | ||
33 | |||
34 | {- | ||
35 | -- The locking of gethostbyaddr is similar to gethostbyname. | ||
36 | -- | Get a 'HostEntry' corresponding to the given address and family. | ||
37 | -- Note that only IPv4 is currently supported. | ||
38 | getHostByAddr :: Family -> SockAddr -> IO HostEntry | ||
39 | getHostByAddr family addr = do | ||
40 | withSockAddr addr $ \ ptr_addr len -> withLock $ do | ||
41 | throwNoSuchThingIfNull "getHostByAddr" "no such host entry" | ||
42 | $ trySysCall $ c_gethostbyaddr ptr_addr (fromIntegral len) (packFamily family) | ||
43 | >>= peek | ||
44 | -} | ||
45 | |||
46 | |||
47 | -- The locking of gethostbyaddr is similar to gethostbyname. | ||
48 | -- | Get a 'HostEntry' corresponding to the given address and family. | ||
49 | -- Note that only IPv4 is currently supported. | ||
50 | -- getHostByAddr :: Family -> HostAddress -> IO HostEntry | ||
51 | -- getHostByAddr family addr = do | ||
52 | getHostByAddr :: SockAddr -> IO HostEntry | ||
53 | getHostByAddr (SockAddrInet port addr ) = do | ||
54 | let family = AF_INET | ||
55 | with addr $ \ ptr_addr -> withLock $ do | ||
56 | throwNoSuchThingIfNull "getHostByAddr" "no such host entry" | ||
57 | $ trySysCall $ c_gethostbyaddr ptr_addr (fromIntegral (sizeOf addr)) (packFamily family) | ||
58 | >>= peek | ||
59 | getHostByAddr (SockAddrInet6 port flow (a,b,c,d) scope) = do | ||
60 | let family = AF_INET6 | ||
61 | allocaBytes 16 $ \ ptr_addr -> do | ||
62 | pokeElemOff ptr_addr 0 a | ||
63 | pokeElemOff ptr_addr 1 b | ||
64 | pokeElemOff ptr_addr 2 c | ||
65 | pokeElemOff ptr_addr 3 d | ||
66 | withLock $ do | ||
67 | throwNoSuchThingIfNull "getHostByAddr" "no such host entry" | ||
68 | $ trySysCall $ c_gethostbyaddr ptr_addr 16 (packFamily family) | ||
69 | >>= peek | ||
70 | |||
71 | |||
72 | foreign import ccall safe "gethostbyaddr" | ||
73 | c_gethostbyaddr :: Ptr a -> CInt -> CInt -> IO (Ptr HostEntry) | ||
74 | |||
75 | |||
76 | |||
77 | -- vim:ft=haskell: | ||