summaryrefslogtreecommitdiff
path: root/src/Data/Tox/Relay.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-12-15 02:34:00 -0500
committerJoe Crayne <joe@jerkface.net>2018-12-16 14:08:27 -0500
commit0403b3426c268409969eb517dce86e9c2ce12988 (patch)
tree2d12967dd1c68d8fc7943d94685f67cb84493ec9 /src/Data/Tox/Relay.hs
parenta599a465072409a428ea5973083844090d270968 (diff)
WIP: Support for sending onion queries to TCP relays.
Diffstat (limited to 'src/Data/Tox/Relay.hs')
-rw-r--r--src/Data/Tox/Relay.hs47
1 files changed, 46 insertions, 1 deletions
diff --git a/src/Data/Tox/Relay.hs b/src/Data/Tox/Relay.hs
index 02300866..d1e9fb99 100644
--- a/src/Data/Tox/Relay.hs
+++ b/src/Data/Tox/Relay.hs
@@ -8,16 +8,24 @@
8{-# LANGUAGE UndecidableInstances #-} 8{-# LANGUAGE UndecidableInstances #-}
9module Data.Tox.Relay where 9module Data.Tox.Relay where
10 10
11import Data.Aeson (ToJSON(..),FromJSON(..))
12import qualified Data.Aeson as JSON
11import Data.ByteString as B 13import Data.ByteString as B
12import Data.Data 14import Data.Data
13import Data.Functor.Contravariant 15import Data.Functor.Contravariant
16import Data.Hashable
17import qualified Data.HashMap.Strict as HashMap
14import Data.Monoid 18import Data.Monoid
15import Data.Serialize 19import Data.Serialize
20import qualified Data.Vector as Vector
16import Data.Word 21import Data.Word
22import Network.Socket
17import qualified Rank2 23import qualified Rank2
24import qualified Text.ParserCombinators.ReadP as RP
18 25
19import Crypto.Tox 26import Crypto.Tox
20import Network.Tox.Onion.Transport 27import Data.Tox.Onion
28import qualified Network.Tox.NodeId as UDP
21 29
22newtype ConId = ConId Word8 30newtype ConId = ConId Word8
23 deriving (Eq,Show,Ord,Data,Serialize) 31 deriving (Eq,Show,Ord,Data,Serialize)
@@ -178,3 +186,40 @@ instance Sized (Welcome Encrypted) where
178instance Serialize (Welcome Encrypted) where 186instance Serialize (Welcome Encrypted) where
179 get = Welcome <$> get <*> get 187 get = Welcome <$> get <*> get
180 put (Welcome n dta) = put n >> put dta 188 put (Welcome n dta) = put n >> put dta
189
190data NodeInfo = NodeInfo
191 { udpNodeInfo :: UDP.NodeInfo
192 , tcpPort :: PortNumber
193 }
194 deriving (Eq,Ord)
195
196instance Read NodeInfo where
197 readsPrec _ = RP.readP_to_S $ do
198 udp <- RP.readS_to_P reads
199 port <- RP.between (RP.char '{') (RP.char '}') $ do
200 mapM_ RP.char ("tcp:" :: String)
201 w16 <- RP.readS_to_P reads
202 return $ fromIntegral (w16 :: Word16)
203 return $ NodeInfo udp port
204
205instance ToJSON NodeInfo where
206 toJSON (NodeInfo udp port) = case (toJSON udp) of
207 JSON.Object tbl -> JSON.Object $ HashMap.insert "tcp_ports"
208 (JSON.Array $ Vector.fromList
209 [JSON.Number (fromIntegral port)])
210 tbl
211 x -> x -- Shouldn't happen.
212
213instance FromJSON NodeInfo where
214 parseJSON json = do
215 udp <- parseJSON json
216 port <- case json of
217 JSON.Object v -> do
218 portnum:_ <- v JSON..: "tcp_ports"
219 return (fromIntegral (portnum :: Word16))
220 _ -> fail "TCP.NodeInfo: Expected JSON object."
221 return $ NodeInfo udp port
222
223instance Hashable NodeInfo where
224 hashWithSalt s n = hashWithSalt s (udpNodeInfo n)
225