From add36aaffd9ef06ca9c6f2a73a185ba531581c25 Mon Sep 17 00:00:00 2001 From: joe Date: Mon, 13 Nov 2017 05:19:50 -0500 Subject: Added PacketQueue for handling inbound netcrypto packets. --- src/Network/Tox/Crypto/Handlers.hs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/Network/Tox/Crypto') diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index 13db02f3..787c69c2 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs @@ -18,6 +18,8 @@ import qualified Data.ByteString as B import Data.ByteString (ByteString) import Control.Lens import Data.Function +import qualified Data.PacketQueue as PQ + ;import Data.PacketQueue (PacketQueue) import Data.Serialize as S import Data.Word import GHC.Conc (unsafeIOToSTM) @@ -63,7 +65,7 @@ data NetCryptoSession = NCrypto { ncState :: TVar NetCryptoSessionStatus , ncTheirBaseNonce :: TVar Nonce24 -- base nonce + packet number , ncMyPacketNonce :: TVar Nonce24 -- base nonce + packet number , ncHandShake :: TVar (Maybe (Handshake Encrypted)) - , ncCookie :: TVar (Maybe Cookie) + , ncCookie :: TVar (Maybe Cookie) -- ^ Cookie issued by remote peer , ncTheirDHTKey :: PublicKey , ncTheirSessionPublic :: Maybe PublicKey , ncSessionSecret :: SecretKey @@ -80,6 +82,8 @@ data NetCryptoSession = NCrypto { ncState :: TVar NetCryptoSessionStatus -- needs to possibly start another, as is -- the case in group chats , ncView :: TVar SessionView + , ncPacketQueue :: PacketQueue CryptoMessage + , ncBufferStart :: TVar Word32 } data NetCryptoSessions = NCSessions { netCryptoSessions :: TVar (Map.Map SockAddr NetCryptoSession) @@ -88,6 +92,7 @@ data NetCryptoSessions = NCSessions { netCryptoSessions :: TVar (Map.Map SockAdd , defaultUnrecognizedHook :: MessageType -> NetCryptoHook , sessionView :: SessionView , msgTypeArray :: MsgTypeArray + , inboundQueueCapacity :: Word32 } newSessionsState :: TransportCrypto @@ -120,6 +125,7 @@ newSessionsState crypto unrechook hooks = do , svDownloadDir = svDownloadDir0 } , msgTypeArray = allMsgTypes -- todo make this a parameter + , inboundQueueCapacity = 200 } data HandshakeParams @@ -195,6 +201,8 @@ freshCryptoSession sessions ncUnrecognizedHook0 <- atomically $ newTVar (defaultUnrecognizedHook sessions) ncMessageTypes0 <- atomically $ newTVar (msgTypeArray sessions) ncView0 <- atomically $ newTVar (sessionView sessions) + pktq <- atomically $ PQ.new (inboundQueueCapacity sessions) 0 + bufstart <- atomically $ newTVar 0 let netCryptoSession = NCrypto { ncState = ncState0 , ncTheirBaseNonce= ncTheirBaseNonce0 @@ -210,6 +218,8 @@ freshCryptoSession sessions , ncAllSessions = sessions , ncMessageTypes = ncMessageTypes0 , ncView = ncView0 + , ncPacketQueue = pktq + , ncBufferStart = bufstart } atomically $ modifyTVar allsessions (Map.insert addr netCryptoSession) @@ -224,7 +234,7 @@ updateCryptoSession sessions addr hp session = do -- duplicate handshake packet, otherwise disregard everything, and -- refresh all state. -- - then when ( Just ncTheirBaseNonce0 /= hpTheirBaseNonce hp + then when ( Just ncTheirBaseNonce0 /= hpTheirBaseNonce hp -- XXX: Do we really want to compare base nonce here? || ncTheirDHTKey session /= hpCookieRemoteDhtkey hp ) $ freshCryptoSession sessions addr hp else if ( Just ncTheirBaseNonce0 /= hpTheirBaseNonce hp) -- cgit v1.2.3