-
Notifications
You must be signed in to change notification settings - Fork 58
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
rm1984
wants to merge
1
commit into
jvns:main
Choose a base branch
from
rm1984:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -34,6 +34,7 @@ struct OrigPacket { | |
struct Opts { | ||
source: Source, | ||
timestamp: bool, | ||
nic: String, | ||
} | ||
|
||
#[derive(Clone)] | ||
|
@@ -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", | ||
|
@@ -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()) | ||
} | ||
}; | ||
if matches.opt_present("h") { | ||
|
@@ -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(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") { | ||
|
@@ -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.")? | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this produced a warning