feat: expose interface hardware (MAC) address#54
Open
franchb wants to merge 1 commit into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
mac_addr: Option<MacAddr>field and anInterface::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
/sysor other platform-specific locations:getifaddrsentries (AF_PACKET/sockaddr_ll)AF_LINK/sockaddr_dlGetAdaptersAddressesPhysicalAddressMacAddrwraps[u8; 6]with:Displayformatting asaa:bb:cc:dd:ee:ffoctets()andis_zero()helpersFrom<[u8; 6]>Only 6-byte (EUI-48) addresses are reported; other lengths (e.g. InfiniBand) yield
None.sockaddr_dlcorrectness (BSD/Apple)sockaddr_dlis a variable-length structure: the interface name (sdl_nlenbytes) is followed by the link-layer address starting atsdl_data. Since libc modelssdl_dataas a fixed-size minimum work area (only[c_char; 12]on Apple), the MAC is read via pointer arithmetic from the start ofsdl_dataand bounded by the structure's ownsdl_len(on platforms that have it; illumos/Solaris lacksdl_lenand are excluded). This avoids dropping the address for interfaces whose name is 6+ characters (e.g.bridge0).addr_of!/offset_fromare used to respect the crate's 1.71 MSRV.Testing
test_get_if_addrsnow cross-checks the parsed MAC againstip addr/ifconfigoutput, asserting the crate reports a MAC whenever the system tool does.test_mac_addr_displaycoversMacAddrformatting and helpers.sockaddr_dlpath) and Windows, plus cross-builds for Android, illumos, FreeBSD, NetBSD and the Apple tier-3 targets.Notes
0.16.0; happy to drop that commit/line if you'd prefer to manage the version bump yourself.🤖 Generated with Claude Code