DEV Community

Cover image for What is CIDR?
Sparky-code
Sparky-code

Posted on • Updated on

What is CIDR?

CIDR (Classless Inter-Domain Routing) is a method of assigning IP addresses that allows for more efficient use of available address space. In the past, IP addresses were assigned using one of three classes: Class A, Class B, or Class C. These classes were determined by the first few bits of the IP address, and each class had a fixed number of network and host bits.

CIDR, on the other hand, does away with the concept of classes altogether. Instead, CIDR assigns IP addresses using a notation that specifies both the network address and the number of significant bits in the host address. For example, the CIDR notation 192.168.0.0/24 indicates that the first 24 bits of the IP address (corresponding to the first three octets) represent the network portion of the address, while the last 8 bits represent the host portion.

CIDR allows for more flexible use of available address space because it allows for variable-length subnet masks. In other words, instead of being limited to the fixed network and host bits defined by a classful IP address, CIDR allows for any number of bits to be used for the network portion of the address. This means that networks can be subdivided into smaller subnets, which can be more efficiently allocated to different devices or users.

CIDR has become the standard method for IP address assignment on the Internet, as it allows for more efficient use of available address space and better scalability. It has also simplified network management by allowing for more flexible subnetting and easier route aggregation. However, CIDR does require more careful planning and configuration than classful addressing, as assigning incorrect subnet masks can lead to routing issues and other problems.

Sounds simple right? I added a couple of examples to illustrate this in practice.

Example 1: Suppose an organization has been allocated the IP address range 10.0.0.0/8. This means that the first 8 bits of the IP address (i.e., the first octet) represent the network portion of the address, while the remaining 24 bits represent the host portion. This organization can then subdivide this address space into smaller subnets using a subnet mask of their choice. For example, they might use the subnet 10.1.0.0/16 to represent a subnet with 16 bits of network address and 16 bits of host address, allowing for up to 65,534 hosts on this subnet.

Example 2: An Internet Service Provider (ISP) might be assigned the IP address range 200.100.50.0/22. This means that the first 22 bits of the IP address (i.e., the first three octets and the first 2 bits of the fourth octet) represent the network portion of the address, while the remaining 10 bits represent the host portion. The ISP can then allocate portions of this address space to their customers, using a smaller subnet mask to divide the address space further. For example, they might allocate the subnet 200.100.52.0/23 to a customer, allowing for up to 510 hosts on this subnet.


import ipaddress

# Allocate a /24 network address to a department with 10 devices
network_address = ipaddress.IPv4Network('192.168.1.0/24')
department_subnet = network_address.subnets(prefixlen_diff=3)

# Allocate a /27 subnet address to the department
department_address = next(department_subnet)
print(department_address)

# Allocate unique IP addresses to each device in the department
devices = [ipaddress.IPv4Address(str(department_address.network_address + i)) for i in range(10)]
print(devices)

Enter fullscreen mode Exit fullscreen mode

A basic example of how you might script this process

Top comments (1)

Collapse
 
femolacaster profile image
femolacaster

Good topic. Keep going.