Professional Documents
Culture Documents
Computer Networks Lab Project: Fragmentation
Computer Networks Lab Project: Fragmentation
FRAGMENTATION
Submitted to Manipal University, Jaipur
Towards the partial fulfilment for the Award of the Degree of
BACHELORS OF TECHNOLOGY
In Computers Science and Engineering
2021-2022
By
Akshay Jain
189301060
AIM:
Build an algorithm showing the process of fragmentation and demonstrate the
same using any programming language of your choice.
SYSTEM REQUIREMNTS:
Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
4 GB RAM
64-bit Operating System
THEORY
Fragmentation is done by the network layer when the maximum size of
datagram is greater than maximum size of data that can be held a frame i.e., it’s
Maximum Transmission Unit (MTU). The network layer divides the datagram
received from transport layer into fragments so that data flow is not disrupted.
It is done by network layer at the destination side and is usually done at
routers.
Source side does not require fragmentation due to wise (good)
segmentation by transport layer i.e. instead of doing segmentation at
transport layer and fragmentation at network layer, the transport layer
looks at datagram data limit and frame data limit and does segmentation
in such a way that resulting data can easily fit in a frame without the need
of fragmentation.
Receiver identifies the frame with the identification (16 bits) field in IP
header.
Each fragment of a frame has same identification number.
Receiver identifies sequence of frames using the fragment offset(13
bits) field in IP header
3
ALGORITHM DEVELOPED:
1. IP_Fragmentation_Module (datagram)
2. {
3. Extract the size of datagram
4. If (size > MTU of the corresponding network)
5. {
6. If (D bit is set)
7. {
8. Discard datagram
9. Send an ICMP error message
10. return
11. }
12. Else
13. {
4
CODE BUILD:
5
6
import math
class Datagram :
"""
A class to represent a Datagram to be sent from Source
...
Attributes
----------
version (string) - 4 bits : IPv4 Datagram
header_length (int) - 4 bits : Header length (20B - 60B) in Bytes
total_length (int) - 16 bits : Length of Header + Data in Bytes
data_length (int) : Total_length - header_length in Bytes
"""
7
version = "0100"
identifier = "10101110011110000"
DNF = 0
MF = 0
offset = 0
def __str__(self):
message = f"Total Length: {self.total_length} \nData : {self.data_length}" \
f"\nHeader Length : {self.header_length} \n\nDNF : {self.DNF} | MF : {self.MF}
\nOffset : {self.offset}"
return message
class Fragmented_Datagram(Datagram):
"""
A class to represent a
...
Attributes
----------
version (string) - 4 bits : IPv4 Datagram
header_length (int) - 4 bits : Header length (20B - 60B) in Bytes
8
"""
def __init__(self,header_length,data_length,MF,offset):
super().__init__(header_length,data_length + header_length)
self.MF = MF
self.offset = offset
if datagram.DNF == 1 :
print("\nDNF == 1")
print("\nDatagram Cannot be Fragmented")
return None
else :
total_transfer_data = datagram.data_length
9
if ( (MTU - datagram.header_length) // 8 == 0) :
new_data_length = MTU - datagram.header_length
else :
new_data_length = 8*((MTU - datagram.header_length) // 8)
fragments_list = []
offset = 0
MF = 1
for i in range(fragments_count):
offset = i * (new_data_length // 8)
obj = Fragmented_Datagram(datagram.header_length,new_data_length,MF,offset)
fragments_list.append(obj)
return fragments_list
else :
print("`n____________No Fragmentation Required____________")
print("\n----------------------------------------")
10
def main():
datagram_total_len = int(input("Enter Datagram Total (Header + Data) Length: ").strip())
datagram_header_len = int(input("Enter Datagram Header Length: ").strip())
MTU = int(input("Enter Max. Transmission Unit (MTU): ").strip())
datagram = Datagram(datagram_header_len,datagram_total_len)
print("\n<--------- FOLLOWING DATAGRAM TO SENT --------->\n")
print(datagram)
print("<------------------------------------------------> ")
if fragments_list != None :
for i in range(len(fragments_list)):
print("\nPacket No: {}".format(i+1))
print("-----------------")
print(fragments_list[i])
print("-----------------")
print("\n\n__________Fragmentation Ends__________\n")
if __name__ == "__main__":
main()
11
EXCECUTION:
Input 1:
Output 1:
12
Input 2:
Output 2:
13
14
CONCLUSION:
IP fragmentation is an Internet Protocol (IP) process that breaks packets into
smaller pieces (fragments), so that the resulting pieces can pass through a link
with a smaller maximum transmission unit (MTU) than the original packet size.
The fragments are reassembled by the receiving host.