Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

c - 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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...