summaryrefslogtreecommitdiff
path: root/dht/HandshakeCache.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-12-14 16:11:03 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-01 23:26:49 -0500
commitd5efdc327bbb69a905043df45415817e318e38ee (patch)
tree7be975048f3e40c27811bdb39ba92d871a42588c /dht/HandshakeCache.hs
parent8c04d9cca70241bebe4b94b779fe7bbfe6140f51 (diff)
Multi Transports: TCP for DHT/Cookies/Handshakes.
Diffstat (limited to 'dht/HandshakeCache.hs')
-rw-r--r--dht/HandshakeCache.hs13
1 files changed, 7 insertions, 6 deletions
diff --git a/dht/HandshakeCache.hs b/dht/HandshakeCache.hs
index 91f5faaf..d9ffacab 100644
--- a/dht/HandshakeCache.hs
+++ b/dht/HandshakeCache.hs
@@ -14,6 +14,7 @@ import Crypto.Hash
14import Crypto.Tox 14import Crypto.Tox
15import qualified Data.MinMaxPSQ as MM 15import qualified Data.MinMaxPSQ as MM
16 ;import Data.MinMaxPSQ (MinMaxPSQ') 16 ;import Data.MinMaxPSQ (MinMaxPSQ')
17import qualified Data.Tox.DHT.Multi as Multi
17import DPut 18import DPut
18import DebugTag 19import DebugTag
19import Network.Tox.Crypto.Transport (Handshake, HandshakeData (..)) 20import Network.Tox.Crypto.Transport (Handshake, HandshakeData (..))
@@ -26,13 +27,13 @@ data HandshakeCache = HandshakeCache
26 { -- Note that currently we are storing sent handshakes keyed by the 27 { -- Note that currently we are storing sent handshakes keyed by the
27 -- locally issued cookie nonce. 28 -- locally issued cookie nonce.
28 hscTable :: TVar (MinMaxPSQ' Nonce24 POSIXTime (SecretKey,HandshakeData)) 29 hscTable :: TVar (MinMaxPSQ' Nonce24 POSIXTime (SecretKey,HandshakeData))
29 , hscSend :: SockAddr -> Handshake Encrypted -> IO () 30 , hscSend :: Multi.SessionAddress -> Handshake Encrypted -> IO ()
30 , hscCrypto :: TransportCrypto 31 , hscCrypto :: TransportCrypto
31 , hscPendingCookies :: TVar (Map (PublicKey,PublicKey) ()) 32 , hscPendingCookies :: TVar (Map (PublicKey,PublicKey) ())
32 } 33 }
33 34
34 35
35newHandshakeCache :: TransportCrypto -> (SockAddr -> Handshake Encrypted -> IO ()) -> IO HandshakeCache 36newHandshakeCache :: TransportCrypto -> (Multi.SessionAddress -> Handshake Encrypted -> IO ()) -> IO HandshakeCache
36newHandshakeCache crypto send = atomically $ do 37newHandshakeCache crypto send = atomically $ do
37 tbl <- newTVar MM.empty 38 tbl <- newTVar MM.empty
38 pcs <- newTVar Map.empty 39 pcs <- newTVar Map.empty
@@ -45,7 +46,7 @@ newHandshakeCache crypto send = atomically $ do
45 46
46getSentHandshake :: HandshakeCache 47getSentHandshake :: HandshakeCache
47 -> SecretKey 48 -> SecretKey
48 -> SockAddr 49 -> Multi.SessionAddress
49 -> Cookie Identity -- locally issued 50 -> Cookie Identity -- locally issued
50 -> Cookie Encrypted -- remotely issued 51 -> Cookie Encrypted -- remotely issued
51 -> IO (Maybe (SecretKey, HandshakeData)) 52 -> IO (Maybe (SecretKey, HandshakeData))
@@ -57,7 +58,7 @@ getSentHandshake hscache me their_addr (Cookie n24 (Identity cd)) ecookie = do
57 Just s -> return $ return $ Just s 58 Just s -> return $ return $ Just s
58 Nothing -> do 59 Nothing -> do
59 let them = longTermKey cd 60 let them = longTermKey cd
60 case nodeInfo (key2id $ dhtKey cd) their_addr of 61 case Multi.nodeInfo (key2id $ dhtKey cd) their_addr of
61 Left _ -> return $ return Nothing -- Non-internet address. 62 Left _ -> return $ return Nothing -- Non-internet address.
62 Right their_node -> do 63 Right their_node -> do
63 (s,hs) <- cacheHandshakeSTM hscache me them their_node ecookie now 64 (s,hs) <- cacheHandshakeSTM hscache me them their_node ecookie now
@@ -83,7 +84,7 @@ hashCookie (Cookie n24 encrypted)
83cacheHandshakeSTM :: HandshakeCache 84cacheHandshakeSTM :: HandshakeCache
84 -> SecretKey -- ^ my ToxID key 85 -> SecretKey -- ^ my ToxID key
85 -> PublicKey -- ^ them 86 -> PublicKey -- ^ them
86 -> NodeInfo -- ^ their DHT node 87 -> Multi.NodeInfo -- ^ their DHT node
87 -> Cookie Encrypted -- ^ issued to me by them 88 -> Cookie Encrypted -- ^ issued to me by them
88 -> POSIXTime -- ^ current time 89 -> POSIXTime -- ^ current time
89 -> STM ((SecretKey,HandshakeData), Handshake Encrypted) 90 -> STM ((SecretKey,HandshakeData), Handshake Encrypted)
@@ -105,7 +106,7 @@ cacheHandshakeSTM hscache me them their_node ecookie timestamp = do
105cacheHandshake :: HandshakeCache 106cacheHandshake :: HandshakeCache
106 -> SecretKey 107 -> SecretKey
107 -> PublicKey 108 -> PublicKey
108 -> NodeInfo 109 -> Multi.NodeInfo
109 -> Cookie Encrypted 110 -> Cookie Encrypted
110 -> IO (Handshake Encrypted) 111 -> IO (Handshake Encrypted)
111cacheHandshake hscache me them their_node ecookie = do 112cacheHandshake hscache me them their_node ecookie = do