2 // System.Net.NetworkInformation.IPInterfaceProperties
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
8 // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.Globalization;
32 using System.Net.Sockets;
33 using System.Runtime.CompilerServices;
34 using System.Text.RegularExpressions;
35 using System.Runtime.InteropServices;
37 namespace System.Net.NetworkInformation {
38 abstract class UnixIPInterfaceProperties : IPInterfaceProperties
40 protected IPv4InterfaceProperties ipv4iface_properties;
41 protected UnixNetworkInterface iface;
42 List <IPAddress> addresses;
43 IPAddressCollection dns_servers;
45 public UnixIPInterfaceProperties (UnixNetworkInterface iface, List <IPAddress> addresses)
48 this.addresses = addresses;
51 public override IPv6InterfaceProperties GetIPv6Properties ()
53 throw new NotImplementedException ();
56 [DllImport ("__Internal")]
57 static extern int _monodroid_get_dns_servers (out IntPtr dns_servers_array);
59 void GetDNSServersFromOS ()
62 int len = _monodroid_get_dns_servers (out dsa);
66 var servers = new IntPtr [len];
67 Marshal.Copy (dsa, servers, 0, len);
69 dns_servers = new IPAddressCollection ();
70 foreach (IntPtr s in servers) {
71 string server_ip = Marshal.PtrToStringAnsi (s);
72 Marshal.FreeHGlobal (s);
75 if (!IPAddress.TryParse (server_ip, out addr))
77 dns_servers.InternalAdd (addr);
79 Marshal.FreeHGlobal (dsa);
82 static Regex ns = new Regex (@"\s*nameserver\s+(?<address>.*)");
83 static Regex search = new Regex (@"\s*search\s+(?<domain>.*)");
88 void ParseResolvConf ()
91 DateTime wt = File.GetLastWriteTime ("/etc/resolv.conf");
97 dns_servers = new IPAddressCollection ();
98 using (StreamReader reader = new StreamReader ("/etc/resolv.conf")) {
101 while ((line = reader.ReadLine ()) != null) {
103 if (line.Length == 0 || line [0] == '#')
105 Match match = ns.Match (line);
108 str = match.Groups ["address"].Value;
110 dns_servers.InternalAdd (IPAddress.Parse (str));
114 match = search.Match (line);
116 str = match.Groups ["domain"].Value;
117 string [] parts = str.Split (',');
118 dns_suffix = parts [0].Trim ();
127 public override IPAddressInformationCollection AnycastAddresses {
129 var c = new IPAddressInformationCollection ();
130 foreach (IPAddress address in addresses) {
131 c.InternalAdd (new SystemIPAddressInformation (address, false, false));
137 [MonoTODO ("Always returns an empty collection.")]
138 public override IPAddressCollection DhcpServerAddresses {
140 // There are lots of different DHCP clients
141 // that all store their configuration differently.
142 // I'm not sure what to do here.
143 IPAddressCollection coll = new IPAddressCollection ();
148 public override IPAddressCollection DnsAddresses {
151 GetDNSServersFromOS ();
159 public override string DnsSuffix {
170 [MonoTODO ("Always returns true")]
171 public override bool IsDnsEnabled {
177 [MonoTODO ("Always returns false")]
178 public override bool IsDynamicDnsEnabled {
184 public override MulticastIPAddressInformationCollection MulticastAddresses {
186 var multicastAddresses = new MulticastIPAddressInformationCollection ();
187 foreach (IPAddress address in addresses) {
188 byte[] addressBytes = address.GetAddressBytes ();
189 if (addressBytes[0] >= 224 && addressBytes[0] <= 239) {
190 multicastAddresses.InternalAdd (new SystemMulticastIPAddressInformation (new SystemIPAddressInformation (address, true, false)));
193 return multicastAddresses;
197 public override UnicastIPAddressInformationCollection UnicastAddresses {
199 var unicastAddresses = new UnicastIPAddressInformationCollection ();
200 foreach (IPAddress address in addresses) {
201 switch (address.AddressFamily) {
202 case AddressFamily.InterNetwork:
203 byte top = address.GetAddressBytes () [0];
204 if (top >= 224 && top <= 239)
206 unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
209 case AddressFamily.InterNetworkV6:
210 if (address.IsIPv6Multicast)
212 unicastAddresses.InternalAdd (new LinuxUnicastIPAddressInformation (address));
216 return unicastAddresses;
220 [MonoTODO ("Always returns an empty collection.")]
221 public override IPAddressCollection WinsServersAddresses {
223 // I do SUPPOSE we could scrape /etc/samba/smb.conf, but.. yeesh.
224 return new IPAddressCollection ();
229 class LinuxIPInterfaceProperties : UnixIPInterfaceProperties
231 public LinuxIPInterfaceProperties (LinuxNetworkInterface iface, List <IPAddress> addresses)
232 : base (iface, addresses)
236 public override IPv4InterfaceProperties GetIPv4Properties ()
238 if (ipv4iface_properties == null)
239 ipv4iface_properties = new LinuxIPv4InterfaceProperties (iface as LinuxNetworkInterface);
241 return ipv4iface_properties;
244 IPAddressCollection ParseRouteInfo (string iface)
246 var col = new IPAddressCollection ();
248 using (StreamReader reader = new StreamReader ("/proc/net/route")) {
250 reader.ReadLine (); // Ignore first line
251 while ((line = reader.ReadLine ()) != null) {
253 if (line.Length == 0)
256 string [] parts = line.Split ('\t');
257 if (parts.Length < 3)
259 string gw_address = parts [2].Trim ();
260 byte [] ipbytes = new byte [4];
261 if (gw_address.Length == 8 && iface.Equals (parts [0], StringComparison.OrdinalIgnoreCase)) {
262 for (int i = 0; i < 4; i++) {
263 if (!Byte.TryParse (gw_address.Substring (i * 2, 2), NumberStyles.HexNumber, null, out ipbytes [3 - i]))
266 IPAddress ip = new IPAddress (ipbytes);
267 if (!ip.Equals (IPAddress.Any) && !col.Contains (ip))
268 col.InternalAdd (ip);
278 public override GatewayIPAddressInformationCollection GatewayAddresses {
280 return SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection (ParseRouteInfo (this.iface.Name.ToString()));
285 class MacOsIPInterfaceProperties : UnixIPInterfaceProperties
287 public MacOsIPInterfaceProperties (MacOsNetworkInterface iface, List <IPAddress> addresses)
288 : base (iface, addresses)
292 public override IPv4InterfaceProperties GetIPv4Properties ()
294 if (ipv4iface_properties == null)
295 ipv4iface_properties = new MacOsIPv4InterfaceProperties (iface as MacOsNetworkInterface);
297 return ipv4iface_properties;
300 [MethodImplAttribute(MethodImplOptions.InternalCall)]
301 private extern static bool ParseRouteInfo_internal(string iface, out string[] gw_addr_list);
303 public override GatewayIPAddressInformationCollection GatewayAddresses {
305 var gateways = new IPAddressCollection ();
306 string[] gw_addrlist;
307 if (!ParseRouteInfo_internal (this.iface.Name.ToString(), out gw_addrlist))
308 return new GatewayIPAddressInformationCollection ();
310 for(int i=0; i<gw_addrlist.Length; i++) {
312 IPAddress ip = IPAddress.Parse(gw_addrlist[i]);
313 if (!ip.Equals (IPAddress.Any) && !gateways.Contains (ip))
314 gateways.InternalAdd (ip);
315 } catch (ArgumentNullException) {
316 /* Ignore this, as the
317 * internal call might have
318 * left some blank entries at
319 * the end of the array
324 return SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection (gateways);
330 class Win32IPInterfaceProperties2 : IPInterfaceProperties
332 readonly Win32_IP_ADAPTER_ADDRESSES addr;
333 readonly Win32_MIB_IFROW mib4, mib6;
335 public Win32IPInterfaceProperties2 (Win32_IP_ADAPTER_ADDRESSES addr, Win32_MIB_IFROW mib4, Win32_MIB_IFROW mib6)
342 public override IPv4InterfaceProperties GetIPv4Properties ()
344 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
345 return new Win32IPv4InterfaceProperties (v4info, mib4);
348 public override IPv6InterfaceProperties GetIPv6Properties ()
350 Win32_IP_ADAPTER_INFO v6info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib6.Index);
351 return new Win32IPv6InterfaceProperties (mib6);
354 public override IPAddressInformationCollection AnycastAddresses {
355 get { return Win32FromAnycast (addr.FirstAnycastAddress); }
358 static IPAddressInformationCollection Win32FromAnycast (IntPtr ptr)
360 var c = new IPAddressInformationCollection ();
361 Win32_IP_ADAPTER_ANYCAST_ADDRESS a;
362 for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
363 a = (Win32_IP_ADAPTER_ANYCAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_ANYCAST_ADDRESS));
364 c.InternalAdd (new SystemIPAddressInformation (
365 a.Address.GetIPAddress (),
366 a.LengthFlags.IsDnsEligible,
367 a.LengthFlags.IsTransient));
372 public override IPAddressCollection DhcpServerAddresses {
374 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
375 // FIXME: should ipv6 DhcpServer be considered?
377 return new Win32IPAddressCollection (v4info.DhcpServer);
378 } catch (IndexOutOfRangeException) {
379 return Win32IPAddressCollection.Empty;
384 public override IPAddressCollection DnsAddresses {
385 get { return Win32IPAddressCollection.FromDnsServer (addr.FirstDnsServerAddress); }
388 public override string DnsSuffix {
389 get { return addr.DnsSuffix; }
392 public override GatewayIPAddressInformationCollection GatewayAddresses {
394 var col = new GatewayIPAddressInformationCollection ();
396 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
397 // FIXME: should ipv6 DhcpServer be considered?
399 var a = v4info.GatewayList;
400 if (!String.IsNullOrEmpty (a.IpAddress)) {
401 col.InternalAdd(new SystemGatewayIPAddressInformation(IPAddress.Parse (a.IpAddress)));
402 AddSubsequently (a.Next, col);
404 } catch (IndexOutOfRangeException) {}
409 static void AddSubsequently (IntPtr head, GatewayIPAddressInformationCollection col)
411 Win32_IP_ADDR_STRING a;
412 for (IntPtr p = head; p != IntPtr.Zero; p = a.Next) {
413 a = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p, typeof (Win32_IP_ADDR_STRING));
414 col.InternalAdd (new SystemGatewayIPAddressInformation (IPAddress.Parse (a.IpAddress)));
418 public override bool IsDnsEnabled {
419 get { return Win32NetworkInterface.FixedInfo.EnableDns != 0; }
422 public override bool IsDynamicDnsEnabled {
423 get { return addr.DdnsEnabled; }
426 public override MulticastIPAddressInformationCollection MulticastAddresses {
427 get { return Win32FromMulticast (addr.FirstMulticastAddress); }
430 static MulticastIPAddressInformationCollection Win32FromMulticast (IntPtr ptr)
432 var c = new MulticastIPAddressInformationCollection ();
433 Win32_IP_ADAPTER_MULTICAST_ADDRESS a;
434 for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
435 a = (Win32_IP_ADAPTER_MULTICAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_MULTICAST_ADDRESS));
436 c.InternalAdd (new SystemMulticastIPAddressInformation (new SystemIPAddressInformation (
437 a.Address.GetIPAddress (),
438 a.LengthFlags.IsDnsEligible,
439 a.LengthFlags.IsTransient)));
444 public override UnicastIPAddressInformationCollection UnicastAddresses {
447 Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
448 // FIXME: should ipv6 DhcpServer be considered?
449 return Win32FromUnicast (addr.FirstUnicastAddress);
450 } catch (IndexOutOfRangeException) {
451 return new UnicastIPAddressInformationCollection ();
456 static UnicastIPAddressInformationCollection Win32FromUnicast (IntPtr ptr)
458 UnicastIPAddressInformationCollection c = new UnicastIPAddressInformationCollection ();
459 Win32_IP_ADAPTER_UNICAST_ADDRESS a;
460 for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
461 a = (Win32_IP_ADAPTER_UNICAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_UNICAST_ADDRESS));
462 c.InternalAdd (new Win32UnicastIPAddressInformation (a));
467 public override IPAddressCollection WinsServersAddresses {
470 Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
471 // FIXME: should ipv6 DhcpServer be considered?
472 return new Win32IPAddressCollection (v4info.PrimaryWinsServer, v4info.SecondaryWinsServer);
473 } catch (IndexOutOfRangeException) {
474 return Win32IPAddressCollection.Empty;