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