Esempio n. 1
0
 public IPRule(string username, UInt16[] ipAddress, UInt16 mask, bool isAllowed) : base(username, isAllowed)
 {
     if (ipAddress.Length != 4)
     {
         throw new ArgumentException("ipAddress should have four octets");
     }
     myIPAddress       = ipAddress;
     myMask            = mask;
     myMaskedIPAddress = IPUtils.ApplyMask(myMask, myIPAddress);
 }
Esempio n. 2
0
 public static bool TryParse(string line, out Packet packet)
 {
     packet = null;
     //System.Console.WriteLine(line);
     string[] fields = line.Split('|');
     if (fields.Length != 2)
     {
         return(false);
     }
     UInt16[] ipAddress = IPUtils.ParseIPAddress(fields[1]);
     if (ipAddress == null)
     {
         return(false);
     }
     packet = new Packet(fields[0], ipAddress);
     return(true);
 }
Esempio n. 3
0
        private HashSet <uint> FindRulesMatchingIP(Packet packet)
        {
            HashSet <uint> maskedIPRuleSet = new HashSet <uint>();

            foreach (UInt16 mask in myMaskedIPToRuleMap.Keys)
            {
                uint maskedPacketIP = IPUtils.ApplyMask(mask, packet.IPAddress);
                Dictionary <uint, HashSet <uint> > ipRuleMap = null;
                if (myMaskedIPToRuleMap.TryGetValue(mask, out ipRuleMap))
                {
                    HashSet <uint> ruleSet = null;
                    ipRuleMap.TryGetValue(maskedPacketIP, out ruleSet);
                    if (ruleSet != null && ruleSet.Count != 0)
                    {
                        maskedIPRuleSet.UnionWith(ruleSet);
                    }
                }
            }
            return(maskedIPRuleSet);
        }
Esempio n. 4
0
        public static bool CreateIPRule(string line, out IPRule rule)
        {
            rule = null;
            //System.Console.WriteLine(line);
            string[] fields = line.Split('|');
            if (fields.Length != RULE_FIELDS_COUNT)
            {
                return(false);
            }

            string[] ipAddressWithMask = fields[1].Split('/');
            if (ipAddressWithMask.Length != 1 && ipAddressWithMask.Length != 2)
            {
                return(false);
            }

            UInt16[] ipAddress = IPUtils.ParseIPAddress(ipAddressWithMask[0]);
            if (ipAddress == null)
            {
                return(false);
            }

            UInt16 mask = NOMASK;

            if (ipAddressWithMask.Length == 2 && !UInt16.TryParse(ipAddressWithMask[1], out mask))
            {
                return(false);
            }

            bool isAllowed = false;

            if (!BaseRule.TryParseIsAllowed(fields[2], out isAllowed))
            {
                return(false);
            }

            string username = fields[0];

            rule = new IPRule(username, ipAddress, mask, isAllowed);
            return(true);
        }