Sie sind auf Seite 1von 8

/*******************************************************************************

***********************
FILE NAME:client_function.c
FILE DESCRIPTION:client function file which contais different fu
nctions used by client
to send user credentials,receive server acknowl
egdment and receive file content
REVISION HISTORY
DATE

NAME

10-MAR-2014

REFERENCE

REASON

NONE

PROJECT

Revision History
Copyright 2014,Aricent Group(Holdings) pvt,ltd
********************************************************************************
*********************/
/*******************************************************************************
********************
HEADER FILE INCLUSION
********************************************************************************
*******************/
#include"./../include/header.h"
/*******************************************************************************
**********************
FUNCTION NAME:input_login_credential
FUNCTION DESCRIPTION:Creates main interface for the user in whic
h username,password
and type of user is taken as arguments and
based on the type of
user and acknowledgment from the server oth
er functions are called
ARGUMENTS:soket descriptor
RETURN TYPE:SUCCESS if user credentials are successfully sent to
server
FAILURE if user credentials are not successfulyy sen
t to server
********************************************************************************
***********************/
int input_login_credential(int sd)
{
char username[MAX],type_user[CHOICE],buf[MAX];
char *password;

const char prompt[MAX] = {'\0'};


int choice = 0,len = 0,flag = 0,ret = 0,length = 0,chance = 0;
system("clear");
printf("\t\t ***************************************************
****************\n");
printf("\t\t\t\t\t\t AUTHENTICATION SYSTEM\n");
printf("\t\t ***************************************************
****************\n");
sleep(3);
memset(type_user,0,CHOICE);
memset(buf,0,MAX);/***buf carries the string to be sent to the s
erver**/
while(1)
{
printf("\t\t\t Press \n\t\t\t1.New User\n\t\t\t2.Existing User\n
");
scanf("%d",&choice);
while('\n' != getchar())
{
;
}
switch(choice)
{
case 1:
while(3 > chance)/**giving three chances to new user to
enter unique username and Password**/
{
strncpy(type_user,"New_user",8);
len = strlen(type_user);
flag = 1;
memset(username,0,MAX);
printf("\n\t\t\t Enter the username\n");
fgets(username, MAX, stdin);
length = strlen(username);
if('\n' == username[length-1])/***removing new l
ine character**/
{
username[length-1] = '\0';
}
memset(buf, 0,MAX);
strncat(buf, type_user, strlen(type_user));
strncat(buf, " ",1);
strncat(buf, username, strlen(username));

strncat(buf, "$", 1);/**appending $ as a delimit


or for server**/
ret = send_login_credential(buf, sd);/**function
to write user credentials on to the socket**/
if(FAILURE == ret)/**write to socket unsuccessfu
ll**/
{
return FAILURE;
}
ret = read_socket(sd);/**reads acknowlegment fro
m the server**/
if(SUCCESS == ret)
{
break;
}
else
{
chance ++;
if(3 == chance)/**user unable to
enter unique username**/
{
return ;
}
printf("\n\t\t\tUsername not ava
ilable.Try with new username\n");
}
}
memset(buf, 0, MAX);
printf("\n\t\t\tEnter the Password\n");
password = getpass(prompt);/**Function to stop e
cho the password onto the screen**/
strncat(buf,type_user,len);
strncat(buf," ",1);
strncat(buf,username,strlen(username));
strncat(buf," ",1);
strncat(buf,password,strlen(password));
strncat(buf,"$",1);
ret = send_login_credential(buf,sd);
if(FAILURE == ret)
e socket
{
return(FAILURE);
}

// for write failure on th

ret = receive_from_server(sd);/**receive file fr


om the server**/
if(FAILURE == ret)
{
printf("CLIENT:error in receiving from s
erver");
}
break;
case 2:
while(3 > chance)
{
memset(buf, 0, MAX);
strncpy(type_user,"Existing_user",13);
len = strlen(type_user);
memset(username,0,MAX);
printf("\n\t\t\t Enter the username\n");
fgets(username, MAX, stdin);
length = strlen(username);
if('\n' == username[length-1])
{
username[length-1] = '\0';
}
printf("\n\t\t\tEnter the Password\n");
password = getpass(prompt);
strncat(buf,type_user,len);
strncat(buf," ",1);
strncat(buf,username,strlen(username));
strncat(buf," ",1);
strncat(buf,password,strlen(password));
strncat(buf,"$",1);
ret = send_login_credential(buf,sd);
if(FAILURE == ret)/**write to socket unsuccessfu
ll**/
{
return FAILURE;
}
ret = read_socket(sd);/***return Failure if pass
word and username does not match***/
if(FAILURE == ret)

{
flag = 0;
chance ++;
printf("\nYou have entered wrong usernam
e and password...Try Again\n");
}
else
{
flag = 1;
ret = receive_from_server(sd);
if(FAILURE == ret)
{
printf("CLIENT:error in receivin
g from server");
}
break;
}
}
break;
default:
printf("\nt\t\t Wrong Choice !!!\n");
system("clear");
}/**end of switch**/
if(1 == flag)
{
break;
}
else if(0 == flag)
{
return ;
}
else
{
;
}
}/**end of while 1**/
return SUCCESS;
}
/*******************************************************************************
*****************
FUNCTION NAME:send_login_credential
FUNCTION DESCRIPTION:Writes user credentials onto the socket

ARGUMENTS:takes buffer(which contains user credentials) and sock


et descriptor
RETURN TYPE:SUCCESS on successfull write onto the socket
FAILURE when write to socket is failed
********************************************************************************
*****************/
int send_login_credential(char *buf,int sd)
{
int byteswritten = 0,written = 0,len = 0;
len = strlen(buf);
while(byteswritten < len)
{
written = write(sd,buf+byteswritten,len-byteswritten);
if(0 > written)
{
return FAILURE;
}
byteswritten = byteswritten + written;
}
return SUCCESS;
}
/*******************************************************************************
******************
FUNCTION NAME:read_socket()
FUNCTION DESCRIPTION :This function is used to tell server's ack
nowledgmnet to
the user i.e for new user (wether user has
provided unique username
or not) and for existing user(wether usern
ame and password are
correct or not).
ARGUMENT:socket desrcriptor
RETURN TYPE:SUCCESS if server writes SUCCESS on the socket
FAILURE if server writes FAILURE on the socket
********************************************************************************
******************/
int read_socket(int sd)
{
int pointer = 0,n = 0;
char buf[MAX];
memset(buf,0,MAX);

while((n = read(sd,buf+pointer,MAX)) > 0)


{
pointer = pointer + n;
if(0 > n)
{
printf("\n Unable to read from server");
return FAILURE;
}
if('$' == buf[n-1])
{
buf[n-1] = '\0';
break;
}
}
if(0 == strcmp(buf,"SUCCESS"))
{
return SUCCESS;
}
else if(0 == strcmp(buf,"FAILURE"))
{
return FAILURE;
}
else
{
;
}
return SUCCESS;
}
/*******************************************************************************
******************
FUNCTION NAME:receive_from_server()
FUNCTION DESCRIPTION:This function receives the file content fro
m the server
FUNCTION ARGUMENTS:Takes socket descriptor as arguments
RETURN TYPE:SUCCESS if file is read successfull from the socket
FAILURE if file is not read successfully from the so
cket
********************************************************************************
*******************/
int receive_from_server(int sd)
{
int n = 0,pointer = 0,ret = 0;
char buf[MAX];
memset(buf,0,MAX);
while((n = read(sd,buf+pointer,MAX))>0)
{

pointer = pointer + n;
if(0 > n)
{
return FAILURE;
}
if('$' == buf[n-1])
{
buf[n-1] = '\0';
ret = write(1,buf,n-1);
if(0 > ret)
{
perror("\nError in writing into Monitor");
printf("%s",strerror(errno));
}
break;
}
ret = write(1,buf,n);
if(-1 == ret)
{
perror("\nError in writing into Monitor");
printf("%s",strerror(errno));
}
memset(buf,0,MAX);
pointer = 0;
}
return(SUCCESS);
}

Das könnte Ihnen auch gefallen