Esempio n. 1
0
        public static string GetcnIpRange(Dictionary <IPAddress, int> ipv4Subnets)
        {
            var sb = new StringBuilder("[\n{");

            uint startNum = 0;
            var  comma    = @"";

            foreach (var ipv4Subnet in ipv4Subnets)
            {
                var p = new IPv4Subnet(ipv4Subnet.Key, ipv4Subnet.Value);

                while (IPv4Subnet.IPv42UintBE(p.FirstIP) >> 24 > startNum)
                {
                    ++startNum;
                    sb.Append(@"},{");
                    comma = @"";
                }

                sb.Append(comma);
                sb.Append(@"0x");
                sb.Append(Convert.ToString(IPv4Subnet.IPv42UintBE(p.FirstIP) / 256, 16));
                sb.Append(@":");
                sb.Append(ipv4Subnet.Value / 256);
                comma = @",";
            }
            sb.Append("}\n];");
            return(sb.ToString());
        }
Esempio n. 2
0
        public static string GetcnIp16Range(Dictionary <IPAddress, int> ipv4Subnets)
        {
            var sb = new StringBuilder("{\n");

            var masterNetSet = new SortedSet <uint>();

            foreach (var ipv4Subnet in ipv4Subnets)
            {
                var p = new IPv4Subnet(ipv4Subnet.Key, ipv4Subnet.Value);
                if (ipv4Subnet.Value < 1 << 14)
                {
                    masterNetSet.Add(IPv4Subnet.IPv42UintBE(p.FirstIP) >> 14);
                }
            }
            var masterNet = new List <uint>(masterNetSet.Count);

            foreach (var x in masterNetSet)
            {
                masterNet.Add(x);
            }
            //masterNet.Sort();
            foreach (var x in masterNet)
            {
                sb.Append(@"0x");
                sb.Append(Convert.ToString(x, 16));
                sb.Append(@":1,");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append("\n};");
            return(sb.ToString());
        }
Esempio n. 3
0
        private static KeyValuePair <IPAddress, int>?GetCNIPv4InfoFromLine(string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return(null);
            }

            var reg   = new Regex("^(.+)/(.+)$");
            var match = reg.Match(str);

            if (match.Groups.Count == 3)
            {
                var ipv4  = IPAddress.Parse(match.Groups[1].Value);
                var hosts = IPv4Subnet.CIDR2Hosts(Convert.ToInt32(match.Groups[2].Value));
                return(new KeyValuePair <IPAddress, int>(ipv4, hosts));
            }

            return(null);
        }