summaryrefslogtreecommitdiff
path: root/dht/src/Data/Tox
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-12-14 16:11:03 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-01 23:26:49 -0500
commitd5efdc327bbb69a905043df45415817e318e38ee (patch)
tree7be975048f3e40c27811bdb39ba92d871a42588c /dht/src/Data/Tox
parent8c04d9cca70241bebe4b94b779fe7bbfe6140f51 (diff)
Multi Transports: TCP for DHT/Cookies/Handshakes.
Diffstat (limited to 'dht/src/Data/Tox')
-rw-r--r--dht/src/Data/Tox/DHT/Multi.hs126
-rw-r--r--dht/src/Data/Tox/Msg.hs26
-rw-r--r--dht/src/Data/Tox/Relay.hs3
3 files changed, 149 insertions, 6 deletions
diff --git a/dht/src/Data/Tox/DHT/Multi.hs b/dht/src/Data/Tox/DHT/Multi.hs
new file mode 100644
index 00000000..3f91387c
--- /dev/null
+++ b/dht/src/Data/Tox/DHT/Multi.hs
@@ -0,0 +1,126 @@
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE GADTs #-}
4{-# LANGUAGE MultiParamTypeClasses #-}
5{-# LANGUAGE StandaloneDeriving #-}
6{-# LANGUAGE TemplateHaskell #-}
7{-# LANGUAGE TypeFamilies #-}
8module Data.Tox.DHT.Multi where
9
10import qualified Network.Tox.NodeId as UDP
11 ;import Network.Tox.NodeId (NodeId)
12import qualified Network.Tox.TCP.NodeId as TCP
13import Data.Tox.Relay
14import Network.Address (either4or6)
15import Network.Tox.TCP as TCP (ViaRelay(..))
16import Network.QueryResponse (Tagged(..))
17
18import Data.Dependent.Sum
19import Data.GADT.Compare
20import Data.GADT.Show
21import Data.Functor.Identity
22import Data.Typeable
23import Network.Socket
24
25#if MIN_VERSION_dependent_sum(0,6,0)
26import Data.Constraint.Compose
27import Data.Constraint.Extras
28import Data.Constraint.Extras.TH
29#endif
30
31
32data T ni where
33 UDP :: T UDP.NodeInfo
34 TCP :: T TCP.ViaRelay
35
36instance GEq T where
37 geq UDP UDP = Just Refl
38 geq TCP TCP = Just Refl
39 geq _ _ = Nothing
40instance GCompare T where
41 gcompare UDP UDP = GEQ
42 gcompare UDP TCP = GLT
43 gcompare TCP TCP = GEQ
44 gcompare TCP UDP = GGT
45instance GShow T where
46 gshowsPrec _ UDP = showString "UDP"
47 gshowsPrec _ TCP = showString "TCP"
48
49data S addr where
50 SessionUDP :: S SockAddr
51 SessionTCP :: S TCP.ViaRelay
52
53instance GEq S where
54 geq SessionUDP SessionUDP = Just Refl
55 geq SessionTCP SessionTCP = Just Refl
56 geq _ _ = Nothing
57instance GCompare S where
58 gcompare SessionUDP SessionUDP = GEQ
59 gcompare SessionUDP SessionTCP = GLT
60 gcompare SessionTCP SessionTCP = GEQ
61 gcompare SessionTCP SessionUDP = GGT
62instance GShow S where
63 gshowsPrec _ SessionUDP = showString "UDP"
64 gshowsPrec _ SessionTCP = showString "TCP"
65
66-- Canonical in case of 6-mapped-4 addresses.
67canonize :: DSum S Identity -> DSum S Identity
68canonize (SessionUDP :=> Identity saddr) = SessionUDP ==> either id id (either4or6 saddr)
69canonize taddr = taddr
70
71data A addr where
72 AddrUDP :: SockAddr -> A UDP.NodeInfo
73 AddrTCP :: Maybe ConId -> TCP.NodeInfo -> A TCP.ViaRelay
74
75deriving instance Eq (A addr)
76
77type NodeInfo = DSum T Identity
78type SessionAddress = DSum S Identity
79
80type Address = DSum T A
81
82#if MIN_VERSION_dependent_sum(0,6,0)
83deriveArgDict ''T
84deriveArgDict ''S
85#else
86instance ShowTag T Identity where
87 showTaggedPrec UDP = showsPrec
88 showTaggedPrec TCP = showsPrec
89instance ShowTag S Identity where
90 showTaggedPrec SessionUDP = showsPrec
91 showTaggedPrec SessionTCP = showsPrec
92instance EqTag S Identity where
93 eqTagged SessionUDP SessionUDP = (==)
94 eqTagged SessionTCP SessionTCP = (==)
95instance OrdTag S Identity where
96 compareTagged SessionUDP SessionUDP = compare
97 compareTagged SessionTCP SessionTCP = compare
98#endif
99
100
101{-
102nodeInfo :: NodeId -> DSum T A -> Either String (DSum T Identity )
103nodeInfo nid (UDP :=> AddrUDP saddr ) = fmap (UDP ==>) $ UDP.nodeInfo nid saddr
104nodeInfo nid (TCP :=> AddrTCP conid relay) = Right $ TCP ==> ViaRelay conid nid relay
105
106nodeAddr :: DSum T Identity -> DSum T A
107nodeAddr (UDP :=> Identity ni ) = UDP :=> AddrUDP (UDP.nodeAddr ni)
108nodeAddr (TCP :=> Identity (ViaRelay conid _ relay)) = TCP :=> AddrTCP conid relay
109-}
110
111nodeInfo :: NodeId -> DSum S Identity -> Either String (DSum T Identity)
112nodeInfo nid (SessionUDP :=> Identity saddr) = fmap (UDP ==>) $ UDP.nodeInfo nid saddr
113nodeInfo nid (SessionTCP :=> Identity taddr@(ViaRelay _ nid2 _)) =
114 if nid2 == nid then Right $ TCP ==> taddr
115 else Left $ "Cached dht-key doesn't match."
116
117nodeId :: DSum T Identity -> NodeId
118nodeId (UDP :=> Identity ni ) = UDP.nodeId ni
119nodeId (TCP :=> Identity (ViaRelay _ nid _)) = nid
120
121relayNodeId :: TCP.ViaRelay -> UDP.NodeId
122relayNodeId (ViaRelay _ nid _) = nid
123
124udpNode :: DSum T Identity -> Maybe UDP.NodeInfo
125udpNode (UDP :=> Identity ni) = Just ni
126udpNode _ = Nothing
diff --git a/dht/src/Data/Tox/Msg.hs b/dht/src/Data/Tox/Msg.hs
index 8819faa7..4398586f 100644
--- a/dht/src/Data/Tox/Msg.hs
+++ b/dht/src/Data/Tox/Msg.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE DataKinds #-} 2{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE DefaultSignatures #-} 3{-# LANGUAGE DefaultSignatures #-}
3{-# LANGUAGE FlexibleInstances #-} 4{-# LANGUAGE FlexibleInstances #-}
@@ -7,6 +8,7 @@
7{-# LANGUAGE MultiParamTypeClasses #-} 8{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE PolyKinds #-} 9{-# LANGUAGE PolyKinds #-}
9{-# LANGUAGE StandaloneDeriving #-} 10{-# LANGUAGE StandaloneDeriving #-}
11{-# LANGUAGE TemplateHaskell #-}
10{-# LANGUAGE TypeFamilies #-} 12{-# LANGUAGE TypeFamilies #-}
11module Data.Tox.Msg where 13module Data.Tox.Msg where
12 14
@@ -14,6 +16,7 @@ import Crypto.Error
14import qualified Crypto.PubKey.Ed25519 as Ed25519 16import qualified Crypto.PubKey.Ed25519 as Ed25519
15import Data.ByteArray as BA 17import Data.ByteArray as BA
16import Data.ByteString as B 18import Data.ByteString as B
19import Data.Constraint
17import Data.Dependent.Sum 20import Data.Dependent.Sum
18import Data.Functor.Contravariant 21import Data.Functor.Contravariant
19import Data.Functor.Identity 22import Data.Functor.Identity
@@ -31,6 +34,12 @@ import Crypto.Tox
31import Data.PacketBuffer (compressSequenceNumbers, decompressSequenceNumbers) 34import Data.PacketBuffer (compressSequenceNumbers, decompressSequenceNumbers)
32import Network.Tox.NodeId 35import Network.Tox.NodeId
33 36
37#if MIN_VERSION_dependent_sum(0,6,0)
38import Data.Constraint.Compose
39import Data.Constraint.Extras
40import Data.Constraint.Extras.TH
41#endif
42
34newtype Unknown = Unknown B.ByteString deriving (Eq,Show) 43newtype Unknown = Unknown B.ByteString deriving (Eq,Show)
35newtype Padded = Padded B.ByteString deriving (Eq,Show) 44newtype Padded = Padded B.ByteString deriving (Eq,Show)
36 45
@@ -102,11 +111,7 @@ msgID (Pkt mid :=> Identity _) = M mid
102 111
103-- TODO 112-- TODO
104instance GShow Pkt where gshowsPrec = showsPrec 113instance GShow Pkt where gshowsPrec = showsPrec
105instance ShowTag Pkt Identity where
106 showTaggedPrec (Pkt _) = showsPrec
107
108instance GEq Pkt where geq (Pkt _) (Pkt _) = eqT 114instance GEq Pkt where geq (Pkt _) (Pkt _) = eqT
109instance EqTag Pkt Identity where eqTagged (Pkt _) (Pkt _) = (==)
110 115
111someMsgVal :: KnownMsg n => Msg n a -> SomeMsg 116someMsgVal :: KnownMsg n => Msg n a -> SomeMsg
112someMsgVal m = msgid (proxy m) 117someMsgVal m = msgid (proxy m)
@@ -311,3 +316,16 @@ instance Serialize Invite where
311 ConfirmedInvite ns -> return () -- TODO: encode nodes. 316 ConfirmedInvite ns -> return () -- TODO: encode nodes.
312 317
313instance Packet Invite where 318instance Packet Invite where
319
320#if MIN_VERSION_dependent_sum(0,6,0)
321-- deriveArgDict ''Pkt
322instance ArgDict (ComposeC Show Identity) Pkt where
323 type ConstraintsFor Pkt (ComposeC Show Identity) = ()
324 argDict (Pkt _) = Dict
325instance ArgDict (ComposeC Eq Identity) Pkt where
326 type ConstraintsFor Pkt (ComposeC Eq Identity) = ()
327 argDict (Pkt _) = Dict
328#else
329instance EqTag Pkt Identity where eqTagged (Pkt _) (Pkt _) = (==)
330instance ShowTag Pkt Identity where showTaggedPrec (Pkt _) = showsPrec
331#endif
diff --git a/dht/src/Data/Tox/Relay.hs b/dht/src/Data/Tox/Relay.hs
index 1bce76db..31752433 100644
--- a/dht/src/Data/Tox/Relay.hs
+++ b/dht/src/Data/Tox/Relay.hs
@@ -8,7 +8,7 @@
8{-# LANGUAGE StandaloneDeriving #-} 8{-# LANGUAGE StandaloneDeriving #-}
9{-# LANGUAGE UndecidableInstances #-} 9{-# LANGUAGE UndecidableInstances #-}
10module Data.Tox.Relay 10module Data.Tox.Relay
11 ( module Network.Tox.TCP.NodeId 11 ( module TCP
12 , module Data.Tox.Relay 12 , module Data.Tox.Relay
13 ) where 13 ) where
14 14
@@ -30,7 +30,6 @@ import qualified Rank2
30import qualified Text.ParserCombinators.ReadP as RP 30import qualified Text.ParserCombinators.ReadP as RP
31 31
32import Crypto.Tox 32import Crypto.Tox
33import Network.Tox.TCP.NodeId
34import Data.Tox.Onion 33import Data.Tox.Onion
35import qualified Network.Tox.NodeId as UDP 34import qualified Network.Tox.NodeId as UDP
36import Network.Tox.TCP.NodeId as TCP 35import Network.Tox.TCP.NodeId as TCP