summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2020-01-17 17:40:51 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-17 17:41:07 -0500
commitebacfa8f8cbd8b5cc3a1995188cc2cc8c260d76e (patch)
treeee1da2e7110edbda15dff6356ab693bd50ccc519
parentdaeaa4fd91bce1626d126628a2f04d7653c8069a (diff)
Improved TCP-mediated handshake protocol.
-rw-r--r--dht/ToxManager.hs28
-rw-r--r--dht/src/Network/Tox/TCP.hs5
2 files changed, 16 insertions, 17 deletions
diff --git a/dht/ToxManager.hs b/dht/ToxManager.hs
index 126efa36..63cb3967 100644
--- a/dht/ToxManager.hs
+++ b/dht/ToxManager.hs
@@ -297,25 +297,24 @@ connectViaRelay tx theirKey theirDhtKey ann tkey now = do
297 established <- activeSesh tx theirKey 297 established <- activeSesh tx theirKey
298 runAction ann "connectViaRelay-print" $ do 298 runAction ann "connectViaRelay-print" $ do
299 dput XMan $ "connectViaRelay("++unpackAnnounceKey ann tkey++") established=" ++ show established 299 dput XMan $ "connectViaRelay("++unpackAnnounceKey ann tkey++") established=" ++ show established
300 return $ when (not established) go 300 let Tox.SendNodes ns = Tox.dhtpkNodes theirDhtKey
301 do
302 -- Schedule a retry in 5 seconds.
303 let theirDhtKey' = theirDhtKey { Tox.dhtpkNodes = Tox.SendNodes (cycled ns) }
304 scheduleRel ann tkey (ScheduledItem $ connectViaRelay tx theirKey theirDhtKey') 5
305 return $ when (not established) (go ns)
301 -- $ scheduleImmediately ann tkey $ ScheduledItem go 306 -- $ scheduleImmediately ann tkey $ ScheduledItem go
302 -- return $ return () 307 -- return $ return ()
303 where 308 where
304 myPublicKey = toPublic $ userSecret (txAccount tx) 309 myPublicKey = toPublic $ userSecret (txAccount tx)
305 me = key2id myPublicKey 310 me = key2id myPublicKey
306 tkey = akeyConnectTCP (txAnnouncer tx) me theirKey 311 tkey = akeyConnectTCP (txAnnouncer tx) me theirKey
307 go = do 312 go ns = do
308 let Tox.SendNodes ns = Tox.dhtpkNodes theirDhtKey 313 mapM_ (TCP.sendConnectionRequest (txTCP tx) $ Tox.dhtpk theirDhtKey)
309 mcons <- forM (filter (\n -> TCP.tcpPort n /= 0) ns) $ \ni -> do 314 (filter ((/= 0) . TCP.tcpPort) ns)
310 mcon <- Multi.tcpConnectionRequest (txTCP tx) (Tox.dhtpk theirDhtKey) ni
311 return mcon
312 let oobs = [ Multi.TCP ==> TCP.ViaRelay Nothing (Tox.key2id $ Tox.dhtpk theirDhtKey) ni | ni <- ns ] 315 let oobs = [ Multi.TCP ==> TCP.ViaRelay Nothing (Tox.key2id $ Tox.dhtpk theirDhtKey) ni | ni <- ns ]
313 addrs = catMaybes mcons ++ oobs 316 dput XMan $ "connectViaRelay: address count is " ++ show (length oobs)
314 -- wait a sec to give connection requests a chance to be processed. 317 forM_ oobs $ \ni -> do
315 -- probably we should be handling ConnectNotification/DisconnectNotification
316 (\kont -> atomically $ scheduleRel ann tkey (ScheduledItem $ \_ _ _ -> return kont) 1) $ do
317 dput XMan $ "connectViaRelay: address count is " ++ show (length addrs)
318 forM_ addrs $ \ni -> do
319 cookieRequest (toxCryptoKeys $ txTox tx) (toxDHT $ txTox tx) myPublicKey ni >>= \case 318 cookieRequest (toxCryptoKeys $ txTox tx) (toxDHT $ txTox tx) myPublicKey ni >>= \case
320 Nothing -> dput XMan $ "connectViaRelay: no cookie from " ++ show ni 319 Nothing -> dput XMan $ "connectViaRelay: no cookie from " ++ show ni
321 Just cookie -> do 320 Just cookie -> do
@@ -325,11 +324,6 @@ connectViaRelay tx theirKey theirDhtKey ann tkey now = do
325 hs <- cacheHandshake (toxHandshakeCache $ txTox tx) (userSecret (txAccount tx)) theirKey ni cookie 324 hs <- cacheHandshake (toxHandshakeCache $ txTox tx) (userSecret (txAccount tx)) theirKey ni cookie
326 dput XMan $ show their_nid ++ "<-- handshake " ++ show (key2id theirKey) ++ " (TCP)" 325 dput XMan $ show their_nid ++ "<-- handshake " ++ show (key2id theirKey) ++ " (TCP)"
327 sendMessage (toxHandshakes $ txTox tx) (Multi.sessionAddr ni) hs 326 sendMessage (toxHandshakes $ txTox tx) (Multi.sessionAddr ni) hs
328 dput XMan $ "connectViaRelay trying again in 5 seconds ... "
329 atomically $ do
330 -- Try again in 5 seconds.
331 let theirDhtKey' = theirDhtKey { Tox.dhtpkNodes = Tox.SendNodes (cycled ns) }
332 scheduleRel ann tkey (ScheduledItem $ connectViaRelay tx theirKey theirDhtKey') 5
333 327
334gotDhtPubkey :: Tox.DHTPublicKey -> ToxToXMPP -> PublicKey -> IO () 328gotDhtPubkey :: Tox.DHTPublicKey -> ToxToXMPP -> PublicKey -> IO ()
335gotDhtPubkey theirDhtKey tx theirKey = do 329gotDhtPubkey theirDhtKey tx theirKey = do
diff --git a/dht/src/Network/Tox/TCP.hs b/dht/src/Network/Tox/TCP.hs
index ebb15e3b..27a0f3d6 100644
--- a/dht/src/Network/Tox/TCP.hs
+++ b/dht/src/Network/Tox/TCP.hs
@@ -333,6 +333,11 @@ tcpPing client dst = do
333 , method = PingPacket 333 , method = PingPacket
334 } 334 }
335 335
336sendConnectionRequest :: Client err PacketNumber tid addr (Bool, RelayPacket)
337 -> PublicKey -> addr -> IO ()
338sendConnectionRequest client pubkey ni =
339 sendMessage (clientNet client) ni (True,RoutingRequest pubkey)
340
336tcpConnectionRequest_ :: Client err PacketNumber tid addr (Bool, RelayPacket) 341tcpConnectionRequest_ :: Client err PacketNumber tid addr (Bool, RelayPacket)
337 -> PublicKey -> addr -> IO (Maybe ConId) 342 -> PublicKey -> addr -> IO (Maybe ConId)
338tcpConnectionRequest_ client pubkey ni = do 343tcpConnectionRequest_ client pubkey ni = do