IPv4 Addresses and Address Classes
Before we dive into CIDR Blocks and CIDR notation, it is important to have a fundamental understanding IP address. IPv4 is the fourth version of the IP. It is one of the core protocols of the standards-based methods used to interconnect the internet and other networks. IPv4 is currently assigned to all computers. An IPv4 address uses 32-bit binary numbers to form a unique IP address. It takes the format of four sets of numbers, each of which ranges from 0 to 255 and represents an eight-digit binary number, separated by a period point.
Address Classes
Some IP addresses are reserved by the Internet Assigned Numbers Authority (IANA). These are typically reserved for networks that carry a specific purpose on the Transmission Control Protocol/Internet Protocol (TCP/IP), which is used to interconnect devices. Four of these IP address classes include:CIDR Blocks
Filtering an IP address using a CIDR block (a backslash followed by a number) requires thinking of an address in binary. An IP address in four 8-bit sections results in a total of 32 bits. The number that follows the backslash represents the amount of bits that are blocked when defining the range.
CIDR Notation
CIDR stands for Classless Inter-Domain Routing, and is a compact representation of an IP address and its associated routing prefix and a method for allocating IP addresses and for IP routing. It is the "/" at the end of an IP address, like 192.168.60.55/20 for example.
To visualize what is going on, we can look at something called the "Powers of Two" table, and taking the above example of 192.168.60.55/20, begin with the Subnet Mask, which is a bitmask that encodes the prefix length associated with an IPv4 address or network in quad-dotted notation: 32 bits, starting with a number of 1 bits equal to the prefix length, ending with 0 bits, and encoded in four-part dotted-decimal format: 255.255.255.0. The CIDR notation is in simple terms, the number of bits "turned on". (Represented by 1). More on a bitmask here.
It is first important to define what an octet is, as I will be referring to this term many times. The 32-bit IP address is grouped 8 bits at a time, each group of 8 bits is an octet. Each of the four octets are separated by a dot, and represented in decimal format. Put simply, an octet is each group of numbers in the IP address that are separated by the dot.
Using our example above of a CIDR notation of 192.168.60.55/20, moving through the octets, this table shows we have 20 bits turned on - 8 for the first 2, and 4 for the third. Important to note, each position in the octet corresponds with a position in the Powers of Two table.
From here, we could translate each octet into binary, but in the example of 255.255.240.0, that wouldn't be unnecessary. Since the first two octets are all "on" (255.255), and the last is all "off", we can simply look at the third octet, (240).
Since the first two octets are all turned on, we know they will be 192.168, as well as the last octet being all zeros, that will be 0 for Network IP, and 255 for Broadcast ID. For more on why the BroadcastID is 255, see here: BroadcastID.
Now to calculate, and since we already know the first two and last octets, those are represented in x's.
(Top row in picture above.) Use the Powers of Two table to determine. Starting with our IP number of 60, and moving through the table:
To get final number for our IP address, all we do is use the logic table to determine the values. So, just using the octet we have been looking at, we take the value of the subnet mask and 'multiply' it by the binary we just calculated.
The result is 48 because we have ones in the 32 and 16 positions in the Powers of Two tables. So 32 + 16 = 48.
The definition of a broadcast address is a network address used to transmit to all devices connected to a multiple-access communications network. A message sent to a broadcast address may be received by all network-attached hosts. In simple terms, a broadcast address refers to a special IP address that is used to send a message or packet to all devices on a network. This may sound similar to a default gateway, but there is a key difference:
Back to our example, to figure this number out, we need to find whatever number would be the next possible Network ID in the list, and the number right before it would be the Broadcast ID. This is determined by looking at something called the "magic number". The magic number is the last bit that is turned on in the subnet mask (in our case it is the 4th from the left). That number represents 16.
BroadcastID or Broadcast Address is the address in a subnet not assigned to single host, it is broadcasted or sent to all hosts and devices on the network. Since we know the network portion of the ID based on the subnet, we now take what is called the wildcard bits which will indicate the portion of the octet bits that are not a part of the network. We logically assign a value of 1 to all those bits, and in our working example our wildcard is: 0.0.0.255.
In simple terms, you can think of the broadcast ID as an inversion or opposite of the bits set in the subnet ID. Using our working example, since the subnet is 192.168.40.0, the last octet consists of the wildcard bits. Since all bits are set a value of 1, it will be: 192.168.63.255.
Now that we have both the Network and Broadcast IDs, thus having our upper and lower range we just calculated (the 48 and 63 above), expanding on the table shown above, we can determine the full IP range of available IPs:
I created a CLI tool in Python that performs this logic for a given IP address. It takes an IP address as a command line argument and determines whether the IP provided on the CLI is in any of the CIDRs retrieved. (The list of IP, ASN, and CIDR ranges is a default list taken from https://stat.ripe.net/data/country-resource-list)
The tool with output Pass/ Fail to the command line based on the presence of the IP address in the CIDR ranges.Written: December 26, 2023