summaryrefslogtreecommitdiff
path: root/OnionRouter.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-12-16 13:56:12 -0500
committerJoe Crayne <joe@jerkface.net>2018-12-16 14:08:26 -0500
commit006093d2ec381739d0fffb5e3c4534daaea774d2 (patch)
tree1e23d3efbf7504841d1c82965a9506241a2ac688 /OnionRouter.hs
parent4f0c515a83c2ca54e5951fed2b7593273821ef2f (diff)
Bug fix: distinct index selection for trampolines.
Diffstat (limited to 'OnionRouter.hs')
-rw-r--r--OnionRouter.hs26
1 files changed, 17 insertions, 9 deletions
diff --git a/OnionRouter.hs b/OnionRouter.hs
index f1b0b41c..ad6323fe 100644
--- a/OnionRouter.hs
+++ b/OnionRouter.hs
@@ -17,6 +17,8 @@ import Control.Monad
17import Crypto.Random 17import Crypto.Random
18import Data.Array.MArray 18import Data.Array.MArray
19import Data.Bits 19import Data.Bits
20import Data.Bool
21import Data.List
20import qualified Data.ByteString as B 22import qualified Data.ByteString as B
21import qualified Data.HashMap.Strict as HashMap 23import qualified Data.HashMap.Strict as HashMap
22 ;import Data.HashMap.Strict (HashMap) 24 ;import Data.HashMap.Strict (HashMap)
@@ -251,6 +253,20 @@ selectTrampolines or = do
251 myThreadId >>= flip labelThread ("OnionRouter") 253 myThreadId >>= flip labelThread ("OnionRouter")
252 return ns 254 return ns
253 255
256choose3 :: (Integral a, DRG drg) => drg -> a -> ([a], drg)
257choose3 drg0 cnt = ([a,b,c], drg)
258 where
259 (a, drg1) = randomR (0,cnt - 1) drg0
260 (b0, drg2) = randomR (0,cnt - 2) drg1
261 (c0, drg ) = randomR (0,cnt - 3) drg2
262 b | b0 < a = b0
263 | otherwise = b0 + 1
264 [ac,bc] = sort [a,b]
265 c1 | c0 < ac = c0
266 | otherwise = c0 + 1
267 c | c1 < bc = c1
268 | otherwise = c1 + 1
269
254-- Select 3 indices into the trampolineNodes set and returns the associated 270-- Select 3 indices into the trampolineNodes set and returns the associated
255-- nodes provided they are suitable for use in an onion route. Otherwise, it 271-- nodes provided they are suitable for use in an onion route. Otherwise, it
256-- returns Left with the nodes that were selected. 272-- returns Left with the nodes that were selected.
@@ -262,15 +278,7 @@ selectTrampolines' or = do
262 cnt <- readTVar (trampolineCount or) 278 cnt <- readTVar (trampolineCount or)
263 ts <- readTVar (trampolineNodes or) 279 ts <- readTVar (trampolineNodes or)
264 drg0 <- readTVar (onionDRG or) 280 drg0 <- readTVar (onionDRG or)
265 let (a, drg1) = randomR (0,cnt - 1) drg0 281 let ([a,b,c],drg) = choose3 drg0 cnt
266 (b0, drg2) = randomR (0,cnt - 2) drg1
267 (c0, drg ) = randomR (0,cnt - 3) drg2
268 b | b0 < a = b0
269 | otherwise = b0 + 1
270 c1 | c0 < a = c0
271 | otherwise = c0 + 1
272 c | c1 < b = c1
273 | otherwise = c1 + 1
274 ns = mapMaybe (\n -> IntMap.lookup n ts) [a,b,c] 282 ns = mapMaybe (\n -> IntMap.lookup n ts) [a,b,c]
275 ns' <- case ns of 283 ns' <- case ns of
276 [an,bn,cn] | distinct3by nodeClass an bn cn 284 [an,bn,cn] | distinct3by nodeClass an bn cn