mirror of https://github.com/dswd/vpncloud.git
Behavior
This commit is contained in:
parent
64342d1288
commit
83f53c16d7
30
src/cloud.rs
30
src/cloud.rs
|
@ -18,6 +18,11 @@ use super::ip::{InternetProtocol, IpAddress, RoutingTable};
|
|||
|
||||
pub type NetworkId = u64;
|
||||
|
||||
#[derive(RustcDecodable, Debug)]
|
||||
pub enum Behavior {
|
||||
Normal, Hub, Switch, Router
|
||||
}
|
||||
|
||||
pub trait Address: Sized + fmt::Debug + Clone {
|
||||
fn from_bytes(&[u8]) -> Result<Self, Error>;
|
||||
fn to_bytes(&self) -> Vec<u8>;
|
||||
|
@ -347,14 +352,20 @@ impl<A: Address, T: Table<Address=A>, M: Protocol<Address=A>, I: VirtualInterfac
|
|||
pub type TapCloud = GenericCloud<EthAddr, MacTable, Frame, TapDevice>;
|
||||
|
||||
impl TapCloud {
|
||||
pub fn new_tap_cloud(device: &str, listen: String, network_id: Option<NetworkId>, mac_timeout: Duration, peer_timeout: Duration) -> Self {
|
||||
pub fn new_tap_cloud(device: &str, listen: String, behavior: Behavior, network_id: Option<NetworkId>, mac_timeout: Duration, peer_timeout: Duration) -> Self {
|
||||
let device = match TapDevice::new(device) {
|
||||
Ok(device) => device,
|
||||
_ => panic!("Failed to open tap device")
|
||||
};
|
||||
info!("Opened tap device {}", device.ifname());
|
||||
let table = MacTable::new(mac_timeout);
|
||||
Self::new(device, listen, network_id, table, peer_timeout, true, true, vec![])
|
||||
let (learning, broadcasting) = match behavior {
|
||||
Behavior::Normal => (true, true),
|
||||
Behavior::Switch => (true, true),
|
||||
Behavior::Hub => (false, true),
|
||||
Behavior::Router => (false, false)
|
||||
};
|
||||
Self::new(device, listen, network_id, table, peer_timeout, learning, broadcasting, vec![])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -362,14 +373,23 @@ impl TapCloud {
|
|||
pub type TunCloud = GenericCloud<IpAddress, RoutingTable, InternetProtocol, TunDevice>;
|
||||
|
||||
impl TunCloud {
|
||||
pub fn new_tun_cloud(device: &str, listen: String, network_id: Option<NetworkId>, subnet: String, peer_timeout: Duration) -> Self {
|
||||
pub fn new_tun_cloud(device: &str, listen: String, behavior: Behavior, network_id: Option<NetworkId>, subnets: Vec<String>, peer_timeout: Duration) -> Self {
|
||||
let device = match TunDevice::new(device) {
|
||||
Ok(device) => device,
|
||||
_ => panic!("Failed to open tun device")
|
||||
};
|
||||
info!("Opened tun device {}", device.ifname());
|
||||
let table = RoutingTable::new();
|
||||
let subnet = IpAddress::from_str(&subnet).expect("Invalid subnet");
|
||||
Self::new(device, listen, network_id, table, peer_timeout, false, false, vec![subnet])
|
||||
let mut addrs = Vec::with_capacity(subnets.len());
|
||||
for s in subnets {
|
||||
addrs.push(IpAddress::from_str(&s).expect("Invalid subnet"));
|
||||
}
|
||||
let (learning, broadcasting) = match behavior {
|
||||
Behavior::Normal => (false, false),
|
||||
Behavior::Switch => (true, true),
|
||||
Behavior::Hub => (false, true),
|
||||
Behavior::Router => (false, false)
|
||||
};
|
||||
Self::new(device, listen, network_id, table, peer_timeout, learning, broadcasting, addrs)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -187,12 +187,12 @@ impl Table for RoutingTable {
|
|||
match src {
|
||||
IpAddress::V4(_) => (),
|
||||
IpAddress::V4Net(base, prefix_len) => {
|
||||
info!("Adding to routing table: {:?} => {}", src, addr);
|
||||
info!("Adding to routing table: {}/{} => {}", base, prefix_len, addr);
|
||||
self.add(IpAddress::V4(base).to_bytes(), prefix_len, addr);
|
||||
},
|
||||
IpAddress::V6(_) => (),
|
||||
IpAddress::V6Net(base, prefix_len) => {
|
||||
info!("Adding to routing table: {:?} => {}", src, addr);
|
||||
info!("Adding to routing table: {}/{} => {}", base, prefix_len, addr);
|
||||
self.add(IpAddress::V6(base).to_bytes(), prefix_len, addr);
|
||||
}
|
||||
}
|
||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -16,9 +16,11 @@ use docopt::Docopt;
|
|||
|
||||
use std::hash::{Hash, SipHasher, Hasher};
|
||||
|
||||
use cloud::{Error, TapCloud, TunCloud};
|
||||
use cloud::{Error, TapCloud, TunCloud, Behavior};
|
||||
|
||||
|
||||
//TODO: hub behavior
|
||||
//TODO: L2 routing/L3 switching
|
||||
//TODO: Implement IPv6
|
||||
//TODO: Encryption
|
||||
//TODO: Call close
|
||||
|
@ -44,19 +46,19 @@ Usage:
|
|||
|
||||
Options:
|
||||
-t <type>, --type <type> Set the type of network [default: tap]
|
||||
--behavior <behavior> The behavior of the vpn [default: normal]
|
||||
-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]
|
||||
-c <connect>, --connect <connect> List of peers (addr:port) to connect to
|
||||
--network-id <network_id> Optional token that identifies the network
|
||||
--peer-timeout <peer_timeout> Peer timeout in seconds [default: 1800]
|
||||
--subnet <subnet> The local subnet to use (only for tun)
|
||||
--subnet <subnet>... The local subnets to use (only for tun)
|
||||
--mac-timeout <mac_timeout> Mac table entry timeout in seconds (only for tap) [default: 300]
|
||||
-v, --verbose Log verbosely
|
||||
-q, --quiet Only print error messages
|
||||
-h, --help Display the help
|
||||
";
|
||||
|
||||
|
||||
#[derive(RustcDecodable, Debug)]
|
||||
enum Type {
|
||||
Tun, Tap
|
||||
|
@ -65,7 +67,8 @@ enum Type {
|
|||
#[derive(RustcDecodable, Debug)]
|
||||
struct Args {
|
||||
flag_type: Type,
|
||||
flag_subnet: String,
|
||||
flag_behavior: Behavior,
|
||||
flag_subnet: Vec<String>,
|
||||
flag_device: String,
|
||||
flag_listen: String,
|
||||
flag_network_id: Option<String>,
|
||||
|
@ -80,6 +83,7 @@ fn tap_cloud(args: Args) {
|
|||
let mut tapcloud = TapCloud::new_tap_cloud(
|
||||
&args.flag_device,
|
||||
args.flag_listen,
|
||||
args.flag_behavior,
|
||||
args.flag_network_id.map(|name| {
|
||||
let mut s = SipHasher::new();
|
||||
name.hash(&mut s);
|
||||
|
@ -98,6 +102,7 @@ fn tun_cloud(args: Args) {
|
|||
let mut tuncloud = TunCloud::new_tun_cloud(
|
||||
&args.flag_device,
|
||||
args.flag_listen,
|
||||
args.flag_behavior,
|
||||
args.flag_network_id.map(|name| {
|
||||
let mut s = SipHasher::new();
|
||||
name.hash(&mut s);
|
||||
|
|
Loading…
Reference in New Issue