summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Crypto/Tox.hs2
-rw-r--r--src/Network/Tox.hs2
-rw-r--r--src/Network/Tox/DHT/Handlers.hs24
3 files changed, 17 insertions, 11 deletions
diff --git a/src/Crypto/Tox.hs b/src/Crypto/Tox.hs
index d5727254..8a65dfb4 100644
--- a/src/Crypto/Tox.hs
+++ b/src/Crypto/Tox.hs
@@ -81,6 +81,7 @@ import System.Endian
81import qualified Data.ByteString.Internal 81import qualified Data.ByteString.Internal
82import Control.Concurrent.STM 82import Control.Concurrent.STM
83import Crypto.Error.Types (CryptoFailable (..), throwCryptoError) 83import Crypto.Error.Types (CryptoFailable (..), throwCryptoError)
84import Network.Socket (SockAddr)
84 85
85-- | A 16-byte mac and an arbitrary-length encrypted stream. 86-- | A 16-byte mac and an arbitrary-length encrypted stream.
86newtype Encrypted a = Encrypted ByteString 87newtype Encrypted a = Encrypted ByteString
@@ -367,6 +368,7 @@ data TransportCrypto = TransportCrypto
367 , transportSymmetric :: STM SymmetricKey 368 , transportSymmetric :: STM SymmetricKey
368 , transportNewNonce :: STM Nonce24 369 , transportNewNonce :: STM Nonce24
369 , userKeys :: TVar [(SecretKey,PublicKey)] 370 , userKeys :: TVar [(SecretKey,PublicKey)]
371 , pendingCookies :: TVar [(SockAddr, (Int, PublicKey))]
370 } 372 }
371 373
372getPublicKey :: S.Get PublicKey 374getPublicKey :: S.Get PublicKey
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
134updateIP :: TVar (R.BucketList NodeInfo) -> SockAddr -> STM () 136updateIP :: 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
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