From 27c23434f4d7a1ae963c34e307681259bd05f798 Mon Sep 17 00:00:00 2001 From: "jim@bo" Date: Thu, 21 Jun 2018 00:38:42 -0400 Subject: netcrypto debug messages, extra online packet, and try thrice code --- src/Network/Tox/Crypto/Handlers.hs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index 35681c4a..ae8ce873 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs @@ -781,6 +781,14 @@ runUponHandshake netCryptoSession0 addr pktoq = do dput XNetCrypto $ "runUponHandshake: Announcing new session" hooks <- atomically $ readTVar (announceNewSessionHooks sessions) sendOnline crypto netCryptoSession + -- in case ONLINE packet is dropped, send anohter after delay + forkIO $ do + tid <- myThreadId + labelThread tid ("Second Online." ++ show (key2id remotePublicKey) ++ sidStr) + threadDelay 1000 + _ <- sendOnline crypto netCryptoSession + return () + -- Run new session hooks flip fix (hooks,netCryptoSession) $ \loop (hooks,session) -> case hooks of [] -> return () @@ -943,10 +951,23 @@ sessionPacketH sessions addrRaw (CryptoPacket nonce16 encrypted) = do Just session@(NCrypto { ncIncomingTypeArray, ncState, ncPacketQueue, ncHooks, ncSessionSecret, ncTheirSessionPublic, ncTheirBaseNonce, ncPingMachine, ncSessionId}) -> do - mbTheirBaseNonce <- atomically $ readTVar ncTheirBaseNonce - case mbTheirBaseNonce of - NeedHandshake -> dput XNetCrypto "CryptoPacket recieved, but we still dont have their base nonce?" >> return Nothing - HaveHandshake theirBaseNonce -> do + -- Unrecognized packets, try them thrice so as to give + -- handshakes some time to come in + -- TODO: Remove this loop, as it is probably unnecessary. + -- If it is necessary, use a queue instead. + flip fix (0::Int) $ \loop i -> do + mbTheirBaseNonce <- atomically $ readTVar ncTheirBaseNonce + case mbTheirBaseNonce of + NeedHandshake -> do + dput XNetCrypto "CryptoPacket recieved, but we still dont have their base nonce?" + if (i < 3) + then do + dput XNetCrypto $ "Trying again (maybe handshake is on its way) ... i == " ++ show i + loop (i+1) + else do + dput XNetCrypto "Tried 3 times.. giving up on this packet" + return Nothing + HaveHandshake theirBaseNonce -> do -- Try to decrypt message let diff :: Word16 diff = nonce16 - (last2Bytes theirBaseNonce) -- truncating to Word16 @@ -1327,12 +1348,15 @@ defaultUnRecHook typ session cm = do hookHelper False typ session cm hookHelper :: Bool -> MessageType -> NetCryptoHook -hookHelper _ typ session cm | any ($ typ) [isKillPacket, isOFFLINE] = atomically $ do +hookHelper _ typ session cm | any ($ typ) [isKillPacket, isOFFLINE] = do + dput XNetCrypto $ "(hookHelper kill/offline) cm=" ++ show cm + atomically $ do tmchans <- map snd . IntMap.elems <$> readTVar (ncListeners session) forM_ tmchans $ \chan -> closeTMChan chan return Nothing hookHelper handledFlg typ session cm = do + dput XNetCrypto $ "(ENTER hookHelper) " ++ show cm addMsgToLastN handledFlg typ session (In cm) atomically $ do idtmchans <- IntMap.assocs <$> readTVar (ncListeners session) -- cgit v1.2.3