libp2p_swarm/
translation.rs

1// Copyright 2019 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use libp2p_core::{multiaddr::Protocol, Multiaddr};
22
23/// Perform IP address translation.
24///
25/// Given an `original` [`Multiaddr`] and some `observed` [`Multiaddr`], replace the first protocol
26/// of the `original` with the first protocol of the `observed` [`Multiaddr`] and return this
27/// translated [`Multiaddr`].
28///
29/// This function can for example be useful when handling tcp connections. Tcp does not listen and
30/// dial on the same port by default. Thus when receiving an observed address on a connection that
31/// we initiated, it will contain our dialing port, not our listening port. We need to take the ip
32/// address or dns address from the observed address and the port from the original address.
33///
34/// This is a mixed-mode translation, i.e. an IPv4 / DNS4 address may be replaced by an IPv6 / DNS6
35/// address and vice versa.
36///
37/// If the first [`Protocol`]s are not IP addresses, `None` is returned instead.
38#[doc(hidden)]
39pub fn _address_translation(original: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
40    original.replace(0, move |proto| match proto {
41        Protocol::Ip4(_)
42        | Protocol::Ip6(_)
43        | Protocol::Dns(_)
44        | Protocol::Dns4(_)
45        | Protocol::Dns6(_) => match observed.iter().next() {
46            x @ Some(Protocol::Ip4(_)) => x,
47            x @ Some(Protocol::Ip6(_)) => x,
48            x @ Some(Protocol::Dns(_)) => x,
49            x @ Some(Protocol::Dns4(_)) => x,
50            x @ Some(Protocol::Dns6(_)) => x,
51            _ => None,
52        },
53        _ => None,
54    })
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_address_translation() {
63        struct Test {
64            original: Multiaddr,
65            observed: Multiaddr,
66            expected: Multiaddr,
67        }
68
69        let tests = [
70            // Basic ipv4.
71            Test {
72                original: "/ip4/192.0.2.1/tcp/1".parse().unwrap(),
73                observed: "/ip4/192.0.2.2/tcp/2".parse().unwrap(),
74                expected: "/ip4/192.0.2.2/tcp/1".parse().unwrap(),
75            },
76            // Basic ipv6.
77            Test {
78                original: "/ip6/2001:db8:0:0:0:0:0:0/tcp/1".parse().unwrap(),
79                observed: "/ip6/2001:db8:0:0:0:0:0:1/tcp/2".parse().unwrap(),
80                expected: "/ip6/2001:db8:0:0:0:0:0:1/tcp/1".parse().unwrap(),
81            },
82            // Ipv4  ipv6 mix.
83            Test {
84                original: "/ip4/192.0.2.1/tcp/1".parse().unwrap(),
85                observed: "/ip6/2001:db8:0:0:0:0:0:1/tcp/2".parse().unwrap(),
86                expected: "/ip6/2001:db8:0:0:0:0:0:1/tcp/1".parse().unwrap(),
87            },
88            // Ipv6  ipv4 mix.
89            Test {
90                original: "/ip6/2001:db8:0:0:0:0:0:0/tcp/1".parse().unwrap(),
91                observed: "/ip4/192.0.2.2/tcp/2".parse().unwrap(),
92                expected: "/ip4/192.0.2.2/tcp/1".parse().unwrap(),
93            },
94            // Dns.
95            Test {
96                original: "/dns4/foo/tcp/1".parse().unwrap(),
97                observed: "/dns4/bar/tcp/2".parse().unwrap(),
98                expected: "/dns4/bar/tcp/1".parse().unwrap(),
99            },
100            // Ipv4 Dns mix.
101            Test {
102                original: "/ip4/192.0.2.1/tcp/1".parse().unwrap(),
103                observed: "/dns4/bar/tcp/2".parse().unwrap(),
104                expected: "/dns4/bar/tcp/1".parse().unwrap(),
105            },
106        ];
107
108        for test in tests.iter() {
109            assert_eq!(
110                _address_translation(&test.original, &test.observed),
111                Some(test.expected.clone())
112            );
113        }
114    }
115}