[英]Function that return a list of IP addresses for a given IP adress and subnetmask
I need help in writing a function in C/C++ that receives two parameters: IP address and subnetmask . 在用C / C ++编写一个接收两个参数的函数时,我需要帮助: IP地址和subnetmask 。
The function needs to reutrn a list of all IP addresses that are in the associated network. 该功能需要重新列出关联网络中所有IP地址的列表。
For example: Given two parameters: IP address = 192.168.33.72 and mask = 255.255.255.192 the function will return a list that contain the IP's 192.168.33.65 to 192.168.33.126. 例如:给定两个参数: IP地址 = 192.168.33.72和mask = 255.255.255.192,该函数将返回一个列表,其中包含IP的192.168.33.65至192.168.33.126。
1) first you can transform the ipaddress
and the subnetmask
from string format to binary format with inet_pton()
. 1)首先,您可以使用
inet_pton()
将ipaddress
和subnetmask
从字符串格式转换为二进制格式。
2) make a check on the subnetmask
mask it should be a valid subnet mask 2)检查
subnetmask
掩码,它应该是有效的子网掩码
3) get the subnetmask
inverse value ( ~subnetmask
) 3)获取
subnetmask
逆值( ~subnetmask
)
4) 4)
for (i=1; i<(~subnetmask); i++) {
ip = ipaddress & (subnetmask + i);
//append ip to your ip list
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.