Tun feature

This commit is contained in:
Dennis Schwerdel 2015-11-22 20:02:02 +01:00
parent 8716c70e6d
commit f559e9bf09
4 changed files with 63 additions and 25 deletions

View File

@ -15,6 +15,7 @@ trait DeviceSetup {
fn setup_device(RawFd, &str) -> IoResult<String>; fn setup_device(RawFd, &str) -> IoResult<String>;
} }
#[allow(dead_code)]
struct TapSetup; struct TapSetup;
impl DeviceSetup for TapSetup { impl DeviceSetup for TapSetup {
@ -31,7 +32,7 @@ impl DeviceSetup for TapSetup {
} }
} }
#[allow(dead_code)]
struct TunSetup; struct TunSetup;
impl DeviceSetup for TunSetup { impl DeviceSetup for TunSetup {

View File

@ -1,10 +1,9 @@
use std::{mem, fmt, fs}; use std::{mem, fmt};
use std::net::SocketAddr; use std::net::SocketAddr;
use std::collections::HashMap; use std::collections::HashMap;
use std::os::unix::io::{AsRawFd, RawFd}; use std::io::Write;
use std::io::{Result as IoResult, Error as IoError, Read, Write};
use super::cloud::{Error, Table, Protocol, VirtualInterface}; use super::cloud::{Error, Table, Protocol};
use super::util::as_obj; use super::util::as_obj;
use time::{Duration, SteadyTime}; use time::{Duration, SteadyTime};

View File

@ -53,7 +53,7 @@ impl IpAddress {
} }
} }
#[allow(dead_code)]
pub struct InternetProtocol; pub struct InternetProtocol;
impl Protocol for InternetProtocol { impl Protocol for InternetProtocol {

View File

@ -16,8 +16,10 @@ use time::Duration;
use docopt::Docopt; use docopt::Docopt;
use std::hash::{Hash, SipHasher, Hasher}; use std::hash::{Hash, SipHasher, Hasher};
use std::path::PathBuf;
use cloud::{Error, TapCloud}; use ip::RoutingTable;
use cloud::{Error, TapCloud, TunCloud};
//TODO: Implement IPv6 //TODO: Implement IPv6
@ -39,25 +41,34 @@ impl log::Log for SimpleLogger {
} }
} }
static USAGE: &'static str = " static USAGE: &'static str = "
Usage: Usage:
ethcloud [options] [-d <device>] [-l <listen>] [-c <connect>...] ethcloud [options] [-t <type>] [-d <device>] [-l <listen>] [-c <connect>...]
Options: Options:
-d <device>, --device <device> Name of the tap device [default: ethcloud%d] -t <type>, --type <type> Set the type of network [default: tap]
-d <device>, --device <device> Name of the virtual device [default: cloud%d]
-l <listen>, --listen <listen> Address to listen on [default: 0.0.0.0:3210] -l <listen>, --listen <listen> Address to listen on [default: 0.0.0.0:3210]
-c <connect>, --connect <connect> List of peers (addr:port) to connect to -c <connect>, --connect <connect> List of peers (addr:port) to connect to
--network-id <network_id> Optional token that identifies the network --network-id <network_id> Optional token that identifies the network
--peer-timeout <peer_timeout> Peer timeout in seconds [default: 1800] --peer-timeout <peer_timeout> Peer timeout in seconds [default: 1800]
--mac-timeout <mac_timeout> Mac table entry timeout in seconds [default: 300] --table <file> The file containing the routing table (only for tun)
--mac-timeout <mac_timeout> Mac table entry timeout in seconds (only for tap) [default: 300]
-v, --verbose Log verbosely -v, --verbose Log verbosely
-q, --quiet Only print error messages -q, --quiet Only print error messages
-h, --help Display the help -h, --help Display the help
"; ";
#[derive(RustcDecodable, Debug)]
enum Type {
Tun, Tap
}
#[derive(RustcDecodable, Debug)] #[derive(RustcDecodable, Debug)]
struct Args { struct Args {
flag_type: Type,
flag_table: PathBuf,
flag_device: String, flag_device: String,
flag_listen: String, flag_listen: String,
flag_network_id: Option<String>, flag_network_id: Option<String>,
@ -68,20 +79,7 @@ struct Args {
flag_quiet: bool flag_quiet: bool
} }
fn main() { fn tap_cloud(args: Args) {
let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit());
log::set_logger(|max_log_level| {
assert!(!args.flag_verbose || !args.flag_quiet);
if args.flag_verbose {
max_log_level.set(log::LogLevelFilter::Debug);
} else if args.flag_quiet {
max_log_level.set(log::LogLevelFilter::Error);
} else {
max_log_level.set(log::LogLevelFilter::Info);
}
Box::new(SimpleLogger)
}).unwrap();
debug!("Args: {:?}", args);
let mut tapcloud = TapCloud::new_tap_cloud( let mut tapcloud = TapCloud::new_tap_cloud(
&args.flag_device, &args.flag_device,
args.flag_listen, args.flag_listen,
@ -98,3 +96,43 @@ fn main() {
} }
tapcloud.run() tapcloud.run()
} }
fn tun_cloud(args: Args) {
let mut table = RoutingTable::new();
table.load_from(&args.flag_table).unwrap();
let mut tuncloud = TunCloud::new_tun_cloud(
&args.flag_device,
args.flag_listen,
args.flag_network_id.map(|name| {
let mut s = SipHasher::new();
name.hash(&mut s);
s.finish()
}),
table,
Duration::seconds(args.flag_peer_timeout as i64)
);
for addr in args.flag_connect {
tuncloud.connect(&addr as &str, true).expect("Failed to send");
}
tuncloud.run()
}
fn main() {
let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit());
log::set_logger(|max_log_level| {
assert!(!args.flag_verbose || !args.flag_quiet);
if args.flag_verbose {
max_log_level.set(log::LogLevelFilter::Debug);
} else if args.flag_quiet {
max_log_level.set(log::LogLevelFilter::Error);
} else {
max_log_level.set(log::LogLevelFilter::Info);
}
Box::new(SimpleLogger)
}).unwrap();
debug!("Args: {:?}", args);
match args.flag_type {
Type::Tap => tap_cloud(args),
Type::Tun => tun_cloud(args)
}
}