diff options
author | Joe Crayne <joe@jerkface.net> | 2020-01-25 03:11:44 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-25 19:18:12 -0500 |
commit | 782aada5511ce8bdf8ae63fee189e6c0c9481e1d (patch) | |
tree | de1d57fedc6cf9b311f319d7af2130439586753c | |
parent | 938ef8b447e975a39121104b4206cd149a2f911e (diff) |
getContactInfo: Send optional TCP connection requests.
-rw-r--r-- | dht/ToxManager.hs | 4 | ||||
-rw-r--r-- | dht/examples/dhtd.hs | 6 | ||||
-rw-r--r-- | dht/src/Network/Tox.hs | 12 | ||||
-rw-r--r-- | dht/src/Network/Tox/ContactInfo.hs | 23 |
4 files changed, 32 insertions, 13 deletions
diff --git a/dht/ToxManager.hs b/dht/ToxManager.hs index c1112a05..19db77ec 100644 --- a/dht/ToxManager.hs +++ b/dht/ToxManager.hs | |||
@@ -646,7 +646,9 @@ startConnecting0 tx them contact reason = do | |||
646 | let meth = SearchMethod (toxQSearch tox) onResult (nearNodes tox) (key2id them) 30 | 646 | let meth = SearchMethod (toxQSearch tox) onResult (nearNodes tox) (key2id them) 30 |
647 | where | 647 | where |
648 | onResult theirkey rendezvous = do | 648 | onResult theirkey rendezvous = do |
649 | dkey <- Tox.getContactInfo tox | 649 | mTheirDHTKey <- atomically $ fmap ((,) (txTCP tx)) <$> contactDHTKey contact |
650 | dkey <- Tox.getContactInfo mTheirDHTKey tox | ||
651 | |||
650 | let tr = Tox.toxToRoute tox | 652 | let tr = Tox.toxToRoute tox |
651 | route = Tox.AnnouncedRendezvous theirkey rendezvous | 653 | route = Tox.AnnouncedRendezvous theirkey rendezvous |
652 | dput XMan $ unwords [ take 8 (show $ key2id theirkey) | 654 | dput XMan $ unwords [ take 8 (show $ key2id theirkey) |
diff --git a/dht/examples/dhtd.hs b/dht/examples/dhtd.hs index 0dcb4237..eb31543a 100644 --- a/dht/examples/dhtd.hs +++ b/dht/examples/dhtd.hs | |||
@@ -1596,7 +1596,11 @@ initTox runio opts ssvar keysdb mbxmpp invc = case porttox opts of | |||
1596 | -- | 1596 | -- |
1597 | -- > a +dhtkey KWoEx1XQHrluIoW.3nK6BFb6XCebKWr3nDDt3V7CcoJ | 1597 | -- > a +dhtkey KWoEx1XQHrluIoW.3nK6BFb6XCebKWr3nDDt3V7CcoJ |
1598 | , ("dhtkey", DHTAnnouncable { announceSendData = Left ("toxid", readEither, \me them addr -> do | 1598 | , ("dhtkey", DHTAnnouncable { announceSendData = Left ("toxid", readEither, \me them addr -> do |
1599 | dkey <- Tox.getContactInfo tox | 1599 | -- let mthem = Just ( TCP.tcpClient |
1600 | -- $ tcpKademliaClient | ||
1601 | -- $ toxOnionRoutes tox | ||
1602 | -- , them ) | ||
1603 | dkey <- Tox.getContactInfo Nothing tox | ||
1600 | sendMessage | 1604 | sendMessage |
1601 | (Tox.toxToRoute tox) | 1605 | (Tox.toxToRoute tox) |
1602 | (Tox.AnnouncedRendezvous them addr) | 1606 | (Tox.AnnouncedRendezvous them addr) |
diff --git a/dht/src/Network/Tox.hs b/dht/src/Network/Tox.hs index 0a6cccaa..6b39d57a 100644 --- a/dht/src/Network/Tox.hs +++ b/dht/src/Network/Tox.hs | |||
@@ -73,7 +73,7 @@ import qualified Network.Tox.Onion.Transport as Onion | |||
73 | import Network.Tox.RelayPinger | 73 | import Network.Tox.RelayPinger |
74 | import System.Global6 | 74 | import System.Global6 |
75 | import Network.Tox.Transport | 75 | import Network.Tox.Transport |
76 | import Network.Tox.TCP (tcpClient, ViaRelay(..)) | 76 | import Network.Tox.TCP (tcpClient, ViaRelay(..), RelayClient) |
77 | import Network.Tox.Onion.Routes | 77 | import Network.Tox.Onion.Routes |
78 | import Network.Tox.ContactInfo | 78 | import Network.Tox.ContactInfo |
79 | import Text.XXD | 79 | import Text.XXD |
@@ -214,8 +214,8 @@ data Tox extra = Tox | |||
214 | 214 | ||
215 | 215 | ||
216 | -- | Create a DHTPublicKey packet to send to a remote contact. | 216 | -- | Create a DHTPublicKey packet to send to a remote contact. |
217 | getContactInfo :: Tox extra -> IO DHT.DHTPublicKey | 217 | getContactInfo :: Maybe (RelayClient,PublicKey) -> Tox extra -> IO DHT.DHTPublicKey |
218 | getContactInfo Tox{toxCryptoKeys,toxRouting,toxOnionRoutes} = join $ atomically $ do | 218 | getContactInfo mthem Tox{toxCryptoKeys,toxRouting,toxOnionRoutes} = join $ atomically $ do |
219 | (rcnt,relays) <- currentRelays (tcpRelayPinger toxOnionRoutes) | 219 | (rcnt,relays) <- currentRelays (tcpRelayPinger toxOnionRoutes) |
220 | r4 <- readTVar $ DHT.routing4 toxRouting | 220 | r4 <- readTVar $ DHT.routing4 toxRouting |
221 | r6 <- readTVar $ DHT.routing6 toxRouting | 221 | r6 <- readTVar $ DHT.routing6 toxRouting |
@@ -227,12 +227,16 @@ getContactInfo Tox{toxCryptoKeys,toxRouting,toxOnionRoutes} = join $ atomically | |||
227 | n6s = R.kclosest DHT.toxSpace 4 self r6 | 227 | n6s = R.kclosest DHT.toxSpace 4 self r6 |
228 | ns = filter (DHT.isGlobal . nodeIP) [n4,n6] | 228 | ns = filter (DHT.isGlobal . nodeIP) [n4,n6] |
229 | ++ concat (zipWith (\a b -> [a,b]) n4s n6s) | 229 | ++ concat (zipWith (\a b -> [a,b]) n4s n6s) |
230 | sending_ns = take 4 $ relays ++ map TCP.fromUDPNode ns | ||
230 | return $ do | 231 | return $ do |
232 | forM_ mthem $ \(tcp,theirDHTKey) -> | ||
233 | forM_ (filter (\n -> TCP.tcpPort n /= 0) sending_ns) $ \ni -> do | ||
234 | Multi.tcpConnectionRequest tcp theirDHTKey ni | ||
231 | timestamp <- round . (* 1000000) <$> getPOSIXTime | 235 | timestamp <- round . (* 1000000) <$> getPOSIXTime |
232 | return DHT.DHTPublicKey | 236 | return DHT.DHTPublicKey |
233 | { dhtpkNonce = timestamp | 237 | { dhtpkNonce = timestamp |
234 | , dhtpk = id2key self | 238 | , dhtpk = id2key self |
235 | , dhtpkNodes = DHT.SendNodes $ take 4 $ relays ++ map TCP.fromUDPNode ns | 239 | , dhtpkNodes = DHT.SendNodes sending_ns |
236 | } | 240 | } |
237 | 241 | ||
238 | isLocalHost :: SockAddr -> Bool | 242 | isLocalHost :: SockAddr -> Bool |
diff --git a/dht/src/Network/Tox/ContactInfo.hs b/dht/src/Network/Tox/ContactInfo.hs index d5640ce8..3cf5bb2b 100644 --- a/dht/src/Network/Tox/ContactInfo.hs +++ b/dht/src/Network/Tox/ContactInfo.hs | |||
@@ -2,20 +2,23 @@ | |||
2 | {-# LANGUAGE LambdaCase #-} | 2 | {-# LANGUAGE LambdaCase #-} |
3 | module Network.Tox.ContactInfo where | 3 | module Network.Tox.ContactInfo where |
4 | 4 | ||
5 | import Connection | 5 | import Control.Arrow |
6 | |||
7 | import Data.Time.Clock.POSIX | ||
8 | import Control.Concurrent.STM | 6 | import Control.Concurrent.STM |
9 | import Control.Monad | 7 | import Control.Monad |
10 | import Crypto.PubKey.Curve25519 | 8 | import Crypto.PubKey.Curve25519 |
11 | import qualified Data.HashMap.Strict as HashMap | 9 | import qualified Data.HashMap.Strict as HashMap |
12 | ;import Data.HashMap.Strict (HashMap) | 10 | ;import Data.HashMap.Strict (HashMap) |
11 | import Data.List | ||
13 | import Data.Maybe | 12 | import Data.Maybe |
14 | import Network.Tox.DHT.Transport as DHT | 13 | import Data.Ord |
15 | import Network.Tox.NodeId (id2key) | 14 | import Data.Time.Clock.POSIX |
16 | import Network.Tox.Onion.Transport as Onion | 15 | |
17 | import DPut | 16 | import Connection |
18 | import DebugTag | 17 | import DebugTag |
18 | import DPut | ||
19 | import Network.Tox.DHT.Transport as DHT | ||
20 | import Network.Tox.NodeId (id2key) | ||
21 | import Network.Tox.Onion.Transport as Onion | ||
19 | 22 | ||
20 | newtype ContactInfo extra = ContactInfo | 23 | newtype ContactInfo extra = ContactInfo |
21 | { | 24 | { |
@@ -43,6 +46,12 @@ data Contact = Contact | |||
43 | , contactPolicy :: TVar (Maybe Connection.Policy) | 46 | , contactPolicy :: TVar (Maybe Connection.Policy) |
44 | } | 47 | } |
45 | 48 | ||
49 | contactDHTKey :: Contact -> STM (Maybe PublicKey) | ||
50 | contactDHTKey c = do | ||
51 | mkeypkt <- fmap (second dhtpk) <$> readTVar (contactKeyPacket c) | ||
52 | mseen <- fmap (second $ id2key . nodeId) <$> readTVar (contactLastSeenAddr c) | ||
53 | return $ fmap snd $ listToMaybe $ sortOn (Down . fst) $ catMaybes [mkeypkt,mseen] | ||
54 | |||
46 | newContactInfo :: IO (ContactInfo extra) | 55 | newContactInfo :: IO (ContactInfo extra) |
47 | newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty | 56 | newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty |
48 | 57 | ||