将子网掩码“/”表示法转换为Cisco 0.0.0.0标准

时间:2010-06-30 16:42:20

标签: .net mask iptables cisco subnet

我搜索了SO寻求帮助,但无法找到我的问题的答案。

情况:我需要将“/ NN”子网掩码表示法(想想IPTABLES)转换为0.0.0.0 cisco表示法。

NN是子掩码中“1”的数字,从最低八位字节到最高八位字节。每个八位字节都是8位整数。

可能的解决方案:

创建一个32“0”的数组并用“1”填充最后一个NN数字,然后分组为4个八位字节并转换为int ... a / 23 mask应该像0.0.1.255。

我的问题是如何在.NET中完成...我从未使用二进制操作和转换。

你能帮助我解决这个问题吗?

更新 - 斯蒂芬回答正确!

以下是移植到.NET的代码

        if (p.LastIndexOf("/") < 0 ) return p;
        int mask= Convert.ToInt32("0"+p.Substring(p.LastIndexOf("/")+1,2));

        int zeroBits = 32 - mask; // the number of zero bits
        uint result = uint.MaxValue; // all ones

        // Shift "cidr" and subtract one to create "cidr" one bits;
        //  then move them left the number of zero bits.
        result &= (uint)((((ulong)0x1 << mascara) - 1) << zeroBits);
        result = ~result;
        // Note that the result is in host order, so we'd have to convert
        //  like this before passing to an IPAddress constructor
        result = (uint)IPAddress.HostToNetworkOrder((int)result);
        string convertedMask = new IPAddress(result).ToString();

3 个答案:

答案 0 :(得分:5)

我一直想把一些通用的地址掩码程序放在一起......

这是一种从CIDR表示法转换为子网掩码的快捷方式:

var cidr = 23; // e.g., "/23"
var zeroBits = 32 - cidr; // the number of zero bits
var result = uint.MaxValue; // all ones

// Shift "cidr" and subtract one to create "cidr" one bits;
//  then move them left the number of zero bits.
result &= (uint)((((ulong)0x1 << cidr) - 1) << zeroBits);

// Note that the result is in host order, so we'd have to convert
//  like this before passing to an IPAddress constructor
result = (uint)IPAddress.HostToNetworkOrder((int)result);

答案 1 :(得分:1)

相同的吗?作为VB .Net中的Stephens

Function CIDRtoMask(ByVal CIDR As Integer) As String

    If CIDR < 2 OrElse CIDR > 30 Then
        Stop
    End If

    Debug.WriteLine(CIDR.ToString)

    'simulated ip address
    Dim ipAsNum As UInt32 = 3232300291 '192.168.253.3
    Debug.WriteLine(Convert.ToString(ipAsNum, 2).PadLeft(32, "0"c) & " IP as num") 'show binary


    'create mask
    Dim mask As UInt32 = UInt32.MaxValue << (32 - CIDR)
    Debug.WriteLine(Convert.ToString(mask, 2).PadLeft(32, "0"c) & " mask") 'show binary

    Dim CT As UInt32 = UInt32.MaxValue Xor mask 'the zero based count of hosts in network
    Dim NN As UInt32 = ipAsNum And mask 'network number
    Dim NB As UInt32 = NN Or CT 'network broadcast
    Debug.WriteLine(Convert.ToString(CT, 2).PadLeft(32, "0"c) & " CT") 'show binary
    Debug.WriteLine(Convert.ToString(NN, 2).PadLeft(32, "0"c) & " NN") 'show binary
    Debug.WriteLine(Convert.ToString(NB, 2).PadLeft(32, "0"c) & " NB") 'show binary

    'get bytes
    Dim tb() As Byte = BitConverter.GetBytes(mask)
    Array.Reverse(tb)

    'convert to string
    Dim stringMask As String = String.Format("{0}.{1}.{2}.{3}",
                                  tb(0), tb(1), tb(2), tb(3))

    Return stringMask
End Function

答案 2 :(得分:0)

我建议使用IPNetwork Library https://github.com/lduchosal/ipnetwork。 从版本2开始,它也支持IPv4和IPv6。

<强>的IPv4

IPNetwork ipnetwork = IPNetwork.Parse("192.168.0.1/25");

Console.WriteLine("Network : {0}", ipnetwork.Network);
Console.WriteLine("Netmask : {0}", ipnetwork.Netmask);
Console.WriteLine("Cidr : {0}", ipnetwork.Cidr);

输出

Network : 192.168.0.0
Netmask : 255.255.255.128
Cidr : 25

玩得开心!