Network Simulation and Data Security: Practical Implementations
1. Implementing a Three-Node Point-to-Point Network
This section demonstrates a three-node point-to-point network with duplex links. We will set the queue size, vary the bandwidth, and determine the number of packets dropped.
TCL Script
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 nf tf $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] $ns duplex-link $n0 $n2 200Mb 10ms DropTail $ns duplex-link $n1 $n2 100Mb 5ms DropTail $ns duplex-link $n2 $n3 1Mb 1000ms DropTail $ns queue-limit $n0 $n2 10 $ns queue-limit $n1 $n2 10 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0 set udp1 [new Agent/UDP] $ns attach-agent $n1 $udp1 set cbr1 [new Application/Traffic/CBR] $cbr1 attach-agent $udp1 set udp2 [new Agent/UDP] $ns attach-agent $n2 $udp2 set cbr2 [new Application/Traffic/CBR] $cbr2 attach-agent $udp2 set null0 [new Agent/Null] $ns attach-agent $n3 $null0 $ns connect $udp0 $null0 $ns connect $udp1 $null0 $ns at 0.1 "$cbr0 start" $ns at 0.2 "$cbr1 start" $ns at 1.0 "finish" $ns run
AWK Script
BEGIN{ c=0; } { if($1=="d") { c++; printf(" %s\t %s\t%s\t%s\t%s\n",$2,$3,$4,$5,$11); } } END{ printf("the number of packets dropped=%d\n",c); }
2. Implementing Ping and Traceroute Over a Six-Node Network
This section demonstrates the transmission of ping messages and traceroute over a network topology consisting of six nodes. We will determine the number of packets dropped due to congestion.
TCL Script
set ns [ new Simulator ] set nf [ open lab2.nam w ] $ns namtrace-all $nf set tf [ open lab2.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] $n4 shape box $ns duplex-link $n0 $n4 1005Mb 1ms DropTail $ns duplex-link $n1 $n4 50Mb 1ms DropTail $ns duplex-link $n2 $n4 2000Mb 1ms DropTail $ns duplex-link $n3 $n4 200Mb 1ms DropTail $ns duplex-link $n4 $n5 1Mb 1ms DropTail set p0 [new Agent/Ping] $ns attach-agent $n0 $p0 $p0 set packetSize_ 50000 $p0 set interval_ 0.0001 set p1 [new Agent/Ping] $ns attach-agent $n1 $p1 set p2 [new Agent/Ping] $ns attach-agent $n2 $p2 $p2 set packetSize_ 30000 $p2 set interval_ 0.00001 set p3 [new Agent/Ping] $ns attach-agent $n3 $p3 set p5 [new Agent/Ping] $ns attach-agent $n5 $p5 $ns queue-limit $n0 $n4 5 $ns queue-limit $n2 $n4 3 $ns queue-limit $n4 $n5 2 Agent/Ping instproc recv {from rtt} { $self instvar node_ puts "node [$node_ id] received answer from $from with round trip time $rtt msec" } $ns connect $p0 $p5 $ns connect $p2 $p3 proc finish { } { global ns nf tf $ns flush-trace close $nf close $tf exec nam lab2.nam & exit 0 } $ns at 0.1 "$p0 send" $ns at 0.2 "$p0 send" $ns at 0.3 "$p0 send" $ns at 0.4 "$p0 send" $ns at 0.5 "$p0 send" $ns at 0.6 "$p0 send" $ns at 0.7 "$p0 send" $ns at 0.8 "$p0 send" $ns at 0.9 "$p0 send" $ns at 1.0 "$p0 send" $ns at 1.1 "$p0 send" $ns at 1.2 "$p0 send" $ns at 1.3 "$p0 send" $ns at 1.4 "$p0 send" $ns at 1.5 "$p0 send" $ns at 1.6 "$p0 send" $ns at 1.7 "$p0 send" $ns at 1.8 "$p0 send" $ns at 1.9 "$p0 send" $ns at 2.0 "$p0 send" $ns at 2.1 "$p0 send" $ns at 2.2 "$p0 send" $ns at 2.3 "$p0 send" $ns at 2.4 "$p0 send" $ns at 2.5 "$p0 send" $ns at 2.6 "$p0 send" $ns at 2.7 "$p0 send" $ns at 2.8 "$p0 send" $ns at 2.9 "$p0 send" $ns at 0.1 "$p2 send" $ns at 0.2 "$p2 send" $ns at 0.3 "$p2 send" $ns at 0.4 "$p2 send" $ns at 0.5 "$p2 send" $ns at 0.6 "$p2 send" $ns at 0.7 "$p2 send" $ns at 0.8 "$p2 send" $ns at 0.9 "$p2 send" $ns at 1.0 "$p2 send" $ns at 1.1 "$p2 send" $ns at 1.2 "$p2 send" $ns at 1.3 "$p2 send" $ns at 1.4 "$p2 send" $ns at 1.5 "$p2 send" $ns at 1.6 "$p2 send" $ns at 1.7 "$p2 send" $ns at 1.8 "$p2 send" $ns at 1.9 "$p2 send" $ns at 2.0 "$p2 send" $ns at 2.1 "$p2 send" $ns at 2.2 "$p2 send" $ns at 2.3 "$p2 send" $ns at 2.4 "$p2 send" $ns at 2.5 "$p2 send" $ns at 2.6 "$p2 send" $ns at 2.7 "$p2 send" $ns at 2.8 "$p2 send" $ns at 2.9 "$p2 send" $ns at 3.0 "finish" $ns run
AWK Script
BEGIN{ drop=0; } { if($1=="d") { drop++; } } END{ printf("total no of %s type packets dropped=%d\n",$5,drop); }
Steps for Execution
- Open the VI editor and type the program. The program name should have the extension
.tcl
.
[root@localhost ~]# vi lab2.tcl
Or use Gedit:
[root@localhost ~]# gedit lab2.tcl
Type the code and save.
Save the program by pressing the ESC key first, followed by Shift and : keys simultaneously, and typewq
, then press Enter.
Open the VI editor and type the AWK program. The program name should have the extension .awk
.[root@localhost ~]# vi lab2.awkSave the program by pressing the ESC key first, followed by Shift and : keys simultaneously, and type
wq
, then press Enter.
Run the simulation program.[root@localhost~]# ns lab2.tcl
Here, ns
indicates the network simulator. You will get the required topology in the simulator.
[root@localhost~]# awk –f lab2.awk lab2.trTo see the trace file contents, open the file as follows:
[root@localhost~]# vi lab2.tr
The trace file contains 12 columns: Event type, Event time, From Node, Source Node, Packet Type, Packet Size, Flags (indicated by ——–), Flow ID, Source address, Destination address, Sequence ID, and Packet ID.
3. Implementing an Ethernet LAN with Congestion Window Plotting
This section implements an Ethernet LAN using n nodes, sets multiple traffic nodes, and plots the congestion window for different source/destination pairs.
TCL Script
set ns [new Simulator] set tf [open lab3.tr w] $ns trace-all $tf set nf [open lab3.nam w] $ns namtrace-all $nf set n0 [$ns node] $n0 color "magenta" $n0 label "src1" set n1 [$ns node] set n2 [$ns node] $n2 color "magenta" $n2 label "src2" set n3 [$ns node] $n3 color "blue" $n3 label "dest2" set n4 [$ns node] set n5 [$ns node] $n5 color "blue" $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 connect $tcp0 $sink5 set tcp2 [new Agent/TCP] $ns attach-agent $n2 $tcp2 set ftp2 [new Application/FTP] $ftp2 attach-agent $tcp2 $ftp2 set packetSize_ 600 $ftp2 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 nf tf $ns flush-trace close $tf close $nf exec nam lab3.nam & exit 0 } $ns at 0.1 "$ftp0 start" $ns at 5 "$ftp0 stop" $ns at 7 "$ftp0 start" $ns at 14 "$ftp0 stop" $ns at 0.2 "$ftp2 start" $ns at 8 "$ftp2 stop" $ns at 10 "$ftp2 start" $ns at 15 "$ftp2 stop" $ns at 16 "finish" $ns run
AWK Script
BEGIN{ } { if($6=="cwnd_") printf("%f\t%f\t\n",$1,$7); } END{ }
4. Error Detection Using CRC-CCITT (16-bits)
This section develops a program for error detection using CRC-CCITT (16-bits).
Source Code (Java)
import java.util.Scanner; class CRC{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); //Input Data Stream System.out.print("Enter data stream: "); String datastream = sc.nextLine(); System.out.print("Enter generator: "); String generator = sc.nextLine(); int data[] = new int[datastream.length() + generator.length()-1]; int divisor[] = new int[generator.length()]; for(int i=0;i<datastream.length();i++) data[i] = Integer.parseInt(datastream.charAt(i)+""); for(int i=0;i<generator.length();i++) divisor[i] = Integer.parseInt(generator.charAt(i)+""); //Calculation of CRC for(int i=0;i<datastream.length();i++){ if(data[i]==1) for(int j=0;j<divisor.length;j++) data[i+j] ^= divisor[j]; } //Display CRC System.out.print("The CRC code is: "); for(int i=0;i<datastream.length();i++) data[i] = Integer.parseInt(datastream.charAt(i)+""); for(int i=0;i<data.length;i++) System.out.print(data[i]); System.out.println(); //Check for input CRC code System.out.print("Enter CRC code: "); String datastream2 = sc.nextLine(); data = new int[datastream2.length()]; for(int i=0;i<datastream2.length();i++) data[i] = Integer.parseInt(datastream2.charAt(i)+""); //Calculation of remainder for(int i=0;i<datastream.length();i++){ if(data[i]==1) for(int j=0;j<divisor.length;j++) data[i+j] ^= divisor[j]; } //Display validity of data boolean valid = true; for(int i=0;i<data.length;i++) if(data[i]==1){ valid = false; break; } if(valid==true) System.out.println("Data stream is valid"); else System.out.println("Data stream is invalid. CRC error occurred."); } }
Output
Enter data stream: 1001 Enter generator: 1011 The CRC code is: 1001110 Enter CRC code: 1001111 Data stream is invalid. CRC error occurred. Enter data stream: 1001 Enter generator: 1011 The CRC code is: 1001110 Enter CRC code: 1001110 Data stream is valid
5. Implementing a Sliding Window Protocol
This section develops a program to implement a sliding window protocol in the data link layer.
import java.util.Timer; import java.util.TimerTask; class Frame { int seqNum; boolean acknowledged; Frame(int seqNum) { this.seqNum = seqNum; this.acknowledged = false; } } class Sender { private int windowSize; private int nextSeqNum; private int base; private Frame[] window; private Timer timer; private long timeout = 3000; // Timeout in milliseconds public Sender(int windowSize) { this.windowSize = windowSize; this.nextSeqNum = 0; this.base = 0; this.window = new Frame[windowSize]; this.timer = new Timer(); } public void sendData(int totalFrames) { while (base < totalFrames) { while (nextSeqNum < base + windowSize && nextSeqNum < totalFrames) { sendFrame(nextSeqNum); nextSeqNum++; } try { Thread.sleep(1000); // Simulate time for receiving an acknowledgment } catch (InterruptedException e) { e.printStackTrace(); } } } private void sendFrame(int seqNum) { System.out.println("Sending frame: " + seqNum); window[seqNum % windowSize] = new Frame(seqNum); startTimer(seqNum); // Start timer for the frame } public void receiveAck(int ackNum) { System.out.println("Received ACK for frame: " + ackNum); if (ackNum >= base) { base = ackNum + 1; } stopTimer(); } private void startTimer(int seqNum) { timer.schedule(new TimerTask() { @Override public void run() { System.out.println("Timeout for frame: " + seqNum); resendFrame(seqNum); // Resend the frame on timeout } }, timeout); } private void stopTimer() { timer.cancel(); timer = new Timer(); // Restart timer for the next frame } private void resendFrame(int seqNum) { System.out.println("Resending frame: " + seqNum); sendFrame(seqNum); } } class Receiver { private int expectedSeqNum = 0; public void receiveFrame(int seqNum, Sender sender) { if (seqNum == expectedSeqNum) { System.out.println("Received correct frame: " + seqNum); sender.receiveAck(seqNum); expectedSeqNum++; } else { System.out.println("Received out-of-order frame: " + seqNum); } } } public class SlidingWindowProtocol { public static void main(String[] args) { int totalFrames = 10; // Total number of frames to send int windowSize = 4; // Window size Sender sender = new Sender(windowSize); Receiver receiver = new Receiver(); // Simulate sending data and receiving acknowledgments new Thread(() -> sender.sendData(totalFrames)).start(); for (int i = 0; i < totalFrames; i++) { try { Thread.sleep(1500); // Simulate delay in receiving frames receiver.receiveFrame(i, sender); } catch (InterruptedException e) { e.printStackTrace(); } } } }
6. Congestion Control Using a Leaky Bucket Algorithm
This section develops a program for congestion control using a leaky bucket algorithm.
import java.util.Scanner; public class Leakybucket { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Enter the bucket size:"); int n=sc.nextInt(); System.out.println("Enter the number of incomming packets:"); int np=sc.nextInt(); int a[ ]=new int[np]; System.out.println("Enter the incomming packet size:"); for(int i=0;i<np;i++) { a[i]=sc.nextInt(); } System.out.println("Enter output rate="); int out=sc.nextInt(); for(int i=0;i<np;i++) { System.out.println("Transmitting packet of size:"+a[i]); if(a[i]>n) { System.out.println("packet cannot be transmitted, Bucket overflow: "+a[i]+"bits"); } else { if (a[i]<=out) System.out.println(" transmitted: "+a[i]+"bits"); else if(a[i]>out) { while(a[i]>out) { System.out.println(" transmitted: "+out+"bits"); a[i]=a[i]-out; } System.out.println(" transmitted: "+a[i]+"bits"); } } } } }
Output
Enter the bucket size: 30 Enter the number of incomming packets: 4 Enter the incomming packet size: 15 35 3 30 Enter output rate=10 Transmitting packet of size:15 transmitted: 10bits transmitted: 5bits Transmitting packet of size:35 packet cannot be transmitted, Bucket overflow: 35bits Transmitting packet of size:3 transmitted: 3bits Transmitting packet of size:30 transmitted: 10bits transmitted: 10bits transmitted: 10bits
7. Simple RSA Algorithm for Encryption and Decryption
This section develops a program for a simple RSA algorithm to encrypt and decrypt data.
import java.util.Scanner; import java.math.BigInteger; import java.util.Random; public class RSA { public void keygen() { Random rand1=new Random(System.currentTimeMillis()); Random rand2=new Random(System.currentTimeMillis()*10); Scanner sc = new Scanner(System.in); System.out.println("Enter an initial value for public key"); int pubkey=sc.nextInt(); BigInteger p=BigInteger.probablePrime(32, rand1); BigInteger q=BigInteger.probablePrime(32, rand2); BigInteger n=p.multiply(q); BigInteger p_1=p.subtract(new BigInteger("1")); BigInteger q_1=q.subtract(new BigInteger("1")); BigInteger fi=p_1.multiply(q_1); while(true) { BigInteger GCD=fi.gcd(new BigInteger(pubkey+"")); if(GCD.equals(BigInteger.ONE)) { break; } pubkey++; } BigInteger new_pubkey=new BigInteger(pubkey+""); BigInteger new_prvkey=new_pubkey.modInverse(fi); System.out.println("public key : "+new_pubkey+","+n); System.out.println("private key : "+new_prvkey+","+n); } public void encdec() { Scanner sc = new Scanner(System.in); System.out.println("Enter the public key"); String pubkey1=sc.nextLine(); BigInteger pubkey = new BigInteger(pubkey1); System.out.println("Enter the Private key"); String prvkey1=sc.nextLine(); BigInteger prvkey = new BigInteger(prvkey1); System.out.println("Enter the value of n"); String n1=sc.nextLine(); BigInteger n = new BigInteger(n1); System.out.println("Enter the message:"); String msg1=sc.nextLine(); BigInteger msg=new BigInteger(msg1); BigInteger cipherVal=msg.modPow(pubkey,n); System.out.println("Cipher text: " + cipherVal); BigInteger plainVal=cipherVal.modPow(prvkey,n); System.out.println("Plain text:" + plainVal); } public static void main(String[] args) { RSA R= new RSA(); System.out.println("The Key generation phase"); R.keygen(); System.out.println("The Encryption/Decryption phase"); R.encdec(); } }
Output
The Key generation phase Enter an initial value for public key 10 public key : 13,8735161591355742359 private key : 4031613039378571477,8735161591355742359 The Encryption/Decryption phase Enter the public key: 13 Enter the Private key: 4031613039378571477 Enter the value of n: 8735161591355742359 Enter the message: 787 Cipher text: 6309385004489097632 Plain text: 787