{-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TupleSections #-} {-# LANGUAGE TypeOperators #-} module Network.Tox.Transport (toxTransport, RouteId) where import Network.QueryResponse import Crypto.Tox import Data.Tox.Relay as TCP import Network.Tox.DHT.Transport as UDP import Network.Tox.Onion.Transport import Network.Tox.Crypto.Transport import OnionRouter import Network.Socket toxTransport :: TransportCrypto -> OnionRouter -> (PublicKey -> IO (Maybe UDP.NodeInfo)) -> SockAddr -- ^ UDP bind-address -> UDPTransport -> (TCP.NodeInfo -> RelayPacket -> IO ()) -- ^ TCP server-bound callback. -> (Int -> OnionMessage Encrypted -> IO ()) -- ^ TCP client-bound callback. -> IO ( Transport String SockAddr (CryptoPacket Encrypted) , Transport String UDP.NodeInfo (DHTMessage Encrypted8) , Transport String (OnionDestination RouteId) (OnionMessage Encrypted) , Transport String AnnouncedRendezvous (PublicKey,OnionData) , Transport String SockAddr (Handshake Encrypted)) toxTransport crypto orouter closeLookup addr udp tcp2server tcp2client = do (netcrypto, udp0) <- partitionTransport parseCrypto encodeCrypto udp (dht,udp1) <- partitionTransportM (parseDHTAddr crypto) (fmap Just . encodeDHTAddr) $ forwardOnions crypto addr udp0 tcp2client (onion1,udp2) <- partitionAndForkTransport tcp2server (parseOnionAddr $ lookupSender orouter) (encodeOnionAddr crypto $ lookupRoute orouter) udp1 (dta,onion) <- partitionTransportM (parseDataToRoute crypto) (encodeDataToRoute crypto) onion1 let handshakes = layerTransport parseHandshakes encodeHandshakes udp2 return ( netcrypto , forwardDHTRequests crypto closeLookup dht , onion , dta , handshakes ) -- instance (Sized a, Sized b) => Sized (a,b) where size = _todo -- Byte value Packet Kind Return address -- :----------- :-------------------- -- `0x00` Ping Request DHTNode -- `0x01` Ping Response - -- `0x02` Nodes Request DHTNode -- `0x04` Nodes Response - -- `0x18` Cookie Request DHTNode, but without sending pubkey in response -- `0x19` Cookie Response - (no pubkey) -- -- `0x21` LAN Discovery DHTNode (No reply, port 33445, trigger Nodes Request/Response) -- -- `0x20` DHT Request DHTNode/-forward -- -- `0x1a` Crypto Handshake CookieAddress -- -- `0x1b` Crypto Data SessionAddress -- -- `0x83` Announce Request OnionToOwner -- `0x84` Announce Response - -- `0x85` Onion Data Request OnionToOwner -- `0x86` Onion Data Response - -- -- `0xf0` Bootstrap Info SockAddr? -- -- `0x80` Onion Request 0 -forward -- `0x81` Onion Request 1 -forward -- `0x82` Onion Request 2 -forward -- `0x8c` Onion Response 3 -return -- `0x8d` Onion Response 2 -return -- `0x8e` Onion Response 1 -return