diff options
author | Joe Crayne <joe@jerkface.net> | 2019-11-28 12:05:47 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 22:50:28 -0500 |
commit | 62be467e38b5919baeed90784ac1b62a3e256649 (patch) | |
tree | 13ea603a52e1c4972fedda18a306da0c27ae63af /dht/src/Data/Tox/Onion.hs | |
parent | 3ebd7ae11d7a86798b31bdb17af9797ba5e09f1d (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.hs | 28 |
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 @@ | |||
19 | module Data.Tox.Onion where | 19 | module Data.Tox.Onion where |
20 | 20 | ||
21 | 21 | ||
22 | import Network.Address (fromSockAddr,toSockAddr,setPort,either4or6,sockAddrPort) | 22 | import Network.Address (fromSockAddr,toSockAddr,setPort,either4or6,sockAddrPort,localhost4,localhost6) |
23 | import Network.QueryResponse | 23 | import Network.QueryResponse |
24 | import Crypto.Tox hiding (encrypt,decrypt) | 24 | import Crypto.Tox hiding (encrypt,decrypt) |
25 | import Network.Tox.NodeId | 25 | import Network.Tox.NodeId |
@@ -57,6 +57,7 @@ import DebugTag | |||
57 | import Data.Word64Map (fitsInInt) | 57 | import Data.Word64Map (fitsInInt) |
58 | import Data.Bits (shiftR,shiftL) | 58 | import Data.Bits (shiftR,shiftL) |
59 | import qualified Rank2 | 59 | import qualified Rank2 |
60 | import Util (sameAddress) | ||
60 | 61 | ||
61 | type HandleLo a = Maybe (Either String (ByteString, SockAddr)) -> IO a | 62 | type HandleLo a = Maybe (Either String (ByteString, SockAddr)) -> IO a |
62 | 63 | ||
@@ -239,11 +240,30 @@ routeId :: NodeId -> RouteId | |||
239 | routeId nid = RouteId $ mod (hash nid) 12 | 240 | routeId nid = RouteId $ mod (hash nid) 12 |
240 | 241 | ||
241 | 242 | ||
243 | substituteLoopback :: SockAddr -- ^ UDP bind address | ||
244 | -> SockAddr -- ^ Logical destination address. | ||
245 | -> SockAddr -- ^ Destination address unless localhost, then bind address. | ||
246 | substituteLoopback (SockAddrInet 0 _ ) saddr = saddr | ||
247 | substituteLoopback (SockAddrInet6 _ _ (0,0,0,0) _) saddr = saddr | ||
248 | substituteLoopback 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 | ||
243 | forwardOnions :: TransportCrypto -> UDPTransport -> (Int -> OnionMessage Encrypted -> IO ()) {- ^ TCP relay send -} -> UDPTransport | 253 | handleLoopback :: SockAddr -> UDPTransport -> UDPTransport |
244 | forwardOnions crypto udp sendTCP = udp { awaitMessage = forwardAwait crypto udp sendTCP } | 254 | handleLoopback baddr udp = udp |
255 | { sendMessage = \a x -> sendMessage udp (substituteLoopback baddr a) x | ||
256 | } | ||
257 | |||
258 | forwardOnions :: TransportCrypto | ||
259 | -> SockAddr -- UDP bind address | ||
260 | -> UDPTransport | ||
261 | -> (Int -> OnionMessage Encrypted -> IO ()) {- ^ TCP relay send -} -> UDPTransport | ||
262 | forwardOnions crypto baddr udp sendTCP = udp { awaitMessage = forwardAwait crypto (handleLoopback baddr udp) sendTCP } | ||
245 | 263 | ||
246 | forwardAwait :: TransportCrypto -> UDPTransport -> (Int -> OnionMessage Encrypted -> IO ()) {- ^ TCP relay send -} -> HandleLo a -> IO a | 264 | forwardAwait :: TransportCrypto |
265 | -> UDPTransport | ||
266 | -> (Int -> OnionMessage Encrypted -> IO ()) {- ^ TCP relay send -} -> HandleLo a -> IO a | ||
247 | forwardAwait crypto udp sendTCP kont = do | 267 | forwardAwait crypto udp sendTCP kont = do |
248 | fix $ \another -> do | 268 | fix $ \another -> do |
249 | awaitMessage udp $ \case | 269 | awaitMessage udp $ \case |