Example #1
0
 public CommandParser(string[] arguments, IpTablesCommand ipCommand, IpTablesChainSet chains)
 {
     _arguments = arguments;
     _ipCommand = ipCommand;
     _parsers   = ModuleRegistry.PreloadDuplicateModules.ToList();
     _chains    = chains;
 }
        internal void TestApply(IpTablesRuleSet rulesOrig, IpTablesRuleSet rulesSynced, IpTablesRuleSet rulesNew, List <string> commands)
        {
            try
            {
                Assert.AreEqual(rulesNew, rulesSynced);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Sync:");
                DumpRuleset(rulesSynced);
                Console.WriteLine("New:");
                DumpRuleset(rulesNew);
                throw;
            }


            String currentTable = "filter";

            foreach (var cmd in commands)
            {
                if (cmd.StartsWith("*"))
                {
                    currentTable = cmd.Substring(1);
                    continue;
                }
                if (!cmd.StartsWith("-"))
                {
                    continue;
                }
                var ipCmd = IpTablesCommand.Parse(cmd, rulesOrig.System, rulesOrig.Chains, rulesOrig.IpVersion, currentTable);

                if (ipCmd.Rule != null)
                {
                    ipCmd.Rule.Chain = rulesOrig.Chains.GetChain(ipCmd.ChainName, ipCmd.Table);
                }

                rulesOrig.ApplyCommand(ipCmd);
            }

            try
            {
                Assert.AreEqual(rulesNew, rulesOrig);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Orig:");
                DumpRuleset(rulesOrig);
                Console.WriteLine("New:");
                DumpRuleset(rulesNew);
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// Consume arguments
        /// </summary>
        /// <param name="position">Rhe position to parse</param>
        /// <param name="not"></param>
        /// <param name="version"></param>
        /// <returns>number of arguments consumed</returns>
        public int FeedToSkip(int position, bool not, int version)
        {
            Position = position;
            String option = GetCurrentArg();

            if (option == "-m")
            {
                LoadParserModule(GetNextArg(), version);
                return(1);
            }
            if (option == "-A" || option == "-D" || option == "-R" || option == "-I")
            {
                _ipCommand.ChainName = GetNextArg();
                _ipCommand.Type      = IpTablesCommand.GetCommandType(option);
                if (option == "-D" || option == "-R" || option == "-I")
                {
                    var  nextArg = GetNextArg(2);
                    uint offset;
                    if (uint.TryParse(nextArg, out offset))
                    {
                        if (offset == 0)
                        {
                            throw new Exception("Invalid offset");
                        }
                        _ipCommand.Offset = ((int)offset - 1);
                        return(2);
                    }
                    else
                    {
                        _ipCommand.Offset = -1;
                    }
                }
                return(1);
            }
            if (option == "-t")
            {
                _ipCommand.Table = GetNextArg();
                return(1);
            }
            if (option == "-j")
            {
                LoadParserModule(GetNextArg(), version, true);
            }

            //All the preloaded modules are indexed here
            ModuleEntry mQuick;

            if (ModuleRegistry.PreloadOptions.TryGetValue(option, out mQuick))
            {
                IIpTablesModule module = _ipCommand.Rule.GetModuleForParseInternal(mQuick.Name, mQuick.Activator, version);
                return(module.Feed(this, not));
            }

            //Search each module, do it verbosely from the most recently added
            for (int index = _parsers.Count - 1; index >= 0; index--)
            {
                ModuleEntry m = _parsers[index];
                if (m.Options.Contains(option))
                {
                    IIpTablesModule module = _ipCommand.Rule.GetModuleForParseInternal(m.Name, m.Activator, version);
                    return(module.Feed(this, not));
                }
            }

            if (_polyfill != null)
            {
                IIpTablesModule module = _ipCommand.Rule.GetModuleForParseInternal(_polyfill.Value.Name, _polyfill.Value.Activator, version);
                return(module.Feed(this, not));
            }

            throw new IpTablesNetException("Unknown option: \"" + option + "\"");
        }