Networking 101/3: Find the number of valid Hosts in a Subnet
Description:
Introduction
An IPv4 subnet mask consist of 32 bits, divided into four octets. As an example, a subnet mask of 255.255.255.0 can be written as:
11111111.11111111.11111111.00000000
255 . 255 . 255 . 0
The ones (1) represent the network/subnet part of the mask and the zeroes (0) represent the host part of the mask.
The zeroes tell us how many IP addresses there are in the network defined by the ones.
In the example subnet above there are 256 addresses available; more precisely 0-255.
The Assignment
Your task is to implement a function uint32_t hosts_in_subnet(const char* netmask)
which takes in an IPv4 subnet mask netmask
and returns a 32-bit unsigned integer uint32_t
with the number of valid host addresses. Using the subnet mask netmask
you can figure out how many usable host addresses there are in a network defined by that netmask.
Notes
The first and last address in a subnet are not valid host addresses; they are reserved for the network and broadcast addresses.
There are two special cases:
- If all 32 bits are ones there is only one valid host address.
- When the mask has 31 bits set to ones there are two valid host addresses (according to RFC 3021).
Testing
The tests will challenge you with valid netmasks in the range (128.0.0.0 - 255.255.255.255
) and nothing else.
Similar Kata:
Stats:
Created | May 18, 2022 |
Published | May 19, 2022 |
Warriors Trained | 155 |
Total Skips | 13 |
Total Code Submissions | 406 |
Total Times Completed | 71 |
C Completions | 71 |
Total Stars | 4 |
% of votes with a positive feedback rating | 95% of 20 |
Total "Very Satisfied" Votes | 19 |
Total "Somewhat Satisfied" Votes | 0 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 5 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |