CN 5
CN 5
CN 5
Title: Subnetting
Aim: Subnetting: (Language: C/C++/Python/Java)/Packet Tracer
Write a program to implement subnetting to find subnet mask
Objectives:
1. To understand and learn the concept of IP address, subnet mask and subnetting
Theory:
IP addresses are unique identifiers for devices on a network, enabling communication between
them. There are two versions of IP: IPv4 and IPv6.
● IPv4: Uses a 32-bit address space, represented in decimal format as four octets (e.g.,
192.168.1.1).
● IPv6: Employs a 128-bit address space, represented in hexadecimal format, allowing
for a vastly larger number of addresses (e.g.,
2001:0db8:85a3:0000:0000:8a2e:0370:7334).
An IPv4 datagram consists of a header and a data section. The header contains essential
information, including source and destination IP addresses, protocol type, and length. The
header size can vary from 20 to 60 bytes.
IPv4 Addressing
1) Prefixes: Indicates the number of bits used for the network portion of the address (e.g., /24
means 24 bits for the network).
2) CIDR (Classless Inter-Domain Routing): A method for allocating IP addresses and IP
routing that replaces the traditional classful addressing system, allowing for more efficient
use of IP address space.
3) Classful Addressing: Divides IP addresses into classes (A, B, C, D, E) based on their
leading bits, dictating default subnet masks.
4) Special Addressing: Includes private addresses (e.g., 10.0.0.0, 192.168.0.0) and loopback
addresses (127.0.0.1).
5) NAT (Network Address Translation): A method that allows multiple devices on a local
network to share a single public IP address for accessing the internet.
The default subnet mask varies based on the class of the IP address:
● Class A: 255.0.0.0
● Class B: 255.255.0.0
● Class C: 255.255.255.0
Subnetting involves borrowing bits from the host portion of the IP address to create additional
network addresses. This process results in a new subnet mask.
To calculate the number of subnets and hosts per subnet, we use the following formulas:
1. Number of Subnets:
Number of Subnets=2nNumber of Subnets=2n
Where nn is the number of bits borrowed from the host portion.
2. Hosts per Subnet:
Hosts per Subnet=2h−2Hosts per Subnet=2h−2
Where hh is the number of bits remaining for host addresses (the subtraction of 2
accounts for the network and broadcast addresses).
Example
For a Class C address 192.168.1.0 with a default subnet mask of 255.255.255.0 (or
/24), if we borrow 2 bits for subnetting, we get:
● Design Screenshots:
CN.ipynb - Colab 21/10/24, 20:45
import ipaddress
def calculate_subnet_mask(num_addresses):
host_bits = (num_addresses - 1).bit_length()
return 32 - host_bits
subnets = []
current_ip = network.network_address
return subnets
def format_output(subnets):
output = "Subnetting Results:\n"
for i, subnet in enumerate(subnets, 1):
first_usable = subnet.network_address + 1
last_usable = subnet.broadcast_address - 1
output += f"{i}. Subnet Block {i}:\n"
output += f" Network Address: {subnet.network_address}/{subnet.prefixlen}
output += f" First Usable Host: {first_usable}\n"
output += f" Last Usable Host: {last_usable}\n"
output += f" Broadcast Address: {subnet.broadcast_address}\n"
output += f" Subnet Mask: {subnet.netmask}\n\n"
return output
https://colab.research.google.com/drive/1KN7nGBBtcPYfhnR4hwKrTc_xx59rTrLp?authuser=1 Page 3 of 19
CN.ipynb - Colab 21/10/24, 20:45
def main():
print("Subnetting Calculator")
ip_block = input("Enter the IP block: ")
subnet_sizes = input("Enter the required subnet sizes separated by commas: ")
subnet_sizes = [int(size) for size in subnet_sizes.split(',')]
try:
subnets = subnet_calculator(ip_block, subnet_sizes)
print(format_output(subnets))
except ValueError as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()
https://colab.research.google.com/drive/1KN7nGBBtcPYfhnR4hwKrTc_xx59rTrLp?authuser=1 Page 4 of 19
CN.ipynb - Colab 21/10/24, 20:45
Subnetting Calculator
Enter the IP block: 172.16.0.0/26
Enter the required subnet sizes separated by commas: 32,16,8,4,2,2
Subnetting Results:
1. Subnet Block 1:
Network Address: 172.16.0.0/27
First Usable Host: 172.16.0.1
Last Usable Host: 172.16.0.30
Broadcast Address: 172.16.0.31
Subnet Mask: 255.255.255.224
2. Subnet Block 2:
Network Address: 172.16.0.32/28
First Usable Host: 172.16.0.33
Last Usable Host: 172.16.0.46
Broadcast Address: 172.16.0.47
Subnet Mask: 255.255.255.240
3. Subnet Block 3:
Network Address: 172.16.0.48/29
First Usable Host: 172.16.0.49
Last Usable Host: 172.16.0.54
Broadcast Address: 172.16.0.55
Subnet Mask: 255.255.255.248
4. Subnet Block 4:
Network Address: 172.16.0.56/30
First Usable Host: 172.16.0.57
Last Usable Host: 172.16.0.58
Broadcast Address: 172.16.0.59
Subnet Mask: 255.255.255.252
5. Subnet Block 5:
Network Address: 172.16.0.60/31
First Usable Host: 172.16.0.61
Last Usable Host: 172.16.0.60
Broadcast Address: 172.16.0.61
Subnet Mask: 255.255.255.254
6. Subnet Block 6:
Network Address: 172.16.0.62/31
First Usable Host: 172.16.0.63
Last Usable Host: 172.16.0.62
Broadcast Address: 172.16.0.63
Subnet Mask: 255.255.255.254
https://colab.research.google.com/drive/1KN7nGBBtcPYfhnR4hwKrTc_xx59rTrLp?authuser=1 Page 5 of 19
Students Observation:
In the subnetting lab assignment, I gained a deeper understanding of IP addressing and subnet
masks, which are crucial for network management. The practical exercise of calculating subnet masks
and determining the number of subnets and hosts allowed me to apply theoretical knowledge to
real-world scenarios. I found the process of subnetting enlightening, as it helped clarify how networks
can be efficiently segmented for improved performance and security. Additionally, the use of tools like
Packet Tracer facilitated hands-on experience in visualizing subnet configurations, reinforcing my
learning through simulation. Overall, this assignment enhanced my comprehension of fundamental
networking concepts and their significance in computer networks.
Classful Addressing divides the IP address space into fixed classes (A, B, C, D, and E) based on the
leading bits. For instance, Class A addresses (1.0.0.0 to 126.255.255.255) are designed for very
large networks and have a default subnet mask of 255.0.0.0. Class B addresses (128.0.0.0 to
191.255.255.255) are suitable for medium-sized networks with a default mask of 255.255.0.0.
Class C addresses (192.0.0.0 to 223.255.255.255) are intended for small networks, using a default
mask of 255.255.255.0.
Classless Addressing (CIDR) allows for variable-length subnet masking (VLSM), which enables
more efficient use of IP addresses by allowing subnets to be defined with any length. For example,
an IP address such as 192.168.1.0/24 indicates that the first 24 bits are used for the network part,
which can be adjusted based on requirements.
Special/reserved IPv4 addresses serve specific purposes within networking. Some key
examples include:
1. Loopback Address: 127.0.0.1 is used for testing network applications on the local
machine.
2. Private Addresses: Ranges such as 10.0.0.0 to 10.255.255.255, 172.16.0.0 to
172.31.255.255, and 192.168.0.0 to 192.168.255.255 are used for internal networks and
are not routable on the internet.
3. Broadcast Address: The highest address in a subnet (e.g., for 192.168.1.0/24, the
broadcast address is 192.168.1.255) is used to send data to all devices in the subnet.
4. Multicast Addresses: Ranges from 224.0.0.0 to 239.255.255.255 are used for multicast
communication.
3. What is subnetting? Explain the use of subnetting with an example.
Subnetting is the process of dividing a larger network into smaller, more manageable
subnetworks (subnets). This practice optimizes network performance and enhances security by
isolating segments. For example, if an organization has a Class C address of 192.168.1.0/24
and needs to create 4 subnets, it can borrow 2 bits from the host portion, resulting in a new
subnet mask of 255.255.255.192 (/26). This allows for four subnets:
1. 192.168.1.0/26
2. 192.168.1.64/26
3. 192.168.1.128/26
4. 192.168.1.192/26
Each subnet will have 62 usable host addresses, enhancing network organization and
security.
○ FLSM (Fixed Length Subnet Mask) is a subnetting technique where all subnets have the
same size and subnet mask. This simplifies routing and management but can lead to
inefficient use of addresses. For instance, if a network uses FLSM with a /24 subnet
mask, each subnet will always contain 256 addresses.
○ VLSM (Variable Length Subnet Mask) allows subnets of different sizes, enabling
more efficient address space utilization. For example, a network can have a /24 subnet
for larger departments and /30 for point-to-point links.
○ CIDR (Classless Inter-Domain Routing) is a method that replaces the traditional
class-based IP addressing with a more flexible approach. It allows for variable-length
prefixes (e.g., 192.168.1.0/22) to optimize routing and conserve IP addresses.
○ Find the subnet mask: To create 14 subnets, the administrator needs to borrow bits from
the host portion. The closest power of 2 that accommodates 14 is 16 (2^4). Therefore, 4
bits are borrowed, changing the default subnet mask of /24 (255.255.255.0) to /28
(255.255.255.240).
○ Find the number of addresses in each subnet: With a /28 subnet mask, there are
2(32−28)=24=162(32−28)=24=16 addresses per subnet, with 14 usable host addresses
(subtracting 2 for network and broadcast addresses).
○ Find the first and last IP addresses in subnet 1:
■ Subnet 1: 200.50.100.0/28
● First IP: 200.50.100.1
● Last IP: 200.50.100.14
○ Find the first and last IP addresses in subnet 14:
■ Subnet 14: 200.50.100.192/28 (Subnet 14 starts after 13 previous subnets)
● First IP: 200.50.100.193
● Last IP: 200.50.100.206