summaryrefslogtreecommitdiff
path: root/OnionRouter.hs
diff options
context:
space:
mode:
Diffstat (limited to 'OnionRouter.hs')
-rw-r--r--OnionRouter.hs23
1 files changed, 19 insertions, 4 deletions
diff --git a/OnionRouter.hs b/OnionRouter.hs
index 20279c5d..428b6d8e 100644
--- a/OnionRouter.hs
+++ b/OnionRouter.hs
@@ -210,6 +210,11 @@ randomIvalInteger (l,h) rng
210 (x,g') = next g -- next :: RandomGen g => g -> (Int, g) 210 (x,g') = next g -- next :: RandomGen g => g -> (Int, g)
211 v' = (v * b + (fromIntegral x - fromIntegral genlo)) 211 v' = (v * b + (fromIntegral x - fromIntegral genlo))
212 212
213-- Repeatedly attempt to select 3 nodes as a secure onion route letting 1 second
214-- elapse between retries.
215--
216-- Only the DRG random seed is updated. Hopefully another thread will change the
217-- trampolineNodes set so that selection can succeed.
213selectTrampolines :: OnionRouter -> IO [NodeInfo] 218selectTrampolines :: OnionRouter -> IO [NodeInfo]
214selectTrampolines or = do 219selectTrampolines or = do
215 myThreadId >>= flip labelThread ("OnionRouter.selectTrampolines") 220 myThreadId >>= flip labelThread ("OnionRouter.selectTrampolines")
@@ -226,6 +231,12 @@ selectTrampolines or = do
226 myThreadId >>= flip labelThread ("OnionRouter") 231 myThreadId >>= flip labelThread ("OnionRouter")
227 return ns 232 return ns
228 233
234-- Select 3 indices into the trampolineNodes set and returns the associated
235-- nodes provided they are suitable for use in an onion route. Otherwise, it
236-- returns Left with the nodes that were selected.
237--
238-- The only write this function does to STM state is that the onionDRG random
239-- seed will be updated.
229selectTrampolines' :: OnionRouter -> STM (Either [NodeInfo] [NodeInfo]) 240selectTrampolines' :: OnionRouter -> STM (Either [NodeInfo] [NodeInfo])
230selectTrampolines' or = do 241selectTrampolines' or = do
231 cnt <- readTVar (trampolineCount or) 242 cnt <- readTVar (trampolineCount or)
@@ -425,10 +436,14 @@ hookBucketList _ _ or (RoutingTransition ni Stranger) = do
425 Just n -> do writeTVar (trampolineIds or) (HashMap.delete (nodeId ni) ns) 436 Just n -> do writeTVar (trampolineIds or) (HashMap.delete (nodeId ni) ns)
426 cnt <- pred <$> readTVar (trampolineCount or) 437 cnt <- pred <$> readTVar (trampolineCount or)
427 writeTVar (trampolineCount or) cnt 438 writeTVar (trampolineCount or) cnt
428 if n == cnt 439 case compare n cnt of
429 then modifyTVar' (trampolineNodes or) (IntMap.delete n) 440 EQ -> modifyTVar' (trampolineNodes or) (IntMap.delete n)
430 else do lastnode <- (IntMap.! cnt) <$> readTVar (trampolineNodes or) 441 LT -> do lastnode <- (IntMap.! cnt) <$> readTVar (trampolineNodes or)
431 modifyTVar' (trampolineNodes or) (IntMap.insert n lastnode . IntMap.delete cnt) 442 modifyTVar' (trampolineNodes or)
443 (IntMap.insert n lastnode . IntMap.delete cnt)
444 modifyTVar' (trampolineIds or)
445 (HashMap.delete (nodeId ni) . HashMap.insert (nodeId lastnode) n)
446 GT -> writeTChan (routeLog or) $ "BUG!! Trampoline maps are out of sync."
432 writeTChan (routeLog or) $ "ONION trampoline Stranger " ++ unwords [show n,show ni] 447 writeTChan (routeLog or) $ "ONION trampoline Stranger " ++ unwords [show n,show ni]
433 Nothing -> return () 448 Nothing -> return ()
434hookBucketList _ _ _ _ = return () -- ignore Applicant event. 449hookBucketList _ _ _ _ = return () -- ignore Applicant event.