Runtime: NetworkInterface Extensions

Created on 18 Jul 2020  路  5Comments  路  Source: dotnet/runtime

Background and Motivation

People need to be aware of how to adapt to network change such as cell to wifi or network failover.

I might be using a usb nic etc.

Proposed API

+ public static class NetworkInterfaceExtensions {
+public static bool TryGetNetworkInterface(System.Net.IPAddress localAddress, out System.Net.NetworkInformation.NetworkInterface nic)
+public static System.Collections.Generic.IEnumerable<System.Net.NetworkInformation.NetworkInterface> GetNetworkInterface(System.Net.NetworkInformation.OperationalStatus status, params System.Net.NetworkInformation.NetworkInterfaceType[] interfaceTypes)
+  public static System.Collections.Generic.IEnumerable<System.Net.NetworkInformation.NetworkInterface> GetNetworkInterface(params System.Net.NetworkInformation.NetworkInterfaceType[] interfaceTypes)
+  public static bool TryGetNetworkInterface(System.Net.IPEndPoint localEndPoint, out System.Net.NetworkInformation.NetworkInterface nic)
+  public static bool  TryGetNetworkInterface(System.Net.Sockets.Socket socket, System.Net.NetworkInformation.NetworkInterface)
+ public static double GetSpeedInMBytesPerSecond(this System.Net.NetworkInformation.NetworkInterface networkInterface);
+ public static double CaulculateInterframeGapNanoseconds(long speedAsBitsPerSecond);
+  public static double GetInterframeGapNanoseconds(this System.Net.NetworkInformation.NetworkInterface networkInterface);
+ public static double GetInterframeGapMicroseconds(this System.Net.NetworkInformation.NetworkInterface networkInterface);
+ public static bool TryGetFirstMulticastIPAddress(System.Net.NetworkInformation.NetworkInterface networkInterface, System.Net.Sockets.AddressFamily addressFamily,  out System.Net.IPAddress ip)
+ public static bool TryGetFirstUnicastIPAddress(System.Net.NetworkInformation.NetworkInterface networkInterface, System.Net.Sockets.AddressFamily addressFamily, out System.Net.IPAddress ip)

//Counter support, Because bandwidth monitoring is applicable as well as SNMP v3 for security etc.
+ public BigInteger BytesSent (this System.Net.NetworkInformation.NetworkInterface networkInterface);
+ public BigInteger BytesReceived (this System.Net.NetworkInformation.NetworkInterface networkInterface);
+ public bool TrySetBytesReceived (this System.Net.NetworkInformation.NetworkInterface networkInterface, BigInteger value);
+ public bool TrySetBytesSent (this System.Net.NetworkInformation.NetworkInterface networkInterface, BigInteger value);
+ public bool TryResetBytesSent (this System.Net.NetworkInformation.NetworkInterface networkInterface);
+ public bool TryResetBytesReceived (this System.Net.NetworkInformation.NetworkInterface networkInterface);

+ public BigInteger BytesSent (this System.Net.NetworkInformation.NetworkInterface networkInterface, ProtocolType protocolType);
+ public BigInteger BytesReceived (this System.Net.NetworkInformation.NetworkInterface networkInterface, ProtocolType protocolType);

+ public bool TrySetBytesReceived (this System.Net.NetworkInformation.NetworkInterface networkInterface, , ProtocolType protocolType, BigInteger value);
+ public bool TrySetBytesSent (this System.Net.NetworkInformation.NetworkInterface networkInterface , ProtocolType protocolType, BigInteger value);
+ public bool TryResetBytesSent (this System.Net.NetworkInformation.NetworkInterface networkInterface , ProtocolType protocolType, BigInteger value);
+ public bool TryResetBytesReceived (this System.Net.NetworkInformation.NetworkInterface networkInterface , ProtocolType protocolType, BigInteger value);


+ }

Alternative design

On NetworkInterface itself so people don't have to use it or can import the statics they want.

Usage Examples

``` C#
if(NetworkInterfaceExtensions.TryGetNetworkInterface(someAddr, out var nic)){
NetworkInterfaceExtensions.GetSpeedInMBytesPerSecond(nic);
///
}

```

cc @scalablecory

api-suggestion area-System.Net

Most helpful comment

yes but I figured people might not want to see all that? I dunno I would...

That's a reasonable concern -- just suggesting your proposal include it as an alternative. I see you added an "alternatives" section - you might also note there why you suggest one rather than the other.

All 5 comments

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@juliusfriedman thank you for the proposal.

Can you please share more about the motivation in your proposal? What kinds of applications might particularly use this -- apps, servers, devices? What approaches do they use today, without this API? Are there existing libraries, eg., on Nuget?

Do other application platforms have similar API, and if so what shape does it have?

long should be sufficient, there is no need to use BigInteger.

For the Try* API's, please see the relevant Framework Design Guidelines commentary. The Try* pattern is essentially only for very hot paths, particularly parsing. In all other cases, we prefer the try/throw pattern for reasons explained there.

Did you consider exposing these on NetworkInterface itself instead?

Could you please fix the syntax above, eg., public BigInteger BytesSent System.Net.NetworkInformation.NetworkInterface networkInterface, ProtocolType protocolType); should probably be public BigInteger BytesSent(this System.Net.NetworkInformation.NetworkInterface networkInterface, ProtocolType protocolType);

Did you consider exposing these on NetworkInterface itself instead?

Could you please fix the syntax above, eg., public BigInteger BytesSent System.Net.NetworkInformation.NetworkInterface networkInterface, ProtocolType protocolType); should probably be public BigInteger BytesSent(this System.Net.NetworkInformation.NetworkInterface networkInterface, ProtocolType protocolType);

yes but I figured people might not want to see all that? I dunno I would...

yes but I figured people might not want to see all that? I dunno I would...

That's a reasonable concern -- just suggesting your proposal include it as an alternative. I see you added an "alternatives" section - you might also note there why you suggest one rather than the other.

Was this page helpful?
0 / 5 - 0 ratings