Sunday, November 11, 2007

IP Mask Merging

I'm back after a week vacation. I was drafted to do nothing for a week, apparently I do to much work.
The issue for today is: IP mask, or how to merge a few subnets into a big subnet.
Easy said, not as easily done. The function I had to make has simple idea, "we have a range of IPs (startIP to endIP) and we want a subnet for it."

The resulting algorithm, I had to devise it after a failed algorithm didn't do the job, is as follows:
1. check the startingIP min bits (i.e. 192.168.0.0 is 192.168.0.0/13)
2. if the endIP matches the region made by this bits - hooray, we're done! if not move to 3.
3. if the endIP is bellow the available range - increase the bits, move to 2 else move to 4.
4. add the current subnet, and start the whole process with the range (startIP + (2 power (32-bits)) to endIP

Sorry I can't post the source code for copyright reasons, we're using it after all...

Important notes:
If you're using an IP to number conversion make sure it's a 4 byte unsigned (uint32) or in the case of vb/vbs use double (or force the result to be double by using cdbl(...))
Another point of interest is the "check the startingIP min bits" it's a bit wise comparison of the IP, a co-worker of mine used the following method:
find the relevant byte (byte1.byte2.byte3.byte4/bits) "AND" it with 256-(2 power (bits mod 8)), it's needed especially if you can't use a uint32.

No comments: