返回给定IP地址和子网掩码的IP地址列表的函数 [英] Function that return a list of IP addresses for a given IP adress and subnetmask

查看:435
本文介绍了返回给定IP地址和子网掩码的IP地址列表的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助在C / C ++中编写一个函数,它接收两个参数: IP地址子网掩码

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

该功能需要重新计算相关网络中所有IP地址的列表。

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

例如:
给出两个参数: IP地址 = 192.168.33.72和掩码 = 255.255.255.192
该函数将返回一个包含IP 192.168.33.65到192.168.33.126的列表。

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)首先你可以转换 ipaddress 子网掩码从字符串格式到二进制格式,带 inet_pton()

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

2)制作一个检查子网掩码掩码它应该是有效的子网掩码

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

3)获取 subnetmask 反向值( ~pubnetmask

4)

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

    ip = ipaddress & (subnetmask + i);

    //append ip to your ip list

}

这篇关于返回给定IP地址和子网掩码的IP地址列表的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