CIDR validation W/ C#

  • Can anyone point me to some resources or examples or explain to me how to do ip address validation using CIDR as the basis.

    I understand the basics of what CIDR is and using an online calculator I figured out that for example a CIDR of "192.168.0.0/24" would mean that an ip in the range of 192.168.0.0 to 192.168.0.255 is valid.

    So i'm loooking to impliment a function where i can pass in a cidr and ip address and have it return a bool if it's valid for that cidr or not.  I'm using C# but any examples or instructions in any language will help.

    Thanks!

  • Hi,

    Were you able to get any information on how to calculate CIDR notations? I am trying to do the same using VB6. Can you help me in any way? My email address: boyinapalli@hotmail.com

    Thanks.

    Bhagavan

  • yes over on the MSDN forums someone gave some code although it is very specific to c# and .net

    private bool IsInSubnet (string ipAddress, string cidr) {

    string [] parts = cidr.Split ('/');

    int baseAddress = BitConverter.ToInt32 (IPAddress.Parse (parts [0]).GetAddressBytes (), 0);

    int address = BitConverter.ToInt32 (IPAddress.Parse (ipAddress).GetAddressBytes (), 0);

    int mask = IPAddress.HostToNetworkOrder (-1 << (32 - int.Parse (parts [1])));

    return ((baseAddress & mask) == (address & mask));

    }

    you can read the whole post here: https://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1955891&SiteID=1

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply