diff options
author | Joe Crayne <joe@jerkface.net> | 2019-12-12 03:30:20 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 23:26:05 -0500 |
commit | b675708ddb86efcdb2000c5e4b152d7744cbe16f (patch) | |
tree | 8fd610a9a40f25ff76a1b3271a83fbff72c9a790 /dht/src/Network | |
parent | 458d053dcff411179d52d437087cce76b8af4a9c (diff) |
TCP client: RoutingRequest query + cosmetic changes
Diffstat (limited to 'dht/src/Network')
-rw-r--r-- | dht/src/Network/Tox/TCP.hs | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/dht/src/Network/Tox/TCP.hs b/dht/src/Network/Tox/TCP.hs index 1531dfb4..4b3a4594 100644 --- a/dht/src/Network/Tox/TCP.hs +++ b/dht/src/Network/Tox/TCP.hs | |||
@@ -16,6 +16,7 @@ import Control.Monad | |||
16 | import Crypto.Random | 16 | import Crypto.Random |
17 | import Data.Aeson (ToJSON(..),FromJSON(..)) | 17 | import Data.Aeson (ToJSON(..),FromJSON(..)) |
18 | import qualified Data.Aeson as JSON | 18 | import qualified Data.Aeson as JSON |
19 | import Data.ByteArray (withByteArray) | ||
19 | import Data.Functor.Contravariant | 20 | import Data.Functor.Contravariant |
20 | import Data.Functor.Identity | 21 | import Data.Functor.Identity |
21 | import Data.Hashable | 22 | import Data.Hashable |
@@ -26,9 +27,11 @@ import Data.Monoid | |||
26 | import Data.Serialize | 27 | import Data.Serialize |
27 | import Data.Word | 28 | import Data.Word |
28 | import qualified Data.Vector as Vector | 29 | import qualified Data.Vector as Vector |
30 | import Foreign.Storable (peek) | ||
29 | import Network.Socket (SockAddr(..)) | 31 | import Network.Socket (SockAddr(..)) |
30 | import qualified Text.ParserCombinators.ReadP as RP | 32 | import qualified Text.ParserCombinators.ReadP as RP |
31 | import System.IO.Error | 33 | import System.IO.Error |
34 | import System.IO.Unsafe (unsafeDupablePerformIO) | ||
32 | import System.Timeout | 35 | import System.Timeout |
33 | 36 | ||
34 | import ControlMaybe | 37 | import ControlMaybe |
@@ -270,8 +273,25 @@ tcpPing client dst = do | |||
270 | , method = PingPacket | 273 | , method = PingPacket |
271 | } | 274 | } |
272 | 275 | ||
276 | tcpConnectionRequest :: Client err PacketNumber tid addr (Bool, RelayPacket) | ||
277 | -> PublicKey -> addr -> IO (Maybe ConId) | ||
278 | tcpConnectionRequest client pubkey ni = do | ||
279 | sendQuery client meth pubkey ni | ||
280 | where | ||
281 | meth = MethodSerializer | ||
282 | { wrapQuery = \n8 src dst pubkey -> (True,RoutingRequest pubkey) | ||
283 | , unwrapResponse = \(_,RoutingResponse cid pubkey) -> cid | ||
284 | , methodTimeout = \dst -> return (dst,5000000) | ||
285 | , method = RoutingRequestPacket | ||
286 | } | ||
287 | |||
273 | type RelayClient = Client String PacketNumber Nonce8 NodeInfo (Bool,RelayPacket) | 288 | type RelayClient = Client String PacketNumber Nonce8 NodeInfo (Bool,RelayPacket) |
274 | 289 | ||
290 | keyToNonce :: PublicKey -> Nonce8 | ||
291 | keyToNonce k = unsafeDupablePerformIO $ withByteArray k $ \ptr -> do | ||
292 | w8 <- peek ptr | ||
293 | return $ Nonce8 w8 | ||
294 | |||
275 | -- | Create a new TCP relay client. Because polymorphic existential record | 295 | -- | Create a new TCP relay client. Because polymorphic existential record |
276 | -- updates are currently hard with GHC, this function accepts parameters for | 296 | -- updates are currently hard with GHC, this function accepts parameters for |
277 | -- generalizing the table-entry type for pending transactions. Safe trivial | 297 | -- generalizing the table-entry type for pending transactions. Safe trivial |
@@ -291,11 +311,13 @@ newClient crypto store load = do | |||
291 | { clientNet = {- XXX: Client type forces this pointless layering. -} layerTransport ((Right .) . (,) . (,) False) (,) net | 311 | { clientNet = {- XXX: Client type forces this pointless layering. -} layerTransport ((Right .) . (,) . (,) False) (,) net |
292 | , clientDispatcher = DispatchMethods | 312 | , clientDispatcher = DispatchMethods |
293 | { classifyInbound = (. snd) $ \case | 313 | { classifyInbound = (. snd) $ \case |
294 | RelayPing n -> IsQuery PingPacket n | 314 | RelayPing n -> IsQuery PingPacket n |
295 | RelayPong n -> IsResponse n | 315 | RelayPong n -> IsResponse n |
316 | RoutingRequest k -> IsQuery RoutingRequestPacket (keyToNonce k) | ||
317 | RoutingResponse conId k -> IsResponse (keyToNonce k) | ||
296 | OnionPacketResponse (OnionAnnounceResponse n8 n24 ciphered) -> IsResponse n8 | 318 | OnionPacketResponse (OnionAnnounceResponse n8 n24 ciphered) -> IsResponse n8 |
297 | OnionPacketResponse o@(OnionToRouteResponse _) -> IsUnsolicited $ handle2route o | 319 | OnionPacketResponse o@(OnionToRouteResponse _) -> IsUnsolicited $ handle2route o |
298 | OOBRecv k bs -> IsUnsolicited $ handleOOB k bs | 320 | OOBRecv k bs -> IsUnsolicited $ handleOOB k bs |
299 | wut -> IsUnknown (show wut) | 321 | wut -> IsUnknown (show wut) |
300 | , lookupHandler = \case | 322 | , lookupHandler = \case |
301 | PingPacket -> trace ("tcp-received-ping") $ Just MethodHandler | 323 | PingPacket -> trace ("tcp-received-ping") $ Just MethodHandler |