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);
            }
        }
Beispiel #2
0
        private void EndReceive(UDPSocket socket, byte[] buffer, int offset, int count, System.Net.EndPoint ep)
        {
            if (count > 0)
            {
                byte[] bytes = new byte[count];
                Buffer.BlockCopy(buffer, 0, bytes, 0, count);

                if (ep.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    IPEndPoint ipep = (IPEndPoint)ep;
                    if (IPAddressExtensions.IsIPv4MappedToIPv6(ipep.Address))
                    {
                        ipep.Address = IPAddressExtensions.MapToIPv4(ipep.Address);
                    }
                }

                FireDataReceived(bytes, ep);
            }

            BeginReceive(socket);
        }