summaryrefslogtreecommitdiff
path: root/dht/src/Data/Tox
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-12-18 18:06:12 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-01 23:27:11 -0500
commitb6676d7c3339e46752cadfc1198886062f5c666d (patch)
tree25d8630d8d2fa6b2f5d3234a07445d61c02194df /dht/src/Data/Tox
parent4e8aa82d56129aae9e5ef22e5e0aa9287b993a92 (diff)
Used partitionTransform to simplify the onion client.
Diffstat (limited to 'dht/src/Data/Tox')
-rw-r--r--dht/src/Data/Tox/DHT/Multi.hs45
-rw-r--r--dht/src/Data/Tox/Onion.hs19
2 files changed, 45 insertions, 19 deletions
diff --git a/dht/src/Data/Tox/DHT/Multi.hs b/dht/src/Data/Tox/DHT/Multi.hs
index f769e384..7c8804b5 100644
--- a/dht/src/Data/Tox/DHT/Multi.hs
+++ b/dht/src/Data/Tox/DHT/Multi.hs
@@ -11,6 +11,7 @@ import Crypto.PubKey.Curve25519 (PublicKey)
11import qualified Network.Tox.NodeId as UDP 11import qualified Network.Tox.NodeId as UDP
12 ;import Network.Tox.NodeId (NodeId) 12 ;import Network.Tox.NodeId (NodeId)
13import qualified Network.Tox.TCP.NodeId as TCP 13import qualified Network.Tox.TCP.NodeId as TCP
14import Data.Tox.Onion (OnionDestination,RouteId)
14import Data.Tox.Relay hiding (NodeInfo) 15import Data.Tox.Relay hiding (NodeInfo)
15import Network.Address (either4or6) 16import Network.Address (either4or6)
16import Network.Tox.TCP as TCP (ViaRelay(..), tcpConnectionRequest_) 17import Network.Tox.TCP as TCP (ViaRelay(..), tcpConnectionRequest_)
@@ -64,25 +65,40 @@ instance GShow S where
64 gshowsPrec _ SessionUDP = showString "UDP" 65 gshowsPrec _ SessionUDP = showString "UDP"
65 gshowsPrec _ SessionTCP = showString "TCP" 66 gshowsPrec _ SessionTCP = showString "TCP"
66 67
68data O addr where
69 OnionUDP :: O (OnionDestination RouteId)
70 OnionTCP :: O (OnionDestination RouteId)
71
72instance GEq O where
73 geq OnionUDP OnionUDP = Just Refl
74 geq OnionTCP OnionTCP = Just Refl
75 geq _ _ = Nothing
76instance GCompare O where
77 gcompare OnionUDP OnionUDP = GEQ
78 gcompare OnionUDP OnionTCP = GLT
79 gcompare OnionTCP OnionTCP = GEQ
80 gcompare OnionTCP OnionUDP = GGT
81instance GShow O where
82 gshowsPrec _ OnionUDP = showString "UDP"
83 gshowsPrec _ OnionTCP = showString "TCP"
84
85untagOnion :: DSum O Identity -> OnionDestination RouteId
86untagOnion (OnionUDP :=> Identity o) = o
87untagOnion (OnionTCP :=> Identity o) = o
88
67-- Canonical in case of 6-mapped-4 addresses. 89-- Canonical in case of 6-mapped-4 addresses.
68canonize :: DSum S Identity -> DSum S Identity 90canonize :: DSum S Identity -> DSum S Identity
69canonize (SessionUDP :=> Identity saddr) = SessionUDP ==> either id id (either4or6 saddr) 91canonize (SessionUDP :=> Identity saddr) = SessionUDP ==> either id id (either4or6 saddr)
70canonize taddr = taddr 92canonize taddr = taddr
71 93
72data A addr where
73 AddrUDP :: SockAddr -> A UDP.NodeInfo
74 AddrTCP :: Maybe ConId -> TCP.NodeInfo -> A TCP.ViaRelay
75
76deriving instance Eq (A addr)
77
78type NodeInfo = DSum T Identity 94type NodeInfo = DSum T Identity
79type SessionAddress = DSum S Identity 95type SessionAddress = DSum S Identity
80 96type OnionAddress = DSum O Identity
81type Address = DSum T A
82 97
83#if MIN_VERSION_dependent_sum(0,6,0) 98#if MIN_VERSION_dependent_sum(0,6,0)
84deriveArgDict ''T 99deriveArgDict ''T
85deriveArgDict ''S 100deriveArgDict ''S
101deriveArgDict ''O
86#else 102#else
87instance ShowTag T Identity where 103instance ShowTag T Identity where
88 showTaggedPrec UDP = showsPrec 104 showTaggedPrec UDP = showsPrec
@@ -90,6 +106,9 @@ instance ShowTag T Identity where
90instance ShowTag S Identity where 106instance ShowTag S Identity where
91 showTaggedPrec SessionUDP = showsPrec 107 showTaggedPrec SessionUDP = showsPrec
92 showTaggedPrec SessionTCP = showsPrec 108 showTaggedPrec SessionTCP = showsPrec
109instance ShowTag O Identity where
110 showTaggedPrec OnionUDP = showsPrec
111 showTaggedPrec OnionTCP = showsPrec
93instance EqTag S Identity where 112instance EqTag S Identity where
94 eqTagged SessionUDP SessionUDP = (==) 113 eqTagged SessionUDP SessionUDP = (==)
95 eqTagged SessionTCP SessionTCP = (==) 114 eqTagged SessionTCP SessionTCP = (==)
@@ -99,16 +118,6 @@ instance OrdTag S Identity where
99#endif 118#endif
100 119
101 120
102{-
103nodeInfo :: NodeId -> DSum T A -> Either String (DSum T Identity )
104nodeInfo nid (UDP :=> AddrUDP saddr ) = fmap (UDP ==>) $ UDP.nodeInfo nid saddr
105nodeInfo nid (TCP :=> AddrTCP conid relay) = Right $ TCP ==> ViaRelay conid nid relay
106
107nodeAddr :: DSum T Identity -> DSum T A
108nodeAddr (UDP :=> Identity ni ) = UDP :=> AddrUDP (UDP.nodeAddr ni)
109nodeAddr (TCP :=> Identity (ViaRelay conid _ relay)) = TCP :=> AddrTCP conid relay
110-}
111
112nodeInfo :: NodeId -> DSum S Identity -> Either String (DSum T Identity) 121nodeInfo :: NodeId -> DSum S Identity -> Either String (DSum T Identity)
113nodeInfo nid (SessionUDP :=> Identity saddr) = fmap (UDP ==>) $ UDP.nodeInfo nid saddr 122nodeInfo nid (SessionUDP :=> Identity saddr) = fmap (UDP ==>) $ UDP.nodeInfo nid saddr
114nodeInfo nid (SessionTCP :=> Identity taddr@(ViaRelay _ nid2 _)) = 123nodeInfo nid (SessionTCP :=> Identity taddr@(ViaRelay _ nid2 _)) =
diff --git a/dht/src/Data/Tox/Onion.hs b/dht/src/Data/Tox/Onion.hs
index a9bc4e1d..55e81069 100644
--- a/dht/src/Data/Tox/Onion.hs
+++ b/dht/src/Data/Tox/Onion.hs
@@ -19,7 +19,7 @@
19module Data.Tox.Onion where 19module Data.Tox.Onion where
20 20
21 21
22import Network.Address (fromSockAddr,toSockAddr,setPort,either4or6,sockAddrPort,localhost4,localhost6) 22import Network.Address (fromSockAddr,toSockAddr,setPort,either4or6,sockAddrPort,localhost4,localhost6,nullAddress4)
23import Network.QueryResponse 23import Network.QueryResponse
24import Crypto.Tox hiding (encrypt,decrypt) 24import Crypto.Tox hiding (encrypt,decrypt)
25import Network.Tox.NodeId 25import Network.Tox.NodeId
@@ -873,6 +873,19 @@ data OnionRoute = OnionRoute
873 } 873 }
874 deriving Show 874 deriving Show
875 875
876dummySecret :: SecretKey
877dummySecret = fromJust $ decodeSecret "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
878
879dummyNodeId :: NodeId
880dummyNodeId = read "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
881
882dummyNode :: NodeInfo
883dummyNode = k where Right k = nodeInfo dummyNodeId nullAddress4
884
885dummyRoute :: OnionRoute
886dummyRoute = OnionRoute dummySecret dummySecret dummySecret
887 dummyNode dummyNode dummyNode
888 Nothing
876 889
877wrapOnion :: Serialize (Forwarding n msg) => 890wrapOnion :: Serialize (Forwarding n msg) =>
878 TransportCrypto 891 TransportCrypto
@@ -956,6 +969,10 @@ instance Read AnnouncedRendezvous where
956 } 969 }
957 970
958 971
972-- | Lookup the secret key for the given toxid public key. If it is not found,
973-- then the SearchingAlias symbol will be used to indicate that a new temporary
974-- key pair should be generated or that all known keys should be tried until one
975-- succeeds to decrypt the message.
959selectAlias :: TransportCrypto -> NodeId -> STM AliasSelector 976selectAlias :: TransportCrypto -> NodeId -> STM AliasSelector
960selectAlias crypto pkey = do 977selectAlias crypto pkey = do
961 ks <- filter (\(sk,pk) -> pk == id2key pkey) 978 ks <- filter (\(sk,pk) -> pk == id2key pkey)