views:

422

answers:

3

Ok I can't seem to figure this out: given the following:

IP address = 192.168.1.0
Subnetmask = 255.255.255.240

Using c#, how do I calculate the CIDR notation 192.168.1.0/28 ? Is there an easy way to achieve this? Am I missing something?

Thanks!

+5  A: 

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

It is not really a C# question.

To get a net address from an IP and mask you can apply bytewise and to the IP and mask. You can get bytes from a string using IPAddress.Parse() and IPAddress.GetAddressBytes().

wRAR
Thanks for the info. I know I can manipulate string to ip address and bytes with the IPAddress class. I also understand the logic behind the subnet/broadcast/cidr calculations etc... I'm just having trouble putting this into a simple c# method which gives me `26` when I input `255.255.255.192`
Roel
C# has bit manipulation operators.
wRAR
I know. That was not the question. Nevermind I figured it out.
Roel
+1  A: 

I don't have it as C# code but here is the answer in VB. Should not be to hard to convert.

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
dbasnett
A: 

I had to do the same thing, no new info but this snippet may come in handy for the next person looking for a way to do this in C#. note that this method only counts the number of consecutive 1s, and leaves you the work of appending it to the 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;
    }
}
Yonatan Karni