Add examples

main
shellrow 4 years ago
parent c3d9642e6c
commit 16fa48a8a6
  1. 5
      examples/default_gateway.rs
  2. 20
      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 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,32 +57,34 @@ 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<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 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())},
}
}
}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"));
}
}
}
#[cfg(test)]

Loading…
Cancel
Save