Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  client.c
Language: C/C++
Code:
#include"<netinet/in.h>"
#include"<netdb.h>"
#include"<sys/socket.h>"
#include"<arpa/inet.h>"
#include<stdio.h>
#include<stdlib.h>  
#include<string.h> 
#include<fcntl.h> 
#if defined (__APPLE__)
#  define COMMON_DIGEST_FOR_OPENSSL
#  include"<CommonCrypto/CommonDigest.h>"
#  define SHA1 CC_SHA1
#else
#  include"<openssl/md5.h>"
#endif
#define    LOGIN     "LOGIN   "
#define    AUTH      "AUTH    "
#define    GETFILE   "GETFILE "
#define    NEEDAUTH  "NEEDAUTH"
#define    LOGINOK   "LOGINOK "
#define    ERROR     "ERROR   "
#define    DATA      "DATA    "
#define    CREFAIL   40
#define    FWRTFAIL   50
#define    TYPELEN   8
#define    OUTSEQPK  12
#define    MEMERROR  11
#define    READFAIL  10
#define    WRTFAIL   9
#define    LOGINSUCCESS   0
#define    DATACORU  8
#define    SOCKERR   1
#define    BINDERR   2
#define    LISTERR   3
#define    CONNERR   4
#define    ACCEERR   5
#define    SUCCESS   0
#define    COMERROR   310
//const char NEEDAUTH[8]="NEEDAUTH";
struct TLV_Struct
{
  char pacType[8];
  int  length;
} ;

struct TLV_Struct1
{
  char pacType[8];
  int  length;
  char *value;
} ;

typedef struct TLV_Struct tlvStruct;
typedef struct TLV_Struct1 tlvFull;

char* getStream(tlvStruct*,char* );
tlvFull* putStreamEncode(char* );
void printcmPacket(tlvFull*);
void printErr(int);
int getFileFromServer(int,char*);
tlvFull* readFromSocket(int,int*);
int writeToSocket(int,char*,char*,int);
int writeToSocket1(int,char*,char*,int);
void printSerError(tlvFull*);
void freeCmPacket(tlvFull*);
char* getpwdHash(char*,int);
char* userName;
char* path_file;
char* Filename;
char* DownloadFile;
char* Password;
int main(int argc, char **argv) 

     int intServer_FD,intClient_FD; 
     char buf[1000] = "";
     char buf1[1000] = ""; 
     tlvStruct *tlvPack = NULL;
     char *bufferData = NULL;
     int intRetValue = 0;
     struct sockaddr_in ser; 
     char *Path = NULL;
     char *ipaddress = NULL;
     char *port_char = NULL;
     int i,j,k=0,port;
     for (i = 0; i < argc; i++)
     {
          if(strcmp("-i",argv[i]) == 0)
          {
               j = i;
               j++;
               ipaddress = argv[j];
               k++;
          }
          if(strcmp("-p",argv[i]) == 0)
          {
               j = i;
               j++;
               k++;
               port_char = argv[j];
               port = atoi(port_char);
          }
          if(strcmp("-u",argv[i]) == 0)
          {
               j = i;
               j++;
               k++;
               userName = argv[j];
          }
          if(strcmp("-pwd",argv[i]) == 0)
          {
               j = i;
               j++;
               k++;
               Password = argv[j];
          }
          if(strcmp("-f",argv[i]) == 0)
          {
               j = i;
               j++;
               k++;
               DownloadFile = argv[j];
          }
          if(strcmp("-dp",argv[i]) == 0)
          {
               j = i;
               j++;
               k++;
               path_file = argv[j];
          }
          if(strcmp("-dn",argv[i]) == 0)
          {
               j = i;
               j++;
               k++;
               Filename = argv[j];
          }
          if((argc-1 == i ) && (k != 7))
          {
               printf("Enter the options in the order -i ip_address -p port_number n -u username -pwd password -f file_to_be_downloaded -dp path_to_save_file -dn file_to_be_saved_as and try againn");
               exit(0);
          }
     }
     printf("Server IP address to connect: %s n",ipaddress);
     printf("Port number used to bind: %d n",port);
     printf("UserName for login: %s n",userName);
     printf("Password for login: %s n",Password);
     printf("File Name to download: %s n",DownloadFile);
     printf("Path to download file: %s n",path_file);
     printf("File to be renamed as: %s n",Filename);
     strcat(path_file,Filename);
     intServer_FD=socket(AF_INET,SOCK_STREAM,0); 
     if(intServer_FD<0)
     { 
          printErr(SOCKERR);
          return 8;
     }
     bzero(&ser,sizeof(struct sockaddr_in)); 
     ser.sin_family=AF_INET; 
     ser.sin_port=htons(port); 
     ser.sin_addr.s_addr = inet_addr(ipaddress);
     //printf("DownLoaded the file successfully. File Name is - %sn",path_file);
     intRetValue=connect(intServer_FD,(struct sockaddr *)&ser,sizeof(ser)); 
     //printf("DownLoaded the file successfully. File Name is - %sn",path_file);
     if (intRetValue<0)
     {
          printErr(CONNERR);
          return 8;
     }
     //printf("/nTEST2");
     intRetValue=clientGetAutheticate(intServer_FD);
     if(intRetValue>0) 
     {
          printErr(intRetValue);
          close(intServer_FD);
          return 6;
     }
     printf("Autheticated Successfullyn");
     //sleep(100);
     intRetValue=getFileFromServer(intServer_FD,DownloadFile);
     if(intRetValue>0)
     {
          printErr(intRetValue);
     }
     else
     {
          printf("DownLoaded the file successfully. File Name is - %sn",path_file);
     }
     close(intServer_FD); 
     if(intRetValue==0)
      {
     printErr(SUCCESS);
     }
     else
      {
        printf("Terminating the client",path_file);
      }
     return 0;


