summaryrefslogtreecommitdiff
path: root/dht/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'dht/src/Network')
-rw-r--r--dht/src/Network/Tox.hs12
-rw-r--r--dht/src/Network/Tox/ContactInfo.hs23
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
73import Network.Tox.RelayPinger 73import Network.Tox.RelayPinger
74import System.Global6 74import System.Global6
75import Network.Tox.Transport 75import Network.Tox.Transport
76import Network.Tox.TCP (tcpClient, ViaRelay(..)) 76import Network.Tox.TCP (tcpClient, ViaRelay(..), RelayClient)
77import Network.Tox.Onion.Routes 77import Network.Tox.Onion.Routes
78import Network.Tox.ContactInfo 78import Network.Tox.ContactInfo
79import Text.XXD 79import 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.
217getContactInfo :: Tox extra -> IO DHT.DHTPublicKey 217getContactInfo :: Maybe (RelayClient,PublicKey) -> Tox extra -> IO DHT.DHTPublicKey
218getContactInfo Tox{toxCryptoKeys,toxRouting,toxOnionRoutes} = join $ atomically $ do 218getContactInfo 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
238isLocalHost :: SockAddr -> Bool 242isLocalHost :: 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 #-}
3module Network.Tox.ContactInfo where 3module Network.Tox.ContactInfo where
4 4
5import Connection 5import Control.Arrow
6
7import Data.Time.Clock.POSIX
8import Control.Concurrent.STM 6import Control.Concurrent.STM
9import Control.Monad 7import Control.Monad
10import Crypto.PubKey.Curve25519 8import Crypto.PubKey.Curve25519
11import qualified Data.HashMap.Strict as HashMap 9import qualified Data.HashMap.Strict as HashMap
12 ;import Data.HashMap.Strict (HashMap) 10 ;import Data.HashMap.Strict (HashMap)
11import Data.List
13import Data.Maybe 12import Data.Maybe
14import Network.Tox.DHT.Transport as DHT 13import Data.Ord
15import Network.Tox.NodeId (id2key) 14import Data.Time.Clock.POSIX
16import Network.Tox.Onion.Transport as Onion 15
17import DPut 16import Connection
18import DebugTag 17import DebugTag
18import DPut
19import Network.Tox.DHT.Transport as DHT
20import Network.Tox.NodeId (id2key)
21import Network.Tox.Onion.Transport as Onion
19 22
20newtype ContactInfo extra = ContactInfo 23newtype 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
49contactDHTKey :: Contact -> STM (Maybe PublicKey)
50contactDHTKey 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
46newContactInfo :: IO (ContactInfo extra) 55newContactInfo :: IO (ContactInfo extra)
47newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty 56newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty
48 57