diff options
Diffstat (limited to 'dht/src')
-rw-r--r-- | dht/src/Data/Tox/Onion.hs | 2 | ||||
-rw-r--r-- | dht/src/Network/Tox.hs | 2 | ||||
-rw-r--r-- | dht/src/Network/Tox/DHT/Transport.hs | 61 | ||||
-rw-r--r-- | dht/src/Network/Tox/Transport.hs | 4 |
4 files changed, 38 insertions, 31 deletions
diff --git a/dht/src/Data/Tox/Onion.hs b/dht/src/Data/Tox/Onion.hs index e0d7c744..e19f71b6 100644 --- a/dht/src/Data/Tox/Onion.hs +++ b/dht/src/Data/Tox/Onion.hs | |||
@@ -198,7 +198,7 @@ parseOnionAddr lookupSender (msg,saddr) | |||
198 | (return . Left . \od -> (msg,od)) | 198 | (return . Left . \od -> (msg,od)) |
199 | maddr | 199 | maddr |
200 | Just (Right msg@(OnionToRouteResponse asym)) -> do | 200 | Just (Right msg@(OnionToRouteResponse asym)) -> do |
201 | let ni = asymNodeInfo saddr asym | 201 | let ni = asymNodeInfo nodeInfo saddr asym |
202 | return $ Left (msg, OnionDestination SearchingAlias ni Nothing) | 202 | return $ Left (msg, OnionDestination SearchingAlias ni Nothing) |
203 | _ -> return right | 203 | _ -> return right |
204 | 204 | ||
diff --git a/dht/src/Network/Tox.hs b/dht/src/Network/Tox.hs index 4b66cfc8..5d27f34f 100644 --- a/dht/src/Network/Tox.hs +++ b/dht/src/Network/Tox.hs | |||
@@ -351,7 +351,7 @@ newToxOverTransport keydb addr onNewSession (crypto,roster) udp tcp = do | |||
351 | (fromMaybe (\_ _ -> return ()) tcp) | 351 | (fromMaybe (\_ _ -> return ()) tcp) |
352 | sessions <- initSessions (sendMessage cryptonet) | 352 | sessions <- initSessions (sendMessage cryptonet) |
353 | 353 | ||
354 | let dhtnet0 = layerTransportM (DHT.decrypt crypto) (DHT.encrypt crypto) dhtcrypt | 354 | let dhtnet0 = layerTransportM (DHT.decrypt crypto nodeId) (DHT.encrypt crypto nodeId) dhtcrypt |
355 | tbl4 = DHT.routing4 $ mkrouting (error "missing client") | 355 | tbl4 = DHT.routing4 $ mkrouting (error "missing client") |
356 | tbl6 = DHT.routing6 $ mkrouting (error "missing client") | 356 | tbl6 = DHT.routing6 $ mkrouting (error "missing client") |
357 | updateOnion bkts tr = hookBucketList DHT.toxSpace bkts orouter (trampolinesUDP orouter) tr | 357 | updateOnion bkts tr = hookBucketList DHT.toxSpace bkts orouter (trampolinesUDP orouter) tr |
diff --git a/dht/src/Network/Tox/DHT/Transport.hs b/dht/src/Network/Tox/DHT/Transport.hs index 7414343d..0583c9a3 100644 --- a/dht/src/Network/Tox/DHT/Transport.hs +++ b/dht/src/Network/Tox/DHT/Transport.hs | |||
@@ -58,8 +58,8 @@ import Data.Word | |||
58 | import GHC.Generics | 58 | import GHC.Generics |
59 | import Network.Socket | 59 | import Network.Socket |
60 | 60 | ||
61 | type DHTTransport = Transport String NodeInfo (DHTMessage Encrypted8) | 61 | type DHTTransport ni = Transport String ni (DHTMessage Encrypted8) |
62 | type HandleHi a = Arrival String NodeInfo (DHTMessage Encrypted8) -> IO a | 62 | type HandleHi ni a = Arrival String ni (DHTMessage Encrypted8) -> IO a |
63 | 63 | ||
64 | 64 | ||
65 | data DHTMessage (f :: * -> *) | 65 | data DHTMessage (f :: * -> *) |
@@ -95,32 +95,36 @@ mapMessage f (DHTLanDiscovery nid) = Nothing | |||
95 | instance Sized Ping where size = ConstSize 1 | 95 | instance Sized Ping where size = ConstSize 1 |
96 | instance Sized Pong where size = ConstSize 1 | 96 | instance Sized Pong where size = ConstSize 1 |
97 | 97 | ||
98 | parseDHTAddr :: TransportCrypto -> (ByteString, SockAddr) -> IO (Either (DHTMessage Encrypted8,NodeInfo) (ByteString,SockAddr)) | 98 | parseDHTAddr :: Eq saddr => |
99 | parseDHTAddr crypto (msg,saddr) | 99 | STM [(saddr, (Int, PublicKey))] |
100 | -> (NodeId -> saddr -> Either String ni) | ||
101 | -> (ByteString, saddr) | ||
102 | -> IO (Either (DHTMessage Encrypted8,ni) (ByteString,saddr)) | ||
103 | parseDHTAddr pendingCookies nodeInfo (msg,saddr) | ||
100 | | Just (typ,bs) <- B.uncons msg | 104 | | Just (typ,bs) <- B.uncons msg |
101 | , let right = return $ Right (msg,saddr) | 105 | , let right = return $ Right (msg,saddr) |
102 | left = either (const right) (return . Left) | 106 | left = either (const right) (return . Left) |
103 | = case typ of | 107 | = case typ of |
104 | 0x00 -> left $ direct bs saddr DHTPing | 108 | 0x00 -> left $ direct nodeInfo bs saddr DHTPing |
105 | 0x01 -> left $ direct bs saddr DHTPong | 109 | 0x01 -> left $ direct nodeInfo bs saddr DHTPong |
106 | 0x02 -> left $ direct bs saddr DHTGetNodes | 110 | 0x02 -> left $ direct nodeInfo bs saddr DHTGetNodes |
107 | 0x04 -> left $ direct bs saddr DHTSendNodes | 111 | 0x04 -> left $ direct nodeInfo bs saddr DHTSendNodes |
108 | 0x18 -> left $ direct bs saddr DHTCookieRequest | 112 | 0x18 -> left $ direct nodeInfo bs saddr DHTCookieRequest |
109 | 0x19 -> do | 113 | 0x19 -> do |
110 | cs <- atomically $ readTVar (pendingCookies crypto) | 114 | cs <- atomically pendingCookies |
111 | let ni = fromMaybe (noReplyAddr saddr) $ do | 115 | let ni = fromMaybe (noReplyAddr nodeInfo saddr) $ do |
112 | (cnt,key) <- lookup saddr cs <|> listToMaybe (map snd cs) | 116 | (cnt,key) <- lookup saddr cs <|> listToMaybe (map snd cs) |
113 | either (const Nothing) Just $ nodeInfo (key2id key) saddr | 117 | either (const Nothing) Just $ nodeInfo (key2id key) saddr |
114 | left $ fanGet bs getCookie (uncurry DHTCookie) (const $ ni) | 118 | left $ fanGet bs getCookie (uncurry DHTCookie) (const $ ni) |
115 | 0x20 -> left $ fanGet bs getDHTReqest (uncurry DHTDHTRequest) (asymNodeInfo saddr . snd) | 119 | 0x20 -> left $ fanGet bs getDHTReqest (uncurry DHTDHTRequest) (asymNodeInfo nodeInfo saddr . snd) |
116 | 0x21 -> left $ do | 120 | 0x21 -> left $ do |
117 | nid <- runGet get bs | 121 | nid <- runGet get bs |
118 | ni <- nodeInfo nid saddr | 122 | ni <- nodeInfo nid saddr |
119 | return (DHTLanDiscovery nid, ni) | 123 | return (DHTLanDiscovery nid, ni) |
120 | _ -> right | 124 | _ -> right |
121 | 125 | ||
122 | encodeDHTAddr :: (DHTMessage Encrypted8,NodeInfo) -> IO (ByteString, SockAddr) | 126 | encodeDHTAddr :: (ni -> saddr) -> (DHTMessage Encrypted8,ni) -> IO (ByteString, saddr) |
123 | encodeDHTAddr (msg,ni) = return (runPut $ putMessage msg, nodeAddr ni) | 127 | encodeDHTAddr nodeAddr (msg,ni) = return (runPut $ putMessage msg, nodeAddr ni) |
124 | 128 | ||
125 | dhtMessageType :: ( Serialize (f DHTRequest) | 129 | dhtMessageType :: ( Serialize (f DHTRequest) |
126 | , Serialize (f (Cookie Encrypted)), Serialize (f CookieRequest) | 130 | , Serialize (f (Cookie Encrypted)), Serialize (f CookieRequest) |
@@ -159,23 +163,24 @@ getDHT = getAsymm | |||
159 | 163 | ||
160 | 164 | ||
161 | -- Throws an error if called with a non-internet socket. | 165 | -- Throws an error if called with a non-internet socket. |
162 | direct :: Sized a => ByteString | 166 | direct :: Sized a => (NodeId -> saddr -> Either String ni) |
163 | -> SockAddr | 167 | -> ByteString |
168 | -> saddr | ||
164 | -> (Asymm (Encrypted8 a) -> DHTMessage Encrypted8) | 169 | -> (Asymm (Encrypted8 a) -> DHTMessage Encrypted8) |
165 | -> Either String (DHTMessage Encrypted8, NodeInfo) | 170 | -> Either String (DHTMessage Encrypted8, ni) |
166 | direct bs saddr f = fanGet bs getDHT f (asymNodeInfo saddr) | 171 | direct nodeInfo bs saddr f = fanGet bs getDHT f (asymNodeInfo nodeInfo saddr) |
167 | 172 | ||
168 | -- Throws an error if called with a non-internet socket. | 173 | -- Throws an error if called with a non-internet socket. |
169 | asymNodeInfo :: SockAddr -> Asymm a -> NodeInfo | 174 | asymNodeInfo :: (NodeId -> saddr -> Either String ni) -> saddr -> Asymm a -> ni |
170 | asymNodeInfo saddr asym = either (error . mappend "asymNodeInfo: ") id $ nodeInfo (key2id $ senderKey asym) saddr | 175 | asymNodeInfo nodeInfo saddr asym = either (error . mappend "asymNodeInfo: ") id $ nodeInfo (key2id $ senderKey asym) saddr |
171 | 176 | ||
172 | 177 | ||
173 | fanGet :: ByteString -> Get x -> (x -> a) -> (x -> b) -> Either String (a,b) | 178 | fanGet :: ByteString -> Get x -> (x -> a) -> (x -> b) -> Either String (a,b) |
174 | fanGet bs getIt f nid = fmap (f &&& nid) $ runGet getIt bs | 179 | fanGet bs getIt f nid = fmap (f &&& nid) $ runGet getIt bs |
175 | 180 | ||
176 | -- Throws an error if called with a non-internet socket. | 181 | -- Throws an error if called with a non-internet socket. |
177 | noReplyAddr :: SockAddr -> NodeInfo | 182 | noReplyAddr :: (NodeId -> saddr -> Either String ni) -> saddr -> ni |
178 | noReplyAddr saddr = either (error . mappend "noReplyAddr: ") id $ nodeInfo zeroID saddr | 183 | noReplyAddr nodeInfo saddr = either (error . mappend "noReplyAddr: ") id $ nodeInfo zeroID saddr |
179 | 184 | ||
180 | 185 | ||
181 | data DHTRequest | 186 | data DHTRequest |
@@ -396,10 +401,10 @@ instance Serialize CookieRequest where | |||
396 | get = CookieRequest <$> getPublicKey <* {- padding -} getPublicKey | 401 | get = CookieRequest <$> getPublicKey <* {- padding -} getPublicKey |
397 | put (CookieRequest k) = putPublicKey k >> {- padding -} putPublicKey k | 402 | put (CookieRequest k) = putPublicKey k >> {- padding -} putPublicKey k |
398 | 403 | ||
399 | forwardDHTRequests :: TransportCrypto -> (PublicKey -> IO (Maybe NodeInfo)) -> DHTTransport -> DHTTransport | 404 | forwardDHTRequests :: TransportCrypto -> (PublicKey -> IO (Maybe ni)) -> DHTTransport ni -> DHTTransport ni |
400 | forwardDHTRequests crypto closeLookup dht = dht { awaitMessage = await' } | 405 | forwardDHTRequests crypto closeLookup dht = dht { awaitMessage = await' } |
401 | where | 406 | where |
402 | await' :: HandleHi a -> STM (IO a) | 407 | -- await' :: HandleHi ni a -> STM (IO a) |
403 | await' pass = awaitMessage dht $ \case | 408 | await' pass = awaitMessage dht $ \case |
404 | Arrival src m@(DHTDHTRequest target payload) | target /= transportPublic crypto | 409 | Arrival src m@(DHTDHTRequest target payload) | target /= transportPublic crypto |
405 | -> do mni <- closeLookup target | 410 | -> do mni <- closeLookup target |
@@ -408,8 +413,8 @@ forwardDHTRequests crypto closeLookup dht = dht { awaitMessage = await' } | |||
408 | join $ atomically (await' pass) | 413 | join $ atomically (await' pass) |
409 | m -> pass m | 414 | m -> pass m |
410 | 415 | ||
411 | encrypt :: TransportCrypto -> DHTMessage ((,) Nonce8) -> NodeInfo -> IO (DHTMessage Encrypted8, NodeInfo) | 416 | encrypt :: TransportCrypto -> (ni -> NodeId) -> DHTMessage ((,) Nonce8) -> ni -> IO (DHTMessage Encrypted8, ni) |
412 | encrypt crypto msg ni = do | 417 | encrypt crypto nodeId msg ni = do |
413 | let cipher n plain = Composed $ encryptMessage crypto (id2key $ nodeId ni) n plain | 418 | let cipher n plain = Composed $ encryptMessage crypto (id2key $ nodeId ni) n plain |
414 | m <- sequenceMessage $ transcode cipher msg | 419 | m <- sequenceMessage $ transcode cipher msg |
415 | return (m, ni) | 420 | return (m, ni) |
@@ -423,8 +428,8 @@ encryptMessage crypto destKey n arg = do | |||
423 | secret <- lookupSharedSecret crypto (transportSecret crypto) destKey n | 428 | secret <- lookupSharedSecret crypto (transportSecret crypto) destKey n |
424 | return $ E8 $ ToxCrypto.encrypt secret plain | 429 | return $ E8 $ ToxCrypto.encrypt secret plain |
425 | 430 | ||
426 | decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> IO (Either String (DHTMessage ((,) Nonce8), NodeInfo)) | 431 | decrypt :: TransportCrypto -> (ni -> NodeId) -> DHTMessage Encrypted8 -> ni -> IO (Either String (DHTMessage ((,) Nonce8), ni)) |
427 | decrypt crypto msg ni = do | 432 | decrypt crypto nodeId msg ni = do |
428 | let decipher n c = Composed $ decryptMessage crypto n . left ((,) $ id2key $ nodeId ni) $ c | 433 | let decipher n c = Composed $ decryptMessage crypto n . left ((,) $ id2key $ nodeId ni) $ c |
429 | msg' <- sequenceMessage $ transcode decipher msg | 434 | msg' <- sequenceMessage $ transcode decipher msg |
430 | return $ fmap (, ni) $ sequenceMessage msg' | 435 | return $ fmap (, ni) $ sequenceMessage msg' |
diff --git a/dht/src/Network/Tox/Transport.hs b/dht/src/Network/Tox/Transport.hs index fef8e8bb..7728ba7a 100644 --- a/dht/src/Network/Tox/Transport.hs +++ b/dht/src/Network/Tox/Transport.hs | |||
@@ -16,6 +16,7 @@ import Network.Tox.Onion.Transport | |||
16 | import Network.Tox.Crypto.Transport | 16 | import Network.Tox.Crypto.Transport |
17 | import Network.Tox.Onion.Routes | 17 | import Network.Tox.Onion.Routes |
18 | 18 | ||
19 | import Control.Concurrent.STM | ||
19 | import Network.Socket | 20 | import Network.Socket |
20 | 21 | ||
21 | toxTransport :: | 22 | toxTransport :: |
@@ -33,7 +34,8 @@ toxTransport :: | |||
33 | , Transport String SockAddr (Handshake Encrypted)) | 34 | , Transport String SockAddr (Handshake Encrypted)) |
34 | toxTransport crypto orouter closeLookup addr udp tcp2server tcp2client = do | 35 | toxTransport crypto orouter closeLookup addr udp tcp2server tcp2client = do |
35 | (netcrypto, udp0) <- partitionTransport parseCrypto encodeCrypto udp | 36 | (netcrypto, udp0) <- partitionTransport parseCrypto encodeCrypto udp |
36 | (dht,udp1) <- partitionTransportM (parseDHTAddr crypto) (fmap Just . encodeDHTAddr) | 37 | (dht,udp1) <- partitionTransportM (parseDHTAddr (readTVar $ pendingCookies crypto) nodeInfo) |
38 | (fmap Just . encodeDHTAddr nodeAddr) | ||
37 | $ forwardOnions crypto addr udp0 tcp2client | 39 | $ forwardOnions crypto addr udp0 tcp2client |
38 | (onion1,udp2) <- partitionAndForkTransport tcp2server | 40 | (onion1,udp2) <- partitionAndForkTransport tcp2server |
39 | (parseOnionAddr $ lookupSender orouter) | 41 | (parseOnionAddr $ lookupSender orouter) |