mwc_libp2p_core/
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 multiaddr::{Multiaddr, Protocol};
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.
38pub fn address_translation(original: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
39    original.replace(0, move |proto| match proto {
40        Protocol::Ip4(_)
41        | Protocol::Ip6(_)
42        | Protocol::Dns(_)
43        | Protocol::Dns4(_)
44        | Protocol::Dns6(_) => match observed.iter().next() {
45            x @ Some(Protocol::Ip4(_)) => x,
46            x @ Some(Protocol::Ip6(_)) => x,
47            x @ Some(Protocol::Dns(_)) => x,
48            x @ Some(Protocol::Dns4(_)) => x,
49            x @ Some(Protocol::Dns6(_)) => x,
50            _ => None,
51        },
52        _ => None,
53    })
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_address_translation() {
62        struct Test {
63            original: Multiaddr,
64            observed: Multiaddr,
65            expected: Multiaddr,
66        }
67
68        let tests = vec![
69            // Basic ipv4.
70            Test {
71                original: "/ip4/192.0.2.1/tcp/1".parse().unwrap(),
72                observed: "/ip4/192.0.2.2/tcp/2".parse().unwrap(),
73                expected: "/ip4/192.0.2.2/tcp/1".parse().unwrap(),
74            },
75            // Basic ipv6.
76            Test {
77                original: "/ip6/2001:db8:0:0:0:0:0:0/tcp/1".parse().unwrap(),
78                observed: "/ip6/2001:db8:0:0:0:0:0:1/tcp/2".parse().unwrap(),
79                expected: "/ip6/2001:db8:0:0:0:0:0:1/tcp/1".parse().unwrap(),
80            },
81            // Ipv4  ipv6 mix.
82            Test {
83                original: "/ip4/192.0.2.1/tcp/1".parse().unwrap(),
84                observed: "/ip6/2001:db8:0:0:0:0:0:1/tcp/2".parse().unwrap(),
85                expected: "/ip6/2001:db8:0:0:0:0:0:1/tcp/1".parse().unwrap(),
86            },
87            // Ipv6  ipv4 mix.
88            Test {
89                original: "/ip6/2001:db8:0:0:0:0:0:0/tcp/1".parse().unwrap(),
90                observed: "/ip4/192.0.2.2/tcp/2".parse().unwrap(),
91                expected: "/ip4/192.0.2.2/tcp/1".parse().unwrap(),
92            },
93            // Dns.
94            Test {
95                original: "/dns4/foo/tcp/1".parse().unwrap(),
96                observed: "/dns4/bar/tcp/2".parse().unwrap(),
97                expected: "/dns4/bar/tcp/1".parse().unwrap(),
98            },
99            // Ipv4 Dns mix.
100            Test {
101                original: "/ip4/192.0.2.1/tcp/1".parse().unwrap(),
102                observed: "/dns4/bar/tcp/2".parse().unwrap(),
103                expected: "/dns4/bar/tcp/1".parse().unwrap(),
104            },
105        ];
106
107        for test in tests.iter() {
108            assert_eq!(
109                address_translation(&test.original, &test.observed),
110                Some(test.expected.clone())
111            );
112        }
113    }
114}