示例#1
0
        public static bool MatchAddress(IPAddress Address, string strRanges, NetworkMonitor.AdapterInfo NicInfo = null)
        {
            int        type  = Address.GetAddressBytes().Length == 4 ? 4 : 6;
            BigInteger numIP = NetFunc.IpToInt(Address);

            foreach (string range in strRanges.Split(','))
            {
                string[] strTemp = range.Split('-');
                if (strTemp.Length == 1)
                {
                    if (strTemp[0].Contains("/")) // ip/net
                    {
                        string[]   strTemp2 = strTemp[0].Split('/');
                        int        temp;
                        BigInteger num1 = NetFunc.IpStrToInt(strTemp2[0], out temp);
                        int        pow  = MiscFunc.parseInt(strTemp2[1]);
                        BigInteger num2 = num1 + BigInteger.Pow(new BigInteger(2), pow);

                        if (type == temp && num1 <= numIP && numIP <= num2)
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        string Addresses = GetSpecialNet(strTemp[0].Trim(), NicInfo);
                        if (Addresses != null)
                        {
                            if (Addresses.Length > 0)
                            {
                                return(MatchAddress(Address, Addresses));
                            }
                        }
                        else
                        {
                            int        temp;
                            BigInteger num1 = NetFunc.IpStrToInt(strTemp[0], out temp);
                            if (type == temp && num1 == numIP)
                            {
                                return(true);
                            }
                        }
                    }
                }
                else if (strTemp.Length == 2)
                {
                    int        temp;
                    BigInteger num1 = NetFunc.IpStrToInt(strTemp[0], out temp);
                    BigInteger num2 = NetFunc.IpStrToInt(strTemp[1], out temp);
                    if (type == temp && num1 <= numIP && numIP <= num2)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
示例#2
0
        public static bool MatchAddress(string strIP, string strRanges)
        {
            int        type;
            BigInteger numIP = NetFunc.IpStrToInt(strIP, out type);

            foreach (string range in strRanges.Split(','))
            {
                string[] strTemp = range.Split('-');
                if (strTemp.Length == 1)
                {
                    if (strTemp[0].Contains("/")) // ip/net
                    {
                        string[]   strTemp2 = strTemp[0].Split('/');
                        int        temp;
                        BigInteger num1 = NetFunc.IpStrToInt(strTemp2[0], out temp);
                        int        pow  = MiscFunc.parseInt(strTemp2[1]);
                        BigInteger num2 = num1 + BigInteger.Pow(new BigInteger(2), pow);

                        if (type == temp && num1 <= numIP && numIP <= num2)
                        {
                            return(true);
                        }
                    }
                    else if (FirewallRule.SpecialAddresses.Contains(strTemp[0].Trim(), StringComparer.OrdinalIgnoreCase))
                    {
                        return(MatchAddress(strIP, NetFunc.GetSpecialNet(strTemp[0].Trim())));
                    }
                    else
                    {
                        int        temp;
                        BigInteger num1 = NetFunc.IpStrToInt(strTemp[0], out temp);
                        if (type == temp && num1 == numIP)
                        {
                            return(true);
                        }
                    }
                }
                else if (strTemp.Length == 2)
                {
                    int        temp;
                    BigInteger num1 = NetFunc.IpStrToInt(strTemp[0], out temp);
                    BigInteger num2 = NetFunc.IpStrToInt(strTemp[1], out temp);
                    if (type == temp && num1 <= numIP && numIP <= num2)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
示例#3
0
        public static bool ValidateAddress(string Address, ref string reason)
        {
            string[] strTemp = Address.Split('-');
            if (strTemp.Length == 1)
            {
                int        temp;
                BigInteger num;
                if (strTemp[0].Contains("/")) // ip/net
                {
                    string[] strTemp2 = strTemp[0].Split('/');
                    if (strTemp2.Length != 2)
                    {
                        reason = Translate.fmt("err_invalid_subnet");
                        return(false);
                    }

                    num = NetFunc.IpStrToInt(strTemp2[0], out temp);
                    int        pow  = MiscFunc.parseInt(strTemp2[1]);
                    BigInteger num2 = num + BigInteger.Pow(new BigInteger(2), pow);

                    BigInteger numMax = NetFunc.MaxIPofType(temp);
                    if (num2 > numMax)
                    {
                        reason = Translate.fmt("err_invalid_subnet");
                        return(false);
                    }
                }
                else
                {
                    num = NetFunc.IpStrToInt(strTemp[0], out temp);
                }

                if (temp != 4 && temp != 6)
                {
                    reason = Translate.fmt("err_invalid_ip");
                    return(false);
                }
            }
            else if (strTemp.Length == 2)
            {
                int        tempL;
                BigInteger numL = NetFunc.IpStrToInt(strTemp[0], out tempL);
                int        tempR;
                BigInteger numR = NetFunc.IpStrToInt(strTemp[1], out tempR);

                if ((tempL != 4 && tempL != 6) || tempL != tempR)
                {
                    reason = Translate.fmt("err_invalid_ip");
                    return(false);
                }

                if (!(numL < numR))
                {
                    reason = Translate.fmt("err_invalid_range");
                    return(false);
                }
            }
            else
            {
                reason = Translate.fmt("err_invalid_range");
                return(false);
            }
            return(true);
        }