Function that return a list of IP addresses for a given IP adress and subnetmask

三世轮回 提交于 2019-12-14 03:25:02

问题


I need help in writing a function in C/C++ that receives two parameters: IP address and subnetmask.

The function needs to reutrn a list of all IP addresses that are in the associated network.

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.


回答1:


1) first you can transform the ipaddress and the subnetmask from string format to binary format with inet_pton().

2) make a check on the subnetmask mask it should be a valid subnet mask

3) get the subnetmask inverse value (~subnetmask)

4)

for (i=1; i<(~subnetmask); i++) {

    ip = ipaddress & (subnetmask + i);

    //append ip to your ip list

}


来源:https://stackoverflow.com/questions/19728127/function-that-return-a-list-of-ip-addresses-for-a-given-ip-adress-and-subnetmask

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!