Changing Win32_IP_ADAPTER_INFO to struct to work on AOT builds
authorHenric Müller <hemuller@microsoft.com>
Tue, 17 Jan 2017 15:20:03 +0000 (16:20 +0100)
committerHenric Müller <hemuller@microsoft.com>
Wed, 25 Jan 2017 15:34:55 +0000 (16:34 +0100)
mcs/class/System/System.Net.NetworkInformation/IPInterfaceProperties.cs
mcs/class/System/System.Net.NetworkInformation/NetworkInterface.cs
mcs/class/System/System.Net.NetworkInformation/Win32NetworkInterfaceMarshal.cs

index 3d0e2404648cb368576db60e593164e63a7fd3c5..bcae621e00d7c71ef58a52c46f7bdcbb83c665cb 100644 (file)
@@ -342,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 {
@@ -373,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;
+                               }
                        }
                }
 
@@ -387,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;
                        }
                }
@@ -440,9 +443,13 @@ 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 (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 ();
+                               }
                        }
                }
 
@@ -459,9 +466,13 @@ namespace System.Net.NetworkInformation {
 
                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;
+                               }
                        }
                }
 
index 3897bf1dd6da344fa216d2a4cf74534ae818726e..0967c80485dd8988f9c0936f0017bf3981fa2446 100644 (file)
@@ -441,30 +441,28 @@ namespace System.Net.NetworkInformation {
                        private const string IPHLPAPI = "iphlpapi.dll";
 
                        [DllImport (IPHLPAPI, SetLastError = true)]
-                       static extern int GetAdaptersAddresses (uint family, uint flags, IntPtr reserved, byte [] info, ref int size);
+                       static extern int GetAdaptersAddresses (uint family, uint flags, IntPtr reserved, IntPtr info, ref int size);
 
                        [DllImport (IPHLPAPI)]
                        static extern uint GetBestInterfaceEx (byte[] ipAddress, out int index);
 
-                       unsafe static Win32_IP_ADAPTER_ADDRESSES [] GetAdaptersAddresses ()
+                       static Win32_IP_ADAPTER_ADDRESSES [] GetAdaptersAddresses ()
                        {
-                               byte [] bytes = null;
+                               IntPtr ptr = IntPtr.Zero;
                                int len = 0;
-                               GetAdaptersAddresses (0, 0, IntPtr.Zero, bytes, ref len);
-                               bytes = new byte [len];
-                               int ret = GetAdaptersAddresses (0, 0, IntPtr.Zero, bytes, ref len);
+                               GetAdaptersAddresses (0, 0, IntPtr.Zero, ptr, ref len);
+                               ptr = Marshal.AllocHGlobal(len);
+                               int ret = GetAdaptersAddresses (0, 0, IntPtr.Zero, ptr, ref len);
                                if (ret != 0)
                                        throw new NetworkInformationException (ret);
 
                                List<Win32_IP_ADAPTER_ADDRESSES> l = new List<Win32_IP_ADAPTER_ADDRESSES> ();
-                               fixed (byte* ptr = bytes) {
-                                       Win32_IP_ADAPTER_ADDRESSES info;
-                                       for (IntPtr p = (IntPtr) ptr; p != IntPtr.Zero; p = info.Next) {
-                                               info = new Win32_IP_ADAPTER_ADDRESSES ();
-                                               Marshal.PtrToStructure (p, info);
-                                               l.Add (info);
-                                       }
+                               Win32_IP_ADAPTER_ADDRESSES info;
+                               for (IntPtr p = ptr; p != IntPtr.Zero; p = info.Next) {
+                                       info = Marshal.PtrToStructure<Win32_IP_ADAPTER_ADDRESSES> (p);
+                                       l.Add (info);
                                }
+
                                return l.ToArray ();
                        }
 
@@ -811,7 +809,7 @@ namespace System.Net.NetworkInformation {
        class Win32NetworkInterface2 : NetworkInterface
        {
                [DllImport ("iphlpapi.dll", SetLastError = true)]
-               static extern int GetAdaptersInfo (byte [] info, ref int size);
+               static extern int GetAdaptersInfo (IntPtr info, ref int size);
 
                [DllImport ("iphlpapi.dll", SetLastError = true)]
                static extern int GetIfEntry (ref Win32_MIB_IFROW row);
@@ -821,28 +819,25 @@ namespace System.Net.NetworkInformation {
                        foreach (Win32_IP_ADAPTER_INFO info in GetAdaptersInfo ())
                                if (info.Index == index)
                                        return info;
-                       return null;
+                       throw new IndexOutOfRangeException ("No adapter found for index " + index);
                }
 
-               unsafe static Win32_IP_ADAPTER_INFO [] GetAdaptersInfo ()
+               static Win32_IP_ADAPTER_INFO [] GetAdaptersInfo ()
                {
-                       byte [] bytes = null;
                        int len = 0;
-                       GetAdaptersInfo (bytes, ref len);
-                       bytes = new byte [len];
-                       int ret = GetAdaptersInfo (bytes, ref len);
+                       IntPtr ptr = IntPtr.Zero;
+                       GetAdaptersInfo (ptr, ref len);
+                       ptr = Marshal.AllocHGlobal(len);
+                       int ret = GetAdaptersInfo (ptr, ref len);
 
                        if (ret != 0)
                                throw new NetworkInformationException (ret);
 
                        List<Win32_IP_ADAPTER_INFO> l = new List<Win32_IP_ADAPTER_INFO> ();
-                       fixed (byte* ptr = bytes) {
-                               Win32_IP_ADAPTER_INFO info;
-                               for (IntPtr p = (IntPtr) ptr; p != IntPtr.Zero; p = info.Next) {
-                                       info = new Win32_IP_ADAPTER_INFO ();
-                                       Marshal.PtrToStructure (p, info);
-                                       l.Add (info);
-                               }
+                       Win32_IP_ADAPTER_INFO info;
+                       for (IntPtr p = ptr; p != IntPtr.Zero; p = info.Next) {
+                               info = Marshal.PtrToStructure<Win32_IP_ADAPTER_INFO> (p);
+                               l.Add (info);
                        }
                        return l.ToArray ();
                }
index 1ae3bc30f04290bc3e1ee6bb45aaefc60e83030b..88ae0b8e3589b60d62e6609279f20a2eb10815bf 100644 (file)
@@ -101,7 +101,7 @@ namespace System.Net.NetworkInformation
        }
 
        [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Unicode)]
-       class Win32_IP_ADAPTER_ADDRESSES {
+       struct Win32_IP_ADAPTER_ADDRESSES {
                public AlignmentUnion Alignment;
                public IntPtr Next; // to Win32_IP_ADAPTER_ADDRESSES
                [MarshalAs (UnmanagedType.LPStr)]
@@ -147,7 +147,7 @@ namespace System.Net.NetworkInformation
        }
 
        [StructLayout (LayoutKind.Sequential)]
-       class Win32_IP_ADAPTER_INFO
+       struct Win32_IP_ADAPTER_INFO
        {
                const int MAX_ADAPTER_NAME_LENGTH = 256;
                const int MAX_ADAPTER_DESCRIPTION_LENGTH = 128;