summaryrefslogtreecommitdiff
path: root/Presence/DNSCache.hs
blob: c5154e34e40b154002d1f9377df04eb1f26f7441 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
-- | Both 'getAddrInfo' and 'getHostByAddr' have hard-coded timeouts for
-- waiting upon network queries that can be a little too long for some use
-- cases. This module wraps both of them so that they block for at most one
-- second.  It caches late-arriving results so that they can be returned by
-- repeated timed-out queries.
--
-- In order to achieve the shorter timeout, it is likely that the you will need
-- to build with GHC's -threaded option.  Otherwise, if the wrapped FFI calls
-- to resolve the address will block Haskell threads.  Note: I didn't verify
-- this.
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE RankNTypes    #-}
{-# LANGUAGE CPP           #-}
module DNSCache
    ( DNSCache
    , reverseResolve
    , forwardResolve
    , newDNSCache
    , parseAddress
    , unsafeParseAddress
    , strip_brackets
    , withPort
    ) where

#ifdef THREAD_DEBUG
import Control.Concurrent.Lifted.Instrument
#else
import Control.Concurrent.Lifted
import GHC.Conc                  (labelThread)
#endif
import Control.Arrow
import Control.Concurrent.STM
import Data.Text       ( Text )
import Network.Socket  ( SockAddr(..), AddrInfoFlag(..), defaultHints, getAddrInfo, AddrInfo(..) )
import Data.Time.Clock ( UTCTime, getCurrentTime, diffUTCTime )
import System.IO.Error ( isDoesNotExistError )
import System.Endian   ( fromBE32, toBE32 )
import Control.Exception ( handle )
import Data.Map        ( Map )
import qualified Data.Map    as Map
import qualified Network.BSD as BSD
import qualified Data.Text   as Text
import Control.Monad
import Data.Function
import Data.List
import Data.Ord
import Data.Maybe
import System.IO.Error
import System.IO.Unsafe

import SockAddr      ()
import ControlMaybe  ( handleIO_ )
import GetHostByAddr ( getHostByAddr )
import InterruptibleDelay
import DPut

type TimeStamp = UTCTime

data DNSCache =
    DNSCache
    { fcache :: TVar (Map Text [(TimeStamp, SockAddr)])
    , rcache :: TVar (Map SockAddr [(TimeStamp, Text)])
    }


newDNSCache :: IO DNSCache
newDNSCache = do
        fcache <- newTVarIO Map.empty
        rcache <- newTVarIO Map.empty
        return DNSCache { fcache=fcache, rcache=rcache }

updateCache :: Eq x =>
    Bool -> TimeStamp -> [x] -> Maybe [(TimeStamp,x)] -> Maybe [(TimeStamp,x)]
updateCache withScrub utc xs mys = do
    let ys = maybe [] id mys
        ys' = filter scrub ys
        ys'' = map (utc,) xs ++ ys'
        minute = 60
        scrub (t,x) | withScrub && diffUTCTime utc t < minute = False
        scrub (t,x) | x `elem` xs = False
        scrub _ = True
    guard $ not (null ys'')
    return ys''

dnsObserve :: DNSCache -> Bool -> TimeStamp -> [(Text,SockAddr)] -> STM ()
dnsObserve dns withScrub utc obs = do
    f <- readTVar $ fcache dns
    r <- readTVar $ rcache dns
    let obs' = map (\(n,a)->(n,a `withPort` 0)) obs
        gs = do
            g <- groupBy ((==) `on` fst) $ sortBy (comparing fst) obs'
            (n,_) <- take 1 g
            return (n,map snd g)
        f' = foldl' updatef f gs
        hs = do
            h <- groupBy ((==) `on` snd) $ sortBy (comparing snd) obs'
            (_,a) <- take 1 h
            return (a,map fst h)
        r' = foldl' updater r hs
    writeTVar (fcache dns) f'
    writeTVar (rcache dns) r'
 where
    updatef f (n,addrs) = Map.alter (updateCache withScrub utc addrs) n f
    updater r (a,ns) = Map.alter (updateCache withScrub utc ns) a r

make6mapped4 :: SockAddr -> SockAddr
make6mapped4 addr@(SockAddrInet6 {}) = addr
make6mapped4 addr@(SockAddrInet port a) = SockAddrInet6 port 0 (0,0,0xFFFF,fromBE32 a) 0

tryForkOS :: IO () -> IO ThreadId
tryForkOS action = catchIOError (forkOS action) $ \e -> do
    dput XMisc $ "DNSCache: Link with -threaded to avoid excessively long time-out."
    forkIO action


-- Attempt to resolve the given domain name.  Returns an empty list if the
-- resolve operation takes longer than the timeout, but the 'DNSCache' will be
-- updated when the resolve completes.
--
-- When the resolve operation does complete, any entries less than a minute old
-- will be overwritten with the new results.  Older entries are allowed to
-- persist for reasons I don't understand as of this writing. (See 'updateCache')
rawForwardResolve ::
    DNSCache -> (Text -> IO ()) -> Int -> Text -> IO [SockAddr]
rawForwardResolve dns onFail timeout addrtext = do
    r <- atomically newEmptyTMVar
    mvar <- interruptibleDelay
    rt <- tryForkOS $ do
        myThreadId >>= flip labelThread ("resolve."++show addrtext)
        resolver r mvar
    startDelay mvar timeout
    did <- atomically $ tryPutTMVar r []
    when did (onFail addrtext)
    atomically $ readTMVar r
 where
    resolver r mvar = do
      xs <- handle (\e -> let _ = isDoesNotExistError e in return [])
            $ do fmap (nub . map (make6mapped4 . addrAddress)) $ 
                  getAddrInfo (Just $ defaultHints { addrFlags = [ AI_CANONNAME, AI_V4MAPPED ]})
                    (Just $ Text.unpack $ strip_brackets addrtext)
                    (Just "5269")
      did <- atomically $ tryPutTMVar r xs
      when did $ do
      interruptDelay mvar
      utc <- getCurrentTime
      atomically $ dnsObserve dns True utc $ map (addrtext,) xs
      return ()

strip_brackets :: Text -> Text
strip_brackets s =
      case Text.uncons s of
          Just ('[',t) -> Text.takeWhile (/=']') t
          _            -> s


reportTimeout :: forall a. Show a => a -> IO ()
reportTimeout addrtext = do
      dput XMisc $ "timeout resolving: "++show addrtext
      -- killThread rt

unmap6mapped4 :: SockAddr -> SockAddr
unmap6mapped4 addr@(SockAddrInet6 port _ (0,0,0xFFFF,a) _) =
    SockAddrInet port (toBE32 a)
unmap6mapped4 addr = addr

rawReverseResolve ::
    DNSCache -> (SockAddr -> IO ()) -> Int -> SockAddr -> IO [Text]
rawReverseResolve dns onFail timeout addr = do
    r <- atomically newEmptyTMVar
    mvar <- interruptibleDelay
    rt <- forkOS $ resolver r mvar
    startDelay mvar timeout
    did <- atomically $ tryPutTMVar r []
    when did (onFail addr)
    atomically $ readTMVar r
 where
    resolver r mvar = 
      handleIO_ (return ()) $ do
        ent <- getHostByAddr (unmap6mapped4 addr) --  AF_UNSPEC addr
        let names = BSD.hostName ent : BSD.hostAliases ent
            xs = map Text.pack $ nub names
        forkIO $ do
            utc <- getCurrentTime
            atomically $ dnsObserve dns False utc $ map (,addr) xs
        atomically $ putTMVar r  xs

-- Returns expired (older than a minute) cached reverse-dns results
-- and removes them from the cache.
expiredReverse :: DNSCache -> SockAddr -> IO [Text]
expiredReverse dns addr = do
    utc <- getCurrentTime
    addr <- return $ addr `withPort` 0
    es <- atomically $ do
        r <- readTVar $ rcache dns
        let ns = maybe [] id $ Map.lookup addr r
            minute = 60 -- seconds
            -- XXX: Is this right?  flip diffUTCTime utc returns the age of the 
            -- cache entry?
            (es0,ns') = partition ( (>=minute) . flip diffUTCTime utc . fst ) ns
            es = map snd es0
        modifyTVar' (rcache dns) $ Map.insert addr ns'
        f <- readTVar $ fcache dns
        let f' = foldl' (flip $ Map.alter (expire utc)) f es
            expire utc Nothing   = Nothing
            expire utc (Just as) = if null as' then Nothing else Just as'
                where as' = filter ( (<minute) . flip diffUTCTime utc . fst) as
        writeTVar (fcache dns) f'
        return es
    return es

cachedReverse :: DNSCache -> SockAddr -> IO [Text]
cachedReverse dns addr = do
    utc <- getCurrentTime
    addr <- return $ addr `withPort` 0
    atomically $ do
        r <- readTVar (rcache dns)
        let ns = maybe [] id $ Map.lookup addr r
        {-
            ns' = filter ( (<minute) . flip diffUTCTime utc . fst) ns
            minute = 60 -- seconds
        modifyTVar' (rcache dns) $ Map.insert addr ns'
        return $ map snd ns'
        -}
        return $ map snd ns

-- Returns any dns query results for the given name that were observed less
-- than a minute ago and updates the forward-cache to remove any results older
-- than that.
cachedForward :: DNSCache -> Text -> IO [SockAddr]
cachedForward dns n = do
    utc <- getCurrentTime
    atomically $ do
        f <- readTVar (fcache dns)
        let as = maybe [] id $ Map.lookup n f
            as' = filter ( (<minute) . flip diffUTCTime utc . fst) as
            minute = 60 -- seconds
        modifyTVar' (fcache dns) $ Map.insert n as'
        return $ map snd as'

-- Reverse-resolves an address to a domain name.  Returns both the result of a
-- new query and any freshly cached results.  Cache entries older than a minute
-- will not be returned, but will be refreshed in spawned threads so that they
-- may be available for the next call.
reverseResolve :: DNSCache -> SockAddr -> IO [Text]
reverseResolve dns addr = do
    expired <- expiredReverse dns addr
    forM_ expired $ \n -> forkIO $ do
        rawForwardResolve dns (const $ return ()) 1000000 n
        return ()
    xs <- rawReverseResolve dns (const $ return ()) 1000000 addr
    cs <- cachedReverse dns addr
    return $ xs ++ filter (not . flip elem xs) cs

-- Resolves a name, if there's no result within one second, then any cached
-- results that are less than a minute old are returned.
forwardResolve :: DNSCache -> Text -> IO [SockAddr]
forwardResolve dns n = do
    as <- rawForwardResolve dns (const $ return ()) 1000000 n
    if null as
      then cachedForward dns n
      else return as

parseAddress :: Text -> IO (Maybe SockAddr)
parseAddress addr_str = do
    info <- getAddrInfo (Just $ defaultHints { addrFlags = [ AI_NUMERICHOST ] }) 
                        (Just . Text.unpack $ addr_str) 
                        (Just "0")
    return . listToMaybe $ map addrAddress info


splitAtPort :: String -> (String,String)
splitAtPort s = second sanitizePort $ case s of
    ('[':t) -> break (==']') t
    _       -> break (==':') s
 where
    sanitizePort (']':':':p) = p
    sanitizePort (':':p)     = p
    sanitizePort _           = "0"

unsafeParseAddress :: String -> Maybe SockAddr
unsafeParseAddress addr_str = unsafePerformIO $ do
    let (ipstr,portstr) = splitAtPort addr_str
    info <- getAddrInfo (Just $ defaultHints { addrFlags = [ AI_NUMERICHOST ] })
                        (Just ipstr)
                        (Just portstr)
    return . listToMaybe $ map addrAddress info

withPort :: SockAddr -> Int -> SockAddr
withPort (SockAddrInet _ a)      port = SockAddrInet (toEnum port) a
withPort (SockAddrInet6 _ a b c) port = SockAddrInet6 (toEnum port) a b c