main
shellrow 4 years ago
parent ecae4273e4
commit 1346c5a852
  1. 37
      README.md
  2. 4
      src/gateway.rs
  3. 38
      src/interface.rs
  4. 7
      src/lib.rs

@ -1,2 +1,37 @@
# default-net # 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.

@ -16,11 +16,13 @@ use pnet::datalink;
use crate::interface; use crate::interface;
/// Struct of default Gateway information
pub struct Gateway { pub struct Gateway {
pub ip: Option<String>, pub ip: Option<String>,
pub mac: Option<String>, pub mac: Option<String>,
} }
/// Get default Gateway
pub fn get_default_gateway() -> Gateway { pub fn get_default_gateway() -> Gateway {
let mut gateway: Gateway = Gateway { let mut gateway: Gateway = Gateway {
ip: None, ip: None,
@ -41,6 +43,7 @@ pub fn get_default_gateway() -> Gateway {
return gateway; return gateway;
} }
/// Get default Gateway IP address
pub fn get_default_gateway_ip() -> Result<String,String>{ pub fn get_default_gateway_ip() -> Result<String,String>{
send_udp_packet(); send_udp_packet();
let timeout = Duration::from_millis(3000); let timeout = Duration::from_millis(3000);
@ -48,6 +51,7 @@ pub fn get_default_gateway_ip() -> Result<String,String>{
return r; return r;
} }
/// Get default Gateway MAC address
pub fn get_default_gateway_mac(gateway_ip: String) -> Result<String,String>{ pub fn get_default_gateway_mac(gateway_ip: String) -> Result<String,String>{
match gateway_ip.parse::<Ipv4Addr>(){ match gateway_ip.parse::<Ipv4Addr>(){
Ok(ipv4_addr) => { Ok(ipv4_addr) => {

@ -3,6 +3,7 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use pnet::datalink; use pnet::datalink;
use crate::gateway; use crate::gateway;
/// Struct of default Network Interface information
pub struct Interface { pub struct Interface {
pub index: u32, pub index: u32,
pub name: String, pub name: String,
@ -12,23 +13,7 @@ pub struct Interface {
pub gateway: gateway::Gateway, pub gateway: gateway::Gateway,
} }
fn get_local_ipaddr() -> Option<String> { /// Get default Interface
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,
};
}
pub fn get_default_interface()-> Option<Interface> { pub fn get_default_interface()-> Option<Interface> {
let local_ip = get_local_ipaddr(); let local_ip = get_local_ipaddr();
let all_interfaces = datalink::interfaces(); let all_interfaces = datalink::interfaces();
@ -71,6 +56,7 @@ pub fn get_default_interface()-> Option<Interface> {
} }
} }
/// Get default Interface index
pub fn get_default_interface_index() -> Option<u32> { pub fn get_default_interface_index() -> Option<u32> {
let local_ip = get_local_ipaddr(); let local_ip = get_local_ipaddr();
let all_interfaces = datalink::interfaces(); let all_interfaces = datalink::interfaces();
@ -88,6 +74,7 @@ pub fn get_default_interface_index() -> Option<u32> {
} }
} }
/// Get default Interface name
pub fn get_default_interface_name() -> Option<String> { pub fn get_default_interface_name() -> Option<String> {
let local_ip = get_local_ipaddr(); let local_ip = get_local_ipaddr();
let all_interfaces = datalink::interfaces(); let all_interfaces = datalink::interfaces();
@ -104,3 +91,20 @@ pub fn get_default_interface_name() -> Option<String> {
return None; return None;
} }
} }
fn get_local_ipaddr() -> Option<String> {
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,
};
}

@ -11,10 +11,3 @@ pub use gateway::get_default_gateway;
pub use gateway::get_default_gateway_ip; pub use gateway::get_default_gateway_ip;
pub use gateway::get_default_gateway_mac; pub use gateway::get_default_gateway_mac;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

Loading…
Cancel
Save