December 8th, 2008
Querying System.Net.NetworkInformation in C# - 0
.NET 2.0 added the NetworkInformation namespace which allow you to discover and query all the network interfaces in the local machine. The information this provides is very similar to what you can find by opening the “Network Connections” in Windows. In this post I have a look into the kinds information it provides — and what to look out for if your code has to run on Mono as well.
In a previous post we looked at how we could use System.Net.NetworkInformation to determine the machines local IP addresses, but there is much more stored in here.
The code below steps through each network interface on my machine:
Interface information for mymachine.mydomain.com
NetBIOS node type: Unknown
——————————————————
Name: Local Area Connection
Description: ASUSTeK/Broadcom 440x 10/100 Integrated Controller – Packet Scheduler Miniport
Network Interface Type: Ethernet
Physical Address: 000EA99F2D04
Adapter ID: {34F1A99A-3449-4290-8FFF-C1C62211696A}
Receive only: False
Status: Up
Speed: 100000000
Properties:
|DNS Addresses: 255.255.255.255
|DNS Suffic:
|DNS Enabled: False
|Dynamic DNS Enabled: True
|DHPC Server Addresses: 255.255.255.255
|UniCast Addresses: 10.0.0.6 192.168.1.6
|AnyCast Addresses:
|Supports multi-cast: True
|Multicast Addresses: 224.0.0.1 239.255.255.250
|Gateway Addresses: 10.0.0.253
+IPV4 Properties:
|Interface Index: 2
|Automatic Private Addressing Active: False
|Automatic Private Addressing Enabled: True
|DHCP Enabled: False
|Forwarding Enabled: False
|MTU Size: 1500
\Uses Wins: False
+IPV6 is not implemented
For brevity I have omitted the local loopback adaptor.
The Mono implementation of System.Net.NetworkInformation is spotty at best. This has much to do with the fact that on Linux much information isn’t easily stored in one central location. As can be seen from the comments in the Mono 2.01 source code:
[MonoTODO ("Always returns an empty collection.")]
public override IPAddressCollection DhcpServerAddresses {
get {
// There are lots of different DHCP clients
// that all store their configuration differently.
// I'm not sure what to do here.
return new IPAddressCollection ();
}
}
[MonoTODO ("Always returns an empty collection.")]
public override IPAddressCollection DnsAddresses {
get {
// XXX: Parse /etc/resolv.conf, I suppose.
return new IPAddressCollection ();
}
}
The following code runs under both .NET & Mono and gives you a quick overview of the kinds of adapter information provided by System.Net.NetworkInformation. It is also possible to query each adapter for usage statistics — if you would like to know how many bytes it has send, but we won’t go there.
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Text;
namespace Examples.System.Net.NetworkInformation.IfConfig
{
public class IfConfig
{
public static bool DetectMono()
{
int p = (int)Environment.OSVersion.Platform;
return (p == 4) || (p == 128);
}
public static void Main(string[] args)
{
// Mono's implementation of NetworkInformation is spotty, many values just return hard coded defaults
// or throw an NotImplementedExceptions
bool IsMono = DetectMono();
// Global IP settings
IPGlobalProperties global_properties = IPGlobalProperties.GetIPGlobalProperties();
Console.WriteLine("Interface information for {0}.{1}", global_properties.HostName, global_properties.DomainName);
Console.WriteLine("NetBIOS node type: {0}", global_properties.NodeType);
//Gateway IP
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
Console.WriteLine("------------------------------------------------------");
Console.WriteLine("Name: {0}", adapter.Name);
Console.WriteLine("Description: {0}", adapter.Description);
Console.WriteLine("Network Interface Type: {0}", adapter.NetworkInterfaceType);
Console.WriteLine("Physical Address: {0}", adapter.GetPhysicalAddress());
Console.WriteLine("Adapter ID: {0}", adapter.Id);
Console.WriteLine("Receive only: {0}", adapter.IsReceiveOnly);
Console.WriteLine("Status: {0}", adapter.OperationalStatus);
if (!IsMono) Console.WriteLine("Speed: {0}", adapter.Speed); // 100 Mb is really a lot of zeros
IPInterfaceProperties properties = adapter.GetIPProperties();
Console.WriteLine("Properties: ");
Console.Write("|DNS Addresses: ");
foreach (IPAddress DnsServer in properties.DhcpServerAddresses)
Console.Write("{0} ", DnsServer);
Console.WriteLine();
Console.WriteLine("|DNS Suffic: {0}", properties.DnsSuffix);
Console.WriteLine("|DNS Enabled: {0}", properties.IsDnsEnabled);
Console.WriteLine("|Dynamic DNS Enabled: {0}", properties.IsDynamicDnsEnabled);
Console.Write("|DHPC Server Addresses: ");
foreach (IPAddress DhcpServer in properties.DhcpServerAddresses)
Console.Write("{0} ", DhcpServer);
Console.WriteLine();
Console.Write("|UniCast Addresses: ");
foreach (IPAddressInformation uniCast in properties.UnicastAddresses)
Console.Write("{0} ", uniCast.Address);
Console.WriteLine();
Console.Write("|AnyCast Addresses: ");
foreach (IPAddressInformation anyCast in properties.AnycastAddresses)
Console.Write("{0} ", anyCast.Address);
Console.WriteLine();
Console.WriteLine("|Supports multi-cast: {0}", adapter.SupportsMulticast);
Console.Write("|Multicast Addresses: ");
foreach (IPAddressInformation multiCast in properties.MulticastAddresses)
Console.Write("{0} ", multiCast.Address);
Console.WriteLine();
Console.Write("|Gateway Addresses: ");
foreach (GatewayIPAddressInformation GateWay in properties.GatewayAddresses)
Console.Write("{0} ", GateWay.Address);
Console.WriteLine();
if (adapter.Supports(NetworkInterfaceComponent.IPv4) == true)
{
IPv4InterfaceProperties IPV4Props = properties.GetIPv4Properties();
// Loopback device does not have any property information associated
Console.WriteLine("+IPV4 Properties: ");
if (IPV4Props != null)
{
Console.WriteLine(" |Interface Index: {0}", IPV4Props.Index);
Console.WriteLine(" |Automatic Private Addressing Active: {0}", IPV4Props.IsAutomaticPrivateAddressingActive);
Console.WriteLine(" |Automatic Private Addressing Enabled: {0}", IPV4Props.IsAutomaticPrivateAddressingEnabled);
Console.WriteLine(" |DHCP Enabled: {0}", IPV4Props.IsDhcpEnabled);
Console.WriteLine(" |Forwarding Enabled: {0}", IPV4Props.IsForwardingEnabled);
Console.WriteLine(" |MTU Size: {0}", IPV4Props.Mtu);
Console.WriteLine(" \\Uses Wins: {0}", IPV4Props.UsesWins);
}
else
{
Console.WriteLine(" |Device has no IPv4 properties");
}
}
else
{
Console.WriteLine("+IPV4 is not implemented");
}
if (!IsMono && adapter.Supports(NetworkInterfaceComponent.IPv6) == true)
{
IPv6InterfaceProperties IPV6Props = properties.GetIPv6Properties();
Console.WriteLine("+IPV6 Properties: ");
if (IPV6Props != null)
{
Console.WriteLine("+IPV6 Properties: ");
Console.WriteLine(" |Interface Index: {0}", IPV6Props.Index);
Console.WriteLine(" \\MTU Size: {0}", IPV6Props.Mtu);
}
else
{
Console.WriteLine(" |Device has no IPv6 properties");
}
}
else
{
Console.WriteLine("+IPV6 is not implemented");
}
}
}
}
}









Except where otherwise noted, content on this site is