Example #1
0
        /// <summary>
        /// Append extra options to an existing rule (via parsing)
        /// </summary>
        /// <param name="rule"></param>
        /// <param name="version"></param>
        /// <param name="chains"></param>
        /// <param name="createChain"></param>
        public void AppendToRule(String rule, int version = -1, IpTablesChainSet chains = null, bool createChain = false)
        {
            if (version == -1)
            {
                version = IpVersion;
            }

            Cow();
            string[] arguments = ArgumentHelper.SplitArguments(rule);
            int      count     = arguments.Length;

            try
            {
                var command = new IpTablesCommand(Chain.Name, Chain.Table, IpTablesCommandType.Add);
                command.Rule  = this;
                command.Table = Chain.Table;
                var parser = new CommandParser(arguments, command, chains);

                //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, version);
                    not = false;
                }

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

                        chain = parser.GetNewChain(_system, chain.IpVersion);
                    }

                    Chain = chain;
                }
            }
            catch (Exception ex)
            {
                throw new IpTablesParserException(rule, ex);
            }
        }
Example #2
0
        public IpTablesRuleSet(int ipVersion, IEnumerable <string> rules, IpTablesSystem system)
        {
            _system    = system;
            _ipVersion = ipVersion;
            _chains    = new IpTablesChainSet(ipVersion);

            foreach (string s in rules)
            {
                AddRule(s);
            }
        }
Example #3
0
        /// <summary>
        /// Parse a IPTables rule
        /// </summary>
        /// <param name="rule"></param>
        /// <param name="system"></param>
        /// <param name="chains"></param>
        /// <param name="version"></param>
        /// <param name="defaultTable"></param>
        /// <param name="createChain"></param>
        /// <returns></returns>
        public static IpTablesRule Parse(String rule, NetfilterSystem system, IpTablesChainSet chains,
                                         int version = 4, String defaultTable = "filter", ChainCreateMode createChain = ChainCreateMode.CreateNewChainIfNeeded)
        {
            Debug.Assert(chains.IpVersion == version);
            string[] arguments = ArgumentHelper.SplitArguments(rule);
            int      count     = arguments.Length;
            var      ipRule    = new IpTablesRule(system, new IpTablesChain(null, defaultTable, version, system));

            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, version);
                    not = false;
                }

                var chain = parser.GetChainFromSet();
                if (chain == null)
                {
                    if (createChain == ChainCreateMode.DontCreateErrorInstead)
                    {
                        throw new IpTablesNetException(String.Format("Unable to find chain: {0}", parser.ChainName));
                    }

                    var ipVersion = chains == null ? 4 : chains.IpVersion;
                    if (createChain == ChainCreateMode.ReturnNewChain)
                    {
                        chain = parser.GetNewChain(system, ipVersion);
                    }
                    else
                    {
                        chain = parser.CreateChain(system, ipVersion);
                    }
                }
                Debug.Assert(chain.IpVersion == version);
                ipRule.Chain = chain;
            }
            catch (Exception ex)
            {
                throw new IpTablesParserException(rule, ex);
            }

            return(ipRule);
        }
Example #4
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);
            }
        }
Example #5
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);
        }
Example #6
0
 public IpTablesRuleSet(int ipVersion, IpTablesSystem system)
 {
     _system    = system;
     _ipVersion = ipVersion;
     _chains    = new IpTablesChainSet(ipVersion);
 }