简体   繁体   English

in_addr_t,从一个IP跳到下一个IP

[英]in_addr_t, jump from one IP to the next

My program aims to browse a range of IPs, delimited by a start IP, and an end IP. 我的程序旨在浏览由起始IP和结束IP分隔的一系列IP。 Here's an example on a local wireless network : 这是本地无线网络上的示例:

MyIP & Netmask = 192.168.1.0
MyIP | ~Netmask = 192.168.1.255

My program must be able to iterate over all available IPs, from 192.168.1.1 to 192.168.1.254, but I cannot find a constant or "proper" way to get the distance between an IP and another. 我的程序必须能够遍历从192.168.1.1到192.168.1.254的所有可用IP,但是我找不到一种恒定或“正确”的方法来获取IP与另一个IP之间的距离。 Here's a sample program I used for testing : 这是我用于测试的示例程序:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char** argv)
{
      struct sockaddr_in addr1, addr2, addr3, addr4, addr5;

      addr1.sin_addr.s_addr = inet_addr("192.168.1.1");
      addr2.sin_addr.s_addr = inet_addr("192.168.1.2");
      addr3.sin_addr.s_addr = inet_addr("192.168.1.3");
      addr4.sin_addr.s_addr = inet_addr("192.168.1.255");
      addr5.sin_addr.s_addr = inet_addr("192.168.2.1");

      fprintf(stdout, "addr2 - addr1 = %u\n", addr2.sin_addr.s_addr - addr1.sin_addr.s_addr);
      fprintf(stdout, "addr3 - addr2 = %u\n", addr3.sin_addr.s_addr - addr2.sin_addr.s_addr);
      fprintf(stdout, "addr4 - addr3 = %u\n", addr4.sin_addr.s_addr - addr3.sin_addr.s_addr);
      fprintf(stdout, "addr5 - addr4 = %u\n", addr5.sin_addr.s_addr - addr4.sin_addr.s_addr);

      return EXIT_SUCCESS;
}

I get the following output : 我得到以下输出:

addr2 - addr1 = 16777216
addr3 - addr2 = 16777216
addr4 - addr3 = 4227858432
addr5 - addr4 = 33619968

Now, I understand why the distance between 192.168.1.1 and 192.168.1.2 is 16777216 (2^24). 现在,我了解了为什么192.168.1.1和192.168.1.2之间的距离为16777216(2 ^ 24)。 Now, is there any way I could get the above values "properly", using predefined constants if possible (manipulating IP strings in dot format isn't really a solution) ? 现在,有什么办法可以(如果可以的话)使用预定义的常量“适当地”获得上述值(以点格式操作IP字符串实际上不是解决方案)?

  • What do I need to add to the in_addr_t value to jump from 192.168.1.1 to 192.168.1.2 ? 我需要添加到in_addr_t值中以便从192.168.1.1跳到192.168.1.2吗?
  • What do I need to add to the in_addr_t value to jump from 175.1.1.255 to 175.1.2.0 on bigger subnets ? 在更大的子网中,我需要添加什么in_addr_t值才能从175.1.1.255跳到175.1.2.0? What about class A IPs then ? 那么A类IP呢?
  • Is there anyway to determine the "gap" values using the network information, in case my subnet is more specific ? 无论如何,如果我的子网更具体,是否可以使用网络信息来确定“间隙”值?
  • Am I missing an easier/cleaner way for my iterating problem ? 我是否缺少解决迭代问题的更简便方法?

It's an endian problem 这是一个字节序问题

In your system the inet_addr("192.168.1.1") returns the ip address presented in 4 octets in this way: 在您的系统中, inet_addr("192.168.1.1")以这种方式返回以4个八位字节表示的ip地址:

    0x01 | 0x01 | 0xA8 | 0xC0
//    1  .   1  .  168 . 192

the inet_addr("192.168.1.2") returns inet_addr("192.168.1.2")返回

    0x02 | 0x01 | 0xA8 | 0xC0
//    2  .   1  .  168 . 192

The difference inet_addr("192.168.1.2") - inet_addr("192.168.1.2") will be: inet_addr("192.168.1.2") - inet_addr("192.168.1.2")的区别为:

0x0201A8C0 - 0x0101A8C0 = 0x1000000 // in decimal is 16777216

Use htonl() function for each inet_addr() inorder to avoid the endian issues 对每个inet_addr()使用htonl()函数,以避免出现字节序问题

So htonl() of inet_addr("192.168.1.2") will return 因此htonl() inet_addr("192.168.1.2") htonl() inet_addr("192.168.1.2")将返回

    0xC0 | 0xA8 | 0x01 | 0x02
//   192 .  168 .   1  . 2

And htonl() of inet_addr("192.168.1.1") returns 然后htonl() inet_addr("192.168.1.1") htonl() inet_addr("192.168.1.1")返回

    0xC0 | 0xA8 | 0x01 | 0x01
//   192 .  168 .   1  . 1

And then the difference will be equal to 1 然后差等于1

      addr1.sin_addr.s_addr = htonl(inet_addr("192.168.1.1"));
      addr2.sin_addr.s_addr = htonl(inet_addr("192.168.1.2"));
      addr3.sin_addr.s_addr = htonl(inet_addr("192.168.1.3"));
      addr4.sin_addr.s_addr = htonl(inet_addr("192.168.1.255"));
      addr5.sin_addr.s_addr = htonl(inet_addr("192.168.2.1"));

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

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