Complex Math - IP Block Splitting Help

  • Thread starter Thread starter lucius
  • Start date Start date
L

lucius

I have a given IP address that represents a "block". I would like to
split this block range into smaller blocks of "/24" CIDR size. What is
the best way to do this?
An example is 66.179.96.0 / 20.

Thanks.
 
Hello Lucius,

This seems to be a pure ip calculation question ;)

So far what I can get is using the bit operation(such as bit shift) to
calculate subnet addresses based on the base network address e.g.

static void RunIPTest()
{
int imask = 20;
int isubmask = 24;
IPAddress ip = IPAddress.Parse("66.179.96.0");

byte[] ipbytes = ip.GetAddressBytes();

//reverse the bytes due to little endian
Array.Reverse(ipbytes);

int baseIP = BitConverter.ToInt32(ipbytes, 0);

int ideltamask = isubmask - imask;
int isubnets = (int)Math.Pow((double)2, (double)ideltamask);

int ioffset = 32 - isubmask;

Console.WriteLine("offset: {0}", 1 << ioffset);

int tempIP;
byte[] tempbytes;
for (int i = 1; i < isubnets; i++)
{
tempIP = baseIP + (i << ioffset);

tempbytes = BitConverter.GetBytes(tempIP);

Array.Reverse(tempbytes);


//Console.WriteLine("tempbytes: {0}.{1}.{2}.{3}----int:{4}
", tempbytes[0], tempbytes[1], tempbytes[2], tempbytes[3], tempIP);

IPAddress subip = new IPAddress(tempbytes);

Console.WriteLine("subnet ipaddress: {0}", subip);
}


}


Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thank you very much. That is much more than I needed, you anticipated
my follow-up question after I got a count. I originally needed help
just with the count, and I had this :

int nets=0;
int preval = child - parent;
nets = Convert.ToInt32( Math.Pow( ( preval ) , 2 ) );
if ( nets <= 1 )
{
nets++;
}

I just wanted a simple binary split ( a 20 splits into two 21s that
each split into two 22s that each split into two 23s that each split
into two 24s).

But my next step/question would have been about the actual address
calculation.

Thanks!
 
For dividing a /20 address to /24, I'll try converting the base address to
32-bit integer, then adds the address by 256, loop by 2^4=16 times.

66.179.96.0 = 42B36000h
Loop 16 times:
1) 42B36000h = 66.179.96.0
2) 42B36100h = 66.179.96.0
..
..
..
16) 42B36F00h = 66.179.111.0

Simple binary arithmatics. :) Now you just need to convert above into
code...

P.S.: Each "." seperated part can be changed to a byte-length HEX value.
Removing the "."s you can converting in 32-bit integer.
 
Thanks for your reply Lucius,

I'm glad that the information is of assistance. Actually I originally try
using the BitArray class since I've expected that the main point here is
bit operation. However, after some test I found BitArray is not good enough
here as I've expected(especially when I want to calculate the detailed
subnet address ...) :). So I switch to use the bit operator (such as bit
shift) directly.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top