0% found this document useful (0 votes)
14 views7 pages

Padi

The document contains code snippets for several networking algorithms: 1. CRC calculates a cyclic redundancy check value for error detection by XORing the message with a polynomial. 2. Bit stuffing prevents data corruption by inserting extra bits when long runs of 1s are detected in transmitted data. 3. Distance vector routing uses the Bellman-Ford algorithm to find the shortest paths between nodes in a graph representing a network. 4. Link state routing builds a complete map of the network's topology and uses Dijkstra's algorithm to find the shortest path from a source router to all other routers. 5. Encryption and decryption functions shift the characters in a string by a key value to securely

Uploaded by

Srikumar T B
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)
14 views7 pages

Padi

The document contains code snippets for several networking algorithms: 1. CRC calculates a cyclic redundancy check value for error detection by XORing the message with a polynomial. 2. Bit stuffing prevents data corruption by inserting extra bits when long runs of 1s are detected in transmitted data. 3. Distance vector routing uses the Bellman-Ford algorithm to find the shortest paths between nodes in a graph representing a network. 4. Link state routing builds a complete map of the network's topology and uses Dijkstra's algorithm to find the shortest path from a source router to all other routers. 5. Encryption and decryption functions shift the characters in a string by a key value to securely

Uploaded by

Srikumar T B
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/ 7

CRC

#include <stdio.h>

#include <string.h>

int crc(char* ip, char* op, char* poly, int mode) {

strcpy(op, ip);

if (mode) {

for(int i = 1; i < strlen(poly); i++) {

strcat(op, "0");} }

/* Perform XOR on the msg with the selected polynomial */

for (int i = 0; i < strlen(ip); i++) {

if (op[i] == '1') {

for (int j = 0; j < strlen(poly); j++) {

if (op[i+j] == poly[j]) {

op[i+j] = '0';

} else {

op[i+j] = '1';}}}} //return 0 if error detected

for (int i = 0; i < strlen(op); i++) {

if (op[i] == '1') {

return 0;}} // change return value from "1" to "01"

return 1;}

int main() {

char ip[50], op[50], recv[50];

char poly[] = "1001";

printf("Enter the input message in binary\n");

scanf("%s", ip);

crc(ip, op, poly, 1);


printf("The transmitted message is: %s%s\n", ip, op+strlen(ip));

printf("Enter the received message in binary\n");

scanf("%s", recv);

if (crc(recv, op, poly, 0)) {

printf("No Error in data\n");}

else {printf("Error in data transmission has occured\n");}

return 0;}

____________________________________________________________________________

Bit stuff
#include<stdio.h>

#include<string.h>

int main(){

char data[50],stuff[100],dstuff[50];

int i,j,cnt,len;

printf("Enter data\n");

scanf("%s",data);

len=strlen(data);

cnt=0;

j=0;

for(i=0;i<len;i++)

if (data[i]=='1')

cnt++;

else

cnt=0;

stuff[j]=data[i];

j++;
}

stuff[j]='\0';

printf("Stuffed data at intermediate site is:\n01111110 %s 01111110\n", stuff);

// destuffing

printf("Destuffed data:%s",stuff);

return 0;

_______________________________________________________________

Distance vector
#include <stdio.h>

#include <stdlib.h>

#define INF 999

int d[10][10], via[10][10];

int main() {

int i, j, k, n, g[10][10];

printf("\nEnter the no of nodes: ");

scanf("%d", &n);

// Input graph

for (i = 0; i < n; i++) {

printf("Enter the record for route %c \n", i + 97);

for (j = 0; j < n; j++) {

printf("(%c: %c) :: ", i + 97, j + 97);

scanf("%d", &g[i][j]);

if (g[i][j] != INF) {

d[i][j] = 1;

}} }

// Initialize via array


for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

via[i][j] = i;

}}

// Apply Bellman-Ford algorithm

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

if (d[i][j] == 1) {

for (k = 0; k < n; k++) {

if (g[i][k] + g[k][j] < g[i][j]) {

g[i][j] = g[i][k] + g[k][j];

via[i][j] = k;}}}}

// Output results

for (i = 0; i < n; i++) {

printf("Cost of route %c:\n", i + 97);

for (j = 0; j < n; j++) {

printf("%c : %d via %c \n", j + 97, g[i][j], via[i][j] + 97);} }

return 0;}

}___________________________________________________________________

Link state
#include<stdio.h>

int main(){

int count,src_router,i,j,k,w,v,min;

int cost_matrix[100][100],dist[100],last[100];

int flag[100];

printf("\n Enter the no of routers");

scanf("%d",&count);
printf("\n Enter the cost matrix values:");

for(i=0;i<count;i++){

for(j=0;j<count;j++){

printf("\n%d->%d:",i,j);

scanf("%d",&cost_matrix[i][j]);

if(cost_matrix[i][j]<0)cost_matrix[i][j]=1000;}}

printf("\n Enter the source router:");

scanf("%d",&src_router);

for(v=0;v<count;v++){

flag[v]=0;

last[v]=src_router;

dist[v]=cost_matrix[src_router][v];}

flag[src_router]=1;

for(i=0;i<count;i++){

min=1000;

for(w=0;w<count;w++){

if(!flag[w])

if(dist[w]<min){

v=w;

min=dist[w];}}

flag[v]=1;

for(w=0;w<count;w++){

if(!flag[w])

if(min+cost_matrix[v][w]<dist[w]){

dist[w]=min+cost_matrix[v][w];

last[w]=v;}}}

for(i=0;i<count;i++){

printf("\n%d==>%d:Path taken:%d",src_router,i,i);

w=i;
while(w!=src_router){

printf("\n<--%d",last[w]);w=last[w];}

printf("\n Shortest path cost:%d",dist[i]);}}

____________________________________________________________

Encryption and Decryption


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

void encrypt(char *str, int num) {

int len = strlen(str);

for (int i = 0; i < len; i++) {

str[i] -= num;}}

void decrypt(char *str, int num) {

int len = strlen(str);

for (int i = 0; i < len; i++) {

str[i] += num;}}

int main() {

int choice;

char str[100];

int num;

printf("Choose an option:\n");

printf("1. Input a string\n");

printf("2. Encrypt string\n");

printf("3. Decrypt string\n");

printf("4. Exit\n");

do {

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {
case 1:

printf("Enter a string to encrypt: ");

scanf("%s", str);

printf("Original string: %s\n", str);

printf("Enter an integer to use as key: ");

scanf("%d", &num);

break;

case 2:

encrypt(str, num);

printf("Encrypted string: %s\n", str);

break;

case 3:

decrypt(str, num);

printf("Decrypted string: %s\n", str);

break;

case 4:

printf("Exiting...\n");

break;

default:

printf("Invalid choice.\n");

break; }

} while (choice != 4);

return 0;}

__________________________________________________

You might also like