Support Windows ICMP issues (pnet v0.27)

main
shellrow 4 years ago
parent 67a82ee5d8
commit 3d25873c4f
  1. 6
      Cargo.toml
  2. 28
      src/gateway.rs

@ -10,6 +10,8 @@ keywords = ["network"]
categories = ["network-programming"] categories = ["network-programming"]
license = "MIT" license = "MIT"
[dependencies] [target.'cfg(not(windows))'.dependencies]
pnet = "0.27"
[target.'cfg(target_os = "windows")'.dependencies]
pnet = "0.26" pnet = "0.26"
pnet_datalink = "0.27"

@ -65,29 +65,43 @@ fn send_udp_packet(){
socket.send_to(&buf, dest).unwrap(); socket.send_to(&buf, dest).unwrap();
} }
#[cfg(not(windows))]
fn receive_icmp_packets(icmp_type: pnet::packet::icmp::IcmpType, timeout: &Duration) -> Result<String, String>{ fn receive_icmp_packets(icmp_type: pnet::packet::icmp::IcmpType, timeout: &Duration) -> Result<String, String>{
let default_idx = interface::get_default_interface_index().unwrap(); let default_idx = interface::get_default_interface_index().unwrap();
let interfaces = pnet_datalink::interfaces(); let interfaces = pnet::datalink::interfaces();
let interface = interfaces.into_iter().filter(|interface: &pnet_datalink::NetworkInterface| interface.index == default_idx).next().expect("Failed to get Interface"); let interface = interfaces.into_iter().filter(|interface: &pnet::datalink::NetworkInterface| interface.index == default_idx).next().expect("Failed to get Interface");
let config = pnet_datalink::Config { let config = pnet::datalink::Config {
write_buffer_size: 4096, write_buffer_size: 4096,
read_buffer_size: 4096, read_buffer_size: 4096,
read_timeout: None, read_timeout: None,
write_timeout: None, write_timeout: None,
channel_type: pnet_datalink::ChannelType::Layer2, channel_type: pnet::datalink::ChannelType::Layer2,
bpf_fd_attempts: 1000, bpf_fd_attempts: 1000,
linux_fanout: None, linux_fanout: None,
promiscuous: false, promiscuous: false,
}; };
let (mut _tx, mut rx) = match pnet_datalink::channel(&interface, config) { let (mut _tx, mut rx) = match pnet::datalink::channel(&interface, config) {
Ok(pnet_datalink::Channel::Ethernet(tx, rx)) => (tx, rx), Ok(pnet::datalink::Channel::Ethernet(tx, rx)) => (tx, rx),
Ok(_) => panic!("Unknown channel type"),
Err(e) => panic!("Error happened {}", e),
};
receive_packets(&mut rx, icmp_type, timeout)
}
#[cfg(target_os = "windows")]
fn receive_icmp_packets(icmp_type: pnet::packet::icmp::IcmpType, timeout: &Duration) -> Result<String, String>{
let default_idx = interface::get_default_interface_index().unwrap();
let interfaces = pnet::datalink::interfaces();
let interface = interfaces.into_iter().filter(|interface: &pnet::datalink::NetworkInterface| interface.index == default_idx).next().expect("Failed to get Interface");
let (mut _tx, mut rx) = match pnet::datalink::channel(&interface, Default::default()) {
Ok(pnet::datalink::Channel::Ethernet(tx, rx)) => (tx, rx),
Ok(_) => panic!("Unknown channel type"), Ok(_) => panic!("Unknown channel type"),
Err(e) => panic!("Error happened {}", e), Err(e) => panic!("Error happened {}", e),
}; };
receive_packets(&mut rx, icmp_type, timeout) receive_packets(&mut rx, icmp_type, timeout)
} }
fn receive_packets(rx: &mut Box<dyn pnet_datalink::DataLinkReceiver>, icmp_type: pnet::packet::icmp::IcmpType, timeout: &Duration) -> Result<String, String>{ fn receive_packets(rx: &mut Box<dyn pnet::datalink::DataLinkReceiver>, icmp_type: pnet::packet::icmp::IcmpType, timeout: &Duration) -> Result<String, String>{
let start_time = Instant::now(); let start_time = Instant::now();
loop { loop {
match rx.next() { match rx.next() {

Loading…
Cancel
Save