简体   繁体   English

生成IPv4范围中的所有IP地址

[英]Generating all IP addresses in the IPv4 Range

What would be a efficient way to generate all possible IP v4 addresses? 什么是生成所有可能的IP v4地址的有效方法? other than iterating all bytes in a one giant nested for loop. 除了迭代一个巨大的嵌套for循环中的所有字节。

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 . 编辑 :我以前的答案将从128.0.0.0变为255.255.255.2550.0.0.0127.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. 大概你想要从0.0.0.0255.255.255.255 ,所以我编辑了我的解决方案来做到这一点。

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 (ie how adding 1 to -1 enough times makes it -1 again), read up on two's complement . 注意:如果你对这个循环将如何结束感到困惑(即如何将足够的时间加1到-1再次使其为-1),请阅读2的补码 Basically, adding one to Integer.MAX_VALUE results in Integer.MIN_VALUE , and does not throw any kind of exception. 基本上,向Integer.MAX_VALUE添加一个会导致Integer.MIN_VALUE ,并且不会抛出任何类型的异常。


Old answer . 老答案 Still hits all IPs, but probably not in the order you desire: 仍然击中所有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
}

Please note: If the loop control variable was an int instead of a long , this would be an infinite loop (since all int s are always <= Integer.MAX_VALUE ). 请注意:如果循环控制变量是int而不是long ,则这将是一个无限循环(因为所有int都始终<= Integer.MAX_VALUE )。

Not all IPv4 addresses are valid, depending on what purpose they're intended for. 并非所有IPv4地址都有效,具体取决于它们的用途。 See the section on reserved address blocks and the linked RFCs here: http://en.wikipedia.org/wiki/IPv4 请参阅此处有关保留地址块和链接RFC的部分: 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. 因此,根据您要执行的操作,您可能需要检查保留的地址并将其保留。

All Possible? 一切可能? 0.0.0.0 to 255.255.255.255 which is 0 to 0xFFFFFFFF 0.0.0.0到255.255.255.255,它是0到0xFFFFFFFF

You can start with an unsigned int/long (32-bit data type) initialized to zero and keep incrementing until you reach 0xffffffff. 您可以从初始化为零的无符号整数/长整数(32位数据类型)开始,并继续递增,直到达到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. 使用位掩码和位移运算符来提取您感兴趣的任何给定字节。

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. 请注意两件事:1。有很多地址,因此效率不高。 2. Not all IP addresses are valid (and there are plenty of addresses which you probably don't intend to go over). 2.并非所有IP地址都有效(并且有很多地址可能不打算过去)。

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.xxx are invalid, etc. 有关IP地址有效的示例,请注意224.0.0.0和239.255.255.255之间的所有地址都是多播地址,所有以127.xxx开头的地址都是无效的,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM