diff options
Diffstat (limited to 'src/Network/Tox/Crypto/Handlers.hs')
-rw-r--r-- | src/Network/Tox/Crypto/Handlers.hs | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs index b7414095..6f20e670 100644 --- a/src/Network/Tox/Crypto/Handlers.hs +++ b/src/Network/Tox/Crypto/Handlers.hs | |||
@@ -268,12 +268,13 @@ data NetCryptoSession = NCrypto | |||
268 | -- is started, its ThreadId is stored here | 268 | -- is started, its ThreadId is stored here |
269 | , ncPingMachine :: Maybe PingMachine | 269 | , ncPingMachine :: Maybe PingMachine |
270 | -- ^ when the ping thread is started, store it here | 270 | -- ^ when the ping thread is started, store it here |
271 | , ncOutgoingQueue :: UponHandshake | 271 | , ncOutgoingQueue :: TVar |
272 | (PQ.PacketOutQueue | 272 | (UponHandshake |
273 | (State,Nonce24,RangeMap TArray Word8 TVar) | 273 | (PQ.PacketOutQueue |
274 | CryptoMessage | 274 | (State,Nonce24,RangeMap TArray Word8 TVar) |
275 | (CryptoPacket Encrypted) | 275 | CryptoMessage |
276 | CryptoData) | 276 | (CryptoPacket Encrypted) |
277 | CryptoData)) | ||
277 | -- ^ To send a message add it to this queue, by calling 'tryAppendQueueOutgoing' | 278 | -- ^ To send a message add it to this queue, by calling 'tryAppendQueueOutgoing' |
278 | -- but remember to call 'readyOutGoing' first, because the shared secret cache | 279 | -- but remember to call 'readyOutGoing' first, because the shared secret cache |
279 | -- presently requires the IO monad. | 280 | -- presently requires the IO monad. |
@@ -546,6 +547,7 @@ freshCryptoSession sessions | |||
546 | <- case mbtheirSessionKey of | 547 | <- case mbtheirSessionKey of |
547 | Nothing -> return NeedHandshake | 548 | Nothing -> return NeedHandshake |
548 | Just theirSessionKey -> createNetCryptoOutQueue sessions newsession theirSessionKey pktq ncMyPacketNonce0 ncOutgoingIdMap0 | 549 | Just theirSessionKey -> createNetCryptoOutQueue sessions newsession theirSessionKey pktq ncMyPacketNonce0 ncOutgoingIdMap0 |
550 | mbpktoqVar <- newTVar mbpktoq | ||
549 | lastNQ <- CB.new 10 0 :: STM (CyclicBuffer (Bool,(ViewSnapshot,InOrOut CryptoMessage))) | 551 | lastNQ <- CB.new 10 0 :: STM (CyclicBuffer (Bool,(ViewSnapshot,InOrOut CryptoMessage))) |
550 | listeners <- newTVar IntMap.empty | 552 | listeners <- newTVar IntMap.empty |
551 | msgNum <- newTVar 0 | 553 | msgNum <- newTVar 0 |
@@ -578,7 +580,7 @@ freshCryptoSession sessions | |||
578 | , ncPacketQueue = pktq | 580 | , ncPacketQueue = pktq |
579 | , ncDequeueThread = Nothing -- error "you want the NetCrypto-Dequeue thread id, but is it started?" | 581 | , ncDequeueThread = Nothing -- error "you want the NetCrypto-Dequeue thread id, but is it started?" |
580 | , ncPingMachine = Nothing -- error "you want the NetCrypto-PingMachine, but is it started?" | 582 | , ncPingMachine = Nothing -- error "you want the NetCrypto-PingMachine, but is it started?" |
581 | , ncOutgoingQueue = mbpktoq | 583 | , ncOutgoingQueue = mbpktoqVar |
582 | , ncLastNMsgs = lastNQ | 584 | , ncLastNMsgs = lastNQ |
583 | , ncListeners = listeners | 585 | , ncListeners = listeners |
584 | } | 586 | } |
@@ -726,8 +728,30 @@ updateCryptoSession sessions addr newsession timestamp hp session handshake = do | |||
726 | ++ "hpTheirBaseNonce=" ++ show (hpTheirBaseNonce hp)) | 728 | ++ "hpTheirBaseNonce=" ++ show (hpTheirBaseNonce hp)) |
727 | if ( ncTheirBaseNonce0 /= frmMaybe (hpTheirBaseNonce hp)) | 729 | if ( ncTheirBaseNonce0 /= frmMaybe (hpTheirBaseNonce hp)) |
728 | then do | 730 | then do |
729 | dmsg "basenonce mismatch, trigger refresh" | 731 | case ncTheirBaseNonce0 of |
730 | freshCryptoSession sessions addr newsession timestamp hp -- basenonce mismatch, trigger refresh | 732 | NeedHandshake | Just theirSessionPublic <- hpTheirSessionKeyPublic hp -> do |
733 | writeTVar (ncTheirBaseNonce session) (frmMaybe (hpTheirBaseNonce hp)) | ||
734 | writeTVar (ncTheirBaseNonce session) (frmMaybe (hpTheirBaseNonce hp)) | ||
735 | writeTVar (ncTheirSessionPublic session) (frmMaybe (hpTheirSessionKeyPublic hp)) | ||
736 | writeTVar (ncHandShake session) (HaveHandshake handshake) | ||
737 | writeTVar (ncState session) {-Accepted-}(InProgress AwaitingSessionPacket) | ||
738 | mbpktoq <- createNetCryptoOutQueue | ||
739 | sessions | ||
740 | newsession | ||
741 | theirSessionPublic | ||
742 | (ncPacketQueue session) | ||
743 | (ncMyPacketNonce session) | ||
744 | (ncOutgoingIdMap session) | ||
745 | writeTVar (ncOutgoingQueue session) mbpktoq | ||
746 | return (Nothing,maybe (dput XNetCrypto "ERROR: something went wrong creating the ncOutgoingQueue") | ||
747 | (runUponHandshake session addr) | ||
748 | (toMaybe mbpktoq)) | ||
749 | HaveHandshake _ -> do | ||
750 | dmsg "basenonce mismatch, trigger refresh" | ||
751 | freshCryptoSession sessions addr newsession timestamp hp -- basenonce mismatch, trigger refresh | ||
752 | _ -> do | ||
753 | dmsg "updateCryptoSession -- unexpected condition! have hpTheirSessionKeyPublic but missing hpTheirBaseNonce?" | ||
754 | return (Nothing,return ()) | ||
731 | else do | 755 | else do |
732 | writeTVar (ncState session) {-Accepted-}(InProgress AwaitingSessionPacket) | 756 | writeTVar (ncState session) {-Accepted-}(InProgress AwaitingSessionPacket) |
733 | return (Nothing,return ()) | 757 | return (Nothing,return ()) |
@@ -1000,7 +1024,7 @@ allMsgTypes fDefault = A.listArray (minBound,maxBound) (0:knownMsgs) | |||
1000 | 1024 | ||
1001 | sendCrypto :: TransportCrypto -> NetCryptoSession -> (STM ()) -> CryptoMessage -> IO (Either String ()) | 1025 | sendCrypto :: TransportCrypto -> NetCryptoSession -> (STM ()) -> CryptoMessage -> IO (Either String ()) |
1002 | sendCrypto crypto session updateLocal cm = do | 1026 | sendCrypto crypto session updateLocal cm = do |
1003 | let HaveHandshake outq = ncOutgoingQueue session | 1027 | HaveHandshake outq <- atomically $ readTVar (ncOutgoingQueue session) |
1004 | -- XXX: potential race? if shared secret comes out of sync with cache? | 1028 | -- XXX: potential race? if shared secret comes out of sync with cache? |
1005 | dput XNetCrypto "sendCrypto: enter " | 1029 | dput XNetCrypto "sendCrypto: enter " |
1006 | getOutGoingParam <- PQ.readyOutGoing outq | 1030 | getOutGoingParam <- PQ.readyOutGoing outq |