Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Merge 30b4175 into 35e1600
Browse files Browse the repository at this point in the history
  • Loading branch information
canndrew committed Feb 11, 2016
2 parents 35e1600 + 30b4175 commit 0755a98
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 205 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "GPL-3.0"
name = "nat_traversal"
readme = "README.md"
repository = "https://github.com/maidsafe/nat_traversal"
version = "0.0.1"
version = "0.0.2"

[dependencies]
cbor = "~0.3.16"
Expand Down
31 changes: 0 additions & 31 deletions src/hole_punch_server_addr.rs

This file was deleted.

21 changes: 9 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,45 @@
// #![feature(plugin)]
// #![plugin(clippy)]

#![allow(unused, unused_extern_crates)]
// TODO(canndrew): Remove this once this: https://github.com/tailhook/quick-error/issues/18
// is fixed.
#![allow(missing_docs)]

extern crate cbor;
#[macro_use]
extern crate log;
extern crate net2;
extern crate rand;
extern crate rustc_serialize;
extern crate time;
extern crate crossbeam;
extern crate void;
#[macro_use]
extern crate maidsafe_utilities;
extern crate igd;
extern crate socket_addr;
extern crate get_if_addrs;
extern crate sodiumoxide;
extern crate libc;
//extern crate libc;
extern crate w_result;
extern crate ip;
#[allow(unused_extern_crates)] // Needed because the crate is only used for macros
#[macro_use]
extern crate quick_error;

pub use mapping_context::MappingContext;
pub use hole_punch_server_addr::HolePunchServerAddr;
pub use mapping_context::{MappingContext, MappingContextNewError, MappingContextNewWarning};
pub use mapped_socket_addr::MappedSocketAddr;
pub use rendezvous_info::{PrivRendezvousInfo, PubRendezvousInfo,
gen_rendezvous_info};
pub use mapped_udp_socket::MappedUdpSocket;
pub use mapped_udp_socket::{MappedUdpSocket, MappedUdpSocketMapError,
MappedUdpSocketMapWarning, MappedUdpSocketNewError};
pub use punched_udp_socket::PunchedUdpSocket;
pub use mapped_tcp_socket::{MappedTcpSocket, tcp_punch_hole};
pub use simple_udp_hole_punch_server::SimpleUdpHolePunchServer;
pub use simple_udp_hole_punch_server::{SimpleUdpHolePunchServer, SimpleUdpHolePunchServerNewError};

mod mapping_context;
mod hole_punch_server_addr;
mod mapped_socket_addr;
mod rendezvous_info;
mod mapped_udp_socket;
mod punched_udp_socket;
mod mapped_tcp_socket;
mod simple_udp_hole_punch_server;
mod periodic_sender;
mod socket_utils;
mod listener_message;

2 changes: 0 additions & 2 deletions src/listener_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

use sodiumoxide::crypto::sign::PublicKey;
use socket_addr::SocketAddr;


