summaryrefslogtreecommitdiff
path: root/dht/src/Data/Tox/Onion.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-11-28 12:05:47 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-01 22:50:28 -0500
commit62be467e38b5919baeed90784ac1b62a3e256649 (patch)
tree13ea603a52e1c4972fedda18a306da0c27ae63af /dht/src/Data/Tox/Onion.hs
parent3ebd7ae11d7a86798b31bdb17af9797ba5e09f1d (diff)
* Fixed accidental usage of TCP rather than UDP port.
* Onion: special handling for localhost addresses.
Diffstat (limited to 'dht/src/Data/Tox/Onion.hs')
-rw-r--r--dht/src/Data/Tox/Onion.hs28
1 files changed, 24 insertions, 4 deletions
diff --git a/dht/src/Data/Tox/Onion.hs b/dht/src/Data/Tox/Onion.hs
index 0338111b..258a9f73 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) 22import Network.Address (fromSockAddr,toSockAddr,setPort,either4or6,sockAddrPort,localhost4,localhost6)
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
@@ -57,6 +57,7 @@ import DebugTag
57import Data.Word64Map (fitsInInt) 57import Data.Word64Map (fitsInInt)
58import Data.Bits (shiftR,shiftL) 58import Data.Bits (shiftR,shiftL)
59import qualified Rank2 59import qualified Rank2
60import Util (sameAddress)
60 61
61type HandleLo a = Maybe (Either String (ByteString, SockAddr)) -> IO a 62type HandleLo a = Maybe (Either String (ByteString, SockAddr)) -> IO a
62 63
@@ -239,11 +240,30 @@ routeId :: NodeId -> RouteId
239routeId nid = RouteId $ mod (hash nid) 12 240routeId nid = RouteId $ mod (hash nid) 12
240 241
241 242
243substituteLoopback :: SockAddr -- ^ UDP bind address
244 -> SockAddr -- ^ Logical destination address.
245 -> SockAddr -- ^ Destination address unless localhost, then bind address.
246substituteLoopback (SockAddrInet 0 _ ) saddr = saddr
247substituteLoopback (SockAddrInet6 _ _ (0,0,0,0) _) saddr = saddr
248substituteLoopback baddr saddr =
249 case either4or6 saddr of
250 Left s -> if sameAddress s localhost4 then baddr else saddr
251 Right s -> if sameAddress s localhost6 then baddr else saddr
242 252
243forwardOnions :: TransportCrypto -> UDPTransport -> (Int -> OnionMessage Encrypted -> IO ()) {- ^ TCP relay send -} -> UDPTransport 253handleLoopback :: SockAddr -> UDPTransport -> UDPTransport
244forwardOnions crypto udp sendTCP = udp { awaitMessage = forwardAwait crypto udp sendTCP } 254handleLoopback baddr udp = udp
255 { sendMessage = \a x -> sendMessage udp (substituteLoopback baddr a) x
256 }
257
258forwardOnions :: TransportCrypto
259 -> SockAddr -- UDP bind address
260 -> UDPTransport
261 -> (Int -> OnionMessage Encrypted -> IO ()) {- ^ TCP relay send -} -> UDPTransport
262forwardOnions crypto baddr udp sendTCP = udp { awaitMessage = forwardAwait crypto (handleLoopback baddr udp) sendTCP }
245 263
246forwardAwait :: TransportCrypto -> UDPTransport -> (Int -> OnionMessage Encrypted -> IO ()) {- ^ TCP relay send -} -> HandleLo a -> IO a 264forwardAwait :: TransportCrypto
265 -> UDPTransport
266 -> (Int -> OnionMessage Encrypted -> IO ()) {- ^ TCP relay send -} -> HandleLo a -> IO a
247forwardAwait crypto udp sendTCP kont = do 267forwardAwait crypto udp sendTCP kont = do
248 fix $ \another -> do 268 fix $ \another -> do
249 awaitMessage udp $ \case 269 awaitMessage udp $ \case