From 1346c5a8526b137bddcac6ecd43165a1d95a6930 Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Sun, 11 Apr 2021 18:11:26 +0900 Subject: [PATCH] Add doc --- README.md | 37 ++++++++++++++++++++++++++++++++++++- src/gateway.rs | 4 ++++ src/interface.rs | 38 +++++++++++++++++++++----------------- src/lib.rs | 7 ------- 4 files changed, 61 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index ef36748..a19928b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,37 @@ # default-net -Get default network interface information +Get default network information +`default-net` provides a cross-platform API for network interface and gateway. + +## Supported platform +- Linux +- macOS(OS X) +- Windows + +## Usage +Add `default-net` to your dependencies +```toml:Cargo.toml +[dependencies] +default-net = "0.1.0" +``` + +## Example +```rust +use default_net; + +fn main(){ + if let Some(default_interface) = default_net::get_default_interface(){ + println!("Index {}", default_interface.index); + println!("Name {}", default_interface.name); + println!("MAC {:?}", default_interface.mac); + println!("IPv4 {:?}", default_interface.ipv4); + println!("IPv6 {:?}", default_interface.ipv6); + println!("Gateway IP {:?}", default_interface.gateway.ip); + println!("Gateway MAC {:?}", default_interface.gateway.mac); + }else{ + println!("Failed to get default interface info"); + } +} +``` + +For more details, see examples or doc. + diff --git a/src/gateway.rs b/src/gateway.rs index 29968d9..35846f7 100644 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -16,11 +16,13 @@ use pnet::datalink; use crate::interface; +/// Struct of default Gateway information pub struct Gateway { pub ip: Option, pub mac: Option, } +/// Get default Gateway pub fn get_default_gateway() -> Gateway { let mut gateway: Gateway = Gateway { ip: None, @@ -41,6 +43,7 @@ pub fn get_default_gateway() -> Gateway { return gateway; } +/// Get default Gateway IP address pub fn get_default_gateway_ip() -> Result{ send_udp_packet(); let timeout = Duration::from_millis(3000); @@ -48,6 +51,7 @@ pub fn get_default_gateway_ip() -> Result{ return r; } +/// Get default Gateway MAC address pub fn get_default_gateway_mac(gateway_ip: String) -> Result{ match gateway_ip.parse::(){ Ok(ipv4_addr) => { diff --git a/src/interface.rs b/src/interface.rs index d0e6c42..7b84cba 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -3,6 +3,7 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use pnet::datalink; use crate::gateway; +/// Struct of default Network Interface information pub struct Interface { pub index: u32, pub name: String, @@ -12,23 +13,7 @@ pub struct Interface { pub gateway: gateway::Gateway, } -fn get_local_ipaddr() -> Option { - let socket = match UdpSocket::bind("0.0.0.0:0") { - Ok(s) => s, - Err(_) => return None, - }; - - match socket.connect("8.8.8.8:80") { - Ok(()) => (), - Err(_) => return None, - }; - - match socket.local_addr() { - Ok(addr) => return Some(addr.ip().to_string()), - Err(_) => return None, - }; -} - +/// Get default Interface pub fn get_default_interface()-> Option { let local_ip = get_local_ipaddr(); let all_interfaces = datalink::interfaces(); @@ -71,6 +56,7 @@ pub fn get_default_interface()-> Option { } } +/// Get default Interface index pub fn get_default_interface_index() -> Option { let local_ip = get_local_ipaddr(); let all_interfaces = datalink::interfaces(); @@ -88,6 +74,7 @@ pub fn get_default_interface_index() -> Option { } } +/// Get default Interface name pub fn get_default_interface_name() -> Option { let local_ip = get_local_ipaddr(); let all_interfaces = datalink::interfaces(); @@ -104,3 +91,20 @@ pub fn get_default_interface_name() -> Option { return None; } } + +fn get_local_ipaddr() -> Option { + let socket = match UdpSocket::bind("0.0.0.0:0") { + Ok(s) => s, + Err(_) => return None, + }; + + match socket.connect("8.8.8.8:80") { + Ok(()) => (), + Err(_) => return None, + }; + + match socket.local_addr() { + Ok(addr) => return Some(addr.ip().to_string()), + Err(_) => return None, + }; +} diff --git a/src/lib.rs b/src/lib.rs index 868f2b5..bbc4be2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,10 +11,3 @@ pub use gateway::get_default_gateway; pub use gateway::get_default_gateway_ip; pub use gateway::get_default_gateway_mac; -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -}