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