给定IP地址和子网掩码,如何计算CIDR?

时间:2010-03-24 13:25:14

标签: c# .net ip-address subnet

好吧我似乎无法弄明白:给出以下内容:

IP address = 192.168.1.0
Subnetmask = 255.255.255.240

使用c#,如何计算CIDR表示法192.168.1.0/28?有没有一种简单的方法来实现这一目标?我错过了什么吗?

谢谢!

7 个答案:

答案 0 :(得分:6)

256 - 240 = 16 = 2**4, 32 - 4 = 28

这不是一个真正的C#问题。

要从IP和掩码中获取网络地址,您可以将字节and应用于IP和掩码。您可以使用IPAddress.Parse()IPAddress.GetAddressBytes()从字符串中获取字节。

答案 1 :(得分:3)

我必须做同样的事情,没有新的信息,但这个片段可能会派上用场,以便下一个人在C#中寻找一种方法。 请注意,此方法仅计算连续1的数量,并让您将其附加到IP。

public class IPAddressHelper
{
    public static UInt32 SubnetToCIDR(string subnetStr)
    {
        IPAddress subnetAddress = IPAddress.Parse(subnetStr);
        byte[] ipParts = subnetAddress.GetAddressBytes();
        UInt32 subnet = 16777216 * Convert.ToUInt32(ipParts[0]) + 65536 * Convert.ToUInt32(ipParts[1]) + 256 * Convert.ToUInt32(ipParts[2]) + Convert.ToUInt32(ipParts[3]);
        UInt32 mask = 0x80000000;
        UInt32 subnetConsecutiveOnes = 0;
        for (int i = 0; i < 32; i++)
        {
            if (!(mask & subnet).Equals(mask)) break;

            subnetConsecutiveOnes++;
            mask = mask >> 1;
        }
        return subnetConsecutiveOnes;
    }
}

答案 2 :(得分:2)

我没有它作为C#代码,但这是VB中的答案。不应该难以转换。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim someIP As Net.IPAddress = Net.IPAddress.Parse("192.168.1.10")
    Dim someMASK As Net.IPAddress = Net.IPAddress.Parse("255.255.255.240")

    Dim ipL As Long = IPtoLong(someIP)
    Dim maskL As Long = IPtoLong(someMASK)

    'Convert  Mask to CIDR(1-30)
    Dim oneBit As Long = &H80000000L
    Dim CIDR As Integer = 0

    For x As Integer = 31 To 0 Step -1
        If (maskL And oneBit) = oneBit Then CIDR += 1 Else Exit For
        oneBit = oneBit >> 1
    Next

    Dim answer As String = LongToIp(ipL And maskL) & " /" & CIDR.ToString

End Sub

Public Function IPtoLong(ByVal theIP As Net.IPAddress) As Long 'convert IP to number

    Dim IPb() As Byte = theIP.GetAddressBytes 'get the octets
    Dim addr As Long 'accumulator for address

    For x As Integer = 0 To 3
        addr = addr Or (CLng(IPb(x)) << (3 - x) * 8)
    Next
    Return addr

End Function

Public Function LongToIp(ByVal theIP As Long) As String 'convert number back to IP

    Dim IPb(3) As Byte '4 octets
    Dim addr As String = "" 'accumulator for address

    Dim mask8 As Long = MaskFromCidr(8) 'create eight bit mask

    For x = 0 To 3 'get the octets
        IPb(x) = CByte((theIP And mask8) >> ((3 - x) * 8))
        mask8 = mask8 >> 8
        addr &= IPb(x).ToString & "." 'add current octet to string
    Next
    Return addr.TrimEnd("."c)

End Function

Private Function MaskFromCidr(ByVal CIDR As Integer) As Long
    MaskFromCidr = CLng(2 ^ ((32 - CIDR)) - 1) Xor 4294967295L
End Function

答案 3 :(得分:1)

保持简单!

这仅适用于IPv4 ,但是由于IPv6仅支持CIDR(例如/64中的fe80::1ff:fe23:4567:890a/64),因此不需要像这样。

所有需要的IPv4网络掩码:

int cidr = Convert.ToString(mask.Address, 2).Count( o => o == '1'); 

基于给定示例的说明:

IPAddress mask = new IPAddress(new byte[] { 255, 255, 255, 240 });

// maskBinAsString = 11110000111101001111111111111111
string maskBinAsString = Convert.ToString(mask.Address, 2); 

// cidr = 28
int cidr = Convert.ToString(mask.Address, 2).Count( o=> o == '1'); 

答案 4 :(得分:0)

请参阅Get CIDR from netmask

用法:

var cidrnetmask = MaskToCIDR(IPAddress.Parse("255.0.0.0").GetAddressBytes());

