IP落在CIDR范围 [英] IP falls in CIDR range
本文介绍了IP落在CIDR范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的IP:12.12.12.12
我通过不同的IP范围循环(在12.12.12.0/24(实施例))格式,并试图以查看是否IP是在范围内。
我已经试过各种方法,如inet_addr和比较,但我似乎无法得到它。
有没有一种简单的方法来做到这一点?我使用Windows。
I have an IP like this: 12.12.12.12
I'm looping through different IP ranges (in 12.12.12.0/24 (example)) format, and trying to see if the IP is in the range.
I have tried various methods such as inet_addr and comparing but I can't seem to get it.
Is there an easy way to do this? I'm using Windows.
推荐答案
只是测试是否:
(ip & netmask) == (range & netmask)
您可以决定从CIDR参数范围/ netbits
如下子网掩码:
You can determine the netmask from the CIDR parameters range/netbits
as follows:
uint32_t netmask = ~(~uint32_t(0) >> netbits);
这篇关于IP落在CIDR范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文