0% found this document useful (0 votes)
50 views50 pages

CNS Lab Programs

Uploaded by

daniel007work
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views50 pages

CNS Lab Programs

Uploaded by

daniel007work
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Computer Networks and Security Lab Manual

PART-A
1.1 Introduction to NS-2:
• Widely known as NS2, is simply an event driven simulation tool.
• Useful in studying the dynamic nature of communication networks.
• Simulation of wired as well as wireless network functions and protocols (e.g., routing
algorithms, TCP, UDP) can be done using NS2.
• In general, NS2 provides users with a way of specifying such network protocols and
simulating their corresponding behaviors.

Basic Architecture of NS2

1.2Tcl scripting
• Tcl is a general purpose scripting language. [Interpreter]
• Tcl runs on most of the platforms such as Unix, Windows, and Mac.
• The strength of Tcl is its simplicity.
• It is not necessary to declare a data type for variable prior to the usage.

1.2.1 Basics of TCL


Syntax: command arg1 arg2 arg3
 Hello World!
puts stdout{Hello, World!}
Hello, World!
 Variables Command Substitution
set a 5 set len [string length foobar]
set b $a set len [expr [string length foobar] + 9]

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

 Simple Arithmetic
o expr 7.2 / 4
 Procedures
proc Diag {a b} {
set c [expr sqrt($a * $a + $b * $b)]
o return $c }
puts “Diagonal of a 3, 4 right triangle is [Diag 3 4]”
Output: Diagonal of a 3, 4 right triangle is 5.0
 Loops
