summaryrefslogtreecommitdiff
path: root/dht/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'dht/src/Network')
-rw-r--r--dht/src/Network/Tox/TCP.hs28
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
16import Crypto.Random 16import Crypto.Random
17import Data.Aeson (ToJSON(..),FromJSON(..)) 17import Data.Aeson (ToJSON(..),FromJSON(..))
18import qualified Data.Aeson as JSON 18import qualified Data.Aeson as JSON
19import Data.ByteArray (withByteArray)
19import Data.Functor.Contravariant 20import Data.Functor.Contravariant
20import Data.Functor.Identity 21import Data.Functor.Identity
21import Data.Hashable 22import Data.Hashable
@@ -26,9 +27,11 @@ import Data.Monoid
26import Data.Serialize 27import Data.Serialize
27import Data.Word 28import Data.Word
28import qualified Data.Vector as Vector 29import qualified Data.Vector as Vector
30import Foreign.Storable (peek)
29import Network.Socket (SockAddr(..)) 31import Network.Socket (SockAddr(..))
30import qualified Text.ParserCombinators.ReadP as RP 32import qualified Text.ParserCombinators.ReadP as RP
31import System.IO.Error 33import System.IO.Error
34import System.IO.Unsafe (unsafeDupablePerformIO)
32import System.Timeout 35import System.Timeout
33 36
34import ControlMaybe 37import ControlMaybe
@@ -270,8 +273,25 @@ tcpPing client dst = do
270 , method = PingPacket 273 , method = PingPacket
271 } 274 }
272 275
276tcpConnectionRequest :: Client err PacketNumber tid addr (Bool, RelayPacket)
277 -> PublicKey -> addr -> IO (Maybe ConId)
278tcpConnectionRequest 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
273type RelayClient = Client String PacketNumber Nonce8 NodeInfo (Bool,RelayPacket) 288type RelayClient = Client String PacketNumber Nonce8 NodeInfo (Bool,RelayPacket)
274 289
290keyToNonce :: PublicKey -> Nonce8
291keyToNonce 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