Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to specify the network interface (now it works on FreeBSD) #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use futures::StreamExt;
use getopts::Options;
use hex::encode;
use pcap::stream::{PacketCodec, PacketStream};
use pcap::{Active, Capture, Linktype, Packet};
use pcap::{Active, Capture, Device, Linktype, Packet};
use std::collections::HashMap;
use std::env;
use std::net::IpAddr;
Expand All @@ -34,6 +34,7 @@ struct OrigPacket {
struct Opts {
source: Source,
timestamp: bool,
nic: String,
}

#[derive(Clone)]
Expand Down Expand Up @@ -99,6 +100,7 @@ fn parse_args() -> Result<Opts> {

let mut opts = Options::new();
opts.optopt("p", "port", "port number to listen on", "PORT");
opts.optopt("i", "interface", "network interface to listen on", "NIC");
opts.optopt("f", "file", "read packets from pcap file", "FILENAME");
opts.optflag(
"t",
Expand All @@ -109,7 +111,7 @@ fn parse_args() -> Result<Opts> {
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => {
panic!(f.to_string())
panic!("{}", f.to_string())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this produced a warning

}
};
if matches.opt_present("h") {
Expand All @@ -119,8 +121,13 @@ fn parse_args() -> Result<Opts> {
let mut opts = Opts {
source: Source::Port(53),
timestamp: matches.opt_present("t"),
nic: "any".to_string(),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default value to "any"

};

if let Some(nic) = matches.opt_str("i") {
opts.nic = nic.to_string();
}

if let Some(filename) = matches.opt_str("f") {
opts.source = Source::Filename(filename.to_string());
} else if let Some(port_str) = matches.opt_str("p") {
Expand Down Expand Up @@ -169,8 +176,13 @@ fn capture_stream(
map: Arc<Mutex<HashMap<u16, OrigPacket>>>,
port: u16,
) -> Result<PacketStream<Active, PrintCodec>> {
let mut cap = Capture::from_device("any")
.wrap_err("Failed to find device 'any'")?
let _nic = Device {
name: opts.nic.clone(),
desc: std::option::Option::None,
};
let wrap_err_msg = format!("Failed to find device '{}'", opts.nic);
let mut cap = Capture::from_device(_nic)
.wrap_err(wrap_err_msg)?
.immediate_mode(true)
.open()
.wrap_err("Failed to start. This may be because you need to run this as root.")?
Expand Down