Adding reference source for System.Net
[mono.git] / mcs / class / referencesource / System / net / System / Net / NetworkInformation / SystemIPInterfaceProperties.cs
1
2     /// <summary><para>
3     ///    Provides support for ip configuation information and statistics.
4     ///</para></summary>
5     ///
6 namespace System.Net.NetworkInformation {
7
8     using System.Net.Sockets;
9     using System;
10
11     /// <summary>
12     /// Provides information specific to a network
13     /// interface.
14     /// </summary>
15     /// <remarks>
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>
19     /// </remarks>
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;
35
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);
42
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));
54
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());
60
61             if ((adapterFlags & AdapterFlags.IPv4Enabled) != 0) {
62                 ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses);
63             }
64
65             if ((adapterFlags & AdapterFlags.IPv6Enabled) != 0) {
66                 ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index, 
67                     ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices);
68             }
69         }
70
71         public override bool IsDnsEnabled{get { return dnsEnabled;}}
72
73         public override bool IsDynamicDnsEnabled{get {return dynamicDnsEnabled;}}
74
75         public override IPv4InterfaceProperties GetIPv4Properties(){
76             if ((adapterFlags & AdapterFlags.IPv4Enabled) == 0) {
77                 throw new NetworkInformationException(SocketError.ProtocolNotSupported);
78             }
79             return ipv4Properties;
80         }
81
82         public override IPv6InterfaceProperties GetIPv6Properties(){
83             if ((adapterFlags & AdapterFlags.IPv6Enabled) == 0) {
84                 throw new NetworkInformationException(SocketError.ProtocolNotSupported);
85             }
86             return ipv6Properties;
87         }
88
89         public override string DnsSuffix {
90             get {
91                 return dnsSuffix;
92             }
93         }
94         
95         //returns the addresses specified by the address type.
96         public override IPAddressInformationCollection AnycastAddresses{
97             get{
98                 return anycastAddresses;
99             }
100         }
101
102         //returns the addresses specified by the address type.
103         public override UnicastIPAddressInformationCollection UnicastAddresses{
104             get{
105                 return unicastAddresses;
106             }
107         }
108
109         //returns the addresses specified by the address type.
110         public override MulticastIPAddressInformationCollection MulticastAddresses{
111             get{
112                 return multicastAddresses;
113             }
114         }
115
116         //returns the addresses specified by the address type.
117         public override IPAddressCollection DnsAddresses{
118             get{
119                 return dnsAddresses;
120             }
121         }
122
123         /// <summary>IP Address of the default gateway.</summary>
124         public override GatewayIPAddressInformationCollection GatewayAddresses{
125             get{
126                 return gatewayAddresses;
127             }
128         }
129                         
130         public override IPAddressCollection DhcpServerAddresses{
131             get{
132                 return dhcpServers;
133             }
134         }
135
136         public override IPAddressCollection WinsServersAddresses{
137             get{
138                 return winsServersAddresses;
139             }
140         }
141     }
142 }