diff options
Diffstat (limited to 'src/Network/Tox')
-rw-r--r-- | src/Network/Tox/DHT/Handlers.hs | 69 | ||||
-rw-r--r-- | src/Network/Tox/Onion/Handlers.hs | 46 |
2 files changed, 100 insertions, 15 deletions
diff --git a/src/Network/Tox/DHT/Handlers.hs b/src/Network/Tox/DHT/Handlers.hs index abd607c3..1eec93b9 100644 --- a/src/Network/Tox/DHT/Handlers.hs +++ b/src/Network/Tox/DHT/Handlers.hs | |||
@@ -400,20 +400,61 @@ unsendNodes _ = Nothing | |||
400 | unwrapNodes :: SendNodes -> ( [NodeInfo], [NodeInfo], Maybe () ) | 400 | unwrapNodes :: SendNodes -> ( [NodeInfo], [NodeInfo], Maybe () ) |
401 | unwrapNodes (SendNodes ns) = (ns,ns,Just ()) | 401 | unwrapNodes (SendNodes ns) = (ns,ns,Just ()) |
402 | 402 | ||
403 | data SendableQuery x a b = SendableQuery | ||
404 | { sendableSerializer :: MethodSerializer TransactionId NodeInfo Message PacketKind a (Maybe x) | ||
405 | , sendableQuery :: NodeId -> a | ||
406 | , sendableResult :: Maybe (Maybe x) -> IO b | ||
407 | } | ||
408 | |||
409 | sendQ :: SendableQuery x a b | ||
410 | -> QR.Client err PacketKind TransactionId NodeInfo Message | ||
411 | -> NodeId | ||
412 | -> NodeInfo | ||
413 | -> IO b | ||
414 | sendQ s client nid addr = do | ||
415 | reply <- QR.sendQuery client (sendableSerializer s) (sendableQuery s nid) addr | ||
416 | sendableResult s reply | ||
417 | |||
418 | asyncQ :: SendableQuery x a b | ||
419 | -> QR.Client err PacketKind TransactionId NodeInfo Message | ||
420 | -> NodeId | ||
421 | -> NodeInfo | ||
422 | -> (b -> IO ()) | ||
423 | -> IO () | ||
424 | asyncQ s client nid addr go = do | ||
425 | QR.asyncQuery client (sendableSerializer s) (sendableQuery s nid) addr | ||
426 | $ sendableResult s >=> go | ||
427 | |||
428 | getNodesSendable :: TVar (HashMap NodeId [NodeInfoCallback]) | ||
429 | -> NodeInfo | ||
430 | -> SendableQuery SendNodes GetNodes (Maybe ([NodeInfo], [NodeInfo], Maybe ())) | ||
431 | getNodesSendable cbvar addr = SendableQuery (serializer GetNodesType DHTGetNodes unsendNodes) | ||
432 | GetNodes | ||
433 | go | ||
434 | where | ||
435 | go reply = do | ||
436 | forM_ (join reply) $ \(SendNodes ns) -> | ||
437 | forM_ ns $ \n -> do | ||
438 | now <- getPOSIXTime | ||
439 | atomically $ do | ||
440 | mcbs <- HashMap.lookup (nodeId n) <$> readTVar cbvar | ||
441 | forM_ mcbs $ \cbs -> do | ||
442 | forM_ cbs $ \cb -> do | ||
443 | rumoredAddress cb now (nodeAddr addr) n | ||
444 | return $ fmap unwrapNodes $ join reply | ||
445 | |||
403 | getNodes :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],Maybe ())) | 446 | getNodes :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],Maybe ())) |
404 | getNodes client cbvar nid addr = do | 447 | getNodes client cbvar nid addr = |
405 | -- dput XMisc $ show addr ++ " <-- getnodes " ++ show nid | 448 | sendQ (getNodesSendable cbvar addr) client nid addr |
406 | reply <- QR.sendQuery client (serializer GetNodesType DHTGetNodes unsendNodes) (GetNodes nid) addr | 449 | |
407 | -- dput XMisc $ show addr ++ " -sendnodes-> " ++ show reply | 450 | asyncGetNodes :: QR.Client err PacketKind TransactionId NodeInfo Message |
408 | forM_ (join reply) $ \(SendNodes ns) -> | 451 | -> TVar (HashMap NodeId [NodeInfoCallback]) |
409 | forM_ ns $ \n -> do | 452 | -> NodeId |
410 | now <- getPOSIXTime | 453 | -> NodeInfo |
411 | atomically $ do | 454 | -> (Maybe ([NodeInfo], [NodeInfo], Maybe ()) -> IO ()) |
412 | mcbs <- HashMap.lookup (nodeId n) <$> readTVar cbvar | 455 | -> IO () |
413 | forM_ mcbs $ \cbs -> do | 456 | asyncGetNodes client cbvar nid addr go = |
414 | forM_ cbs $ \cb -> do | 457 | asyncQ (getNodesSendable cbvar addr) client nid addr go |
415 | rumoredAddress cb now (nodeAddr addr) n | ||
416 | return $ fmap unwrapNodes $ join reply | ||
417 | 458 | ||
418 | updateRouting :: Client -> Routing | 459 | updateRouting :: Client -> Routing |
419 | -> (TVar (R.BucketList NodeInfo) -> RoutingTransition NodeInfo -> STM ()) | 460 | -> (TVar (R.BucketList NodeInfo) -> RoutingTransition NodeInfo -> STM ()) |
@@ -525,7 +566,7 @@ nodeSearch :: Client -> TVar (HashMap NodeId [NodeInfoCallback]) -> Search NodeI | |||
525 | nodeSearch client cbvar = Search | 566 | nodeSearch client cbvar = Search |
526 | { searchSpace = toxSpace | 567 | { searchSpace = toxSpace |
527 | , searchNodeAddress = nodeIP &&& nodePort | 568 | , searchNodeAddress = nodeIP &&& nodePort |
528 | , searchQuery = Left $ getNodes client cbvar | 569 | , searchQuery = Right $ asyncGetNodes client cbvar |
529 | , searchAlpha = 8 | 570 | , searchAlpha = 8 |
530 | , searchK = 16 | 571 | , searchK = 16 |
531 | 572 | ||
diff --git a/src/Network/Tox/Onion/Handlers.hs b/src/Network/Tox/Onion/Handlers.hs index edbbbb49..f44dd79c 100644 --- a/src/Network/Tox/Onion/Handlers.hs +++ b/src/Network/Tox/Onion/Handlers.hs | |||
@@ -218,7 +218,7 @@ toxidSearch :: (TransactionId -> OnionDestination r -> STM (OnionDestination r, | |||
218 | toxidSearch getTimeout crypto client = Search | 218 | toxidSearch getTimeout crypto client = Search |
219 | { searchSpace = toxSpace | 219 | { searchSpace = toxSpace |
220 | , searchNodeAddress = nodeIP &&& nodePort | 220 | , searchNodeAddress = nodeIP &&& nodePort |
221 | , searchQuery = Left $ getRendezvous getTimeout crypto client | 221 | , searchQuery = Right $ asyncGetRendezvous getTimeout crypto client |
222 | , searchAlpha = 3 | 222 | , searchAlpha = 3 |
223 | , searchK = 6 | 223 | , searchK = 6 |
224 | } | 224 | } |
@@ -290,6 +290,30 @@ sendOnion getTimeout client req oaddr unwrap = | |||
290 | (return . Just . unwrap (onionNodeInfo oaddr)) | 290 | (return . Just . unwrap (onionNodeInfo oaddr)) |
291 | $ join mb | 291 | $ join mb |
292 | 292 | ||
293 | asyncOnion :: (TransactionId | ||
294 | -> OnionDestination r -> STM (OnionDestination r, Int)) | ||
295 | -> QR.Client | ||
296 | err | ||
297 | PacketKind | ||
298 | TransactionId | ||
299 | (OnionDestination r) | ||
300 | (OnionMessage Identity) | ||
301 | -> AnnounceRequest | ||
302 | -> OnionDestination r | ||
303 | -> (NodeInfo -> AnnounceResponse -> a) | ||
304 | -> (Maybe a -> IO ()) | ||
305 | -> IO () | ||
306 | asyncOnion getTimeout client req oaddr unwrap go = | ||
307 | -- Four tries and then we tap out. | ||
308 | flip fix 4 $ \loop n -> do | ||
309 | QR.asyncQuery client (announceSerializer getTimeout) req oaddr | ||
310 | $ \mb -> do | ||
311 | forM_ mb $ \r -> dput XAnnounce $ show (onionNodeInfo oaddr) ++ " sent response: " ++ show r | ||
312 | maybe (if n>0 then loop $! n - 1 else go Nothing) | ||
313 | (go . Just . unwrap (onionNodeInfo oaddr)) | ||
314 | $ join mb | ||
315 | |||
316 | |||
293 | -- | Lookup the secret counterpart for a given alias key. | 317 | -- | Lookup the secret counterpart for a given alias key. |
294 | getRendezvous :: (TransactionId -> OnionDestination r -> STM (OnionDestination r, Int)) | 318 | getRendezvous :: (TransactionId -> OnionDestination r -> STM (OnionDestination r, Int)) |
295 | -> TransportCrypto | 319 | -> TransportCrypto |
@@ -308,6 +332,26 @@ getRendezvous getTimeout crypto client nid ni = do | |||
308 | oaddr | 332 | oaddr |
309 | (unwrapAnnounceResponse rkey) | 333 | (unwrapAnnounceResponse rkey) |
310 | 334 | ||
335 | asyncGetRendezvous | ||
336 | :: (TransactionId -> OnionDestination r -> STM (OnionDestination r, Int)) | ||
337 | -> TransportCrypto | ||
338 | -> Client r | ||
339 | -> NodeId | ||
340 | -> NodeInfo | ||
341 | -> (Maybe ([NodeInfo], [Rendezvous], Maybe Nonce32) -> IO ()) | ||
342 | -> IO () | ||
343 | asyncGetRendezvous getTimeout crypto client nid ni go = do | ||
344 | asel <- atomically $ selectAlias crypto nid | ||
345 | let oaddr = OnionDestination asel ni Nothing | ||
346 | rkey = case asel of | ||
347 | SearchingAlias -> Nothing | ||
348 | _ -> Just $ key2id $ rendezvousPublic crypto | ||
349 | asyncOnion getTimeout client | ||
350 | (AnnounceRequest zeros32 nid $ fromMaybe zeroID rkey) | ||
351 | oaddr | ||
352 | (unwrapAnnounceResponse rkey) | ||
353 | go | ||
354 | |||
311 | putRendezvous :: (TransactionId -> OnionDestination r -> STM (OnionDestination r, Int)) | 355 | putRendezvous :: (TransactionId -> OnionDestination r -> STM (OnionDestination r, Int)) |
312 | -> TransportCrypto | 356 | -> TransportCrypto |
313 | -> Client r | 357 | -> Client r |