Skip to content

Commit

Permalink
Merge pull request #12 from djc/no-get
Browse files Browse the repository at this point in the history
Deprecate `get_()` prefix
  • Loading branch information
larseggert authored Sep 3, 2024
2 parents fbf8d71 + 67f3dc4 commit 8d9a619
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A crate to return the maximum transmission unit (MTU) of the local network inter
This crate exports a single function

```rust
pub fn get_interface_mtu(remote: &SocketAddr) -> Result<usize, Error>
pub fn interface_mtu(remote: &SocketAddr) -> Result<usize, Error>
```

that returns the MTU of the local network interface towards the `remote` destination, or an `Error` when the MTU could not be determined. It supports both IPv4 and IPv6.
Expand Down
28 changes: 17 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ fn default_result<T>() -> Result<T, Error> {
///
/// ```
/// let saddr = "127.0.0.1:443".parse().unwrap();
/// let mtu = mtu::get_interface_mtu(&saddr).unwrap();
/// println!("MTU towards {:?} is {}", saddr, mtu);
/// let mtu = mtu::interface_mtu(&saddr).unwrap();
/// println!("MTU towards {saddr:?} is {mtu}");
/// ```
///
/// # Errors
///
/// This function returns an error if the local interface MTU cannot be determined.
pub fn get_interface_mtu(remote: &SocketAddr) -> Result<usize, Error> {
pub fn interface_mtu(remote: &SocketAddr) -> Result<usize, Error> {
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
#[allow(unused_assignments)] // Yes, res is reassigned in the platform-specific code.
let mut res = default_result();
Expand All @@ -59,21 +59,27 @@ pub fn get_interface_mtu(remote: &SocketAddr) -> Result<usize, Error> {

#[cfg(any(target_os = "macos", target_os = "linux"))]
{
res = get_interface_mtu_linux_macos(&socket);
res = interface_mtu_linux_macos(&socket);
}

#[cfg(target_os = "windows")]
{
res = get_interface_mtu_windows(&socket);
res = interface_mtu_windows(&socket);
}
}

trace!("MTU towards {:?} is {:?}", remote, res);
trace!("MTU towards {remote:?} is {res:?}");
res
}

#[doc(hidden)]
#[deprecated(since = "0.1.2", note = "Use `interface_mtu()` instead")]
pub fn get_interface_mtu(remote: &SocketAddr) -> Result<usize, Error> {
interface_mtu(remote)
}

#[cfg(any(target_os = "macos", target_os = "linux"))]
fn get_interface_mtu_linux_macos(socket: &UdpSocket) -> Result<usize, Error> {
fn interface_mtu_linux_macos(socket: &UdpSocket) -> Result<usize, Error> {
use std::ffi::{c_int, CStr};
#[cfg(target_os = "linux")]
use std::{ffi::c_char, mem, os::fd::AsRawFd};
Expand Down Expand Up @@ -175,7 +181,7 @@ fn get_interface_mtu_linux_macos(socket: &UdpSocket) -> Result<usize, Error> {
}

#[cfg(target_os = "windows")]
fn get_interface_mtu_windows(socket: &UdpSocket) -> Result<usize, Error> {
fn interface_mtu_windows(socket: &UdpSocket) -> Result<usize, Error> {
use std::{ffi::c_void, slice};

use windows::Win32::{
Expand Down Expand Up @@ -260,18 +266,18 @@ mod test {
.unwrap()
.find(|a| a.is_ipv4() == ipv4);
if let Some(addr) = addr {
match super::get_interface_mtu(&addr) {
match super::interface_mtu(&addr) {
Ok(mtu) => assert_eq!(mtu, expected),
Err(e) => {
// Some GitHub runners don't have IPv6. Just warn if we can't get the MTU.
assert!(addr.is_ipv6());
warn!("Error getting MTU for {}: {}", sockaddr, e);
warn!("Error getting MTU for {sockaddr}: {e}");
}
}
} else {
// Some GitHub runners don't have IPv6. Just warn if we can't get an IPv6 address.
assert!(!ipv4);
warn!("No IPv6 address found for {}", sockaddr);
warn!("No IPv6 address found for {sockaddr}");
}
}

Expand Down

0 comments on commit 8d9a619

Please sign in to comment.