int clientGetAutheticate(int serverFD)
{
    tlvFull *cmPacket=NULL;
     char *pwdHash=NULL;
     char *pwdToSend=NULL;
     int retValue=0;
     retValue=writeToSocket(serverFD,userName,LOGIN,strlen(userName));
     if(retValue>0)
     {
          return retValue;
     }
     cmPacket=readFromSocket(serverFD,&retValue);
     if(retValue>0)
     {
         freeCmPacket(cmPacket);
          return retValue;
     }
     if(strncmp(cmPacket->pacType,NEEDAUTH,TYPELEN)!=0) 
     {
         freeCmPacket(cmPacket);
          return OUTSEQPK;

     }
     pwdHash=(char*)malloc(cmPacket->length+strlen(Password));
     
     if(pwdHash==NULL)
     {
          freeCmPacket(cmPacket);
          return MEMERROR;
     }
     bzero(pwdHash,cmPacket->length+strlen(Password));
     if(cmPacket->length==0)
     {
          strncpy(pwdHash,Password,strlen(Password));
     }
     else
     {
          strncpy(pwdHash,Password,strlen(Password));
          strncat(pwdHash+strlen(Password),cmPacket->value,cmPacket->length);
     }
     pwdToSend=getpwdHash(pwdHash,cmPacket->length+strlen(Password));
     if(pwdToSend==NULL)
     {
          freeCmPacket(cmPacket);
          return MEMERROR;
     }
     freeCmPacket(cmPacket);
     cmPacket=NULL;
     retValue=writeToSocket(serverFD,pwdToSend,AUTH,strlen(pwdToSend));
     free(pwdToSend);
     if(retValue>0)
     {
          return retValue;
     }
     cmPacket=readFromSocket(serverFD,&retValue);
     if(retValue>0)
     {
         freeCmPacket(cmPacket);
          return retValue;
     }
      
     if(strncmp(cmPacket->pacType,LOGINOK,TYPELEN)!=0) 
     {
        if(strncmp(cmPacket->pacType,ERROR,8)==0)
          {
               printSerError(cmPacket);
               freeCmPacket(cmPacket);
               return COMERROR;
          }
          else
          {
               freeCmPacket(cmPacket);
               return OUTSEQPK;

          }
     }
     freeCmPacket(cmPacket);
     cmPacket=NULL;
     return SUCCESS;
}


