CN Lab Manual - r22
CN Lab Manual - r22
2. To understand the network simulator environment and visualize a network topology and observe
its performance
3. To analyze the traffic flow and the contents of protocol frames
Course Outcomes
1. Implement data link layer farming methods
List of Experiments
1. Implement the data link layer framing methods such as character, character-stuffing and bit stuffing.
2. Write a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC CCIP
3. Develop a simple data link layer that performs the flow control using the sliding window protocol,
and loss recovery using the Go-Back-N mechanism.
4. Implement Dijsktra’s algorithm to compute the shortest path through a network
5. Take an example subnet of hosts and obtain a broadcast tree for the subnet.
6. Implement distance vector routing algorithm for obtaining routing tables at each node.
10. Wireshark
i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii. Viewing Captured Traffic
1
iv. Analysis and Statistics & Filters.
11. How to run Nmap scan
2
12. Operating System Detection using Nmap
i. NS2 Simulator-Introduction
ii. Simulate to Find the Number of Packets Dropped
iii. Simulate to Find the Number of Packets Dropped by TCP/UDP
iv. Simulate to Find the Number of Packets Dropped due to Congestion
v. Simulate to Compare Data Rate& Throughput.
vi. Simulate to Plot Congestion for Different Source/Destination
vii. Simulate to Determine the Performance with respect to Transmission of Packets
3
1. Implement the data link layer framing methods such as character, character-
#include<stdio.h>
#include<string.h>
void main()
int a[20],b[30],i,j,k,count,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
while(i<n)
if(a[i]==1)
b[j]=a[i];
{ j+
+;
b[j]=a[k];
count++;
4
if(count==5)
5
{ j+
+;
b[j]=0;
i=k;
}}
else
b[j]=a[i];
} i+
+;
j++;
for(i=0;i<j;i++)
printf("%d",b[i]);
Output
6
1
7
2. Write a program to compute CRC code for the polynomials CRC-12, CRC-16
#include<stdio.h>
#include<conio.h>
int main(void)
int data[50],div[16],rem[16];
int ch;
clrscr();
i = 0;
if(ch == '1')
data[i] = 1;
else
data[i] = 0;
i++;
8
}
datalen = i;
i = 0;
if(ch == '1')
div[i] = 1;
else
div[i] = 0; i+
+;
divlen = i;
data[i] = 0;
rem[i] = data[i];
k = divlen-1;
else
if(k == datalen-1)
break;
rem[i] = rem[i+1];
printf("%d",rem[i]);
rem[i] = data[++k];
printf("%d\n",rem[i]);
j=1;
data[i] = rem[j++];
10
}
printf("%d",data[i]);
getch();
return 0;
OUTPUT:
0011
0111
1111
1001
0100
1000
0110
10101111110
11
OUTPUT CONSOLE:
3. Develop a simple data link layer that performs the flow control using the
sliding window protocol, and loss recovery using the Go-Back-N mechanism.
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
printf("Enter window size: ");
scanf("%d",&w);
printf("\nEnter number of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\nWith sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)\n\n");
12
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);
for(i=1;i<=f;i++)
if(i%w==0)
printf("%d\n",frames[i]);
else
printf("%d ",frames[i]);
if(f%w!=0)
return 0;
Output
With sliding window protocol the frames will be sent in the following manner (assuming no corruption of
frames)
After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver
12 5 89
Acknowledgement of above frames sent is received by sender
13
46
Acknowledgement of above frames sent is received by sender
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
//pred[] stores the predecessor of each node
else
cost[i][j]=G[i][j];
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
15
while(count<n-1)
{
mindistance=INFINITY;
mindistance=distance[i];
nextnode=i;
}
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
OUTPUT CONSOL
17
5. Take an example subnet of hosts and obtain a broadcast tree for the subnet.
#include<stdio.h>
int a[10][10],n;
main()
int i,j,root;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++) for(j=1;j<=n;j+
+)
scanf("%d",&a[i][j]);
scanf("%d",&root);
adj(root);
adj(int k)
int i,j;
18
printf("Adjacent node of root node::\n");
printf("%d\n",k);
for(j=1;j<=n;j++)
if(a[k][j]==1 || a[j][k]==1)
printf("%d\t",j);
printf("\n");
for(i=1;i<=n;i++)
printf("%d",i);
}}
OUTPUT:
Enter adjacent
matrix
20
Enter connecting of 3–>2::1
21
6. Implement distance vector routing algorithm for obtaining routing tables at
each node.
#include<stdio.h>
struct node
unsigned dist[20];
unsigned
from[20];
}rt[10];
int main()
int dmat[20][20];
int
n,i,j,k,count=0;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]); dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j]; rt[i].from[j]=j;
22
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
while(count!=0);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
}
}
printf("\n\n");
23
OUTPUT CONSOLE:
24
7. Implement data encryption and data decryption
#include
<stdio.h> int
main()
int i, x;
char str[100];
gets(str);
scanf("%d", &x);
switch(x)
case 1:
str[i] = str[i] + 3; //the key for encryption is 3 that is added to ASCII value
25
break;
26
case 2:
str[i] = str[i] - 3; //the key for encryption is 3 that is subtracted to ASCII value
break;
default: printf("\nError\
n");
return 0;
Output:
Encryption:
27
8. Write a program for congestion control using Leaky bucket algorithm.
#include<iostream.h>
#include<dos.h>
#include<stdlib.h>
if(a>bucketSize) cout<<"\n\t\
delay(500);
while(a>b
cout<<"\n\t\t"<<b<<" bytes
void main()
randomize();
cin>>op;
for(int i=1;i<=5;i++)
28
{
29
delay(random(1000));
pktSize=random(1000);
bktInput(pktSize,op);
Packet no 0 Packet size = 3 Bucket output successful Last 3 bytes sent Packet no 1
Packet size = 33 Bucket output successful
30
9. Write a program for frame sorting technique used in buffers.
#include <stdio.h>
#include
<stdlib.h>
#include
<string.h> struct
frame
int sno;
char
msg[15]; int
flag;
};
int main()
int i,j,n,r,k;
printf("enter no of frames\n");
scanf("%d",&n);
int s[n];
for(i=0;i<n;i++)
s[i]=-1;
fr[i].sno=-1;
31
}
for(i=0;i<n;i++)
32
scanf("%s",fr[i].msg);
fr[i].sno=i;
for(j=0;j<n;j++)
r=rand()
%n;
if(s[r]==-1)
fr[j].flag=r;
s[r]=1;
else if(s[r]==1)
for(k=0;k<n;k++){
r=k;
if(s[r]==-1)
fr[j].flag=r;
s[r]=1;
break;
33
}
34
printf("arrived frame are:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(fr[j].flag==i)
printf("%d\t%s",fr[j].sno,fr[j].msg);
printf("\n");
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(fr[j].sno>fr[j+1].sno)
temp=fr[j];
fr[j]=fr[j+1];
fr[j+1]=temp;
35
}
36
}
for(i=0;i<n;i++)
printf("%d\t%s",fr[i].sno,fr[i].msg);
printf("\n");
return 0;
37
10. Wire shark
When you start Wireshark, you will see a list of interfaces that you can capture packets to and from.
There are many types of interfaces you can monitor using Wireshark, for example, Wired, Wireless, USB
and many external devices. You can choose to show specific types of interfaces in the welcome screen from
the marked section of the screenshot below.
38
Here, I listed only the Wired network interfaces.
Now to start capturing packets, just select the interface (in my case interface ens33) and click on the
Start capturing packets icon as marked in the screenshot below. You can also double click on the
interface that you want to capture packets to and from to start capturing packets on that particular
interface.
39
You can also capture packets to and from multiple interfaces at the same time. Just press and hold <Ctrl>
and click on the interfaces that you want to capture packets to and from and then click on the Start capturing
packets icon as marked in the screenshot below.
40
Using Wireshark on Ubuntu:
I am capturing packets on the ens33 wired network interface as you can see in the screenshot below. Right
now, I have no captured packets.
41
I pinged google.com from the terminal and as you can see, many packets were captured.
42
Starting Wireshark:
You can also run the following command to start Wireshark from the Terminal:
$ wireshark
If you did not enable Wireshark to run without root privileges or sudo, then the command should be:
$ sudo wireshark
43
Viewing Captured Traffic
You can also see the RAW data of that particular packet.
you can also click on the arrows to expand packet data for a particular TCP/IP Protocol Layer.
44
Analysis and Statistics & Filters.
To filter packets, you can directly type in the filter expression in the textbox as marked in the
screenshot below.
45
46
11. How to run Nmap scan
We will be using the Ubuntu command line, the Terminal, in order to view the devices connected to
our network. Open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.
When it comes to reliable network scanning, nmap is a tool that you can totally depend on.
Enter the following command as sudo in the Terminal application in order to install the
tool.
The system will ask you the password for sudo as only an authorized user can install/uninstall and
The system will also prompt you with a y/n option to confirm the installation. Please enter Y and hit
In order to know which devices are connected to your network, you first need to get the IP range or
the subnet mask of your network. We will be using the ifconfig command in order to get this IP. In
order to run the ifconfig command, we need to have net-tools installed on our Ubuntu. Use the
following command in order to install net-tools if you already do not have it installed on your system:
47
The system will prompt you with a y/n option to confirm the installation. Please enter Y and hit enter to
begin the installation process.
Once you have the net-tools utility available, run the following command in order to get information about
the network(s) your system is connected to:
$ ifconfig
The highlighted IP from the output indicates that our system is using 192.168.100.0 subnet mask and the
range is 255. Thus our network IP range is from 192.168.100.0 to 192.168.100.255.
Alternative (UI)
48
Instead of using the ifconfig tool, you can get the subnet mask IP through the Ubuntu GUI as well.
Access the Settings utility from the system Dash and check the details of your network either by clicking the
settings icon against the wifi or ethernet network you are connected to.
In this example, we have checked the settings of a wi-fi network we are currently connected to.
49
Operating System Detection using Nmap
NMAP has a database which is installed when you install NMAP. The database is used when doing OS
detection, but it is not automatically updated.
The database is located at ‘/usr/share/nmap/nmap-os-db’. The easiest way to manage an update is first to
look at the database version number. Open the file in a text editor and the version number is usually listed
on the second line. The second line of my database is ‘# $Id: nmap-os-db 35407 2015-11-10 04:26:26Z
dmiller
$’. The database version for this file is 35407.
OS Detection Process
Before we get into the actual command and performing an OS Detection we should cover
some details about what is happening during this scan.
There are five separate probes being performed. Each probe may consist of one or more
packets. The response to each packet by the target system helps to determine the OS type.
1. Sequence Generation
2. ICMP Echo
3. TCP Explicit Congestion Notification
4. TCP
5. UDP
Now we can look at these individually to see what they are doing.
Sequence Generation
The Sequence Generation Probe consists of six packets. The six packets are sent 100 ms apart and are all
TCP SYN packets.
The result of each TCP SYN packet will help NMAP determine the OS type.
ICMP Echo
Two ICMP Request packets are sent to the target system with varying settings in the packet.
When a lot of packets are being generated and passing through a router causing it to be burdened is known as
congestion. The result is that systems slow down to reduce congestion so the router is not dropping packets.
50
The packet being sent is only to get a response from the target system. Specific values returned are used to
determine the specific OS since each OS handles the packets in different ways.
TCP
Some packets are sent to open or closed ports with specific packet settings. Again, the results will vary
depending on the target OS.
The TCP Packets are all sent with varying flags as follows:
1. no flags
3. ACK
4. SYN
5. ACK
UDP
If the port used on the target system is closed and an ICMP Port Unreachable message is returned then there
is no Firewall.
Now we need to run the actual command to perform an OS Detection. If you have read any of the other of
my NMAP articles then it is best not to perform a PING. To skip the PING we use the parameter ‘-Pn’. To
see the extra information we may require you should use the ‘-v’ parameter for adding verbosity.
Specifically to get the OS Detection the parameter ‘-O’ is needed.
For the command to complete properly and perform the TCP SYN Scan you need to perform the
command as ROOT. In my case, I will perform the scan on one system only and not the whole network so
the command would be:
51
The results of the scan are shown in Figure 2. The scan shows that there are seven open ports using a SYN
Stealth Scan.
FIGURE 2
1. 21/tcp ftp
2. 22/tcp ssh
3. 111/tcp rpcbind
4. 139/tcp netbios-ssn
5. 445/tcp microsoft-ds
6. 2049/tcp nfs
7. 54045/tcp unknown
52
F:7F. The final portion shows the OS type:
The system is running Ubuntu Server 16.10 and the Linux Kernel is 4.8. The uptime is guessed
correctly. I performed a second test against another system. The results are shown in Figure 3.
FIGURE 3
In this scan different ports are open. The system is guessed to be ‘Microsoft Windows 2000|XP’. It is
Windows XP running SP3.
Let’s look at what is going on in the background of the first scan shown in Figure 2.
Initially NMAP is performing a TCP Stealth Scan. In this instance the OS Detection Probes start at Packet
2032 in Figure 4.
53
FIGURE 4
The Sequence Generation starts at Packet 2032/2033 and the sixth one is at Packet 2047/2048. Notice that
each one is sent twice and the next packet in the probe is sent 100 ms later.
The ICMP Packets are sent at 2050-2053. The two packets are duplicated making a total of four.
The six probes for the TCP are on lines 2059, 2060, 2061, 2063, 2065 and 2067.
These are the probes that are used to determine the OS of the target system.
I hope this helps you to understand how to update the NMAP OS Detection Database and perform a scan on
systems. Be aware that you can run the scan on systems which are not on your network, so the scan can be
performed on systems on the Internet.
54
13. Do the following using NS2 Simulator
i. NS2 Simulator-Introduction
NS2 stands for Network Simulator Version 2. It is an open-source event-driven simulator designed
specifically for research in computer communication networks.
Features of NS2
2. It provides substantial support to simulate bunch of protocols like TCP, FTP, UDP, https and DSR.
Basic Architecture
NS2 consists of two key languages: C++ and Object-oriented Tool Command Language (OTcl). While the
C++ defines the internal mechanism (i.e., a backend) of the simulation objects, the OTcl sets up
simulation by assembling and configuring the objects as well as scheduling discrete events. The C++ and
the OTcl are linked together using TclCL
55
ii. Simulate to Find the Number of Packets Dropped
/* Letter S is capital */
/* nf – nam file */
proc finish { } { /* provide space b/w proc and finish and all are in small case */ global ns nf tf
$ns duplex-link $n0 $n2 200Mb 10ms DropTail /*Letter M is capital Mb*/
$ns duplex-link $n1 $n2 100Mb 5ms DropTail /*D and T are capital*/ $ns duplex-link $n2 $n3 1Mb 1000ms
DropTail
56
set udp0 [new Agent/UDP] /* Letters A,U,D and P are capital */
57
$ns attach-agent $n0 $udp0
58
$ns run
59
AWK file (Open a new editor using “vi command” and write awk file and save with “.awk” extension)
BEGIN { c=0;
If ($1= ="d")
{ c+
+;
printf("%s\t%s\n",$5,$11);
END{
Event type, Event time, From Node, Source Node, Packet Type, Packet Size, Flags
60
(indicated by --------- ), Flow ID, Source address, Destination address, Sequence ID,
Packet ID
61
Output
63
set n1 [$ns
node]
64
$self instvar node_
puts "node [$node_ id] received answer from $from with round trip time $rtt msec"
65
}
# please provide space between $node_ and id. No space between $ and from. No #space between and $ and
rtt */
66
$ns at 2.1 "$p1 send"
67
$ns at 2.3 "$p1 send"
68
$ns at 2.2 "$p3 send"
69
$ns at 2.3 "$p3 send"
$ns run
AWK file (Open a new editor using “vi command” and write awk file and save with “.awk” extension)
BEGIN{
drop=0;
if($1= ="d" )
drop++;
} END{
70
i) Here “ns” indicates network simulator. We get the topology shown in
the snapshot.
ii) Now press the play button in the simulation window and the simulation will
71
begins.
6) After simulation is completed run awk file to see the output ,
[root@localhost~]# awk –f lab2.awk lab2.tr
7) To see the trace file contents open the file as ,
[root@localhost~]# vi lab2.tr
Topology
OUTPUT
72
Note:
Vary the bandwidth and queue size between the nodes n0-n2 , n2-n4. n6-n2 and n2- n5 and see the number
of packets dropped at the nodes.
73
$n5 color "blue"
$n5 l
74
abel "dest1"
$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/DropTail Mac/802_3
75
$tcp0 trace cwnd_ /* must put underscore ( _ ) after cwnd and no space between them*/
close $nf
$ns at 16 "finish"
$ns run
AWK file (Open a new editor using “vi command” and write awk file and
save with “.awk” extension)
BEGIN {
}
{
if($6= ="cwnd_") /* don‘t leave space after writing cwnd_ */ printf("%f\t%f\t\n",
$1,$7); /* you must put \n in printf */ }
END {
}
Steps for execution
1) Open vi editor and type program. Program name should have the extension ― .tcl
‖ [root@localhost ~]# vi lab3.tcl
2) Save the program by pressing “ESC key” first, followed by “Shift and :”
76
keys simultaneously and type “wq” and press Enter key.
3) Open vi editor and type awk program. Program name should have the extension
77
―.awk ‖
78
4) Save the program by pressing “ESC key” first, followed by
“Shift and :” keys simultaneously and type “wq” and press
Enter key.
5) Run the simulation program
[root@localhost~]# ns lab3.tcl
8) After simulation is completed run awk file to see the output ,
i. [root@localhost~]# awk –f lab3.awk file1.tr > a1
ii. [root@localhost~]# awk –f lab3.awk file2.tr > a2
iii. [root@localhost~]# xgraph a1 a2
9) Here we are using the congestion window trace files i.e.
file1.tr and file2.tr and we are redirecting the contents of
those files to new files say a1 and a2 using output
redirection operator (>).
10) To see the trace file contents open the file as ,
[root@localhost~]# vi lab3.tr
Topology
Output
79
V.Simulate to Determine the Performance with respect to Transmission of Packets
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-topoInstance $topo \
$n0 set X_ 50
$n0 set Y_ 50
80
$n0 set Z_ 0
$n1 set Z_ 0
$n2 set Z_ 0
81
$ns at 100 "$n1 setdest 550 550 15"
$ns flush-trace
exec nam
lab4.nam &
$ns run
AWK file (Open a new editor using “vi command” and write awk
file and save with “.awk” extension)
BEGIN{
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{
if($1= ="r"&& $3= ="_1_" && $4= ="AGT")
{
count1++ pack1=pack1+
$8 time1=$2
}
if($1= ="r" && $3= ="_2_" && $4= ="AGT")
{
count2++ pack2=pack2+
$8
time2=$2
}
}
END{
printf("The Throughput from n0 to n1: %f Mbps \n‖,
((count1*pack1*8)/(time1*1000000))); printf("The Throughput from n1 to
n2: %f Mbps", ((count2*pack2*8)/(time2*1000000))); }
Steps for execution
82
1) Open vi editor and type program. Program name should have the extension ― .tcl
‖ [root@localhost ~]# vi lab4.tcl
2) Save the program by pressing “ESC key” first, followed by
“Shift and :” keys simultaneously and type “wq” and press Enter
key.
3) Open vi editor and type awk program. Program name should have the extension
―.awk ‖
Trace file
83
Here “M” indicates mobile nodes, “AGT” indicates Agent Trace, “RTR”
indicates Router Trace
Output
84
85