Skip to content

Commit

Permalink
added pub mod:: to expose the correct find_af_inet implementation to …
Browse files Browse the repository at this point in the history
…the user
  • Loading branch information
nicguzzo authored and EstebanBorai committed Jun 19, 2021
1 parent 7c1de78 commit 266f69d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 41 deletions.
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@

# Reorder import statements alphabetically.
# Default: false
# reorder_imports =
reorder_imports = false

# Reorder lists of names in import statements alphabetically.
# Default: false
Expand Down
27 changes: 2 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,11 @@ pub enum Error {
#[cfg(target_family = "unix")]
pub mod unix;
#[cfg(target_family = "unix")]
use crate::unix::*;
pub use crate::unix::*;
#[cfg(target_family = "windows")]
pub mod windows;
#[cfg(target_family = "windows")]
use crate::windows::*;

/// Perform a search over the system's network interfaces using `getifaddrs`,
/// retrieved network interfaces belonging to both socket address families
/// `AF_INET` and `AF_INET6` are retrieved along with the interface address name.
///
/// # Example
///
/// ```
/// use std::net::IpAddr;
/// use local_ip_address::find_af_inet;
///
/// let ifas = find_af_inet().unwrap();
///
/// if let Some((_, ipaddr)) = ifas
/// .iter()
/// .find(|(name, ipaddr)| *name == "en0" && matches!(ipaddr, IpAddr::V4(_))) {
/// // This is your local IP address: 192.168.1.111
/// println!("This is your local IP address: {:?}", ipaddr);
/// }
/// ```
pub fn find_af_inet() -> Result<Vec<(String, IpAddr)>, Error> {
impl_find_af_inet()
}
pub use crate::windows::*;

/// Finds the network interface with the provided name in the vector of network
/// interfaces provided
Expand Down
21 changes: 20 additions & 1 deletion src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
/// `ifaddrs` struct raw pointer alias
type IfAddrsPtr = *mut *mut ifaddrs;

pub fn impl_find_af_inet() -> Result<Vec<(String, IpAddr)>, Error> {
/// Perform a search over the system's network interfaces using `getifaddrs`,
/// retrieved network interfaces belonging to both socket address families
/// `AF_INET` and `AF_INET6` are retrieved along with the interface address name.
///
/// # Example
///
/// ```
/// use std::net::IpAddr;
/// use local_ip_address::find_af_inet;
///
/// let ifas = find_af_inet().unwrap();
///
/// if let Some((_, ipaddr)) = ifas
/// .iter()
/// .find(|(name, ipaddr)| *name == "en0" && matches!(ipaddr, IpAddr::V4(_))) {
/// // This is your local IP address: 192.168.1.111
/// println!("This is your local IP address: {:?}", ipaddr);
/// }
/// ```
pub fn find_af_inet() -> Result<Vec<(String, IpAddr)>, Error> {
let ifaddrs_size = mem::size_of::<IfAddrsPtr>();

unsafe {
Expand Down
9 changes: 5 additions & 4 deletions src/windows/bindings/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
fn main() {
windows::build! {
Windows::Win32::NetworkManagement::IpHelper::{GetIpAddrTable,GetInterfaceInfo,GetAdaptersAddresses},
Windows::Win32::Networking::WinSock::{SOCKADDR_IN,SOCKADDR_IN6},
Windows::Win32::System::Diagnostics::Debug::*
//Windows::Win32::System::Diagnostics::Debug::{ERROR_BUFFER_OVERFLOW, ERROR_INSUFFICIENT_BUFFER, NO_ERROR}
Windows::Win32::{
NetworkManagement::IpHelper::GetAdaptersAddresses,
Networking::WinSock::{SOCKADDR_IN,SOCKADDR_IN6},
System::Diagnostics::Debug::*
}
};
}
40 changes: 30 additions & 10 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
use crate::Error;

use bindings::Windows::Win32::NetworkManagement::IpHelper::{
GetAdaptersAddresses, ADDRESS_FAMILY, AF_INET, AF_INET6, AF_UNSPEC,
GET_ADAPTERS_ADDRESSES_FLAGS, IP_ADAPTER_ADDRESSES_LH,
use bindings::Windows::Win32::{
NetworkManagement::IpHelper::{
ADDRESS_FAMILY, AF_INET, AF_INET6, AF_UNSPEC, GET_ADAPTERS_ADDRESSES_FLAGS,
IP_ADAPTER_ADDRESSES_LH, GetAdaptersAddresses,
},
Networking::WinSock::{SOCKADDR_IN, SOCKADDR_IN6},
System::Diagnostics::Debug::{ERROR_BUFFER_OVERFLOW, NO_ERROR},
};

use bindings::Windows::Win32::Networking::WinSock::{SOCKADDR_IN, SOCKADDR_IN6};
use bindings::Windows::Win32::System::Diagnostics::Debug::{ERROR_BUFFER_OVERFLOW, NO_ERROR};
use libc::{wchar_t, wcslen};
use memalloc::{allocate, deallocate};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

pub fn impl_find_af_inet() -> Result<Vec<(String, IpAddr)>, Error> {
use crate::Error;

/// Perform a search over the system's network interfaces using `getifaddrs`,
/// retrieved network interfaces belonging to both socket address families
/// `AF_INET` and `AF_INET6` are retrieved along with the interface address name.
///
/// # Example
///
/// ```
/// use std::net::IpAddr;
/// use local_ip_address::find_af_inet;
///
/// let ifas = find_af_inet().unwrap();
///
/// if let Some((_, ipaddr)) = ifas
/// .iter()
/// .find(|(name, ipaddr)| *name == "en0" && matches!(ipaddr, IpAddr::V4(_))) {
/// // This is your local IP address: 192.168.1.111
/// println!("This is your local IP address: {:?}", ipaddr);
/// }
/// ```
pub fn find_af_inet() -> Result<Vec<(String, IpAddr)>, Error> {
let mut out: Vec<(String, IpAddr)> = Vec::new();
let mut dwsize: u32 = 1500;
let mut dwsize: u32 = 2000; //20kb should be enough to prevent realloc

let mut mem = unsafe { allocate(dwsize as usize) } as *mut IP_ADAPTER_ADDRESSES_LH;

Expand Down Expand Up @@ -54,7 +75,6 @@ pub fn impl_find_af_inet() -> Result<Vec<(String, IpAddr)>, Error> {
let a = unsafe { (*sockaddr).sin6_addr.u.Byte };
let ipv6 = Ipv6Addr::from(a);
let ip = IpAddr::V6(ipv6);
//println!("ipv6 {}", ip);
let name = String::from_utf16(slice).unwrap();
out.push((name, ip));
} else if sockaddr.sa_family == AF_INET.0 as u16 {
Expand Down

0 comments on commit 266f69d

Please sign in to comment.