tlvFull* readFromSocket(int sockFD,int *errorVal)
{
     tlvStruct *packet=NULL;
     tlvFull  *cmPacket=NULL;
     int totalSize;
     int totalRead=0;
     int readData=0;
     int length=0;
     packet=(tlvStruct*)malloc(sizeof(tlvStruct));

     fflush(stdout);
     if(packet==NULL)
     {
          *errorVal=MEMERROR;
          return NULL;
     }

     readData=123;
     while(totalRead<sizeof(tlvStruct))
     {
          readData=read(sockFD,packet+totalRead,sizeof(tlvStruct)-totalRead);
          if(readData<=0)
          {
               free(packet);
               *errorVal=READFAIL;
               return NULL;
          }
          totalRead+=readData;
     }

     cmPacket=(tlvFull*)malloc(sizeof(tlvFull));
     bzero(cmPacket,sizeof(tlvFull));
     if(cmPacket==NULL)
     {
          free(packet);
          *errorVal=MEMERROR;
          return NULL;
     }
     strncpy(cmPacket->pacType,packet->pacType,TYPELEN);
     cmPacket->length=(packet->length);
     free(packet);
     
     if(cmPacket->length>0)
     {
          cmPacket->value=(char*)malloc(cmPacket->length);
          
          if(cmPacket->value==NULL)
          {
          free(cmPacket);
          *errorVal=MEMERROR;
          return NULL;
          }
          totalRead=0;
          readData=0;
          while(totalRead<cmPacket->length)
          {
               readData=read(sockFD,cmPacket->value,cmPacket->length-totalRead);
               if(readData<=0)
               {
                    free(cmPacket->value);
                    free(cmPacket);
                    *errorVal=READFAIL;
                    return NULL;
               }
               totalRead+=readData;
          }
          if(totalRead!=cmPacket->length)
          {
               *errorVal=DATACORU;
               free(cmPacket->value);
               free(cmPacket);
               return NULL;
          }
     }

     *errorVal=SUCCESS;
     if(cmPacket!=NULL)
     { 
          printcmPacket(cmPacket);
     }
     return cmPacket;
}

void freeCmPacket(tlvFull *pack)
{
     if (pack->value!=NULL)
     {
          free(pack->value);
     }
     free(pack);
}
  

