Skip to content

feat: expose interface hardware (MAC) address#54

Open
franchb wants to merge 1 commit into
messense:masterfrom
franchb:backport/mac-addr
Open

feat: expose interface hardware (MAC) address#54
franchb wants to merge 1 commit into
messense:masterfrom
franchb:backport/mac-addr

Conversation

@franchb

@franchb franchb commented May 30, 2026

Copy link
Copy Markdown

Summary

Adds a mac_addr: Option<MacAddr> field and an Interface::mac_addr() accessor that report an interface's 6-byte hardware (MAC) address.

The address is read from the same OS interface-enumeration call already used for the IP information — no reaching into /sys or other platform-specific locations:

  • Linux/Android — link-layer getifaddrs entries (AF_PACKET / sockaddr_ll)
  • BSD / Apple / illumosAF_LINK / sockaddr_dl
  • WindowsGetAdaptersAddresses PhysicalAddress

MacAddr wraps [u8; 6] with:

  • Display formatting as aa:bb:cc:dd:ee:ff
  • octets() and is_zero() helpers
  • From<[u8; 6]>

Only 6-byte (EUI-48) addresses are reported; other lengths (e.g. InfiniBand) yield None.

sockaddr_dl correctness (BSD/Apple)

sockaddr_dl is a variable-length structure: the interface name (sdl_nlen bytes) is followed by the link-layer address starting at sdl_data. Since libc models sdl_data as a fixed-size minimum work area (only [c_char; 12] on Apple), the MAC is read via pointer arithmetic from the start of sdl_data and bounded by the structure's own sdl_len (on platforms that have it; illumos/Solaris lack sdl_len and are excluded). This avoids dropping the address for interfaces whose name is 6+ characters (e.g. bridge0). addr_of!/offset_from are used to respect the crate's 1.71 MSRV.

Testing

  • test_get_if_addrs now cross-checks the parsed MAC against ip addr / ifconfig output, asserting the crate reports a MAC whenever the system tool does.
  • New test_mac_addr_display covers MacAddr formatting and helpers.
  • Verified green in CI on Linux, macOS (real runtime coverage of the sockaddr_dl path) and Windows, plus cross-builds for Android, illumos, FreeBSD, NetBSD and the Apple tier-3 targets.

Notes

  • This bumps the version to 0.16.0; happy to drop that commit/line if you'd prefer to manage the version bump yourself.

🤖 Generated with Claude Code

Add a `mac_addr: Option<MacAddr>` field (and `Interface::mac_addr()` accessor) reporting the interface's 6-byte hardware address.

The address is read from the same OS interface-enumeration call already used for IP information: link-layer `getifaddrs` entries (`AF_PACKET`/`sockaddr_ll` on Linux/Android, `AF_LINK`/`sockaddr_dl` on the BSDs/Apple/illumos) and `GetAdaptersAddresses` `PhysicalAddress` on Windows. This keeps MAC retrieval cross-platform without reaching into `/sys` or other platform-specific locations.

`sockaddr_dl` is variable-length, so the address is read via pointer arithmetic from the start of `sdl_data` and bounded by the structure's own `sdl_len` (where available); this avoids dropping the MAC for interfaces whose name is 6+ characters (e.g. "bridge0"), which would overrun libc's fixed-size `sdl_data` array.

`MacAddr` wraps `[u8; 6]` with a `Display` impl that formats as `aa:bb:cc:dd:ee:ff`, plus `octets()` and `is_zero()` helpers. Only 6-byte addresses are reported; other lengths yield `None`.

Tests cross-check the parsed MAC against `ip addr` / `ifconfig` output and cover `MacAddr` formatting.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants