2017-11-02 11:58:02 +01:00
|
|
|
// Copyright 2017 Parity Technologies (UK) Ltd.
|
2018-03-07 16:20:55 +01:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
2017-11-02 11:58:02 +01:00
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
2018-03-07 16:20:55 +01:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
2017-11-02 11:58:02 +01:00
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
2018-03-07 16:20:55 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
2017-11-02 11:58:02 +01:00
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2017-12-04 16:05:37 +01:00
|
|
|
// TODO: use this once stable ; for now we just copy-paste the content of the README.md
|
|
|
|
//#![doc(include = "../README.md")]
|
|
|
|
|
2017-11-02 11:58:02 +01:00
|
|
|
//! Implementation of the libp2p `Transport` trait for TCP/IP.
|
2017-12-04 16:05:37 +01:00
|
|
|
//!
|
|
|
|
//! Uses [the *tokio* library](https://tokio.rs).
|
2018-03-07 16:20:55 +01:00
|
|
|
//!
|
2017-12-04 16:05:37 +01:00
|
|
|
//! # Usage
|
2018-03-07 16:20:55 +01:00
|
|
|
//!
|
2017-12-04 16:05:37 +01:00
|
|
|
//! Create [a tokio `Core`](https://docs.rs/tokio-core/0.1/tokio_core/reactor/struct.Core.html),
|
|
|
|
//! then grab a handle by calling the `handle()` method on it, then create a `TcpConfig` and pass
|
|
|
|
//! the handle.
|
|
|
|
//!
|
|
|
|
//! Example:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! extern crate libp2p_tcp_transport;
|
|
|
|
//! extern crate tokio_core;
|
|
|
|
//!
|
|
|
|
//! use libp2p_tcp_transport::TcpConfig;
|
|
|
|
//! use tokio_core::reactor::Core;
|
|
|
|
//!
|
|
|
|
//! # fn main() {
|
|
|
|
//! let mut core = Core::new().unwrap();
|
|
|
|
//! let tcp = TcpConfig::new(core.handle());
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The `TcpConfig` structs implements the `Transport` trait of the `swarm` library. See the
|
|
|
|
//! documentation of `swarm` and of libp2p in general to learn how to use the `Transport` trait.
|
2017-11-02 11:58:02 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
extern crate futures;
|
2018-05-16 12:59:36 +02:00
|
|
|
extern crate libp2p_core as swarm;
|
2018-03-15 16:08:49 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2018-03-07 16:20:55 +01:00
|
|
|
extern crate multiaddr;
|
2017-09-18 16:52:51 +02:00
|
|
|
extern crate tokio_core;
|
|
|
|
extern crate tokio_io;
|
|
|
|
|
2018-06-19 14:38:55 +02:00
|
|
|
use futures::future::{self, Future, FutureResult};
|
2017-09-18 16:52:51 +02:00
|
|
|
use futures::stream::Stream;
|
2018-03-07 16:20:55 +01:00
|
|
|
use multiaddr::{AddrComponent, Multiaddr, ToMultiaddr};
|
2018-05-14 15:55:16 +02:00
|
|
|
use std::io::Error as IoError;
|
|
|
|
use std::iter;
|
|
|
|
use std::net::SocketAddr;
|
2017-11-02 11:58:02 +01:00
|
|
|
use swarm::Transport;
|
2018-05-14 15:55:16 +02:00
|
|
|
use tokio_core::net::{TcpListener, TcpStream};
|
|
|
|
use tokio_core::reactor::Handle;
|
2017-09-18 16:52:51 +02:00
|
|
|
|
2017-12-04 16:05:37 +01:00
|
|
|
/// Represents the configuration for a TCP/IP transport capability for libp2p.
|
2017-11-02 11:58:02 +01:00
|
|
|
///
|
2017-12-04 16:05:37 +01:00
|
|
|
/// Each connection created by this config is tied to a tokio reactor. The TCP sockets created by
|
|
|
|
/// libp2p will need to be progressed by running the futures and streams obtained by libp2p
|
|
|
|
/// through the tokio reactor.
|
2017-11-02 11:58:02 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2017-12-04 16:05:37 +01:00
|
|
|
pub struct TcpConfig {
|
2017-11-02 11:58:02 +01:00
|
|
|
event_loop: Handle,
|
2017-09-18 17:25:04 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 16:05:37 +01:00
|
|
|
impl TcpConfig {
|
|
|
|
/// Creates a new configuration object for TCP/IP. The `Handle` is a tokio reactor the
|
|
|
|
/// connections will be created with.
|
|
|
|
#[inline]
|
|
|
|
pub fn new(handle: Handle) -> TcpConfig {
|
|
|
|
TcpConfig { event_loop: handle }
|
2017-10-23 11:45:35 +02:00
|
|
|
}
|
2017-09-18 17:25:04 +02:00
|
|
|
}
|
2017-09-18 16:52:51 +02:00
|
|
|
|
2017-12-04 16:05:37 +01:00
|
|
|
impl Transport for TcpConfig {
|
2018-05-14 15:55:16 +02:00
|
|
|
type Output = TcpStream;
|
2018-03-07 10:49:11 +01:00
|
|
|
type Listener = Box<Stream<Item = Self::ListenerUpgrade, Error = IoError>>;
|
2018-06-19 14:38:55 +02:00
|
|
|
type ListenerUpgrade = FutureResult<(Self::Output, Self::MultiaddrFuture), IoError>;
|
|
|
|
type MultiaddrFuture = FutureResult<Multiaddr, IoError>;
|
|
|
|
type Dial = Box<Future<Item = (TcpStream, Self::MultiaddrFuture), Error = IoError>>;
|
2017-10-23 11:45:35 +02:00
|
|
|
|
|
|
|
/// Listen on the given multi-addr.
|
|
|
|
/// Returns the address back if it isn't supported.
|
2017-11-24 16:10:34 +01:00
|
|
|
fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> {
|
2017-10-23 11:45:35 +02:00
|
|
|
if let Ok(socket_addr) = multiaddr_to_socketaddr(&addr) {
|
2017-11-24 16:10:34 +01:00
|
|
|
let listener = TcpListener::bind(&socket_addr, &self.event_loop);
|
|
|
|
// We need to build the `Multiaddr` to return from this function. If an error happened,
|
|
|
|
// just return the original multiaddr.
|
|
|
|
let new_addr = match listener {
|
|
|
|
Ok(ref l) => if let Ok(new_s_addr) = l.local_addr() {
|
2018-03-07 16:20:55 +01:00
|
|
|
new_s_addr.to_multiaddr().expect(
|
|
|
|
"multiaddr generated from socket addr is \
|
|
|
|
always valid",
|
|
|
|
)
|
2017-11-24 16:10:34 +01:00
|
|
|
} else {
|
|
|
|
addr
|
2018-03-07 16:20:55 +01:00
|
|
|
},
|
2017-11-24 16:10:34 +01:00
|
|
|
Err(_) => addr,
|
|
|
|
};
|
2017-12-19 18:09:17 +01:00
|
|
|
|
2018-05-17 15:14:13 +02:00
|
|
|
debug!("Now listening on {}", new_addr);
|
2018-03-15 16:08:49 +01:00
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
let future = future::result(listener)
|
|
|
|
.map(|listener| {
|
2017-10-23 11:45:35 +02:00
|
|
|
// Pull out a stream of sockets for incoming connections
|
2017-11-28 12:20:28 +01:00
|
|
|
listener.incoming().map(|(sock, addr)| {
|
|
|
|
let addr = addr.to_multiaddr()
|
|
|
|
.expect("generating a multiaddr from a socket addr never fails");
|
2018-05-17 15:14:13 +02:00
|
|
|
debug!("Incoming connection from {}", addr);
|
2018-06-19 14:38:55 +02:00
|
|
|
future::ok((sock, future::ok(addr)))
|
2017-11-28 12:20:28 +01:00
|
|
|
})
|
2017-10-23 11:45:35 +02:00
|
|
|
})
|
2018-03-07 16:20:55 +01:00
|
|
|
.flatten_stream();
|
2017-11-24 16:10:34 +01:00
|
|
|
Ok((Box::new(future), new_addr))
|
2017-10-23 11:45:35 +02:00
|
|
|
} else {
|
2017-11-02 11:58:02 +01:00
|
|
|
Err((self, addr))
|
2017-10-23 11:45:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dial to the given multi-addr.
|
|
|
|
/// Returns either a future which may resolve to a connection,
|
|
|
|
/// or gives back the multiaddress.
|
2017-11-02 11:58:02 +01:00
|
|
|
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, (Self, Multiaddr)> {
|
2017-10-23 11:45:35 +02:00
|
|
|
if let Ok(socket_addr) = multiaddr_to_socketaddr(&addr) {
|
2018-05-17 15:14:13 +02:00
|
|
|
debug!("Dialing {}", addr);
|
2018-06-19 14:38:55 +02:00
|
|
|
let fut = TcpStream::connect(&socket_addr, &self.event_loop).map(|t| (t, future::ok(addr)));
|
2018-03-07 10:49:11 +01:00
|
|
|
Ok(Box::new(fut) as Box<_>)
|
2017-10-23 11:45:35 +02:00
|
|
|
} else {
|
2017-11-02 11:58:02 +01:00
|
|
|
Err((self, addr))
|
2017-10-23 11:45:35 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-08 13:50:16 +01:00
|
|
|
|
|
|
|
fn nat_traversal(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
|
|
|
|
let server_protocols: Vec<_> = server.iter().collect();
|
|
|
|
let observed_protocols: Vec<_> = observed.iter().collect();
|
|
|
|
|
|
|
|
if server_protocols.len() != 2 || observed_protocols.len() != 2 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that `server` is a valid TCP/IP address.
|
|
|
|
match (&server_protocols[0], &server_protocols[1]) {
|
2018-03-07 16:20:55 +01:00
|
|
|
(&AddrComponent::IP4(_), &AddrComponent::TCP(_))
|
|
|
|
| (&AddrComponent::IP6(_), &AddrComponent::TCP(_)) => {}
|
2018-02-08 13:50:16 +01:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that `observed` is a valid TCP/IP address.
|
|
|
|
match (&observed_protocols[0], &observed_protocols[1]) {
|
2018-03-07 16:20:55 +01:00
|
|
|
(&AddrComponent::IP4(_), &AddrComponent::TCP(_))
|
|
|
|
| (&AddrComponent::IP6(_), &AddrComponent::TCP(_)) => {}
|
2018-02-08 13:50:16 +01:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = iter::once(observed_protocols[0].clone())
|
|
|
|
.chain(iter::once(server_protocols[1].clone()))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Some(result)
|
|
|
|
}
|
2017-09-18 16:52:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This type of logic should probably be moved into the multiaddr package
|
2017-11-24 16:10:34 +01:00
|
|
|
fn multiaddr_to_socketaddr(addr: &Multiaddr) -> Result<SocketAddr, ()> {
|
2017-12-15 17:29:54 +01:00
|
|
|
let protocols: Vec<_> = addr.iter().collect();
|
2017-10-23 11:45:35 +02:00
|
|
|
|
2017-12-15 17:29:54 +01:00
|
|
|
if protocols.len() != 2 {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
match (&protocols[0], &protocols[1]) {
|
|
|
|
(&AddrComponent::IP4(ref ip), &AddrComponent::TCP(port)) => {
|
|
|
|
Ok(SocketAddr::new(ip.clone().into(), port))
|
2017-10-23 11:45:35 +02:00
|
|
|
}
|
2017-12-15 17:29:54 +01:00
|
|
|
(&AddrComponent::IP6(ref ip), &AddrComponent::TCP(port)) => {
|
|
|
|
Ok(SocketAddr::new(ip.clone().into(), port))
|
2017-10-23 11:45:35 +02:00
|
|
|
}
|
2017-11-24 16:10:34 +01:00
|
|
|
_ => Err(()),
|
2017-10-23 11:45:35 +02:00
|
|
|
}
|
2017-09-18 16:52:51 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 15:55:57 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-03-07 16:20:55 +01:00
|
|
|
use super::{multiaddr_to_socketaddr, TcpConfig};
|
2017-10-23 11:45:35 +02:00
|
|
|
use futures::Future;
|
|
|
|
use futures::stream::Stream;
|
|
|
|
use multiaddr::Multiaddr;
|
2018-05-14 15:55:16 +02:00
|
|
|
use std;
|
|
|
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
2017-11-02 11:58:02 +01:00
|
|
|
use swarm::Transport;
|
2018-05-14 15:55:16 +02:00
|
|
|
use tokio_core::reactor::Core;
|
|
|
|
use tokio_io;
|
2017-10-23 11:45:35 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiaddr_to_tcp_conversion() {
|
|
|
|
use std::net::Ipv6Addr;
|
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
assert!(
|
|
|
|
multiaddr_to_socketaddr(&"/ip4/127.0.0.1/udp/1234".parse::<Multiaddr>().unwrap())
|
|
|
|
.is_err()
|
|
|
|
);
|
2017-11-16 23:59:38 +08:00
|
|
|
|
2017-10-23 11:45:35 +02:00
|
|
|
assert_eq!(
|
2017-12-28 18:07:49 +01:00
|
|
|
multiaddr_to_socketaddr(&"/ip4/127.0.0.1/tcp/12345".parse::<Multiaddr>().unwrap()),
|
2017-10-23 11:45:35 +02:00
|
|
|
Ok(SocketAddr::new(
|
|
|
|
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
|
|
|
|
12345,
|
|
|
|
))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2018-03-07 16:20:55 +01:00
|
|
|
multiaddr_to_socketaddr(&"/ip4/255.255.255.255/tcp/8080"
|
|
|
|
.parse::<Multiaddr>()
|
|
|
|
.unwrap()),
|
2017-10-23 11:45:35 +02:00
|
|
|
Ok(SocketAddr::new(
|
|
|
|
IpAddr::V4(Ipv4Addr::new(255, 255, 255, 255)),
|
|
|
|
8080,
|
|
|
|
))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2017-12-28 18:07:49 +01:00
|
|
|
multiaddr_to_socketaddr(&"/ip6/::1/tcp/12345".parse::<Multiaddr>().unwrap()),
|
2017-10-23 11:45:35 +02:00
|
|
|
Ok(SocketAddr::new(
|
|
|
|
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
|
|
|
|
12345,
|
|
|
|
))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2017-12-28 18:07:49 +01:00
|
|
|
multiaddr_to_socketaddr(&"/ip6/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/tcp/8080"
|
2018-03-07 16:20:55 +01:00
|
|
|
.parse::<Multiaddr>()
|
|
|
|
.unwrap()),
|
2017-10-23 11:45:35 +02:00
|
|
|
Ok(SocketAddr::new(
|
|
|
|
IpAddr::V6(Ipv6Addr::new(
|
|
|
|
65535,
|
|
|
|
65535,
|
|
|
|
65535,
|
|
|
|
65535,
|
|
|
|
65535,
|
|
|
|
65535,
|
|
|
|
65535,
|
|
|
|
65535,
|
|
|
|
)),
|
|
|
|
8080,
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn communicating_between_dialer_and_listener() {
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
std::thread::spawn(move || {
|
2017-11-02 11:58:02 +01:00
|
|
|
let mut core = Core::new().unwrap();
|
2017-12-28 18:07:49 +01:00
|
|
|
let addr = "/ip4/127.0.0.1/tcp/12345".parse::<Multiaddr>().unwrap();
|
2017-12-04 16:05:37 +01:00
|
|
|
let tcp = TcpConfig::new(core.handle());
|
2017-11-02 11:58:02 +01:00
|
|
|
let handle = core.handle();
|
2018-03-07 10:49:11 +01:00
|
|
|
let listener = tcp.listen_on(addr).unwrap().0.for_each(|sock| {
|
|
|
|
sock.and_then(|(sock, _)| {
|
2017-12-19 18:09:17 +01:00
|
|
|
// Define what to do with the socket that just connected to us
|
|
|
|
// Which in this case is read 3 bytes
|
|
|
|
let handle_conn = tokio_io::io::read_exact(sock, [0; 3])
|
|
|
|
.map(|(_, buf)| assert_eq!(buf, [1, 2, 3]))
|
|
|
|
.map_err(|err| panic!("IO error {:?}", err));
|
2017-10-23 11:45:35 +02:00
|
|
|
|
2017-12-19 18:09:17 +01:00
|
|
|
// Spawn the future as a concurrent task
|
|
|
|
handle.spawn(handle_conn);
|
2017-10-23 11:45:35 +02:00
|
|
|
|
2017-12-19 18:09:17 +01:00
|
|
|
Ok(())
|
|
|
|
})
|
2017-10-23 11:45:35 +02:00
|
|
|
});
|
|
|
|
|
2017-11-02 11:58:02 +01:00
|
|
|
core.run(listener).unwrap();
|
2017-10-23 11:45:35 +02:00
|
|
|
});
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(100));
|
2017-12-28 18:07:49 +01:00
|
|
|
let addr = "/ip4/127.0.0.1/tcp/12345".parse::<Multiaddr>().unwrap();
|
2017-11-02 11:58:02 +01:00
|
|
|
let mut core = Core::new().unwrap();
|
2017-12-04 16:05:37 +01:00
|
|
|
let tcp = TcpConfig::new(core.handle());
|
2017-10-23 11:45:35 +02:00
|
|
|
// Obtain a future socket through dialing
|
|
|
|
let socket = tcp.dial(addr.clone()).unwrap();
|
|
|
|
// Define what to do with the socket once it's obtained
|
|
|
|
let action = socket.then(|sock| match sock {
|
2018-03-07 10:49:11 +01:00
|
|
|
Ok((mut s, _)) => {
|
2017-10-23 11:45:35 +02:00
|
|
|
let written = s.write(&[0x1, 0x2, 0x3]).unwrap();
|
|
|
|
Ok(written)
|
|
|
|
}
|
|
|
|
Err(x) => Err(x),
|
|
|
|
});
|
|
|
|
// Execute the future in our event loop
|
2017-11-02 11:58:02 +01:00
|
|
|
core.run(action).unwrap();
|
2017-10-23 11:45:35 +02:00
|
|
|
std::thread::sleep(std::time::Duration::from_millis(100));
|
|
|
|
}
|
2017-11-24 16:10:34 +01:00
|
|
|
|
|
|
|
#[test]
|
2018-01-02 16:00:08 +01:00
|
|
|
fn replace_port_0_in_returned_multiaddr_ipv4() {
|
2017-11-24 16:10:34 +01:00
|
|
|
let core = Core::new().unwrap();
|
2017-12-04 16:05:37 +01:00
|
|
|
let tcp = TcpConfig::new(core.handle());
|
2017-11-24 16:10:34 +01:00
|
|
|
|
2017-12-28 18:07:49 +01:00
|
|
|
let addr = "/ip4/127.0.0.1/tcp/0".parse::<Multiaddr>().unwrap();
|
2017-11-24 16:10:34 +01:00
|
|
|
assert!(addr.to_string().contains("tcp/0"));
|
|
|
|
|
|
|
|
let (_, new_addr) = tcp.listen_on(addr).unwrap();
|
|
|
|
assert!(!new_addr.to_string().contains("tcp/0"));
|
|
|
|
}
|
2017-12-15 17:29:54 +01:00
|
|
|
|
2018-01-02 16:00:08 +01:00
|
|
|
#[test]
|
|
|
|
fn replace_port_0_in_returned_multiaddr_ipv6() {
|
|
|
|
let core = Core::new().unwrap();
|
|
|
|
let tcp = TcpConfig::new(core.handle());
|
|
|
|
|
2017-12-28 18:07:49 +01:00
|
|
|
let addr: Multiaddr = "/ip6/::1/tcp/0".parse().unwrap();
|
2018-01-02 16:00:08 +01:00
|
|
|
assert!(addr.to_string().contains("tcp/0"));
|
|
|
|
|
|
|
|
let (_, new_addr) = tcp.listen_on(addr).unwrap();
|
|
|
|
assert!(!new_addr.to_string().contains("tcp/0"));
|
|
|
|
}
|
|
|
|
|
2017-12-15 17:29:54 +01:00
|
|
|
#[test]
|
|
|
|
fn larger_addr_denied() {
|
|
|
|
let core = Core::new().unwrap();
|
|
|
|
let tcp = TcpConfig::new(core.handle());
|
|
|
|
|
2018-03-07 16:20:55 +01:00
|
|
|
let addr = "/ip4/127.0.0.1/tcp/12345/tcp/12345"
|
|
|
|
.parse::<Multiaddr>()
|
|
|
|
.unwrap();
|
2017-12-15 17:29:54 +01:00
|
|
|
assert!(tcp.listen_on(addr).is_err());
|
|
|
|
}
|
2018-02-08 13:50:16 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nat_traversal() {
|
|
|
|
let core = Core::new().unwrap();
|
|
|
|
let tcp = TcpConfig::new(core.handle());
|
|
|
|
|
|
|
|
let server = "/ip4/127.0.0.1/tcp/10000".parse::<Multiaddr>().unwrap();
|
|
|
|
let observed = "/ip4/80.81.82.83/tcp/25000".parse::<Multiaddr>().unwrap();
|
|
|
|
|
|
|
|
let out = tcp.nat_traversal(&server, &observed);
|
2018-03-07 16:20:55 +01:00
|
|
|
assert_eq!(
|
|
|
|
out.unwrap(),
|
|
|
|
"/ip4/80.81.82.83/tcp/10000".parse::<Multiaddr>().unwrap()
|
|
|
|
);
|
2018-02-08 13:50:16 +01:00
|
|
|
}
|
2017-09-18 16:52:51 +02:00
|
|
|
}
|