views:

252

answers:

5

What would be a efficient way to generate all possible IP v4 addresses? other than iterating all bytes in a one giant nested for loop.

+6  A: 

Edit: My previous answer would have gone from 128.0.0.0 to 255.255.255.255 to 0.0.0.0 to 127.255.255.255. Presumably you want to go from 0.0.0.0 to 255.255.255.255, so I've edited my solution to do that.

int i = -1;
do {
  i++;

  int b1 = (i >> 24) & 0xff;
  int b2 = (i >> 16) & 0xff;
  int b3 = (i >>  8) & 0xff;
  int b4 = (i      ) & 0xff;

  //Now the IP is b1.b2.b3.b4

} while(i != -1);

Note: if you're confused how this loop will ever end (i.e. how adding 1 to -1 enough times makes it -1 again), read up on two's complement. Basically, adding one to Integer.MAX_VALUE results in Integer.MIN_VALUE, and does not throw any kind of exception.


Old answer. Still hits all IPs, but probably not in the order you desire:

for(long n = Integer.MIN_VALUE; n <= Integer.MAX_VALUE; n++)
{
  int i = (int)n;

  int b1 = (i >> 24) & 0xff;
  int b2 = (i >> 16) & 0xff;
  int b3 = (i >>  8) & 0xff;
  int b4 = (i      ) & 0xff;

  //Now the IP is b1.b2.b3.b4
}

Please note: If the loop control variable was an int instead of a long, this would be an infinite loop (since all ints are always <= Integer.MAX_VALUE).

Kip
Is Integer unsigned in java?
S.Mark
Kip
@S.Mark: i'm not sure if this is what you were getting at, but he probably wanted to go from 0 to 0xffffffff. i've updated my answer to do that
Kip
I see, thanks, I've got your point too.
S.Mark
+2  A: 

All Possible? 0.0.0.0 to 255.255.255.255 which is 0 to 0xFFFFFFFF

S.Mark
Yes all possible valid IP addresses from beginning to end.
Hamza Yerlikaya
I see, May I know the purpose of doing that? for checking valid IP?
S.Mark
trying to replicate http://www.isi.edu/ant/address/index.html
Hamza Yerlikaya
Ah, I see, to do a Assigned IP address map, you will need some of data resources which I dont have in my ref links for now.
S.Mark
+1  A: 

You can start with an unsigned int/long (32-bit data type) initialized to zero and keep incrementing until you reach 0xffffffff.

The increment operator is usually slightly more efficient than nested loops.

Use bit masks and bit shift operators to pull out any given byte that you are interested in.

Eric J.
+3  A: 

Not all IPv4 addresses are valid, depending on what purpose they're intended for. See the section on reserved address blocks and the linked RFCs here: http://en.wikipedia.org/wiki/IPv4

So depending on what you want to do, you might need to check for reserved addresses and leave them out.

Andrew McGregor
+1  A: 

In terms of "efficiency", I don't think there is a much better way than looping through all possible values.

Do take note of two things: 1. There are a lot of addresses, so it won't be that efficient. 2. Not all IP addresses are valid (and there are plenty of addresses which you probably don't intend to go over).

For an example of what IP addresses are valid, take note that all addresses between 224.0.0.0 and 239.255.255.255 are multicast addresses, all addresses starting with 127.x.x.x are invalid, etc.

Edan Maor