summaryrefslogtreecommitdiff
path: root/Connection
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-16 02:59:42 -0400
committerjoe <joe@jerkface.net>2018-06-16 02:59:42 -0400
commit8d6a50dde334a2dc4039c0c718a992e4df36402f (patch)
tree58558561314a49a29ea6a0ab82a6a4cb9550cf81 /Connection
parent939c23d57365a49d366b6534c6f343f0a12a770a (diff)
more persueContact wip
Diffstat (limited to 'Connection')
-rw-r--r--Connection/Tox/Threads.hs85
1 files changed, 12 insertions, 73 deletions
diff --git a/Connection/Tox/Threads.hs b/Connection/Tox/Threads.hs
index 12ac9682..3cf9486d 100644
--- a/Connection/Tox/Threads.hs
+++ b/Connection/Tox/Threads.hs
@@ -13,12 +13,14 @@ module Connection.Tox.Threads where
13 13
14import Connection 14import Connection
15-- import Connection.Tox 15-- import Connection.Tox
16import Crypto.Tox
16import Data.IP (IP) 17import Data.IP (IP)
17import Network.Tox.Crypto.Transport 18import Network.Tox.Crypto.Transport
18import Network.Tox.Crypto.Handlers 19import Network.Tox.Crypto.Handlers
19import Network.Tox.NodeId 20import Network.Tox.NodeId
20import Network.Tox.ContactInfo 21import Network.Tox.ContactInfo
21import Network.Tox.DHT.Handlers {- (nodeSearch) -} as DHT 22import Network.Tox.DHT.Handlers {- (nodeSearch) -} as DHT
23import Network.Tox.DHT.Transport as DHT (dhtpk)
22import Network.Socket 24import Network.Socket
23import Network.Kademlia.Search 25import Network.Kademlia.Search
24import Network.Kademlia.Routing (BucketList) 26import Network.Kademlia.Routing (BucketList)
@@ -30,68 +32,6 @@ import Data.Functor.Identity
30import System.Timeout 32import System.Timeout
31 33
32 34
33-- | This type indicates the progress of a tox encrypted friend link
34-- connection. Two scenarios are illustrated below. The parenthesis show the
35-- current 'G.Status' 'ToxProgress' of the session.
36--
37--
38-- Perfect handshake scenario:
39--
40-- Peer 1 Peer 2
41-- (InProgress AcquiringCookie) (Dormant/InProgress AcquiringCookie)
42-- Cookie request ->
43-- <- Cookie response
44-- (InProgress AwaitingHandshake) (Dormant/InProgress AcquiringCookie)
45-- Handshake packet ->
46-- * accepts connection
47-- (InProgress AwaitingSessionPacket)
48-- <- Handshake packet
49-- *accepts connection
50-- (InProgress AwaitingSessionPacket)
51-- Encrypted packet -> <- Encrypted packet
52-- *confirms connection *confirms connection
53-- (Established) (Established)
54--
55-- Connection successful.
56--
57-- Encrypted packets -> <- Encrypted packets
58--
59--
60--
61--
62-- More realistic handshake scenario:
63-- Peer 1 Peer 2
64-- (InProgress AcquiringCookie) (Dormant/InProgress AcquiringCookie)
65-- Cookie request -> *packet lost*
66-- Cookie request ->
67-- <- Cookie response
68-- (InProgress AwaitingHandshake) (Dormant/InProgress AcquiringCookie)
69--
70-- *Peer 2 randomly starts new connection to peer 1
71-- (InProgress AcquiringCookie)
72-- <- Cookie request
73-- Cookie response ->
74-- (InProgress AwaitingHandshake)
75--
76-- Handshake packet -> <- Handshake packet
77-- *accepts connection * accepts connection
78-- (InProgress AwaitingSessionPacket) (InProgress AwaitingSessionPacket)
79--
80-- Encrypted packet -> <- Encrypted packet
81-- *confirms connection *confirms connection
82-- (Established) (Established)
83--
84-- Connection successful.
85--
86-- Encrypted packets -> <- Encrypted packets
87data ToxProgress
88 = AwaitingDHTKey -- ^ Waiting to receive their DHT key.
89 | AcquiringIPAddress -- ^ Searching DHT to obtain their node's IP & port.
90 | AcquiringCookie -- ^ Attempting to obtain a cookie.
91 | AwaitingHandshake -- ^ Waiting to receive a handshake.
92 | AwaitingSessionPacket -- ^ Connection is "accepted" but not yet "confirmed".
93 deriving (Eq,Ord,Enum,Show)
94
95 35
96type NodeSearch = Search NodeId (IP,PortNumber) () NodeInfo NodeInfo 36type NodeSearch = Search NodeId (IP,PortNumber) () NodeInfo NodeInfo
97 37
@@ -150,7 +90,8 @@ data PersueContactMethods params = PersueContactMethods
150 , myseckey :: SecretKey 90 , myseckey :: SecretKey
151 , theirpubkey :: PublicKey 91 , theirpubkey :: PublicKey
152 , client :: DHT.Client 92 , client :: DHT.Client
153 , retryInterval :: Int 93 , shortRetryInterval :: Int -- successful cookie, try again soon.
94 , longRetryInterval :: Int -- no cookie, he's offline, give it some time.
154 , contact :: Contact 95 , contact :: Contact
155 } 96 }
156 97
@@ -185,19 +126,16 @@ persueContact getPolicy getStatus PersueContactMethods{..} writeStatus
185 either retry return $ nodeInfo (key2id theirDhtKey) saddr 126 either retry return $ nodeInfo (key2id theirDhtKey) saddr
186 let mykeyAsId = key2id (toPublic myseckey) 127 let mykeyAsId = key2id (toPublic myseckey)
187 theirkeyAsId = key2id theirpubkey 128 theirkeyAsId = key2id theirpubkey
129 crypto = transportCrypto allsessions
130 -- AcquiringCookie
188 atomically $ writeStatus (InProgress AcquiringCookie) 131 atomically $ writeStatus (InProgress AcquiringCookie)
189 -- if no session: 132 mbCookie <- -- TODO: Check for recent cached cookie.
190 -- Convert to NodeInfo, so we can send cookieRequest 133 DHT.cookieRequest crypto client (toPublic myseckey) ni
191 let crypto = transportCrypto allsessions 134 interval <- case mbCookie of
192 case nodeInfo (key2id theirDhtKey) saddr of
193 Left e -> hPutStrLn stderr ("persueContact: nodeInfo fail... " ++ e)
194 Right ni -> do
195 -- AcquiringCookie
196 mbCookie <- DHT.cookieRequest crypto client (toPublic myseckey) ni
197 case mbCookie of
198 Nothing -> do 135 Nothing -> do
199 hPutStrLn stderr ("persueContact: (" ++ show mykeyAsId ++") <--> (" ++ show theirkeyAsId ++ ").") 136 hPutStrLn stderr ("persueContact: (" ++ show mykeyAsId ++") <--> (" ++ show theirkeyAsId ++ ").")
200 hPutStrLn stderr ("persueContact: CookieRequest failed. TODO: dhtpkNodes thingy") 137 hPutStrLn stderr ("persueContact: CookieRequest failed. TODO: dhtpkNodes thingy")
138 return longRetryInterval
201 Just cookie -> do 139 Just cookie -> do
202 hPutStrLn stderr "Have cookie, creating handshake packet..." 140 hPutStrLn stderr "Have cookie, creating handshake packet..."
203 let hp = HParam { hpOtherCookie = cookie 141 let hp = HParam { hpOtherCookie = cookie
@@ -216,9 +154,10 @@ persueContact getPolicy getStatus PersueContactMethods{..} writeStatus
216 forM myhandshake $ \response_handshake -> do 154 forM myhandshake $ \response_handshake -> do
217 sendHandshake (toxCryptoSessions tox) saddr response_handshake 155 sendHandshake (toxCryptoSessions tox) saddr response_handshake
218 atomically $ writeStatus $ InProgress AwaitingHandshake 156 atomically $ writeStatus $ InProgress AwaitingHandshake
157 return shortRetryInterval
219 -- AwaitingHandshake 158 -- AwaitingHandshake
220 -- AwaitingSessionPacket 159 -- AwaitingSessionPacket
221 retryAfterTimeout retryInterval 160 retryAfterTimeout interval
222 161
223data FreshenContactMethods = FreshenContactMethods 162data FreshenContactMethods = FreshenContactMethods
224 { dhtkeyInterval :: Int 163 { dhtkeyInterval :: Int