Merge pull request #3609 from xmcclure/checked-imageset
[mono.git] / mcs / class / System / System.Net.NetworkInformation / NetworkInterface.cs
index 2177ecf43c2792f0e1681fb95f40005c6702fc80..d1bf2e763b08f02eb55c7bd576b58c423bbacf9d 100644 (file)
@@ -41,15 +41,11 @@ using System.IO;
 using System.Globalization;
 
 namespace System.Net.NetworkInformation {
-       public abstract class NetworkInterface {
+       static class SystemNetworkInterface {
 
                static readonly NetworkInterfaceFactory nif = NetworkInterfaceFactory.Create ();
 
-               protected NetworkInterface ()
-               {
-               }
-
-               public static NetworkInterface [] GetAllNetworkInterfaces ()
+               public static NetworkInterface [] GetNetworkInterfaces ()
                {
                        try {
                                return nif.GetAllNetworkInterfaces ();
@@ -58,36 +54,28 @@ namespace System.Net.NetworkInformation {
                        }
                }
 
-               [MonoTODO("Always returns true")]
-               public static bool GetIsNetworkAvailable ()
+               public static bool InternalGetIsNetworkAvailable ()
                {
+                       // TODO:
                        return true;
                }
-               
-               public static int LoopbackInterfaceIndex {
+
+               public static int InternalLoopbackInterfaceIndex {
                        get {
                                return nif.GetLoopbackInterfaceIndex ();
                        }
                }
 
+               public static int InternalIPv6LoopbackInterfaceIndex {
+                       get {
+                               throw new NotImplementedException ();
+                       }
+               }
+
                public static IPAddress GetNetMask (IPAddress address)
                {
                        return nif.GetNetMask (address);
                }
-
-               public abstract IPInterfaceProperties GetIPProperties ();
-               public abstract IPv4InterfaceStatistics GetIPv4Statistics ();
-               public abstract PhysicalAddress GetPhysicalAddress ();
-               public abstract bool Supports (NetworkInterfaceComponent networkInterfaceComponent);
-
-               public abstract string Description { get; }
-               public abstract string Id { get; }
-               public abstract bool IsReceiveOnly { get; }
-               public abstract string Name { get; }
-               public abstract NetworkInterfaceType NetworkInterfaceType { get; }
-               public abstract OperationalStatus OperationalStatus { get; }
-               public abstract long Speed { get; }
-               public abstract bool SupportsMulticast { get; }
        }
 
        abstract class NetworkInterfaceFactory
@@ -450,9 +438,14 @@ namespace System.Net.NetworkInformation {
 #if !MOBILE
                class Win32NetworkInterfaceAPI : NetworkInterfaceFactory
                {
-                       [DllImport ("iphlpapi.dll", SetLastError = true)]
+                       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);
 
+                       [DllImport (IPHLPAPI)]
+                       static extern uint GetBestInterfaceEx (byte[] ipAddress, out int index);
+
                        unsafe static Win32_IP_ADAPTER_ADDRESSES [] GetAdaptersAddresses ()
                        {
                                byte [] bytes = null;
@@ -485,9 +478,20 @@ namespace System.Net.NetworkInformation {
                                return ret;
                        }
 
+                       private static int GetBestInterfaceForAddress (IPAddress addr) {
+                               int index;
+                               SocketAddress address = new SocketAddress (addr);
+                               int error = (int) GetBestInterfaceEx (address.m_Buffer, out index);
+                               if (error != 0) {
+                                       throw new NetworkInformationException (error);
+                               }
+
+                               return index;
+                       }
+
                        public override int GetLoopbackInterfaceIndex ()
                        {
-                               throw new NotImplementedException ();
+                               return GetBestInterfaceForAddress (IPAddress.Loopback);
                        }
 
                        public override IPAddress GetNetMask (IPAddress address)
@@ -516,7 +520,7 @@ namespace System.Net.NetworkInformation {
                                return new LinuxNetworkInterfaceAPI ();
                        }
 
-#if !MONODROID
+#if !MOBILE
                        if (Environment.OSVersion.Version >= windowsVer51)
                                return new Win32NetworkInterfaceAPI ();
 #endif