Skip to content

Commit

Permalink
[#13] Allow specifying multiple interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
msiodelski committed Feb 7, 2024
1 parent ef05081 commit 77b8f20
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
12 changes: 7 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum Commands {
Collect {
/// Interface name.
#[arg(short, long)]
interface_name: String,
interface_name: Vec<String>,
},
}

Expand All @@ -57,13 +57,15 @@ impl Cli {
let args = Cli::parse();
if let Some(commands) = args.commands {
match commands {
Commands::Collect { interface_name } => {
Commands::Collect { interface_name: interface_names } => {
Cli::install_signal_handler();
let mut dispatcher = dispatcher::Dispatcher::new();
let filter = Filter::new().bootp_server_relay();
dispatcher
.add_listener(interface_name.as_str(), filter)
.expect("listener already added");
for interface_name in interface_names.iter() {
dispatcher
.add_listener(interface_name.as_str(), &filter)
.expect("listener already added");
};
dispatcher
.add_timer(timer::Type::DataScrape, 3000)
.expect("timer already added");
Expand Down
8 changes: 4 additions & 4 deletions src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Dispatcher {
/// The [Filter] applies filtering rules for packets capturing. For example,
/// it can be used to filter only BOOTP packets, only UDP packets, select
/// port number etc.
pub fn add_listener(&mut self, interface_name: &str, filter: Filter) -> Result<(), Error> {
pub fn add_listener(&mut self, interface_name: &str, filter: &Filter) -> Result<(), Error> {
if self.listeners.contains_key(interface_name) {
return Err(Error::AddListenerExists);
}
Expand Down Expand Up @@ -168,12 +168,12 @@ mod tests {
fn add_listener() {
let mut dispatcher = Dispatcher::new();
let filter = Filter::new().udp();
assert_eq!(dispatcher.add_listener("lo", filter), Ok(()));
assert_eq!(dispatcher.add_listener("lo", &filter), Ok(()));
assert_eq!(
dispatcher.add_listener("lo", Filter::new()),
dispatcher.add_listener("lo", &Filter::new()),
Err(AddListenerExists)
);
assert_eq!(dispatcher.add_listener("lo0", Filter::new()), Ok(()));
assert_eq!(dispatcher.add_listener("lo0", &Filter::new()), Ok(()));
assert_eq!(dispatcher.listeners.len(), 2);
assert!(dispatcher.listeners.contains_key("lo"));
assert!(dispatcher.listeners.contains_key("lo0"));
Expand Down
14 changes: 7 additions & 7 deletions src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ trait State {
///
/// A filter is only applied when the listener is in the [Inactive]
/// state.
fn filter(self: Box<Self>, packet_filter: Filter) -> Box<dyn State>;
fn filter(self: Box<Self>, packet_filter: &Filter) -> Box<dyn State>;

/// Starts the listener thread if not started yet.
///
Expand Down Expand Up @@ -88,7 +88,7 @@ struct Inactive {
struct Active {}

/// An enum of protocols used for filtering.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Proto {
/// Filtering by BOOTP or DHCPv4 messages.
Bootp,
Expand All @@ -107,7 +107,7 @@ pub enum Proto {
/// ```rust
/// let filter = Filter::new().udp().port(10067);
/// ```
#[derive(Debug)]
#[derive(Copy, Debug)]
pub struct Filter {
proto: Option<Proto>,
port: Option<u16>,
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Listener {
///
/// - `packet_filter` - a packet filter instance used for capturing
/// a specific type of the packets.
pub fn filter(&mut self, packet_filter: Filter) {
pub fn filter(&mut self, packet_filter: &Filter) {
if let Some(s) = self.state.take() {
self.state = Some(s.filter(packet_filter))
}
Expand All @@ -161,10 +161,10 @@ impl Listener {
}

impl State for Inactive {
fn filter(self: Box<Self>, packet_filter: Filter) -> Box<dyn State> {
fn filter(self: Box<Self>, packet_filter: &Filter) -> Box<dyn State> {
Box::new(Inactive {
interface_name: self.interface_name.to_string(),
filter: Some(packet_filter),
filter: Some(packet_filter.clone()),
})
}

Expand Down Expand Up @@ -201,7 +201,7 @@ impl State for Inactive {
}

impl State for Active {
fn filter(self: Box<Self>, _packet_filter: Filter) -> Box<dyn State> {
fn filter(self: Box<Self>, _packet_filter: &Filter) -> Box<dyn State> {
self
}

Expand Down

0 comments on commit 77b8f20

Please sign in to comment.