summaryrefslogtreecommitdiff
path: root/Connection/Tox/Threads.hs
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2018-06-16 05:41:12 +0000
committerJames Crayne <jim.crayne@gmail.com>2018-06-16 06:41:15 +0000
commit939c23d57365a49d366b6534c6f343f0a12a770a (patch)
treec7523e9daf59f63bd91f51a4ccd93090d0b0bbc0 /Connection/Tox/Threads.hs
parent82fb11604685273a2071e75f725280dfed884730 (diff)
persueContact wip
Diffstat (limited to 'Connection/Tox/Threads.hs')
-rw-r--r--Connection/Tox/Threads.hs69
1 files changed, 58 insertions, 11 deletions
diff --git a/Connection/Tox/Threads.hs b/Connection/Tox/Threads.hs
index 2ff058b3..12ac9682 100644
--- a/Connection/Tox/Threads.hs
+++ b/Connection/Tox/Threads.hs
@@ -15,8 +15,10 @@ import Connection
15-- import Connection.Tox 15-- import Connection.Tox
16import Data.IP (IP) 16import Data.IP (IP)
17import Network.Tox.Crypto.Transport 17import Network.Tox.Crypto.Transport
18import Network.Tox.Crypto.Handlers
18import Network.Tox.NodeId 19import Network.Tox.NodeId
19-- import Network.Tox.DHT.Handlers (nodeSearch) 20import Network.Tox.ContactInfo
21import Network.Tox.DHT.Handlers {- (nodeSearch) -} as DHT
20import Network.Socket 22import Network.Socket
21import Network.Kademlia.Search 23import Network.Kademlia.Search
22import Network.Kademlia.Routing (BucketList) 24import Network.Kademlia.Routing (BucketList)
@@ -144,11 +146,21 @@ whileTryingAndNotEstablished getPolicy getStatus writeStatus body = fix $ \loop
144 (body retryAfterTimeout)) 146 (body retryAfterTimeout))
145 147
146data PersueContactMethods params = PersueContactMethods 148data PersueContactMethods params = PersueContactMethods
147 { getHandshakeParams :: STM params 149 { allsessions :: NetCryptoSessions
148 , sendHandshake :: params -> IO () 150 , myseckey :: SecretKey
151 , theirpubkey :: PublicKey
152 , client :: DHT.Client
149 , retryInterval :: Int 153 , retryInterval :: Int
154 , contact :: Contact
150 } 155 }
151 156
157retryUntilJust :: TVar (Maybe a) -> STM a
158retryUntilJust tvar = do
159 mb <- readTVar tvar
160 case mb of
161 mempty -> retry
162 Just x -> return x
163
152-- | Continuously attempt to send handshake packets until a connection is 164-- | Continuously attempt to send handshake packets until a connection is
153-- established. 165-- established.
154-- 166--
@@ -163,15 +175,50 @@ persueContact getPolicy getStatus PersueContactMethods{..} writeStatus
163 = whileTryingAndNotEstablished getPolicy getStatus writeStatus 175 = whileTryingAndNotEstablished getPolicy getStatus writeStatus
164 $ \retryAfterTimeout -> do 176 $ \retryAfterTimeout -> do
165 -- AwaitingDHTKey 177 -- AwaitingDHTKey
178 atomically $ writeStatus (InProgress AwaitingDHTKey)
179 keypkt <- atomically $ retryUntilJust (contactKeyPacket contact)
180 let theirDhtKey = DHT.dhtpk keypkt
166 -- AcquiringIPAddress 181 -- AcquiringIPAddress
167 params <- getHandshakeParams 182 atomically $ writeStatus (InProgress AcquiringIPAddress)
168 writeStatus (InProgress AcquiringCookie) 183 ni <- atomically $ do
169 return $ do -- AcquiringCookie 184 saddr <- retryUntilJust (contactLastSeenAddr contact)
170 -- AwaitingHandshake 185 either retry return $ nodeInfo (key2id theirDhtKey) saddr
171 -- AwaitingSessionPacket 186 let mykeyAsId = key2id (toPublic myseckey)
172 sendHandshake params 187 theirkeyAsId = key2id theirpubkey
173 atomically $ writeStatus $ InProgress AwaitingHandshake 188 atomically $ writeStatus (InProgress AcquiringCookie)
174 retryAfterTimeout retryInterval 189 -- if no session:
190 -- Convert to NodeInfo, so we can send cookieRequest
191 let crypto = transportCrypto allsessions
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
199 hPutStrLn stderr ("persueContact: (" ++ show mykeyAsId ++") <--> (" ++ show theirkeyAsId ++ ").")
200 hPutStrLn stderr ("persueContact: CookieRequest failed. TODO: dhtpkNodes thingy")
201 Just cookie -> do
202 hPutStrLn stderr "Have cookie, creating handshake packet..."
203 let hp = HParam { hpOtherCookie = cookie
204 , hpMySecretKey = myseckey
205 , hpCookieRemotePubkey = theirpubkey
206 , hpCookieRemoteDhtkey = theirDhtKey
207 , hpTheirBaseNonce = Nothing
208 , hpTheirSessionKeyPublic = Nothing
209 }
210 newsession <- generateSecretKey
211 timestamp <- getPOSIXTime
212 (myhandshake,ioAction)
213 <- atomically $ freshCryptoSession allsessions saddr newsession timestamp hp
214 ioAction
215 -- send handshake
216 forM myhandshake $ \response_handshake -> do
217 sendHandshake (toxCryptoSessions tox) saddr response_handshake
218 atomically $ writeStatus $ InProgress AwaitingHandshake
219 -- AwaitingHandshake
220 -- AwaitingSessionPacket
221 retryAfterTimeout retryInterval
175 222
176data FreshenContactMethods = FreshenContactMethods 223data FreshenContactMethods = FreshenContactMethods
177 { dhtkeyInterval :: Int 224 { dhtkeyInterval :: Int