0% found this document useful (0 votes)
77 views

Pure and Slotted Aloha: Program

This document contains code for implementing distance vector routing to avoid two-node routing loops. It defines a router class with distance values to other routers. The main function gets input on the total routers and their names. Each router object gets its adjacent router names and distance values. A find_table function calculates the final routing table for a router based on distance vectors from adjacent routers, avoiding values that would create a two-node loop. It outputs the routing table.

Uploaded by

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

Pure and Slotted Aloha: Program

This document contains code for implementing distance vector routing to avoid two-node routing loops. It defines a router class with distance values to other routers. The main function gets input on the total routers and their names. Each router object gets its adjacent router names and distance values. A find_table function calculates the final routing table for a router based on distance vectors from adjacent routers, avoiding values that would create a two-node loop. It outputs the routing table.

Uploaded by

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

2.

Pure and Slotted Aloha


Program :
#include <stdio.h>
#include <math.h>
#include <time.h>
#define FRAME_TIME 250
int main(){
float S1, S2, G, J, val[100];
int I, n, K, delay;
printf("Please Give the Total Load : ");
scanf("%d", &n);
printf("Please Enter the value of load \n");
for (I=0; I<n; I++)
scanf("%f", &val[I]);
printf("\nOUTPUT 1: (THROUGHPUT Vs LOADCURVE)\n\n");
printf("s=g*exp(-G) FOR SLOTTED ALOHA * \n");
printf("s=g*exp(-2G) FOR PURE ALPHA #\n");
printf("\n------ (THROUGHPUT PER FRAME TIME)----\n");
for(K=0; K<n; K++){
G=val[K];
S1 = G * exp (-G);
S2 = G * exp(-2 * G);
printf("%1.3f", G );
for (I=0; I <=S1*20; I++)
printf(" ");
printf("*");
for(I=S2*20; I<=S2*75; I++ )
printf(" ");
printf("#\n");
}
printf("G (ATTEMPTS PER PACKET TIME) \n\n");
printf("\nOUTPUT 2 (DELAY Vs THROUGHPUT) \n\n");
printf("\n-----(THOUGHPUT PER FRAME TIME)----\n");
for(K=0; K<n; K++){
G=val[K];
S1 = G * exp (-G);
printf("3");
for (I=0; I <=S1*2.7; I++)
printf(" ");
printf("*\n");
}
printf("\n");
printf("---- DELAY -----");
}

Output :

3. Stop and Wait , Stop and Wait ARQ ,


