Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkInterface.GetAllNetworkInterfaces() doesn't list disabled adapters on Windows #89990

Closed
skyoxZ opened this issue Aug 4, 2023 · 8 comments · Fixed by #100824
Closed
Assignees
Labels
area-System.Net help wanted [up-for-grabs] Good issue for external contributors in-pr There is an active PR which will close this issue when it is merged os-windows
Milestone

Comments

@skyoxZ
Copy link
Contributor

skyoxZ commented Aug 4, 2023

Description

On Windows, a disabled network adapter is not listed by NetworkInterface.GetAllNetworkInterfaces().

Reproduction Steps

  1. foreach (var ni in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
    {
        Console.WriteLine(ni.Name + ": " + ni.OperationalStatus);
    }
  2. Disable a network adapter.
  3. Run the code again.

Expected behavior

The disabled adapter is listed with OperationalStatus.Down.

Actual behavior

The disabled adapter is not listed.

Regression?

No response

Known Workarounds

No response

Configuration

Windows 10
.NET 6 / .NET 7

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 4, 2023
@ghost
Copy link

ghost commented Aug 4, 2023

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

On Windows, a disabled network adapter is not listed by NetworkInterface.GetAllNetworkInterfaces().

Reproduction Steps

  1. foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
    {
        Console.WriteLine(ni.Name);
    }
  2. Disable a network adapter.
  3. Run the code again.

Expected behavior

The disabled adapter is listed with OperationalStatus.Down.

Actual behavior

The disabled adapter is not listed.

Regression?

No response

Known Workarounds

No response

Configuration

Windows 11
.NET 6 / .NET 7

Other information

No response

Author: skyoxZ
Assignees: -
Labels:

area-System.Net

Milestone: -

@karelz
Copy link
Member

karelz commented Aug 4, 2023

.NET returns what GetAdapterAddresses OS function gives us back:

result = Interop.IpHlpApi.GetAdaptersAddresses(
family, (uint)flags, IntPtr.Zero, buffer, &bufferSize);
// If succeeded, we're going to add each new interface.
if (result == Interop.IpHlpApi.ERROR_SUCCESS)
{
// Linked list of interfaces.
Interop.IpHlpApi.IpAdapterAddresses* adapterAddresses = (Interop.IpHlpApi.IpAdapterAddresses*)buffer;
while (adapterAddresses != null)
{
// Traverse the list, marshal in the native structures, and create new NetworkInterfaces.
interfaceList.Add(new SystemNetworkInterface(in *adapterAddresses));
adapterAddresses = adapterAddresses->next;
}
}

I assume that the function does not list disabled interfaces and that's why you don't see them.
I am not sure if there is other way for us to query network interfaces from the OS. @wfurt any thoughts?

Not critical for 8.0, moving to Future.

@karelz karelz added this to the Future milestone Aug 4, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 4, 2023
@wfurt
Copy link
Member

wfurt commented Aug 4, 2023

We should verify what GetAdaptersAddresses returns. If anything, we should make it clear in documentation.

@MojtabaTajik
Copy link
Contributor

@karelz @wfurt

It depends on the "flags" parameter we asked for "GetAdaptersAddresses" The current .Net implementation is:

Interop.IpHlpApi.GetAdaptersAddressesFlags flags =
Interop.IpHlpApi.GetAdaptersAddressesFlags.IncludeGateways
| Interop.IpHlpApi.GetAdaptersAddressesFlags.IncludeWins;

Based on Windows API docs, we need to pass "GAA_FLAG_INCLUDE_ALL_INTERFACES" as flag, if we want information of all adapters:

image

Then the received "IP_ADAPTER_ADDRESSES_LH" structure contains "OperStatus", which indicates the enabled state of the adapter.

@wfurt wfurt added the help wanted [up-for-grabs] Good issue for external contributors label Apr 8, 2024
@MojtabaTajik
Copy link
Contributor

MojtabaTajik commented Apr 9, 2024

@wfurt I want to pick this ticket up.

@danmoseley
Copy link
Member

What does it do on Linux/Mac?

@wfurt
Copy link
Member

wfurt commented Apr 9, 2024

I don't think the concept is the same. You can do ifconfig down but it is not quite the same and AFAIK we still list it @danmoseley

@wfurt wfurt modified the milestones: Future, 9.0.0 Apr 9, 2024
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Apr 9, 2024
@MojtabaTajik
Copy link
Contributor

MojtabaTajik commented Apr 9, 2024

@wfurt I fixed the issue and updated one of the tests because of that change.

Based on the documentation of GetAdaptersAddresses, this change will retrieve even those addresses associated with adapters not bound to an address family specified in the Family parameter.

If the GAA_FLAG_INCLUDE_ALL_INTERFACES is set, then all NDIS adapters will be retrieved even those addresses associated with adapters not bound to an address family specified in the Family parameter. When this flag is not set, then only the addresses that are bound to an adapter enabled for the address family specified in the Family parameter are returned.

Maybe to make it more fancy, we can add an optional parameter with the default value for backward compatibility like this:

internal static unsafe NetworkInterface[] GetNetworkInterfaces(Interop.IpHlpApi.GetAdaptersAddressesFlags flags =Interop.IpHlpApi.GetAdaptersAddressesFlags.IncludeAllInterfaces)
{
		
}

This modification allows developers to choose interface types according to their preferences while ensuring backward compatibility by providing a default value for the flags parameter.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net help wanted [up-for-grabs] Good issue for external contributors in-pr There is an active PR which will close this issue when it is merged os-windows
Projects
None yet
5 participants