Ejemplo n.º 1
0
        /// <summary>
        /// Append extra options to an existing rule (via parsing)
        /// </summary>
        /// <param name="rule"></param>
        /// <param name="chains"></param>
        /// <param name="createChain"></param>
        public void AppendToRule(String rule, IpTablesChainSet chains = null, bool createChain = false)
        {
            string[] arguments = ArgumentHelper.SplitArguments(rule);
            int      count     = arguments.Length;

            try
            {
                var parser = new RuleParser(arguments, this, chains, Chain.Table);

                //Parse the extra options
                bool not = false;
                for (int i = 0; i < count; i++)
                {
                    if (arguments[i] == "!")
                    {
                        not = true;
                        continue;
                    }
                    i  += parser.FeedToSkip(i, not);
                    not = false;
                }

                //Only replace the chain if a new one has been supplied
                if (parser.GetChainName() != null)
                {
                    var chain = parser.GetChain(_system);
                    if (chain == null)
                    {
                        if (!createChain)
                        {
                            throw new IpTablesNetException(String.Format("Unable to find chain: {0}", parser.ChainName));
                        }
                        chain = parser.CreateNewChain(_system, chain.IpVersion);
                    }

                    Chain = chain;
                }
            }
            catch (Exception ex)
            {
                throw new IpTablesParserException(rule, ex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parse a IPTables rule
        /// </summary>
        /// <param name="rule"></param>
        /// <param name="system"></param>
        /// <param name="chains"></param>
        /// <param name="defaultTable"></param>
        /// <param name="createChain"></param>
        /// <param name="ipVersion"></param>
        /// <returns></returns>
        public static IpTablesRule Parse(String rule, NetfilterSystem system, IpTablesChainSet chains,
                                         String defaultTable = "filter", bool createChain = false)
        {
            string[] arguments = ArgumentHelper.SplitArguments(rule);
            int      count     = arguments.Length;
            var      ipRule    = new IpTablesRule(system, null);

            try
            {
                var parser = new RuleParser(arguments, ipRule, chains, defaultTable);

                bool not = false;
                for (int i = 0; i < count; i++)
                {
                    if (arguments[i] == "!")
                    {
                        not = true;
                        continue;
                    }
                    i  += parser.FeedToSkip(i, not);
                    not = false;
                }

                var chain = parser.GetChain(system);
                if (chain == null)
                {
                    if (!createChain)
                    {
                        throw new IpTablesNetException(String.Format("Unable to find chain: {0}", parser.ChainName));
                    }
                    chain = parser.CreateNewChain(system, chains == null ? 4 : chains.IpVersion);
                }
                ipRule.Chain = chain;
            }
            catch (Exception ex)
            {
                throw new IpTablesParserException(rule, ex);
            }

            return(ipRule);
        }