2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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.IO;
32 using System.Net.Sockets;
33 using System.Text.RegularExpressions;
34
35 namespace System.Net.NetworkInformation {
36         public abstract class IPInterfaceProperties {
37                 protected IPInterfaceProperties ()
38                 {
39                 }
40
41                 public abstract IPv4InterfaceProperties GetIPv4Properties ();
42                 public abstract IPv6InterfaceProperties GetIPv6Properties ();
43
44                 public abstract IPAddressInformationCollection AnycastAddresses { get; }
45                 public abstract IPAddressCollection DhcpServerAddresses { get; }
46                 public abstract IPAddressCollection DnsAddresses { get; }
47                 public abstract string DnsSuffix { get; }
48                 public abstract GatewayIPAddressInformationCollection GatewayAddresses { get; }
49                 public abstract bool IsDnsEnabled { get; }
50                 public abstract bool IsDynamicDnsEnabled { get; }
51                 public abstract MulticastIPAddressInformationCollection MulticastAddresses { get; }
52                 public abstract UnicastIPAddressInformationCollection UnicastAddresses { get; }
53                 public abstract IPAddressCollection WinsServersAddresses { get; }
54         }
55
56         abstract class UnixIPInterfaceProperties : IPInterfaceProperties
57         {
58                 protected IPv4InterfaceProperties ipv4iface_properties;
59                 protected UnixNetworkInterface iface;
60                 List <IPAddress> addresses;
61                 IPAddressCollection dns_servers;
62                 string dns_suffix;
63                 DateTime last_parse;
64                 
65                 public UnixIPInterfaceProperties (UnixNetworkInterface iface, List <IPAddress> addresses)
66                 {
67                         this.iface = iface;
68                         this.addresses = addresses;
69                 }
70
71                 public override IPv6InterfaceProperties GetIPv6Properties ()
72                 {
73                         throw new NotImplementedException ();
74                 }
75
76                 static Regex ns = new Regex (@"\s*nameserver\s+(?<address>.*)");
77                 static Regex search = new Regex (@"\s*search\s+(?<domain>.*)");
78                 void ParseResolvConf ()
79                 {
80                         try {
81                                 DateTime wt = File.GetLastWriteTime ("/etc/resolv.conf");
82                                 if (wt <= last_parse)
83                                         return;
84
85                                 last_parse = wt;
86                                 dns_suffix = "";
87                                 dns_servers = new IPAddressCollection ();
88                                 using (StreamReader reader = new StreamReader ("/etc/resolv.conf")) {
89                                         string str;
90                                         string line;
91                                         while ((line = reader.ReadLine ()) != null) {
92                                                 line = line.Trim ();
93                                                 if (line.Length == 0 || line [0] == '#')
94                                                         continue;
95                                                 Match match = ns.Match (line);
96                                                 if (match.Success) {
97                                                         try {
98                                                                 str = match.Groups ["address"].Value;
99                                                                 str = str.Trim ();
100                                                                 dns_servers.Add (IPAddress.Parse (str));
101                                                         } catch {
102                                                         }
103                                                 } else {
104                                                         match = search.Match (line);
105                                                         if (match.Success) {
106                                                                 str = match.Groups ["domain"].Value;
107                                                                 string [] parts = str.Split (',');
108                                                                 dns_suffix = parts [0].Trim ();
109                                                         }
110                                                 }
111                                         }
112                                 }
113                         } catch {
114                         } finally {
115                                 dns_servers.SetReadOnly ();
116                         }
117                 }
118
119                 public override IPAddressInformationCollection AnycastAddresses {
120                         get {
121                                 List<IPAddress> anycastAddresses = new List<IPAddress> ();
122                                 /* XXX:
123                                 foreach (IPAddress address in addresses) {
124                                         if (is_anycast_address (address)) {
125                                                 anycastAddresses.Add (address);
126                                         }
127                                 }
128                                 */
129                                 return IPAddressInformationImplCollection.LinuxFromAnycast (anycastAddresses);
130                         }
131                 }
132
133                 [MonoTODO ("Always returns an empty collection.")]
134                 public override IPAddressCollection DhcpServerAddresses {
135                         get {
136                                 // There are lots of different DHCP clients
137                                 // that all store their configuration differently.
138                                 // I'm not sure what to do here.
139                                 IPAddressCollection coll = new IPAddressCollection ();
140                                 coll.SetReadOnly ();
141                                 return coll;
142                         }
143                 }
144
145                 public override IPAddressCollection DnsAddresses {
146                         get {
147                                 ParseResolvConf ();
148                                 return dns_servers;
149                         }
150                 }
151
152                 public override string DnsSuffix {
153                         get {
154                                 ParseResolvConf ();
155                                 return dns_suffix;
156                         }
157                 }
158
159                 [MonoTODO ("Always returns an empty collection.")]
160                 public override GatewayIPAddressInformationCollection GatewayAddresses {
161                         get {
162                                 // XXX: Pull information from route table.
163                                 return LinuxGatewayIPAddressInformationCollection.Empty;
164                         }
165                 }
166
167                 [MonoTODO ("Always returns true")]
168                 public override bool IsDnsEnabled {
169                         get {
170                                 return true;
171                         }
172                 }
173
174                 [MonoTODO ("Always returns false")]
175                 public override bool IsDynamicDnsEnabled {
176                         get {
177                                 return false;
178                         }
179                 }
180
181                 public override MulticastIPAddressInformationCollection MulticastAddresses {
182                         get {
183                                 List<IPAddress> multicastAddresses = new List<IPAddress> ();
184                                 foreach (IPAddress address in addresses) {
185                                         byte[] addressBytes = address.GetAddressBytes ();
186                                         if (addressBytes[0] >= 224 && addressBytes[0] <= 239) {
187                                                 multicastAddresses.Add (address);
188                                         }
189                                 }
190                                 return MulticastIPAddressInformationImplCollection.LinuxFromList (multicastAddresses);
191                         }
192                 }
193
194                 public override UnicastIPAddressInformationCollection UnicastAddresses {
195                         get {
196                                 List<IPAddress> unicastAddresses = new List<IPAddress> ();
197                                 foreach (IPAddress address in addresses) {
198                                         switch (address.AddressFamily) {
199                                                 case AddressFamily.InterNetwork:
200                                                         byte top = address.GetAddressBytes () [0];
201                                                         if (top >= 224 && top <= 239)
202                                                                 continue;
203                                                         unicastAddresses.Add (address);
204                                                         break;
205
206                                                 case AddressFamily.InterNetworkV6:
207                                                         if (address.IsIPv6Multicast)
208                                                                 continue;
209                                                         unicastAddresses.Add (address);
210                                                         break;
211                                         }
212                                 }
213                                 return UnicastIPAddressInformationImplCollection.LinuxFromList (unicastAddresses);
214                         }
215                 }
216
217                 [MonoTODO ("Always returns an empty collection.")]
218                 public override IPAddressCollection WinsServersAddresses {
219                         get {
220                                 // I do SUPPOSE we could scrape /etc/samba/smb.conf, but.. yeesh.
221                                 return new IPAddressCollection ();
222                         }
223                 }
224         }
225
226         class LinuxIPInterfaceProperties : UnixIPInterfaceProperties
227         {
228                 public LinuxIPInterfaceProperties (LinuxNetworkInterface iface, List <IPAddress> addresses)
229                         : base (iface, addresses)
230                 {
231                 }
232
233                 public override IPv4InterfaceProperties GetIPv4Properties ()
234                 {
235                         if (ipv4iface_properties == null)
236                                 ipv4iface_properties = new LinuxIPv4InterfaceProperties (iface as LinuxNetworkInterface);
237                         
238                         return ipv4iface_properties;
239                 }
240         }
241
242         class MacOsIPInterfaceProperties : UnixIPInterfaceProperties
243         {
244                 public MacOsIPInterfaceProperties (MacOsNetworkInterface iface, List <IPAddress> addresses)
245                         : base (iface, addresses)
246                 {
247                 }
248
249                 public override IPv4InterfaceProperties GetIPv4Properties ()
250                 {
251                         if (ipv4iface_properties == null)
252                                 ipv4iface_properties = new MacOsIPv4InterfaceProperties (iface as MacOsNetworkInterface);
253                         
254                         return ipv4iface_properties;
255                 }
256         }
257
258         class Win32IPInterfaceProperties2 : IPInterfaceProperties
259         {
260                 readonly Win32_IP_ADAPTER_ADDRESSES addr;
261                 readonly Win32_MIB_IFROW mib4, mib6;
262
263                 public Win32IPInterfaceProperties2 (Win32_IP_ADAPTER_ADDRESSES addr, Win32_MIB_IFROW mib4, Win32_MIB_IFROW mib6)
264                 {
265                         this.addr = addr;
266                         this.mib4 = mib4;
267                         this.mib6 = mib6;
268                 }
269
270                 public override IPv4InterfaceProperties GetIPv4Properties ()
271                 {
272                         Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
273                         return v4info != null ? new Win32IPv4InterfaceProperties (v4info, mib4) : null;
274                 }
275
276                 public override IPv6InterfaceProperties GetIPv6Properties ()
277                 {
278                         Win32_IP_ADAPTER_INFO v6info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib6.Index);
279                         return v6info != null ? new Win32IPv6InterfaceProperties (mib6) : null;
280                 }
281
282                 public override IPAddressInformationCollection AnycastAddresses {
283                         get { return IPAddressInformationImplCollection.Win32FromAnycast (addr.FirstAnycastAddress); }
284                 }
285
286                 public override IPAddressCollection DhcpServerAddresses {
287                         get {
288                                 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
289                                 // FIXME: should ipv6 DhcpServer be considered?
290                                 return v4info != null ? new Win32IPAddressCollection (v4info.DhcpServer) : Win32IPAddressCollection.Empty;
291                         }
292                 }
293
294                 public override IPAddressCollection DnsAddresses {
295                         get { return Win32IPAddressCollection.FromDnsServer (addr.FirstDnsServerAddress); }
296                 }
297
298                 public override string DnsSuffix {
299                         get { return addr.DnsSuffix; }
300                 }
301
302                 public override GatewayIPAddressInformationCollection GatewayAddresses {
303                         get {
304                                 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
305                                 // FIXME: should ipv6 DhcpServer be considered?
306                                 return v4info != null ? new Win32GatewayIPAddressInformationCollection (v4info.GatewayList) : Win32GatewayIPAddressInformationCollection.Empty;
307                         }
308                 }
309
310                 public override bool IsDnsEnabled {
311                         get { return Win32_FIXED_INFO.Instance.EnableDns != 0; }
312                 }
313
314                 public override bool IsDynamicDnsEnabled {
315                         get { return addr.DdnsEnabled; }
316                 }
317
318                 public override MulticastIPAddressInformationCollection MulticastAddresses {
319                         get { return MulticastIPAddressInformationImplCollection.Win32FromMulticast (addr.FirstMulticastAddress); }
320                 }
321
322                 public override UnicastIPAddressInformationCollection UnicastAddresses {
323                         get {
324                                 Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
325                                 // FIXME: should ipv6 DhcpServer be considered?
326                                 return ai != null ? UnicastIPAddressInformationImplCollection.Win32FromUnicast ((int) ai.Index, addr.FirstUnicastAddress) : UnicastIPAddressInformationImplCollection.Empty;
327                         }
328                 }
329
330                 public override IPAddressCollection WinsServersAddresses {
331                         get {
332                                 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
333                                 // FIXME: should ipv6 DhcpServer be considered?
334                                 return v4info != null ? new Win32IPAddressCollection (v4info.PrimaryWinsServer, v4info.SecondaryWinsServer) : Win32IPAddressCollection.Empty;
335                         }
336                 }
337
338         }
339 }
340 #endif
341