Create all IP addresses in the IPv4 range

What would be an efficient way to create all possible v4 IP addresses? except repeating all bytes in one giant nested loop.

+3
source share
5 answers

Change . My previous answer would be passed on 128.0.0.0to 255.255.255.255up 0.0.0.0to 127.255.255.255. Presumably you want to go from 0.0.0.0to 255.255.255.255, so I edited my solution to do this.

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);

: , - ( , 1 1 -1 ), . , Integer.MAX_VALUE Integer.MIN_VALUE - .


. IP-, , , , :

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
}

: int long, ( int <= Integer.MAX_VALUE).

+10

IPv4 , , . . RFC : http://en.wikipedia.org/wiki/IPv4

, , , .

+3

? 0.0.0 255.255.255.255, 0 - 0xFFFFFFFF

+2

unsigned int/long (32- ), , , 0xffffffff.

, .

- , , .

+1

"", , , .

Pay attention to two things: 1. There are many addresses, so it will not be so effective. 2. Not all IP addresses are valid (and there are many addresses that you are probably not going to transfer).

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

+1
source

Source: https://habr.com/ru/post/1727580/


All Articles