Inheritance: NetworkInterface
Exemple #1
0
            private static void ChangedAddress(object sender, EventArgs eventArgs)
            {
                lock (syncObject){
                    bool isAvailableNow = SystemNetworkInterface.InternalGetIsNetworkAvailable();

                    if (isAvailableNow != isAvailable)
                    {
                        isAvailable = isAvailableNow;

                        DictionaryEntry[] callerArray = new DictionaryEntry[s_availabilityCallerArray.Count];
                        s_availabilityCallerArray.CopyTo(callerArray, 0);

                        for (int i = 0; i < callerArray.Length; i++)
                        {
                            NetworkAvailabilityChangedEventHandler handler = (NetworkAvailabilityChangedEventHandler)callerArray[i].Key;
                            ExecutionContext context = (ExecutionContext)callerArray[i].Value;
                            if (context == null)
                            {
                                handler(null, new NetworkAvailabilityEventArgs(isAvailable));
                            }
                            else
                            {
                                ExecutionContext.Run(context.CreateCopy(), s_RunHandlerCallback, handler);
                            }
                        }
                    }
                }
            }
Exemple #2
0
        /// Returns objects that describe the network interfaces on the local computer.
        public static NetworkInterface[] GetAllNetworkInterfaces()
        {
#if FEATURE_MONO_CAS
            (new NetworkInformationPermission(NetworkInformationAccess.Read)).Demand();
#endif
            return(SystemNetworkInterface.GetNetworkInterfaces());
        }
Exemple #3
0
        /// Returns objects that describe the network interfaces on the local computer.
        public static NetworkInterface[] GetAllNetworkInterfaces()
        {
#if !DISABLE_CAS_USE
            (new NetworkInformationPermission(NetworkInformationAccess.Read)).Demand();
#endif
            return(SystemNetworkInterface.GetNetworkInterfaces());
        }
            private static void ChangedAddress(object sender, EventArgs eventArgs)
            {
                lock (s_syncObject)
                {
                    bool isAvailableNow = SystemNetworkInterface.InternalGetIsNetworkAvailable();

                    if (isAvailableNow != s_isAvailable)
                    {
                        s_isAvailable = isAvailableNow;

                        var s_copy =
                            new Dictionary <NetworkAvailabilityChangedEventHandler, ExecutionContext>(s_availabilityCallerArray);

                        foreach (var handler in s_copy.Keys)
                        {
                            ExecutionContext context = s_copy[handler];
                            if (context == null)
                            {
                                handler(null, new NetworkAvailabilityEventArgs(s_isAvailable));
                            }
                            else
                            {
                                ExecutionContext.Run(context, s_RunHandlerCallback, handler);
                            }
                        }
                    }
                }
            }
        private static NetworkInterface[] GetNetworkInterfaces(AddressFamily family)
        {
            IpHelperErrors.CheckFamilyUnspecified(family);
            if (ComNetOS.IsPostWin2K)
            {
                return(PostWin2KGetNetworkInterfaces(family));
            }
            FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo();

            if ((family != AddressFamily.Unspecified) && (family != AddressFamily.InterNetwork))
            {
                throw new PlatformNotSupportedException(SR.GetString("WinXPRequired"));
            }
            SafeLocalFree pAdapterInfo = null;
            uint          pOutBufLen   = 0;
            ArrayList     list         = new ArrayList();
            uint          adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(SafeLocalFree.Zero, ref pOutBufLen);

            while (adaptersInfo == 0x6f)
            {
                try
                {
                    pAdapterInfo = SafeLocalFree.LocalAlloc((int)pOutBufLen);
                    adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(pAdapterInfo, ref pOutBufLen);
                    if (adaptersInfo == 0)
                    {
                        IpAdapterInfo ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(pAdapterInfo.DangerousGetHandle(), typeof(IpAdapterInfo));
                        list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterInfo));
                        while (ipAdapterInfo.Next != IntPtr.Zero)
                        {
                            ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(ipAdapterInfo.Next, typeof(IpAdapterInfo));
                            list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterInfo));
                        }
                    }
                    continue;
                }
                finally
                {
                    if (pAdapterInfo != null)
                    {
                        pAdapterInfo.Close();
                    }
                }
            }
            if (adaptersInfo == 0xe8)
            {
                return(new SystemNetworkInterface[0]);
            }
            if (adaptersInfo != 0)
            {
                throw new NetworkInformationException((int)adaptersInfo);
            }
            SystemNetworkInterface[] interfaceArray = new SystemNetworkInterface[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                interfaceArray[i] = (SystemNetworkInterface)list[i];
            }
            return(interfaceArray);
        }
