Beispiel #1
0
        public override IpTablesChainSet ListRules(String table)
        {
            IpTablesChainSet chains = new IpTablesChainSet(_ipVersion);

            var ipc = GetInterface(table);

            foreach (String chain in ipc.GetChains())
            {
                chains.AddChain(chain, table, _system);
            }

            foreach (var chain in chains)
            {
                foreach (var ipc_rule in ipc.GetRules(chain.Name))
                {
                    String rule = ipc.GetRuleString(chain.Name, ipc_rule);
                    if (rule == null)
                    {
                        throw new IpTablesNetException("Unable to get string version of rule");
                    }
                    chains.AddRule(IpTablesRule.Parse(rule, _system, chains, table));
                }
            }

            return(chains);
        }
        public static IpTablesChainSet GetRulesFromOutput(NetfilterSystem system, String output, String table, int ipVersion, bool ignoreErrors = false)
        {
            var    ret    = new IpTablesChainSet(ipVersion);
            String ttable = null;

            foreach (string lineRaw in output.Split(new[] { '\n' }))
            {
                string line = lineRaw.Trim();

                if (String.IsNullOrEmpty(line))
                {
                    continue;
                }

                char          c = line[0];
                IpTablesRule  rule;
                IpTablesChain chain;
                switch (c)
                {
                case '*':
                    ttable = line.Substring(1);
                    break;

                case ':':
                    string[] split = line.Split(new[] { ' ' });
                    ret.AddChain(new IpTablesChain(ttable, split[0].Substring(1), ipVersion, system));
                    break;

                //Byte & packet count
                case '[':
                    int positionEnd = line.IndexOf(']');
                    if (positionEnd == -1)
                    {
                        throw new IpTablesNetException("Parsing error, could not find end of counters");
                    }
                    string[] counters = line.Substring(1, positionEnd - 1).Split(new[] { ':' });
                    line = line.Substring(positionEnd + 1);

                    try
                    {
                        rule = IpTablesRule.Parse(line, system, ret, ttable);
                    }
                    catch
                    {
                        if (ignoreErrors)
                        {
                            continue;
                        }
                        throw;
                    }
                    rule.Counters = new PacketCounters(long.Parse(counters[0]), long.Parse(counters[1]));
                    ret.AddRule(rule);
                    break;


                case '-':
                    rule = IpTablesRule.Parse(line, system, ret, ttable);
                    ret.AddRule(rule);
                    break;

                case '#':
                    break;

                case 'C':
                    if (line == "COMMIT" && ttable == table)
                    {
                        if (ttable == null)
                        {
                            throw new IpTablesNetException("Parsing error");
                        }
                        return(ret);
                    }
                    throw new IpTablesNetException("Unexepected table \"" + table + "\" found \"" + ttable + "\" instead");
                }
            }

            return(null);
        }
        public override IpTablesChainSet ListRules(String table)
        {
            IpTablesChainSet chains = new IpTablesChainSet(_ipVersion);
            
            var ipc = GetInterface(table);

            foreach (String chain in ipc.GetChains())
            {
                var newChain = chains.AddChain(chain, table, _system);
                Debug.Assert(newChain.IpVersion == _ipVersion);
            }

            Debug.Assert(_ipVersion == chains.IpVersion);
            foreach (var chain in chains)
            {
                foreach (var ipc_rule in ipc.GetRules(chain.Name))
                {
                    String rule = ipc.GetRuleString(chain.Name, ipc_rule);
                    if (rule == null)
                    {
                        throw new IpTablesNetException("Unable to get string version of rule");
                    }
                    chains.AddRule(IpTablesRule.Parse(rule, _system, chains, _ipVersion, table));
                }
            }   

            return chains;
        }
        public static IpTablesChainSet GetRulesFromOutput(NetfilterSystem system, String output, String table, int ipVersion, bool ignoreErrors = false)
        {
            var ret = new IpTablesChainSet(ipVersion);
            String ttable = null;

            foreach (string lineRaw in output.Split(new[] { '\n' }))
            {
                string line = lineRaw.Trim();

                if (String.IsNullOrEmpty(line))
                    continue;

                char c = line[0];
                IpTablesRule rule;
                IpTablesChain chain;
                switch (c)
                {
                    case '*':
                        ttable = line.Substring(1);
                        break;

                    case ':':
                        string[] split = line.Split(new[] { ' ' });
                        ret.AddChain(new IpTablesChain(ttable, split[0].Substring(1), ipVersion, system));
                        break;

                    //Byte & packet count
                    case '[':
                        int positionEnd = line.IndexOf(']');
                        if (positionEnd == -1)
                        {
                            throw new IpTablesNetException("Parsing error, could not find end of counters");
                        }
                        string[] counters = line.Substring(1, positionEnd - 1).Split(new[] { ':' });
                        line = line.Substring(positionEnd + 1);

                        try
                        {
                            rule = IpTablesRule.Parse(line, system, ret, ipVersion, ttable);
                        }
                        catch
                        {
                            if (ignoreErrors)
                            {
                                continue;
                            }
                            throw;
                        }
                        rule.Counters = new PacketCounters(long.Parse(counters[0]), long.Parse(counters[1]));
                        ret.AddRule(rule);
                        break;


                    case '-':
                        rule = IpTablesRule.Parse(line, system, ret, ipVersion, ttable);
                        ret.AddRule(rule);
                        break;

                    case '#':
                        break;

                    case 'C':
                        if (line == "COMMIT" && ttable == table)
                        {
                            if (ttable == null)
                            {
                                throw new IpTablesNetException("Parsing error");
                            }
                            return ret;
                        }
                        throw new IpTablesNetException("Unexepected table \"" + table + "\" found \"" + ttable + "\" instead");
                }
            }

            return null;
        }