/// <summary>
        /// Gets the first <see cref="System.Net.NetworkInformation.NetworkInterface"/> which has the given address bound.
        /// </summary>
        /// <param name="localAddress">The address which should be bound to the interface.</param>
        /// <returns>The <see cref="System.Net.NetworkInformation.NetworkInterface"/> associated with the address or the default if none were found.</returns>
        public static System.Net.NetworkInformation.NetworkInterface GetNetworkInterface(System.Net.IPAddress localAddress)
        {
            if (localAddress == null)
            {
                throw new System.ArgumentNullException();
            }

            bool isMulticast = IPAddressExtensions.IsMulticast(localAddress);

            //Iterate all NetworkInterfaves
            foreach (System.Net.NetworkInformation.NetworkInterface networkInterface in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
            {
                //Only look for interfaces which are UP
                if (networkInterface.OperationalStatus != System.Net.NetworkInformation.OperationalStatus.Up)
                {
                    continue;
                }

                if (isMulticast)
                {
                    if (false == networkInterface.SupportsMulticast)
                    {
                        continue;
                    }

                    //Check for the Multicast Address to be bound on the networkInterface
                    foreach (System.Net.NetworkInformation.MulticastIPAddressInformation ip in networkInterface.GetIPProperties().MulticastAddresses)
                    {
                        //If equal return
                        if (System.Net.IPAddress.Equals(localAddress, ip.Address))
                        {
                            return(networkInterface);
                        }
                    }
                }
                else
                {
                    //Check for the Unicast Address to be bound on the networkInterface
                    foreach (System.Net.NetworkInformation.UnicastIPAddressInformation ip in networkInterface.GetIPProperties().UnicastAddresses)
                    {
                        //If equal return
                        if (System.Net.IPAddress.Equals(localAddress, ip.Address))
                        {
                            return(networkInterface);
                        }
                    }

                    //Check for the Anycast Address to be bound on the networkInterface
                    //if(s.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) foreach (System.Net.NetworkInformation.UnicastIPAddressInformation ip in networkInterface.GetIPProperties().AnycastAddresses)
                    //{
                    //    if (System.Net.IPAddress.Equals(localEndPoint.Address, ip.Address))
                    //    {
                    //        return networkInterface;
                    //    }
                    //}
                }
            }

            return(default(System.Net.NetworkInformation.NetworkInterface));
        }
Exemplo n.º 2
0
        public static bool IsMulticast(this System.Net.IPAddress ipAddress)
        {
            if (ipAddress == null)
            {
                return(false);
            }

            byte[] addressBytes;

            switch (ipAddress.AddressFamily)
            {
            case System.Net.Sockets.AddressFamily.InterNetwork:
            {
#if UNSAFE
                unsafe
                {
                    //Read bits as a byte
                    byte prop = (byte)(long *)ipAddress.Address;

                    //Return the result of the evaluation
                    return(prop >= 224 && prop <= 239);
                }
#elif NATIVE
                byte highIP = System.Runtime.InteropServices.Marshal.ReadByte(ipAddress.Address, 0);

                return(highIP >= 224 && highIP <= 239);
#else
                byte highIP = (byte)(ipAddress.Address & byte.MaxValue);         // ipAddress.GetAddressBytes()[0];

                return(highIP >= 224 && highIP <= 239);
#endif
            }

            case System.Net.Sockets.AddressFamily.InterNetworkV6:
            {
                //Check for a ipv6 multicast address
                if (ipAddress.IsIPv6Multicast)
                {
                    return(true);
                }

                //could use out overload and check in place or pass out to MapToIpv4...

                //Check if mapped to v6 from v4 and unmap
                if (IPAddressExtensions.IsIPv4MappedToIPv6(ipAddress, out addressBytes)) //(ipAddress.IsIPv4MappedToIPv6)
                {
                    ipAddress = IPAddressExtensions.MapToIPv4(addressBytes);             //ipAddress.MapToIPv4();

                    //handle as v4
                    goto case System.Net.Sockets.AddressFamily.InterNetwork;
                }

                return(false);
            }

            default: return(false);
            }
        }