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

Simple server cleanup #30

Merged
merged 5 commits into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions examples/udp-hole-punch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,13 @@ fn main() {
// Now we use the socket, our private rendezvous info and their public rendezvous info to
// complete the connection.
let punched_socket = match PunchedUdpSocket::punch_hole(socket, our_priv_info, their_pub_info) {
Ok(punched_socket) => punched_socket,
Err(e) => {
WOk(punched_socket, warnings) => {
for warning in warnings {
println!("Warning when punching hole: {}", warning);
}
punched_socket
},
WErr(e) => {
println!("IO error punching udp socket: {}", e);
println!("Exiting.");
return;
Expand Down
31 changes: 0 additions & 31 deletions src/hole_punch_server_addr.rs

This file was deleted.

15 changes: 3 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,54 +41,45 @@
#![cfg_attr(feature="clippy", plugin(clippy))]
#![cfg_attr(feature="clippy", deny(clippy, clippy_pedantic))]

// TODO - remove this
#![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, MappingContextNewError, MappingContextNewWarning};
pub use hole_punch_server_addr::HolePunchServerAddr;
pub use mapped_socket_addr::MappedSocketAddr;
pub use rendezvous_info::{PrivRendezvousInfo, PubRendezvousInfo,
gen_rendezvous_info};
pub use mapped_udp_socket::{MappedUdpSocket, MappedUdpSocketMapError,
MappedUdpSocketMapWarning, MappedUdpSocketNewError};
pub use punched_udp_socket::PunchedUdpSocket;
pub use punched_udp_socket::{PunchedUdpSocket, filter_udp_hole_punch_packet};
pub use mapped_tcp_socket::{MappedTcpSocket, tcp_punch_hole};
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;

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!();
}
28 changes: 12 additions & 16 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 maidsafe_utilities::serialisation::deserialise;
use socket_addr::SocketAddr;
use w_result::{WResult, WOk, WErr};

use hole_punch_server_addr::HolePunchServerAddr;
use listener_message;
use mapping_context;
use mapping_context::MappingContext;
use mapped_socket_addr::MappedSocketAddr;
use periodic_sender::PeriodicSender;
use socket_utils;
use socket_utils::RecvUntil;

Expand Down Expand Up @@ -266,13 +263,12 @@ impl MappedUdpSocket {
let send_data = listener_message::REQUEST_MAGIC_CONSTANT;
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 @@ -304,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
5 changes: 0 additions & 5 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 Down
97 changes: 0 additions & 97 deletions src/periodic_sender.rs

This file was deleted.

Loading