C++ 基于IP和掩码C+打印所有IP+;

C++ 基于IP和掩码C+打印所有IP+;,c++,network-programming,ip,netmask,C++,Network Programming,Ip,Netmask,我想为给定的掩码打印所有可能的IP。我有这个代码来获取它,但似乎我遗漏了一些东西,因为我无法获取IP列表。我的代码已经基于 无符号整数IP地址,子网掩码; inet\u pton(AF\u inet,b->IpAddress list.IpAddress.String,&IpAddress); inet\u pton(AF\u inet,b->ipaddress list.IpMask.String和subnetmask); 对于(unsigned int i=1;i您将IP地址与添加了子网掩码

我想为给定的掩码打印所有可能的IP。我有这个代码来获取它,但似乎我遗漏了一些东西,因为我无法获取IP列表。我的代码已经基于

无符号整数IP地址,子网掩码;
inet\u pton(AF\u inet,b->IpAddress list.IpAddress.String,&IpAddress);
inet\u pton(AF\u inet,b->ipaddress list.IpMask.String和subnetmask);
对于(unsigned int i=1;i您将IP地址与添加了子网掩码的IP地址(基本上是or)和更改的主机部分进行anding。此处的优先级错误。您应该将IP地址与网络掩码一起获得网络部分,然后或主机部分:

auto ip = (ipaddress & subnetmask) | i;
另外,
inet\u pton
的结果不是
int
,而是
结构in\u addr
so YMMV。最可能的情况是,您应该使用
inet\u addr
,因为它返回
uint32\u t

ip_address = inet_addr("127.0.0.1");
但是,您的代码仍然希望
127
是最重要的字节,而LSB系统中没有该字节。因此,您需要先用
ntohl
交换这些地址一次,然后再用
htonl
交换这些地址

因此,我们得到如下结果:

uint32_t ipaddress;
uint32_t subnetmask;

ipaddress = ntohl(inet_addr(b->IpAddressList.IpAddress.String));
subnetmask = ntohl(inet_addr(b->IpAddressList.IpMask.String));

for (uint32_t i = 1; i<(~subnetmask); i++) {
    uint32_t ip = (ipaddress & subnetmask) | i;
    struct in_addr x = { htonl(ip) };
    printf("%s\n", inet_ntoa(x));
}
uint32\u t ip地址;
uint32_t子网掩码;
ipaddress=ntohl(inet_addr(b->IpAddressList.ipaddress.String));
subnetmask=ntohl(inet_addr(b->IpAddressList.IpMask.String));
对于(uint32_t i=1;i您将IP地址与添加了子网掩码的IP地址(基本上是or)和更改的主机部分进行anding。此处的优先级错误。您应该将IP地址与网络掩码进行and,以获得网络部分,然后或主机部分:

auto ip = (ipaddress & subnetmask) | i;
另外,
inet\u pton
的结果不是
int
,而是
结构in\u addr
so YMMV。最可能的情况是,您应该使用
inet\u addr
,因为它返回
uint32\u t

ip_address = inet_addr("127.0.0.1");
但是,您的代码仍然希望
127
是最重要的字节,而LSB系统中没有该字节。因此,您需要先用
ntohl
交换这些地址一次,然后再用
htonl
交换这些地址

因此,我们得到如下结果:

uint32_t ipaddress;
uint32_t subnetmask;

ipaddress = ntohl(inet_addr(b->IpAddressList.IpAddress.String));
subnetmask = ntohl(inet_addr(b->IpAddressList.IpMask.String));

for (uint32_t i = 1; i<(~subnetmask); i++) {
    uint32_t ip = (ipaddress & subnetmask) | i;
    struct in_addr x = { htonl(ip) };
    printf("%s\n", inet_ntoa(x));
}
uint32\u t ip地址;
uint32_t子网掩码;
ipaddress=ntohl(inet_addr(b->IpAddressList.ipaddress.String));
subnetmask=ntohl(inet_addr(b->IpAddressList.IpMask.String));

对于(uint32_t i=1;i您可以使用输入掩码按位
输入IP地址来确定范围内的第一个IP,使用掩码的倒数按位
输入IP地址来确定范围内的最后一个IP。然后您可以循环使用介于两者之间的值

另外,
inet\u pton(AF\u inet)
需要指向addr
中的
结构的指针,而不是
无符号int

请尝试以下方法:

struct in_addr ipaddress, subnetmask;

inet_pton(AF_INET, b->IpAddressList.IpAddress.String, &ipaddress);
inet_pton(AF_INET, b->IpAddressList.IpMask.String, &subnetmask);

unsigned long first_ip = ntohl(ipaddress.s_addr & subnetmask.s_addr);
unsigned long last_ip = ntohl(ipaddress.s_addr | ~(subnetmask.s_addr));

for (unsigned long ip = first_ip; ip <= last_ip; ++ip) {
    unsigned long theip = htonl(ip);
    // use theip as needed...
}

您可以使用输入掩码按位
输入IP地址来确定范围内的第一个IP,使用掩码的倒数按位
输入IP地址来确定范围内的最后一个IP。然后,您可以循环使用介于两者之间的值

另外,
inet\u pton(AF\u inet)
需要指向addr
中的
结构的指针,而不是
无符号int

请尝试以下方法:

struct in_addr ipaddress, subnetmask;

inet_pton(AF_INET, b->IpAddressList.IpAddress.String, &ipaddress);
inet_pton(AF_INET, b->IpAddressList.IpMask.String, &subnetmask);

unsigned long first_ip = ntohl(ipaddress.s_addr & subnetmask.s_addr);
unsigned long last_ip = ntohl(ipaddress.s_addr | ~(subnetmask.s_addr));

for (unsigned long ip = first_ip; ip <= last_ip; ++ip) {
    unsigned long theip = htonl(ip);
    // use theip as needed...
}


如果你知道你的代码是C++,那么为什么你还添加了C编辑的C和C++回滚,因为C++和C++的答案会有很大的不同。想要两者都要问两个问题。考虑网络掩码:例如1.200。你是否还添加了C编辑的C和C++回滚,因为C和C++的答案会有很大的不同。想要两者都要问两个问题。考虑网络掩码:例如1.200。。040和Subnermak是0xfffffc00。我在文章中使用的示例的正确结果是使用您的代码,IP 172.0.0.0一直被打印。ipaddress是0xac160040,Subnermak是0xfffffc00。我在文章中使用的示例的正确结果是Hi,它抱怨错误C2678:二进制“&”:未找到接受“ULONG”类型左侧操作数的运算符(或没有可接受的转换)“在计算
第一个\u ip
最后一个\u ip
的行中,尝试更改
子网掩码的
子网掩码.s\u addr
,但循环没有提供预期的IPS,我忘记在
子网掩码上应用
s\u addr
,以及
ntohl()
nlhto()
到生成的ip。我已经修复了它。我更新了问题中的代码,因为您的代码现在似乎也不工作。我设置了ip和网络掩码,以便您可以测试它是否工作正常。谢谢!我在发布更新的代码之前测试了它。事实上,我已经使用
inet\u ntoa()对其进行了测试
在循环中,就像你做的那样。它对我来说很好,正如我所展示的那样,并打印正确的IP。但是,你在循环中调用了两次
htonl()
,因此你实际上是在取消它,并使用错误的字节顺序打印IP。将
struct in_addr x={htonl(theip)};
更改为
struct in_addr x={theip};
因为
theip
已经是
inet\u ntoa()的字节顺序
是预期的。这不是我运行显示的代码时得到的结果。我得到了
172.22.0.1 172.22.0.2 172.22.0.3 172.22.0.4…
,正如预期的那样。在Windows上,
in_addr
是一个
联合体
,因此使用
={theip}初始化
in_addr x中的结构
语法无法初始化正确的字段。请改用
struct in_addr x;x.s_addr=theip;
。您好,它抱怨“error C2678:binary'&”:未找到接受左操作数类型“ULONG”的运算符(或没有可接受的转换)在计算
第一个\u ip
最后一个\u ip
的行中,尝试更改