public void SyncChainRules(INetfilterAdapterClient client, IEnumerable <INetfilterRule> with, IEnumerable <INetfilterRule> currentRules)
        {
            var withCast         = with.Cast <T>();
            var currentRulesCast = currentRules.Cast <T>();

            SyncChainRules(client, withCast, currentRulesCast);
        }
Exemplo n.º 2
0
 public void AddRule(INetfilterAdapterClient client)
 {
     if (Chain == null)
     {
         throw new IpTablesNetException("Unknown Chain");
     }
     client.AddRule(this);
 }
        public void SyncChainRules(INetfilterAdapterClient client, IEnumerable <T> with, INetfilterChain <T> chain)
        {
            //Copy the rules
            var currentRules = new List <T>(chain.Rules);


            int i = 0, len = with.Count();

            bool shouldUpdate = currentRules.Count == len;

            foreach (T cR in currentRules)
            {
                //Delete any extra rules
                if (i == len)
                {
                    if (_shouldDelete(cR))
                    {
                        cR.DeleteRule(client);
                    }
                    continue;
                }

                //Get the rule for comparison
                T withRule = with.ElementAt(i);

                bool eq = _comparer.Equals(cR, withRule);
                if (eq)
                {
                    //No need to make any changes
                    i++;
                    continue;
                }

                //Debug:
                if (_ruleComparerForUpdate(cR, withRule) || shouldUpdate)
                {
                    //Replace this rule
                    cR.ReplaceRule(client, withRule);
                    i++;
                }
                else
                {
                    // Don't delete if this is non deletable
                    if (_shouldDelete(cR))
                    {
                        cR.DeleteRule(client);
                    }
                }
            }

            //Get rules to be added
            foreach (T rR in with.Skip(i))
            {
                var newRule = rR.ShallowClone();
                newRule.Chain = chain;
                newRule.AddRule(client);
            }
        }
Exemplo n.º 4
0
        public void Sync(INetfilterAdapterClient client, IEnumerable <IpTablesRule> with,
                         INetfilterSync <IpTablesRule> sync)
        {
            client.StartTransaction();

            SyncInternal(client, with, sync);

            client.EndTransactionCommit();
        }
Exemplo n.º 5
0
        public void ReplaceRule(INetfilterAdapterClient client, INetfilterRule with)
        {
            var withCast = with as IpTablesRule;

            if (withCast == null)
            {
                throw new IpTablesNetException("Comparing different Netfilter rule types, unsupported");
            }
            ReplaceRule(client, withCast);
        }
Exemplo n.º 6
0
        public INetfilterChain GetChain(INetfilterAdapterClient client, string table, string chain)
        {
            INetfilterChainSet tableRules = GetRules(client, table);

            if (tableRules == null)
            {
                throw new IpTablesNetException("Unable to get a chainset for table: " + table);
            }
            return(tableRules.GetChainOrDefault(chain, table));
        }
Exemplo n.º 7
0
        public void ReplaceRule(INetfilterAdapterClient client, IpTablesRule withRule)
        {
            if (Chain == null)
            {
                throw new IpTablesNetException("Unknown Chain");
            }
            int idx = Chain.Rules.IndexOf(this);

            client.ReplaceRule(withRule);
            Chain.Rules[idx] = withRule;
        }
Exemplo n.º 8
0
        public void TestSync <TSync>(INetfilterAdapterClient client, IpTablesRuleSet rulesOriginal, IpTablesRuleSet rulesNew, TSync sync, List <string> expectedCommands = null) where TSync : INetfilterSync <IpTablesRule>
        {
            IpTablesChain chain = rulesOriginal.Chains.First();

            chain.Sync(client, rulesNew.Chains.First().Rules, sync);

            if (expectedCommands != null)
            {
                CollectionAssert.AreEqual(expectedCommands, ExecutionLog.Select(a => a.Value).ToList());
            }
        }
Exemplo n.º 9
0
 public NetfilterSystem(ISystemFactory system, INetfilterAdapter tableAdapter, IpSetBinaryAdapter setAdapter = null)
 {
     _system        = system;
     _tableAdapter4 = tableAdapter == null ? null : tableAdapter.GetClient(this, 4);
     _tableAdapter6 = tableAdapter == null ? null : tableAdapter.GetClient(this, 6);
     if (setAdapter == null)
     {
         setAdapter = new IpSetBinaryAdapter(system);
     }
     _setAdapter = setAdapter;
 }
        public void TestSync(INetfilterAdapterClient client, IpTablesRuleSet rulesOriginal, IpTablesRuleSet rulesNew, Func<IpTablesRule, IpTablesRule, bool> commentComparer = null)
        {
            IpTablesChain chain = rulesOriginal.Chains.First();

            DefaultNetfilterSync<IpTablesRule> sync = new DefaultNetfilterSync<IpTablesRule>(commentComparer,null);

            if (commentComparer == null)
                chain.Sync(client, rulesNew.Chains.First().Rules, sync);
            else
                chain.Sync(client, rulesNew.Chains.First().Rules, sync);
        }
