How to check if an IP address is within a particular subnet

后端 未结 7 1434
深忆病人
深忆病人 2020-12-01 12:15

I have a subnet in the format 10.132.0.0/20 and an IP address from the ASP.Net request object.

Is there a .NET framework function to check to see if the IP address i

7条回答
  •  执笔经年
    2020-12-01 12:19

    I have also created a class which calculates the network and broadcast address and checks if the IP is neither broadcast nor network address.

    private static IPValidationFailedReason PerformIPRangeValidation(string ipAddress, string subnetMask)
            {
                IPValidationFailedReason ipValidationType = IPValidationFailedReason.None;
                string networkaddress = string.Empty;
                string broadcastAddress = string.Empty;
                string networkAddressBinary = string.Empty;
                string broadcastAddressBinary = string.Empty;
                int zerosCountInSubnetMask = 0;
    
                Array.ForEach(subnetMask.Split(SplitterChar), (eachOctet) => Array.ForEach(IPInterfaceHelper.GetOctetWithPadding(eachOctet).Where(c => c == CharZero).ToArray(), (k) => zerosCountInSubnetMask++));
    
                if (zerosCountInSubnetMask == 0)
                {
                    return ipValidationType;
                }
    
                string ipAddressBinary = IPInterfaceHelper.ToBinary(ipAddress);
                networkAddressBinary = GetNetworkAddressInBinaryFormat(zerosCountInSubnetMask, ipAddressBinary);
                broadcastAddressBinary = GetBroadcastAddressInBinaryFormat(zerosCountInSubnetMask, ipAddressBinary);
    
                networkaddress = ToIPFromBinary(networkAddressBinary);
                broadcastAddress = ToIPFromBinary(broadcastAddressBinary);
    
                if (ipAddress == networkaddress)
                {
                    ipValidationType = IPValidationFailedReason.NetworkAddressZero;
                    return ipValidationType;
                }
                if (ipAddress == broadcastAddress)
                {
                    ipValidationType = IPValidationFailedReason.BroadcastAddressNotPermiited;
                    return ipValidationType;
                }
    
                return ipValidationType;
            }
    
    
    private static string GetNetworkAddressInBinaryFormat(int zeroCountInSubnetMask, string ipAddressBinary)
        {
            string networkAddressBinary = string.Empty;
            int countOfOnesInSubnetMask = TotalBitCount - zeroCountInSubnetMask;
            StringBuilder sb = new StringBuilder(ipAddressBinary);
            //When Subnet is like 255.255.255.0
            if (zeroCountInSubnetMask >= 1 && zeroCountInSubnetMask <= 8)
            {
                networkAddressBinary = sb.Replace(CharOne, CharZero, countOfOnesInSubnetMask + 3, zeroCountInSubnetMask).ToString();
            }
            //When Subnet is like 255.255.0.0
            if (zeroCountInSubnetMask > 8 && zeroCountInSubnetMask <= 16)
            {
                networkAddressBinary = sb.Replace(CharOne, CharZero, countOfOnesInSubnetMask + 2, zeroCountInSubnetMask + 1).ToString();
            }
            //When Subnet is like 255.0.0.0
            if (zeroCountInSubnetMask > 16 && zeroCountInSubnetMask <= 24)
            {
                networkAddressBinary = sb.Replace(CharOne, CharZero, countOfOnesInSubnetMask + 1, zeroCountInSubnetMask + 2).ToString();
            }
            //When Subnet is like 128.0.0.0
            if (zeroCountInSubnetMask > 24 && zeroCountInSubnetMask < 32)
            {
                networkAddressBinary = sb.Replace(CharOne, CharZero, countOfOnesInSubnetMask , zeroCountInSubnetMask + 3).ToString();
            }
            return networkAddressBinary;
        }
    
    
     private static string GetBroadcastAddressInBinaryFormat(int zeroCountInSubnetMask, string ipAddressBinary)
        {
            string broadcastAddressBinary = string.Empty;
            int countOfOnesInSubnetMask = TotalBitCount - zeroCountInSubnetMask;
            StringBuilder sb = new StringBuilder(ipAddressBinary);
            //When Subnet is like 255.255.255.0
            if (zeroCountInSubnetMask >= 1 && zeroCountInSubnetMask <= 8)
            {
                broadcastAddressBinary = sb.Replace(CharZero, CharOne, countOfOnesInSubnetMask + 3, zeroCountInSubnetMask).ToString();
            }
            //When Subnet is like 255.255.0.0
            if (zeroCountInSubnetMask > 8 && zeroCountInSubnetMask <= 16)
            {
                broadcastAddressBinary = sb.Replace(CharZero, CharOne, countOfOnesInSubnetMask + 2, zeroCountInSubnetMask + 1).ToString();
            }
            //When Subnet is like 255.0.0.0
            if (zeroCountInSubnetMask > 16 && zeroCountInSubnetMask <= 24)
            {
                broadcastAddressBinary = sb.Replace(CharZero, CharOne, countOfOnesInSubnetMask + 1, zeroCountInSubnetMask + 2).ToString();
            }
            //When Subnet is like 128.0.0.0
            if (zeroCountInSubnetMask > 24 && zeroCountInSubnetMask < 32)
            {
                broadcastAddressBinary = sb.Replace(CharZero, CharOne, countOfOnesInSubnetMask , zeroCountInSubnetMask + 3).ToString();
            }
            return broadcastAddressBinary;
        }
    
    private static string ToIPFromBinary(string ipAddressBinary)
            {
                string addrTemp = string.Empty;
                string[] networkAddressBinaryOctets = ipAddressBinary.Split(SplitterChar);
                foreach (var eachOctet in networkAddressBinaryOctets)
                {
                    string temp = Convert.ToUInt32(eachOctet, 2).ToString(CultureInfo.InvariantCulture);
                    addrTemp += temp + SplitterChar;
                }
                // remove last '.'
                string ipAddress = addrTemp.Substring(0, addrTemp.Length - 1);
                return ipAddress;
            }
    

提交回复
热议问题