public static void ExecuteIptables(NetfilterSystem system, String command, String iptablesBinary, out String output, out String error)
        {
            using (ISystemProcess process = system.System.StartProcess(iptablesBinary, command))
            {
                ProcessHelper.ReadToEnd(process, out output, out error);

                //OK
                if (process.ExitCode == 0)
                    return;

                //ERR: INVALID COMMAND LINE
                if (process.ExitCode == 2)
                {
                    throw new IpTablesNetException("IPTables execution failed: Invalid Command Line - " + command);
                }

                //ERR: GENERAL ERROR
                if (process.ExitCode == 1)
                {
                    throw new IpTablesNetException("IPTables execution failed: Error - " + command);
                }

                //ERR: UNKNOWN
                throw new IpTablesNetException("IPTables execution failed: Unknown Error - " + command);
            }
        }
        private T AddChain(string chainName, string tableName, NetfilterSystem system)
        {
            var chain = CreateChain(tableName, chainName, system);

            AddChain(chain);
            return(chain);
        }
 public IPTablesRestoreAdapterClient(int ipVersion, NetfilterSystem system, String iptablesRestoreBinary = "iptables-restore", String iptableSaveBinary = "iptables-save", String iptablesBinary = "iptables")
 {
     _system = system;
     _iptablesRestoreBinary = iptablesRestoreBinary;
     _iptablesSaveBinary = iptableSaveBinary;
     _iptablesBinary = iptablesBinary;
     _ipVersion = ipVersion;
 }
        public T GetChainOrAdd(string chainName, string tableName, NetfilterSystem system)
        {
            T chain = GetChainOrDefault(chainName, tableName);

            if (chain != null)
            {
                return(chain);
            }

            return(AddChain(chainName, tableName, system));
        }
        public static ISystemProcess ExecuteIptables(NetfilterSystem system, String command, String iptablesBinary)
        {
            ISystemProcess process = system.System.StartProcess(iptablesBinary, command);
            process.WaitForExit();

            //OK
            if (process.ExitCode == 0)
                return process;

            //ERR: INVALID COMMAND LINE
            if (process.ExitCode == 2)
            {
                throw new IpTablesNetException("IPTables execution failed: Invalid Command Line - "+command);
            }

            //ERR: GENERAL ERROR
            if (process.ExitCode == 1)
            {
                throw new IpTablesNetException("IPTables execution failed: Error - " + command);
            }

            //ERR: UNKNOWN
            throw new IpTablesNetException("IPTables execution failed: Unknown Error - " + command);
        }
 protected abstract T CreateChain(String tableName, String chainName, NetfilterSystem system);
 public IPTablesBinaryAdapterClient(int ipVersion, NetfilterSystem system, String iptablesBinary)
 {
     _system = system;
     _iptablesBinary = iptablesBinary;
     _ipVersion = ipVersion;
 }
Beispiel #8
0
 public NfTablesChain(string tableName, string chainName, NetfilterSystem netfilterSystem)
 {
     throw new NotImplementedException();
 }
 public MockIpTablesRestoreAdapterClient(NetfilterSystem system, string iptablesRestoreBinary = "iptables-restore") : base(4, system, iptablesRestoreBinary)
 {
 }
Beispiel #10
0
 public IpTablesChain CreateNewChain(NetfilterSystem system, int ipVersion)
 {
     return new IpTablesChain(_tableName, _chainName, ipVersion, system);
 }
Beispiel #11
0
 public IpTablesChain GetChain(NetfilterSystem system)
 {
     return _chains.GetChainOrAdd(_chainName, _tableName, system);
 }
 INetfilterAdapterClient INetfilterAdapter.GetClient(NetfilterSystem system, int ipVersion = 4)
 {
     return GetClient(system as IpTablesSystem, ipVersion);
 }
        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;
        }
Beispiel #14
0
 public IpTablesChain CreateChain(NetfilterSystem system, int ipVersion)
 {
     var chain = GetNewChain(system, ipVersion);
     _chains.AddChain(chain);
     return chain;
 }
 public static void ExecuteIptables(NetfilterSystem system, String command, String iptablesBinary)
 {
     String output, error;
     ExecuteIptables(system, command, iptablesBinary, out output, out error);
 }