Exemplo n.º 11
0
        public void TestSync(INetfilterAdapterClient client, IpTablesRuleSet rulesOriginal, IpTablesRuleSet rulesNew, Func <IpTablesRule, IpTablesRule, bool> commentComparer = null)
        {
            IpTablesChain chain = rulesOriginal.Chains.First();

            DefaultNetfilterSync <IpTablesRule> sync = new DefaultNetfilterSync <IpTablesRule>(commentComparer, null);

            if (commentComparer == null)
            {
                chain.Sync(client, rulesNew.Chains.First().Rules, sync);
            }
            else
            {
                chain.Sync(client, rulesNew.Chains.First().Rules, sync);
            }
        }
Exemplo n.º 12
0
 public void DeleteRule(INetfilterAdapterClient client, bool usingPosition = true)
 {
     if (Chain == null)
     {
         throw new IpTablesNetException("Unknown Chain");
     }
     if (usingPosition)
     {
         var position = Chain.GetRulePosition(this);
         client.DeleteRule(Chain.Table, Chain.Name, position);
     }
     else
     {
         client.DeleteRule(this);
     }
     Chain.Rules.Remove(this);
 }
Exemplo n.º 13
0
        public IpTablesChain AddChain(INetfilterAdapterClient client, IpTablesChain chain, bool addRules = false)
        {
            client.AddChain(chain.Table, chain.Name);

            if (addRules)
            {
                foreach (IpTablesRule r in chain.Rules)
                {
                    r.AddRule();
                }
            }
            else
            {
                chain = new IpTablesChain(chain.Table, chain.Name, chain.IpVersion, chain.System);
            }

            return(chain);
        }
Exemplo n.º 14
0
        public void SyncChainRules(INetfilterAdapterClient client, IEnumerable <T> with, IEnumerable <T> currentRules)
        {
            //Copy the rules
            currentRules = new List <T>(currentRules.ToArray());

            int i = 0, len = with.Count();

            foreach (T cR in currentRules)
            {
                //Delete any extra rules
                if (i == len)
                {
                    if (_shouldDelete(cR))
                    {
                        cR.DeleteRule(client);
                    }
                    continue;
                }

                //Get the rule for comparison
                T withRule = with.ElementAt(i);

                bool eq;
                if (_debug)
                {
                    eq = cR.DebugEquals(withRule, true);
                }
                else
                {
                    eq = cR.Equals(withRule);
                }

                if (eq)
                {
                    //No need to make any changes
                    i++;
                }
                else
                {
                    //Debug:
                    if (_ruleComparerForUpdate(cR, withRule))
                    {
                        //Replace this rule
                        cR.ReplaceRule(client, withRule);
                        i++;
                    }
                    else
                    {
                        if (_shouldDelete(cR))
                        {
                            cR.DeleteRule(client);
                        }
                    }
                }
            }

            //Get rules to be added
            foreach (T rR in with.Skip(i))
            {
                rR.AddRule(client);
            }
        }
Exemplo n.º 15
0
 public void DeleteRule(INetfilterAdapterClient client, bool usingPosition = true)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 16
0
 public void DeleteChain(INetfilterAdapterClient client, string name, string table = "filter", int ipVersion = 4, bool flush = false)
 {
     client.DeleteChain(table, name, flush);
 }
Exemplo n.º 17
0
        public void TestSync(INetfilterAdapterClient client, IpTablesRuleSet rulesOriginal, IpTablesRuleSet rulesNew, List <string> expectedCommands, Func <IpTablesRule, IpTablesRule, bool> commentComparer = null)
        {
            TestSync(client, rulesOriginal, rulesNew, commentComparer);

            CollectionAssert.AreEqual(expectedCommands, ExecutionLog.Select(a => a.Value).ToList());
        }
Exemplo n.º 18
0
 internal void SyncInternal(INetfilterAdapterClient client, IEnumerable <IpTablesRule> with, INetfilterSync <IpTablesRule> sync)
 {
     sync.SyncChainRules(client, with, this);
 }
Exemplo n.º 19
0
        public IpTablesChain AddChain(INetfilterAdapterClient client, String name, String table = "filter", int ipVersion = 4)
        {
            client.AddChain(table, name);

            return(new IpTablesChain(table, name, ipVersion, this, new List <IpTablesRule>()));
        }
