summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/dhtd.hs13
-rw-r--r--src/Crypto/Tox.hs2
-rw-r--r--src/Network/Tox.hs2
-rw-r--r--src/Network/Tox/DHT/Handlers.hs24
4 files changed, 26 insertions, 15 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 36b8e294..3fb1c641 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -734,10 +734,15 @@ main = do
734 734
735 let toxDHT bkts = DHT 735 let toxDHT bkts = DHT
736 { dhtBuckets = bkts (Tox.toxRouting tox) 736 { dhtBuckets = bkts (Tox.toxRouting tox)
737 , dhtPing = Map.singleton "ping" $ DHTPing 737 , dhtPing = Map.fromList
738 { pingQuery = fmap (bool Nothing (Just ())) . Tox.ping (Tox.toxDHT tox) 738 [ ("ping", DHTPing
739 , pingShowResult = show 739 { pingQuery = fmap (bool Nothing (Just ())) . Tox.ping (Tox.toxDHT tox)
740 } 740 , pingShowResult = show
741 })
742 , ("cookie", DHTPing
743 { pingQuery = Tox.cookieRequest (Tox.toxCryptoKeys tox) (Tox.toxDHT tox)
744 , pingShowResult = show
745 })]
741 , dhtQuery = Map.fromList 746 , dhtQuery = Map.fromList
742 [ ("node", DHTQuery (Tox.nodeSearch $ Tox.toxDHT tox) 747 [ ("node", DHTQuery (Tox.nodeSearch $ Tox.toxDHT tox)
743 (\ni -> fmap Tox.unwrapNodes 748 (\ni -> fmap Tox.unwrapNodes
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