summaryrefslogtreecommitdiff
path: root/src/Network/Tox/TCP.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Tox/TCP.hs')
-rw-r--r--src/Network/Tox/TCP.hs24
1 files changed, 15 insertions, 9 deletions
diff --git a/src/Network/Tox/TCP.hs b/src/Network/Tox/TCP.hs
index 36200586..adb42514 100644
--- a/src/Network/Tox/TCP.hs
+++ b/src/Network/Tox/TCP.hs
@@ -132,18 +132,22 @@ tcpStream crypto = StreamHandshake
132 dput XTCP $ "TCP exception: " ++ show e 132 dput XTCP $ "TCP exception: " ++ show e
133 return Nothing 133 return Nothing
134 , streamEncode = \y -> do 134 , streamEncode = \y -> do
135 dput XTCP $ "TCP(acquire nonce):" ++ show addr ++ " <-- " ++ show y
135 n24 <- takeMVar nsend 136 n24 <- takeMVar nsend
136 dput XTCP $ "TCP:" ++ show addr ++ " <-- " ++ show y 137 dput XTCP $ "TCP(got nonce):" ++ show addr ++ " <-- " ++ show y
137 let bs = encode $ encrypt (noncef' n24) $ encodePlain y 138 let bs = encode $ encrypt (noncef' n24) $ encodePlain y
138 ($ h) -- bracket (takeMVar hvar) (putMVar hvar) 139 ($ h) -- bracket (takeMVar hvar) (putMVar hvar)
139 $ \h -> hPut h (encode (fromIntegral $ Data.ByteString.length bs :: Word16) <> bs) 140 $ \h -> hPut h (encode (fromIntegral $ Data.ByteString.length bs :: Word16) <> bs)
140 `catchIOError` \e -> dput XTCP $ "TCP write exception: " ++ show e 141 `catchIOError` \e -> dput XTCP $ "TCP write exception: " ++ show e
142 dput XTCP $ "TCP(incrementing nonce): " ++ show addr ++ " <-- " ++ show y
141 putMVar nsend (incrementNonce24 n24) 143 putMVar nsend (incrementNonce24 n24)
144 dput XTCP $ "TCP(finished): " ++ show addr ++ " <-- " ++ show y
142 } 145 }
143 , streamAddr = nodeAddr 146 , streamAddr = nodeAddr
144 } 147 }
145 148
146toxTCP :: TransportCrypto -> IO (TransportA err NodeInfo RelayPacket (Bool,RelayPacket)) 149toxTCP :: TransportCrypto -> IO ( TCPCache (SessionProtocol RelayPacket RelayPacket)
150 , TransportA err NodeInfo RelayPacket (Bool,RelayPacket) )
147toxTCP crypto = tcpTransport 30 (tcpStream crypto) 151toxTCP crypto = tcpTransport 30 (tcpStream crypto)
148 152
149tcpSpace :: KademliaSpace NodeId NodeInfo 153tcpSpace :: KademliaSpace NodeId NodeInfo
@@ -267,13 +271,14 @@ type RelayClient = Client String PacketNumber Nonce8 NodeInfo (Bool,RelayPacket)
267newClient :: TransportCrypto 271newClient :: TransportCrypto
268 -> (MVar (Bool,RelayPacket) -> a) -- ^ store mvar for query 272 -> (MVar (Bool,RelayPacket) -> a) -- ^ store mvar for query
269 -> (a -> RelayPacket -> IO void) -- ^ load mvar for query 273 -> (a -> RelayPacket -> IO void) -- ^ load mvar for query
270 -> IO ( TVar (ChaChaDRG, Data.Word64Map.Word64Map a) 274 -> IO ( ( TVar (ChaChaDRG, Data.Word64Map.Word64Map a)
275 , TCPCache (SessionProtocol RelayPacket RelayPacket) )
271 , Client String PacketNumber Nonce8 NodeInfo (Bool,RelayPacket)) 276 , Client String PacketNumber Nonce8 NodeInfo (Bool,RelayPacket))
272newClient crypto store load = do 277newClient crypto store load = do
273 net <- toxTCP crypto 278 (tcpcache,net) <- toxTCP crypto
274 drg <- drgNew 279 drg <- drgNew
275 map_var <- atomically $ newTVar (drg, Data.Word64Map.empty) 280 map_var <- atomically $ newTVar (drg, Data.Word64Map.empty)
276 return $ (,) map_var Client 281 return $ (,) (map_var,tcpcache) Client
277 { clientNet = {- XXX: Client type forces this pointless layering. -} layerTransport ((Right .) . (,) . (,) False) (,) net 282 { clientNet = {- XXX: Client type forces this pointless layering. -} layerTransport ((Right .) . (,) . (,) False) (,) net
278 , clientDispatcher = DispatchMethods 283 , clientDispatcher = DispatchMethods
279 { classifyInbound = (. snd) $ \case 284 { classifyInbound = (. snd) $ \case
@@ -284,10 +289,11 @@ newClient crypto store load = do
284 OOBRecv k bs -> IsUnsolicited $ handleOOB k bs 289 OOBRecv k bs -> IsUnsolicited $ handleOOB k bs
285 wut -> IsUnknown (show wut) 290 wut -> IsUnknown (show wut)
286 , lookupHandler = \case 291 , lookupHandler = \case
287 PingPacket -> Just MethodHandler 292 PingPacket -> trace ("tcp-received-ping") $ Just MethodHandler
288 { methodParse = \(_,RelayPing n8) -> Right () 293 { methodParse = \case (_,RelayPing n8) -> Right ()
289 , methodSerialize = \n8 src dst () -> (False, RelayPong n8) 294 _ -> trace ("tcp-non-ping") $ Left "TCP: Non-ping?"
290 , methodAction = \src () -> return () 295 , methodSerialize = \n8 src dst () -> trace ("tcp-made-pong-"++show n8) (False, RelayPong n8)
296 , methodAction = \src () -> dput XTCP $ "TCP pinged by "++show src
291 } 297 }
292 w -> trace ("tcp-lookupHandler: "++show w) $ Just NoReply 298 w -> trace ("tcp-lookupHandler: "++show w) $ Just NoReply
293 { methodParse = \x -> Left "tcp-lookuphandler?" -- :: x -> Either err a 299 { methodParse = \x -> Left "tcp-lookuphandler?" -- :: x -> Either err a