将CIDR表示法转换为IP范围 [英] Convert CIDR notation into IP range

查看:738
本文介绍了将CIDR表示法转换为IP范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用GeoLite2数据库来实现 IP->国家/地区查找.出于性能原因,我们要导入CSV并将其转换为我们自己的格式.

We are using the GeoLite2 database in order to implement an IP -> country lookup. For performance reasons, we want to import the CSV and convert it to our own format.

CSV表示如下:

5.39.40.96/27,3017382,3017382,,0,0
5.39.40.128/28,3017382,3017382,,0,0
5.39.40.144/28,2635167,3017382,,0,0
5.39.40.160/27,3017382,3017382,,0,0
5.39.40.192/26,3017382,3017382,,0,0
5.39.41.0/25,3017382,3017382,,0,0
5.39.41.128/26,3017382,3017382,,0,0
5.39.41.192/26,2635167,3017382,,0,0
5.39.42.0/24,3017382,3017382,,0,0
5.39.43.0/25,3017382,3017382,,0,0

因此,我们需要将CIDR表示法(示例:5.39.40.96/27 )转换为IP地址范围. (从IP-到IP)

So we need to convert the CIDR notation (example: 5.39.40.96/27) into an IP address range. (From IP - To IP)

如何在C#中完成?

注意:这不是

Note: This is not a duplicate of this question, since I'm asking about a C# implementation and not Java.

推荐答案

这里是处理它的一种方法,无需使用任何库函数即可清楚说明正在发生的事情,并在以后有人需要使用其他语言来实现时提供帮助.

Here is one way to handle it, without using any library functions to make it clear what's happening and to help if someone needs to implement it in other languages later on.

代码首先将CIDR转换为32位数字,然后创建掩码以确定起始地址,使用掩码的反数确定结束地址,然后转换回CIDR格式.

The code first converts the CIDR into a 32bit number, then creates the mask to determine the start address, uses the inverse of the mask to determine the end address and then converts back to CIDR format.

请注意,由于没有错误检测,因此输入的格式必须为a.b.c.d/m.

Note that there is no error detection so the input must be in the a.b.c.d/m format.

IP地址的转换只是使用位移将大尾数形式(AABBCCDD)的四个八位字节进行简单的串联.

The conversion of the IP address is just a simple concatenation of the four octets in big endian form (AABBCCDD) using bit shifts.

掩码告诉您最高有效位中有多少位是固定的,这意味着32是单个IP​​范围,0将是整个IP范围.因此,我们可以将所有位都置为掩码,并用32-maskbits左移以确定实际的掩码.

The mask tells how many bits from the most significant bit are fixed, meaning 32 is a single IP range and 0 would be the whole IP range. Thus we can take a mask with all bits set and shift it left with 32-maskbits to determine the actual mask.

如果将maskbits位设置为零,则得到范围的开始,因此我们将IP与掩码位进行与"运算.如果将这些位设置为1,则得到范围的结尾,因此我们将对掩码的取反位进行或"运算.

If we set the maskbits bits to zero, we get the beginning of the range, so we AND the IP with maskbits. If we set the bits to one, we get the end of the range, so we will OR with the negated bits of mask.

再次以CIDR格式打印IP地址很简单:只需将32位值拆分为八位字节,然后将它们用点分隔开即可.

Printing the IP address in CIDR format is again simple: just split the 32bit value into octets and write them separated with dots.

using System;

namespace CSTests
{
    class Program
    {
        static string toip(uint ip)
        {
            return String.Format("{0}.{1}.{2}.{3}", ip >> 24, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
        }

        static void Main(string[] args)
        {
            string IP = "5.39.40.96/27";
            string[] parts = IP.Split('.', '/');

            uint ipnum = (Convert.ToUInt32(parts[0]) << 24) |
                (Convert.ToUInt32(parts[1]) << 16) |
                (Convert.ToUInt32(parts[2]) << 8) |
                Convert.ToUInt32(parts[3]);

            int maskbits = Convert.ToInt32(parts[4]);
            uint mask = 0xffffffff;
            mask <<= (32 - maskbits);

            uint ipstart = ipnum & mask;
            uint ipend = ipnum | (mask ^ 0xffffffff);

            Console.WriteLine(toip(ipstart) + " - " + toip(ipend));
        }
    }
}

输出:

5.39.40.96-5.39.40.127

5.39.40.96 - 5.39.40.127

这篇关于将CIDR表示法转换为IP范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