Exemple #6
0
        public static bool GetIsNetworkAvailable()
        {
#if WASM
            throw new PlatformNotSupportedException();
#else
            return(SystemNetworkInterface.InternalGetIsNetworkAvailable());
#endif
        }
Exemple #7
0
        /// Returns objects that describe the network interfaces on the local computer.
        public static NetworkInterface[] GetAllNetworkInterfaces()
        {
#if WASM
            throw new PlatformNotSupportedException();
#else
#if MONO_FEATURE_CAS
            (new NetworkInformationPermission(NetworkInformationAccess.Read)).Demand();
#endif
            return(SystemNetworkInterface.GetNetworkInterfaces());
#endif
        }
        private static SystemNetworkInterface[] GetAdaptersAddresses(AddressFamily family, FixedInfo fixedInfo)
        {
            uint          outBufLen        = 0;
            SafeLocalFree adapterAddresses = null;
            ArrayList     list             = new ArrayList();

            SystemNetworkInterface[] interfaceArray = null;
            uint num2 = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(family, 0, IntPtr.Zero, SafeLocalFree.Zero, ref outBufLen);

            while (num2 == 0x6f)
            {
                try
                {
                    adapterAddresses = SafeLocalFree.LocalAlloc((int)outBufLen);
                    num2             = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(family, 0, IntPtr.Zero, adapterAddresses, ref outBufLen);
                    if (num2 == 0)
                    {
                        IpAdapterAddresses ipAdapterAddresses = (IpAdapterAddresses)Marshal.PtrToStructure(adapterAddresses.DangerousGetHandle(), typeof(IpAdapterAddresses));
                        list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterAddresses));
                        while (ipAdapterAddresses.next != IntPtr.Zero)
                        {
                            ipAdapterAddresses = (IpAdapterAddresses)Marshal.PtrToStructure(ipAdapterAddresses.next, typeof(IpAdapterAddresses));
                            list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterAddresses));
                        }
                    }
                    continue;
                }
                finally
                {
                    if (adapterAddresses != null)
                    {
                        adapterAddresses.Close();
                    }
                    adapterAddresses = null;
                }
            }
            switch (num2)
            {
            case 0xe8:
            case 0x57:
                return(new SystemNetworkInterface[0]);
            }
            if (num2 != 0)
            {
                throw new NetworkInformationException((int)num2);
            }
            interfaceArray = new SystemNetworkInterface[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                interfaceArray[i] = (SystemNetworkInterface)list[i];
            }
            return(interfaceArray);
        }
            private static void ChangedAddress(object?sender, EventArgs eventArgs)
            {
                Dictionary <NetworkAvailabilityChangedEventHandler, ExecutionContext?>?availabilityChangedSubscribers = null;

                lock (s_globalLock)
                {
                    bool isAvailableNow = SystemNetworkInterface.InternalGetIsNetworkAvailable();

                    // If there is an Availability Change, need to execute user callbacks.
                    if (isAvailableNow != s_isAvailable)
                    {
                        s_isAvailable = isAvailableNow;

                        if (s_availabilityChangedSubscribers.Count > 0)
                        {
                            availabilityChangedSubscribers = new Dictionary <NetworkAvailabilityChangedEventHandler, ExecutionContext?>(s_availabilityChangedSubscribers);
                        }
                    }
                }

                // Executing user callbacks if Availability Change event occured.
                if (availabilityChangedSubscribers != null)
                {
                    bool isAvailable = s_isAvailable;
                    NetworkAvailabilityEventArgs args = isAvailable ? s_availableEventArgs : s_notAvailableEventArgs;
                    ContextCallback callbackContext   = isAvailable ? s_runHandlerAvailable : s_runHandlerNotAvailable;

                    foreach (KeyValuePair <NetworkAvailabilityChangedEventHandler, ExecutionContext?>
                             subscriber in availabilityChangedSubscribers)
                    {
                        NetworkAvailabilityChangedEventHandler handler = subscriber.Key;
                        ExecutionContext?ec = subscriber.Value;

                        if (ec == null) // Flow supressed
                        {
                            handler(null, args);
                        }
                        else
                        {
                            ExecutionContext.Run(ec, callbackContext, handler);
                        }
                    }
                }
            }
            private static void ChangedAddress(object sender, EventArgs eventArgs)
            {
                DictionaryEntry[] callerArray = null;

                lock (s_globalLock) {
                    bool isAvailableNow = SystemNetworkInterface.InternalGetIsNetworkAvailable();

                    if (isAvailableNow != isAvailable)
                    {
                        isAvailable = isAvailableNow;

                        callerArray = new DictionaryEntry[s_availabilityCallerArray.Count];
                        s_availabilityCallerArray.CopyTo(callerArray, 0);
                    }
                }

                // Release the s_globalLock before calling into user callback.
                if (callerArray != null)
                {
                    // Synchronization dedicated just to the invocation of the callback,
                    // for back compat reason.
                    lock (s_protectCallbackLock)
                    {
                        Debug.Assert(!Monitor.IsEntered(s_globalLock), "Should not invoke user callback while holding globalLock");

                        foreach (var entry in callerArray)
                        {
                            NetworkAvailabilityChangedEventHandler handler = (NetworkAvailabilityChangedEventHandler)entry.Key;
                            ExecutionContext context = (ExecutionContext)entry.Value;
                            if (context == null)
                            {
                                handler(null, new NetworkAvailabilityEventArgs(isAvailable));
                            }
                            else
                            {
                                ExecutionContext.Run(context.CreateCopy(), s_RunHandlerCallback, handler);
                            }
                        }
                    }
                }
            }
            private static void ChangedAddress(object sender, EventArgs eventArgs)
            {
                Dictionary <NetworkAvailabilityChangedEventHandler, ExecutionContext> copy = null;

                lock (s_globalLock)
                {
                    bool isAvailableNow = SystemNetworkInterface.InternalGetIsNetworkAvailable();

                    // If there is an Availability Change, need to execute user callbacks.
                    if (isAvailableNow != s_isAvailable)
                    {
                        s_isAvailable = isAvailableNow;

                        copy =
                            new Dictionary <NetworkAvailabilityChangedEventHandler, ExecutionContext>(s_availabilityCallerArray);
                    }
                }

                // Executing user callbacks if Availability Change event occured.
                if (copy != null)
                {
                    foreach (var entry in copy)
                    {
                        NetworkAvailabilityChangedEventHandler handler = entry.Key;
                        ExecutionContext context = entry.Value;
                        if (context == null)
                        {
                            handler(null, new NetworkAvailabilityEventArgs(s_isAvailable));
                        }
                        else
                        {
                            ExecutionContext.Run(context, s_RunHandlerCallback, handler);
                        }
                    }
                }
            }
