views:

218

answers:

2

Hi there,

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();
+2  A: 

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);
Stephen Cleary
Thanks for answering :DI'll try that and post the feedback, but before that i have a question. The result will be the "255.255" way or the "0.0.1.255" like the cisco?Regards!
jaderanderson
This converts to a standard "255.255.128.0" sort of subnet mask. If you need a "wildcard" mask, then you can take the bitwise not (`~`) of the result before calling `HostToNetworkOrder`, and that will give you a result like "0.0.1.255".
Stephen Cleary
Thanks man, will update the code with the answer! It works!
jaderanderson
A: 

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
dbasnett