diff options
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/Tox.hs | 34 | ||||
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 9 |
2 files changed, 11 insertions, 32 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs index 3ad2b11e..861d71d3 100644 --- a/src/Network/Tox.hs +++ b/src/Network/Tox.hs | |||
@@ -363,32 +363,6 @@ getOnionAlias crypto dhtself remoteNode = atomically $ do | |||
363 | _ -> ni { nodeId = key2id (onionAliasPublic crypto) } | 363 | _ -> ni { nodeId = key2id (onionAliasPublic crypto) } |
364 | return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing | 364 | return $ Onion.OnionDestination Onion.SearchingAlias alias Nothing |
365 | 365 | ||
366 | mkDefaultDestroyHook :: ContactInfo extra -> NetCryptoSession -> IO () | ||
367 | mkDefaultDestroyHook roster = \session -> do | ||
368 | now <- getPOSIXTime | ||
369 | r <- atomically $ do | ||
370 | accounts <- readTVar (accounts roster) -- :: STM (HashMap NodeId (Account extra) | ||
371 | let mbAccount = HashMap.lookup (key2id $ ncMyPublicKey session) accounts -- :: STM (Maybe (Account extra) | ||
372 | case mbAccount of | ||
373 | Just account -> do | ||
374 | mp <- readTVar (netCryptoSessionsByKey (ncAllSessions session)) | ||
375 | case Map.lookup (ncTheirPublicKey session) mp of | ||
376 | Just sessionsWithThisPublicKey | ||
377 | | relevantSessions <- filter ((/=ncSessionId session) . ncSessionId) sessionsWithThisPublicKey | ||
378 | , not (null relevantSessions) | ||
379 | -> do | ||
380 | let showsession x = printf "%x" (ncSessionId x) | ||
381 | return . Left $ "Not calling setTerminated on " ++ show (key2id (ncTheirPublicKey session)) | ||
382 | ++" despite session(" ++ showsession session | ||
383 | ++ ") failure. (Still have sessions: [" | ||
384 | ++ Data.List.intercalate "," (map showsession relevantSessions) | ||
385 | ++ "]" | ||
386 | _ -> Right <$> setTerminated (ncTheirPublicKey session) account | ||
387 | Nothing -> return . Left $ "(defaultDestroyHook) their is no account! pubkey=" ++ show (key2id (ncTheirPublicKey session)) | ||
388 | case r of | ||
389 | Left msg -> dput XMan msg | ||
390 | _ -> return () | ||
391 | |||
392 | newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. | 366 | newTox :: TVar Onion.AnnouncedKeys -- ^ Store of announced keys we are a rendezvous for. |
393 | -> SockAddr -- ^ Bind-address to listen on. | 367 | -> SockAddr -- ^ Bind-address to listen on. |
394 | -> Maybe NetCryptoSessions -- ^ State of all one-on-one Tox links. | 368 | -> Maybe NetCryptoSessions -- ^ State of all one-on-one Tox links. |
@@ -411,13 +385,9 @@ newToxOverTransport keydb addr mbSessionsState suppliedDHTKey udp = do | |||
411 | (crypto0,sessionsState0) <- case mbSessionsState of | 385 | (crypto0,sessionsState0) <- case mbSessionsState of |
412 | Nothing -> do | 386 | Nothing -> do |
413 | crypto <- newCrypto | 387 | crypto <- newCrypto |
414 | sessionsState <- newSessionsState crypto (mkDefaultDestroyHook roster) defaultUnRecHook defaultCryptoDataHooks | 388 | sessionsState <- newSessionsState crypto (const $ dput XUnexpected "Missing destroy hook!") defaultUnRecHook defaultCryptoDataHooks |
415 | return (crypto,sessionsState) | 389 | return (crypto,sessionsState) |
416 | Just s -> do | 390 | Just s -> return (transportCrypto s, s) |
417 | let oldhook = defaultDestroyHook s | ||
418 | oldhook' = filter ((==0) . fst) oldhook | ||
419 | newhook = (0,mkDefaultDestroyHook roster):oldhook' | ||
420 | return (transportCrypto s, s { defaultDestroyHook = newhook}) | ||
421 | 391 | ||
422 | let -- patch in supplied DHT key | 392 | let -- patch in supplied DHT key |
423 | crypto1 = fromMaybe crypto0 $do | 393 | crypto1 = fromMaybe crypto0 $do |
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index f0332df0..50224178 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs | |||
@@ -360,6 +360,15 @@ type NewSessionHook = IOHook (Maybe NoSpam) NetCryptoSession | |||
360 | addNewSessionHook :: NetCryptoSessions -> NewSessionHook -> STM () | 360 | addNewSessionHook :: NetCryptoSessions -> NewSessionHook -> STM () |
361 | addNewSessionHook allsessions@(NCSessions { announceNewSessionHooks }) hook = modifyTVar announceNewSessionHooks (hook:) | 361 | addNewSessionHook allsessions@(NCSessions { announceNewSessionHooks }) hook = modifyTVar announceNewSessionHooks (hook:) |
362 | 362 | ||
363 | addDestroySessionHook :: NetCryptoSession -> (Maybe Int) -> (NetCryptoSession -> IO ()) -> STM Int | ||
364 | addDestroySessionHook netcrypto mbkey hook = do | ||
365 | modifyTVar (ncDestoryHooks netcrypto) $ \hooklist -> | ||
366 | case mbkey of | ||
367 | Just key -> ((key,hook):filter ((/=key) . fst) hooklist) | ||
368 | Nothing -> let maxkey = maximum (map fst hooklist) | ||
369 | in if null hooklist then [(0,hook)] else (maxkey+1,hook):hooklist | ||
370 | fst . head <$> readTVar (ncDestoryHooks netcrypto) | ||
371 | |||
363 | forgetCrypto :: TransportCrypto -> NetCryptoSessions -> NetCryptoSession -> STM () | 372 | forgetCrypto :: TransportCrypto -> NetCryptoSessions -> NetCryptoSession -> STM () |
364 | forgetCrypto crypto (NCSessions {netCryptoSessions,netCryptoSessionsByKey}) session = do | 373 | forgetCrypto crypto (NCSessions {netCryptoSessions,netCryptoSessionsByKey}) session = do |
365 | let HaveDHTKey addr = ncSockAddr session | 374 | let HaveDHTKey addr = ncSockAddr session |