From 9045835b429e88e8cbcca2b41c126f664b53d471 Mon Sep 17 00:00:00 2001 From: James Crayne Date: Thu, 31 May 2018 14:24:43 +0000 Subject: debug prints and clean up stale sessions --- src/Network/Tox/Crypto/Handlers.hs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/Network/Tox') diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index b47aeac1..eabbc9b0 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs @@ -619,15 +619,27 @@ createNetCryptoOutQueue sessions newsession theirSessionKey pktq ncMyPacketNonce -- | add this session to the lookup maps, unless its already in them addSessionToMapIfNotThere :: NetCryptoSessions -> SockAddr -> NetCryptoSession -> IO () addSessionToMapIfNotThere sessions addr netCryptoSession = do + dput XNetCrypto $ "addSessionToMapIfNotThere sockaddr = " ++ show addr ++ ", sessionid = " ++ show (ncSessionId netCryptoSession) atomically $ do let remotePublicKey = ncTheirPublicKey netCryptoSession allsessions = netCryptoSessions sessions allsessionsByKey= netCryptoSessionsByKey sessions + byAddrResult <- readTVar allsessions >>= return . Map.lookup addr + case byAddrResult of + Just (NCrypto { ncSessionId = staleId }) -> do + -- manually remove the stale session from the by-key map + modifyTVar allsessionsByKey (Map.map (filter ((/=staleId) . ncSessionId))) + Nothing -> return () -- nothing to remove + -- write session to by-addr map regardless of whether one is in there, + -- it should overwrite on match modifyTVar allsessions (Map.insert addr netCryptoSession) + -- Now insert new session into by-key map byKeyResult <- readTVar allsessionsByKey >>= return . Map.lookup remotePublicKey case byKeyResult of Nothing -> modifyTVar allsessionsByKey (Map.insert remotePublicKey [netCryptoSession]) - Just xs -> modifyTVar allsessionsByKey (Map.insert remotePublicKey (netCryptoSession:xs)) + Just xs -> do + -- in case we're using the same long term key on different IPs ... + modifyTVar allsessionsByKey (Map.insert remotePublicKey (netCryptoSession:xs)) runUponHandshake :: NetCryptoSession -> SockAddr -> NetCryptoOutQueue -> IO () runUponHandshake netCryptoSession0 addr pktoq = do -- cgit v1.2.3