Subnet Mask to IP Range

Source: Internet
Author: User

To convert a subnet mask to an IP address range, you often need to convert the subnet mask to an IP address range for further computation. the conversion principle and Code are as follows: www.2cto.com is used to divide an IP address into a 'subnet number' and a 'host address' mask format [subnet number: 26bit] host address: 6bit 172.16.2.64/26 [10101100 00010000 00000010 000000 01] 10101100 172.16.2.96/26 [00010000 00000010 100000 01] 6bit host address, which can accommodate 63 numbers. then the range of the above two masks is the mask format IP address range 172.16.2.64/26 [172.16.2.64-172.16.2.127] 172.16.2.96/26 [172.16.2.96-172.16.2.127] * Previous subset 2 algorithm www.2cto.com Typedef struct {u_int32_t min; u_int32_t max;} ip_range_t; int str2iprange (ip_range_t * ipr, char * sipr) {char sip [16]; char smask [3]; sscanf (sipr, "% [^/]/% s", sip, smask); ipr-> min = ntohl (inet_addr (sip )); ipr-> max = (ipr-> min &~ (0x1 <(32-atoi (smask)-1) + (0x1 <(32-atoi (smask ))) -1);} test www.2cto.com ip_range_t ipr; str2iprange (& ipr, "172.16.2.94/24"); struct in_addr min = {htonl (ipr. min)}; printf ("% s-", inet_ntoa (min); struct in_addr max = {htonl (ipr. max)}; printf ("% s \ n", inet_ntoa (max); output result: 172.16.2.94-172.16.2.20.end
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.