mirror of
https://github.com/jgoerzen/xbnet.git
synced 2025-01-18 09:58:21 -04:00
cleanups
This commit is contained in:
parent
adb2c18b12
commit
7b2fc94f7f
46
src/main.rs
46
src/main.rs
@ -31,8 +31,8 @@ mod xbpacket;
|
|||||||
mod xbrx;
|
mod xbrx;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::time::Duration;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use tun_tap::Iface;
|
|
||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
#[structopt(
|
#[structopt(
|
||||||
@ -93,6 +93,22 @@ enum Command {
|
|||||||
#[structopt(long)]
|
#[structopt(long)]
|
||||||
broadcast_everything: bool,
|
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".
|
/// 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
|
/// Note that this name is not guaranteed; the name allocated by the OS is displayed
|
||||||
/// at startup.
|
/// at startup.
|
||||||
@ -167,7 +183,33 @@ fn main() {
|
|||||||
.expect("Failure in frames_from_xb_processor");
|
.expect("Failure in frames_from_xb_processor");
|
||||||
});
|
});
|
||||||
tap_reader
|
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");
|
.expect("Failure in frames_from_tap_processor");
|
||||||
// Make sure queued up data is sent
|
// Make sure queued up data is sent
|
||||||
let _ = writerthread.join();
|
let _ = writerthread.join();
|
||||||
|
@ -95,7 +95,6 @@ impl XBTap {
|
|||||||
|
|
||||||
pub fn frames_from_tap_processor(
|
pub fn frames_from_tap_processor(
|
||||||
&self,
|
&self,
|
||||||
maxframesize: usize,
|
|
||||||
sender: crossbeam_channel::Sender<XBTX>,
|
sender: crossbeam_channel::Sender<XBTX>,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
let mut buf = [0u8; 9100]; // Enough to handle even jumbo frames
|
let mut buf = [0u8; 9100]; // Enough to handle even jumbo frames
|
||||||
|
15
src/tun.rs
15
src/tun.rs
@ -29,9 +29,8 @@ use crossbeam_channel;
|
|||||||
use etherparse::*;
|
use etherparse::*;
|
||||||
use log::*;
|
use log::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::TryInto;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
use std::net::IpAddr;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
@ -51,7 +50,7 @@ pub struct XBTun {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl XBTun {
|
impl XBTun {
|
||||||
pub fn new_tap(
|
pub fn new_tun(
|
||||||
myxbmac: u64,
|
myxbmac: u64,
|
||||||
broadcast_everything: bool,
|
broadcast_everything: bool,
|
||||||
iface_name_requested: String,
|
iface_name_requested: String,
|
||||||
@ -62,7 +61,7 @@ impl XBTun {
|
|||||||
|
|
||||||
println!("Interface {} (XBee MAC {:x}) ready", name, myxbmac,);
|
println!("Interface {} (XBee MAC {:x}) ready", name, myxbmac,);
|
||||||
|
|
||||||
let mut desthm = HashMap::new();
|
let desthm = HashMap::new();
|
||||||
|
|
||||||
Ok(XBTun {
|
Ok(XBTun {
|
||||||
myxbmac,
|
myxbmac,
|
||||||
@ -95,7 +94,6 @@ impl XBTun {
|
|||||||
|
|
||||||
pub fn frames_from_tun_processor(
|
pub fn frames_from_tun_processor(
|
||||||
&self,
|
&self,
|
||||||
maxframesize: usize,
|
|
||||||
sender: crossbeam_channel::Sender<XBTX>,
|
sender: crossbeam_channel::Sender<XBTX>,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
let mut buf = [0u8; 9100]; // Enough to handle even jumbo frames
|
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> {
|
pub fn extract_ip<'a>(packet: &SlicedPacket<'a>) -> Option<IpAddr> {
|
||||||
match &packet.ip {
|
match &packet.ip {
|
||||||
Some(InternetSlice::Ipv4(header)) => Some(IpAddr::V4(header.destination_addr())),
|
Some(InternetSlice::Ipv4(header)) => Some(IpAddr::V4(header.destination_addr())),
|
||||||
|
Loading…
Reference in New Issue
Block a user