这适用于IPv4。为了支持IPv6,可以扩展字节数,但希望没有人会尝试使用旧式网络掩码用于IPv6:o)

方法:

static int MaskToCIDR(byte[] bytes)
{

        var b0 = bytes[0];
        var b1 = bytes[1];
        var b2 = bytes[2];
        var b3 = bytes[3];

        return
            b3 != 0 ? (
                (b3 & 0x01) != 0 ? 32 :
                (b3 & 0x02) != 0 ? 31 :
                (b3 & 0x04) != 0 ? 30 :
                (b3 & 0x08) != 0 ? 29 :
                (b3 & 0x10) != 0 ? 28 :
                (b3 & 0x20) != 0 ? 27 :
                (b3 & 0x40) != 0 ? 26 :
                                   25 ) :
            b2 != 0 ? (
                (b2 & 0x01) != 0 ? 24 :
                (b2 & 0x02) != 0 ? 23 :
                (b2 & 0x04) != 0 ? 22 :
                (b2 & 0x08) != 0 ? 21 :
                (b2 & 0x10) != 0 ? 20 :
                (b2 & 0x20) != 0 ? 19 :
                (b2 & 0x40) != 0 ? 18 :
                                   17 ) :
            b1 != 0 ? (
                (b1 & 0x01) != 0 ? 16 :
                (b1 & 0x02) != 0 ? 15 :
                (b1 & 0x04) != 0 ? 14 :
                (b1 & 0x08) != 0 ? 13 :
                (b1 & 0x10) != 0 ? 12 :
                (b1 & 0x20) != 0 ? 11 :
                (b1 & 0x40) != 0 ? 10 :
                                   9  ) :
            b0 != 0 ? (
                (b0 & 0x01) != 0 ? 8 :
                (b0 & 0x02) != 0 ? 7 :
                (b0 & 0x04) != 0 ? 6 :
                (b0 & 0x08) != 0 ? 5 :
                (b0 & 0x10) != 0 ? 4 :
                (b0 & 0x20) != 0 ? 3 :
                (b0 & 0x40) != 0 ? 2 :
                                   1 ) :
                               0;
}

答案 5 :(得分:0)

我的解决方案,首先解析为IPAddress:

var Subnetmask = "255.255.255.240";
IPAddress ip = IPAddress.Parse(Subnetmask);

然后,检查掩码ip中的设置位数:

var intAddress = (int)IPAddress.Address;
Console.WriteLine(NumberOfSetBits(intAddress)); //28

该功能(来自https://stackoverflow.com/a/12175897/1271037):

int NumberOfSetBits(int i)
{
    i = i - ((i >> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
    return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}

答案 6 :(得分:0)

我知道已经有点晚了,但是我花了一些时间将上面的dbasnett's answer VB.NET代码转换为C#(感谢Telerik Code Converter)并将其放入.NET提琴{{3} },并在下面添加了我的转化

public static void Main()
{
    System.Net.IPAddress someIP = System.Net.IPAddress.Parse("192.168.1.23");
    System.Net.IPAddress someMASK = System.Net.IPAddress.Parse("255.255.255.128");

    long ipL = IPtoLong(someIP);
    long maskL = IPtoLong(someMASK);

    // Convert  Mask to CIDR(1-30)
    long oneBit = 0x80000000L;
    int CIDR = 0;

    for (int x = 31; x >= 0; x += -1)
    {
        if ((maskL & oneBit) == oneBit)
            CIDR += 1;
        else
            break;
        oneBit = oneBit >> 1;
    }

    string answer = LongToIp(ipL & maskL) + "/" + CIDR.ToString();

    Console.Out.WriteLine("woah woah we woah " + answer);
}

public static long IPtoLong(System.Net.IPAddress theIP) // convert IP to number
{
    byte[] IPb = theIP.GetAddressBytes(); // get the octets
    long addr = 0; // accumulator for address

    for (int x = 0; x <= 3; x++) {
        addr |=  (System.Convert.ToInt64(IPb[x]) << (3 - x) * 8);
    }
    return addr;
}

public static string LongToIp(long theIP) // convert number back to IP
{
    byte[] IPb = new byte[4]; // 4 octets
    string addr = ""; // accumulator for address

    long mask8 = MaskFromCidr(8); // create eight bit mask

    for (var x = 0; x <= 3; x++) // get the octets
    {
        IPb[x] = System.Convert.ToByte((theIP & mask8) >> ((3 - x) * 8));
        mask8 = mask8 >> 8;
        addr += IPb[x].ToString() + "."; // add current octet to string
    }
    return addr.TrimEnd('.');
}

private static long MaskFromCidr(int CIDR)
{
    return  System.Convert.ToInt64(Math.Pow(2, ((32 - CIDR))) - 1) ^ 4294967295L;
}