summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjim@bo <jim@bo>2018-06-22 15:06:51 -0400
committerjim@bo <jim@bo>2018-06-22 15:06:51 -0400
commitd96aa110fcc32d9a8afb14564f45f296dd1624e4 (patch)
tree8043a7ae3623501c012198fb4dd2f1eaf9c79b61 /src
parent2f0e7ebc7bc3abf5cb03ccff2de77d3557ba2702 (diff)
packet request off by 1 serializatoin fix & getMissing fix
Diffstat (limited to 'src')
-rw-r--r--src/Data/PacketQueue.hs59
-rw-r--r--src/Network/Tox/Crypto/Transport.hs5
2 files changed, 34 insertions, 30 deletions
diff --git a/src/Data/PacketQueue.hs b/src/Data/PacketQueue.hs
index b5d8a756..59b41d91 100644
--- a/src/Data/PacketQueue.hs
+++ b/src/Data/PacketQueue.hs
@@ -19,6 +19,7 @@ module Data.PacketQueue
19 , packetQueueViewList 19 , packetQueueViewList
20 , newOutGoing 20 , newOutGoing
21 , readyOutGoing 21 , readyOutGoing
22 , toPNums
22 , getRequested 23 , getRequested
23 , peekPacket 24 , peekPacket
24 , tryAppendQueueOutgoing 25 , tryAppendQueueOutgoing
@@ -86,13 +87,18 @@ observeOutOfBand PacketQueue { seqno, qsize, buffend } numberOfNextLosslessPacke
86-- | If seqno < buffend then return expected packet numbers for all 87-- | If seqno < buffend then return expected packet numbers for all
87-- the Nothings in the array between them. 88-- the Nothings in the array between them.
88-- Otherwise, return empty list. 89-- Otherwise, return empty list.
89getMissing :: PacketQueue a -> STM [Word32] 90getMissing :: Show a => PacketQueue a -> STM [Word32]
90getMissing PacketQueue { pktq, seqno, qsize, buffend } = do 91getMissing PacketQueue { pktq, seqno, qsize, buffend } = do
91 seqno0 <- readTVar seqno 92 seqno0 <- readTVar seqno
92 buffend0 <- readTVar buffend 93 buffend0 <- readTVar buffend
94 -- tput XNetCrypto $ "getMissing: seqno = " ++ show seqno0
95 -- tput XNetCrypto $ "getMissing: buffend0 = " ++ show buffend0
93 -- note relying on fact that [ b .. a ] is null when a < b 96 -- note relying on fact that [ b .. a ] is null when a < b
94 maybes <- mapM (readArray pktq) (take (fromIntegral qsize) $ map (`mod` qsize) [ seqno0 .. buffend0 ]) 97 let indices = take (fromIntegral qsize) $ map (`mod` qsize) [ seqno0 .. buffend0 - 1]
95 let nums = map fst . filter (isNothing . snd) $ zip [buffend0 ..] maybes 98 -- tput XNetCrypto $ "indices: " ++ show indices
99 maybes <- mapM (\i -> do {x <- readArray pktq i; return (i,x)}) indices
100 -- tput XNetCrypto $ "getMissing: maybers = " ++ show maybes
101 let nums = map fst . filter (isNothing . snd) $ maybes
96 return nums 102 return nums
97 103
98-- | If seqno < buffend then return expected packet numbers for all 104-- | If seqno < buffend then return expected packet numbers for all
@@ -209,23 +215,28 @@ data OutGoingResult = OGSuccess | OGFull | OGEncodeFail
209readyOutGoing :: PacketOutQueue extra msg wire fromwire -> IO (STM extra) 215readyOutGoing :: PacketOutQueue extra msg wire fromwire -> IO (STM extra)
210readyOutGoing (PacketOutQueue {pktoToWireIO }) = pktoToWireIO 216readyOutGoing (PacketOutQueue {pktoToWireIO }) = pktoToWireIO
211 217
212getRequested :: STM extra -> PacketOutQueue extra msg wire fromwire -> Word32 -> [Word8] -> STM [Maybe (Word32,wire)] 218getRequested :: Show wire => STM extra -> PacketOutQueue extra msg wire fromwire -> Word32 -> [Word8] -> STM [Maybe (Word32,wire)]
213getRequested _ _ _ [] = return [] 219getRequested _ _ _ [] = return []
214getRequested getExtra pktoq snum ns = do 220getRequested getExtra pktoq snum ns = do
215 let pnums = toPNums snum ns 221 let pnums = toPNums snum ns
216 indices = map toIndex pnums 222 indices = map toIndex pnums
223 tput XNetCrypto $ "getRequested: snum = " ++ show snum
224 tput XNetCrypto $ "getRequested: pnums = " ++ show pnums ++ " indices = " ++ show indices
225 xs <- packetQueueViewList (pktoOutPQ pktoq)
226 tput XNetCrypto $ "getRequested viewList -> "
227 mapM_ (tput XNetCrypto . show) xs
217 forM indices $ \i -> readArray (pktq $ pktoOutPQ pktoq) i 228 forM indices $ \i -> readArray (pktq $ pktoOutPQ pktoq) i
218 where 229 where
219 toIndex :: Word32 -> Word32 230 toIndex :: Word32 -> Word32
220 toIndex = (`mod` qsize (pktoOutPQ pktoq)) 231 toIndex = (`mod` qsize (pktoOutPQ pktoq))
221 232
222 toPNums :: Word32 -> [Word8] -> [Word32] 233toPNums :: Word32 -> [Word8] -> [Word32]
223 toPNums snum ns = reverse . snd $ foldl doOne (snum,[]) ns 234toPNums snum ns = reverse . snd $ foldl doOne (snum-1,[]) ns
224 where 235 where
225 doOne :: (Word32,[Word32]) -> Word8 -> (Word32,[Word32]) 236 doOne :: (Word32,[Word32]) -> Word8 -> (Word32,[Word32])
226 doOne (addend,as) 0 = (addend+255,as) 237 doOne (addend,as) 0 = (addend+255,as)
227 doOne (addend,as) x = let y = fromIntegral x + addend 238 doOne (addend,as) x = let y = fromIntegral x + addend
228 in (y,y:as) 239 in (y,y:as)
229 240
230peekPacket :: STM extra -> PacketOutQueue extra msg wire fromwire -> msg -> STM (Maybe (wire,Word32)) 241peekPacket :: STM extra -> PacketOutQueue extra msg wire fromwire -> msg -> STM (Maybe (wire,Word32))
231peekPacket getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoToWireIO, pktoToWire }) msg 242peekPacket getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoToWireIO, pktoToWire }) msg
@@ -256,40 +267,30 @@ peekPacket getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoT
256-- index in this implementation.) 267-- index in this implementation.)
257tryAppendQueueOutgoing :: STM extra -> PacketOutQueue extra msg wire fromwire -> msg -> STM OutGoingResult 268tryAppendQueueOutgoing :: STM extra -> PacketOutQueue extra msg wire fromwire -> msg -> STM OutGoingResult
258tryAppendQueueOutgoing getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoToWireIO, pktoToWire }) msg 269tryAppendQueueOutgoing getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoToWireIO, pktoToWire }) msg
259 = dtrace XNetCrypto "(tryAppendQueueOutgoing)" $ do 270 = do
260 be <- readTVar (buffend pktoOutPQ) 271 be <- readTVar (buffend pktoOutPQ)
261 let i = be `mod` (qsize pktoOutPQ) 272 let i = be `mod` (qsize pktoOutPQ)
262 let arrayEmpty :: MArray a e m => a Word32 e -> m Bool 273 let arrayEmpty :: MArray a e m => a Word32 e -> m Bool
263 arrayEmpty ar = do (lowB,highB) <- getBounds ar 274 arrayEmpty ar = do (lowB,highB) <- getBounds ar
264 let result= lowB > highB 275 return $ lowB > highB
265 tput XNetCrypto
266 ("arrayEmpty result=" ++ show result
267 ++ " lowB=" ++ show lowB
268 ++ " highB = " ++ show highB
269 ++ " i = " ++ show i)
270 return result
271 mbPkt <- do emp <- arrayEmpty (pktq pktoOutPQ) 276 mbPkt <- do emp <- arrayEmpty (pktq pktoOutPQ)
272 if emp then tput XNetCrypto "(tryAppendQueueOutgoing empty)" >> return Nothing 277 if emp then return Nothing
273 else do tput XNetCrypto "(tryAppendQueueOutgoing nonempty)" 278 else do readArray (pktq pktoOutPQ) i
274 result <- readArray (pktq pktoOutPQ) i
275 tput XNetCrypto ("readArray (isJust result)==" ++ show (isJust result))
276 return result
277 pktno <- readTVar pktoPacketNo 279 pktno <- readTVar pktoPacketNo
278 nextno <- readTVar (seqno pktoInPQ) 280 nextno <- readTVar (seqno pktoInPQ)
279 mbWire <- pktoToWire getExtra nextno be pktno msg 281 mbWire <- pktoToWire getExtra nextno be pktno msg
280 -- TODO all the above lines ^^ can be replaced with call to peekPacket 282 -- TODO all the above lines ^^ can be replaced with call to peekPacket
281 case dtrace XNetCrypto "(tryAppendQueueOutgoing mbWire)" mbWire of 283 case mbWire of
282 Just (pkt,pktno') 284 Just (pkt,pktno')
283 -> dtrace XNetCrypto "(tryAppendQueueOutgoing A)" 285 -> case mbPkt of
284 $ case mbPkt of
285 -- slot is free, insert element 286 -- slot is free, insert element
286 Nothing -> dtrace XNetCrypto "(tryAppendQueueOutgoing Nothing case)" $ do 287 Nothing -> do
287 modifyTVar' (buffend pktoOutPQ) (+1) 288 modifyTVar' (buffend pktoOutPQ) (+1)
288 writeTVar pktoPacketNo $! pktno' 289 writeTVar pktoPacketNo $! pktno'
289 writeArray (pktq pktoOutPQ) i (Just (pktno,pkt)) 290 writeArray (pktq pktoOutPQ) i (Just (pktno,pkt))
290 return OGSuccess 291 return OGSuccess
291 -- queue is full 292 -- queue is full
292 Just (n,_) -> dtrace XNetCrypto "tryAppendQueueOutgoing Just case)" $ do 293 Just (n,_) -> do
293 nn <- getHighestHandledPacketPlus1 q 294 nn <- getHighestHandledPacketPlus1 q
294 if (n < nn) 295 if (n < nn)
295 -- but we can overwrite an old packet 296 -- but we can overwrite an old packet
diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs
index 678bb16d..ea8565b2 100644
--- a/src/Network/Tox/Crypto/Transport.hs
+++ b/src/Network/Tox/Crypto/Transport.hs
@@ -4,6 +4,7 @@
4{-# LANGUAGE FlexibleInstances #-} 4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE LambdaCase #-} 5{-# LANGUAGE LambdaCase #-}
6{-# LANGUAGE TupleSections #-} 6{-# LANGUAGE TupleSections #-}
7{-# LANGUAGE StandaloneDeriving #-}
7module Network.Tox.Crypto.Transport 8module Network.Tox.Crypto.Transport
8 ( parseCrypto 9 ( parseCrypto
9 , encodeCrypto 10 , encodeCrypto
@@ -97,7 +98,7 @@ encodeHandshakes x saddr = (B.cons 0x1a (runPut $ put x),saddr)
97createRequestPacket :: Word32 -> [Word32] -> CryptoMessage 98createRequestPacket :: Word32 -> [Word32] -> CryptoMessage
98createRequestPacket seqno xs = UpToN PacketRequest (B.pack ns) 99createRequestPacket seqno xs = UpToN PacketRequest (B.pack ns)
99 where 100 where
100 ys = Prelude.map (subtract seqno) xs 101 ys = Prelude.map (subtract (seqno - 1)) xs
101 reduceToSums [] = [] 102 reduceToSums [] = []
102 reduceToSums (x:xs) = x:(reduceToSums $ Prelude.map (subtract x) xs) 103 reduceToSums (x:xs) = x:(reduceToSums $ Prelude.map (subtract x) xs)
103 makeZeroes :: Word32 -> [Word32] 104 makeZeroes :: Word32 -> [Word32]
@@ -168,6 +169,8 @@ data CryptoPacket (f :: * -> *) = CryptoPacket
168 , pktData :: f CryptoData 169 , pktData :: f CryptoData
169 } 170 }
170 171
172deriving instance Show (CryptoPacket Encrypted)
173
171instance Sized CryptoData where 174instance Sized CryptoData where
172 size = contramap bufferStart size 175 size = contramap bufferStart size
173 <> contramap bufferEnd size 176 <> contramap bufferEnd size