3 /// Provides support for ip configuation information and statistics.
6 namespace System.Net.NetworkInformation {
8 using System.Net.Sockets;
12 /// Provides information specific to a network
16 /// <para>Provides information specific to a network interface. A network interface can have more
17 /// than one IPAddress associated with it. We call the native GetAdaptersAddresses api to
18 /// prepopulate all of the interface instances and most of their associated information.</para>
20 internal class SystemIPInterfaceProperties:IPInterfaceProperties {
21 //these are valid for all interfaces
22 private bool dnsEnabled = false;
23 private bool dynamicDnsEnabled = false;
24 private IPAddressCollection dnsAddresses = null;
25 private UnicastIPAddressInformationCollection unicastAddresses = null;
26 private MulticastIPAddressInformationCollection multicastAddresses = null;
27 private IPAddressInformationCollection anycastAddresses = null;
28 private AdapterFlags adapterFlags;
29 private string dnsSuffix;
30 private SystemIPv4InterfaceProperties ipv4Properties;
31 private SystemIPv6InterfaceProperties ipv6Properties;
32 private IPAddressCollection winsServersAddresses;
33 private GatewayIPAddressInformationCollection gatewayAddresses;
34 private IPAddressCollection dhcpServers;
36 // This constructor is for Vista and newer
37 internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) {
38 adapterFlags = ipAdapterAddresses.flags;
39 dnsSuffix = ipAdapterAddresses.dnsSuffix;
40 dnsEnabled = fixedInfo.EnableDns;
41 dynamicDnsEnabled = ((ipAdapterAddresses.flags & AdapterFlags.DnsEnabled) > 0);
43 multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection(
44 IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress));
45 dnsAddresses = IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress);
46 anycastAddresses = IpAdapterAddress.MarshalIpAddressInformationCollection(
47 ipAdapterAddresses.firstAnycastAddress);
48 unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection(
49 ipAdapterAddresses.firstUnicastAddress);
50 winsServersAddresses = IpAdapterAddress.MarshalIpAddressCollection(
51 ipAdapterAddresses.firstWinsServerAddress);
52 gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection(
53 IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress));
55 dhcpServers = new IPAddressCollection();
56 if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero)
57 dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress());
58 if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero)
59 dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress());
61 if ((adapterFlags & AdapterFlags.IPv4Enabled) != 0) {
62 ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses);
65 if ((adapterFlags & AdapterFlags.IPv6Enabled) != 0) {
66 ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index,
67 ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices);
71 public override bool IsDnsEnabled{get { return dnsEnabled;}}
73 public override bool IsDynamicDnsEnabled{get {return dynamicDnsEnabled;}}
75 public override IPv4InterfaceProperties GetIPv4Properties(){
76 if ((adapterFlags & AdapterFlags.IPv4Enabled) == 0) {
77 throw new NetworkInformationException(SocketError.ProtocolNotSupported);
79 return ipv4Properties;
82 public override IPv6InterfaceProperties GetIPv6Properties(){
83 if ((adapterFlags & AdapterFlags.IPv6Enabled) == 0) {
84 throw new NetworkInformationException(SocketError.ProtocolNotSupported);
86 return ipv6Properties;
89 public override string DnsSuffix {
95 //returns the addresses specified by the address type.
96 public override IPAddressInformationCollection AnycastAddresses{
98 return anycastAddresses;
102 //returns the addresses specified by the address type.
103 public override UnicastIPAddressInformationCollection UnicastAddresses{
105 return unicastAddresses;
109 //returns the addresses specified by the address type.
110 public override MulticastIPAddressInformationCollection MulticastAddresses{
112 return multicastAddresses;
116 //returns the addresses specified by the address type.
117 public override IPAddressCollection DnsAddresses{
123 /// <summary>IP Address of the default gateway.</summary>
124 public override GatewayIPAddressInformationCollection GatewayAddresses{
126 return gatewayAddresses;
130 public override IPAddressCollection DhcpServerAddresses{
136 public override IPAddressCollection WinsServersAddresses{
138 return winsServersAddresses;