summaryrefslogtreecommitdiff
path: root/dht/src/Network/Tox/Onion/Transport.hs
diff options
context:
space:
mode:
Diffstat (limited to 'dht/src/Network/Tox/Onion/Transport.hs')
-rw-r--r--dht/src/Network/Tox/Onion/Transport.hs53
1 files changed, 29 insertions, 24 deletions
diff --git a/dht/src/Network/Tox/Onion/Transport.hs b/dht/src/Network/Tox/Onion/Transport.hs
index 407cd387..913d339d 100644
--- a/dht/src/Network/Tox/Onion/Transport.hs
+++ b/dht/src/Network/Tox/Onion/Transport.hs
@@ -24,6 +24,7 @@ module Network.Tox.Onion.Transport
24 , decrypt 24 , decrypt
25 , peelSymmetric 25 , peelSymmetric
26 , OnionRoute(..) 26 , OnionRoute(..)
27 , dummyRoute
27 , N0 28 , N0
28 , N1 29 , N1
29 , N2 30 , N2
@@ -39,9 +40,11 @@ module Network.Tox.Onion.Transport
39 , wrapOnion 40 , wrapOnion
40 , wrapOnionPure 41 , wrapOnionPure
41 , unwrapAnnounceResponse 42 , unwrapAnnounceResponse
43 , wrapIndirectHops
42 ) where 44 ) where
43 45
44import Data.ByteString (ByteString) 46import qualified Data.ByteString as B
47 ;import Data.ByteString (ByteString)
45import Data.Maybe 48import Data.Maybe
46import Data.Serialize 49import Data.Serialize
47import Network.Socket 50import Network.Socket
@@ -50,8 +53,10 @@ import Crypto.Tox hiding (encrypt,decrypt)
50import Network.Tox.TCP.NodeId (udpNodeInfo) 53import Network.Tox.TCP.NodeId (udpNodeInfo)
51import qualified Data.Tox.Relay as TCP 54import qualified Data.Tox.Relay as TCP
52import Data.Tox.Onion 55import Data.Tox.Onion
56import Network.Address (nullAddress4)
53import Network.Tox.DHT.Transport (SendNodes(..)) 57import Network.Tox.DHT.Transport (SendNodes(..))
54import Network.Tox.NodeId 58import Network.Tox.NodeId
59import Network.QueryResponse
55 60
56{- 61{-
57encodeOnionAddr :: TransportCrypto 62encodeOnionAddr :: TransportCrypto
@@ -88,28 +93,28 @@ wrapForRoute :: TransportCrypto
88 -> NodeInfo 93 -> NodeInfo
89 -> OnionRoute 94 -> OnionRoute
90 -> IO (Either TCP.RelayPacket (OnionRequest N0)) 95 -> IO (Either TCP.RelayPacket (OnionRequest N0))
91wrapForRoute crypto msg ni r@OnionRoute{routeRelayPort=Nothing} = do 96wrapForRoute crypto msg ni r@OnionRoute{routeRelayPort=Nothing} =
92 -- We needn't use the same nonce value here, but I think it is safe to do so. 97 wrapIndirectHops crypto msg ni r $ \nonce saddr msg' -> do
93 let nonce = msgNonce msg 98 fwd <- wrapOnion crypto (routeAliasA r)
94 fwd <- wrapOnion crypto (routeAliasA r) 99 nonce
95 nonce 100 (id2key . nodeId $ routeNodeA r)
96 (id2key . nodeId $ routeNodeA r) 101 saddr
97 (nodeAddr $ routeNodeB r) 102 msg'
98 =<< wrapOnion crypto (routeAliasB r) 103 return $ Right OnionRequest { onionNonce = nonce
99 nonce 104 , onionForward = fwd
100 (id2key . nodeId $ routeNodeB r) 105 , pathFromOwner = NoReturnPath
101 (nodeAddr $ routeNodeC r) 106 }
102 =<< wrapOnion crypto (routeAliasC r) 107wrapForRoute crypto msg ni r@OnionRoute { routeRelayPort = Just _ } =
103 nonce 108 wrapIndirectHops crypto msg ni r $ \nonce saddr fwd ->
104 (id2key . nodeId $ routeNodeC r) 109 return $ Left $ TCP.OnionPacket nonce $ Addressed (nodeAddr $ routeNodeB r) fwd
105 (nodeAddr ni) 110
106 (NotForwarded msg) 111wrapIndirectHops :: TransportCrypto
107 return $ Right OnionRequest 112 -> OnionMessage Encrypted
108 { onionNonce = nonce 113 -> NodeInfo
109 , onionForward = fwd 114 -> OnionRoute
110 , pathFromOwner = NoReturnPath 115 -> (Nonce24 -> SockAddr -> Forwarding N2 (OnionMessage Encrypted) -> IO a)
111 } 116 -> IO a
112wrapForRoute crypto msg ni r@OnionRoute{routeRelayPort = Just tcpport} = do 117wrapIndirectHops crypto msg ni r fin = do
113 let nonce = msgNonce msg 118 let nonce = msgNonce msg
114 fwd <- wrapOnion crypto (routeAliasB r) 119 fwd <- wrapOnion crypto (routeAliasB r)
115 nonce 120 nonce
@@ -120,7 +125,7 @@ wrapForRoute crypto msg ni r@OnionRoute{routeRelayPort = Just tcpport} = do
120 (id2key . nodeId $ routeNodeC r) 125 (id2key . nodeId $ routeNodeC r)
121 (nodeAddr ni) 126 (nodeAddr ni)
122 (NotForwarded msg) 127 (NotForwarded msg)
123 return $ Left $ TCP.OnionPacket nonce $ Addressed (nodeAddr $ routeNodeB r) fwd 128 fin nonce (nodeAddr $ routeNodeB r) fwd
124 129
125unwrapAnnounceResponse :: Maybe NodeId -> NodeInfo -> AnnounceResponse -> ([NodeInfo], [Rendezvous], Maybe Nonce32) 130unwrapAnnounceResponse :: Maybe NodeId -> NodeInfo -> AnnounceResponse -> ([NodeInfo], [Rendezvous], Maybe Nonce32)
126unwrapAnnounceResponse alias ni (AnnounceResponse is_stored (SendNodes ns0)) | let ns = map udpNodeInfo ns0 131unwrapAnnounceResponse alias ni (AnnounceResponse is_stored (SendNodes ns0)) | let ns = map udpNodeInfo ns0