Skip to content

Commit

Permalink
Add better error message when ip command is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
blueberrymuffin3 committed Jan 29, 2023
1 parent d2a8a67 commit 5d878b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

[package]
name = "xbnet"
version = "1.2.0"
version = "1.2.1"
authors = ["John Goerzen <jgoerzen@complete.org>"]
edition = "2018"
license = "GPL-3.0+"
Expand Down
59 changes: 32 additions & 27 deletions src/tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crossbeam_channel;
use etherparse::*;
use log::*;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::io;
use std::net::IpAddr;
use std::sync::{Arc, Mutex};
Expand All @@ -51,6 +52,22 @@ pub struct XBTun {
pub dests: Arc<Mutex<HashMap<IpAddr, (u64, Instant)>>>,
}

fn run_ip_cmd<I, S>(args: I)
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
assert_eq!(
std::process::Command::new("ip")
.args(args)
.status()
.expect("Ensure the 'ip' command from 'iproute2' is available")
.code(),
Some(0),
"ip exited with nonzero exit code"
);
}

impl XBTun {
pub fn new_tun(
myxbmac: u64,
Expand All @@ -68,27 +85,13 @@ impl XBTun {
// TODO: This should use ioctl to be more portable

if set_up {
assert_eq!(
std::process::Command::new("ip")
.args(["link", "set", name, "up"])
.status()
.unwrap()
.code(),
Some(0)
);
info!("Set interface to up")
run_ip_cmd(["link", "set", name, "up"]);
info!("Set interface to up");
}

if let Some(ip) = set_ip {
assert_eq!(
std::process::Command::new("ip")
.args(["addr", "add", &ip, "dev", name])
.status()
.unwrap()
.code(),
Some(0)
);
info!("Set ip to {}", ip)
run_ip_cmd(["addr", "add", &ip, "dev", name]);
info!("Set ip to {}", ip);
}

println!("Interface {} (XBee MAC {:x}) ready", name, myxbmac,);
Expand All @@ -114,9 +117,7 @@ impl XBTun {

match self.dests.lock().unwrap().get(ipaddr) {
// Broadcast if we don't know it
None => {
XB_BROADCAST
},
None => XB_BROADCAST,
Some((dest, expiration)) => {
if Instant::now() >= *expiration {
// Broadcast it if the cache entry has expired
Expand Down Expand Up @@ -145,16 +146,18 @@ impl XBTun {
let ips = extract_ips(&packet);
if let Some((source, destination)) = ips {
match destination {
IpAddr::V6(_) =>
IpAddr::V6(_) => {
if self.disable_ipv6 {
debug!("Dropping packet because --disable-ipv6 given");
continue;
},
IpAddr::V4(_) =>
}
}
IpAddr::V4(_) => {
if self.disable_ipv4 {
debug!("Dropping packet because --disable-ipv4 given");
continue;
}
}
};

let destxbmac = self.get_xb_dest_mac(&destination);
Expand Down Expand Up @@ -204,16 +207,18 @@ impl XBTun {
if let Some((source, destination)) = ips {
trace!("SERIN: Packet is {} -> {}", source, destination);
match source {
IpAddr::V6(_) =>
IpAddr::V6(_) => {
if self.disable_ipv6 {
debug!("Dropping packet because --disable-ipv6 given");
continue;
},
IpAddr::V4(_) =>
}
}
IpAddr::V4(_) => {
if self.disable_ipv4 {
debug!("Dropping packet because --disable-ipv4 given");
continue;
}
}
}
if !self.broadcast_everything {
self.dests.lock().unwrap().insert(
Expand Down

0 comments on commit 5d878b8

Please sign in to comment.