Refactor main
This commit is contained in:
45
src/main.rs
45
src/main.rs
@@ -85,7 +85,7 @@ struct Arg {
|
|||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let arg = Arg::parse();
|
let arg = Arg::parse();
|
||||||
let mut file = arg.output.map(|path| {
|
let mut output = arg.output.map(|path| {
|
||||||
//TODO automatically add file extension
|
//TODO automatically add file extension
|
||||||
let file_out = File::create(path).expect("Error opening file for writing");
|
let file_out = File::create(path).expect("Error opening file for writing");
|
||||||
let header = PcapHeader {
|
let header = PcapHeader {
|
||||||
@@ -101,8 +101,8 @@ fn main() -> Result<()> {
|
|||||||
let file = io::BufReader::new(io::stdin());
|
let file = io::BufReader::new(io::stdin());
|
||||||
thread::spawn(move || tnc_loop(tx, file).unwrap());
|
thread::spawn(move || tnc_loop(tx, file).unwrap());
|
||||||
}
|
}
|
||||||
s if Path::new(s).exists() => {
|
path if Path::new(path).exists() => {
|
||||||
let file = io::BufReader::new(File::open(s)?);
|
let file = io::BufReader::new(File::open(path)?);
|
||||||
thread::spawn(move || tnc_loop(tx, file).unwrap());
|
thread::spawn(move || tnc_loop(tx, file).unwrap());
|
||||||
}
|
}
|
||||||
addr => {
|
addr => {
|
||||||
@@ -113,37 +113,28 @@ fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while let Ok(pkt) = rx.recv() {
|
while let Ok(pkt) = rx.recv() {
|
||||||
//let time = SystemTime::now();
|
if let Ok(frame) = Ax25Frame::from_bytes(&pkt.data[1..]) {
|
||||||
//let timestamp = time.duration_since(SystemTime::UNIX_EPOCH);
|
if arg.verbose {
|
||||||
let mut s = String::new();
|
|
||||||
for byte in pkt.data.iter() {
|
|
||||||
//if byte >= ' '.into() && byte <= '~'.into() {
|
|
||||||
if *byte >= 32 && *byte <= 126 {
|
|
||||||
s.push((*byte).into());
|
|
||||||
} else {
|
|
||||||
s.push_str(&format!("\\{:x?} ", *byte));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if arg.verbose {
|
|
||||||
if let Ok(frame) = Ax25Frame::from_bytes(&pkt.data[1..]) {
|
|
||||||
frame.print_long();
|
frame.print_long();
|
||||||
} else {
|
} else {
|
||||||
//TODO print time stamp
|
frame.print_short();
|
||||||
println!("{}", s);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if let Ok(frame) = Ax25Frame::from_bytes(&pkt.data[1..]) {
|
let mut s = String::new();
|
||||||
frame.print_short();
|
for byte in pkt.data.iter() {
|
||||||
} else {
|
if *byte >= 32 && *byte <= 126 {
|
||||||
//TODO print time stamp
|
s.push((*byte).into());
|
||||||
println!("{}", s);
|
} else {
|
||||||
|
s.push_str(&format!("\\{:x?} ", *byte));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
//TODO print time stamp
|
||||||
|
println!("{}", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref mut writer) = file {
|
if let Some(ref mut writer) = output {
|
||||||
let pkt = PcapPacket::new_owned(pkt.timestamp, pkt.orig_len, pkt.data);
|
let pcpkt = PcapPacket::new_owned(pkt.timestamp, pkt.orig_len, pkt.data);
|
||||||
writer.write_packet(&pkt).unwrap();
|
writer.write_packet(&pcpkt).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user