diff options
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/Tox.hs | 2 | ||||
-rw-r--r-- | src/Network/Tox/DHT/Handlers.hs | 24 |
2 files changed, 15 insertions, 11 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs index e9220fcb..d434360f 100644 --- a/src/Network/Tox.hs +++ b/src/Network/Tox.hs | |||
@@ -112,6 +112,7 @@ newCrypto = do | |||
112 | drg0 <- getSystemDRG | 112 | drg0 <- getSystemDRG |
113 | return $ randomBytesGenerate 32 drg0 :: IO (ByteString, SystemDRG) | 113 | return $ randomBytesGenerate 32 drg0 :: IO (ByteString, SystemDRG) |
114 | noncevar <- atomically $ newTVar $ fst $ withDRG drg drgNew | 114 | noncevar <- atomically $ newTVar $ fst $ withDRG drg drgNew |
115 | cookieKeys <- atomically $ newTVar [] | ||
115 | hPutStrLn stderr $ "secret(tox) = " ++ DHT.showHex secret | 116 | hPutStrLn stderr $ "secret(tox) = " ++ DHT.showHex secret |
116 | hPutStrLn stderr $ "public(tox) = " ++ DHT.showHex pubkey | 117 | hPutStrLn stderr $ "public(tox) = " ++ DHT.showHex pubkey |
117 | hPutStrLn stderr $ "symmetric(tox) = " ++ DHT.showHex symkey | 118 | hPutStrLn stderr $ "symmetric(tox) = " ++ DHT.showHex symkey |
@@ -129,6 +130,7 @@ newCrypto = do | |||
129 | writeTVar noncevar drg2 | 130 | writeTVar noncevar drg2 |
130 | return nonce | 131 | return nonce |
131 | , userKeys = ukeys | 132 | , userKeys = ukeys |
133 | , pendingCookies = cookieKeys | ||
132 | } | 134 | } |
133 | 135 | ||
134 | updateIP :: TVar (R.BucketList NodeInfo) -> SockAddr -> STM () | 136 | updateIP :: TVar (R.BucketList NodeInfo) -> SockAddr -> STM () |
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 | ||
246 | cookieRequest :: TVar [(SockAddr,(Int,NodeInfo))] -> PublicKey -> Client -> NodeInfo -> IO (Maybe Cookie) | 246 | cookieRequest :: TransportCrypto -> Client -> NodeInfo -> IO (Maybe Cookie) |
247 | cookieRequest tvar myDhtKey client addr = do | 247 | cookieRequest 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 | ||
280 | unCookie :: DHTMessage t -> Maybe (t Cookie) | ||
279 | unCookie (DHTCookie n24 fcookie) = Just fcookie | 281 | unCookie (DHTCookie n24 fcookie) = Just fcookie |
280 | unCookie _ = Nothing | 282 | unCookie _ = Nothing |
281 | 283 | ||