From 9c25eb80baf9c5d87dc19f6971663d89f814580d Mon Sep 17 00:00:00 2001 From: Eric Mrak Date: Thu, 29 Aug 2024 09:38:10 -0700 Subject: [PATCH] some unwrap clarifications --- src/main.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index de78c52..23e538a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -140,7 +140,7 @@ fn capture_packets(settings: &Settings, sender: Sender<(u32, Vec)>) { match rx.next() { Ok(packet) => child_snd .send((interface.index, packet.to_owned())) - .unwrap(), + .expect("packet should be able to be sent on channel"), Err(e) => panic!("nx: Unable to receive packet: {}", e), }; } @@ -156,16 +156,19 @@ fn print_packets(settings: &Settings, receiver: Receiver<(u32, Vec)>) { loop { match receiver.recv() { Ok((index, packet)) => { - let ethernet_packet = EthernetPacket::new(packet.as_slice()).unwrap(); - match ethernet_packet.get_ethertype() { - EtherTypes::Ipv4 => { - process_ipv4(settings, &inames[&index][..], ðernet_packet) - } - EtherTypes::Ipv6 => { - process_ipv6(settings, &inames[&index][..], ðernet_packet) + if let Some(ethernet_packet) = EthernetPacket::new(packet.as_slice()) { + match ethernet_packet.get_ethertype() { + EtherTypes::Ipv4 => { + process_ipv4(settings, &inames[&index][..], ðernet_packet) + } + EtherTypes::Ipv6 => { + process_ipv6(settings, &inames[&index][..], ðernet_packet) + } + EtherTypes::Arp => { + process_arp(settings, &inames[&index][..], ðernet_packet) + } + _ => {} } - EtherTypes::Arp => process_arp(settings, &inames[&index][..], ðernet_packet), - _ => {} } } Err(_) => panic!("All interfaces closed"),