From 16fa48a8a6ad2ea7c0ebfa6a41650d795b0b7016 Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Sun, 11 Apr 2021 00:29:26 +0900 Subject: [PATCH] Add examples --- examples/default_gateway.rs | 5 +++++ src/lib.rs | 32 +++++++++++++++++--------------- 2 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 examples/default_gateway.rs diff --git a/examples/default_gateway.rs b/examples/default_gateway.rs new file mode 100644 index 0000000..ca6a1a8 --- /dev/null +++ b/examples/default_gateway.rs @@ -0,0 +1,5 @@ +use default_net; + +fn main(){ + default_net::get_default_gateway(); +} diff --git a/src/lib.rs b/src/lib.rs index 469c514..0286bbd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,13 @@ use std::net::UdpSocket; use pnet::transport::TransportChannelType::Layer4; use pnet::transport::TransportProtocol::Ipv4; -use std::time; +//use std::time; use std::time::{Duration, Instant}; use std::net::IpAddr; //use pnet::packet::Packet; use pnet::transport::icmp_packet_iter; -pub fn get_router_ip(){ +pub fn get_default_gateway(){ let buf = [0u8; 0]; let socket = match UdpSocket::bind("0.0.0.0:0") { Ok(s) => s, @@ -34,7 +34,7 @@ pub fn receive_icmp_packets(rx: &mut pnet::transport::TransportReceiver, icmp_ty let mut iter = icmp_packet_iter(rx); let start_time = Instant::now(); loop { - match iter.next_with_timeout(time::Duration::from_millis(100)) { + match iter.next_with_timeout(timeout) { Ok(r) => { if let Some((packet, addr)) = r { if packet.get_icmp_type() == icmp_type { @@ -57,31 +57,33 @@ pub fn receive_icmp_packets(rx: &mut pnet::transport::TransportReceiver, icmp_ty } } +/* #[cfg(target_os = "windows")] -pub fn receive_icmp_packets(rx: &mut pnet::transport::TransportReceiver, icmp_type: pnet::packet::icmp::IcmpType, timeout: &Duration) -> Result{ +fn send_icmp_packet(){ + +} +*/ +#[cfg(target_os = "windows")] +fn receive_icmp_packets(rx: &mut pnet::transport::TransportReceiver, icmp_type: pnet::packet::icmp::IcmpType, timeout: &Duration) -> Result{ let mut iter = icmp_packet_iter(rx); let start_time = Instant::now(); loop { match iter.next() { Ok((packet, addr)) => { - if let Some((packet, addr)) = r { - if packet.get_icmp_type() == icmp_type { - match addr { - IpAddr::V4(ipv4_addr) =>{return Ok(ipv4_addr.to_string())}, - IpAddr::V6(ipv6_addr) =>{return Ok(ipv6_addr.to_string())}, - } + if packet.get_icmp_type() == icmp_type { + match addr { + IpAddr::V4(ipv4_addr) =>{return Ok(ipv4_addr.to_string())}, + IpAddr::V6(ipv6_addr) =>{return Ok(ipv6_addr.to_string())}, } - }else{ - return Err(String::from("Failed to read packet")); } }, Err(e) => { return Err(format!("An error occurred while reading: {}", e)); } } - } - if Instant::now().duration_since(start_time) > *timeout { - return Err(String::from("timeout")); + if Instant::now().duration_since(start_time) > *timeout { + return Err(String::from("timeout")); + } } }