summaryrefslogtreecommitdiff
path: root/dht/src/Network/Tox/Onion/Routes.hs
diff options
context:
space:
mode:
Diffstat (limited to 'dht/src/Network/Tox/Onion/Routes.hs')
-rw-r--r--dht/src/Network/Tox/Onion/Routes.hs83
1 files changed, 44 insertions, 39 deletions
diff --git a/dht/src/Network/Tox/Onion/Routes.hs b/dht/src/Network/Tox/Onion/Routes.hs
index 9ce4e316..2f13a513 100644
--- a/dht/src/Network/Tox/Onion/Routes.hs
+++ b/dht/src/Network/Tox/Onion/Routes.hs
@@ -88,7 +88,7 @@ data OnionRouter = OnionRouter
88 , tcpProber :: TCP.TCPProber 88 , tcpProber :: TCP.TCPProber
89 , tcpProberThread :: ThreadId 89 , tcpProberThread :: ThreadId
90 -- | Kademlia table of TCP relays. 90 -- | Kademlia table of TCP relays.
91 , tcpBucketRefresher :: BucketRefresher NodeId TCP.NodeInfo 91 , tcpBucketRefresher :: BucketRefresher NodeId TCP.NodeInfo Nonce8
92 , tcpRelayPinger :: RelayPinger 92 , tcpRelayPinger :: RelayPinger
93 -- | Debug prints are written to this channel which is then flushed to 93 -- | Debug prints are written to this channel which is then flushed to
94 -- 'routeLogger'. 94 -- 'routeLogger'.
@@ -601,44 +601,49 @@ hookQueries or t8 tmethods = TransactionMethods
601 modifyTVar' (pendingQueries or) (W64.insert w8 pq) 601 modifyTVar' (pendingQueries or) (W64.insert w8 pq)
602 writeTChan (routeLog or) $ "ONION query add " ++ unwords [ show (Just $ pendingVersion pq,w8), ":=", show ni ] 602 writeTChan (routeLog or) $ "ONION query add " ++ unwords [ show (Just $ pendingVersion pq,w8), ":=", show ni ]
603 return (tid,d') 603 return (tid,d')
604 , dispatchResponse = \tid x d -> {-# SCC "hookQ.dispatchResponse" #-} do -- :: tid -> x -> d -> STM (d, IO ()) 604 , dispatchResponse = \tid rx d -> {-# SCC "hookQ.dispatchResponse" #-} do -- :: tid -> x -> d -> STM (d, IO ())
605 let Nonce8 w8 = t8 tid 605 case rx of
606 mb <- W64.lookup w8 <$> readTVar (pendingQueries or) 606 Success x -> do
607 modifyTVar' (pendingQueries or) (W64.delete w8) 607 let Nonce8 w8 = t8 tid
608 forM_ mb $ \pq -> do 608 mb <- W64.lookup w8 <$> readTVar (pendingQueries or)
609 let od = pendingDestination pq 609 modifyTVar' (pendingQueries or) (W64.delete w8)
610 RouteId rid = fromMaybe (routeId (nodeId (onionNodeInfo od))) 610 forM_ mb $ \pq -> do
611 $ onionRouteSpec od 611 let od = pendingDestination pq
612 modifyArray (routeMap or) (fmap gotResponse) rid 612 RouteId rid = fromMaybe (routeId (nodeId (onionNodeInfo od)))
613 writeTChan (routeLog or) $ "ONION query del " ++ show (fmap pendingVersion mb, w8) 613 $ onionRouteSpec od
614 dispatchResponse tmethods tid x d 614 modifyArray (routeMap or) (fmap gotResponse) rid
615 , dispatchCancel = \tid d -> {-# SCC "hookQ.dispatchCancel" #-} do -- :: tid -> d -> STM d 615 writeTChan (routeLog or) $ "ONION query del " ++ show (fmap pendingVersion mb, w8)
616 let Nonce8 w8 = t8 tid 616 dispatchResponse tmethods tid rx d
617 mb <- W64.lookup w8 <$> readTVar (pendingQueries or) 617 _ -> do -- Timed out or canceled...
618 modifyTVar' (pendingQueries or) (W64.delete w8) 618 let Nonce8 w8 = t8 tid
619 forM_ mb $ \pq -> do 619 mb <- W64.lookup w8 <$> readTVar (pendingQueries or)
620 let od = pendingDestination pq 620 modifyTVar' (pendingQueries or) (W64.delete w8)
621 RouteId rid = fromMaybe (routeId (nodeId (onionNodeInfo od))) 621 forM_ mb $ \pq -> do
622 $ onionRouteSpec od 622 let od = pendingDestination pq
623 mrr <- readArray (routeMap or) rid 623 RouteId rid = fromMaybe (routeId (nodeId (onionNodeInfo od)))
624 forM_ mrr $ \rr -> do 624 $ onionRouteSpec od
625 when (routeVersion rr == pendingVersion pq) $ do 625 mrr <- readArray (routeMap or) rid
626 let expireRoute = modifyArray (pendingRoutes or) expire rid 626 forM_ mrr $ \rr -> do
627 expire ver | ver <= succ (pendingVersion pq) = succ (pendingVersion pq) 627 when (routeVersion rr == pendingVersion pq) $ do
628 | otherwise = ver 628 let expireRoute = modifyArray (pendingRoutes or) expire rid
629 modifyArray (routeMap or) (fmap gotTimeout) rid 629 expire ver | ver <= succ (pendingVersion pq) = succ (pendingVersion pq)
630 case rr of 630 | otherwise = ver
631 RouteRecord{ responseCount = 0 631 case rx of
632 , timeoutCount = c 632 TimedOut -> do
633 , routeVersion = v } | c >= 5 -> expireRoute 633 modifyArray (routeMap or) (fmap gotTimeout) rid
634 RouteRecord{ responseCount = 1 634 case rr of
635 , timeoutCount = c 635 RouteRecord{ responseCount = 0
636 , routeVersion = v } | c >= 10 -> expireRoute 636 , timeoutCount = c
637 RouteRecord{ timeoutCount = c 637 , routeVersion = v } | c >= 5 -> expireRoute
638 , routeVersion = v } | c >= 20 -> expireRoute 638 RouteRecord{ responseCount = 1
639 _ -> return () 639 , timeoutCount = c
640 writeTChan (routeLog or) $ "ONION query can " ++ show (fmap pendingVersion mb, w8) 640 , routeVersion = v } | c >= 10 -> expireRoute
641 dispatchCancel tmethods tid d 641 RouteRecord{ timeoutCount = c
642 , routeVersion = v } | c >= 20 -> expireRoute
643 _ -> return ()
644 _ -> return () -- Don't penalize route for canceled queries.
645 writeTChan (routeLog or) $ "ONION query can " ++ show (fmap pendingVersion mb, w8)
646 dispatchResponse tmethods tid rx d
642 } 647 }
643 648
644 649