o while{$i < $n} { for {set i 0} {$i < $n} {incr i} {
... ...
}
1.2.2 Wired TCL Script Components
Steps to write the TCL script for wired network:
1. Create the event scheduler
2. Open new files & turn on the tracing
3. Create the nodes
4. Setup the links
5. Configure the traffic type (e.g., TCP, UDP, etc)
6. Set the time of traffic generation (e.g., CBR, FTP)
7. Terminate the simulation
1.3 NS Simulator Preliminaries
1. Initialization and termination aspects of the ns simulator.
2. Definition of network nodes, links, queues and topology.
3. Definition of agents and of applications.
4. The nam visualization tool.
5. Tracing and random variables.

1.4 Initialization and Termination of TCL Script in NS-2


Create the event scheduler
An ns simulation starts with the command

set ns [new Simulator]

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

Which is thus the first line in the tcl script? This line declares a new variable as using the set
command, you can call this variable as you wish, In general people declares it as ns because it is an
instance of the Simulator class, so an object the code[new Simulator] is indeed the installation of the
class Simulator using the reserved word new.
In order to have output files with data on the simulation (trace files) or files used for visualization
(nam files), we need to create the files using “open” command:
Open new files & turn on the tracing
#Open the Trace file
set tracefile1 [open out.tr w]

$ns trace-all $tracefile1

#Open the NAM trace file

set namfile [open out.nam w]

$ns namtrace-all $namfile


The above creates a dta trace file called “out.tr” and a nam visualization trace file called
“out.nam”.Within the tcl script,these files are not called explicitly by their names,but instead by
pointers that are declared above and called “tracefile1” and “namfile” respectively.Remark that they
begins with a # symbol.The second line open the file “out.tr” to be used for writing,declared with the
letter “w”.The third line uses a simulator method called trace-all that have as parameter the name of
the file where the traces will go.
The last line tells the simulator to record all simulation traces in NAM input format.It also
gives the file name that the trace will be written to later by the command $ns flush-trace.In our
case,this will be the file pointed at by the pointer “$namfile”, i.e the file “out.tr”.

The termination of the program is done using a “finish” procedure.


#Define a ‘finish’ procedure

Proc finish { } {

global ns tracefile1 namfile

$ns flush-trace

Close $tracefile1

Close $namfile

Exec nam out.nam &

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

The word proc declares a procedure in this case called finish and without arguments. The
word global is used to tell that we are using variables declared outside the procedure. The simulator
method “flush-trace” will dump the traces on the respective files. The tcl command “close” closes
the trace files defined before and exec executes the nam program for visualization. The command exit
will ends the application and return the number 0 as status to the system. Zero is the default for a
clean exit. Other values can be used to say that is a exit because something fails.
At the end of ns program we should call the procedure “finish” and specify at what time the
termination should occur. For example,

$ns at 125.0 “finish”

will be used to call “finish” at time 125sec.Indeed,the at method of the simulator allows us to
schedule events explicitly.
The simulation can then begin using the command

$ns run

Definition of a network of links and nodes


The way to define a node is
set n0 [$ns node]
We created a node that is printed by the variable n0. When we shall refer to that node in the script we
shall thus write $n0.
Once we define several nodes, we can define the links that connect them. An example of a
definition of a link is:

$ns duplex-link $n0 $n2 10Mb 10ms DropTail

Which means that $n0 and $n2 are connected using a bi-directional link that has 10ms of
propagation delay and a capacity of 10Mb per sec for each direction.
To define a directional link instead of a bi-directional one, we should replace “duplex-link” by
“simplex-link”.
In NS, an output queue of a node is implemented as a part of each link whose input is that
node. The definition of the link then includes the way to handle overflow at that queue.In our case, if
the buffer capacity of the output queue is exceeded then the last packet to arrive is dropped. Many
alternative options exist, such as the RED (Random Early Discard) mechanism, the FQ (Fair
Queuing), the DRR (Deficit Round Robin), the stochastic Fair Queuing (SFQ) and the CBQ (which
including a priority and a round-robin scheduler).
In ns, an output queue of a node is implemented as a part of each link whose input is that
node. We should also define the buffer capacity of the queue related to each link. An example would
be:

#set Queue Size of link (n0-n2) to 20

$ns queue-limit $n0 $n2 20


Dept of AIML , BNMIT 2023-24
Computer Networks and Security Lab Manual

Agents and Applications


We need to define routing (sources, destinations) the agents (protocols) the application that
use them.
FTP over TCP
TCP is a dynamic reliable congestion control protocol. It uses Acknowledgements created by
the destination to know whether packets are well received.
There are number variants of the TCP protocol, such as Tahoe, Reno, NewReno, Vegas. The
type of agent appears in the first line:

set tcp [new Agent/TCP]

The command $ns attach-agent $n0 $tcp defines the source node of the tcp connection.
The command
set sink [new Agent /TCPSink]

Defines the behavior of the destination node of TCP and assigns to it a pointer called sink.
#Setup a UDP connection
set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n5 $null

$ns connect $udp $null


#setup a CBR over UDP connection

set cbr [new


Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set packetsize_ 100

$cbr set rate_ 0.01Mb

$cbr set random_ false


Above shows the definition of a CBR application using a UDP agent
The command $ns attach-agent $n4 $sink defines the destination node. The command $ns
connect $tcp $sink finally makes the TCP connection between the source and destination nodes.
TCP has many parameters with initial fixed defaults values that can be changed if mentioned
explicitly. For example, the default TCP packet size has a size of 1000bytes.This can be changed to
another value, say 552bytes, using the command

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$tcp set packetSize_ 552.


When we have several flows, we may wish to distinguish them so that we can identify them
with different colors in the visualization part. This is done by the command $tcp set fid_ 1 that
assigns to the TCP connection a flow identification of “1”.We shall later give the flow identification
of “2” to the UDP connection.
Application layer protocols: CBR over UDP
A UDP source and destination is defined in a similar way as in the case of TCP.
Instead of defining the rate in the command $cbr set rate_ 0.01Mb, one can define the time
interval between transmission of packets using the command.

$cbr set interval_ 0.005

The packet size can be set to some value using

$cbr set packetSize_ <packet size>


Scheduling Events
NS is a discrete event based simulation. The tcp script defines when event should occur. The
initializing command set ns [new Simulator] creates an event scheduler, and events are then scheduled
using the format:
$ns at <time> <event>

The scheduler is started when running ns that is through the command $ns run.
The beginning and end of the FTP and CBR application can be done through the following command

$ns at 0.1 “$cbr start”

$ns at 1.0 “ $ftp start”

$ns at 124.0 “$ftp stop”

$ns at 124.5 “$cbr stop”

1.5 Structure of Trace Files


When tracing into an output ASCII file, the trace is organized in 12 fields as follows in fig
shown below, The meaning of the fields are:

Even Time From To PKT PKT Flags Fi Src Dest Seq Pkt
t Node Node Type Size d Addr Add Num id
r

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

1. The first field is the event type. It is given by one of four possible symbols r, +, -, d which
correspond respectively to receive (at the output of the link), enqueued, dequeued and dropped.
2. The second field gives the time at which the event occurs.
3. Gives the input node of the link at which the event occurs.
4. Gives the output node of the link at which the event occurs.
5. Gives the packet type (eg CBR or TCP)
6. Gives the packet size
7. Some flags
8. This is the flow id (fid) of IPv6 that a user can set for each flow at the input OTcl script one
can further use this field for analysis purposes; it is also used when specifying stream color for the
NAM display.
9. This is the source address given in the form of “node.port”.
10. This is the destination address, given in the same form.
11. This is the network layer protocol’s packet sequence number. Even though UDP
implementations in a real network do not use sequence number, ns keeps track of UDP packet
sequence number for analysis purposes
12. The last field shows the unique id of the packet.

1.6 XGRAPH
The xgraph program draws a graph on an x-display given data read from either data file or
from standard input if no files are specified. It can display upto 64 independent data sets using
different colors and line styles for each set. It annotates the graph with a title, axis labels, grid lines or
tick marks, grid labels and a legend.
Syntax:
Xgraph [options] file-name
Options are listed here
/-bd <color> (Border)
This specifies the border color of the xgraph window.
/-bg <color> (Background)
This specifies the background color of the xgraph window.
/-fg<color> (Foreground)
This specifies the foreground color of the xgraph window.
/-lf <fontname> (LabelFont)
All axis labels and grid labels are drawn using this font.
/-t<string> (Title Text)

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

This string is centered at the top of the graph.


/-x <unit name> (XunitText)
This is the unit name for the x-axis. Its default is “X”.
/-y <unit name> (YunitText)
This is the unit name for the y-axis. Its default is “Y”.
1.7 Awk- An Advanced
awk is a programmable, pattern-matching, and processing tool available in UNIX. It works
equally well with text and numbers.
awk is not just a command, but a programming language too. In other words, awk utility is a
pattern scanning and processing language. It searches one or more files to see if they contain lines that
match specified patterns and then perform associated actions, such as writing the line to the standard
output or incrementing a counter each time it finds a match.
Syntax:
awk option ‘selection_criteria {action}’ file(s)

Here, selection_criteria filters input and select lines for the action component to act upon. The
selection_criteria is enclosed within single quotes and the action within the curly braces. Both the
selection_criteria and action forms an awk program.
Example: $ awk ‘/manager/ {print}’ emp.lst
Variables
Awk allows the user to use variables of their choice. You can now print a serial number, using
the variable kount, and apply it those directors drawing a salary exceeding 6700:
$ awk –F”|” ‘$3 == “director” && $6 > 6700 {
count =count+1
printf “ %3f %20s %-12s %d\n”, count,$2,$3,$6 }’ empn.lst
THE –f OPTION: STORING awk PROGRAMS INA FILE
You should holds large awk programs in separate file and provide them with the awk
extension for easier identification. Let’s first store the previous program in the file empawk.awk:
$ cat empawk.awk
Observe that this time we haven’t used quotes to enclose the awk program. You can now use
awk with the –f filename option to obtain the same output:

Awk –F”|” –f empawk.awk empn.lst

THE BEGIN AND END SECTIONS


Awk statements are usually applied to all lines selected by the address, and if there are no
addresses, then they are applied to every line of input. But, if you have to print something before

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

processing the first line, for example, a heading, then the BEGIN section can be used gainfully.
Similarly, the end section useful in printing some totals after processing is over.
The BEGIN and END sections are optional and take the form
BEGIN {action}
END {action}
These two sections, when present, are delimited by the body of the awk program. You can use
them to print a suitable heading at the beginning and the average salary at the end.
BUILT-IN VARIABLES
Awk has several built-in variables. They are all assigned automatically, though it is also
possible for a user to reassign some of them. You have already used NR, which signifies the record
number of the current line. We’ll now have a brief look at some of the other variable.
The FS Variable: as stated elsewhere, awk uses a contiguous string of spaces as the default field
delimiter. FS redefines this field separator, which in the sample database happens to be the |. When
used at all, it must occur in the BEGIN section so that the body of the program knows its value before
it starts processing:
BEGIN {FS=”|”}
This is an alternative to the –F option which does the same thing.
The OFS Variable: when you used the print statement with comma-separated arguments, each
argument was separated from the other by a space. This is awk’s default output field separator, and
can reassigned using the variable OFS in the BEGIN section:
BEGIN { OFS=”~” }
When you reassign this variable with a ~ (tilde), awk will use this character for delimiting the print
arguments. This is a useful variable for creating lines with delimited fields.
The NF variable: NF comes in quite handy for cleaning up a database of lines that don’t contain the
right number of fields. By using it on a file, say emp.lst, you can locate those lines not having 6 fields,
and which have crept in due to faulty data entry:
$awk ‘BEGIN {FS = “|”}
NF! =6 {
Print “Record No “, NR, “has”, “fields”}’ empx.lst
The FILENAME Variable: FILENAME stores the name of the current file being processed. Like
grep and sed, awk can also handle multiple filenames in the command line. By default, awk doesn’t
print the filename, but you can instruct it to do so:
‘$6<4000 {print FILENAME, $0 }’
With FILENAME, you can device logic that does different things depending on the file that is
processed.
1.8 NS2 Installation

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

• NS2 is a free simulation tool.


• It runs on various platforms including UNIX (or Linux), Windows, and Mac systems.
• NS2 source codes are distributed in two forms: the all-in-one suite and the component-wise.
• ‘all-in-one’ package provides an “install” script which configures the NS2 environment and
creates NS2 executable file using the “make” utility.
1.8.1 NS-2 installation steps in Linux
 Go to Computer  File System  now paste the zip file “ns-allinone-2.34.tar.gz” into opt
folder.
 Now unzip the file by typing the following command
[root@localhost opt] # tar -xzvf ns-allinone-2.34.tar.gz
 After the files get extracted, we get ns-allinone-2.34 folder as well as zip file ns-
allinone-2.34.tar.gz
 [root@localhost opt] #
ns-allinone-2.34 ns-allinone-2.34.tar.gz
 Now go to ns-allinone-2.33 folder and install it
 [root@localhost opt] # cd ns-allinone-2.34
 [root@localhost ns-allinone-2.33] # ./install
 Once the installation is completed successfully we get certain pathnames in that terminal
which must be pasted in “.bash_profile” file.
 First minimize the terminal where installation is done and open a new terminal and open
the file “.bash_profile”
 [root@localhost ~] # vi .bash_profile
 When we open this file, we get a line in that file which is shown below
PATH=$PATH:$HOME/bin
To this line we must paste the path which is present in the previous terminal where ns was installed.
First put “:” then paste the path in-front of bin. That path is shown below.
“:/opt/ns-allinone-2.33/bin:/opt/ns-allinone-2.33/tcl8.4.18/unix:/opt/ns-allinone-2.33/tk8.4.18/
unix”.
 In the next line type “LD_LIBRARY_PATH=$LD_LIBRARY_PATH:” and paste the two
paths separated by “:” which are present in the previous terminal i.e Important notices
section (1)
“/opt/ns-allinone-2.33/otcl-1.13:/opt/ns-allinone-2.33/lib”
 In the next line type “TCL_LIBRARY=$TCL_LIBRARY:” and paste the path which is
present in previous terminal i.e Important Notices section (2)
“/opt/ns-allinone-2.33/tcl8.4.18/library”
 In the next line type “export LD_LIBRARY_PATH”
 In the next line type “export TCL_LIBRARY”

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

 The next two lines are already present the file “export PATH” and “unset USERNAME”
 Save the program ( ESC + shift : wq and press enter )
 Now in the terminal where we have opened .bash_profile file, type the following command
to check if path is updated correctly or not
 [root@localhost ~] # vi .bash_profile
 [root@localhost ~] # source .bash_profile
 If path is updated properly, then we will get the prompt as shown below
 [root@localhost ~] #
 Now open the previous terminal where you have installed ns
 [root@localhost ns-allinone-2.33] #
 Here we need to configure three packages “ns-2.33”, “nam-1.13” and “xgraph-12.1”
 First, configure “ns-2.33” package as shown below
 [root@localhost ns-allinone-2.33] # cd ns-2.33
 [root@localhost ns-2.33] # ./configure
 [root@localhost ns-2.33] # make clean
 [root@localhost ns-2.33] # make
 [root@localhost ns-2.33] # make install
 [root@localhost ns-2.33] # ns
 %
 If we get “%” symbol it indicates that ns-2.33 configuration was successful.
 Second, configure “nam-1.13” package as shown below
 [root@localhost ns-2.33] # cd . .
 [root@localhost ns-allinone-2.33] # cd nam-1.13
 [root@localhost nam-1.13] # ./configure
 [root@localhost nam-1.13] # make clean
 [root@localhost nam-1.13] # make
 [root@localhost nam-1.13] # make install
 [root@localhost nam-1.13] # ns
 %
 If we get “%” symbol it indicates that nam-1.13 configuration was successful.
 Third, configure “xgraph-12.1” package as shown below
 [root@localhost nam-1.13] # cd . .
 [root@localhost ns-allinone-2.33] # cd xgraph-12.1
 [root@localhost xgraph-12.1] # ./configure
 [root@localhost xgraph-12.1] # make clean
 [root@localhost xgraph-12.1] # make
 [root@localhost xgraph-12.1] # make install

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

 [root@localhost xgraph-12.1] # ns

1.Implement three nodes point – to – point network with duplex links between them.
Program
set ns [new Simulator]
set nf [open lab1.nam w]
$ns namtrace-all $nf
set tf [open lab1.tr w]
$ns trace-all $tf
proc finish {} {
global ns nftf
$ns flush-trace
close $nf
close $tf
exec nam lab1.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
#hello
$ns duplex-link $n0 $n2 10Mb 1ms DropTail
$ns duplex-link $n1 $n2 10Mb 1ms DropTail
$ns duplex-link $n2 $n3 10Mb 1ms DropTail
$ns duplex-link $n3 $n4 10Mb 1ms DropTail
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set udp0 [new Agent/UDP]

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns attach-agent $n1 $udp0


set null0 [new Agent/Null]
$ns attach-agent $n2 $null0
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$ns connect $tcp0 $sink0
$ns connect $udp0 $null0
$ns at 0.1 "$cbr0 start"
$ns at 0.2 "$ftp0 start"
$ns at 0.5 "finish"
$ns run
Output:

AWK FILE :-
BEGIN{
udp = 0;
tcp = 0;
}
{
if($1 == "r" && $5 == "cbr")
{

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

udp++;
}
else if($1 == "r" && $5 == "tcp")
{
tcp++;
}
}
END{
printf("the no of packets send by udp = %d\n",udp);
printf("the no of packets send by tcp = %d\n",tcp);
}
output:
tcp = 692 udp = 107

2- Implement the following topologies: Bus, Star, Ring


1-Bus
set ns [new Simulator]
#open new file for namtrace
set nf [open out.nam w]
$ns namtrace-all $nf

#open new file to log trace


set tf [open out.tr w]
$ns trace-all $tf

#body of finish procedure


proc finish {} {
global ns nftf
$ns flush-trace
close $nf
close $tf
exec namout.nam&
exit 0
}

#create nodes
set n0 [$ns node]
set n1 [$ns node]

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

set n2 [$ns node]


set n3 [$ns node]
set n4 [$ns node]

$ns color 1 Blue


$ns color 2 Red

set lan0 [$ns newLan "$n0 $n1 $n2 $n3 $n4" 0.5Mb 40ms LL Queue/DropTail
MAC/Csma/Cd Channel]

#create tcp agent between node0 & node1


set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
$tcp0 set class_ 1
set sink0 [new Agent/TCPSink]
$ns attach-agent $n4 $sink0
$ns connect $tcp0 $sink0
#create FTP application for tcp agent
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set rate_ 0.005Mb
$ftp0 set packetSize_ 500
#start ftp traffic
$ns at 0.1 "$ftp0 start"
#stop the simulator
$ns at 0.5 "finish"
#run the simulator
$ns run

2-Star
set ns [new Simulator]
#open new file for namtrace
set nf [open out.nam w]

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns namtrace-all $nf

#open new file to log trace


set tf [open out.tr w]
$ns trace-all $tf

#body of finish procedure


proc finish {} {
global ns nftf
$ns flush-trace
close $nf
close $tf
exec namout.nam&
exit 0
}

#create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

$ns color 1 Blue


$ns color 2 Red

#create links between nodes


$ns duplex-link $n0 $n5 10Mb 1ms DropTail
$ns duplex-link $n0 $n1 10Mb 1ms DropTail
$ns duplex-link $n0 $n2 10Mb 1ms DropTail
$ns duplex-link $n0 $n3 10Mb 1ms DropTail
$ns duplex-link $n0 $n4 10Mb 1ms DropTail

#Set Queue limit -default as 50 packets


$ns queue-limit $n0 $n1 50
$ns queue-limit $n0 $n2 50
$ns queue-limit $n0 $n3 50
$ns queue-limit $n0 $n4 50
$ns queue-limit $n0 $n5 50

#create tcp agent between node0 & node1


set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
$tcp0 set class_ 1

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

set sink0 [new Agent/TCPSink]


$ns attach-agent $n1 $sink0
$ns connect $tcp0 $sink0
#create FTP application for tcp agent
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set rate_ 0.005Mb
$ftp0 set packetSize_ 500

#create tcp agent between node1 & node2


set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
$tcp1 set class_ 2
set sink1 [new Agent/TCPSink]
$ns attach-agent $n2 $sink1
$ns connect $tcp1 $sink1
#create FTP application for tcp agent
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set rate_ 0.005Mb
$ftp1 set packetSize_ 500

#create udp agent between node2 and node3


set udp0 [new Agent/UDP]
$ns attach-agent $n2 $udp0
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
$ns connect $udp0 $null0

#create cbrappln for udp agent


set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
#$cbr0 set packetSize_ 500
#$cbr0 set rate_ 0.01Mb

#start ftp traffic


$ns at 0.1 "$ftp0 start"
$ns at 0.1 "$ftp1 start"
#start cbr traffic
$ns at 0.1 "$cbr0 start"
#stop the simulator
$ns at 0.5 "finish"

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

#run the simulator


$ns run

Output:

3-Ring:
set ns [new Simulator]
#open new file for namtrace
set nf [open out.nam w]
$ns namtrace-all $nf

#open new file to log trace


set tf [open out.tr w]
$ns trace-all $tf

#body of finish procedure


proc finish {} {
global ns nftf
$ns flush-trace
close $nf
close $tf
exec namout.nam&
exit 0
}

#create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns color 1 Blue


$ns color 2 Red

#create links between nodes


$ns duplex-link $n0 $n1 10Mb 1ms DropTail
$ns duplex-link $n1 $n2 10Mb 1ms DropTail
$ns duplex-link $n2 $n3 10Mb 1ms DropTail
$ns duplex-link $n3 $n4 10Mb 1ms DropTail
$ns duplex-link $n4 $n0 10Mb 1ms DropTail

#Set Queue limit -default as 50 packets


$ns queue-limit $n0 $n1 50
$ns queue-limit $n1 $n2 50
$ns queue-limit $n2 $n3 50
$ns queue-limit $n3 $n4 50
$ns queue-limit $n4 $n0 50

#create tcp agent between node0 & node1


set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
$tcp0 set class_ 1
set sink0 [new Agent/TCPSink]
$ns attach-agent $n1 $sink0
$ns connect $tcp0 $sink0
#create FTP application for tcp agent
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set rate_ 0.005Mb
$ftp0 set packetSize_ 500

#create tcp agent between node1 & node2


set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
$tcp1 set class_ 2
set sink1 [new Agent/TCPSink]
$ns attach-agent $n2 $sink1
$ns connect $tcp1 $sink1
#create FTP application for tcp agent
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set rate_ 0.005Mb
$ftp1 set packetSize_ 500

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

#create udp agent between node2 and node3


set udp0 [new Agent/UDP]
$ns attach-agent $n2 $udp0
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
$ns connect $udp0 $null0

#create cbrappln for udp agent


set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
#$cbr0 set packetSize_ 500
#$cbr0 set rate_ 0.01Mb

#start ftp traffic


$ns at 0.1 "$ftp0 start"
$ns at 0.1 "$ftp1 start"

#start cbr traffic


$ns at 0.1 "$cbr0 start"

#stop the simulator


$ns at 0.5 "finish"

#run the simulator


$ns run
Output:

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

3- Implement transmission of ping messages/trace route over a network topology


consisting of 6 nodes and find the number of packets dropped due to congestion.
Program
set ns [new Simulator]
set nf [open ping.nam w]
$ns namtrace-all $nf
set tf [open ping.tr w]
$ns trace-all $tf
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]
$ns duplex-link $n0 $n4 1005mbs 1ms DropTail
$ns duplex-link $n1 $n4 50mbs 1ms DropTail
$ns duplex-link $n2 $n4 2000mbs 1ms DropTail
$ns duplex-link $n3 $n4 200mbs 1ms DropTail
$ns duplex-link $n4 $n5 1mbs 1ms DropTail
$ns duplex-link $n6 $n4 2mbs 1ms DropTail
set p1 [new Agent/Ping]
$ns attach-agent $n0 $p1
set p2 [new Agent/Ping]
$ns attach-agent $n1 $p2
set p3 [new Agent/Ping]
$ns attach-agent $n2 $p3
set p4 [new Agent/Ping]
$ns attach-agent $n3 $p4
set p5 [new Agent/Ping]
$ns attach-agent $n4 $p5

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

set p6 [new Agent/Ping]


$ns attach-agent $n5 $p6
set p7 [new Agent/Ping]
$ns attach-agent $n6 $p7
$ns queue-limit $n0 $n4 1
$ns queue-limit $n2 $n4 1
$ns queue-limit $n4 $n5 1
Agent/Ping instprocrecv {from rtt} {
$self instvar node_
puts "node[$node_ id] received answer from $from with round trip time $rtt msec"
}
$ns connect $p1 $p2
$ns connect $p2 $p4
$ns connect $p3 $p6
proc finish {} {
global ns nftf
$ns flush-trace
close $nf
close $tf
exec namping.nam&
exit 0
}
$ns at 0.1 "$p1 send"
$ns at 0.2 "$p1 send"
$ns at 0.3 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.5 "$p1 send"
$ns at 0.6 "$p1 send"
$ns at 0.7 "$p1 send"
$ns at 0.8 "$p1 send"

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns at 0.9 "$p1 send"


$ns at 1.0 "$p1 send"
$ns at 1.1 "$p1 send"
$ns at 1.2 "$p1 send"
$ns at 1.3 "$p1 send"
$ns at 1.4 "$p1 send"
$ns at 1.5 "$p1 send"
$ns at 1.6 "$p1 send"
$ns at 1.7 "$p1 send"
$ns at 1.8 "$p1 send"
$ns at 1.9 "$p1 send"
$ns at 2.0 "$p1 send"
$ns at 2.1 "$p1 send"
$ns at 2.2 "$p1 send"
$ns at 2.3 "$p1 send"
$ns at 2.4 "$p1 send"
$ns at 2.5 "$p1 send"
$ns at 2.6 "$p1 send"
$ns at 2.7 "$p1 send"
$ns at 2.8 "$p1 send"
$ns at 2.9 "$p1 send"
$ns at 0.1 "$p3 send"
$ns at 0.2 "$p3 send"
$ns at 0.3 "$p3 send"
$ns at 0.4 "$p3 send"
$ns at 0.5 "$p3 send"
$ns at 0.6 "$p3 send"
$ns at 0.7 "$p3 send"
$ns at 0.8 "$p3 send"
$ns at 0.9 "$p3 send"

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns at 1.0 "$p3 send"


$ns at 1.1 "$p3 send"
$ns at 1.2 "$p3 send"
$ns at 1.3 "$p3 send"
$ns at 1.4 "$p3 send"
$ns at 1.5 "$p3 send"
$ns at 1.6 "$p3 send"
$ns at 1.7 "$p3 send"
$ns at 1.8 "$p3 send"
$ns at 1.9 "$p3 send"
$ns at 2.0 "$p3 send"
$ns at 2.1 "$p3 send"
$ns at 2.2 "$p3 send"
$ns at 2.3 "$p3 send"
$ns at 2.4 "$p3 send"
$ns at 2.5 "$p3 send"
$ns at 2.6 "$p3 send"
$ns at 2.7 "$p3 send"
$ns at 2.8 "$p3 send"
$ns at 2.9 "$p3 send"
$ns at 3.0 "finish"
$ns run
Output:

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

Awk file:-
BEGIN{
drop = 0;
}
{
if($1 == "d")
{
drop++;
}
printf("%d\t%d\n",$5,drop);
}
END{
printf("total no.of %s packet dropped due to congestion =%d\n",$5,drop);
}
output:-
total no.of ping packet dropped due to congestion = 58

4- Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.
Program:
set ns [new
Simulator]
set nf [open
eth.nam w]
$ns namtrace-all
$nf
set tf [open eth.tr
w]
$ns trace-all $tf
set n0 [$ns node]
$n0 color "red"

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$n0 label "src1"


set n1 [$ns node]
set n2 [$ns node]
$n2 color "red"
$n2 label "src2"
set n3 [$ns node]
$n3 color "magenta"
$n3 label "dest2"
set n4 [$ns node]
set n5 [$ns node]
$n5 color "magenta"
$n5 label "dest1"

$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/DropTail Mac/802_3
$ns duplex-link $n4 $n5 1Mb 1ms DropTail
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.0001
set sink5 [new Agent/TCPSink]
$ns attach-agent $n5 $sink5
$ns queue-limit $n4 $n5 2
$ns connect $tcp0 $sink5

Set tcp2[newAgent/TCP]
$ns attach-agent $n2 $tcp2
set ftp2 [new Application/FTP]

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ftp2 attach-agent $tcp2


$ftp0 set packetSize_ 600
$ftp0 set interval_ 0.001
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp2 $sink3

set file1 [open file1.tr w]


$tcp0 attach $file1
set file2 [open file2.tr w]
$tcp2 attach $file2
$tcp0 trace cwnd_
$tcp2 trace cwnd_
proc finish {} {
global ns nftf
$ns flush-trace
close $nf
close $tf
exec nameth.nam&
exit 0
}
$ns at 0.1 "$ftp0 start"
$ns at 5 "$ftp0 stop"
$ns at 7 "$ftp0 start"
$ns at 0.2 "$ftp2 start"
$ns at 8 "$ftp2 stop"
$ns at 14 "$ftp0 stop"
$ns at 10 "$ftp2 start"
$ns at 15 "$ftp2 stop"
$ns at 16 "finish"

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns run

eth.awk
BEGIN{
pkt = 0;
time = 0;
}
{
if($6 == "cwnd_")
{
printf("%d\t%d\n",$1,$7)
}
}
END{
}
Output:

5-Simulation of distance vector routing algorithm


Program
set ns [new Simulator]
set nf [open Lab9.nam w]

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns namtrace-all $nf


set tf [open Lab9.tr w]
$ns trace-all $tf
proc finish {} {
global ns nftf
$ns flush-trace
close $nf
close $tf
exec nam Lab9.nam &
exit 0
}
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4[$ns node]
set n5[$ns node]
set n6[$ns node]
set n7[$ns node]
set n8[$ns node]
set n9[$ns node]
setn10[$ns node]
set n11[$ns node]
set n12[$ns node]
set n13[$ns node]
set n14[$ns node]
set n15[$ns node]
$ns duplex-link $n1 $n2 10Mb 10ms DropTail
$ns duplex-link $n1 $n4 10Mb 10ms DropTail
$ns duplex-link $n2 $n3 10Mb 10ms DropTail
$ns duplex-link $n3 $n8 10Mb 10ms DropTail

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns duplex-link $n3 $n7 10Mb 10ms DropTail


$ns duplex-link $n8 $n9 10Mb 10ms DropTail
$ns duplex-link $n8 $n10 10Mb 10ms DropTail
$ns duplex-link $n10 $n11 10Mb 10ms DropTail
$ns duplex-link $n10 $n12 10Mb 10ms DropTail
$ns duplex-link $n12 $n13 10Mb 10ms DropTail
$ns duplex-link $n2 $n4 10Mb 10ms DropTail
$ns duplex-link $n4 $n5 10Mb 10ms DropTail
$ns duplex-link $n5 $n6 10Mb 10ms DropTail
$ns duplex-link $n5 $n15 10Mb 10ms DropTail
$ns duplex-link $n6 $n14 10Mb 10ms DropTail
$ns duplex-link $n4 $n7 10Mb 10ms DropTail
$ns duplex-link $n7 $n9 10Mb 10ms DropTail
$ns duplex-link $n9 $n11 10Mb 10ms DropTail
$ns duplex-link $n11 $n13 10Mb 10ms DropTail
set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set sink11 [new Agent/TCPSink]
$ns attach-agent $n11 $sink11
$ns connect $tcp1 $sink11
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns rtproto DV
$ns rtmodel-at 0.5 down $n1 $n4
$ns rtmodel-at 5.0 up $n1 $n4
$ns at 0.1 "$ftp1 start"
$ns at 20.0 "finish"
$ns run
Output:

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

6-Simulation of link state routing algorithm


Program
set ns [new Simulator]
set nf [open Lab9.nam w]
$ns namtrace-all $nf
set tf [open Lab9.tr w]
$ns trace-all $tf
proc finish {} {
global ns nftf
$ns flush-trace
close $nf
close $tf
exec nam Lab9.nam &
exit 0
}
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4[$ns node]
set n5[$ns node]
set n6[$ns node]

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

set n7[$ns node]


set n8[$ns node]
set n9[$ns node]
setn10[$ns node]
set n11[$ns node]
set n12[$ns node]
set n13[$ns node]
set n14[$ns node]
set n15[$ns node]
$ns duplex-link $n1 $n2 10Mb 10ms DropTail
$ns duplex-link $n1 $n4 10Mb 10ms DropTail
$ns duplex-link $n2 $n3 10Mb 10ms DropTail
$ns duplex-link $n3 $n8 10Mb 10ms DropTail
$ns duplex-link $n3 $n7 10Mb 10ms DropTail
$ns duplex-link $n8 $n9 10Mb 10ms DropTail
$ns duplex-link $n8 $n10 10Mb 10ms DropTail
$ns duplex-link $n10 $n11 10Mb 10ms DropTail
$ns duplex-link $n10 $n12 10Mb 10ms DropTail
$ns duplex-link $n12 $n13 10Mb 10ms DropTail
$ns duplex-link $n2 $n4 10Mb 10ms DropTail
$ns duplex-link $n4 $n5 10Mb 10ms DropTail
$ns duplex-link $n5 $n6 10Mb 10ms DropTail
$ns duplex-link $n5 $n15 10Mb 10ms DropTail
$ns duplex-link $n6 $n14 10Mb 10ms DropTail
$ns duplex-link $n4 $n7 10Mb 10ms DropTail
$ns duplex-link $n7 $n9 10Mb 10ms DropTail
$ns duplex-link $n9 $n11 10Mb 10ms DropTail
$ns duplex-link $n11 $n13 10Mb 10ms DropTail
set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

set sink11 [new Agent/TCPSink]


$ns attach-agent $n11 $sink11
$ns connect $tcp1 $sink11
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns rtprotoLS
$ns rtmodel-at 0.5 down $n1 $n4
$ns rtmodel-at 5.0 up $n1 $n4
$ns at 0.1 "$ftp1 start"
$ns at 20.0 "finish"
$ns run
Output:

7-Implement Transport Control Protocol in sensor network


Program:
set ns [new Simulator]
$ns color 1 Blue
$ns color 2 Red
set file1 [open out.tr w]
$ns trace-all $file1

set file2 [open out.nam w]

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns namtrace-all $file2

proc finish {} {
global ns file1 file2
$ns flush-trace
close $file1
close $file2
exec namout.nam&
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

$ns duplex-link $n0 $n1 10Mb 1ms DropTail


$ns duplex-link $n1 $n2 10Mb 1ms DropTail
$ns duplex-link $n2 $n3 10Mb 1ms DropTail
$ns duplex-link $n3 $n4 10Mb 1ms DropTail
$ns duplex-link $n4 $n5 10Mb 1ms DropTail
$ns duplex-link $n5 $n0 10Mb 1ms DropTail
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n5 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$tcp set packetSize_ 552

set ftp [new Application/FTP]


$ftp attach-agent $tcp
$ns at 1.0 "$ftp start"
$ns at 1.5 "$ftp stop"
$ns at 5.0 "finish"
$ns run

Output:

8-Implement User Datagram protocol in sensor network


set ns [new Simulator]
set file1 [open out.tr w]
$ns trace-all $file1

set file2 [open out.nam w]


$ns namtrace-all $file2

proc finish {} {
global ns file1 file2
$ns flush-trace

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

close $file1
close $file2
exec namout.nam&
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

$ns duplex-link $n0 $n1 10Mb 1ms DropTail


$ns duplex-link $n1 $n2 10Mb 1ms DropTail
$ns duplex-link $n2 $n3 10Mb 1ms DropTail
$ns duplex-link $n3 $n4 10Mb 1ms DropTail
$ns duplex-link $n4 $n5 10Mb 1ms DropTail
$ns duplex-link $n5 $n0 10Mb 1ms DropTail

set udp [new Agent/UDP]


$ns attach-agent $n0 $udp
set null [new Agent/Null]
$ns attach-agent $n5 $null
$ns connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

#$ns at 0.1 "$udp start"


$ns at 0.2 "$cbr start"
$ns at 5.0 "finish"

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns run

Output:

9-Stop and Wait protocol


set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]

$ns at 0.0 "$n0 label Sender"


$ns at 0.0 "$n1 label Receiver"

set nf [open stop.nam w]


$ns namtrace-all $nf
set tf [open stop.tr w]
$ns trace-all $tf

$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail


$ns duplex-link-op $n0 $n1 orient right

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns queue-limit $n0 $n1 10

Agent/TCP set nam_tracevar_ true


set tcp [new Agent/TCP]
$tcp set window_ 1
$tcp set maxcwnd_ 1

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]


$ns attach-agent $n1 $sink

$ns connect $tcp $sink

set ftp [new Application/FTP]


$ftp attach-agent $tcp

$ns add-agent-trace $tcptcp


$ns monitor-agent-trace $tcp

$tcptracevarcwnd_

$ns at 0.1 "$ftp start"


$ns at 3.0 "$ns detach-agent $n0 $tcp; $ns detach-agent $n1 $sink"
$ns at 3.5 "finish"

$ns at 0.0 "$ns trace-annotate \"stop and wait operations\""


$ns at 0.05 "$ns trace-annotate \" FTP starts at 0.1\""
$ns at 0.11 "$ns trace-annotate \" Send pkt_ 0\""
$ns at 0.35 "$ns trace-annotate \" Rec ack_ 0\""

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns at 0.56 "$ns trace-annotate \" Send pkt_ 1\""


$ns at 0.79 "$ns trace-annotate \" Rec ack_ 1\""
$ns at 0.99 "$ns trace-annotate \" Send pkt_ 2\""
$ns at 1.23 "$ns trace-annotate \" Rec ack_ 2\""
$ns at 1.43 "$ns trace-annotate \" Send pkt_ 3\""
$ns at 1.67 "$ns trace-annotate \" Rec ack_ 3\""
$ns at 1.88 "$ns trace-annotate \" Send pkt_ 4\""
$ns at 2.11 "$ns trace-annotate \" Rec ack_ 4\""
$ns at 2.32 "$ns trace-annotate \" Send pkt_ 5\""
$ns at 2.55 "$ns trace-annotate \" Rec ack_ 5\""
$ns at 2.75 "$ns trace-annotate \" Send pkt_ 6\""
$ns at 2.99 "$ns trace-annotate \" Rec ack_ 6\""
$ns at 3.1 "$ns trace-annotate \" FTP stops\""
proc finish {} {
global ns nftf
$ns flush-trace
close $nf
close $tf
puts " running nam--"
exec namstop.nam&
exit 0
}
$ns run
Output:

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

10-Sliding Window
Program
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
$ns at 0.0 "$n0 label Sender"
$ns at 0.0 "$n1 label Receiver"
set nf [open stop.nam w]
$ns namtrace-all $nf
set tf [open stop.tr w]
$ns trace-all $tf

$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail


$ns duplex-link-op $n0 $n1 orient right

$ns queue-limit $n0 $n1 10

Agent/TCP set nam_tracevar_ true


set tcp [new Agent/TCP]
$tcp set windowInit_ 4
$tcp set maxcwnd_ 4

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]


$ns attach-agent $n1 $sink

$ns connect $tcp $sink

set ftp [new Application/FTP]


$ftp attach-agent $tcp

$ns add-agent-trace $tcptcp


$ns monitor-agent-trace $tcp

$tcptracevarcwnd_

$ns at 0.1 "$ftp start"


$ns at 3.0 "$ns detach-agent $n0 $tcp; $ns detach-agent $n1 $sink"
$ns at 3.5 "finish"

$ns at 0.0 "$ns trace-annotate \"Sliding Window operations\""


$ns at 0.05 "$ns trace-annotate \" FTP starts at 0.1\""
$ns at 0.11 "$ns trace-annotate \" Send pkt_ 0,1,2,3\""
$ns at 0.35 "$ns trace-annotate \" Rec ack_ 0,1,2,3\""
$ns at 0.56 "$ns trace-annotate \" Send pkt_ 4,5,6,7\""
$ns at 0.79 "$ns trace-annotate \" Rec ack_ 4,5,6,7\""
$ns at 0.99 "$ns trace-annotate \" Send pkt_ 8,9,10,11\""
$ns at 1.23 "$ns trace-annotate \" Rec ack_ 8,9,10,11\""
$ns at 1.43 "$ns trace-annotate \" Send pkt_ 12,13,14,15\""
$ns at 1.67 "$ns trace-annotate \" Rec ack_ 12,13,14,15\""
$ns at 1.88 "$ns trace-annotate \" Send pkt_ 16,17,18,19,20\""

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

$ns at 2.11 "$ns trace-annotate \" Rec ack_ 16,17,18,19,20\""


$ns at 2.32 "$ns trace-annotate \" Send pkt_ 21,22,23,24\""
$ns at 2.55 "$ns trace-annotate \" Rec ack_ 1,22,23,24\""
$ns at 2.75 "$ns trace-annotate \" Send pkt_ 25,26,27,28\""
$ns at 2.99 "$ns trace-annotate \" Rec ack_ 25,26,27,28\""
$ns at 3.1 "$ns trace-annotate \" FTP stops\""

proc finish {} {
global ns nftf
$ns flush-trace
close $nf
close $tf
puts " running nam--"
exec namstop.nam&
exit 0
}

$ns run
Output:

11- Write a Java program to implement RSA Algorithm

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

import java.math.*;
import java.util.*;

class RSA1 {
public static void main(String args[])
{
int p, q, n, z, d = 0, e, i;

// The number to be encrypted and decrypted


int msg = 12;
double c;
BigIntegermsgback;

// 1st prime number p


p = 3;
// 2nd prime number q
q = 11;
n = p * q;
z = (p - 1) * (q - 1);
System.out.println("the value of z = " + z);

for (e = 2; e < z; e++) {

// e is for public key exponent


if (gcd(e, z) == 1) {
break;
}
}
System.out.println("the value of e = " + e);
for (i = 0; i<= 9; i++) {

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

int x = 1 + (i * z);

// d is for private key exponent


if (x % e == 0) {
d = x / e;
break;
}
}
System.out.println("the value of d = " + d);
c = (Math.pow(msg, e)) % n;
System.out.println("Encrypted message is : " + c);

// converting int value of n to BigInteger


BigInteger N = BigInteger.valueOf(n);

// converting float value of c to BigInteger


BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println("Decrypted message is : "+ msgback);
}
static int gcd(int e, int z)
{
if (e == 0)
return z;
else
return gcd(z % e, e);
}
}
Output:

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

12- Write a Java program to implement the DES algorithm logic


import java.util.*;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

class DES{
public static void main(String[] args) throws IOException, NoSuchAlgorithmException,
InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException {

//String we want to encrypt


String message="This is a confidential message.";
byte[] myMessage =message.getBytes(); //string to byte array as DES works on bytes

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

//If you want to use your own key


// SecretKeyFactory MyKeyFactory = SecretKeyFactory.getInstance("DES");
// String Password = "My Password";
// byte[] mybyte =Password.getBytes();
// DESKeySpecmyMaterial = new DESKeySpec(mybyte);
// SecretKeymyDESKey = MyKeyFactory.generateSecret(myMaterial);

//Generating Key
KeyGeneratorMygenerator = KeyGenerator.getInstance("DES");
SecretKeymyDesKey = Mygenerator.generateKey();

//initializing crypto algorithm


Cipher myCipher = Cipher.getInstance("DES");

//setting encryption mode


myCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] myEncryptedBytes=myCipher.doFinal(myMessage);

//setting decryption mode


myCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] myDecryptedBytes=myCipher.doFinal(myEncryptedBytes);

//print message in byte format


//System.out.println(Arrays.toString(myEncryptedBytes));
//System.out.println(Arrays.toString(myDecryptedBytes));

String encrypteddata=new String(myEncryptedBytes);


String decrypteddata=new String(myDecryptedBytes);

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

System.out.println("Message : "+ message);


System.out.println("Encrypted - "+ encrypteddata);
System.out.println("Decrypted Message - "+ decrypteddata);
}
}

13-Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
// Java program to calculate SHA-1 hash value

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class GFG {


public static String encryptThisString(String input)
{
try {
// getInstance() method is called with algorithm SHA-1
MessageDigest md = MessageDigest.getInstance("SHA-1");

// digest() method is called


// to calculate message digest of the input string
// returned as array of byte
byte[] messageDigest = md.digest(input.getBytes());

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

// Convert byte array into signum representation


BigInteger no = new BigInteger(1, messageDigest);

// Convert message digest into hex value


String hashtext = no.toString(16);

// Add preceding 0s to make it 32 bit


while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}

// return the HashText


return hashtext;
}

// For specifying wrong message digest algorithms


catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}

// Driver code
public static void main(String args[]) throws

NoSuchAlgorithmException
{

System.out.println("HashCode Generated by SHA-1 for: ");

String s1 = "Good morning";

Dept of AIML , BNMIT 2023-24


Computer Networks and Security Lab Manual

System.out.println("\n" + s1 + " : " + encryptThisString(s1));

String s2 = "hello world";


System.out.println("\n" + s2 + " : " + encryptThisString(s2));
}
}
Output:

Dept of AIML , BNMIT 2023-24

You might also like