summaryrefslogtreecommitdiff
path: root/dht/src/Network/Tox/AggregateSession.hs
blob: 44bbf9b94e8bf7c27d40106a81485e5346e2dee9 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
-- | This module aggregates all sessions to the same remote Tox contact into a
-- single online/offline presence.  This allows multiple lossless links to the
-- same identity at different addresses, or even to the same address.
{-# LANGUAGE CPP             #-}
{-# LANGUAGE GADTs           #-}
{-# LANGUAGE LambdaCase      #-}
{-# LANGUAGE PatternSynonyms #-}
module Network.Tox.AggregateSession
    ( AggregateSession(contactSession)
    , SingleCon(singleSession)
    , newAggregateSession
    , aggregateStatus
    , checkCompatible
    , compatibleKeys
    , AddResult(..)
    , addSession
    , DelResult(..)
    , delSession
    , closeAll
    , awaitAny
    , dispatchMessage
    ) where


import Control.Concurrent.STM
import Control.Concurrent.STM.TMChan
import Control.Exception
import Control.Monad
import Data.Dependent.Sum
import Data.Function
import Data.Functor
import qualified Data.IntMap.Strict  as IntMap
         ;import Data.IntMap.Strict  (IntMap)
import Data.List
import Data.Maybe
import Data.Time.Clock.POSIX
import System.IO.Error

#ifdef THREAD_DEBUG
import Control.Concurrent.Lifted.Instrument
#else
import Control.Concurrent.Lifted
import GHC.Conc                  (labelThread)
#endif

import Connection                   (Status (..))
import Crypto.Tox                   (PublicKey, toPublic)
import Data.Tox.Msg
import Data.Wrapper.PSQInt          as PSQ
import DPut
import DebugTag
import Network.QueryResponse
import Network.Tox.Crypto.Transport
import Network.Tox.DHT.Transport    (key2id)
import Network.Tox.NodeId           (ToxProgress (..))
import Network.Tox.Session

-- | For each component session, we track the current status.
data SingleCon = SingleCon
    { singleSession :: Session                   -- ^ A component session.
    , singleStatus  :: TVar (Status ToxProgress) -- ^ Either 'AwaitingSessionPacket' or 'Established'.
    }

-- | A collection of sessions between the same local and remote identities.
data AggregateSession = AggregateSession
    { -- | The set of component sessions indexed by their ID.
      contactSession     :: TVar (IntMap SingleCon)
      -- | Each inbound packets is written to this channel with the session ID
      -- from which it came originally.
    , contactChannel     :: TMChan (Int,CryptoMessage)
      -- | The set of 'Established' sessions IDs.
    , contactEstablished :: TVar (IntMap ())
      -- | Callback for state-change notifications.
    , notifyState        :: AggregateSession -> Session -> Status ToxProgress -> STM ()
    }


-- | Create a new empty aggregate session.  The argument is a callback to
-- receive notifications when the new session changes status.  There are three
-- possible status values:
--
--   [ Dormant ] - No pending or established sessions.
--
--   [ InProgress AwaitingSessionPacket ] - Sessions are pending, but none are
--                                          fully established.
--
--   [ Established ] - At least one session is fully established and we can
--                     send and receive packets via this aggregate.
--
-- The 'Session' object is provided to the callback so that it can determine the
-- current remote and local identities for this AggregateSession.  It may not even
-- be Established, so do not use it to send or receive packets.
newAggregateSession :: (AggregateSession -> Session -> Status ToxProgress -> STM ())
                        -> STM AggregateSession
newAggregateSession notify = do
    vimap <- newTVar IntMap.empty
    chan <- newTMChan
    vemap <- newTVar IntMap.empty
    return AggregateSession
        { contactSession     = vimap
        , contactChannel     = chan
        , contactEstablished = vemap
        , notifyState        = notify
        }

-- | Information returned from 'addSession'.  Note that a value other than
-- 'RejectedSession' does not mean there is any 'Established' session in the
-- Aggregate.  Sessions are in 'AwaitingSessionPacket' state until a single
-- packet is received from the remote end.
data AddResult = FirstSession    -- ^ Initial connection with this contact.
               | AddedSession    -- ^ Added another connection to active session.
               | RejectedSession -- ^ Failed to add session (wrong contact / closed session).
    deriving (Eq,Show)

-- | The 'keepAlive' thread juggles three scheduled tasks.
data KeepAliveEvents = DoTimeout        -- ^ A session timed-out, close it.
                     | DoAlive          -- ^ Send a the keep-alive becon for a session.
                     | DoRequestMissing -- ^ Detect and request lost packets.
                     deriving Enum

-- | This call loops until the provided session is closed or times out.  It
-- monitors the provided (non-empty) priority queue for scheduled tasks (see
-- 'KeepAliveEvents') to perform for the connection.
keepAlive :: Session -> TVar (PSQ POSIXTime) -> IO ()
keepAlive s q = do
    myThreadId >>= flip labelThread
        (intercalate "." ["beacon"
                         , take 8 $ show $ key2id $ sTheirUserKey s
                         , show $ sSessionID s])

    let -- outPrint e   = dput XNetCrypto  $ shows (sSessionID s,sTheirAddr s) $ " <-- " ++ e
        unexpected e = dput XUnexpected $ shows (sSessionID s,sTheirAddr s) $ " <-- " ++ e

        doAlive = do
            -- outPrint $ "Beacon"
            sendMessage (sTransport s) () (Pkt ALIVE ==> ())

        doRequestMissing = do
            (ns,nmin) <- sMissingInbound s
            -- outPrint $ "PacketRequest " ++ show (nmin,ns)
            sendMessage (sTransport s) () (Pkt PacketRequest ==> MissingPackets ns)
                `catchIOError` \e -> do
                    unexpected $ "PacketRequest " ++ take 200 (show (nmin,length ns,ns))
                    unexpected $ "PacketRequest: " ++ show e
                    -- Quit thread by scheduling a timeout event.
                    now <- getPOSIXTime
                    atomically $ modifyTVar' q $ PSQ.insert (fromEnum DoTimeout) now

        re tm e io = do
            io
            atomically $ modifyTVar' q $ PSQ.insert (fromEnum e) tm

        doEvent again now e = case e of
            DoTimeout        -> do dput XNetCrypto $ "TIMEOUT: " ++ show (sSessionID s)
                                   sClose s
            DoAlive          -> re (now + 10) e doAlive >> again
            DoRequestMissing -> re (now + 5{- toxcore uses 1sec -}) e doRequestMissing >> again

    fix $ \again -> do

    now <- getPOSIXTime
    join $ atomically $ do
        PSQ.findMin <$> readTVar q >>= \case
          Nothing           -> return $ do dput XUnexpected "keepAlive: unexpected empty PSQ."
                                           sClose s
          Just ( k :-> tm ) ->
            return $ if now < tm then threadDelay (toMicroseconds $ tm - now) >> again
                                 else doEvent again now (toEnum k)


-- | This function forks two threads: the 'keepAlive' beacon-sending thread and
-- a thread to read all packets from the provided 'Session' and forward them to
-- 'contactChannel' for a containing 'AggregateSession'
forkSession :: AggregateSession -> Session -> (Status ToxProgress -> STM ()) -> IO ThreadId
forkSession c s setStatus = forkIO $ do
    myThreadId >>= flip labelThread
        (intercalate "." ["s"
                         , take 8 $ show $ key2id $ sTheirUserKey s
                         , show $ sSessionID s])

    q <- atomically $ newTVar $ fromList
            [ fromEnum DoAlive :-> 0
            , fromEnum DoRequestMissing :-> 0
            ]

    let sendPacket :: CryptoMessage -> STM ()
        sendPacket msg = writeTMChan (contactChannel c) (sSessionID s, msg)

        inPrint e = dput XNetCrypto $ shows (sSessionID s,sTheirAddr s) $ " --> " ++ e

        bump = do
            -- inPrint $ "BUMP: " ++ show (sSessionID s)
            now <- getPOSIXTime
            atomically $ modifyTVar' q $ PSQ.insert (fromEnum DoTimeout) (now + 15)

        onPacket body loop Terminated     = return ()
        onPacket body loop (ParseError e) = inPrint e >> loop
        onPacket body loop (Arrival _ x)  = body loop x

        awaitPacket body = fix $ \loop -> do
            (m,io) <- atomically $ awaitMessage (sTransport s)
            io
            onPacket body loop m

    atomically $ setStatus $ InProgress AwaitingSessionPacket
    awaitPacket $ \_ online -> do
        when (msgID online /= M ONLINE) $ do
            inPrint $ "Unexpected initial packet: " ++ show (msgID online)
        atomically $ do setStatus Established
                        sendPacket online
        bump
        beacon <- forkIO $ keepAlive s q `finally` sClose s
        awaitPacket $ \awaitNext x -> do
            bump
            case msgID x of
                M ALIVE      -> return ()
                M KillPacket -> sClose s
                _            -> atomically $ sendPacket x
            awaitNext
        atomically $ setStatus Dormant
        killThread beacon

sessionIsPreferredTo :: Session -> Session -> Bool
sessionIsPreferredTo s t = True -- TODO: Check address types.

-- | Add a new session (in 'AwaitingSessionPacket' state) to the
-- 'AggregateSession'.  If the supplied session is not compatible because it is
-- between the wrong ToxIDs or because the AggregateSession is closed,
-- 'RejectedSession' will be returned.  Otherwise, the operation is successful.
--
-- The status-change callback may be triggered by this call as the aggregate
-- may transition from 'Dormant' (empty) to 'AwaitingSessionPacket' (at least
-- one active session).
addSession :: AggregateSession -> Session -> IO AddResult
addSession c s = do
    (result,mcon,rejected,closed) <- atomically $ do
        let them = sTheirUserKey s
            me   = toPublic $ sOurKey s
        result <- checkCompatible me them c <&> \case
                        Nothing      -> FirstSession
                        Just True    -> AddedSession
                        Just False   -> RejectedSession
        case result of
            RejectedSession -> return (result,Nothing,Just s,Nothing)
            _ -> do
                statvar <- newTVar Dormant
                imap <- readTVar (contactSession c)
                let nodekey = sTheirDHTKey s
                    ts = [ t | SingleCon t _ <- IntMap.elems imap, nodekey == sTheirDHTKey t ]
                case ts of
                    [] -> do
                        let con = SingleCon s statvar
                            s0 = IntMap.lookup (sSessionID s) imap
                            imap' = IntMap.insert (sSessionID s) con imap
                        writeTVar (contactSession c) imap'
                        return (result,Just con,singleSession <$> s0, Nothing)
                    (t:_) | s `sessionIsPreferredTo` t -> do
                        let con = SingleCon s statvar
                            s0 = IntMap.lookup (sSessionID s) imap
                            imap' = IntMap.insert (sSessionID s) con imap
                        writeTVar (contactSession c) imap'
                        return (result,Just con,singleSession <$> s0, Just (sSessionID t))
                    (t:_) -> do
                        error "TODO: Prefer older session (udp over tcp for example)"
    mapM_ sClose rejected
    when (isNothing mcon) $ dput XMan "addSession: Rejected session!"
    forM_ (mcon :: Maybe SingleCon) $ \con -> do
        dput XMan $ "addSession: forkSession! " ++ show result
        forkSession c s $ \progress -> do
            status0 <- aggregateStatus c
            writeTVar (singleStatus con) progress
            emap' <- do
                emap <- readTVar (contactEstablished c)
                return $ case progress of
                  Established -> IntMap.insert (sSessionID s) () emap
                  _           -> IntMap.delete (sSessionID s) emap
            writeTVar (contactEstablished c) emap'
            status <- aggregateStatus c
            when (status /= status0) $ notifyState c c s status
    mapM_ (delSession c) closed

    return result

-- | Information returned from 'delSession'.
data DelResult = NoSession      -- ^ Contact is completely disconnected.
               | DeletedSession -- ^ Connection removed but session remains active.

-- | Close and remove the componenent session corresponding to the provided
-- Session ID.
--
-- The status-change callback may be triggered as the aggregate may may
-- transition to 'Dormant' (empty) or 'AwaitingSessionPacket' (if the last
-- 'Established' session is closed).
delSession :: AggregateSession -> Int -> IO DelResult
delSession c sid = do
    (con, r) <- atomically $ do
        imap <- readTVar (contactSession c)
        emap <- readTVar (contactEstablished c)
        let emap' = IntMap.delete sid emap
            imap' = IntMap.delete sid imap
        case IntMap.toList emap of
            (sid0,_):_ | IntMap.null emap'
                       , let s = singleSession $ imap IntMap.! sid0
                -> notifyState c c s
                        $ if IntMap.null imap' then Dormant
                                               else InProgress AwaitingSessionPacket
            _   -> return ()
        writeTVar (contactSession c) imap'
        writeTVar (contactEstablished c) emap'
        return ( IntMap.lookup sid imap, IntMap.null imap')
    mapM_ (sClose . singleSession) con
    return $ if r then NoSession
                  else DeletedSession

-- | Send a packet to one or all of the component sessions in the aggregate.
dispatchMessage :: AggregateSession -> Maybe Int -- ^ 'Nothing' to broadcast, otherwise SessionID.
                                    -> CryptoMessage -> IO ()
dispatchMessage c msid msg = join $ atomically $ do
    imap <- readTVar (contactSession c)
    let go = case msid of Nothing  -> forM_ imap
                          Just sid -> forM_ (IntMap.lookup sid imap)
    return $ go $ \con -> sendMessage (sTransport $ singleSession con) () msg

-- | Retry until:
--
--      * a packet arrives (with component session ID) arrives.
--
--      * the 'AggregateSession' is closed with 'closeAll'.
awaitAny :: AggregateSession -> STM (Maybe (Int,CryptoMessage))
awaitAny c = readTMChan (contactChannel c)

-- | Close all connections associated with the aggregate.  No new sessions will
-- be accepted after this, and the notify callback will be informed that we've
-- transitioned to 'Dormant'.
closeAll :: AggregateSession -> IO ()
closeAll c = join $ atomically $ do
    imap <- readTVar (contactSession c)
    closeTMChan (contactChannel c)
    forM_ (listToMaybe $ IntMap.elems imap) $ \(SingleCon s _) -> do
        notifyState c c s Dormant
    return $ forM_ (IntMap.toList imap) $ \(sid,SingleCon s _) -> do
        sClose s
        delSession c sid

-- | Query the current status of the aggregate, there are three possible
-- values:
--
--   [ Dormant ] - No pending or established sessions.
--
--   [ InProgress AwaitingSessionPacket ] - Sessions are pending, but none are
--                                          fully established.
--
--   [ Established ] - At least one session is fully established and we can
--                     send and receive packets via this aggregate.
--
aggregateStatus :: AggregateSession -> STM (Status ToxProgress)
aggregateStatus c = do
    isclosed <- isClosedTMChan (contactChannel c)
    imap <- readTVar (contactSession c)
    emap <- readTVar (contactEstablished c)
    return $ case () of
        _ | isclosed               -> Dormant
          | not (IntMap.null emap) -> Established
          | not (IntMap.null imap) -> InProgress AwaitingSessionPacket
          | otherwise              -> Dormant

-- | Query whether the supplied ToxID keys are compatible with this aggregate.
--
--   [ Nothing ]    Any keys would be compatible because there is not yet any
--                  sessions in progress.
--
--   [ Just True ]  The supplied keys match the session in progress.
--
--   [ Just False ] The supplied keys are incompatible.
checkCompatible :: PublicKey    -- ^ Local Tox key (for which we know the secret).
                   -> PublicKey -- ^ Remote Tox key.
                   -> AggregateSession -> STM (Maybe Bool)
checkCompatible me them c = do
    isclosed <- isClosedTMChan (contactChannel c)
    imap <- readTVar (contactSession c)
    return $ case IntMap.elems imap of
                    _ | isclosed -> Just False -- All keys are incompatible (closed).
                    con:_        -> Just $ sTheirUserKey (singleSession con)         == them
                                           && toPublic (sOurKey $ singleSession con) == me
                    []           -> Nothing

-- | Returns the local and remote keys that are compatible with this aggregate.
-- If 'Nothing' Is returned, then either no key is compatible ('closeAll' was
-- called) or all keys are compatible because no sessions have been associated.
compatibleKeys :: AggregateSession -> STM (Maybe (PublicKey,PublicKey))
compatibleKeys c = do
    isclosed <- isClosedTMChan (contactChannel c)
    imap <- readTVar (contactSession c)
    return $ case IntMap.elems imap of
                    _ | isclosed -> Nothing -- none.
                    con:_        -> Just ( toPublic (sOurKey $ singleSession con)
                                         , sTheirUserKey (singleSession con))
                    []           -> Nothing -- any.