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

发布于 2024-09-07 15:56:43 字数 1136 浏览 3 评论 0原文

我已经搜索过寻求帮助,但找不到我的问题的答案。

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

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

可能的解决方案:

制作一个包含 32 个“0”的数组,并用“1”填充最后 NN 个数字,然后分组为 4 个八位字节并转换为 int.../23 掩码应该类似于 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();

I've searched SO for help but could'nt find a answer to my question.

Situation: I need to convert a "/NN" subnet mask notation (think IPTABLES) to a 0.0.0.0 cisco notation.

NN are the number of "1" in the submask, from the lowest octet to the higher. Each octet are 8 bit integers.

Possible solution:

Make a array of 32 "0" and filling the last NN digits with "1", then group in 4 octets and converting to int... a /23 mask should be like 0.0.1.255.

My question is how to do it in .NET... i never used binary manipulation and conversion.

Can you guys help me with this solution?

UPDATE - Stephen has answer correctly!

Here is the code ported to .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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

后知后觉 2024-09-14 15:56:43

我一直想组合一些通用的地址屏蔽例程...

这是一种从 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);

I've been meaning to throw together some general-purpose address masking routines...

Here's a quick-and-dirty way to convert from CIDR notation to a subnet mask:

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);
勿忘初心 2024-09-14 15:56:43

相同的?就像 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

Same? as Stephens in VB .Net

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
嘴硬脾气大 2024-09-14 15:56:43

我建议使用 IPNetwork 库 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

玩得开心!

I would recommend the use of IPNetwork Library https://github.com/lduchosal/ipnetwork.
As of version 2, it supports IPv4 and IPv6 as well.

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);

Output

Network : 192.168.0.0
Netmask : 255.255.255.128
Cidr : 25

Have fun !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文