summaryrefslogtreecommitdiff
path: root/OnionRouter.hs
diff options
context:
space:
mode:
Diffstat (limited to 'OnionRouter.hs')
-rw-r--r--OnionRouter.hs32
1 files changed, 22 insertions, 10 deletions
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
45-- Unlike toxcore, we don't currently reserve paths for only-searching or 45-- Unlike toxcore, we don't currently reserve paths for only-searching or
46-- only-announcing. Instead, we maintain 12 multi-purpose routes. 46-- only-announcing. Instead, we maintain 12 multi-purpose routes.
47data OnionRouter = OnionRouter 47data OnionRouter = OnionRouter
48 { pendingQueries :: TVar (Word64Map NodeId) 48 { -- | For every query, we remember the destination and source keys
49 -- so we can decrypt the response. Note, the RouteId field is not
50 -- currently stored here. It is inferred from the lookup key. Instead,
51 -- a 'Nothing' is stored.
52 pendingQueries :: TVar (Word64Map (OnionDestination RouteId))
49 , routeMap :: TVar (IntMap RouteRecord) 53 , routeMap :: TVar (IntMap RouteRecord)
50 , trampolineNodes :: TVar (IntMap NodeInfo) 54 , trampolineNodes :: TVar (IntMap NodeInfo)
51 , trampolineIds :: TVar (HashMap NodeId Int) 55 , trampolineIds :: TVar (HashMap NodeId Int)
@@ -268,7 +272,7 @@ handleEvent getnodes or e@(BuildRoute (RouteId rid)) = do
268routeId :: Nonce8 -> RouteId 272routeId :: Nonce8 -> RouteId
269routeId (Nonce8 w8) = RouteId $ mod (fromIntegral w8) 12 273routeId (Nonce8 w8) = RouteId $ mod (fromIntegral w8) 12
270 274
271lookupSender :: OnionRouter -> SockAddr -> Nonce8 -> IO (Maybe (NodeInfo, RouteId)) 275lookupSender :: OnionRouter -> SockAddr -> Nonce8 -> IO (Maybe (OnionDestination RouteId))
272lookupSender or saddr (Nonce8 w8) = do 276lookupSender or saddr (Nonce8 w8) = do
273 result <- atomically $ do 277 result <- atomically $ do
274 ks <- readTVar (pendingQueries or) 278 ks <- readTVar (pendingQueries or)
@@ -276,9 +280,12 @@ lookupSender or saddr (Nonce8 w8) = do
276 writeTChan (routeLog or) $ "ONION lookupSender " ++ unwords [show w8, "->", show r] 280 writeTChan (routeLog or) $ "ONION lookupSender " ++ unwords [show w8, "->", show r]
277 return r 281 return r
278 return $ do 282 return $ do
279 nid <- result 283 od <- result
284 let nid = nodeId $ onionNodeInfo od
280 ni <- either (const Nothing) Just $ nodeInfo nid saddr 285 ni <- either (const Nothing) Just $ nodeInfo nid saddr
281 Just (ni, routeId (Nonce8 w8)) 286 Just (OnionDestination (onionAliasSelector od)
287 ni
288 (Just $ routeId (Nonce8 w8)))
282 289
283lookupRoute :: OnionRouter -> NodeInfo -> RouteId -> IO (Maybe OnionRoute) 290lookupRoute :: OnionRouter -> NodeInfo -> RouteId -> IO (Maybe OnionRoute)
284lookupRoute or ni (RouteId rid) = do 291lookupRoute or ni (RouteId rid) = do
@@ -286,13 +293,13 @@ lookupRoute or ni (RouteId rid) = do
286 return $ storedRoute <$> mb 293 return $ storedRoute <$> mb
287 294
288lookupTimeout :: OnionRouter -> Nonce8 -> OnionDestination r -> STM (OnionDestination RouteId, Int) 295lookupTimeout :: OnionRouter -> Nonce8 -> OnionDestination r -> STM (OnionDestination RouteId, Int)
289lookupTimeout or n8 (OnionDestination ni Nothing) = do 296lookupTimeout or n8 (OnionDestination asel ni Nothing) = do
290 let RouteId rid = routeId n8 297 let RouteId rid = routeId n8
291 mrr <- IntMap.lookup rid <$> readTVar (routeMap or) 298 mrr <- IntMap.lookup rid <$> readTVar (routeMap or)
292 readTVar (routeMap or) >>= \rm -> writeTChan (routeLog or) $ "ONION lookupTimeout " ++ unwords [show rid,show (IntMap.keys rm)] 299 readTVar (routeMap or) >>= \rm -> writeTChan (routeLog or) $ "ONION lookupTimeout " ++ unwords [show rid,show (IntMap.keys rm)]
293 case mrr of 300 case mrr of
294 Just rr -> return ( OnionDestination ni (Just $ routeId n8), timeoutForRoute rr) 301 Just rr -> return ( OnionDestination asel ni (Just $ routeId n8), timeoutForRoute rr)
295 Nothing -> return ( OnionDestination ni Nothing , 0 ) 302 Nothing -> return ( OnionDestination asel ni Nothing , 0 )
296 303
297hookQueries :: OnionRouter -> (tid -> Nonce8) 304hookQueries :: OnionRouter -> (tid -> Nonce8)
298 -> TransactionMethods d tid (OnionDestination r) x 305 -> TransactionMethods d tid (OnionDestination r) x
@@ -301,8 +308,10 @@ hookQueries or t8 tmethods = TransactionMethods
301 { dispatchRegister = \mvar od d -> do -- :: MVar x -> d -> STM (tid, d) 308 { dispatchRegister = \mvar od d -> do -- :: MVar x -> d -> STM (tid, d)
302 (tid,d') <- dispatchRegister tmethods mvar od d 309 (tid,d') <- dispatchRegister tmethods mvar od d
303 let Nonce8 w8 = t8 tid 310 let Nonce8 w8 = t8 tid
311 od' = case od of OnionDestination {} -> od { onionRouteSpec = Nothing }
312 OnionToOwner a b -> OnionToOwner a b -- Type cast.
304 ni = onionNodeInfo od 313 ni = onionNodeInfo od
305 modifyTVar' (pendingQueries or) (W64.insert w8 (nodeId ni)) 314 modifyTVar' (pendingQueries or) (W64.insert w8 od')
306 writeTChan (routeLog or) $ "ONION query add " ++ unwords [ show w8, ":=", show ni ] 315 writeTChan (routeLog or) $ "ONION query add " ++ unwords [ show w8, ":=", show ni ]
307 return (tid,d') 316 return (tid,d')
308 , dispatchResponse = \tid x d -> do -- :: tid -> x -> d -> STM (d, IO ()) 317 , dispatchResponse = \tid x d -> do -- :: tid -> x -> d -> STM (d, IO ())
@@ -345,8 +354,11 @@ hookBucketList kademlia bkts0 or (RoutingTransition ni Accepted) = do
345 let antibias = 2 ^ bucketNumber kademlia (nodeId ni) bkts 354 let antibias = 2 ^ bucketNumber kademlia (nodeId ni) bkts
346 (s,drg) = randomR (0,antibias - 1) drg0 355 (s,drg) = randomR (0,antibias - 1) drg0
347 writeTVar (onionDRG or) drg 356 writeTVar (onionDRG or) drg
348 -- let self = (thisNode bkts) { nodeIP = read "127.0.0.1" } 357 do -- Store localhost as trampoline node (-1).
349 -- modifyTVar' (trampolineNodes or) (IntMap.insert (-1) self) 358 -- This is not used, but harmless. I'm leaving it in for
359 -- testing purposes.
360 let self = (thisNode bkts) { nodeIP = read "127.0.0.1" }
361 modifyTVar' (trampolineNodes or) (IntMap.insert (-1) self)
350 return s 362 return s
351 -- debias via stochastic filter 363 -- debias via stochastic filter
352 when (s == 0) $ do 364 when (s == 0) $ do