From 987915fb21ac824bfb8fc49c5cceb3aa0f1440c2 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 19 Oct 2017 22:00:34 -0400 Subject: Successful toxid announce. --- OnionRouter.hs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'OnionRouter.hs') diff --git a/OnionRouter.hs b/OnionRouter.hs index 2ec7f3ac..7125fa10 100644 --- a/OnionRouter.hs +++ b/OnionRouter.hs @@ -45,7 +45,11 @@ newtype RouteId = RouteId Int -- Unlike toxcore, we don't currently reserve paths for only-searching or -- only-announcing. Instead, we maintain 12 multi-purpose routes. data OnionRouter = OnionRouter - { pendingQueries :: TVar (Word64Map NodeId) + { -- | For every query, we remember the destination and source keys + -- so we can decrypt the response. Note, the RouteId field is not + -- currently stored here. It is inferred from the lookup key. Instead, + -- a 'Nothing' is stored. + pendingQueries :: TVar (Word64Map (OnionDestination RouteId)) , routeMap :: TVar (IntMap RouteRecord) , trampolineNodes :: TVar (IntMap NodeInfo) , trampolineIds :: TVar (HashMap NodeId Int) @@ -268,7 +272,7 @@ handleEvent getnodes or e@(BuildRoute (RouteId rid)) = do routeId :: Nonce8 -> RouteId routeId (Nonce8 w8) = RouteId $ mod (fromIntegral w8) 12 -lookupSender :: OnionRouter -> SockAddr -> Nonce8 -> IO (Maybe (NodeInfo, RouteId)) +lookupSender :: OnionRouter -> SockAddr -> Nonce8 -> IO (Maybe (OnionDestination RouteId)) lookupSender or saddr (Nonce8 w8) = do result <- atomically $ do ks <- readTVar (pendingQueries or) @@ -276,9 +280,12 @@ lookupSender or saddr (Nonce8 w8) = do writeTChan (routeLog or) $ "ONION lookupSender " ++ unwords [show w8, "->", show r] return r return $ do - nid <- result + od <- result + let nid = nodeId $ onionNodeInfo od ni <- either (const Nothing) Just $ nodeInfo nid saddr - Just (ni, routeId (Nonce8 w8)) + Just (OnionDestination (onionAliasSelector od) + ni + (Just $ routeId (Nonce8 w8))) lookupRoute :: OnionRouter -> NodeInfo -> RouteId -> IO (Maybe OnionRoute) lookupRoute or ni (RouteId rid) = do @@ -286,13 +293,13 @@ lookupRoute or ni (RouteId rid) = do return $ storedRoute <$> mb lookupTimeout :: OnionRouter -> Nonce8 -> OnionDestination r -> STM (OnionDestination RouteId, Int) -lookupTimeout or n8 (OnionDestination ni Nothing) = do +lookupTimeout or n8 (OnionDestination asel ni Nothing) = do let RouteId rid = routeId n8 mrr <- IntMap.lookup rid <$> readTVar (routeMap or) readTVar (routeMap or) >>= \rm -> writeTChan (routeLog or) $ "ONION lookupTimeout " ++ unwords [show rid,show (IntMap.keys rm)] case mrr of - Just rr -> return ( OnionDestination ni (Just $ routeId n8), timeoutForRoute rr) - Nothing -> return ( OnionDestination ni Nothing , 0 ) + Just rr -> return ( OnionDestination asel ni (Just $ routeId n8), timeoutForRoute rr) + Nothing -> return ( OnionDestination asel ni Nothing , 0 ) hookQueries :: OnionRouter -> (tid -> Nonce8) -> TransactionMethods d tid (OnionDestination r) x @@ -301,8 +308,10 @@ hookQueries or t8 tmethods = TransactionMethods { dispatchRegister = \mvar od d -> do -- :: MVar x -> d -> STM (tid, d) (tid,d') <- dispatchRegister tmethods mvar od d let Nonce8 w8 = t8 tid + od' = case od of OnionDestination {} -> od { onionRouteSpec = Nothing } + OnionToOwner a b -> OnionToOwner a b -- Type cast. ni = onionNodeInfo od - modifyTVar' (pendingQueries or) (W64.insert w8 (nodeId ni)) + modifyTVar' (pendingQueries or) (W64.insert w8 od') writeTChan (routeLog or) $ "ONION query add " ++ unwords [ show w8, ":=", show ni ] return (tid,d') , dispatchResponse = \tid x d -> do -- :: tid -> x -> d -> STM (d, IO ()) @@ -345,8 +354,11 @@ hookBucketList kademlia bkts0 or (RoutingTransition ni Accepted) = do let antibias = 2 ^ bucketNumber kademlia (nodeId ni) bkts (s,drg) = randomR (0,antibias - 1) drg0 writeTVar (onionDRG or) drg - -- let self = (thisNode bkts) { nodeIP = read "127.0.0.1" } - -- modifyTVar' (trampolineNodes or) (IntMap.insert (-1) self) + do -- Store localhost as trampoline node (-1). + -- This is not used, but harmless. I'm leaving it in for + -- testing purposes. + let self = (thisNode bkts) { nodeIP = read "127.0.0.1" } + modifyTVar' (trampolineNodes or) (IntMap.insert (-1) self) return s -- debias via stochastic filter when (s == 0) $ do -- cgit v1.2.3