mirror of
https://github.com/jgoerzen/xbnet.git
synced 2025-01-03 12:08:21 -04:00
Some more things handled here
This commit is contained in:
parent
a7feabb93b
commit
1a5c2c47bf
20
src/main.rs
20
src/main.rs
@ -75,7 +75,21 @@ enum Command {
|
|||||||
// FIXME: add a paremter to accept data from only that place
|
// FIXME: add a paremter to accept data from only that place
|
||||||
},
|
},
|
||||||
/// Create a virtual Ethernet interface and send frames across XBee
|
/// Create a virtual Ethernet interface and send frames across XBee
|
||||||
Tap,
|
Tap {
|
||||||
|
/// Broadcast to XBee, instead of dropping, packets to unknown destinations. Has no effect if --broadcast_everything is given.
|
||||||
|
#[structopt(long)]
|
||||||
|
broadcast_unknown: bool,
|
||||||
|
|
||||||
|
/// Broadcast every packet out the XBee side
|
||||||
|
#[structopt(long)]
|
||||||
|
broadcast_everything: bool,
|
||||||
|
|
||||||
|
/// Name for the interface; defaults to "xbnet%d" which the OS usually turns to "xbnet0".
|
||||||
|
/// Note that this name is not guaranteed; the name allocated by the OS is displayed
|
||||||
|
/// at startup.
|
||||||
|
#[structopt(long, default_value = "xbnet%d")]
|
||||||
|
iface_name: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -118,8 +132,8 @@ fn main() {
|
|||||||
// Make sure queued up data is sent
|
// Make sure queued up data is sent
|
||||||
let _ = writerthread.join();
|
let _ = writerthread.join();
|
||||||
}
|
}
|
||||||
Command::Tap => {
|
Command::Tap { broadcast_unknown, broadcast_everything, iface_name } => {
|
||||||
let tap_reader = tap::XBTap::new_tap(xb.mymac).expect("Failure initializing tap");
|
let tap_reader = tap::XBTap::new_tap(xb.mymac, broadcast_unknown, broadcast_everything, iface_name).expect("Failure initializing tap");
|
||||||
let tap_writer = tap_reader.clone();
|
let tap_writer = tap_reader.clone();
|
||||||
let maxpacketsize = xb.maxpacketsize;
|
let maxpacketsize = xb.maxpacketsize;
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
|
34
src/tap.rs
34
src/tap.rs
@ -44,6 +44,8 @@ pub struct XBTap {
|
|||||||
pub myxbmac: u64,
|
pub myxbmac: u64,
|
||||||
pub myethermacstr: String,
|
pub myethermacstr: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub broadcast_unknown: bool,
|
||||||
|
pub broadcast_everything: bool,
|
||||||
pub tap: Arc<Iface>,
|
pub tap: Arc<Iface>,
|
||||||
|
|
||||||
/** We can't just blindly generate destination MACs because there is a bug
|
/** We can't just blindly generate destination MACs because there is a bug
|
||||||
@ -54,10 +56,10 @@ pub struct XBTap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl XBTap {
|
impl XBTap {
|
||||||
pub fn new_tap(myxbmac: u64) -> io::Result<XBTap> {
|
pub fn new_tap(myxbmac: u64, broadcast_unknown: bool, broadcast_everything: bool, iface_name_requested: String) -> io::Result<XBTap> {
|
||||||
let myethermac = mac64to48(myxbmac);
|
let myethermac = mac64to48(myxbmac);
|
||||||
let myethermacstr = showmac(&myethermac);
|
let myethermacstr = showmac(&myethermac);
|
||||||
let tap = Iface::without_packet_info("xbnet%d", Mode::Tap)?;
|
let tap = Iface::without_packet_info(&iface_name_requested, Mode::Tap)?;
|
||||||
let name = tap.name();
|
let name = tap.name();
|
||||||
|
|
||||||
// Set the MAC address.
|
// Set the MAC address.
|
||||||
@ -84,7 +86,7 @@ impl XBTap {
|
|||||||
"Interface {} with ether MAC {} (XBee MAC {:x}) ready",
|
"Interface {} with ether MAC {} (XBee MAC {:x}) ready",
|
||||||
name,
|
name,
|
||||||
myethermacstr,
|
myethermacstr,
|
||||||
myxbmac
|
myxbmac,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut desthm = HashMap::new();
|
let mut desthm = HashMap::new();
|
||||||
@ -95,12 +97,30 @@ impl XBTap {
|
|||||||
myxbmac,
|
myxbmac,
|
||||||
myethermac,
|
myethermac,
|
||||||
myethermacstr,
|
myethermacstr,
|
||||||
|
broadcast_unknown,
|
||||||
|
broadcast_everything,
|
||||||
name: String::from(name),
|
name: String::from(name),
|
||||||
tap: Arc::new(tap),
|
tap: Arc::new(tap),
|
||||||
dests: Arc::new(Mutex::new(desthm)),
|
dests: Arc::new(Mutex::new(desthm)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_xb_dest_mac(&self, ethermac: &[u8; 6]) -> Option<u64> {
|
||||||
|
if self.broadcast_everything {
|
||||||
|
return Some(XB_BROADCAST);
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.dests.lock().unwrap().get(ethermac) {
|
||||||
|
None =>
|
||||||
|
if self.broadcast_unknown {
|
||||||
|
Some(XB_BROADCAST)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
Some(dest) => Some(*dest),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn frames_from_tap_processor(
|
pub fn frames_from_tap_processor(
|
||||||
&self,
|
&self,
|
||||||
maxframesize: usize,
|
maxframesize: usize,
|
||||||
@ -122,7 +142,7 @@ impl XBTap {
|
|||||||
warn!("Packet from tap with MAC address {} mismatches my own MAC address of {}; proceeding anyway",
|
warn!("Packet from tap with MAC address {} mismatches my own MAC address of {}; proceeding anyway",
|
||||||
showmac(header.source().try_into().unwrap()), self.myethermacstr);
|
showmac(header.source().try_into().unwrap()), self.myethermacstr);
|
||||||
}
|
}
|
||||||
match self.dests.lock().unwrap().get(header.destination()) {
|
match self.get_xb_dest_mac(header.destination().try_into().unwrap()) {
|
||||||
None =>
|
None =>
|
||||||
warn!("Destination MAC address unknown; discarding packet"),
|
warn!("Destination MAC address unknown; discarding packet"),
|
||||||
Some(destxbmac) =>
|
Some(destxbmac) =>
|
||||||
@ -130,7 +150,7 @@ impl XBTap {
|
|||||||
let res =
|
let res =
|
||||||
sender
|
sender
|
||||||
.try_send(XBTX::TXData(
|
.try_send(XBTX::TXData(
|
||||||
XBDestAddr::U64(*destxbmac),
|
XBDestAddr::U64(destxbmac),
|
||||||
Bytes::copy_from_slice(tapdata),
|
Bytes::copy_from_slice(tapdata),
|
||||||
));
|
));
|
||||||
match res {
|
match res {
|
||||||
@ -163,7 +183,9 @@ impl XBTap {
|
|||||||
Ok(packet) => {
|
Ok(packet) => {
|
||||||
if let Some(LinkSlice::Ethernet2(header)) = packet.link {
|
if let Some(LinkSlice::Ethernet2(header)) = packet.link {
|
||||||
trace!("SERIN: Packet Ethernet header is {} -> {}", hex::encode(header.source()), hex::encode(header.destination()));
|
trace!("SERIN: Packet Ethernet header is {} -> {}", hex::encode(header.source()), hex::encode(header.destination()));
|
||||||
self.dests.lock().unwrap().insert(header.source().try_into().unwrap(), fromu64);
|
if ! self.broadcast_everything {
|
||||||
|
self.dests.lock().unwrap().insert(header.source().try_into().unwrap(), fromu64);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user