summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2020-01-17 00:22:08 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-17 00:25:22 -0500
commit17c32ae0dcb1c10cfd3b82ceaf10ca5d3990b10b (patch)
treeaa934bf86e1e8caa408a903c801f1c3aaf564f5c
parent6f72701a1f67132649236513959791d8ff4a884f (diff)
Discard unusable Dormant AggregateSession objects.
-rw-r--r--dht/ToxManager.hs20
-rw-r--r--dht/examples/dhtd.hs19
-rw-r--r--dht/src/Network/Tox/AggregateSession.hs8
3 files changed, 33 insertions, 14 deletions
diff --git a/dht/ToxManager.hs b/dht/ToxManager.hs
index 25a4f0f2..126efa36 100644
--- a/dht/ToxManager.hs
+++ b/dht/ToxManager.hs
@@ -134,7 +134,8 @@ toxman ssvar announcer tox presence = ToxManager
134 toxAnnounceInterval) 134 toxAnnounceInterval)
135 pub 135 pub
136 136
137 forkAccountWatcher ssvar 137 forkIO $ do
138 accountWatcher ssvar
138 (TCP.tcpClient $ tcpKademliaClient $ toxOnionRoutes tox) 139 (TCP.tcpClient $ tcpKademliaClient $ toxOnionRoutes tox)
139 acnt 140 acnt
140 tox 141 tox
@@ -155,6 +156,11 @@ toxman ssvar announcer tox presence = ToxManager
155 -- Remove this xmpp client /k/ from the set holding this 156 -- Remove this xmpp client /k/ from the set holding this
156 -- account active. 157 -- account active.
157 modifyTVar' (accountExtra acnt) $ Map.delete k 158 modifyTVar' (accountExtra acnt) $ Map.delete k
159 is_last <- Map.null <$> readTVar (accountExtra acnt)
160 when is_last $ do
161 -- Forget secret key if this was the last client.
162 -- This ensures that incoming connections are rejected.
163 modifyTVar' accounts $ HashMap.delete pubid
158 return rs 164 return rs
159 return $ 165 return $
160 if (Map.null $ Map.delete k refs) then 166 if (Map.null $ Map.delete k refs) then
@@ -165,7 +171,7 @@ toxman ssvar announcer tox presence = ToxManager
165 let Just pubid = mpubid 171 let Just pubid = mpubid
166 pub = Tox.id2key pubid 172 pub = Tox.id2key pubid
167 -- Stop the announce-toxid task for this account. Note that other 173 -- Stop the announce-toxid task for this account. Note that other
168 -- announced tasks will be stopped by the forkAccountWatcher thread 174 -- announced tasks will be stopped by the accountWatcher thread
169 -- when it terminates. 175 -- when it terminates.
170 cancel announcer akey 176 cancel announcer akey
171 177
@@ -705,12 +711,12 @@ closeSessions me them ssvar = do
705 -- Just True <- checkCompatible ag (id2key me) (id2key them) 711 -- Just True <- checkCompatible ag (id2key me) (id2key them)
706 closeAll ag 712 closeAll ag
707 713
708forkAccountWatcher :: TVar (Map.Map Uniq24 AggregateSession) 714accountWatcher :: TVar (Map.Map Uniq24 AggregateSession)
709 -> TCP.RelayClient 715 -> TCP.RelayClient
710 -> Account JabberClients -> Tox JabberClients -> PresenceState Pending -> Announcer -> IO ThreadId 716 -> Account JabberClients -> Tox JabberClients -> PresenceState Pending -> Announcer -> IO ()
711forkAccountWatcher ssvar tcp acc tox st announcer = forkIO $ do 717accountWatcher ssvar tcp acc tox st announcer = do
712 let me = key2id $ toPublic $ userSecret acc 718 let me = key2id $ toPublic $ userSecret acc
713 dput XMan $ "forkAccountWatcher(" ++ show me ++") started" 719 dput XMan $ "accountWatcher(" ++ show me ++") started"
714 myThreadId >>= flip labelThread ("online:" 720 myThreadId >>= flip labelThread ("online:"
715 ++ show (key2id $ toPublic $ userSecret acc)) 721 ++ show (key2id $ toPublic $ userSecret acc))
716 (chan,cs) <- atomically $ do 722 (chan,cs) <- atomically $ do
@@ -743,7 +749,7 @@ forkAccountWatcher ssvar tcp acc tox st announcer = forkIO $ do
743 forM_ (HashMap.toList cs) $ \(them,c) -> do 749 forM_ (HashMap.toList cs) $ \(them,c) -> do
744 stopConnecting tx (id2key them) "disabled account" 750 stopConnecting tx (id2key them) "disabled account"
745 closeSessions me them ssvar 751 closeSessions me them ssvar
746 dput XMan $ "forkAccountWatcher(" ++ show me ++") stopped" 752 dput XMan $ "accountWatcher(" ++ show me ++") stopped"
747 753
748 754
749toxAnnounceInterval :: POSIXTime 755toxAnnounceInterval :: POSIXTime
diff --git a/dht/examples/dhtd.hs b/dht/examples/dhtd.hs
index f4d04761..de315e35 100644
--- a/dht/examples/dhtd.hs
+++ b/dht/examples/dhtd.hs
@@ -1326,17 +1326,24 @@ onNewToxSession :: (IO () -> STM ())
1326 -> Tox.Session 1326 -> Tox.Session
1327 -> IO () 1327 -> IO ()
1328onNewToxSession runio sv ssvar invc ContactInfo{accounts} addrTox netcrypto = do 1328onNewToxSession runio sv ssvar invc ContactInfo{accounts} addrTox netcrypto = do
1329 dput XMan "onNewToxSession"
1329 let them s = Tox.longTermKey $ runIdentity cookie -- remote tox key 1330 let them s = Tox.longTermKey $ runIdentity cookie -- remote tox key
1330 where Tox.Cookie _ cookie = Tox.handshakeCookie (sReceivedHandshake s) 1331 where Tox.Cookie _ cookie = Tox.handshakeCookie (sReceivedHandshake s)
1331 1332
1332 me s = toPublic $ sOurKey s 1333 me s = toPublic $ sOurKey s
1333 1334
1334 onStatusChange :: (Tox.Session -> Tcp.ConnectionEvent XML.Event -> STM ()) 1335 uniqkey <- xor24 <$> hash24 (them netcrypto) <*> hash24 (me netcrypto)
1336
1337 let onStatusChange :: (Tox.Session -> Tcp.ConnectionEvent XML.Event -> STM ())
1335 -> AggregateSession -> Tox.Session -> Status Tox.ToxProgress -> STM () 1338 -> AggregateSession -> Tox.Session -> Status Tox.ToxProgress -> STM ()
1336 onStatusChange announce c s Established = onConnect announce c s 1339 onStatusChange announce c s Established = onConnect announce c s
1337 onStatusChange announce _ s status = onEOF announce s status 1340 onStatusChange announce _ s status = onEOF announce s status
1338 1341
1339 onEOF announce s status = do 1342 onEOF announce s status = do
1343 case status of
1344 Dormant -> -- Dormant AggregateSession is useless, so discard it.
1345 modifyTVar' ssvar $ Map.delete uniqkey
1346 _ -> return ()
1340 runio $ dput XMan $ "EOF(" ++ take 16 (showKey256 $ them s) ++ "): " ++ show status 1347 runio $ dput XMan $ "EOF(" ++ take 16 (showKey256 $ them s) ++ "): " ++ show status
1341 HashMap.lookup (Tox.key2id $ me s) <$> readTVar accounts 1348 HashMap.lookup (Tox.key2id $ me s) <$> readTVar accounts
1342 >>= mapM_ (setTerminated $ them s) 1349 >>= mapM_ (setTerminated $ them s)
@@ -1364,12 +1371,10 @@ onNewToxSession runio sv ssvar invc ContactInfo{accounts} addrTox netcrypto = do
1364 Chunk x -> Just (Nothing,x)) 1371 Chunk x -> Just (Nothing,x))
1365 .| toxSnk 1372 .| toxSnk
1366 1373
1367 uniqkey <- xor24 <$> hash24 (them netcrypto) <*> hash24 (me netcrypto)
1368
1369 let me_dot_tox = xmppHostname $ me netcrypto 1374 let me_dot_tox = xmppHostname $ me netcrypto
1370 them_dot_tox = xmppHostname $ them netcrypto 1375 them_dot_tox = xmppHostname $ them netcrypto
1371 1376
1372 c <- atomically $ do 1377 c <- join $ atomically $ do
1373 mc <- Map.lookup uniqkey <$> readTVar ssvar 1378 mc <- Map.lookup uniqkey <$> readTVar ssvar
1374 case mc of 1379 case mc of
1375 Nothing -> do 1380 Nothing -> do
@@ -1387,8 +1392,10 @@ onNewToxSession runio sv ssvar invc ContactInfo{accounts} addrTox netcrypto = do
1387 return $ \s e -> writeTChan (xmppEventChannel sv) ( (ck, condta s), e) 1392 return $ \s e -> writeTChan (xmppEventChannel sv) ( (ck, condta s), e)
1388 c <- newAggregateSession $ onStatusChange announce 1393 c <- newAggregateSession $ onStatusChange announce
1389 modifyTVar' ssvar $ Map.insert uniqkey c 1394 modifyTVar' ssvar $ Map.insert uniqkey c
1390 return c 1395 return $ do
1391 Just c -> return c 1396 dput XMan $ "New AggregateSession!"
1397 return c
1398 Just c -> return $ return c
1392 1399
1393 addSession c netcrypto 1400 addSession c netcrypto
1394 1401
diff --git a/dht/src/Network/Tox/AggregateSession.hs b/dht/src/Network/Tox/AggregateSession.hs
index d1f42e91..feb634f0 100644
--- a/dht/src/Network/Tox/AggregateSession.hs
+++ b/dht/src/Network/Tox/AggregateSession.hs
@@ -31,6 +31,7 @@ import Data.Function
31import qualified Data.IntMap.Strict as IntMap 31import qualified Data.IntMap.Strict as IntMap
32 ;import Data.IntMap.Strict (IntMap) 32 ;import Data.IntMap.Strict (IntMap)
33import Data.List 33import Data.List
34import Data.Maybe
34import Data.Time.Clock.POSIX 35import Data.Time.Clock.POSIX
35import System.IO.Error 36import System.IO.Error
36 37
@@ -108,6 +109,7 @@ newAggregateSession notify = do
108data AddResult = FirstSession -- ^ Initial connection with this contact. 109data AddResult = FirstSession -- ^ Initial connection with this contact.
109 | AddedSession -- ^ Added another connection to active session. 110 | AddedSession -- ^ Added another connection to active session.
110 | RejectedSession -- ^ Failed to add session (wrong contact / closed session). 111 | RejectedSession -- ^ Failed to add session (wrong contact / closed session).
112 deriving (Eq,Show)
111 113
112-- | The 'keepAlive' thread juggles three scheduled tasks. 114-- | The 'keepAlive' thread juggles three scheduled tasks.
113data KeepAliveEvents = DoTimeout -- ^ A session timed-out, close it. 115data KeepAliveEvents = DoTimeout -- ^ A session timed-out, close it.
@@ -244,7 +246,9 @@ addSession c s = do
244 return (result,Just con,singleSession <$> s0) 246 return (result,Just con,singleSession <$> s0)
245 247
246 mapM_ sClose rejected 248 mapM_ sClose rejected
247 forM_ (mcon :: Maybe SingleCon) $ \con -> 249 when (isNothing mcon) $ dput XMan "addSession: Rejected session!"
250 forM_ (mcon :: Maybe SingleCon) $ \con -> do
251 dput XMan $ "addSession: forkSession! " ++ show result
248 forkSession c s $ \progress -> do 252 forkSession c s $ \progress -> do
249 status0 <- aggregateStatus c 253 status0 <- aggregateStatus c
250 writeTVar (singleStatus con) progress 254 writeTVar (singleStatus con) progress
@@ -314,6 +318,8 @@ closeAll :: AggregateSession -> IO ()
314closeAll c = join $ atomically $ do 318closeAll c = join $ atomically $ do
315 imap <- readTVar (contactSession c) 319 imap <- readTVar (contactSession c)
316 closeTMChan (contactChannel c) 320 closeTMChan (contactChannel c)
321 forM_ (listToMaybe $ IntMap.elems imap) $ \(SingleCon s _) -> do
322 notifyState c c s Dormant
317 return $ forM_ (IntMap.toList imap) $ \(sid,SingleCon s _) -> do 323 return $ forM_ (IntMap.toList imap) $ \(sid,SingleCon s _) -> do
318 sClose s 324 sClose s
319 delSession c sid 325 delSession c sid