diff options
-rw-r--r-- | OnionRouter.hs | 23 | ||||
-rw-r--r-- | examples/dhtd.hs | 4 |
2 files changed, 22 insertions, 5 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. |
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 369650f9..fce976db 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -808,6 +808,8 @@ clientSession s@Session{..} sock cnum h = do | |||
808 | ("onion", s) -> cmd0 $ join $ atomically $ do | 808 | ("onion", s) -> cmd0 $ join $ atomically $ do |
809 | rm <- readTVar $ routeMap onionRouter | 809 | rm <- readTVar $ routeMap onionRouter |
810 | ts <- readTVar $ trampolineNodes onionRouter | 810 | ts <- readTVar $ trampolineNodes onionRouter |
811 | tcnt <- readTVar $ trampolineCount onionRouter | ||
812 | icnt <- HashMap.size <$> readTVar (trampolineIds onionRouter) | ||
811 | rs <- mapM readTVar (pendingRoutes onionRouter) | 813 | rs <- mapM readTVar (pendingRoutes onionRouter) |
812 | let showRecord :: Int -> Bool -> [String] | 814 | let showRecord :: Int -> Bool -> [String] |
813 | showRecord n True = [show n, "pending", ""] | 815 | showRecord n True = [show n, "pending", ""] |
@@ -817,7 +819,7 @@ clientSession s@Session{..} sock cnum h = do | |||
817 | | otherwise = [show n, "error!",""] | 819 | | otherwise = [show n, "error!",""] |
818 | r = map (uncurry showRecord) $ IntMap.toAscList rs | 820 | r = map (uncurry showRecord) $ IntMap.toAscList rs |
819 | return $ do | 821 | return $ do |
820 | hPutClientChunk h $ "trampolines: " ++ show (IntMap.size ts) ++ "\n" | 822 | hPutClientChunk h $ "trampolines: " ++ show (IntMap.size ts,tcnt) ++ "\n" |
821 | hPutClient h $ showColumns $ ["","responses","timeouts"]:r | 823 | hPutClient h $ showColumns $ ["","responses","timeouts"]:r |
822 | 824 | ||
823 | -- necrypto <FRIEND-TOXID> | 825 | -- necrypto <FRIEND-TOXID> |