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