summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjim@bo <jim@bo>2018-06-21 00:38:42 -0400
committerjim@bo <jim@bo>2018-06-21 00:49:31 -0400
commit27c23434f4d7a1ae963c34e307681259bd05f798 (patch)
tree718f156b5887859d5c4c775b0f6d0fa6a6b9e862
parent2ead6b37f07b9d6c0f0ae8402ccabd1f98794c31 (diff)
netcrypto debug messages, extra online packet, and try thrice code
-rw-r--r--src/Network/Tox/Crypto/Handlers.hs34
1 files 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
781 dput XNetCrypto $ "runUponHandshake: Announcing new session" 781 dput XNetCrypto $ "runUponHandshake: Announcing new session"
782 hooks <- atomically $ readTVar (announceNewSessionHooks sessions) 782 hooks <- atomically $ readTVar (announceNewSessionHooks sessions)
783 sendOnline crypto netCryptoSession 783 sendOnline crypto netCryptoSession
784 -- in case ONLINE packet is dropped, send anohter after delay
785 forkIO $ do
786 tid <- myThreadId
787 labelThread tid ("Second Online." ++ show (key2id remotePublicKey) ++ sidStr)
788 threadDelay 1000
789 _ <- sendOnline crypto netCryptoSession
790 return ()
791 -- Run new session hooks
784 flip fix (hooks,netCryptoSession) $ \loop (hooks,session) -> 792 flip fix (hooks,netCryptoSession) $ \loop (hooks,session) ->
785 case hooks of 793 case hooks of
786 [] -> return () 794 [] -> return ()
@@ -943,10 +951,23 @@ sessionPacketH sessions addrRaw (CryptoPacket nonce16 encrypted) = do
943 Just session@(NCrypto { ncIncomingTypeArray, ncState, ncPacketQueue, ncHooks, 951 Just session@(NCrypto { ncIncomingTypeArray, ncState, ncPacketQueue, ncHooks,
944 ncSessionSecret, ncTheirSessionPublic, ncTheirBaseNonce, 952 ncSessionSecret, ncTheirSessionPublic, ncTheirBaseNonce,
945 ncPingMachine, ncSessionId}) -> do 953 ncPingMachine, ncSessionId}) -> do
946 mbTheirBaseNonce <- atomically $ readTVar ncTheirBaseNonce 954 -- Unrecognized packets, try them thrice so as to give
947 case mbTheirBaseNonce of 955 -- handshakes some time to come in
948 NeedHandshake -> dput XNetCrypto "CryptoPacket recieved, but we still dont have their base nonce?" >> return Nothing 956 -- TODO: Remove this loop, as it is probably unnecessary.
949 HaveHandshake theirBaseNonce -> do 957 -- If it is necessary, use a queue instead.
958 flip fix (0::Int) $ \loop i -> do
959 mbTheirBaseNonce <- atomically $ readTVar ncTheirBaseNonce
960 case mbTheirBaseNonce of
961 NeedHandshake -> do
962 dput XNetCrypto "CryptoPacket recieved, but we still dont have their base nonce?"
963 if (i < 3)
964 then do
965 dput XNetCrypto $ "Trying again (maybe handshake is on its way) ... i == " ++ show i
966 loop (i+1)
967 else do
968 dput XNetCrypto "Tried 3 times.. giving up on this packet"
969 return Nothing
970 HaveHandshake theirBaseNonce -> do
950 -- Try to decrypt message 971 -- Try to decrypt message
951 let diff :: Word16 972 let diff :: Word16
952 diff = nonce16 - (last2Bytes theirBaseNonce) -- truncating to Word16 973 diff = nonce16 - (last2Bytes theirBaseNonce) -- truncating to Word16
@@ -1327,12 +1348,15 @@ defaultUnRecHook typ session cm = do
1327 hookHelper False typ session cm 1348 hookHelper False typ session cm
1328 1349
1329hookHelper :: Bool -> MessageType -> NetCryptoHook 1350hookHelper :: Bool -> MessageType -> NetCryptoHook
1330hookHelper _ typ session cm | any ($ typ) [isKillPacket, isOFFLINE] = atomically $ do 1351hookHelper _ typ session cm | any ($ typ) [isKillPacket, isOFFLINE] = do
1352 dput XNetCrypto $ "(hookHelper kill/offline) cm=" ++ show cm
1353 atomically $ do
1331 tmchans <- map snd . IntMap.elems <$> readTVar (ncListeners session) 1354 tmchans <- map snd . IntMap.elems <$> readTVar (ncListeners session)
1332 forM_ tmchans $ \chan -> closeTMChan chan 1355 forM_ tmchans $ \chan -> closeTMChan chan
1333 return Nothing 1356 return Nothing
1334 1357
1335hookHelper handledFlg typ session cm = do 1358hookHelper handledFlg typ session cm = do
1359 dput XNetCrypto $ "(ENTER hookHelper) " ++ show cm
1336 addMsgToLastN handledFlg typ session (In cm) 1360 addMsgToLastN handledFlg typ session (In cm)
1337 atomically $ do 1361 atomically $ do
1338 idtmchans <- IntMap.assocs <$> readTVar (ncListeners session) 1362 idtmchans <- IntMap.assocs <$> readTVar (ncListeners session)