summaryrefslogtreecommitdiff
path: root/OnionRouter.hs
diff options
context:
space:
mode:
Diffstat (limited to 'OnionRouter.hs')
-rw-r--r--OnionRouter.hs24
1 files changed, 13 insertions, 11 deletions
diff --git a/OnionRouter.hs b/OnionRouter.hs
index 757214cd..20279c5d 100644
--- a/OnionRouter.hs
+++ b/OnionRouter.hs
@@ -31,7 +31,6 @@ import qualified Data.Word64Map as W64
31 ;import Data.Word64Map (Word64Map, fitsInInt) 31 ;import Data.Word64Map (Word64Map, fitsInInt)
32import Network.Socket 32import Network.Socket
33import System.Endian 33import System.Endian
34import System.IO
35import System.Timeout 34import System.Timeout
36 35
37-- Toxcore saves a maximum of 12 paths: 6 paths are reserved for announcing 36-- Toxcore saves a maximum of 12 paths: 6 paths are reserved for announcing
@@ -76,8 +75,10 @@ data OnionRouter = OnionRouter
76 -- route should be discarded and replaced with a fresh one. 75 -- route should be discarded and replaced with a fresh one.
77 , pendingRoutes :: IntMap (TVar Bool) 76 , pendingRoutes :: IntMap (TVar Bool)
78 -- | Debug prints are written to this channel which is then flushed to 77 -- | Debug prints are written to this channel which is then flushed to
79 -- stderr from within the 'routeThread'. 78 -- 'routeLogger'.
80 , routeLog :: TChan String 79 , routeLog :: TChan String
80 -- | User supplied log function.
81 , routeLogger :: String -> IO ()
81 } 82 }
82 83
83data RouteRecord = RouteRecord 84data RouteRecord = RouteRecord
@@ -120,8 +121,8 @@ gotTimeout rr = rr
120 121
121data RouteEvent = BuildRoute RouteId 122data RouteEvent = BuildRoute RouteId
122 123
123newOnionRouter :: IO OnionRouter 124newOnionRouter :: (String -> IO ()) -> IO OnionRouter
124newOnionRouter = do 125newOnionRouter perror = do
125 drg0 <- drgNew 126 drg0 <- drgNew
126 or <- atomically $ do 127 or <- atomically $ do
127 chan <- newTChan 128 chan <- newTChan
@@ -144,6 +145,7 @@ newOnionRouter = do
144 , trampolineCount = tc 145 , trampolineCount = tc
145 , routeLog = rlog 146 , routeLog = rlog
146 , routeThread = error "Failed to invoke forkRouteBuilder" 147 , routeThread = error "Failed to invoke forkRouteBuilder"
148 , routeLogger = perror
147 } 149 }
148 return or 150 return or
149 151
@@ -159,7 +161,7 @@ forkRouteBuilder or getnodes = do
159 -- writeTVar want_build False -- Prevent redundant BuildRoute events. 161 -- writeTVar want_build False -- Prevent redundant BuildRoute events.
160 return $ BuildRoute $ RouteId rid 162 return $ BuildRoute $ RouteId rid
161 io <- atomically $ 163 io <- atomically $
162 (readTChan (routeLog or) >>= return . hPutStrLn stderr) 164 (readTChan (routeLog or) >>= return . routeLogger or)
163 `orElse` 165 `orElse`
164 (IntMap.foldrWithKey checkRebuild retry (pendingRoutes or) 166 (IntMap.foldrWithKey checkRebuild retry (pendingRoutes or)
165 >>= return . handleEvent getnodes or { routeThread = me }) 167 >>= return . handleEvent getnodes or { routeThread = me })
@@ -214,7 +216,7 @@ selectTrampolines or = do
214 atomically (selectTrampolines' or) >>= \case 216 atomically (selectTrampolines' or) >>= \case
215 Left ns -> do 217 Left ns -> do
216 -- atomically $ writeTChan (routeLog or) 218 -- atomically $ writeTChan (routeLog or)
217 hPutStrLn stderr $ unwords 219 routeLogger or $ unwords
218 ( "ONION Discarding insecure trampolines:" : (map (show . nodeAddr) ns) ) 220 ( "ONION Discarding insecure trampolines:" : (map (show . nodeAddr) ns) )
219 myThreadId >>= flip labelThread ("OnionRouter.selectTrampolines.sleep") 221 myThreadId >>= flip labelThread ("OnionRouter.selectTrampolines.sleep")
220 threadDelay 1000000 222 threadDelay 1000000
@@ -248,7 +250,7 @@ selectTrampolines' or = do
248 250
249handleEvent :: (NodeId -> NodeInfo -> IO (Maybe [NodeInfo])) -> OnionRouter -> RouteEvent -> IO () 251handleEvent :: (NodeId -> NodeInfo -> IO (Maybe [NodeInfo])) -> OnionRouter -> RouteEvent -> IO ()
250handleEvent getnodes or e@(BuildRoute (RouteId rid)) = do 252handleEvent getnodes or e@(BuildRoute (RouteId rid)) = do
251 hPutStrLn stderr $ "ONION Rebuilding RouteId " ++ show rid 253 routeLogger or $ "ONION Rebuilding RouteId " ++ show rid
252 mb <- do 254 mb <- do
253 ts <- selectTrampolines or 255 ts <- selectTrampolines or
254 join . atomically $ do 256 join . atomically $ do
@@ -279,7 +281,7 @@ handleEvent getnodes or e@(BuildRoute (RouteId rid)) = do
279 -- self <- IntMap.lookup (-1) <$> readTVar (trampolineNodes or) 281 -- self <- IntMap.lookup (-1) <$> readTVar (trampolineNodes or)
280 -- return $ maybe (catMaybes rs) (\x -> [x,x,x]) self 282 -- return $ maybe (catMaybes rs) (\x -> [x,x,x]) self
281 _ -> retry 283 _ -> retry
282 maybe (hPutStrLn stderr "ONION: Unexpected sendq timeout!" >> return []) 284 maybe (routeLogger or "ONION: Unexpected sendq timeout!" >> return [])
283 return 285 return
284 tm 286 tm
285 return $ do 287 return $ do
@@ -288,7 +290,7 @@ handleEvent getnodes or e@(BuildRoute (RouteId rid)) = do
288 [_,_,_] -> sendqs 290 [_,_,_] -> sendqs
289 _ -> return [] 291 _ -> return []
290 myThreadId >>= flip labelThread ("OnionRouter") 292 myThreadId >>= flip labelThread ("OnionRouter")
291 hPutStr stderr $ unlines 293 routeLogger or $ unlines
292 [ "ONION trampolines: " ++ show ts 294 [ "ONION trampolines: " ++ show ts
293 , "ONION query results: " ++ show nodes ] 295 , "ONION query results: " ++ show nodes ]
294 case nodes of 296 case nodes of
@@ -319,8 +321,8 @@ handleEvent getnodes or e@(BuildRoute (RouteId rid)) = do
319 ) 321 )
320 mb 322 mb
321 case mb of 323 case mb of
322 Just _ -> hPutStrLn stderr $ "ONION Finished RouteId " ++ show rid 324 Just _ -> routeLogger or $ "ONION Finished RouteId " ++ show rid
323 Nothing -> hPutStrLn stderr $ "ONION Failed RouteId " ++ show rid 325 Nothing -> routeLogger or $ "ONION Failed RouteId " ++ show rid
324 326
325lookupSender :: OnionRouter -> SockAddr -> Nonce8 -> IO (Maybe (OnionDestination RouteId)) 327lookupSender :: OnionRouter -> SockAddr -> Nonce8 -> IO (Maybe (OnionDestination RouteId))
326lookupSender or saddr (Nonce8 w8) = do 328lookupSender or saddr (Nonce8 w8) = do