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