コード例 #1
0
        public void TestParseEntryIp()
        {
            var set = IpSetSet.Parse("test_set hash:ip family inet hashsize 10 maxelem 14", null);

            IpSetSets sets = new IpSetSets(null);

            sets.AddSet(set);


            String toParse = "test_set 1.2.3.4";
            var    entry   = IpSetEntry.Parse(toParse, sets);

            Assert.AreEqual("test_set", entry.Set.Name);
            Assert.AreEqual(IPAddress.Parse("1.2.3.4"), entry.Cidr.Address);
        }
コード例 #2
0
        /// <summary>
        /// Parse an entry for type
        /// </summary>
        /// <param name="entry"></param>
        /// <param name="value"></param>
        public static void ParseEntry(IpSetEntry entry, String value)
        {
            var typeComponents   = entry.Set.TypeComponents;
            var optionComponents = value.Split(new char[] { ',' });

            for (int i = 0; i < optionComponents.Length; i++)
            {
                switch (typeComponents[i])
                {
                case "ip":
                    if (entry.Cidr.Prefix == 0)
                    {
                        entry.Cidr = new IpCidr(IPAddress.Parse(optionComponents[i]));
                    }
                    else
                    {
                        entry.Cidr2 = new IpCidr(IPAddress.Parse(optionComponents[i]));
                    }
                    break;

                case "net":
                    entry.Cidr = IpCidr.Parse(optionComponents[i]);
                    var network = entry.Cidr.GetIPNetwork();
                    if (!Equals(network.Network, entry.Cidr.Address))
                    {
                        entry.Cidr = new IpCidr(network.Network, entry.Cidr.Prefix);
                    }
                    break;

                case "port":
                    var s = optionComponents[i].Split(':');
                    if (s.Length == 1)
                    {
                        entry.Port = ushort.Parse(s[0]);
                    }
                    else
                    {
                        entry.Protocol = s[0].ToLowerInvariant();
                        entry.Port     = ushort.Parse(s[1]);
                    }
                    break;

                case "mac":
                    entry.Mac = optionComponents[i];
                    break;
                }
            }
        }
コード例 #3
0
        public void TestParseEntry2()
        {
            var set = IpSetSet.Parse("test_set hash:ip,port family inet hashsize 10 maxelem 14", null);

            IpSetSets sets = new IpSetSets(null);

            sets.AddSet(set);


            String toParse = "test_set 8.8.8.8,tcp:80";
            var    entry   = IpSetEntry.Parse(toParse, sets);

            Assert.AreEqual("test_set", entry.Set.Name);
            Assert.AreEqual(IPAddress.Parse("8.8.8.8"), entry.Cidr.Address);
            Assert.AreEqual(80, entry.Port);
        }
コード例 #4
0
        public void DeleteEntry(IpSetEntry entry)
        {
            String command = entry.GetFullCommand("del");

            if (InTransaction)
            {
                _transactionCommands.Add(command);
            }
            else
            {
                var process = _system.StartProcess(BinaryName, command);
                process.WaitForExit();

                if (process.ExitCode != 0)
                {
                    throw new IpTablesNetException(String.Format("Failed to delete entry: {0}",
                                                                 process.StandardError.ReadToEnd()));
                }
            }
        }
