This commit is contained in:
John Goerzen 2020-09-24 22:06:21 -05:00
parent adb2c18b12
commit 7b2fc94f7f
3 changed files with 47 additions and 15 deletions

View File

@ -31,8 +31,8 @@ mod xbpacket;
mod xbrx;
use std::path::PathBuf;
use std::time::Duration;
use structopt::StructOpt;
use tun_tap::Iface;
#[derive(Debug, StructOpt)]
#[structopt(
@ -93,6 +93,22 @@ enum Command {
#[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,
},
/// Create a virtual IP interface and send frames across XBee
Tun {
/// Broadcast every packet out the XBee side
#[structopt(long)]
broadcast_everything: bool,
/** The maximum number of seconds to store the destination XBee MAC for an IP address. */
#[structopt(long)]
max_ip_cache: u64,
/// 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.
@ -167,7 +183,33 @@ fn main() {
.expect("Failure in frames_from_xb_processor");
});
tap_reader
.frames_from_tap_processor(maxpacketsize - 1, xbeesender)
.frames_from_tap_processor(xbeesender)
.expect("Failure in frames_from_tap_processor");
// Make sure queued up data is sent
let _ = writerthread.join();
}
Command::Tun {
broadcast_everything,
iface_name,
max_ip_cache
} => {
let max_ip_cache = Duration::from_secs(max_ip_cache);
let tun_reader = tun::XBTun::new_tun(
xb.mymac,
broadcast_everything,
iface_name,
max_ip_cache,
)
.expect("Failure initializing tun");
let tun_writer = tun_reader.clone();
let maxpacketsize = xb.maxpacketsize;
thread::spawn(move || {
tun_writer
.frames_from_xb_processor(&mut xbreframer, &mut xb.ser_reader)
.expect("Failure in frames_from_xb_processor");
});
tun_reader
.frames_from_tun_processor(xbeesender)
.expect("Failure in frames_from_tap_processor");
// Make sure queued up data is sent
let _ = writerthread.join();

View File

@ -95,7 +95,6 @@ impl XBTap {
pub fn frames_from_tap_processor(
&self,
maxframesize: usize,
sender: crossbeam_channel::Sender<XBTX>,
) -> io::Result<()> {
let mut buf = [0u8; 9100]; // Enough to handle even jumbo frames

View File

@ -29,9 +29,8 @@ use crossbeam_channel;
use etherparse::*;
use log::*;
use std::collections::HashMap;
use std::convert::TryInto;
use std::io;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::net::IpAddr;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
@ -51,7 +50,7 @@ pub struct XBTun {
}
impl XBTun {
pub fn new_tap(
pub fn new_tun(
myxbmac: u64,
broadcast_everything: bool,
iface_name_requested: String,
@ -62,7 +61,7 @@ impl XBTun {
println!("Interface {} (XBee MAC {:x}) ready", name, myxbmac,);
let mut desthm = HashMap::new();
let desthm = HashMap::new();
Ok(XBTun {
myxbmac,
@ -95,7 +94,6 @@ impl XBTun {
pub fn frames_from_tun_processor(
&self,
maxframesize: usize,
sender: crossbeam_channel::Sender<XBTX>,
) -> io::Result<()> {
let mut buf = [0u8; 9100]; // Enough to handle even jumbo frames
@ -169,13 +167,6 @@ impl XBTun {
}
}
pub fn showmac(mac: &[u8; 6]) -> String {
format!(
"{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
)
}
pub fn extract_ip<'a>(packet: &SlicedPacket<'a>) -> Option<IpAddr> {
match &packet.ip {
Some(InternetSlice::Ipv4(header)) => Some(IpAddr::V4(header.destination_addr())),