Page 1 of 1

[SOLVED :P delayed actually] Help with generating ips

Posted: Sat Jun 14, 2003 9:41 am
by coditza
Hi

I have to generate some ips knowing the network and mask: eg 1.1.1.1/16

basicaly, the algorith is: in the network 1.1.1.1 you have 2^(32-16) ips, in general, in the network x.x.x.x you have 2^(32-mask) ips. including the network and broadkast.

So, what i do:

explode the input by "/" to get the network and the mask.
then get the nr of hosts/subnets (ips) with the formula:
no_ips = 2^(32-mask).

then add to the network 1,2,3... until i have no_ips ips.
I do that by transforming the network from a dottend notation to an integer with the formula: aaa*(256^3) + bbb*(256^2) + ccc*256 + ddd, where the network is: aaa.bbb.ccc.ddd.

After getting every ip, i must translate it back to doted notation to make some chekings.

That's all.

But, a biiiiiiig but:
for masks like /31,/30, /16 it works great (a little slower, but it works). EG: for a mask like /16 it takes 20 sec to generate all the ips.

From mask below /10, my script trow the error with maximum 30 sec execution time bla, so is not working.


I need a way to speed up this algorith or a better one.

10x in advance

coditza

Posted: Sun Jun 15, 2003 11:27 am
by nielsene
Well given that you want to explicitly list the IPs and not just test for membership I think you are going to be stuck with an O(2^n) algorithm. However, there's probably a lot of ways the constant factors can be reduced. Can you post your code and we can see if we can offer some suggestions.

Posted: Sun Jun 15, 2003 2:09 pm
by coditza
hi

I manage somehow to speed up that algorithm, by cuting off some uselles chekings. And, as that app should work for ip classes with 128 ip max, my problem is solved ... or delayed.
10x anyway

coditza