Skip to content

Commit

Permalink
feat: Allow passing in local and remote socket addresses (#16)
Browse files Browse the repository at this point in the history
* feat: Allow passing in local and remote socket addresses

@martinthomson, is this what you had in mind?

Fixes #15

* Fix doctest

* Fixes

* Simplify

* Update src/lib.rs

Co-authored-by: Martin Thomson <mt@lowentropy.net>

* Update src/lib.rs

Co-authored-by: Martin Thomson <mt@lowentropy.net>

* Drop `cfg-if``

* Stricter ordering

* Compat

* Docs

* Suggestion from @martinthomson

* Manual merge from `main`

* Cleanups

* str

* Use Cloudflare's IPs, and find unused ports

* No v6 on GitHub

* loopback_0

* No copy

* doctest

* Fix Windows

* Suggestions from @martinthomson

* Rip out deprecated API support

---------

Co-authored-by: Martin Thomson <mt@lowentropy.net>
  • Loading branch information
larseggert and martinthomson authored Sep 5, 2024
1 parent 9fa75a8 commit 8af1e02
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 96 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ rust-version = "1.76.0"
[dependencies]
# Don't increase beyond what Firefox is currently using: https://searchfox.org/mozilla-central/source/Cargo.lock
libc = { version = "0.2", default-features = false }
log = { version = "0.4", default-features = false }

[dev-dependencies]
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"] }

[target."cfg(windows)".dependencies]
# Don't increase beyond what Firefox is currently using: https://searchfox.org/mozilla-central/source/Cargo.lock
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# MTU

A crate to return the maximum transmission unit (MTU) of the local network interface towards a given destination `SocketAddr`.
A crate to return the name and maximum transmission unit (MTU) of the local network interface towards a given destination `SocketAddr`, optionally from a given local `SocketAddr`.

## Usage

This crate exports a single function
This crate exports a single function `interface_and_mtu` that, given a pair of local and remote `SocketAddr`s, returns the name and [maximum transmission unit (MTU)](https://en.wikipedia.org/wiki/Maximum_transmission_unit) of the local network interface used by a socket bound to the local address and connected towards the remote destination.

If the local address is `None`, the function will let the operating system choose the local address based on the given remote address. If the remote address is `None`, the function will return the name and MTU of the local network interface with the given local address.

## Example

```rust
pub fn interface_and_mtu(remote: &SocketAddr) -> Result<(String, usize), Error>
let saddr = "127.0.0.1:443".parse().unwrap();
let (name, mtu) = mtu::interface_and_mtu(&(None, saddr)).unwrap();
println!("MTU for {saddr:?} is {mtu} on {name}");
```

that returns the interface name and MTU of the local network interface used for transmission towards the `remote` destination, or an `Error` when the MTU could not be determined. It supports both IPv4 and IPv6.

## Supported Platforms

* Linux
Expand Down
Loading

0 comments on commit 8af1e02

Please sign in to comment.