2009-06-12 Bill Holmes <billholmes54@gmail.com>
[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                 [MonoTODO ("Always returns an empty collection.")]
146                 public override IPAddressCollection DnsAddresses {
147                         get {
148                                 ParseResolvConf ();
149                                 return dns_servers;
150                         }
151                 }
152
153                 [MonoTODO ("Does not return anything.")]
154                 public override string DnsSuffix {
155                         get {
156                                 ParseResolvConf ();
157                                 return dns_suffix;
158                         }
159                 }
160
161                 [MonoTODO ("Always returns an empty collection.")]
162                 public override GatewayIPAddressInformationCollection GatewayAddresses {
163                         get {
164                                 // XXX: Pull information from route table.
165                                 return LinuxGatewayIPAddressInformationCollection.Empty;
166                         }
167                 }
168
169                 [MonoTODO ("Always returns true")]
170                 public override bool IsDnsEnabled {
171                         get {
172                                 return true;
173                         }
174                 }
175
176                 [MonoTODO ("Always returns false")]
177                 public override bool IsDynamicDnsEnabled {
178                         get {
179                                 return false;
180                         }
181                 }
182
183                 public override MulticastIPAddressInformationCollection MulticastAddresses {
184                         get {
185                                 List<IPAddress> multicastAddresses = new List<IPAddress> ();
186                                 foreach (IPAddress address in addresses) {
187                                         byte[] addressBytes = address.GetAddressBytes ();
188                                         if (addressBytes[0] >= 224 && addressBytes[0] <= 239) {
189                                                 multicastAddresses.Add (address);
190                                         }
191                                 }
192                                 return MulticastIPAddressInformationImplCollection.LinuxFromList (multicastAddresses);
193                         }
194                 }
195
196                 public override UnicastIPAddressInformationCollection UnicastAddresses {
197                         get {
198                                 List<IPAddress> unicastAddresses = new List<IPAddress> ();
199                                 foreach (IPAddress address in addresses) {
200                                         switch (address.AddressFamily) {
201                                                 case AddressFamily.InterNetwork:
202                                                         byte top = address.GetAddressBytes () [0];
203                                                         if (top >= 224 && top <= 239)
204                                                                 continue;
205                                                         unicastAddresses.Add (address);
206                                                         break;
207
208                                                 case AddressFamily.InterNetworkV6:
209                                                         if (address.IsIPv6Multicast)
210                                                                 continue;
211                                                         unicastAddresses.Add (address);
212                                                         break;
213                                         }
214                                 }
215                                 return UnicastIPAddressInformationImplCollection.LinuxFromList (unicastAddresses);
216                         }
217                 }
218
219                 [MonoTODO ("Always returns an empty collection.")]
220                 public override IPAddressCollection WinsServersAddresses {
221                         get {
222                                 // I do SUPPOSE we could scrape /etc/samba/smb.conf, but.. yeesh.
223                                 return new IPAddressCollection ();
224                         }
225                 }
226         }
227
228         class LinuxIPInterfaceProperties : UnixIPInterfaceProperties
229         {
230                 public LinuxIPInterfaceProperties (LinuxNetworkInterface iface, List <IPAddress> addresses)
231                         : base (iface, addresses)
232                 {
233                 }
234
235                 public override IPv4InterfaceProperties GetIPv4Properties ()
236                 {
237                         if (ipv4iface_properties == null)
238                                 ipv4iface_properties = new LinuxIPv4InterfaceProperties (iface as LinuxNetworkInterface);
239                         
240                         return ipv4iface_properties;
241                 }
242         }
243
244         class MacOsIPInterfaceProperties : UnixIPInterfaceProperties
245         {
246                 public MacOsIPInterfaceProperties (MacOsNetworkInterface iface, List <IPAddress> addresses)
247                         : base (iface, addresses)
248                 {
249                 }
250
251                 public override IPv4InterfaceProperties GetIPv4Properties ()
252                 {
253                         if (ipv4iface_properties == null)
254                                 ipv4iface_properties = new MacOsIPv4InterfaceProperties (iface as MacOsNetworkInterface);
255                         
256                         return ipv4iface_properties;
257                 }
258         }
259
260         class Win32IPInterfaceProperties2 : IPInterfaceProperties
261         {
262                 readonly Win32_IP_ADAPTER_ADDRESSES addr;
263                 readonly Win32_MIB_IFROW mib4, mib6;
264
265                 public Win32IPInterfaceProperties2 (Win32_IP_ADAPTER_ADDRESSES addr, Win32_MIB_IFROW mib4, Win32_MIB_IFROW mib6)
266                 {
267                         this.addr = addr;
268                         this.mib4 = mib4;
269                         this.mib6 = mib6;
270                 }
271
272                 public override IPv4InterfaceProperties GetIPv4Properties ()
273                 {
274                         Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
275                         return v4info != null ? new Win32IPv4InterfaceProperties (v4info, mib4) : null;
276                 }
277
278                 public override IPv6InterfaceProperties GetIPv6Properties ()
279                 {
280                         Win32_IP_ADAPTER_INFO v6info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib6.Index);
281                         return v6info != null ? new Win32IPv6InterfaceProperties (mib6) : null;
282                 }
283
284                 public override IPAddressInformationCollection AnycastAddresses {
285                         get { return IPAddressInformationImplCollection.Win32FromAnycast (addr.FirstAnycastAddress); }
286                 }
287
288                 public override IPAddressCollection DhcpServerAddresses {
289                         get {
290                                 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
291                                 // FIXME: should ipv6 DhcpServer be considered?
292                                 return v4info != null ? new Win32IPAddressCollection (v4info.DhcpServer) : Win32IPAddressCollection.Empty;
293                         }
294                 }
295
296                 public override IPAddressCollection DnsAddresses {
297                         get { return Win32IPAddressCollection.FromDnsServer (addr.FirstDnsServerAddress); }
298                 }
299
300                 public override string DnsSuffix {
301                         get { return addr.DnsSuffix; }
302                 }
303
304                 public override GatewayIPAddressInformationCollection GatewayAddresses {
305                         get {
306                                 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
307                                 // FIXME: should ipv6 DhcpServer be considered?
308                                 return v4info != null ? new Win32GatewayIPAddressInformationCollection (v4info.GatewayList) : Win32GatewayIPAddressInformationCollection.Empty;
309                         }
310                 }
311
312                 public override bool IsDnsEnabled {
313                         get { return Win32_FIXED_INFO.Instance.EnableDns != 0; }
314                 }
315
316                 public override bool IsDynamicDnsEnabled {
317                         get { return addr.DdnsEnabled; }
318                 }
319
320                 public override MulticastIPAddressInformationCollection MulticastAddresses {
321                         get { return MulticastIPAddressInformationImplCollection.Win32FromMulticast (addr.FirstMulticastAddress); }
322                 }
323
324                 public override UnicastIPAddressInformationCollection UnicastAddresses {
325                         get {
326                                 Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
327                                 // FIXME: should ipv6 DhcpServer be considered?
328                                 return ai != null ? UnicastIPAddressInformationImplCollection.Win32FromUnicast ((int) ai.Index, addr.FirstUnicastAddress) : UnicastIPAddressInformationImplCollection.Empty;
329                         }
330                 }
331
332                 public override IPAddressCollection WinsServersAddresses {
333                         get {
334                                 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
335                                 // FIXME: should ipv6 DhcpServer be considered?
336                                 return v4info != null ? new Win32IPAddressCollection (v4info.PrimaryWinsServer, v4info.SecondaryWinsServer) : Win32IPAddressCollection.Empty;
337                         }
338                 }
339
340         }
341 }
342 #endif
343