summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Network/Tox.hs2
-rw-r--r--src/Network/Tox/DHT/Handlers.hs34
-rw-r--r--src/Network/Tox/DHT/Transport.hs14
3 files changed, 43 insertions, 7 deletions
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index 29591a23..af8114f4 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -284,7 +284,7 @@ newTox keydb addr = do
284 orouter <- newOnionRouter 284 orouter <- newOnionRouter
285 (dhtcrypt,onioncrypt,dtacrypt,cryptonet) <- toxTransport crypto orouter lookupClose udp 285 (dhtcrypt,onioncrypt,dtacrypt,cryptonet) <- toxTransport crypto orouter lookupClose udp
286 let dhtnet0 = layerTransport (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt 286 let dhtnet0 = layerTransport (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt
287 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr routing) (DHT.handlers routing) id 287 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr routing) (DHT.handlers crypto routing) id
288 $ \client net -> onInbound (DHT.updateRouting client routing orouter) net 288 $ \client net -> onInbound (DHT.updateRouting client routing orouter) net
289 289
290 orouter <- forkRouteBuilder orouter $ \nid ni -> maybe [] (\(_,ns,_)->ns) <$> DHT.getNodes dhtclient nid ni 290 orouter <- forkRouteBuilder orouter $ \nid ni -> maybe [] (\(_,ns,_)->ns) <$> DHT.getNodes dhtclient nid ni
diff --git a/src/Network/Tox/DHT/Handlers.hs b/src/Network/Tox/DHT/Handlers.hs
index 4f891316..9cdf0d06 100644
--- a/src/Network/Tox/DHT/Handlers.hs
+++ b/src/Network/Tox/DHT/Handlers.hs
@@ -93,7 +93,7 @@ classify msg = mapMessage (\nonce24 (nonce8,_) -> go msg (TransactionId nonce8 n
93 go (DHTGetNodes {}) = IsQuery GetNodesType 93 go (DHTGetNodes {}) = IsQuery GetNodesType
94 go (DHTPong {}) = IsResponse 94 go (DHTPong {}) = IsResponse
95 go (DHTSendNodes {}) = IsResponse 95 go (DHTSendNodes {}) = IsResponse
96 go (DHTCookieRequest {}) = IsQuery (PacketKind 0x18) 96 go (DHTCookieRequest {}) = IsQuery CookieRequestType
97 go (DHTCookie {}) = IsResponse 97 go (DHTCookie {}) = IsResponse
98 go (DHTDHTRequest {}) = IsQuery DHTRequestType 98 go (DHTDHTRequest {}) = IsQuery DHTRequestType
99 99
@@ -183,6 +183,23 @@ getNodesH routing addr (GetNodes nid) = do
183 183
184 k = 4 184 k = 4
185 185
186cookieRequestH :: TransportCrypto -> NodeInfo -> CookieRequest -> IO Cookie
187cookieRequestH crypto ni (CookieRequest remoteUserKey) = do
188 (n24,sym,us) <- atomically $ do
189 n24 <- transportNewNonce crypto
190 sym <- transportSymmetric crypto
191 us <- readTVar $ userKeys crypto
192 return (n24,sym,us)
193 timestamp <- round . (* 1000000) <$> getPOSIXTime
194 let dta = encodePlain $ CookieData
195 { cookieTime = timestamp
196 , longTermKey = remoteUserKey
197 , dhtKey = transportPublic crypto
198 }
199 edta = encryptSymmetric sym n24 dta
200 return $ Cookie n24 edta
201
202
186type Message = DHTMessage ((,) Nonce8) 203type Message = DHTMessage ((,) Nonce8)
187 204
188type Client = QR.Client String PacketKind TransactionId NodeInfo Message 205type Client = QR.Client String PacketKind TransactionId NodeInfo Message
@@ -298,9 +315,18 @@ isGetNodes _ _ = Left "Bad GetNodes"
298mkSendNodes :: TransactionId -> NodeInfo -> NodeInfo -> SendNodes -> DHTMessage ((,) Nonce8) 315mkSendNodes :: TransactionId -> NodeInfo -> NodeInfo -> SendNodes -> DHTMessage ((,) Nonce8)
299mkSendNodes tid src dst sendnodes = DHTSendNodes $ wrapAsymm tid src dst (, sendnodes) 316mkSendNodes tid src dst sendnodes = DHTSendNodes $ wrapAsymm tid src dst (, sendnodes)
300 317
301handlers :: Routing -> PacketKind -> Maybe Handler 318isCookieRequest :: (f CookieRequest -> CookieRequest) -> DHTMessage f -> Either String CookieRequest
302handlers routing PingType = Just $ MethodHandler (isPing snd) mkPong pingH 319isCookieRequest unpack (DHTCookieRequest a) = Right $ unpack $ asymmData a
303handlers routing GetNodesType = Just $ MethodHandler (isGetNodes snd) mkSendNodes $ getNodesH routing 320isCookieRequest _ _ = Left "Bad cookie request"
321
322mkCookie :: TransactionId -> NodeInfo -> NodeInfo -> Cookie -> DHTMessage ((,) Nonce8)
323mkCookie (TransactionId n8 n24) src dst cookie = DHTCookie n24 (n8,cookie)
324
325
326handlers :: TransportCrypto -> Routing -> PacketKind -> Maybe Handler
327handlers _ routing PingType = Just $ MethodHandler (isPing snd) mkPong pingH
328handlers _ routing GetNodesType = Just $ MethodHandler (isGetNodes snd) mkSendNodes $ getNodesH routing
329handlers crypto _ CookieRequestType = Just $ MethodHandler (isCookieRequest snd) mkCookie $ cookieRequestH crypto
304 330
305nodeSearch :: Client -> Search NodeId (IP,PortNumber) () NodeInfo NodeInfo 331nodeSearch :: Client -> Search NodeId (IP,PortNumber) () NodeInfo NodeInfo
306nodeSearch client = Search 332nodeSearch client = Search
diff --git a/src/Network/Tox/DHT/Transport.hs b/src/Network/Tox/DHT/Transport.hs
index bebb8ae7..5ebe8b15 100644
--- a/src/Network/Tox/DHT/Transport.hs
+++ b/src/Network/Tox/DHT/Transport.hs
@@ -20,8 +20,9 @@ module Network.Tox.DHT.Transport
20 , FriendRequest(..) 20 , FriendRequest(..)
21 , NoSpam(..) 21 , NoSpam(..)
22 , verifyChecksum 22 , verifyChecksum
23 , CookieRequest 23 , CookieRequest(..)
24 , Cookie 24 , Cookie(..)
25 , CookieData(..)
25 , DHTRequest 26 , DHTRequest
26 , mapMessage 27 , mapMessage
27 , encrypt 28 , encrypt
@@ -378,6 +379,15 @@ data CookieData = CookieData -- 16 (mac)
378instance Sized CookieData where 379instance Sized CookieData where
379 size = ConstSize 72 380 size = ConstSize 72
380 381
382instance Serialize CookieData where
383 get = CookieData <$> get
384 <*> (id2key <$> get)
385 <*> (id2key <$> get)
386 put (CookieData tm userkey dhtkey) = do
387 put tm
388 put (key2id userkey)
389 put (key2id dhtkey)
390
381instance Sized CookieRequest where 391instance Sized CookieRequest where
382 size = ConstSize 64 -- 32 byte key + 32 byte padding 392 size = ConstSize 64 -- 32 byte key + 32 byte padding
383 393