Skip to content

Simplify MacAddressGetter to use NetworkInterface API directly#55420

Open
marcpopMSFT wants to merge 2 commits into
mainfrom
marcpopmsft-simplify-mac-address-getter
Open

Simplify MacAddressGetter to use NetworkInterface API directly#55420
marcpopMSFT wants to merge 2 commits into
mainfrom
marcpopmsft-simplify-mac-address-getter

Conversation

@marcpopMSFT

Copy link
Copy Markdown
Member

Summary

Remove the shell-out-first strategy that spawned platform-specific processes (getmac.exe, ifconfig, ip link) to obtain MAC addresses. Instead, use the .NET NetworkInterface API directly, which was already present as a fallback.

Motivation

  • Reliability: The shell-out approach depends on external binaries existing at expected paths, which is fragile on minimal containers, distroless images, and exotic distros. The NetworkInterface API is built into the runtime and works everywhere .NET runs.
  • Simplicity: This eliminates ~120 lines of process-spawning, path-probing, and regex-parsing code.
  • Performance: No child process creation — just an in-process API call.
  • Post-.NET 8 context: The primary device identifier is now devdeviceid (via DeviceIdGetter), not the MAC-based MachineId. The MAC address is only used for the legacy MachineId/MachineIdOld telemetry properties.

Safety

  • GetMacAddress() is wrapped in a catch-all try/catch returning null on any failure.
  • Callers already handle null gracefully: TelemetryCommonProperties.GetMachineId() falls back to Guid.NewGuid() on null or NetworkInformationException.
  • Telemetry can never break the CLI.

Changes

  • MacAddressGetter.cs: Simplified to use NetworkInterface.GetAllNetworkInterfaces() directly. Removed GetShellOutMacAddressOutput(), GetIpCommandOutput(), ParseMACAddress(), and all related constants/imports. Added filtering for all-zero and empty MAC addresses for parity with the old regex-based filtering.

No signature changes — both callers (TelemetryCommonProperties.cs, WindowsUtils.cs) are unaffected.

Remove the shell-out-first strategy that spawned platform-specific processes
(getmac.exe, ifconfig, ip link) to obtain MAC addresses. Instead, use the
.NET NetworkInterface API directly, which was already present as a fallback.

This eliminates ~120 lines of process-spawning, path-probing, and regex-
parsing code. The NetworkInterface API is built into the runtime and works
reliably across all platforms including minimal containers and distroless
images.

Since .NET 8, the primary device identifier is devdeviceid (via
DeviceIdGetter), not the MAC-based MachineId. GetMacAddress() is now
wrapped in a catch-all try/catch returning null on any failure, and
callers already handle null by falling back to a GUID — so telemetry
can never break the CLI.

Also filters out all-zero and empty MAC addresses for parity with the
old regex-based filtering.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 22, 2026 21:48
@marcpopMSFT
marcpopMSFT requested a review from a team as a code owner July 22, 2026 21:48
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR simplifies MacAddressGetter in the dotnet CLI telemetry area by removing the platform-specific “shell out to external tools” approach and relying solely on the in-box System.Net.NetworkInformation.NetworkInterface API to retrieve MAC addresses.

Changes:

  • Removed process-spawning / regex parsing logic for MAC discovery and kept a single NetworkInterface-based implementation.
  • Added filtering to skip empty and all-zero physical addresses.
  • Switched to a “best effort” API that returns null on any failure to keep telemetry non-blocking.

Comment thread src/Cli/dotnet/Telemetry/MacAddressGetter.cs
Comment thread src/Cli/dotnet/Telemetry/MacAddressGetter.cs Outdated

@baronfel baronfel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it.

Comment thread src/Cli/dotnet/Telemetry/MacAddressGetter.cs Outdated
Comment thread src/Cli/dotnet/Telemetry/MacAddressGetter.cs Outdated
…NICs

- Accept only 6-byte MAC addresses for parity with the old regex-based
  filtering that matched exactly 6 octets (excludes EUI-64 addresses).
- Exclude Loopback and Tunnel network interface types since those are
  virtual adapters without real hardware MACs.
- Remove the arbitrary 10-address cap since we only use the first MAC
  and the NetworkInterface API is fast (no process spawning).
- Simplify the method using LINQ.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.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.

4 participants