summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--OnionRouter.hs26
-rw-r--r--examples/dhtd.hs17
2 files changed, 25 insertions, 18 deletions
diff --git a/OnionRouter.hs b/OnionRouter.hs
index 1d43d05f..f8271239 100644
--- a/OnionRouter.hs
+++ b/OnionRouter.hs
@@ -25,6 +25,7 @@ import qualified Data.IntMap as IntMap
25 ;import Data.IntMap (IntMap) 25 ;import Data.IntMap (IntMap)
26import Data.Maybe 26import Data.Maybe
27import qualified Data.Serialize as S 27import qualified Data.Serialize as S
28import Data.Time.Clock.POSIX
28import Data.Typeable 29import Data.Typeable
29import Data.Word 30import Data.Word
30import qualified Data.Word64Map as W64 31import qualified Data.Word64Map as W64
@@ -87,10 +88,11 @@ data PendingQuery = PendingQuery
87 deriving Show 88 deriving Show
88 89
89data RouteRecord = RouteRecord 90data RouteRecord = RouteRecord
90 { storedRoute :: OnionRoute 91 { storedRoute :: OnionRoute
91 , responseCount :: !Int 92 , responseCount :: !Int
92 , timeoutCount :: !Int 93 , timeoutCount :: !Int
93 , routeVersion :: !Int 94 , routeVersion :: !Int
95 , routeBirthTime :: !POSIXTime
94 } 96 }
95 97
96-- Onion paths have different timeouts depending on whether the path is 98-- Onion paths have different timeouts depending on whether the path is
@@ -107,12 +109,13 @@ timeoutForRoute :: RouteRecord -> Int
107timeoutForRoute RouteRecord{ responseCount = 0 } = 4000000 109timeoutForRoute RouteRecord{ responseCount = 0 } = 4000000
108timeoutForRoute RouteRecord{ responseCount = _ } = 10000000 110timeoutForRoute RouteRecord{ responseCount = _ } = 10000000
109 111
110freshRoute :: OnionRoute -> Maybe RouteRecord -> Maybe RouteRecord 112freshRoute :: POSIXTime -> OnionRoute -> Maybe RouteRecord -> Maybe RouteRecord
111freshRoute r mrec = Just $ RouteRecord 113freshRoute birthday r mrec = Just $ RouteRecord
112 { storedRoute = r 114 { storedRoute = r
113 , responseCount = 0 115 , responseCount = 0
114 , timeoutCount = 0 116 , timeoutCount = 0
115 , routeVersion = maybe 0 succ $ routeVersion <$> mrec 117 , routeVersion = maybe 0 succ $ routeVersion <$> mrec
118 , routeBirthTime = birthday
116 } 119 }
117 120
118gotResponse :: RouteRecord -> RouteRecord 121gotResponse :: RouteRecord -> RouteRecord
@@ -333,9 +336,10 @@ handleEvent getnodes or e@(BuildRoute (RouteId rid)) = do
333 _ -> return Nothing 336 _ -> return Nothing
334 writeTVar (onionDRG or) drg' 337 writeTVar (onionDRG or) drg'
335 return $ getr 338 return $ getr
339 now <- getPOSIXTime
336 atomically $ maybe (return ()) -- writeTVar (pendingRoutes or IntMap.! rid) True) 340 atomically $ maybe (return ()) -- writeTVar (pendingRoutes or IntMap.! rid) True)
337 (\r -> do modifyTVar' (routeMap or) 341 (\r -> do modifyTVar' (routeMap or)
338 (IntMap.alter (freshRoute r) rid) 342 (IntMap.alter (freshRoute now r) rid)
339 v <- routeVersion . (IntMap.! rid) <$> readTVar (routeMap or) 343 v <- routeVersion . (IntMap.! rid) <$> readTVar (routeMap or)
340 writeTVar (pendingRoutes or IntMap.! rid) v 344 writeTVar (pendingRoutes or IntMap.! rid) v
341 ) 345 )
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 6437e94f..36a9fa68 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -813,7 +813,9 @@ clientSession s@Session{..} sock cnum h = do
813 Tox.sendChatMsg (Tox.toxCryptoKeys tox) session (B.pack msg) 813 Tox.sendChatMsg (Tox.toxCryptoKeys tox) session (B.pack msg)
814 hPutClient h "sent MESSAGE" 814 hPutClient h "sent MESSAGE"
815 815
816 ("onion", s) -> cmd0 $ join $ atomically $ do 816 ("onion", s) -> cmd0 $ do
817 now <- getPOSIXTime
818 join $ atomically $ do
817 rm <- readTVar $ routeMap onionRouter 819 rm <- readTVar $ routeMap onionRouter
818 ts <- readTVar $ trampolineNodes onionRouter 820 ts <- readTVar $ trampolineNodes onionRouter
819 tcnt <- readTVar $ trampolineCount onionRouter 821 tcnt <- readTVar $ trampolineCount onionRouter
@@ -822,16 +824,17 @@ clientSession s@Session{..} sock cnum h = do
822 pqs <- readTVar (pendingQueries onionRouter) 824 pqs <- readTVar (pendingQueries onionRouter)
823 let showRecord :: Int -> Int -> [String] 825 let showRecord :: Int -> Int -> [String]
824 showRecord n wanted_ver 826 showRecord n wanted_ver
825 | Just RouteRecord{responseCount,timeoutCount,routeVersion} <- IntMap.lookup n rm 827 | Just RouteRecord{responseCount,timeoutCount,routeVersion,routeBirthTime} <- IntMap.lookup n rm
826 = if routeVersion >= wanted_ver 828 = [ show n, show responseCount, show timeoutCount, show (now-routeBirthTime)
827 then [show n, show responseCount, show timeoutCount, show (routeVersion,wanted_ver) ] 829 , if routeVersion >= wanted_ver
828 else [show n, show responseCount, show timeoutCount, show (routeVersion,wanted_ver) ++ "(pending)" ] 830 then show routeVersion
829 | otherwise = [show n, "error!",""] 831 else show routeVersion ++ "(pending)" ]
832 | otherwise = [show n, "error!","","",""]
830 r = map (uncurry showRecord) $ IntMap.toAscList rs 833 r = map (uncurry showRecord) $ IntMap.toAscList rs
831 return $ do 834 return $ do
832 hPutClientChunk h $ unlines [ "trampolines: " ++ show (IntMap.size ts,tcnt,icnt) 835 hPutClientChunk h $ unlines [ "trampolines: " ++ show (IntMap.size ts,tcnt,icnt)
833 , "pending: " ++ show (W64.size pqs) ] 836 , "pending: " ++ show (W64.size pqs) ]
834 hPutClient h $ showColumns $ ["","responses","timeouts", "version"]:r 837 hPutClient h $ showColumns $ ["","responses","timeouts", "age", "version"]:r
835 838
836 -- necrypto <FRIEND-TOXID> 839 -- necrypto <FRIEND-TOXID>
837 -- establish a netcrypto session with specified person 840 -- establish a netcrypto session with specified person