#[derive(RustcEncodable, RustcDecodable)]
pub enum ListenerRequest {
EchoExternalAddr,
Expand Down
10 changes: 5 additions & 5 deletions src/mapped_tcp_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ impl MappedTcpSocket {
/// Map an existing tcp socket. The socket must not bound or connected. This
/// function will set the options to make the socket address reuseable
/// before binding it.
pub fn map(socket: net2::TcpBuilder, mc: &MappingContext)
pub fn map(_socket: net2::TcpBuilder, _mc: &MappingContext)
-> MappedTcpSocket {
unimplemented!();
}

/// Create a new `MappedTcpSocket`
pub fn new(mc: &MappingContext) -> MappedTcpSocket {
pub fn new(_mc: &MappingContext) -> MappedTcpSocket {
unimplemented!();
}
}

/// Perform a tcp rendezvous connect. `socket` should have been obtained from a
/// `MappedTcpSocket`.
pub fn tcp_punch_hole(socket: net2::TcpBuilder,
our_priv_rendezvous_info: PrivRendezvousInfo,
their_pub_rendezvous_info: PubRendezvousInfo)
pub fn tcp_punch_hole(_socket: net2::TcpBuilder,
_our_priv_rendezvous_info: PrivRendezvousInfo,
_their_pub_rendezvous_info: PubRendezvousInfo)
-> TcpStream {
unimplemented!();
}
53 changes: 31 additions & 22 deletions src/mapped_udp_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ use std::collections::HashSet;

use igd;
use time;
use get_if_addrs;
use ip::{SocketAddrExt, IpAddr};
use maidsafe_utilities::serialisation::{deserialise, serialise};
use socket_addr::SocketAddr;
use w_result::{WResult, WOk, WErr};

use hole_punch_server_addr::HolePunchServerAddr;
use listener_message::{ListenerRequest, ListenerResponse};
use mapping_context;
use mapping_context::MappingContext;
use mapped_socket_addr::MappedSocketAddr;
use periodic_sender::PeriodicSender;
use socket_utils;
use socket_utils::RecvUntil;

Expand All @@ -54,7 +51,9 @@ quick_error! {
#[derive(Debug)]
pub enum MappedUdpSocketMapError {
/// Error getting the local address of the socket.
SocketLocalAddr { err: io::Error } {
SocketLocalAddr {
err: io::Error
} {
description("Error getting local address of socket \
(have you called bind() on the socket?)")
display("Error getting local address of socket. \
Expand All @@ -64,13 +63,17 @@ quick_error! {
cause(err)
}
/// IO error receiving data on the socket.
RecvError { err: io::Error } {
RecvError {
err: io::Error
} {
description("IO error receiving data on socket")
display("IO error receiving data on socket: {}", err)
cause(err)
}
/// IO error sending data on the socket.
SendError { err: io::Error } {
SendError {
err: io::Error
} {
description("IO error sending data on socket")
display("IO error sending data on socket: {}", err)
cause(err)
Expand All @@ -83,14 +86,17 @@ quick_error! {
#[derive(Debug)]
pub enum MappedUdpSocketMapWarning {
/// Error searching for IGD gateway
FindGateway { err: igd::SearchError } {
FindGateway {
err: igd::SearchError
} {
description("Error searching for IGD gateway")
display("Error searching for IGD gateway. \
igd::search_gateway_from_timeout returned an error: {}",
err)
cause(err)
}
/// Error mapping external address and port through IGD gateway
/// Error mapping external address and port through IGD gateway. `gateway_addr` is the
/// address of the IGD gateway that we requested a port mapping from.
GetExternalPort {
gateway_addr: net::SocketAddrV4,
err: igd::AddAnyPortError,
Expand All @@ -110,14 +116,18 @@ quick_error! {
#[derive(Debug)]
pub enum MappedUdpSocketNewError {
/// Error creating new udp socket bound to 0.0.0.0:0
CreateSocket { err: io::Error } {
CreateSocket {
err: io::Error
} {
description("Error creating a new udp socket bound to 0.0.0.0:0")
display("Error creating a new udp socket bound to 0.0.0.0:0. \
UdpSocket::bind returned an IO error: {}", err)
cause(err)
}
/// Error mapping udp socket.
MapSocket { err: MappedUdpSocketMapError } {
MapSocket {
err: MappedUdpSocketMapError
} {
description("Error mapping udp socket")
display("Error mapping udp socket. MappedUdpSocket::map returned \
an error: {}", err)
Expand Down Expand Up @@ -253,13 +263,12 @@ impl MappedUdpSocket {
let send_data = unwrap_result!(serialise(&ListenerRequest::EchoExternalAddr));
let mut simple_servers: HashSet<SocketAddr> = mapping_context::simple_servers(&mc)
.into_iter().collect();
let mut deadline = time::SteadyTime::now();

// Ping all the simple servers and waiting for a response.
// Run this loop at most 8 times for a maximum timeout of 250ms * 8 == 2 seconds.
let mut attempt = 0;
let mut max_attempts = 8;
while attempt < max_attempts && simple_servers.len() > 0 {
attempt += 1;
let start_time = time::SteadyTime::now();
let mut deadline = start_time;
let mut final_deadline = start_time + time::Duration::seconds(2);
while deadline < final_deadline && simple_servers.len() > 0 {
deadline = deadline + time::Duration::milliseconds(250);

// TODO(canndrew): We should limit the number of servers that we send to. If the user
Expand Down Expand Up @@ -291,13 +300,13 @@ impl MappedUdpSocket {
// give us the same address. By contrast, servers on the same subnet as us or
// behind the same carrier-level NAT are likely to respond in under a second.
// So once we have one global address drop the timeout.
// TODO(canndrew): For now this is commented-out. Waiting for the is_global
// method to become available in the next stable rust.
/*
if recv_addr.ip().is_global() {
max_attempts = 4;

// TODO(canndrew): Use IpAddr::is_global when it's available
// let is_global = recv_addr.is_global();
let is_global = false;
if is_global {
final_deadline = start_time + time::Duration::seconds(1);
};
*/

// Add this endpoint if we don't already know about it. We may have found it
// through IGD or it may be a local interface.
Expand Down
15 changes: 8 additions & 7 deletions src/mapping_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,16 @@

use std::sync::RwLock;
use std::io;
use std::fmt;
use std::net::{Ipv4Addr, Ipv6Addr};
use std;
use std::thread;
use std::time::Duration;

use ip::IpAddr;
use igd;
use socket_addr::SocketAddr;
use w_result::{WResult, WOk, WErr};
use get_if_addrs;
use void::Void;

use hole_punch_server_addr::HolePunchServerAddr;

/// You need to create a `MappingContext` before doing any socket mapping. This
/// `MappingContext` should ideally be kept throughout the lifetime of the
/// program. Internally it caches a addresses of UPnP servers and hole punching
Expand All @@ -61,14 +56,18 @@ quick_error! {
#[derive(Debug)]
pub enum MappingContextNewError {
/// Failed to list the local machine's network interfaces.
ListInterfaces { err:io::Error } {
ListInterfaces {
err:io::Error,
} {
description("Failed to list the local machine's network interfaces")
display("Failed to list the local machines's network interfaces \
get_if_addrs returned an error: {}", err)
cause(err)
}
/// Failed to spawn a thread.
SpawnThread { err: io::Error } {
SpawnThread {
err: io::Error
} {
description("Failed to spawn a thread")
display("Failed to spawn a thread. \
thread::spawn returned an error: {}", err)
Expand All @@ -80,6 +79,8 @@ quick_error! {
quick_error! {
#[derive(Debug)]
pub enum MappingContextNewWarning {
/// Error finding IGD gateway. `if_name` and `if_addr` indicate the network interface being
/// searched from when this error was raised.
SearchGateway {
if_name: String,
if_addr: Ipv4Addr,
Expand Down

0 comments on commit 0755a98

Please sign in to comment.