9c011a450c43c548a12695f9a30bd4604ff9651d
[mono.git] / mcs / class / System / System.Net.NetworkInformation / IPInterfaceProperties.cs
1 //
2 // System.Net.NetworkInformation.IPInterfaceProperties
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //      Atsushi Enomoto (atsushi@ximian.com)
7 //
8 // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 #if NET_2_0
30 using System.Collections.Generic;
31 using System.Net.Sockets;
32
33 namespace System.Net.NetworkInformation {
34         public abstract class IPInterfaceProperties {
35                 protected IPInterfaceProperties ()
36                 {
37                 }
38
39                 public abstract IPv4InterfaceProperties GetIPv4Properties ();
40                 public abstract IPv6InterfaceProperties GetIPv6Properties ();
41
42                 public abstract IPAddressInformationCollection AnycastAddresses { get; }
43                 public abstract IPAddressCollection DhcpServerAddresses { get; }
44                 public abstract IPAddressCollection DnsAddresses { get; }
45                 public abstract string DnsSuffix { get; }
46                 public abstract GatewayIPAddressInformationCollection GatewayAddresses { get; }
47                 public abstract bool IsDnsEnabled { get; }
48                 public abstract bool IsDynamicDnsEnabled { get; }
49                 public abstract MulticastIPAddressInformationCollection MulticastAddresses { get; }
50                 public abstract UnicastIPAddressInformationCollection UnicastAddresses { get; }
51                 public abstract IPAddressCollection WinsServersAddresses { get; }
52         }
53
54         class LinuxIPInterfaceProperties : IPInterfaceProperties
55         {
56                 IPv4InterfaceProperties ipv4iface_properties;
57                 LinuxNetworkInterface iface;
58                 List <IPAddress> addresses;
59                 
60                 public LinuxIPInterfaceProperties (LinuxNetworkInterface iface, List <IPAddress> addresses)
61                 {
62                         this.iface = iface;
63                         this.addresses = addresses;
64                 }
65
66                 public override IPv4InterfaceProperties GetIPv4Properties ()
67                 {
68                         if (ipv4iface_properties == null)
69                                 ipv4iface_properties = new LinuxIPv4InterfaceProperties (iface);
70                         
71                         return ipv4iface_properties;
72                 }
73
74                 public override IPv6InterfaceProperties GetIPv6Properties ()
75                 {
76                         throw new NotImplementedException ();
77                 }
78
79                 public override IPAddressInformationCollection AnycastAddresses {
80                         get {
81                                 List<IPAddress> anycastAddresses = new List<IPAddress> ();
82                                 /* XXX:
83                                 foreach (IPAddress address in addresses) {
84                                         if (is_anycast_address (address)) {
85                                                 anycastAddresses.Add (address);
86                                         }
87                                 }
88                                 */
89                                 return IPAddressInformationImplCollection.LinuxFromAnycast (anycastAddresses);
90                         }
91                 }
92
93                 [MonoTODO ("Always returns an empty collection.")]
94                 public override IPAddressCollection DhcpServerAddresses {
95                         get {
96                                 // There are lots of different DHCP clients
97                                 // that all store their configuration differently.
98                                 // I'm not sure what to do here.
99                                 return new IPAddressCollection ();
100                         }
101                 }
102
103                 [MonoTODO ("Always returns an empty collection.")]
104                 public override IPAddressCollection DnsAddresses {
105                         get {
106                                 // XXX: Parse /etc/resolv.conf, I suppose.
107                                 return new IPAddressCollection ();
108                         }
109                 }
110
111                 [MonoTODO ("Does not return anything.")]
112                 public override string DnsSuffix {
113                         get {
114                                 // XXX: Also parse resolv.conf?
115                                 return String.Empty;
116                         }
117                 }
118
119                 [MonoTODO ("Always returns an empty collection.")]
120                 public override GatewayIPAddressInformationCollection GatewayAddresses {
121                         get {
122                                 // XXX: Pull information from route table.
123                                 return LinuxGatewayIPAddressInformationCollection.Empty;
124                         }
125                 }
126
127                 [MonoTODO ("Always returns true")]
128                 public override bool IsDnsEnabled {
129                         get {
130                                 return true;
131                         }
132                 }
133
134                 [MonoTODO ("Always returns false")]
135                 public override bool IsDynamicDnsEnabled {
136                         get {
137                                 return false;
138                         }
139                 }
140
141                 public override MulticastIPAddressInformationCollection MulticastAddresses {
142                         get {
143                                 List<IPAddress> multicastAddresses = new List<IPAddress> ();
144                                 foreach (IPAddress address in addresses) {
145                                         byte[] addressBytes = address.GetAddressBytes ();
146                                         if (addressBytes[0] >= 224 && addressBytes[0] <= 239) {
147                                                 multicastAddresses.Add (address);
148                                         }
149                                 }
150                                 return MulticastIPAddressInformationImplCollection.LinuxFromList (multicastAddresses);
151                         }
152                 }
153
154                 public override UnicastIPAddressInformationCollection UnicastAddresses {
155                         get {
156                                 List<IPAddress> unicastAddresses = new List<IPAddress> ();
157                                 foreach (IPAddress address in addresses) {
158                                         switch (address.AddressFamily) {
159                                                 case AddressFamily.InterNetwork:
160                                                         byte top = address.GetAddressBytes () [0];
161                                                         if (top >= 224 && top <= 239)
162                                                                 continue;
163                                                         unicastAddresses.Add (address);
164                                                         break;
165
166                                                 case AddressFamily.InterNetworkV6:
167                                                         if (address.IsIPv6Multicast)
168                                                                 continue;
169                                                         unicastAddresses.Add (address);
170                                                         break;
171                                         }
172                                 }
173                                 return UnicastIPAddressInformationImplCollection.LinuxFromList (unicastAddresses);
174                         }
175                 }
176
177                 [MonoTODO ("Always returns an empty collection.")]
178                 public override IPAddressCollection WinsServersAddresses {
179                         get {
180                                 // I do SUPPOSE we could scrape /etc/samba/smb.conf, but.. yeesh.
181                                 return new IPAddressCollection ();
182                         }
183                 }
184         }
185
186         class Win32IPInterfaceProperties2 : IPInterfaceProperties
187         {
188                 readonly Win32_IP_ADAPTER_ADDRESSES addr;
189                 readonly Win32_MIB_IFROW mib4, mib6;
190
191                 public Win32IPInterfaceProperties2 (Win32_IP_ADAPTER_ADDRESSES addr, Win32_MIB_IFROW mib4, Win32_MIB_IFROW mib6)
192                 {
193                         this.addr = addr;
194                         this.mib4 = mib4;
195                         this.mib6 = mib6;
196                 }
197
198                 public override IPv4InterfaceProperties GetIPv4Properties ()
199                 {
200                         Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
201                         return v4info != null ? new Win32IPv4InterfaceProperties (v4info, mib4) : null;
202                 }
203
204                 public override IPv6InterfaceProperties GetIPv6Properties ()
205                 {
206                         Win32_IP_ADAPTER_INFO v6info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib6.Index);
207                         return v6info != null ? new Win32IPv6InterfaceProperties (mib6) : null;
208                 }
209
210                 public override IPAddressInformationCollection AnycastAddresses {
211                         get { return IPAddressInformationImplCollection.Win32FromAnycast (addr.FirstAnycastAddress); }
212                 }
213
214                 public override IPAddressCollection DhcpServerAddresses {
215                         get {
216                                 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
217                                 // FIXME: should ipv6 DhcpServer be considered?
218                                 return v4info != null ? new Win32IPAddressCollection (v4info.DhcpServer) : Win32IPAddressCollection.Empty;
219                         }
220                 }
221
222                 public override IPAddressCollection DnsAddresses {
223                         get { return Win32IPAddressCollection.FromDnsServer (addr.FirstDnsServerAddress); }
224                 }
225
226                 public override string DnsSuffix {
227                         get { return addr.DnsSuffix; }
228                 }
229
230                 public override GatewayIPAddressInformationCollection GatewayAddresses {
231                         get {
232                                 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
233                                 // FIXME: should ipv6 DhcpServer be considered?
234                                 return v4info != null ? new Win32GatewayIPAddressInformationCollection (v4info.GatewayList) : Win32GatewayIPAddressInformationCollection.Empty;
235                         }
236                 }
237
238                 public override bool IsDnsEnabled {
239                         get { return Win32_FIXED_INFO.Instance.EnableDns != 0; }
240                 }
241
242                 public override bool IsDynamicDnsEnabled {
243                         get { return addr.DdnsEnabled; }
244                 }
245
246                 public override MulticastIPAddressInformationCollection MulticastAddresses {
247                         get { return MulticastIPAddressInformationImplCollection.Win32FromMulticast (addr.FirstMulticastAddress); }
248                 }
249
250                 public override UnicastIPAddressInformationCollection UnicastAddresses {
251                         get { return UnicastIPAddressInformationImplCollection.Win32FromUnicast (addr.FirstUnicastAddress); }
252                 }
253
254                 public override IPAddressCollection WinsServersAddresses {
255                         get {
256                                 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
257                                 // FIXME: should ipv6 DhcpServer be considered?
258                                 return v4info != null ? new Win32IPAddressCollection (v4info.PrimaryWinsServer, v4info.SecondaryWinsServer) : Win32IPAddressCollection.Empty;
259                         }
260                 }
261
262         }
263 }
264 #endif
265