diff options
Diffstat (limited to 'dht/src/Network')
-rw-r--r-- | dht/src/Network/Tox.hs | 12 | ||||
-rw-r--r-- | dht/src/Network/Tox/ContactInfo.hs | 23 |
2 files changed, 24 insertions, 11 deletions
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 | ||