Monday 4 November 2013

Network security Lab Programs

Week1.
Implement the following forms of IPC.
a) Pipes b) FIFO
a) Named Pipes
Half Duplex
---------------------------------------------------------------------------------------------------------------------
half Duplex.h
#define HALF_DUPLEX "/tmp/halfduplex"
#define MAX_BUF_SIZE 255
---------------------------------------------------------------------------------------------------------------------
hd_server.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "half_duplex.h" /* For name of the named-pipe */
#include <stdlib.h>
int main(int argc, char *argv[])
{
int fd, ret_val, count, numread;
char buf[MAX_BUF_SIZE];
/* Create the named - pipe */
ret_val = mkfifo(HALF_DUPLEX, 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the named pipe\n");
exit (1);
}
/* Open the pipe for reading */
fd = open(HALF_DUPLEX, O_RDONLY);
/* Read from the pipe */
numread = read(fd, buf, MAX_BUF_SIZE);
4
buf[numread] = '0';
printf("Half Duplex Server : Read From the pipe : %s\n", buf);
/* Convert to the string to upper case */
count = 0;
while (count < numread) {
buf[count] = toupper(buf[count]);
count++;
}
printf("Half Duplex Server : Converted String : %s\n", buf);
}
---------------------------------------------------------------------------------------------------------------------
halfduplex1.h
#define HALF_DUPLEX "/tmp/halfduplex1"
#define MAX_BUF_SIZE 255
---------------------------------------------------------------------------------------------------------------------
hd_client.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include "half_duplex1.h" /* For name of the named-pipe */
#include <stdlib.h>
int main(int argc, char *argv[])
{
int fd;
/* Check if an argument was specified. */
if (argc != 2) {
printf("Usage : %s <string to be sent to the server>n", argv[0]);
exit (1);
}
5
/* Open the pipe for writing */
fd = open(HALF_DUPLEX, O_WRONLY);
/* Write to the pipe */
write(fd, argv[1], strlen(argv[1]));
}
Execution Steps:
1. Named Pipes:
a) Half Duplex.
1. Run the server:
% cc hd_server.c
% mv a.out hd_server
%./hd_server &
The server program will block here, and the shell will return control to the
command line.
2. Run the client:
% cc hd_client
% mv a.out hd_client
%./hd_client hello
3. The server prints the string read and terminates:
Output:-
Half Duplex Server : Read From the pipe : hello
Half Duplex Server : Converted String : HELLO
---------------------------------------------------------------------------------------------------------------------
6
b) Named Pipe:
Full Duplex:
full duplex.h
#define NP1 "/tmp/np1"
#define NP2 "/tmp/np2"
#define MAX_BUF_SIZE 255
---------------------------------------------------------------------------------------------------------------------
fd_server.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fullduplex.h" /* For name of the named-pipe */
#include <stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
int rdfd, wrfd, ret_val, count, numread;
char buf[MAX_BUF_SIZE];
/* Create the first named - pipe */
ret_val = mkfifo(NP1, 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the named pipe");
exit (1);
}
ret_val = mkfifo(NP2, 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the named pipe");
exit (1);
}
/* Open the first named pipe for reading */
rdfd = open(NP1, O_RDONLY);
7
/* Open the second named pipe for writing */
wrfd = open(NP2, O_WRONLY);
/* Read from the first pipe */
numread = read(rdfd, buf, MAX_BUF_SIZE);
buf[numread] = '0';
printf("Full Duplex Server : Read From the pipe : %s\n", buf);
/* Convert to the string to upper case */
count = 0;
while (count < numread) {
buf[count] = toupper(buf[count]);
count++;
}
/*
* * Write the converted string back to the second
* * pipe
* */
write(wrfd, buf, strlen(buf));
}
---------------------------------------------------------------------------------------------------------------------
8
fd_client.c
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fullduplex.h" /* For name of the named-pipe */
#include <stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
int wrfd, rdfd, numread;
char rdbuf[MAX_BUF_SIZE];
/* Check if an argument was specified. */
if (argc != 2) {
printf("Usage : %s <string to be sent to the server>n", argv[0]);
exit (1);
}
/* Open the first named pipe for writing */
wrfd = open(NP1, O_WRONLY);
/* Open the second named pipe for reading */
rdfd = open(NP2, O_RDONLY);
/* Write to the pipe */
write(wrfd, argv[1], strlen(argv[1]));
/* Read from the pipe */
numread = read(rdfd, rdbuf, MAX_BUF_SIZE);
rdbuf[numread] = '0';
printf("Full Duplex Client : Read From the Pipe : %s\n", rdbuf);
}
9
Execution Steps:
b) Full Duplex.
1. Run the server:
% cc fd_server.c
% mv a.out fd_server
%./fd_server &
The server program will block here, and the shell will return control to the
command line.
2. Run the client:
% cc fd_client
% mv a.out fd_client
%./fd_client hello
3. The client program will send the string to server and block on the read
to await the server's response.
4. The server prints the following:
Full Duplex Server : Read From the pipe : hello
The client prints the following:
Full Duplex Client : Read From the pipe : HELLO
---------------------------------------------------------------------------------------------------------------------
10
Week2.
Implement file transfer using Message Queue form of IPC
message_send.c -- creating and sending to a simple message queue
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#define MSGSZ 128
/*
* Declare the message structure.
*/
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} message_buf;
main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
message_buf sbuf;
size_t buf_length;
/*
* Get the message queue id for the
* "name" 1234, which was created by
* the server.
*/
key = 1234;
(void) fprintf(stderr, "\nmsgget: Calling msgget(%#lx,\
%#o)\n",
key, msgflg);
if ((msqid = msgget(key, msgflg )) < 0) {
perror("msgget");
11
exit(1);
}
else
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
/*
* We'll send message type 1
*/
sbuf.mtype = 1;
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
(void) strcpy(sbuf.mtext, "Did you get this?");
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
buf_length = strlen(sbuf.mtext) + 1 ;
/*
* Send a message.
*/
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
perror("msgsnd");
exit(1);
}
else
printf("Message: \"%s\" Sent\n", sbuf.mtext);
exit(0);
}
---------------------------------------------------------------------------------------------------------------------
12
message_rec.c -- receiving the above message
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#define MSGSZ 128
/*
* Declare the message structure.
*/
typedef struct msgbuf {
long mtype;
char mtext[MSGSZ];
} message_buf;
main()
{
int msqid;
key_t key;
message_buf rbuf;
/*
* Get the message queue id for the
* "name" 1234, which was created by
* the server.
*/
key = 1234;
if ((msqid = msgget(key, 0666)) < 0) {
perror("msgget");
exit(1);
}
/*
* Receive an answer of message type 1.
*/
if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0) {
perror("msgrcv");
13
exit(1);
}
/*
* Print the answer.
*/
printf("%s\n", rbuf.mtext);
exit(0);
}
14
Execution Steps:
[sampath@localhost msgque]cc message_send.c
[sampath@localhost msgque]mv a.out msgsend
[sampath@localhost msgque]$ ./msgsend
msgget: Calling msgget(0x4d2,01666)
msgget: msgget succeeded: msqid = 0
msgget: msgget succeeded: msqid = 0
msgget: msgget succeeded: msqid = 0
Message: "Did you get this?" Sent
[sampath@localhost msgque]cc message_rec.c
[sampath@localhost msgque]mv a.out msgrec
[sampath@localhost msgque]$ ./msgrec &
[1] 2907
[sampath@localhost msgque]$ Did you get this?
---------------------------------------------------------------------------------------------------------------------
15
Week3.
Write a program to create an integer variable using shared memory concept and increment
the variable simultaneously by two processes. Use semaphores to avoid race conditions
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(void) {
pid_t pid;
int *shared; /* pointer to the shm */
int shmid;
shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
printf("Shared Memory ID=%u",shmid);
if (fork() == 0) { /* Child */
/* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
printf("Child pointer %u\n", shared);
*shared=1;
printf("Child value=%d\n", *shared);
sleep(2);
printf("Child value=%d\n", *shared);
} else { /* Parent */
/* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
printf("Parent pointer %u\n", shared);
printf("Parent value=%d\n", *shared);
sleep(1);
*shared=42;
printf("Parent value=%d\n", *shared);
sleep(5);
shmctl(shmid, IPC_RMID, 0);
}
}
16
Execution steps:
[sampath@localhost ipc]$cc shared_mem.c
[sampath@localhost ipc]$ ./a.out
Shared Memory ID=65537Child pointer 3086680064
Child value=1
Shared Memory ID=65537Parent pointer 3086680064
Parent value=1
Parent value=42
Child value=42




Hill Cipher
#include<stdio.h>
int main()
{
int i,j,ans[25][1],sum=0,mtrx[25][25],end;
char txt[25];
printf("\nEnter the string : ");
scanf("%s",txt);
for(i=0;i<25;i++)
{
if(txt[i]>=97 && txt[i]<122) {}
else
{
end=i;
break;
}
}
for(i=0;i<end;i++)
{
printf("initial : %d ",txt[i]);
txt[i]=txt[i]-'a';
printf("final : %d ",txt[i]);
printf("\n\n");
}
printf("\nEnter matrix...\n");
for(i=0;i<end;i++)
{
for(j=0;j<end;j++)
{
scanf("%d",&mtrx[i][j]);
}
}
for(i=0;i<end;i++)
{
sum=0;
for(j=0;j<end;j++)
{
sum+=mtrx[i][j]*(int)txt[j];
}
ans[i][0]=sum;
}
for(i=0;i<end;i++)
{
printf(" %c",((ans[i][0])%26)+97);
}
}

OUTPUT:
Better only give this as input

[1210310505@CSELABServer2 ~]$ ./a.out
Enter the string : pay
initial : 112 final : 15
initial : 97 final : 0
initial : 121 final : 24

Enter matrix...
17
17
5
21
18
21
2
2
19
 l n s[1210310505@CSELABServer2 ~]$



Ceaser cipher: encryption

#include<stdio.h>
#include<string.h>
main()
{
int i,n;
int j=0;
int k;
int l=0;
char a[26];
char s[100];
for(i=0;i<26;i++)
 {
 j=65+i;
 a[i]=(char)j;
 printf("%c",a[i]);
 }
printf("\nenter the nuber of letters");
scanf("%d",&n);
printf("\nenter the text");
for(i=0;i<(n+1);i++)
 {
 scanf("%c",&s[i]);
 printf("%c",s[i]);
}
printf("\n");
for(i=0;i<(n+1);i++)
{
 for(j=0;j<26;j++)
{
 if(s[i]==a[j])
 {
 k=(j+3)%26;
 printf("%c",a[k]);
 }
}
}
}
Decryption:
#include<stdio.h>
#include<string.h>
main()
{
int i,n;
int j=0;
int k;
int l=0;
char a[26];
char s[100];
for(i=0;i<26;i++)
 {
 j=65+i;
 a[i]=(char)j;
 printf("%c",a[i]);
 }
printf("\nenter the nuber of letters");
scanf("%d",&n);
printf("\nenter the text");
for(i=0;i<(n+1);i++)
 {
 scanf("%c",&s[i]);
 printf("%c",s[i]);
}
for(i=0;i<(n+1);i++)
{
 for(j=0;j<26;j++)
{
 if(s[i]==a[j])
 {
 k=(j-3)%26;
 printf("%c",a[k]);
 }
}
}
}
#include<stdio.h>
#include<conio.h>
#include<string.h>

 void main()
 {
   char s[30],k[27],c[30];
   int i, index;
   clrscr();
   printf("Enter plain text: ");
   gets(s);
   printf("\nEnter key with 26 character:");
   for(i=0;i<26;i++)
   {
     printf("\n%c",i+97);
     k[i]=getch();
     printf("%c",k[i]);
   }
   for(i=0;i<strlen(s);i++)
   {
     index=s[i]-97;
     c[i]=k[index];
   }
   printf("Your cipher text is:");
   for(i=0;i<strlen(s);i++)
   {
    printf("%c",c[i]);
   }
      getch();
 }


Monday 19 November 2012

Unix Shell Programming


Shell Programming
1.program for ARMSTRONG number
Armstrong Number
echo  enter the number
read  n
temp=$n
sum=0
while  [  $n - gt  0  ]
do
r=`expr  $n  %  10`
sum=`expr  $sum  +  \(  $r  \* $r  \*  $r  \)`
n=`expr  $n /  10`
done
if  [  $temp  -eq  $sum ]
then
echo  $temp is armstrong
else
echo  $temp is not armstrong
fi
exit 0

Output 1:
[cse3b520@CSE ~]$ sh a.sh
enter the number
153
153 is armstrong


2.program for FACTORIAL
echo enter a number
read n
sum=1
while  [  $n  -gt  1  ]
do
sum=`expr  $sum  \*  $n`
n=`expr  $n  -  1`
done
echo  $sum
exit 0

Output 1:
[cse3b520@CSE ~]$ sh n.sh
enter a number
6
720

3.Program for LARGEST NUMBER
if  [  $1  -gt  $2  ]
then
x=$1
elif  [  $2  -gt  $3 ]
then
x=$2
else
x=$3
fi
echo  largest number is   $x
exit  0

Output 1:
[cse3b520@CSE ~]$ sh g.sh 12 34 45
largest number is 45


4.Program for PERFECT NUMBER
echo enter a number
read no
i=1
ans=0
while  [  $i  -le  `expr  $no  /  2` ]
do
if  [  `expr  $no  %  $i`  -eq  0  ]
then
ans=`expr  $ans  +  $i`
fi
i=`expr  $i  +  1`
done
if  [  $no  -eq  $ans ]
then  echo  $no is perfect
else
echo  $no is not perfect
fi

Output 1:
 [cse3b520@CSE ~]$ sh p.sh
enter a number
34
34 is not perfect

5. Program for REVERSE
echo enter a num
read num
rev=0
while [ $num  -gt  0  ]
do
k=`expr  $num  \%  10`
l=`expr   $rev  \*  10`
rev=`expr  $l  +  $k`
num=`expr  $num  /  10`
done
echo reverse number is $rev
exit 0

Output 1:
 [cse3b520@CSE ~]$ sh r.sh
enter a num
12345
reverse number is 54321

6. Program for EVEN OR ODD
echo  enter a number
read n
i=1
while  [  $i  -le  $n  ]
do
echo enter term
read  x
if  [  `expr   $x  \%  2`  -eq  0  ]
then
echo  $x is even
else
echo  $x is odd
fi
i=`expr  $i  +  1`
done

Output 1:
[cse3b520@CSE ~]$ sh eo.sh
enter a number
4
enter term
3
3 is odd
enter term
2
2 is even
enter term
7
7 is odd
enter term
8
8 is even



7. Program for FIBONACCI
echo enter the number of terms
read n
f1=0
f2=1
i=1
echo  $f1
echo  $f2
while [  $i  -le  $n  ]
do
f3=`expr  $f1  +  $f2`
f1=$f2
f2=$f3
i=`expr  $i  +  1`
echo  $f3
done
exit 0
Output 1:
[cse3b520@CSE ~]$ sh fb.sh
enter the number of terms
5
0
1
1
2
3
5
8
8. Program for PALINDROME
echo enter a num
read num
rev=0
temp=$num
while  [ $num  -gt  0  ]
do
k=`expr  $num  \%  10`
l=`expr  $rev  \*  10`
rev=`expr  $l  +  $k`
num=`expr  $num  /  10`
done
if  [  $rev  -eq  $temp  ]
then
echo  $temp is a palindrome
else
echo  $temp is not a palindrome
fi
exit 0
Output 1:
[cse3b520@CSE ~]$ sh fbb.sh
enter a num
234
234 is not a palindrome

9. Program for prime or not prime
echo enter the number
read n
count=0
i=1
while  [ $i   -le  $n ]
do
if  [  `expr  $n  %  $i`  -eq  0  ]
then
count=`expr  $count  +  1`
fi
i=`expr  $i  +  1`
done
if  [  $count  -eq  2  ]
then
echo   $n is prime
else
echo   $n is not prime
fi


Output 1:
 [cse3b520@CSE ~]$ sh p1.sh
enter the number
3
3 is prime

10. Program for SUM OF N NUMBERS
echo enter the number
read n
i=1
sum=0
while  [  $i   -le  $n ]
do
sum=`expr  $sum  +  $i`
i=`expr  $i  +  1`
done
echo sum of numbers : $sum
exit 0


Output 1:
 [cse3b520@CSE ~]$ sh sum.sh
enter the number
20
sum of numbers : 210