Merge pull request #4248 from Unity-Technologies/boehm-gc-alloc-fixed
[mono.git] / mcs / class / System / System.Net.NetworkInformation / IPInterfaceProperties.cs
index 9a8560a92b58275149f51640218549c4c1a15652..bcae621e00d7c71ef58a52c46f7bdcbb83c665cb 100644 (file)
@@ -41,8 +41,6 @@ namespace System.Net.NetworkInformation {
                protected UnixNetworkInterface iface;
                List <IPAddress> addresses;
                IPAddressCollection dns_servers;
-               string dns_suffix;
-               DateTime last_parse;
                
                public UnixIPInterfaceProperties (UnixNetworkInterface iface, List <IPAddress> addresses)
                {
@@ -54,9 +52,39 @@ namespace System.Net.NetworkInformation {
                {
                        throw new NotImplementedException ();
                }
+#if MONODROID
+               [DllImport ("__Internal")]
+               static extern int _monodroid_get_dns_servers (out IntPtr dns_servers_array);
 
+               void GetDNSServersFromOS ()
+               {
+                       IntPtr dsa;
+                       int len = _monodroid_get_dns_servers (out dsa);
+                       if (len <= 0)
+                               return;
+
+                       var servers = new IntPtr [len];
+                       Marshal.Copy (dsa, servers, 0, len);
+
+                       dns_servers = new IPAddressCollection ();
+                       foreach (IntPtr s in servers) {
+                               string server_ip = Marshal.PtrToStringAnsi (s);
+                               Marshal.FreeHGlobal (s);
+
+                               IPAddress addr;
+                               if (!IPAddress.TryParse (server_ip, out addr))
+                                       continue;
+                               dns_servers.InternalAdd (addr);
+                       }
+                       Marshal.FreeHGlobal (dsa);
+               }
+#else
                static Regex ns = new Regex (@"\s*nameserver\s+(?<address>.*)");
                static Regex search = new Regex (@"\s*search\s+(?<domain>.*)");
+
+               string dns_suffix;
+               DateTime last_parse;
+
                void ParseResolvConf ()
                {
                        try {
@@ -95,7 +123,7 @@ namespace System.Net.NetworkInformation {
                        } catch {
                        }
                }
-
+#endif
                public override IPAddressInformationCollection AnycastAddresses {
                        get {
                                var c = new IPAddressInformationCollection ();
@@ -119,15 +147,23 @@ namespace System.Net.NetworkInformation {
 
                public override IPAddressCollection DnsAddresses {
                        get {
+#if MONODROID
+                               GetDNSServersFromOS ();
+#else
                                ParseResolvConf ();
+#endif
                                return dns_servers;
                        }
                }
 
                public override string DnsSuffix {
                        get {
+#if MONODROID
+                               return String.Empty;
+#else
                                ParseResolvConf ();
                                return dns_suffix;
+#endif
                        }
                }
 
@@ -290,7 +326,7 @@ namespace System.Net.NetworkInformation {
                }
        }
 
-#if !MOBILE
+#if WIN_PLATFORM
        class Win32IPInterfaceProperties2 : IPInterfaceProperties
        {
                readonly Win32_IP_ADAPTER_ADDRESSES addr;
@@ -306,13 +342,13 @@ namespace System.Net.NetworkInformation {
                public override IPv4InterfaceProperties GetIPv4Properties ()
                {
                        Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
-                       return v4info != null ? new Win32IPv4InterfaceProperties (v4info, mib4) : null;
+                       return new Win32IPv4InterfaceProperties (v4info, mib4);
                }
 
                public override IPv6InterfaceProperties GetIPv6Properties ()
                {
                        Win32_IP_ADAPTER_INFO v6info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib6.Index);
-                       return v6info != null ? new Win32IPv6InterfaceProperties (mib6) : null;
+                       return new Win32IPv6InterfaceProperties (mib6);
                }
 
                public override IPAddressInformationCollection AnycastAddresses {
@@ -337,7 +373,11 @@ namespace System.Net.NetworkInformation {
                        get {
                                Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
                                // FIXME: should ipv6 DhcpServer be considered?
-                               return v4info != null ? new Win32IPAddressCollection (v4info.DhcpServer) : Win32IPAddressCollection.Empty;
+                               try {
+                                       return new Win32IPAddressCollection (v4info.DhcpServer);
+                               } catch (IndexOutOfRangeException) {
+                                       return Win32IPAddressCollection.Empty;
+                               }
                        }
                }
 
@@ -351,18 +391,17 @@ namespace System.Net.NetworkInformation {
 
                public override GatewayIPAddressInformationCollection GatewayAddresses {
                        get {
-                               Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
-                               // FIXME: should ipv6 DhcpServer be considered?
-
                                var col = new GatewayIPAddressInformationCollection ();
-                               if (v4info != null) {
+                               try {
+                                       Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
+                                       // FIXME: should ipv6 DhcpServer be considered?
+
                                        var a = v4info.GatewayList;
                                        if (!String.IsNullOrEmpty (a.IpAddress)) {
                                                col.InternalAdd(new SystemGatewayIPAddressInformation(IPAddress.Parse (a.IpAddress)));
                                                AddSubsequently (a.Next, col);
                                        }
-                               }
-
+                               } catch (IndexOutOfRangeException) {}
                                return col;
                        }
                }
@@ -377,7 +416,7 @@ namespace System.Net.NetworkInformation {
                }
 
                public override bool IsDnsEnabled {
-                       get { return Win32_FIXED_INFO.Instance.EnableDns != 0; }
+                       get { return Win32NetworkInterface.FixedInfo.EnableDns != 0; }
                }
 
                public override bool IsDynamicDnsEnabled {
@@ -404,28 +443,36 @@ namespace System.Net.NetworkInformation {
 
                public override UnicastIPAddressInformationCollection UnicastAddresses {
                        get {
-                               Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
-                               // FIXME: should ipv6 DhcpServer be considered?
-                               return ai != null ? Win32FromUnicast ((int) ai.Index, addr.FirstUnicastAddress) : new UnicastIPAddressInformationCollection ();
+                               try {
+                                       Win32_IP_ADAPTER_INFO ai = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
+                                       // FIXME: should ipv6 DhcpServer be considered?
+                                       return Win32FromUnicast (addr.FirstUnicastAddress);
+                               } catch (IndexOutOfRangeException) {
+                                       return new UnicastIPAddressInformationCollection ();
+                               }
                        }
                }
 
-               static UnicastIPAddressInformationCollection Win32FromUnicast (int ifIndex, IntPtr ptr)
+               static UnicastIPAddressInformationCollection Win32FromUnicast (IntPtr ptr)
                {
                        UnicastIPAddressInformationCollection c = new UnicastIPAddressInformationCollection ();
                        Win32_IP_ADAPTER_UNICAST_ADDRESS a;
                        for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
                                a = (Win32_IP_ADAPTER_UNICAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_UNICAST_ADDRESS));
-                               c.InternalAdd (new Win32UnicastIPAddressInformation (ifIndex, a));
+                               c.InternalAdd (new Win32UnicastIPAddressInformation (a));
                        }
                        return c;
                }
 
                public override IPAddressCollection WinsServersAddresses {
                        get {
-                               Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
-                               // FIXME: should ipv6 DhcpServer be considered?
-                               return v4info != null ? new Win32IPAddressCollection (v4info.PrimaryWinsServer, v4info.SecondaryWinsServer) : Win32IPAddressCollection.Empty;
+                               try {
+                                       Win32_IP_ADAPTER_INFO v4info = Win32NetworkInterface2.GetAdapterInfoByIndex (mib4.Index);
+                                       // FIXME: should ipv6 DhcpServer be considered?
+                                       return new Win32IPAddressCollection (v4info.PrimaryWinsServer, v4info.SecondaryWinsServer);
+                               } catch (IndexOutOfRangeException) {
+                                       return Win32IPAddressCollection.Empty;
+                               }
                        }
                }