summaryrefslogtreecommitdiff
path: root/dht/ToxManager.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-12-17 13:22:28 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-01 23:26:50 -0500
commit2425c1a5df23d7051461d8f9fd32b5d5aa03e104 (patch)
treefad9dc999a824ed7fd951241768682fdeffda04a /dht/ToxManager.hs
parent0c9cf41d9cf8c0b908f38a3ccf66452d56c578e8 (diff)
Schedule TCP chat-link session attempts.
Diffstat (limited to 'dht/ToxManager.hs')
-rw-r--r--dht/ToxManager.hs35
1 files changed, 27 insertions, 8 deletions
diff --git a/dht/ToxManager.hs b/dht/ToxManager.hs
index 00e7146b..c4440409 100644
--- a/dht/ToxManager.hs
+++ b/dht/ToxManager.hs
@@ -277,6 +277,11 @@ nodeinfoStaleTime = 600 -- consider DHT node address stale after 10 minutes
277nodeinfoSearchInterval :: POSIXTime 277nodeinfoSearchInterval :: POSIXTime
278nodeinfoSearchInterval = 15 -- when no address, search DHT node every 15 seconds 278nodeinfoSearchInterval = 15 -- when no address, search DHT node every 15 seconds
279 279
280
281cycled :: [x] -> [x]
282cycled [] = []
283cycled (x:xs) = xs ++ [x]
284
280connectViaRelay :: ToxToXMPP -> PublicKey -> Tox.DHTPublicKey 285connectViaRelay :: ToxToXMPP -> PublicKey -> Tox.DHTPublicKey
281 -> Announcer -> AnnounceKey -> POSIXTime -> STM (IO ()) 286 -> Announcer -> AnnounceKey -> POSIXTime -> STM (IO ())
282connectViaRelay tx theirKey theirDhtKey ann tkey now = do 287connectViaRelay tx theirKey theirDhtKey ann tkey now = do
@@ -287,15 +292,28 @@ connectViaRelay tx theirKey theirDhtKey ann tkey now = do
287 established <- activeSesh tx theirKey 292 established <- activeSesh tx theirKey
288 return $ when (not established) go 293 return $ when (not established) go
289 where 294 where
295 myPublicKey = toPublic $ userSecret (txAccount tx)
296 me = key2id myPublicKey
297 tkey = akeyConnectTCP (txAnnouncer tx) me theirKey
290 go = do 298 go = do
291 let Tox.SendNodes ns = Tox.dhtpkNodes theirDhtKey 299 let Tox.SendNodes ns = Tox.dhtpkNodes theirDhtKey
292 mcon <- foldr (\action next -> action >>= maybe next (return . Just)) 300 mcon <- foldr (\action next -> action >>= maybe next (return . Just))
293 (return Nothing) 301 (return Nothing)
294 $ map (\ni -> fmap ((,) ni) <$> TCP.tcpConnectionRequest (txTCP tx) theirKey ni) ns 302 $ map (Multi.tcpConnectionRequest (txTCP tx) theirKey) ns
295 forM_ mcon $ \(con,ni) -> do 303 forM_ mcon $ \ni -> do
296 return () 304 cookieRequest (toxCryptoKeys $ txTox tx) (toxDHT $ txTox tx) myPublicKey ni >>= \case
297 -- TODO: try connect tcp relays 305 Nothing -> return ()
298 -- TODO: cookie;handshake 306 Just cookie -> do
307 cookieCreationStamp <- getPOSIXTime
308 let their_nid = key2id $ dhtpk theirDhtKey
309 dput XNetCrypto $ show their_nid ++ " --> cookie (TCP)"
310 hs <- cacheHandshake (toxHandshakeCache $ txTox tx) (userSecret (txAccount tx)) theirKey ni cookie
311 dput XNetCrypto $ show their_nid ++ "<-- handshake " ++ show (key2id theirKey) ++ " (TCP)"
312 sendMessage (toxHandshakes $ txTox tx) (Multi.sessionAddr ni) hs
313 atomically $ do
314 -- Try again in 5 seconds.
315 let theirDhtKey' = theirDhtKey' { Tox.dhtpkNodes = Tox.SendNodes (cycled ns) }
316 scheduleRel ann tkey (ScheduledItem $ connectViaRelay tx theirKey theirDhtKey') 5
299 317
300gotDhtPubkey :: Tox.DHTPublicKey -> ToxToXMPP -> PublicKey -> IO () 318gotDhtPubkey :: Tox.DHTPublicKey -> ToxToXMPP -> PublicKey -> IO ()
301gotDhtPubkey theirDhtKey tx theirKey = do 319gotDhtPubkey theirDhtKey tx theirKey = do
@@ -318,7 +336,7 @@ gotDhtPubkey theirDhtKey tx theirKey = do
318 tkey = akeyConnectTCP (txAnnouncer tx) me theirKey 336 tkey = akeyConnectTCP (txAnnouncer tx) me theirKey
319 atomically $ registerNodeCallback (toxRouting tox) (nic akey) 337 atomically $ registerNodeCallback (toxRouting tox) (nic akey)
320 scheduleSearch (txAnnouncer tx) akey meth theirDhtKey 338 scheduleSearch (txAnnouncer tx) akey meth theirDhtKey
321 -- TODO atomically $ scheduleImmediately (txAnnouncer tx) tkey $ ScheduledItem $ connectViaRelay tx theirKey theirDhtKey 339 atomically $ scheduleImmediately (txAnnouncer tx) tkey $ ScheduledItem $ connectViaRelay tx theirKey theirDhtKey
322 340
323 target :: NodeId 341 target :: NodeId
324 target = key2id $ dhtpk theirDhtKey 342 target = key2id $ dhtpk theirDhtKey
@@ -394,6 +412,9 @@ activeSesh tx theirKey = do
394 Just False -> return False 412 Just False -> return False
395 _ -> (== Established) <$> aggregateStatus c 413 _ -> (== Established) <$> aggregateStatus c
396 414
415cookieMaxAge :: POSIXTime
416cookieMaxAge = 60 * 5
417
397getCookie 418getCookie
398 :: ToxToXMPP 419 :: ToxToXMPP
399 -> PublicKey 420 -> PublicKey
@@ -428,8 +449,6 @@ getCookie tx theirKey theirDhtKey ni isActive getC ann akey now = getCookieAgain
428 reschedule n f = scheduleRel ann akey f n 449 reschedule n f = scheduleRel ann akey f n
429 reschedule' n f = reschedule n (ScheduledItem $ \_ _ now -> f now) 450 reschedule' n f = reschedule n (ScheduledItem $ \_ _ now -> f now)
430 451
431 cookieMaxAge = 60 * 5
432
433 getCookieIO :: IO () 452 getCookieIO :: IO ()
434 getCookieIO = do 453 getCookieIO = do
435 dput XNetCrypto $ show addr ++ " <-- request cookie" 454 dput XNetCrypto $ show addr ++ " <-- request cookie"