summaryrefslogtreecommitdiff
path: root/src/Network/Tox/DHT/Handlers.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-10-28 17:05:42 -0400
committerjoe <joe@jerkface.net>2017-10-28 17:05:42 -0400
commite89145df011e7737c59a1ce4568432e2e9291871 (patch)
tree11c76e5d76ecca70393063d1a70cc25fba4097af /src/Network/Tox/DHT/Handlers.hs
parent63f0543e28ac2e73adb4676c36a6fa0b9b7c9d35 (diff)
New "cookie" command for tox command-line interface.
Diffstat (limited to 'src/Network/Tox/DHT/Handlers.hs')
-rw-r--r--src/Network/Tox/DHT/Handlers.hs24
1 files changed, 13 insertions, 11 deletions
diff --git a/src/Network/Tox/DHT/Handlers.hs b/src/Network/Tox/DHT/Handlers.hs
index 840e2e6b..38763f9c 100644
--- a/src/Network/Tox/DHT/Handlers.hs
+++ b/src/Network/Tox/DHT/Handlers.hs
@@ -243,39 +243,41 @@ ping client addr = do
243 hPutStrLn stderr $ show addr ++ " -pong-> " ++ show reply 243 hPutStrLn stderr $ show addr ++ " -pong-> " ++ show reply
244 maybe (return False) (\Pong -> return True) $ join reply 244 maybe (return False) (\Pong -> return True) $ join reply
245 245
246cookieRequest :: TVar [(SockAddr,(Int,NodeInfo))] -> PublicKey -> Client -> NodeInfo -> IO (Maybe Cookie) 246cookieRequest :: TransportCrypto -> Client -> NodeInfo -> IO (Maybe Cookie)
247cookieRequest tvar myDhtKey client addr = do 247cookieRequest crypto client addr = do
248 let sockAddr = nodeAddr addr 248 let sockAddr = nodeAddr addr
249 nid = id2key $ nodeId addr
249 let incAddr sockMap 250 let incAddr sockMap
250 = case partition ((==sockAddr) . fst) sockMap of 251 = case partition ((==sockAddr) . fst) sockMap of
251 ([],xs) -> insert (sockAddr, (1 ,addr)) xs 252 ([],xs) -> (sockAddr, (1 ,nid)) : xs
252 ([(_,(c,addr'))],xs) | addr' == addr -> insert (sockAddr, (c+1,addr)) xs 253 ([(_,(c,addr'))],xs) | addr' == nid -> (sockAddr, (c+1,nid)) : xs
253 anythingElse -> error $ "unreachable at " ++ __FILE__ ++ show __LINE__ ++ show anythingElse ++ show (sockAddr,addr) 254 anythingElse -> error $ "unreachable at " ++ __FILE__ ++ show __LINE__ ++ show anythingElse ++ show (sockAddr,addr)
254 decAddr sockMap 255 decAddr sockMap
255 = case partition ((==sockAddr) . fst) sockMap of 256 = case partition ((==sockAddr) . fst) sockMap of
256 ([],xs) -> xs -- unreachable? 257 ([],xs) -> xs -- unreachable?
257 ([(_,(1,addr'))],xs) | addr' == addr -> xs 258 ([(_,(1,addr'))],xs) | addr' == nid -> xs
258 ([(_,(c,addr'))],xs) | addr' == addr -> insert (sockAddr,(c-1,addr)) xs 259 ([(_,(c,addr'))],xs) | addr' == nid -> (sockAddr,(c-1,nid)) : xs
259 anythingElse -> error $ "unreachable at " ++ __FILE__ ++ show __LINE__ ++ show anythingElse ++ show (sockAddr,addr) 260 anythingElse -> error $ "unreachable at " ++ __FILE__ ++ show __LINE__ ++ show anythingElse ++ show (sockAddr,addr)
260 sockMap <- atomically $ do 261 sockMap <- atomically $ do
261 mp <- incAddr <$> readTVar tvar 262 mp <- incAddr <$> readTVar (pendingCookies crypto)
262 writeTVar tvar mp 263 writeTVar (pendingCookies crypto) mp
263 return mp 264 return mp
264 let cookieSerializer 265 let cookieSerializer
265 = MethodSerializer 266 = MethodSerializer
266 { methodTimeout = \tid addr -> do 267 { methodTimeout = \tid addr -> do
267 modifyTVar tvar decAddr 268 modifyTVar (pendingCookies crypto) decAddr
268 return (addr, 5000000) 269 return (addr, 5000000)
269 , method = CookieRequestType 270 , method = CookieRequestType
270 , wrapQuery = \tid src dst cr -> DHTCookieRequest $ wrapAsymm tid src dst (, cr) 271 , wrapQuery = \tid src dst cr -> DHTCookieRequest $ wrapAsymm tid src dst (, cr)
271 , unwrapResponse = fmap snd . unCookie 272 , unwrapResponse = fmap snd . unCookie
272 } 273 }
273 cookieRequest = CookieRequest myDhtKey 274 cookieRequest = CookieRequest (transportPublic crypto)
274 hPutStrLn stderr $ show addr ++ " <-- cookieRequest" 275 hPutStrLn stderr $ show addr ++ " <-- cookieRequest"
275 reply <- QR.sendQuery client cookieSerializer cookieRequest addr 276 reply <- QR.sendQuery client cookieSerializer cookieRequest addr
276 hPutStrLn stderr $ show addr ++ " -cookieResponse-> " ++ show reply 277 hPutStrLn stderr $ show addr ++ " -cookieResponse-> " ++ show reply
277 return $ join reply 278 return $ join reply
278 279
280unCookie :: DHTMessage t -> Maybe (t Cookie)
279unCookie (DHTCookie n24 fcookie) = Just fcookie 281unCookie (DHTCookie n24 fcookie) = Just fcookie
280unCookie _ = Nothing 282unCookie _ = Nothing
281 283