Exemple #12
0
 public static NetworkInterface[] GetAllNetworkInterfaces()
 {
     new NetworkInformationPermission(NetworkInformationAccess.Read).Demand();
     return(SystemNetworkInterface.GetNetworkInterfaces());
 }
 private static NetworkInterface[] GetNetworkInterfaces(AddressFamily family)
 {
     IpHelperErrors.CheckFamilyUnspecified(family);
     if (ComNetOS.IsPostWin2K)
     {
         return PostWin2KGetNetworkInterfaces(family);
     }
     FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo();
     if ((family != AddressFamily.Unspecified) && (family != AddressFamily.InterNetwork))
     {
         throw new PlatformNotSupportedException(SR.GetString("WinXPRequired"));
     }
     SafeLocalFree pAdapterInfo = null;
     uint pOutBufLen = 0;
     ArrayList list = new ArrayList();
     uint adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(SafeLocalFree.Zero, ref pOutBufLen);
     while (adaptersInfo == 0x6f)
     {
         try
         {
             pAdapterInfo = SafeLocalFree.LocalAlloc((int) pOutBufLen);
             adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(pAdapterInfo, ref pOutBufLen);
             if (adaptersInfo == 0)
             {
                 IpAdapterInfo ipAdapterInfo = (IpAdapterInfo) Marshal.PtrToStructure(pAdapterInfo.DangerousGetHandle(), typeof(IpAdapterInfo));
                 list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterInfo));
                 while (ipAdapterInfo.Next != IntPtr.Zero)
                 {
                     ipAdapterInfo = (IpAdapterInfo) Marshal.PtrToStructure(ipAdapterInfo.Next, typeof(IpAdapterInfo));
                     list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterInfo));
                 }
             }
             continue;
         }
         finally
         {
             if (pAdapterInfo != null)
             {
                 pAdapterInfo.Close();
             }
         }
     }
     if (adaptersInfo == 0xe8)
     {
         return new SystemNetworkInterface[0];
     }
     if (adaptersInfo != 0)
     {
         throw new NetworkInformationException((int) adaptersInfo);
     }
     SystemNetworkInterface[] interfaceArray = new SystemNetworkInterface[list.Count];
     for (int i = 0; i < list.Count; i++)
     {
         interfaceArray[i] = (SystemNetworkInterface) list[i];
     }
     return interfaceArray;
 }
        private static SystemNetworkInterface[] PostWin2KGetNetworkInterfaces(AddressFamily family)
        {
            FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo();

            SystemNetworkInterface[] adaptersAddresses = null;
Label_0008:
            try
            {
                adaptersAddresses = GetAdaptersAddresses(family, fixedInfo);
            }
            catch (NetworkInformationException exception)
            {
                if (exception.ErrorCode != 1L)
                {
                    throw;
                }
                goto Label_0008;
            }
            if (Socket.OSSupportsIPv4)
            {
                uint          pOutBufLen   = 0;
                uint          adaptersInfo = 0;
                SafeLocalFree pAdapterInfo = null;
                if ((family == AddressFamily.Unspecified) || (family == AddressFamily.InterNetwork))
                {
                    adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(SafeLocalFree.Zero, ref pOutBufLen);
                    int num3 = 0;
                    while (adaptersInfo == 0x6f)
                    {
                        try
                        {
                            pAdapterInfo = SafeLocalFree.LocalAlloc((int)pOutBufLen);
                            adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(pAdapterInfo, ref pOutBufLen);
                            if (adaptersInfo == 0)
                            {
                                IntPtr handle = pAdapterInfo.DangerousGetHandle();
                                while (handle != IntPtr.Zero)
                                {
                                    IpAdapterInfo ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(handle, typeof(IpAdapterInfo));
                                    for (int i = 0; i < adaptersAddresses.Length; i++)
                                    {
                                        if ((adaptersAddresses[i] != null) && (ipAdapterInfo.index == adaptersAddresses[i].index))
                                        {
                                            if (!adaptersAddresses[i].interfaceProperties.Update(fixedInfo, ipAdapterInfo))
                                            {
                                                adaptersAddresses[i] = null;
                                                num3++;
                                            }
                                            break;
                                        }
                                    }
                                    handle = ipAdapterInfo.Next;
                                }
                            }
                            continue;
                        }
                        finally
                        {
                            if (pAdapterInfo != null)
                            {
                                pAdapterInfo.Close();
                            }
                        }
                    }
                    if (num3 != 0)
                    {
                        SystemNetworkInterface[] interfaceArray2 = new SystemNetworkInterface[adaptersAddresses.Length - num3];
                        int num5 = 0;
                        for (int j = 0; j < adaptersAddresses.Length; j++)
                        {
                            if (adaptersAddresses[j] != null)
                            {
                                interfaceArray2[num5++] = adaptersAddresses[j];
                            }
                        }
                        adaptersAddresses = interfaceArray2;
                    }
                }
                if ((adaptersInfo != 0) && (adaptersInfo != 0xe8))
                {
                    throw new NetworkInformationException((int)adaptersInfo);
                }
            }
            return(adaptersAddresses);
        }
 private static SystemNetworkInterface[] PostWin2KGetNetworkInterfaces(AddressFamily family)
 {
     FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo();
     SystemNetworkInterface[] adaptersAddresses = null;
 Label_0008:
     try
     {
         adaptersAddresses = GetAdaptersAddresses(family, fixedInfo);
     }
     catch (NetworkInformationException exception)
     {
         if (exception.ErrorCode != 1L)
         {
             throw;
         }
         goto Label_0008;
     }
     if (Socket.OSSupportsIPv4)
     {
         uint pOutBufLen = 0;
         uint adaptersInfo = 0;
         SafeLocalFree pAdapterInfo = null;
         if ((family == AddressFamily.Unspecified) || (family == AddressFamily.InterNetwork))
         {
             adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(SafeLocalFree.Zero, ref pOutBufLen);
             int num3 = 0;
             while (adaptersInfo == 0x6f)
             {
                 try
                 {
                     pAdapterInfo = SafeLocalFree.LocalAlloc((int) pOutBufLen);
                     adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(pAdapterInfo, ref pOutBufLen);
                     if (adaptersInfo == 0)
                     {
                         IntPtr handle = pAdapterInfo.DangerousGetHandle();
                         while (handle != IntPtr.Zero)
                         {
                             IpAdapterInfo ipAdapterInfo = (IpAdapterInfo) Marshal.PtrToStructure(handle, typeof(IpAdapterInfo));
                             for (int i = 0; i < adaptersAddresses.Length; i++)
                             {
                                 if ((adaptersAddresses[i] != null) && (ipAdapterInfo.index == adaptersAddresses[i].index))
                                 {
                                     if (!adaptersAddresses[i].interfaceProperties.Update(fixedInfo, ipAdapterInfo))
                                     {
                                         adaptersAddresses[i] = null;
                                         num3++;
                                     }
                                     break;
                                 }
                             }
                             handle = ipAdapterInfo.Next;
                         }
                     }
                     continue;
                 }
                 finally
                 {
                     if (pAdapterInfo != null)
                     {
                         pAdapterInfo.Close();
                     }
                 }
             }
             if (num3 != 0)
             {
                 SystemNetworkInterface[] interfaceArray2 = new SystemNetworkInterface[adaptersAddresses.Length - num3];
                 int num5 = 0;
                 for (int j = 0; j < adaptersAddresses.Length; j++)
                 {
                     if (adaptersAddresses[j] != null)
                     {
                         interfaceArray2[num5++] = adaptersAddresses[j];
                     }
                 }
                 adaptersAddresses = interfaceArray2;
             }
         }
         if ((adaptersInfo != 0) && (adaptersInfo != 0xe8))
         {
             throw new NetworkInformationException((int) adaptersInfo);
         }
     }
     return adaptersAddresses;
 }
 private static SystemNetworkInterface[] GetAdaptersAddresses(AddressFamily family, FixedInfo fixedInfo)
 {
     uint outBufLen = 0;
     SafeLocalFree adapterAddresses = null;
     ArrayList list = new ArrayList();
     SystemNetworkInterface[] interfaceArray = null;
     uint num2 = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(family, 0, IntPtr.Zero, SafeLocalFree.Zero, ref outBufLen);
     while (num2 == 0x6f)
     {
         try
         {
             adapterAddresses = SafeLocalFree.LocalAlloc((int) outBufLen);
             num2 = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(family, 0, IntPtr.Zero, adapterAddresses, ref outBufLen);
             if (num2 == 0)
             {
                 IpAdapterAddresses ipAdapterAddresses = (IpAdapterAddresses) Marshal.PtrToStructure(adapterAddresses.DangerousGetHandle(), typeof(IpAdapterAddresses));
                 list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterAddresses));
                 while (ipAdapterAddresses.next != IntPtr.Zero)
                 {
                     ipAdapterAddresses = (IpAdapterAddresses) Marshal.PtrToStructure(ipAdapterAddresses.next, typeof(IpAdapterAddresses));
                     list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterAddresses));
                 }
             }
             continue;
         }
         finally
         {
             if (adapterAddresses != null)
             {
                 adapterAddresses.Close();
             }
             adapterAddresses = null;
         }
     }
     switch (num2)
     {
         case 0xe8:
         case 0x57:
             return new SystemNetworkInterface[0];
     }
     if (num2 != 0)
     {
         throw new NetworkInformationException((int) num2);
     }
     interfaceArray = new SystemNetworkInterface[list.Count];
     for (int i = 0; i < list.Count; i++)
     {
         interfaceArray[i] = (SystemNetworkInterface) list[i];
     }
     return interfaceArray;
 }
Exemple #17
0
 public static bool GetIsNetworkAvailable()
 {
     return(SystemNetworkInterface.InternalGetIsNetworkAvailable());
 }