Go-Back N , Selective Repeat ARQ
Program STOP AND WAIT:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define TIMEOUT 5
#define MAX_SEQ 1
#define TOT_PACKETS 8
#define inc(k) if(k<MAX_SEQ) k++; else k=0;
typedef struct{
int data;
}packet;
typedef struct{
int kind;
int seq;
int ack;
packet info;
int err;
}frame;
frame DATA;
typedef enum{frame_arrival,err,timeout,no_event} event_type;
void from_network_layer(packet *);
void to_network_layer(packet *);
void to_physical_layer(frame *);
void from_physical_layer(frame *);
void wait_for_event_sender(event_type *);
void wait_for_event_reciever(event_type *);
void reciever();
void sender();
int i=1;
char turn;
int DISCONNECT=0;
int main() {
while(!DISCONNECT) {
sender();
sleep(2);
reciever();
}
return 0;
}
void sender()
{
static int frame_to_send=0;
static frame s;
packet buffer;

event_type event;
static int flag=0;
if(flag==0)
{
from_network_layer(&buffer);
s.info = buffer;
s.seq = frame_to_send;
printf("SENDER : Info = %d Seq No = %d ",s.info.data,s.seq);
turn = 'r';
to_physical_layer(&s);
flag = 1;
}
wait_for_event_sender(&event);
if(turn=='s')
{
if(event==frame_arrival)
{
from_network_layer(&buffer);
inc(frame_to_send);
s.info = buffer;
s.seq = frame_to_send;
printf("SENDER : Info = %d Seq No = %d ",s.info.data,s.seq);
turn = 'r';
to_physical_layer(&s);
}
if(event==timeout)
{
printf("SENDER : Resending Frame
");
turn = 'r';
to_physical_layer(&s);
}
}
}
void reciever()
{
static int frame_expected=0;
frame r,s;
event_type event;
wait_for_event_reciever(&event);
if(turn=='r')
{
if(event==frame_arrival)
{
from_physical_layer(&r);
if(r.seq==frame_expected)
{
to_network_layer(&r.info);
inc(frame_expected);
}
else
printf("RECIEVER : Acknowledgement Resent\n");
turn = 's';
to_physical_layer(&s);
}
if(event==err)
{
printf("RECIEVER : Garbled Frame\n");
turn = 's';
}

}
}
void from_network_layer(packet *buffer)
{
(*buffer).data = i;
i++;
}
void to_physical_layer(frame *s)
{
srand(time(NULL));
s->err = random()%4;
DATA = *s;
}
void to_network_layer(packet *buffer)
{
printf("RECIEVER :Packet %d recieved , Ack Sent\n",(*buffer).data);
if(i>TOT_PACKETS)
{
DISCONNECT = 1;
printf("\nDISCONNECTED");
}
}
void from_physical_layer(frame *buffer)
{
*buffer = DATA;
}
void wait_for_event_sender(event_type * e)
{
static int timer=0;
if(turn=='s')
{
timer++;
if(timer==TIMEOUT)
{
*e = timeout;
printf("SENDER : Ack not recieved=> TIMEOUT\n");
timer = 0;
return;
}
if(DATA.err==0)
*e = err;
else
{
timer = 0;
*e = frame_arrival;
}
}
}
void wait_for_event_reciever(event_type * e)
{
if(turn=='r')
{
if(DATA.err==0)
*e = err;
else
*e = frame_arrival;
}
}

Output:

Program GO BACK N:
# include <iostream.h>
# include <stdlib.h>
# include <time.h>
# include <math.h>
# define TOT_FRAMES 500
# define FRAMES_SEND 10
class gobkn{
private:
int fr_send_at_instance;
int arr[TOT_FRAMES];
int arr1[FRAMES_SEND];
int sw;
int rw;
public:
gobkn();
void input();
void sender(int);
void reciever(int);
};
gobkn :: gobkn(){
sw = 0;
rw = 0;

}
void gobkn :: input(){
int n;
int m;
cout << "Enter the no of bits for the sequence no ";
cin >> n;
m = pow (2 , n);
int t = 0;
fr_send_at_instance = (m / 2);
for (int i = 0 ; i < TOT_FRAMES ; i++) {
arr[i] = t;
t = (t + 1) % m;
}
sender(m);
}
void gobkn :: sender(int m){
int j = 0;
for (int i = sw ; i < sw + fr_send_at_instance ; i++) {
arr1[j] = arr[i];
j++;
}
for (i = 0 ; i < j ; i++)
cout << " SENDER : Frame " << arr1[i] << " is sent\n";
reciever (m);
}
void gobkn :: reciever(int m){
time_t t;
int f;
int f1;
int a1;
char ch;
srand((unsigned) time(&t));
f = rand() % 10;
if (f != 5){
for (int i = 0 ; i < fr_send_at_instance ; i++){
if (rw == arr1[i]){
cout << "RECIEVER : Frame " << arr1[i] << " recieved correctly\n";
rw = (rw + 1) % m;
}
else
cout << "RECIEVER : Duplicate frame " << arr1[i] << " discarded\n";
}
a1 = rand() % 15;
if (a1 >= 0 && a1 <= 3){
cout << "(Acknowledgement " << arr1[a1] << " & all after this lost)\n";
sw = arr1[a1];
}
else
sw = (sw + fr_send_at_instance) % m;
}
else{
f1 = rand() % fr_send_at_instance;
for (int i = 0 ; i < f1 ; i++){
if (rw == arr1[i]){
cout << " RECIEVER : Frame " << arr1[i] << " recieved correctly\n";
rw = (rw + 1) % m;
}
else
cout << " RECIEVER : Duplicate frame " << arr1[i] << " discarded\n";
}

int ld = rand() % 2;
if (ld == 0)
cout << " RECIEVER : Frame " << arr1[f1] << " damaged\n";
else
cout << "
(Frame " << arr1[f1] << " lost)\n";
for (i = f1 + 1 ; i < fr_send_at_instance ; i++)
cout << " RECIEVER : Frame " << arr1[i] << " discarded\n";
cout << " (SENDER TIMEOUTS --> RESEND THE FRAME)\n";
sw = arr1[f1];
}
cout << "Want to continue...";
cin >> ch;
if (ch == 'y')
sender(m);
else
exit(0);
}
void main()
{
gobkn gb;
gb.input();
}
Output:

Program Selective Repeat:


# include <iostream>
# include <stdlib.h>
# include <time.h>
# include <math.h>
# define TOT_FRAMES 500
# define FRAMES_SEND 10
using namespace std;
class sel_repeat
{
private:
int fr_send_at_instance;
int arr[TOT_FRAMES];
int send[FRAMES_SEND];
int rcvd[FRAMES_SEND];
char rcvd_ack[FRAMES_SEND];
int sw;
int rw;
public:
void input();
void sender(int);
void reciever(int);
};
void sel_repeat :: input(){
int n;
int m;
cout << "Enter the no of bits for the sequence number ";
cin >> n;
m = pow (2 , n);
int t = 0;
fr_send_at_instance = (m / 2);
for (int i = 0 ; i < TOT_FRAMES ; i++){
arr[i] = t;
t = (t + 1) % m;
}
for (i = 0 ; i < fr_send_at_instance ; i++) {
send[i] = arr[i];
rcvd[i] = arr[i];
rcvd_ack[i] = 'n';
}
rw = sw = fr_send_at_instance;
sender(m);
}
void sel_repeat :: sender(int m){
for (int i = 0 ; i < fr_send_at_instance ; i++){
if ( rcvd_ack[i] == 'n' )
cout << " SENDER : Frame " << send[i] << " is sent\n";
}
reciever (m);
}
void sel_repeat :: reciever(int m){
time_t t;
int f;
int f1;
int a1;
char ch;
srand((unsigned) time(&t));
for (int i = 0 ; i < fr_send_at_instance ; i++) {

if (rcvd_ack[i] == 'n'){
f = rand() % 10;
if (f != 5) {
for (int j = 0 ; j < fr_send_at_instance ; j++)
if (rcvd[j] == send[i]) {
cout << "RECIEVER : Frame " << rcvd[j] << " recieved correctly\n";
rcvd[j] = arr[rw];
rw = (rw + 1) % m;
break;
}
if (j == fr_send_at_instance)
cout << "RECIEVER : Duplicate frame " << send[i] << " discarded\n";
a1 = rand() % 5;
if (a1 == 3) {
cout << "(Acknowledgement " << send[i] << " lost)\n";
cout << " (SENDER TIMEOUTS --> RESEND THE FRAME)\n";
rcvd_ack[i] = 'n';
}
else{
cout << "(Acknowledgement " << send[i] << " recieved)\n";
rcvd_ack[i] = 'p';
}
}
else{
int ld = rand() % 2;
if (ld == 0) {
cout << "RECIEVER : Frame " << send[i] << " is damaged\n";
cout << "RECIEVER : Negative acknowledgement " << send[i] << " sent\n";
}
else{
cout << "RECIEVER : Frame " << send[i] << " is lost\n";
cout << " (SENDER TIMEOUTS --> RESEND THE FRAME)\n";
}
rcvd_ack[i] = 'n';
}
}
}
for ( int j = 0 ; j < fr_send_at_instance ; j++){
if (rcvd_ack[j] == 'n')
break;
}
i=0;
for (int k = j ; k < fr_send_at_instance ; k++) {
send[i] = send[k];
if (rcvd_ack[k] == 'n')
rcvd_ack[i] = 'n';
else
rcvd_ack[i] = 'p';
i++;
}
if ( i != fr_send_at_instance ){
for ( int k = i ; k < fr_send_at_instance ; k++){
send[k] = arr[sw];
sw = (sw + 1) % m;
rcvd_ack[k] = 'n';
}
}
cout << "Want to continue...";
cin >> ch;
cout << "\n";
if (ch == 'y')
sender(m);
else

exit(0);
}
int main()
{
sel_repeat sr;
sr.input();
return 0;
}
Output:

4. Distance vector routing avoiding 2-node loop


Program :
# include <iostream>
# include <stdio.h>
# include <string.h>
using namespace std;
int total_r,adj;
char names[10][20],N[20];
class router{
private:
char name[20];
int dist[10];
public:
void get_r_table (int , int);
friend void find_table (router *);
};
void find_table(router *r){
int i,j;
cout << "Routing table for router " << N << " :-\n";
cout << "\tRouter\tDistance\n";
cout << "\t------\t--------\n";
for (i = 0 ; i < total_r ; i++){
int temp;
if(strcmp(N,names[i]) == 0)
temp = 0;
else{
temp = r[0].dist[i];
for(j = 1 ; j < adj + 1 ; j++)
if ( r[j].dist[i] < temp && r[j].dist[i] != 0 )
temp = r[j].dist[i] + r[0].dist[j]; }
r[0].dist[i] = temp;
}
for (i = 0 ; i < total_r ; i++)
cout << "\t" << names[i] << "\t"<<r[0].dist[i]<<"\n";
}
void router :: get_r_table (int no , int flag) {
if ( flag == 1 )
{
cout << "\nEnter the name of the adjacent router " << no << " : ";
cin>>name;
}
else
strcpy (name , N);
cout << "Enter the distances of each router from this router :\n";
cout << "\tROUTER\tDISTANCE\n";
cout << "\t------\t--------\n";
for (int i = 0 ; i < total_r ; i++)
{
cout << "\t" << names[i] << "\t";
if ( strcmp ( names[i] , name ) == 0 )
{
dist[i] = 0;
cout << dist[i] << "\n";
}
else
cin >> dist[i];
}
}
int main()
{
router r[10];

int i,flag=0;
cout << "Enter the total no of routers in the subnet [MAX 10] :- ";
cin >> total_r;
cout << "Enter the names of all the routers :-\n";
for (i = 0 ; i < total_r ; i++)
{
cout << "\t" << i + 1 << " . ";
cin>>names[i];
}
cout << "Enter the router whose routing table is to be found out : ";
cin>>N;
cout << "Enter the no of routers adjacent to this router : ";
cin >> adj;
for ( i = 0 ; i < adj + 1; i++ )
{
r[i].get_r_table (i , flag);
flag = 1;
}
find_table (r);
return 0;
}
Output:

9. S-DES Encryption and Decrytion


Program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define USAGE "Usage: sdes <-e | -d> <input file> <output file> <key 0-1023>\n"
#define BIT_1
#define BIT_2
#define BIT_3
#define BIT_4
#define BIT_5
#define BIT_6
#define BIT_7
#define BIT_8
#define BIT_9
#define BIT_10

1
2
4
8
16
32
64
128
256
512

#define BITMASK_RIGHT

(BIT_1 | BIT_2 | BIT_3 | BIT_4)

#define DBG(desc, num, bits)


static short int sbox0[][4] = {
1, 0, 3, 2,
3, 2, 1, 0,
0, 2, 1, 3,
3, 1, 3, 2
};
static short int sbox1[][4] = {
0, 1, 2, 3,
2, 0, 1, 3,
3, 0, 1, 0,
2, 1, 0, 3
};
void display_bits(int num, int bits) {
unsigned int i;
if(bits < 1 || bits > 32) {
printf("Invalid bit quantity");
return;
}
for(i = pow(2, bits-1); i > 0; i = i / 2) {
printf("%s",(i & num) ? "1" : "0");
}
printf("\n");
}
void dbg_num(char *desc, int num, int bits) {
printf("%s", desc);
display_bits(num, bits);
}
static char f(char input, char sk) {
short int row0=0,col0=0,row1=0,col1=0;
char out='\0',ep='\0',aux='\0';
DBG("f() input: ", (int)input, 8);

aux = !!(input & BIT_1);


ep |= ((aux << 7) | (aux << 1));
aux = !!(input & BIT_2);
ep |= ((aux << 4) | (aux << 2));
aux = !!(input & BIT_3);
ep |= ((aux << 5) | (aux << 3));
aux = !!(input & BIT_4);
ep |= ((aux << 6) | aux);
DBG("E/P: ", (int)ep, 8);
ep = ep ^ sk;
DBG("E/P ^ sk: ", (int)ep, 8);
row0 = !!(ep & BIT_8);
row0 = (row0 << 1) | !!(ep & BIT_5);
col0 = !!(ep & BIT_7);
col0 = (col0 << 1) | !!(ep & BIT_6);
row1 = !!(ep & BIT_4);
row1 = (row1 << 1) | !!(ep & BIT_1);
col1 = !!(ep & BIT_3);
col1 = (col1 << 1) | !!(ep & BIT_2);
out = !!(sbox0[row0][col0] & BIT_1);
out = (out << 1) | !!(sbox1[row1][col1] & BIT_1);
out = (out << 1) | !!(sbox1[row1][col1] & BIT_2);
out = (out << 1) | !!(sbox0[row0][col0] & BIT_2);
DBG("f() out: ", (int)out, 8);
return(out);
}
static char fk(char input, char sk1, char sk2) {
char l='\0',r='\0',out='\0';
DBG("Fk IN: ", (int)input, 8);
r = (input & BITMASK_RIGHT);
l = (input >> 4);
out = ((f(r, sk1) ^ l) & BITMASK_RIGHT) | (r << 4);
DBG("Fk OUT + Swap: ", (int)out, 8);
r = (out & BITMASK_RIGHT);
l = (out >> 4);
out = ((f(r, sk2) ^ l) << 4) | r;
DBG("Fk OUT: ", (int)out, 8);
return(out);
}
static void generate_sub_keys(short int key, char *sk1, char *sk2) {
short int i;
short int k1_order[] = { BIT_10, BIT_4, BIT_2, BIT_7, BIT_3, BIT_8, BIT_1, BIT_5 };
short int k2_order[] = { BIT_3, BIT_8, BIT_5, BIT_6, BIT_1, BIT_9, BIT_2, BIT_10 };
*sk1 = '\0';
*sk2 = '\0';
for (i = 0; i < 8; i++) {
*sk1 = (*sk1 << 1) | !!(k1_order[i] & key);
*sk2 = (*sk2 << 1) | !!(k2_order[i] & key);
}
}

static char ip(char byte) {


short int i;
char ret;
short int order[] = { BIT_7, BIT_3, BIT_6, BIT_8, BIT_5, BIT_1, BIT_4, BIT_2 };
ret = '\0';
for (i = 0; i < 8; i++) {
ret = (ret << 1) | !!(order[i] & byte);
}
return(ret);
}
static char ip_inverse(char byte) {
short int i;
char ret;
short int order[] = { BIT_5, BIT_8, BIT_6, BIT_4, BIT_2, BIT_7, BIT_1, BIT_3 };
ret = '\0';
for (i = 0; i < 8; i++) {
ret = (ret << 1) | !!(order[i] & byte);
}
return(ret);
}
int main(int argc, const char* argv[]) {
FILE
*in,*out;
short int key;
char
ch,sk1,sk2;
if (argc != 5) {
printf(USAGE);
return(1);
}
key = atoi(argv[4]);
if ((key < 0 || key > 1023) || strlen(argv[1]) != 2 || argv[1][0] != '-') {
printf(USAGE);
return(1);
}
key = (key % 1024);
switch (argv[1][1]) {
case 'e':
generate_sub_keys(key,&sk1,&sk2);
break;
case 'd':
generate_sub_keys(key,&sk2,&sk1);
break;
default:
printf(USAGE);
return(1);
}
in = fopen(argv[2], "rb");
if (!in) {
printf("File not found %s\n", argv[2]);
return(1);
}
out = fopen(argv[3], "wb+");
if (!out) {
printf("Error creating output file %s\n", argv[3]);
fclose(in);
return(1);
}
printf("Processing...\n");

while (!feof(in)) {
ch = fgetc(in);
ch = ip(ch);
ch = fk(ch, sk1, sk2);
ch = ip_inverse(ch);
fputc((char)ch, out);
}
printf("Ready!\n");
fclose(in);
fclose(out);
return 0;
}
Output:

7. Leaky bucket and token bucket


Program Leaky Bucket:
#include<stdio.h>
#include<cstdlib>
#include<time.h>
#include<unistd.h>
#define NOF_PACKETS 10
int rand(int a)
{
int rn = (rand()%10)%a;
return rn == 0 ? 1 : rn;
}
int main()
{
srand(time(NULL));
int packet_sz[NOF_PACKETS],i,clk,b_size,o_rate,p_sz_rm=0,p_sz,p_time,op,nop;
printf("\nEnter the number of packets (max 10) : ");
scanf("%d",&nop);
printf("enter the packet sizes in bytes :\n");
for(i=0;i<nop;++i)
scanf("%d",&packet_sz[i]);
printf("\nEnter the Output rate:");
scanf("%d",&o_rate);
printf("Enter the Bucket Size:");
scanf("%d",&b_size);
for(i=0; i<nop; ++i)
{
if( (packet_sz[i] + p_sz_rm) > b_size)
{
if(packet_sz[i] > b_size) /*compare the packet size with bucket size*/
{
printf("\n\nIncomming packet size (%dbytes) is Greater than bucket capacity",packet_sz[i]);
printf(" (%dbytes)-PACKET REJECTED",b_size);
}
else
printf("\n\nBucket capacity exceeded-PACKETS REJECTED!!");
}
else
{
p_sz_rm += packet_sz[i];
printf("\n\nIncomming Packet size: %d",packet_sz[i]);
printf("\nBytes remaining to Transmit: %d",p_sz_rm);
p_time = (rand()%3+1)*10;
printf("\nTime left for transmission: %d units",p_time);
for(clk=10; clk<=p_time; clk+=10)
{
sleep(1);
if(p_sz_rm)
{
if(p_sz_rm <= o_rate)/*packet size remaining comparing with output rate*/
{
op = p_sz_rm;
p_sz_rm = 0;
}
else
op = o_rate,p_sz_rm -= o_rate;
printf("\n Packet of size %d Transmitted",op);
printf("----Bytes Remaining to Transmit: %d",p_sz_rm);
}
else
{
printf("\nTime left for transmission: %d units",p_time-clk);
printf("\n No packets to transmit!!");
}
}
}
}
}

Output:

Program TOKEN BUCEKT:


#include <stdio.h>
#include <time.h>
#include <unistd.h>
float capacity;
float tokens;
float fillRate;
clock_t timestamp;
float getTokens()
{
clock_t now = clock();
if (tokens < capacity)
{
float delta = fillRate * (now - timestamp);
tokens = (capacity < tokens + delta) ? capacity: tokens+delta;
}
timestamp = now;
return tokens;
}
int consume()
{
float numberOfTokens = getTokens();
if (numberOfTokens <= tokens)
{
tokens -= numberOfTokens;
return 1;
}
return 0;
}
int main()
{
timestamp = clock();
fillRate = 1;
capacity = 80;
printf("\nTokens = %f",getTokens());
printf("\nConsume(10) = %d", consume(10));
printf("\nConsume(70) = %d",consume(70));
printf("\nConsume(1) = %d", consume(1));
sleep(1);
printf("\nTokens = %f",getTokens());
sleep(1);
printf("\nTokens = %f", getTokens());
printf("\nConsume(90) = %d", consume(90));
printf("\nTokens = %f", getTokens());
sleep(5);
printf("\nTokens = %f", getTokens());
getch();
return 0;

OUTPUT:

COMPUTER
NETWORKS
ASSIGNMENT

S.VENKKATESH
106114104
CSE- 2nd year

You might also like