Cnlab
Cnlab
Course Objectives:
Learn basic concepts of computer networking and acquire practical notions of
protocols with the emphasis on TCP/IP. A lab provides a practical approach to
Ethernet/Internet networking: networks are assembled, and experiments are
made to understand the layered architecture and how do some important
protocols work
Course Outcomes:
By the end of the course student will be able to
Know how reliable data communication is achieved through data link layer.
Suggest appropriate routing algorithm for the network.
Provide internet connection to the system and its installation.
Work on various network management tools
List of Experiments:
1. Study of Network devices in detail and connect the computers in Local Area
Network.
2. Write a Program to implement the data link layer farming methods such as
2.Hub:An Ethernet hub, active hub, network hub, repeater hub, hub or concentrator is a
device for connecting multiple twisted pair or fiber optic Ethernet devices together and
making them act as a single network segment. Hubs work at the physical layer(layer1)of
the OSI model. The device is a form of multiport repeater. Repeater hubs also
participate in collision detection ,forwarding a jam signal to all ports if it detects a
collision.
4. Bridge: A network bridge connects multiple network segments at the data link
layer(Layer 2) of the OSI model. In Ethernet networks, the term bridge formally means a
device that behaves according to the IEEE 802.1D standard. A bridge and switch are
very much alike; a switch being a bridge with numerous ports. Switch or Layer 2 switch
is often used inter changeably with bridge. Bridges can analyse incoming data packets
to determine if the bridge is able to send the given packet to another segment of the
network.
6.GateWay:In a communications network, a network node equipped for interfacing with another
network that uses different protocols.
A gateway may contain devices such as protocol translators, impedance matching devices, rate
converters, fault isolators, or signal translators as necessary to provide system interoperability.
It also requires the establishment of mutually acceptable administrative procedures between
both networks.
A protocol translation /mapping gate way inter connects networks with different network
protocol technologies by performing there required protocol conversions.
On the host computer, follow these steps to share the Internet connection:
8.Ifyouaresharingadial-pInternetconnection,selecttheEstablishadial-
upconnectionwhenever a computer on my network attempts to access
theInternetcheckboxifyouwanttopermityourcomputertoautomaticallycon
necttotheInternet.
10.ClickYes.
The connection to the Internet is shared to other computers on the local
area network(LAN).
Aim:
Implement the data link layer framing methods such as Bit Stuffing.
Theory
Security and Error detection are the most prominent features that are to be provided by any
application which transfers data from one end to the other end. One of such a mechanism in
tracking errors which may add up to the original data during transfer is known as Stuffing. It is of two
types namely Bit Stuffing and the other Character Stuffing. Coming to the Bit Stuffing, 01111110 is
appended within the original data while transfer of it. The following program describes how it is
stuffed at the sender end and de-stuffed at the reciever end.
Program:
Aim: Implement the data link layer framing methods such as Bit Stuffing.
#include<stdio.h>
#include<string.h>
int main()
{
int a[20],b[30],i,j,k=0,count,n;
printf("Enter frame size (Example: 8):");
scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0; count=1; j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
Output:
Theory
Coming to the Character Stuffing, DLESTX (Data Link EscapeStart of TeXt) and
DLEETX (Data Link EscapeEnd of TeXt)are used to denote start and end of
character data with some constraints imposed on repetition of charaters as
shown in the program below clearly.
Program
#include<stdio.h>
#include<string.h>
int main( )
{
char sdel[100]="DLESTX -",data[100] =" ",sdata[100]=" " ,ch;
int i=0,j=0,k;
printf("\n Enter the message: ");
scanf("%s",data);
for(k=0;data[k]!='\0';k++)
{
if(data[k]>='a'&& data[k]<='z')
{
data[k]=data[k]-32;
}
}
printf("\n Original message:%s",data);
if(strlen(data)<=3)
{
strcat(sdata,sdel);
strcat(sdata,data);
strcat(sdata," -DLETX");
printf("\n Message after character stuffing is : %s",sdata);
}
else
OUTPUT
(1) Enter the message: radle
The actual procedure which yields the checksum, given a data input is called a checksum
function or checksum algorithm.
Checksum Algorithm
#include<stdio.h>
#include<string.h>
int main()
{
char a[20],b[20];
char sum[20],complement[20];
int i,length;
if(strlen(a)==strlen(b))
{
length = strlen(a);
char carry='0';
for(i=length-1;i>=0;i--)
{
if(a[i]=='0' && b[i]=='0' && carry=='0')
{
sum[i]='0';
carry='0';
}
else if(a[i]=='0' && b[i]=='0' && carry=='1')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='0')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='0' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='0')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='1')
{
sum[i]='1';
carry='1';
}
else
break;
}
printf("\nSum=%c%s",carry,sum);
for(i=0;i<length;i++)
{
if(sum[i]=='0')
complement[i]='1';
else
complement[i]='0';
}
if(carry=='1')
carry='0';
else
carry='1';
printf("\nChecksum=%c%s",carry,complement);
}
else {
printf("\nWrong input strings");
Out Put
Enter first binary string
101101
Enter second binary string
110010
Sum=1011111
Checksum=0100000
Hamming code uses redundant bits (extra bits) which are calculated according to the
below formula:-
r
2 ≥ m+r+1
Where r is the number of redundant bits required and m is the number of data bits.
These redundant bits are then added to the original data for the
calculation of error at receiver’s end.
Respective index parity is calculated for r1, r2, r3, r4 and so on.
1. Easy to encode and decode data at both sender and receiver end.
2. Easy to implement.
Disadvantages of Hamming Code
#include <stdio.h>
#include <math.h>
int input[32];
int code[32];
int ham_calc(int,int);
void main()
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&input[i]);
i=0;
while(n>(int)pow(2,i)-(i+1))
p_n++;
i++;
c_l = p_n + n;
j=k=0;
for(i=0;i<c_l;i++)
if(i==((int)pow(2,k)-1))
code[i]=0;
k++;
else
code[i]=input[j];
j++;
for(i=0;i<p_n;i++)
code[position-1]=value;
for(i=0;i<c_l;i++)
printf("%d",code[i]);
printf("\n");
for(i=0;i<c_l;i++)
scanf("%d",&code[i]);
int error_pos = 0;
for(i=0;i<p_n;i++)
if(value != 0)
error_pos+=position;
if(error_pos == 1)
else
int count=0,i,j;
i=position-1;
while(i<c_l)
for(j=i;j<i+position;j++)
if(code[j] == 1)
count++;
if(count%2 == 0)
return 0;
else
return 1;
Out put:-
Please enter the length of the Data Word: 8
Please enter the Data Word:
1
1
0
1
0
0
1
1
#include<stdio.h>
#include<conio.h>
void main()
{
int f[150],g[150],r[150],t[150],i,j,k,m,n,c=0;
OUTPUT 1:
Enter f: 24
111100001010101010000000
Enter g: 12
1 10000001 1 1 1
check sumis:010000001 10
BCS : 111100001010101010000000 01000000110
Enter received data: 1 1 1 100001010101010000000 0100 0000 1 10
Check sum is; 00000000000 NO error
OUTPUT 2:
Enter f: 24
101010101111111 100000000
Enter g: 16
11000000000000101
check sumis:001000001000110
BCS: 101010101111111100000000 001000001000110
Enter received data : 1111111111111111 111111111111 1111111111
Checksum is: 110110011001101 Error
#include<stdio.h>
int main()
{
int windowsize,sent=0,ack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}
if(ack == windowsize)
break;
else
sent = ack;
}
return 0;
}
Output
enter window size
8
Frame 0 has been transmitted.
Frame 1 has been transmitted.
Frame 2 has been transmitted.
Frame 3 has been transmitted.
Frame 4 has been transmitted.
Frame 5 has been transmitted.
Frame 6 has been transmitted.
Frame 7 has been transmitted.
1. Selective Repeat: Sender transmits only that frame which is erroneous or is lost.
2. Go back n: Sender transmits all frames present in the window that occurs after the error
bit including error bit also.
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
printf("\nWith sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)\n\n");
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]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}
if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;
}
OUTPUT
#include<conio.h>
#include<stdlib.h>
int main()
int i,j,nframes,x,x1=10,x2;
scanf("%d",&nframes);
i=1;
j=1;
while(nframes>0)
x=rand()%10;
if(x%10==0)
for(x2=1;x2<2;x2++)
sleep(x2);
x=rand()%10;
}
printf("\n ack for frame %d\n",j);
nframes=nframes-1;
i++;
j++;
output:
no.of frames : 5
sending frams: 1
sending frams: 2
sending frams: 3
sending frams: 4
sending frams: 5
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,packets[10],content=0,newcontent,time,clk,bcktsize,oprate;
for(i=0;i<5;i++)
{
packets[i]=rand()%10;
if(packets[i]==0) --i;
}
printf("\n Enter output rate of the bucket: \n");
scanf("%d",&oprate);
printf("\n Enter Bucketsize\n");
scanf("%d",&bcktsize);
for(i=0;i<5;++i)
{
if((packets[i]+content)>bcktsize)
{
if(packets[i]>bcktsize)
printf("\n Incoming packet size %d greater than the size of the bucket\n",packets[i]);
OUTPUT
#define MAX 10
int main()
int G[MAX][MAX],i,j,n,u;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
for(i=0;i<n;i++)
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
mindistance=distance[i];
nextnode=i;
visited[nextnode]=1;
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
count++;
for(i=0;i<n;i++)
if(i!=startnode)
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
#include<stdio.h>
struct node
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
int costmat[20][20];
int nodes,i,j,k,count=0;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].from[j]=j;
do
count=0;
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[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<nodes;i++)
for(j=0;j<nodes;j++)
printf("\n\n");
#include <stdio.h>
int p,q,u,v,n;
int min = 99,mincost=0;
int t[50][2],i,j;
int parent[50],edge[50][50];
void sunion(int I, int m)
{
parent[I]=m;
}
int find(int I)
{
if(parent[I]>0)
I=parent[I];
return I;
}
int main()
{
printf("Enter the number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%c\t",65+i);
parent[i]=-1;
}
printf("\n");
for(i=0;i<n;i++)
{
output:
13. Wireshark
What is Wireshark?
Wireshark is a packet sniffer and analysis tool. It captures network traffic from
ethernet, Bluetooth, wireless (IEEE.802.11), token ring, and frame relay connections,
among others, and stores that data for offline analysis.
Editor’s Note: A “packet” is a single message from any network protocol (e.g., TCP,
DNS, etc.).
Wireshark comes in two options for Windows: 32-bit and 64-bit. Pick the correct
version for your OS; the current release is 3.0.3 as of this writing.
When you open Wireshark, you see a screen showing you a list of all the network connections you can monitor. You also have a
capture filter field to only capture the network traffic you want to see.
During the capture, Wireshark will show you the packets captured in real-time.
Wireshark shows you three different panes for inspecting packet data. The Packet List, the top pane, lists all the packets in the capture.
When you click on a packet, the other two panes change to show you the details about the selected packet. You can also tell if the
packet is part of a conversation. Here are details about each column in the top pane:
No.: This is the number order of the packet captured. The bracket indicates that this packet is part of a conversation.
Time: This column shows how long after you started the capture this particular packet was captured. You can change this
value in the Settings menu to display a different option.
Source: This is the address of the system that sent the packet.
Destination: This is the address of the packet destination.
Protocol: This is the type of packet. For example: TCP, DNS, DHCPv6, or ARP.
Length: This column shows you the packet’s length, measured in bytes.
Info: This column shows you more information about the packet contents, which will vary depending on the type of packet.
Packet Details, the middle pane, shows you as much readable information about the packet as possible, depending on the packet
type. You can right-click and create filters based on the highlighted text in this field.
The bottom pane, Packet Bytes, displays the packet exactly as it was captured in hexadecimal.
When looking at a packet that is part of a conversation, you can right-click the packet and select Follow to see only the packets that are
part of that conversation.
Wireshark filters
Some of the best features of Wireshark are the capture filters and display filters. Filters allow you to view the capture the way
you need to see it to troubleshoot the issues at hand. Below are several filters to get you started.
host IP-address: This filter limits the captured traffic to and from the IP address
This filter shows packets sent from one computer (ip.src) to another (ip.dst). You can
also use ip.addr to show packets to and from that IP. Other filters include:
tcp.port eq 25: This filter will show you all traffic on port 25, which is usually SMTP
traffic
icmp: This filter will show you only ICMP traffic in the capture, most likely they are
pings
ip.addr != IP_address: This filter shows you all traffic except the traffic to or from the
specified computer
Analysts even build filters to detect specific attacks, like this filter used to detect
the Sasser worm:
ls_ads.opnum==0x09
Nmap is Linux command-line tool for network exploration and security auditing.
This tool is generally used by hackers and cyber security enthusiasts and even by network
and system administrators.
It is used for the following purposes:
a. Real time information of a network
b. Detailed information of all the IPs activated on your network
c. Number of ports open in a network
d. Provide the list of live hosts
e. Port, OS and Host scanning
Installing Nmap Command
1. To scan a System with Hostname and IP address. First, Scan using Hostname
nmap www.geeksforgeeks.org
Detecting firewall settings can be useful during penetration testing and vulnerability scans.
To detect it we use “-sA” option.
This will provide you with information about firewall being active on the host.
It uses an ACK scan to receive the information.
6. To identify Hostnames sudo nmap -sL 103.76.228.244
We use “sL” option to find hostnames for the given host by completing a DNS query for each
one.
In addition to this “-n” command can be used to skip DNS resolution,
while the “-R” command can be used to always resolve DNS.
NS2 Simulator-Introduction
•ns-2 stands for NewworkSimulator version 2.
•ns-2: •Is a discrete event simulator for networking research
•Work at packet level.
•Provide substantial support to simulate bunch of protocols like TCP, UDP, FTP,
HTTP and DSR.
•Simulate wired and wireless network.
•Is primarily Unix based.
•Use TCL as its scripting language.
•ns-2 is a standard in research community
Aim: Simulate the transmission of ping messages over a network topology consisting of 6 nodes and
find the number of packets dropped due to congestion.
#The below code is used to connect between the ping agents to the node n0,
#n4 , n5 and n6. set ping0 [new Agent/Ping]
$ns attach-agent $n0 $ping0
set ping4 [new Agent/Ping]
$ns attach-agent $n4 $ping4
set ping5 [new Agent/Ping]
$ns attach-agent $n5 $ping5
set ping6 [new Agent/Ping]
$ns attach-agent $n6 $ping6
}
proc finish {}
{
global ns nf tf
exec nam lab2.nam &
$ns flush-trace
close $tf
close $nf
exit 0
}
#Schedule events
$ns at 0.1 "$ping0 send"
$ns at 0.2 "$ping0 send"
$ns at 0.3 "$ping0 send"
$ns at 0.4 "$ping0 send"
AWK:
BEGIN
{
count=0;
}
{
if($1=="d")
count++;
}
END
{
printf("The Total no of Packets Drop is :%d\n\n", count);
}