Exemplo n.º 20
0
 public void AddRule(INetfilterAdapterClient client)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 21
0
 public void ReplaceRule(INetfilterAdapterClient client, INetfilterRule with)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 22
0
 public INetfilterChainSet GetRules(INetfilterAdapterClient client, string table)
 {
     return(client.ListRules(table));
 }
Exemplo n.º 23
0
 public IEnumerable <INetfilterChain> GetChains(INetfilterAdapterClient client, string table)
 {
     return(GetRules(client, table).Chains);
 }
Exemplo n.º 24
0
 public INetfilterChainSet GetRules(INetfilterAdapterClient client, string table)
 {
     return client.ListRules(table);
 }
Exemplo n.º 25
0
        public IpTablesChain AddChain(INetfilterAdapterClient client, IpTablesChain chain, bool addRules = false)
        {
            client.AddChain(chain.Table, chain.Name);

            if (addRules)
            {
                foreach (IpTablesRule r in chain.Rules)
                {
                    r.AddRule();
                }
            }
            else
            {
                chain = new IpTablesChain(chain.Table,chain.Name, chain.IpVersion, chain.System);
            }

            return chain;
        }
Exemplo n.º 26
0
        public IpTablesChain AddChain(INetfilterAdapterClient client, String name, String table = "filter", int ipVersion = 4)
        {
            client.AddChain(table, name);

            return new IpTablesChain(table, name, ipVersion, this, new List<IpTablesRule>());
        }
Exemplo n.º 27
0
 public void AddRule(INetfilterAdapterClient client)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 28
0
 public new IEnumerable <IpTablesChain> GetChains(INetfilterAdapterClient client, String table, int ipVersion)
 {
     return(base.GetChains(client, table).Cast <IpTablesChain>());
 }
Exemplo n.º 29
0
 public void ReplaceRule(INetfilterAdapterClient client, INetfilterRule with)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 30
0
        public List <String> GetChainNames(INetfilterAdapterClient client, String table, int ipVersion)
        {
            var adapter = client as IIPTablesAdapterClient;

            return(adapter.GetChains(table));
        }
Exemplo n.º 31
0
 public void Delete(INetfilterAdapterClient client, bool flush = false)
 {
     _system.DeleteChain(client, _name, _table, _ipVersion, flush);
 }
Exemplo n.º 32
0
 public new IEnumerable<IpTablesChain> GetChains(INetfilterAdapterClient client, String table, int ipVersion)
 {
     return base.GetChains(client, table).Cast<IpTablesChain>();
 }
Exemplo n.º 33
0
 public void DeleteChain(INetfilterAdapterClient client, string name, string table = "filter", int ipVersion = 4, bool flush = false)
 {
     client.DeleteChain(table, name, flush);
 }
Exemplo n.º 34
0
 public List<String> GetChainNames(INetfilterAdapterClient client, String table, int ipVersion)
 {
     var adapter = client as IIPTablesAdapterClient;
     return adapter.GetChains(table);
 }
Exemplo n.º 35
0
 public IEnumerable<INetfilterRule> GetRules(INetfilterAdapterClient client, string table, string chain)
 {
     return GetChain(client, table, chain).Rules;
 }
        public void TestSync(INetfilterAdapterClient client, IpTablesRuleSet rulesOriginal, IpTablesRuleSet rulesNew, List<string> expectedCommands, Func<IpTablesRule, IpTablesRule, bool> commentComparer = null)
        {
            TestSync(client, rulesOriginal, rulesNew, commentComparer);

            CollectionAssert.AreEqual(expectedCommands, ExecutionLog.Select(a => a.Value).ToList());
        }
Exemplo n.º 37
0
 public IEnumerable <INetfilterRule> GetRules(INetfilterAdapterClient client, string table, string chain)
 {
     return(GetChain(client, table, chain).Rules);
 }
Exemplo n.º 38
0
 public IEnumerable<INetfilterChain> GetChains(INetfilterAdapterClient client, string table)
 {
     return GetRules(client, table).Chains;
 }
Exemplo n.º 39
0
 public INetfilterChain GetChain(INetfilterAdapterClient client, string table, string chain)
 {
     INetfilterChainSet tableRules = GetRules(client, table);
     if (tableRules == null)
     {
         throw new IpTablesNetException("Unable to get a chainset for table: "+table);
     }
     return tableRules.GetChainOrDefault(chain, table);
 }
Exemplo n.º 40
0
 public void DeleteRule(INetfilterAdapterClient client, bool usingPosition = true)
 {
     throw new NotImplementedException();
 }