FindBroadcastAddress() static private method

static private FindBroadcastAddress ( bool strict ) : IPAddress,
strict bool
return IPAddress,
コード例 #1
0
    private static IPAddress FindBroadcastAddress(bool strict)
    {
        NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
        NetworkInterface[] array = allNetworkInterfaces;
        int i = 0;

        while (i < array.Length)
        {
            NetworkInterface     networkInterface     = array[i];
            NetworkInterfaceType networkInterfaceType = networkInterface.NetworkInterfaceType;
            switch (networkInterfaceType)
            {
            case NetworkInterfaceType.FastEthernetFx:
            case NetworkInterfaceType.Wireless80211:
                goto IL_59;

            case (NetworkInterfaceType)70:
IL_31:
                if (networkInterfaceType != NetworkInterfaceType.Ethernet && networkInterfaceType != NetworkInterfaceType.Ethernet3Megabit && networkInterfaceType != NetworkInterfaceType.FastEthernetT && networkInterfaceType != NetworkInterfaceType.GigabitEthernet)
                {
                    goto IL_182;
                }
                goto IL_59;
            }
            goto IL_31;
IL_182:
            i++;
            continue;
IL_59:
            if (networkInterface.OperationalStatus == OperationalStatus.Up || networkInterface.OperationalStatus == OperationalStatus.Unknown)
            {
                IPInterfaceProperties iPProperties = networkInterface.GetIPProperties();
                if (!strict || DotNetPlatform.IsValidInterface(networkInterface, iPProperties))
                {
                    foreach (UnicastIPAddressInformation current in iPProperties.UnicastAddresses)
                    {
                        if (current.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            IPAddress result;
                            if (iPProperties.DhcpServerAddresses.Count == 0 && !strict)
                            {
                                byte[] addressBytes = current.Address.GetAddressBytes();
                                addressBytes[3] = 255;
                                result          = new IPAddress(addressBytes);
                                return(result);
                            }
                            byte[] addressBytes2 = iPProperties.DhcpServerAddresses[0].GetAddressBytes();
                            byte[] addressBytes3 = current.IPv4Mask.GetAddressBytes();
                            byte[] array2        = new byte[4];
                            for (int j = 0; j < addressBytes2.Length; j++)
                            {
                                array2[j] = ((addressBytes2[j] & addressBytes3[j]) | ~addressBytes3[j]);
                            }
                            result = new IPAddress(array2);
                            return(result);
                        }
                    }
                }
            }
            goto IL_182;
        }
        if (strict)
        {
            return(DotNetPlatform.FindBroadcastAddress(false));
        }
        return(IPAddress.Any);
    }
コード例 #2
0
 public override UdpIPv4Address GetBroadcastAddress()
 {
     return(new UdpIPv4Address(DotNetPlatform.FindBroadcastAddress(true).Address));
 }
コード例 #3
0
ファイル: DotNetPlatform.cs プロジェクト: K07H/The-Forest
    private static IPAddress FindBroadcastAddress(bool strict)
    {
        NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
        NetworkInterface[] array = allNetworkInterfaces;
        int i = 0;

        while (i < array.Length)
        {
            NetworkInterface     networkInterface     = array[i];
            NetworkInterfaceType networkInterfaceType = networkInterface.NetworkInterfaceType;
            switch (networkInterfaceType)
            {
            case NetworkInterfaceType.FastEthernetFx:
            case NetworkInterfaceType.Wireless80211:
                goto IL_59;

            default:
                if (networkInterfaceType == NetworkInterfaceType.Ethernet || networkInterfaceType == NetworkInterfaceType.Ethernet3Megabit || networkInterfaceType == NetworkInterfaceType.FastEthernetT || networkInterfaceType == NetworkInterfaceType.GigabitEthernet)
                {
                    goto IL_59;
                }
                break;
            }
IL_184:
            i++;
            continue;
IL_59:
            if (networkInterface.OperationalStatus == OperationalStatus.Up || networkInterface.OperationalStatus == OperationalStatus.Unknown)
            {
                IPInterfaceProperties ipproperties = networkInterface.GetIPProperties();
                if (!strict || DotNetPlatform.IsValidInterface(networkInterface, ipproperties))
                {
                    foreach (UnicastIPAddressInformation unicastIPAddressInformation in ipproperties.UnicastAddresses)
                    {
                        if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            if (ipproperties.DhcpServerAddresses.Count == 0 && !strict)
                            {
                                byte[] addressBytes = unicastIPAddressInformation.Address.GetAddressBytes();
                                addressBytes[3] = byte.MaxValue;
                                return(new IPAddress(addressBytes));
                            }
                            byte[] addressBytes2 = ipproperties.DhcpServerAddresses[0].GetAddressBytes();
                            byte[] addressBytes3 = unicastIPAddressInformation.IPv4Mask.GetAddressBytes();
                            byte[] array2        = new byte[4];
                            for (int j = 0; j < addressBytes2.Length; j++)
                            {
                                array2[j] = ((addressBytes2[j] & addressBytes3[j]) | ~addressBytes3[j]);
                            }
                            return(new IPAddress(array2));
                        }
                    }
                }
            }
            goto IL_184;
        }
        if (strict)
        {
            return(DotNetPlatform.FindBroadcastAddress(false));
        }
        return(IPAddress.Any);
    }