summaryrefslogtreecommitdiff
path: root/Presence
diff options
context:
space:
mode:
Diffstat (limited to 'Presence')
-rw-r--r--Presence/DNSCache.hs33
1 files changed, 13 insertions, 20 deletions
diff --git a/Presence/DNSCache.hs b/Presence/DNSCache.hs
index 82e0cb56..aaf1a7be 100644
--- a/Presence/DNSCache.hs
+++ b/Presence/DNSCache.hs
@@ -33,7 +33,7 @@ import Network.Socket ( SockAddr(..), AddrInfoFlag(..), defaultHints, getAddrIn
33import Data.Time.Clock ( UTCTime, getCurrentTime, diffUTCTime ) 33import Data.Time.Clock ( UTCTime, getCurrentTime, diffUTCTime )
34import System.IO.Error ( isDoesNotExistError ) 34import System.IO.Error ( isDoesNotExistError )
35import System.Endian ( fromBE32, toBE32 ) 35import System.Endian ( fromBE32, toBE32 )
36import Control.Exception ( handle, ErrorCall(..) ) 36import Control.Exception ( handle )
37import Data.Map ( Map ) 37import Data.Map ( Map )
38import qualified Data.Map as Map 38import qualified Data.Map as Map
39import qualified Network.BSD as BSD 39import qualified Network.BSD as BSD
@@ -49,6 +49,7 @@ import System.IO.Error
49import SockAddr () 49import SockAddr ()
50import ControlMaybe ( handleIO_ ) 50import ControlMaybe ( handleIO_ )
51import GetHostByAddr ( getHostByAddr ) 51import GetHostByAddr ( getHostByAddr )
52import InterruptibleDelay
52 53
53type TimeStamp = UTCTime 54type TimeStamp = UTCTime
54 55
@@ -118,16 +119,15 @@ tryForkOS action = catchIOError (forkOS action) $ \e -> do
118-- persist for reasons I don't understand as of this writing. (See 'updateCache') 119-- persist for reasons I don't understand as of this writing. (See 'updateCache')
119rawForwardResolve :: 120rawForwardResolve ::
120 DNSCache -> (Text -> IO ()) -> Int -> Text -> IO [SockAddr] 121 DNSCache -> (Text -> IO ()) -> Int -> Text -> IO [SockAddr]
121rawForwardResolve dns fail timeout addrtext = do 122rawForwardResolve dns onFail timeout addrtext = do
122 r <- atomically newEmptyTMVar 123 r <- atomically newEmptyTMVar
123 mvar <- atomically newEmptyTMVar 124 mvar <- interruptibleDelay
124 rt <- tryForkOS $ do 125 rt <- tryForkOS $ do
125 myThreadId >>= flip labelThread ("resolve."++show addrtext) 126 myThreadId >>= flip labelThread ("resolve."++show addrtext)
126 resolver r mvar 127 resolver r mvar
127 -- TODO: System.Timeout.timeout might be more appropriate than this 128 startDelay mvar timeout
128 -- hack involving throwTo (ErrorCall "Interrupteddelay"). 129 did <- atomically $ tryPutTMVar r []
129 tt <- forkIO $ timer (fail addrtext) timeout r rt 130 when did (onFail addrtext)
130 atomically $ putTMVar mvar tt
131 atomically $ readTMVar r 131 atomically $ readTMVar r
132 where 132 where
133 resolver r mvar = do 133 resolver r mvar = do
@@ -138,8 +138,7 @@ rawForwardResolve dns fail timeout addrtext = do
138 (Just "5269") 138 (Just "5269")
139 did <- atomically $ tryPutTMVar r xs 139 did <- atomically $ tryPutTMVar r xs
140 when did $ do 140 when did $ do
141 tt <- atomically $ readTMVar mvar 141 interruptDelay mvar
142 throwTo tt (ErrorCall "Interrupted delay")
143 utc <- getCurrentTime 142 utc <- getCurrentTime
144 atomically $ dnsObserve dns True utc $ map (addrtext,) xs 143 atomically $ dnsObserve dns True utc $ map (addrtext,) xs
145 return () 144 return ()
@@ -156,13 +155,6 @@ reportTimeout addrtext = do
156 hPutStrLn stderr $ "timeout resolving: "++show addrtext 155 hPutStrLn stderr $ "timeout resolving: "++show addrtext
157 -- killThread rt 156 -- killThread rt
158 157
159timer :: forall t a. IO () -> Int -> TMVar [a] -> t -> IO ()
160timer fail timeout r rt = do
161 handle (\(ErrorCall _)-> return ()) $ do
162 threadDelay timeout
163 did <- atomically $ tryPutTMVar r []
164 when did fail
165
166unmap6mapped4 :: SockAddr -> SockAddr 158unmap6mapped4 :: SockAddr -> SockAddr
167unmap6mapped4 addr@(SockAddrInet6 port _ (0,0,0xFFFF,a) _) = 159unmap6mapped4 addr@(SockAddrInet6 port _ (0,0,0xFFFF,a) _) =
168 SockAddrInet port (toBE32 a) 160 SockAddrInet port (toBE32 a)
@@ -170,12 +162,13 @@ unmap6mapped4 addr = addr
170 162
171rawReverseResolve :: 163rawReverseResolve ::
172 DNSCache -> (SockAddr -> IO ()) -> Int -> SockAddr -> IO [Text] 164 DNSCache -> (SockAddr -> IO ()) -> Int -> SockAddr -> IO [Text]
173rawReverseResolve dns fail timeout addr = do 165rawReverseResolve dns onFail timeout addr = do
174 r <- atomically newEmptyTMVar 166 r <- atomically newEmptyTMVar
175 mvar <- atomically newEmptyTMVar 167 mvar <- interruptibleDelay
176 rt <- forkOS $ resolver r mvar 168 rt <- forkOS $ resolver r mvar
177 tt <- forkIO $ timer (fail addr) timeout r rt 169 startDelay mvar timeout
178 atomically $ putTMVar mvar tt 170 did <- atomically $ tryPutTMVar r []
171 when did (onFail addr)
179 atomically $ readTMVar r 172 atomically $ readTMVar r
180 where 173 where
181 resolver r mvar = 174 resolver r mvar =