diff options
author | joe <joe@jerkface.net> | 2018-06-09 19:59:27 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-09 19:59:27 -0400 |
commit | 82f28a1161666e27adcbb0e6c383f5ac5a836495 (patch) | |
tree | 6bae86955d6dd74adb149aa10805ee4d5bac7856 /OnionRouter.hs | |
parent | e4e4650d004cba42bfd8897d9658bfcaec82fb6d (diff) |
tox: Keep tramplineIds map in sync when trampoline nodes are abandoned.
Diffstat (limited to 'OnionRouter.hs')
-rw-r--r-- | OnionRouter.hs | 23 |
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. | ||
213 | selectTrampolines :: OnionRouter -> IO [NodeInfo] | 218 | selectTrampolines :: OnionRouter -> IO [NodeInfo] |
214 | selectTrampolines or = do | 219 | selectTrampolines 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. | ||
229 | selectTrampolines' :: OnionRouter -> STM (Either [NodeInfo] [NodeInfo]) | 240 | selectTrampolines' :: OnionRouter -> STM (Either [NodeInfo] [NodeInfo]) |
230 | selectTrampolines' or = do | 241 | selectTrampolines' 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 () |
434 | hookBucketList _ _ _ _ = return () -- ignore Applicant event. | 449 | hookBucketList _ _ _ _ = return () -- ignore Applicant event. |