コード例 #5
0
        public void DeleteEntry(IpSetEntry entry)
        {
            String command = entry.GetFullCommand("del");

            if (InTransaction)
            {
                _transactionCommands.Add(command);
            }
            else
            {
                using (var process = _system.StartProcess(BinaryName, command))
                {
                    String output, error;
                    ProcessHelper.ReadToEnd(process, out output, out error);

                    if (process.ExitCode != 0)
                    {
                        throw new IpTablesNetException(String.Format("Failed to delete entry: {0}", error));
                    }
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Parse an entry for type
        /// </summary>
        /// <param name="entry"></param>
        /// <param name="value"></param>
        public static void ParseEntry(IpSetEntry entry, String value)
        {
            var type = entry.Set.Type;
            var typeComponents = IpSetTypeHelper.TypeComponents(IpSetTypeHelper.TypeToString(type)).ToArray();
            var optionComponents = value.Split(new char[] { ',' });


            for (int i = 0; i < optionComponents.Length; i++)
            {
                switch (typeComponents[i])
                {
                    case "ip":
                    case "net":
                        entry.Cidr = IpCidr.Parse(optionComponents[i]);
                        var network = entry.Cidr.GetIPNetwork();
                        if (!Equals(network.Network, entry.Cidr.Address))
                        {
                            entry.Cidr = new IpCidr(network.Network, entry.Cidr.Prefix);
                        }
                        break;
                    case "port":
                        var s = optionComponents[i].Split(':');
                        if (s.Length == 1)
                        {
                            entry.Port = ushort.Parse(s[0]);
                        }
                        else
                        {
                            entry.Protocol = s[0].ToLowerInvariant();
                            entry.Port = ushort.Parse(s[1]);
                        }
                        break;
                    case "mac":
                        entry.Mac = optionComponents[i];
                        break;
                }
            }
        }
コード例 #7
0
 public IpSetEntryParser(string[] arguments, IpSetEntry entry, IpSetSets sets)
 {
     _arguments = arguments;
     _entry = entry;
     _sets = sets;
 }
コード例 #8
0
 public IpSetEntryParser(string[] arguments, IpSetEntry entry, IpSetSets sets)
 {
     _arguments = arguments;
     _entry     = entry;
     _sets      = sets;
 }
コード例 #9
0
ファイル: ModelLoad.cs プロジェクト: lcfcosta/YFW.Net
        private void CreateSets(IpTablesDetails config, RuleBuilder rb)
        {
            foreach (var set in config.Sets)
            {
                var      ipset    = new IpSetSet(IpSetTypeHelper.StringToType(set.Type), set.Name, 0, set.Family, _iptables, IpSetSyncMode.SetAndEntries);
                String[] resolved = set.Entries.ToArray();

                if (ipset.Type == IpSetType.HashIp)
                {
                    IPAddress ip;
                    int       retries = 0;
                    do
                    {
                        List <Task> tasks = new List <Task>();
                        for (int index = 0; index < resolved.Length; index++)
                        {
                            var entry = resolved[index];

                            String entryIp = rb.Format(entry);
                            if (!IPAddress.TryParse(entryIp, out ip))
                            {
                                DomainName domain;
                                if (!DomainName.TryParse(entryIp, out domain))
                                {
                                    throw new Exception("Unable to parse domain " + entryIp);
                                }
                                var asyncResult = _dns.ResolveAsync(domain).ContinueWith(CompleteLambda(index, resolved));
                                tasks.Add(asyncResult);
                            }
                        }

                        if (tasks.Any())
                        {
                            Task.WaitAll(tasks.ToArray());
                        }
                    } while (++retries <= 3 && resolved.Any((entry) => !IPAddress.TryParse(rb.Format(entry), out ip)));
                    for (int index = 0; index < resolved.Length; index++)
                    {
                        var entry = resolved[index];

                        String entryIp = rb.Format(entry);
                        if (!IPAddress.TryParse(entryIp, out ip))
                        {
                            throw new Exception("Unable to resolve " + entryIp);
                        }
                    }
                }

                //Check Uniqueness
                HashSet <IpSetEntry> ipsetEntries = new HashSet <IpSetEntry>();

                for (int index = 0; index < resolved.Length; index++)
                {
                    var    entry    = resolved[index];
                    String entryIp  = rb.Format(entry);
                    var    setEntry = IpSetEntry.ParseFromParts(ipset, entryIp);
                    if (ipsetEntries.Add(setEntry))
                    {
                        ipset.Entries.Add(setEntry);
                    }
                }
                _sets.AddSet(ipset);
            }

            //Add new sets (dont delete!)
            _sets.Sync((a) => false);
        }
コード例 #10
0
        public void DeleteEntry(IpSetEntry entry)
        {
            String command = entry.GetFullCommand("del");

            if (InTransaction)
            {
                _transactionCommands.Add(command);
            }
            else
            {
                var process = _system.StartProcess(BinaryName, command);
                process.WaitForExit();

                if (process.ExitCode != 0)
                {
                    throw new IpTablesNetException(String.Format("Failed to delete entry: {0}",
                        process.StandardError.ReadToEnd()));
                }
            }
        }
コード例 #11
0
        public void DeleteEntry(IpSetEntry entry)
        {
            String command = entry.GetFullCommand("del");

            if (InTransaction)
            {
                _transactionCommands.Add(command);
            }
            else
            {
                using (var process = _system.StartProcess(BinaryName, command))
                {
                    String output, error;
                    ProcessHelper.ReadToEnd(process, out output, out error);

                    if (process.ExitCode != 0)
                    {
                        throw new IpTablesNetException(String.Format("Failed to delete entry: {0}", error));
                    }
                }
            }
        }