Skip to content
Open
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
8 changes: 8 additions & 0 deletions pot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub struct PotConf {
pub name: String,
pub ip_addr: Option<IpAddr>,
pub network_type: NetType,
pub aliases: Option<Vec<String>>,
}

#[derive(Debug, Default)]
Expand All @@ -99,6 +100,7 @@ pub struct PotConfVerbatim {
pub ip4: Option<String>,
pub ip: Option<String>,
pub network_type: Option<String>,
pub aliases: Option<Vec<String>>,
}

impl Default for PotConf {
Expand All @@ -107,6 +109,7 @@ impl Default for PotConf {
name: String::default(),
ip_addr: None,
network_type: NetType::Inherit,
aliases: None,
}
}
}
Expand Down Expand Up @@ -205,8 +208,13 @@ pub fn get_pot_conf_list(conf: PotSystemConfig) -> Vec<PotConf> {
if s.starts_with("network_type=") {
temp_pot_conf.network_type = Some(s.split('=').nth(1).unwrap().to_string());
}
if s.starts_with("pot.aliases=") {
temp_pot_conf.aliases.get_or_insert(Vec::new()).push(s.split('=')
.nth(1).unwrap().to_string())
}
}
if let Some(network_type) = temp_pot_conf.network_type {
pot_conf.aliases = temp_pot_conf.aliases;
pot_conf.network_type = match network_type.as_str() {
"inherit" => NetType::Inherit,
"alias" => NetType::Alias,
Expand Down
46 changes: 39 additions & 7 deletions src/bin/potnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pot_rs::{get_pot_conf_list, NetType, PotSystemConfig};
use std::collections::BTreeMap;
use std::net::IpAddr;
use std::net::IpAddr::{V4, V6};
use std::string::String;
// use std::string::String;
use structopt::StructOpt;
use structopt_flags::{HostParam, LogLevel};

Expand Down Expand Up @@ -54,6 +54,9 @@ struct BridgeOpt {
/// The name of a private bridge
#[structopt(short = "-b", long = "--bridge-name")]
bridge_name: Option<String>,
/// alias hostnames are included as set in pot's configuration
#[structopt(short = "-a", long)]
aliases_included: bool,
}

#[derive(Clone, Debug, StructOpt)]
Expand Down Expand Up @@ -201,7 +204,8 @@ fn get_next_from_bridge(opt: &Opt, conf: &PotSystemConfig, bridge_name: &str) ->
Ok(())
}

fn get_hosts_from_bridge(_opt: &Opt, conf: &PotSystemConfig, bridge_name: &str) -> Result<()> {
fn get_hosts_from_bridge(_opt: &Opt, conf: &PotSystemConfig, bridge_name: &str, include_aliases: Option<bool>)
-> Result<()> {
let bridges_list = get_bridges_list(conf)?;
if let Some(bridge) = bridges_list.iter().find(|x| x.name == bridge_name) {
info!("bridge {} found", bridge.name);
Expand All @@ -211,7 +215,15 @@ fn get_hosts_from_bridge(_opt: &Opt, conf: &PotSystemConfig, bridge_name: &str)
if v.network_type == NetType::PrivateBridge
&& bridge.network.contains(&v.ip_addr.unwrap())
{
ip_db.insert(v.ip_addr.unwrap(), v.name.clone());
let mut names = v.name.clone();
if include_aliases.unwrap() && v.aliases.is_some() {
names.push(' ');
names.push_str(v.aliases.clone().unwrap().join(" ").as_str());
}
ip_db.insert(v.ip_addr.unwrap(), names);
}
if include_aliases.unwrap() && v.aliases.is_some() {
ip_db.insert(v.ip_addr.unwrap(), v.aliases.clone().unwrap().join(" "));
}
}
for (ip, hostname) in ip_db {
Expand All @@ -221,18 +233,37 @@ fn get_hosts_from_bridge(_opt: &Opt, conf: &PotSystemConfig, bridge_name: &str)
Ok(())
}

fn get_hosts_for_public_bridge(_opt: &Opt, conf: &PotSystemConfig) {
fn get_hosts_for_public_bridge(_opt: &Opt, conf: &PotSystemConfig, include_aliases: Option<bool>) {
let mut ip_db = BTreeMap::new();
for v in &get_pot_conf_list(conf.clone()) {
if v.network_type == NetType::PublicBridge {
ip_db.insert(v.ip_addr.unwrap(), v.name.clone());
let mut names = v.name.clone();
if include_aliases.unwrap() && v.aliases.is_some() {
names.push(' ');
names.push_str(v.aliases.clone().unwrap().join(" ").as_str());
}
ip_db.insert(v.ip_addr.unwrap(), names);
}
}
for (ip, hostname) in ip_db {
println!("{} {}", ip, hostname);
}
}

/*
fn add_aliases(hosts: &Vec<String>, ip_db: &mut BTreeMap<IpAddr, String>)
{
for host in hosts {
let parts = host.rsplit(' ');
let mut pairs: Vec<&str> = parts.collect();
let ip = pairs.pop().unwrap().parse::<IpAddr>().unwrap();
let names = pairs.join(" ");
//println!("{} {}", &ip, &names);
ip_db.insert(ip, names);
};
}
*/

fn validate_with_bridge(conf: &PotSystemConfig, bridge_name: &str, ip: IpAddr) -> Result<()> {
let bridges_list = get_bridges_list(conf)?;
if let Some(bridge) = bridges_list.iter().find(|x| x.name == bridge_name) {
Expand Down Expand Up @@ -421,11 +452,12 @@ fn main() -> Result<()> {
new_net(x.host_number, &conf, &ip_db);
}
Command::EtcHosts(ehopt) => {
let aliases_included = Some(ehopt.aliases_included);
if let Some(bridge_name) = ehopt.bridge_name {
debug!("get an ip for the bridge {}", bridge_name);
get_hosts_from_bridge(&opt_clone, &conf, &bridge_name)?;
get_hosts_from_bridge(&opt_clone, &conf, &bridge_name, aliases_included)?;
} else {
get_hosts_for_public_bridge(&opt_clone, &conf);
get_hosts_for_public_bridge(&opt_clone, &conf, aliases_included);
}
}
}
Expand Down