Add examples

main
shellrow 4 years ago
parent c3d9642e6c
commit 16fa48a8a6
  1. 5
      examples/default_gateway.rs
  2. 32
      src/lib.rs

@ -0,0 +1,5 @@
use default_net;
fn main(){
default_net::get_default_gateway();
}

@ -1,13 +1,13 @@
use std::net::UdpSocket; use std::net::UdpSocket;
use pnet::transport::TransportChannelType::Layer4; use pnet::transport::TransportChannelType::Layer4;
use pnet::transport::TransportProtocol::Ipv4; use pnet::transport::TransportProtocol::Ipv4;
use std::time; //use std::time;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use std::net::IpAddr; use std::net::IpAddr;
//use pnet::packet::Packet; //use pnet::packet::Packet;
use pnet::transport::icmp_packet_iter; use pnet::transport::icmp_packet_iter;
pub fn get_router_ip(){ pub fn get_default_gateway(){
let buf = [0u8; 0]; let buf = [0u8; 0];
let socket = match UdpSocket::bind("0.0.0.0:0") { let socket = match UdpSocket::bind("0.0.0.0:0") {
Ok(s) => s, 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 mut iter = icmp_packet_iter(rx);
let start_time = Instant::now(); let start_time = Instant::now();
loop { loop {
match iter.next_with_timeout(time::Duration::from_millis(100)) { match iter.next_with_timeout(timeout) {
Ok(r) => { Ok(r) => {
if let Some((packet, addr)) = r { if let Some((packet, addr)) = r {
if packet.get_icmp_type() == icmp_type { 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")] #[cfg(target_os = "windows")]
pub fn receive_icmp_packets(rx: &mut pnet::transport::TransportReceiver, icmp_type: pnet::packet::icmp::IcmpType, timeout: &Duration) -> Result<String, String>{ 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<String, String>{
let mut iter = icmp_packet_iter(rx); let mut iter = icmp_packet_iter(rx);
let start_time = Instant::now(); let start_time = Instant::now();
loop { loop {
match iter.next() { match iter.next() {
Ok((packet, addr)) => { Ok((packet, addr)) => {
if let Some((packet, addr)) = r { if packet.get_icmp_type() == icmp_type {
if packet.get_icmp_type() == icmp_type { match addr {
match addr { IpAddr::V4(ipv4_addr) =>{return Ok(ipv4_addr.to_string())},
IpAddr::V4(ipv4_addr) =>{return Ok(ipv4_addr.to_string())}, IpAddr::V6(ipv6_addr) =>{return Ok(ipv6_addr.to_string())},
IpAddr::V6(ipv6_addr) =>{return Ok(ipv6_addr.to_string())},
}
} }
}else{
return Err(String::from("Failed to read packet"));
} }
}, },
Err(e) => { Err(e) => {
return Err(format!("An error occurred while reading: {}", e)); return Err(format!("An error occurred while reading: {}", e));
} }
} }
} if Instant::now().duration_since(start_time) > *timeout {
if Instant::now().duration_since(start_time) > *timeout { return Err(String::from("timeout"));
return Err(String::from("timeout")); }
} }
} }

Loading…
Cancel
Save