void printcmPacket(tlvFull *cmPacket)
{
     char *type;
     char *value;
     type=(char*)malloc(9);
     strncpy(type,cmPacket->pacType,8);
     typetype[8]='';
     printf("CLIENT Type : %sn",type);
     free(type);
     if(cmPacket->value!=0)
     {
          value=(char*)malloc(cmPacket->length+1);
          strncpy(value,cmPacket->value,cmPacket->length);
          value[cmPacket->length]='';
          printf("CLIENT value : %sn",value);
          free(value);
     }
     printf("CLIENT Length : %dn",cmPacket->length);
}

  
  void printErr(int ErrroNumber)
  {
     switch(ErrroNumber)
      {
      case OUTSEQPK:
         printf("C-SERVER detected PACKET out of sequence Errorn");
          break;
     case MEMERROR:
         printf("C-Insuffucient Memory. Unable to Continuen");
          break;
      case READFAIL:
         printf("C-ERROR When reading from Socketn");
          break;
      case WRTFAIL:
         printf("C-ERROR When WRITING from Socketn");
          break;
     case DATACORU:
         printf("C-Data Corruptionn");
          break;
      case SOCKERR:
         printf("C-Unable to create socketn");
          break;
      case BINDERR:
         printf("C-SOCK BIND Errorn");
          break;
      case CONNERR:
         printf("C-Unable to Connect to servern");
          break;
      case LISTERR:
         printf("C-LISTEN Failedn");
          break;
     case ACCEERR:
         printf("C-Unable to accept the clientn");
          break;
     case SUCCESS:
         printf("C-Program completed successfullyn");
          break;
      case CREFAIL:
         printf("C-File requested already Presents in Client Working directory. Use rename option[-dn]n");
          break;
      default:
         printf("C- Client Terminatingn");
          break;
      }
  }
  
 int writeToSocket(int sockFD,char *buffer,char *TYPE,int length)
{
     tlvStruct *packet=NULL;
     int totalSize;
     int totalSent=0;
     int sentData=0;
     char *stream=NULL;
     int totalLen;
     char *test=NULL;
     char leng[4];
     int i;
     stream=(char*)malloc(sizeof(tlvStruct)+length);
     if (stream==NULL)
     {
          return MEMERROR;
     }
     if(buffer!=NULL && length!=0)
     {
          strncpy(stream+12,buffer,length);
          if(strncmp(TYPE,DATA,TYPELEN)==0) 
           {
             free(buffer);
           }
     }
     strncpy(stream,TYPE,TYPELEN);
     totalSize=length;
     length=htonl(length);
     for (i = sizeof(int) - 1; i >= 0; --i)
     leng[3-i] = (unsigned char) ((length >> (i * 8)) & 0xFF);
     strncpy(stream+8,leng,4);
     totalSize=totalSize+sizeof(tlvStruct);
     totalLen=totalSize+1;
     test=(char*)malloc(totalLen);
     if(test!=NULL)
     {
     strncpy(test,stream,totalSize);
     test[totalSize]='';
     printf("Client Stream %sn",test);
     free(test);
     }
     sentData=0;
     while(totalSent<totalSize)
     {
          sentData=write(sockFD,stream+totalSent,totalSize-totalSent);
          if(sentData<=0)
          {
               free(packet);
               return WRTFAIL;
          }
          totalSent+=sentData;

     }
     free(stream);
     return SUCCESS;
}
  
  
  void printSerError(tlvFull *errPack)
   {
     char *data=(char*)malloc(errPack->length+1);
      if(data!=NULL)
      {
      strncpy(data,errPack->value,errPack->length);
      data[errPack->length]='';
     if(errPack!=NULL)
        {
           if(errPack->value!=NULL)
              {
                 printf("SERVER Sends Error :  %sn",data);
               }
        }
        free(data);
      }
   }
 
 int getFileFromServer(int serverFd,char *fileName)
{
     tlvFull *cmPacket=NULL;
     int retValue=0;
     char *stream=NULL;
     int fileFD=0;
     int totalWrite=0;
     char* Path = '';
     printf("%s",fileName);
     retValue=writeToSocket(serverFd,fileName,GETFILE,strlen(fileName));
     if(retValue>0)
     {
          return retValue;
     }
     cmPacket=readFromSocket(serverFd,&retValue);
     if(retValue>0)
     {
          return retValue;
     }
     
     if (strncmp(cmPacket->pacType,DATA,8)!=0)
     {
        if(strncmp(cmPacket->pacType,ERROR,8)==0)
          {
              
               printSerError(cmPacket);
               return  COMERROR;
           }
           else
            {
               return OUTSEQPK;
            }
     }
     
     fileFD=open(path_file,O_CREAT|O_WRONLY);
     if(fileFD<0)
     {
          return CREFAIL;
     }
     while(totalWrite<cmPacket->length)
     {
          retValue= write(fileFD,cmPacket->value+totalWrite,cmPacket->length-totalWrite);
          if(retValue<0)
          {
               free(cmPacket);
               close(fileFD);
               return FWRTFAIL;
          }
          totalWrite+=retValue;
     }
     close(fileFD);
     return 0;
}

char *getpwdHash(char *strToHash,int length)

     char *strHash;
     char *strHashFinal;
     MD5_CTX md5Crypt;
     int retVal=0;
     int index=0;
     strHash=(char*)malloc(MD5_DIGEST_LENGTH);
     strHashFinal=(char*)malloc(2*MD5_DIGEST_LENGTH+1);
     if(strHash==NULL)
     {
          return NULL;
     }
     retVal=MD5_Init(&md5Crypt);
     if(retVal!=1) 
     { 
          free(strHash);
          free(strHashFinal);
          return NULL;
     }
     retVal=MD5_Update(&md5Crypt, strToHash, length);
     if(retVal!=1) 
     {
          free(strHash);
          free(strHashFinal);
          return NULL;
     }
     retVal=MD5_Final(strHash, &md5Crypt);
     if(retVal!=1) 
     {
          free(strHash);
          free(strHashFinal);
          return NULL;
     }
     for (index = 0; index < 16; ++index) 
     {
          snprintf(&(strHashFinal[index*2]), MD5_DIGEST_LENGTH*2, "%02x", (unsigned int)strHash[index]);
     }
     free(strHash);
     strHashFinal[2*MD5_DIGEST_LENGTH]='';
     return strHashFinal;  
}

          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
Comments: