Beispiel #1
0
void 
substitute_back (command_t cmd, char * subs_from,char* subs_with)
{
	if (cmd->type != SIMPLE_COMMAND)
	{
		substitute_back (cmd->u.command[0], subs_from, subs_with);
		substitute_back (cmd->u.command[1], subs_from, subs_with);
		if (cmd->u.command[2] != NULL)
			substitute_back (cmd->u.command[2], subs_from, subs_with);
	}
	else
	{
		int i=0;
		char* tmp = cmd->u.word[i];
		while(tmp != NULL)
		{
			if (strcmp (tmp, subs_from) == 0)
			{
				tmp[0]='$';
				strcpy (tmp+1, subs_with);
			}
			i++;
			tmp = cmd->u.word[i];
		}
	}
}
Beispiel #2
0
void
execute_for_command(command_t c)
{
	if (c->u.command[0]->type != SIMPLE_COMMAND)
		error (1,0, "for command condition error.");
	int i=0;
	char* tmp = c->u.command[0]->u.word[i];
	while (tmp != NULL && strcmp (tmp, "in")!=0)
	{
		i++;
		tmp = c->u.command[0]->u.word[i];
	}
	if (tmp != NULL)	//tmp == "in"
	{
		char* variable = c->u.command[0]->u.word[i-1];
		i++;
		tmp = c->u.command[0]->u.word[i];
		while (tmp != NULL)
		{
			substitute_tokens (c->u.command[1], variable, tmp);
			execute_command (c->u.command[1],0);
			substitute_back (c->u.command[1], tmp, variable);
			c->status = c->u.command[1]->status;
			i++;
			tmp = c->u.command[0]->u.word[i];
		}
	}
	else //tmp == NULL
		error (1,0,"Syntax error: missing in token in for command.");
}
/* Interaction with the server */
void Talk_to_server ( int cfd ,char* agrv_filename)
{
    char str[STACK_SIZE],str_concat[STACK_SIZE];
    int x, e;
    FILE *fptr1;
    key pub_key, pvt_key;
    //char ch;
   char buffer[MAX_LEN];
   int nbytes, status;
   int src_addr, dest_addr;
  long int plaintext, ciphertext, deciphertext;
  KeyGeneration(&pub_key, &pvt_key);
   ReqMsg send_msg;
   RepMsg recv_msg;
   //key_public public_key;
   // printf("\n Public Key of Alice is (n,e): (%ld , %ld)\n\r", pub_key.public_key.n, pub_key.public_key.e);
 printf("\n Private key of Alice is (n,d): (%ld , %ld)\n\r", pvt_key.private_key.n,
  pvt_key.private_key.d);
   dest_addr = inet_addr(DEFAULT_SERVER);
   src_addr = inet_addr("127.0.0.5");

    printf("Sending the public_key to the server\n");          
    Msg msg;
    msg.hdr.opcode=Pubkey;
    msg.hdr.src_addr = src_addr;
    msg.hdr.dest_addr = dest_addr;
    msg.AllMsg.pubkey.n=pub_key.public_key.n;
    msg.AllMsg.pubkey.e=pub_key.public_key.e;
      int t_block=66,n_block=0;
    while(t_block<pub_key.public_key.n)
    {
      t_block=66+t_block*100;
      ++n_block;
    }
  status = send(cfd, &msg, sizeof(Msg), 0);
   if (status == -1) {
      fprintf(stderr, "*** Server error: unable to send\n");
      return;
    }
   /* send the request message REQ to the server */
   printf("Sending the request message REQ to the server\n");          
   msg.hdr.opcode = REQ;
   msg.hdr.src_addr = src_addr;
   msg.hdr.dest_addr = dest_addr;
   strcpy(msg.AllMsg.req.filename, agrv_filename);
   //printf("here\n");
   printf("File name is:- %s\n",msg.AllMsg.req.filename);
  

   status = send(cfd, &msg, sizeof(Msg), 0);
   if (status == -1) {
      fprintf(stderr, "*** Server error: unable to send\n");
      return;
    }
    //exit(0);
  while (1) {
  /* receive greetings from server */
   nbytes = recv(cfd, &msg, sizeof(Msg), 0);
   if (nbytes == -1) {
      fprintf(stderr, "*** Client error: unable to receive\n");
      
   }
   switch ( msg.hdr.opcode ) {
    
   case REP : 
                sprintf(str,"%ld",DecryptionAlgorithm(msg.AllMsg.rep.chyp,pvt_key));

                if(strlen(str)%2==1)
                {
                  strcpy(str_concat,"0");
                  strcat(str_concat,str);
                  strcpy(str,str_concat);
                }
                if(strlen(str)/(2*n_block)!=1)
                {
                  strcpy(str_concat,"00");
                  strcat(str_concat,str);
                  strcpy(str,str_concat);
                }
           /*printf("\nsha1 is:"); 
            int loop_print;
               for (loop_print = 0; loop_print < SHA_DIGEST_LENGTH; ++loop_print)
               {
                 printf("%u",msg.sha1_send[loop_print] );
               }*/
                
                //printf("final %s\n",str);
                substitute_back(str);
//printf("sha1 for sub%s\n",string_sha1);
               unsigned char digest[SHA_DIGEST_LENGTH];
               memset(digest,'\0',SHA_DIGEST_LENGTH);
               /*int x;
               for (x= 0; x < strlen(string_sha1); ++x)
               {
                 printf("string[%d]%c",x,string_sha1[x] );
               }
               printf("\n");*/
               //printf("size : %d\nText : #%s#", strlen(string_sha1), string_sha1);
               SHA1(string_sha1,strlen(string_sha1),digest);
               int for_loop;
               /*  for (i=0; i < SHA_DIGEST_LENGTH; i++) {
                 sprintf((char*)&(buf[i*2]), "%02x", temp[i]);
                 }*/

                for (for_loop = 0; for_loop < SHA_DIGEST_LENGTH; ++for_loop)
                 {
                   if(msg.sha1_send[for_loop]!=digest[for_loop])
                   {
                      msg.hdr.opcode=Disc;
                      msg.AllMsg.disconnect.status=1;
                      status = send(cfd, &msg, sizeof(Msg), 0);
                   if (status == -1) {
                      fprintf(stderr, "hi Server error: unable to send\n");
                      return;
                    }
                   }
                 }
                 memset(string_sha1,'\0',strlen(string_sha1));
                 //printf("\n");
               //printf("substitute after %s\n", string_substitute);
                //printf("plaintext : %ld\n",plain);
              /* Check the status of REP message */
             /* if (msg.AllMsg.r.status) 
                printf("Message REQ has received successfully by the server\n");
              else    
               printf("Message REQ has NOT received successfully by the server\n");*/
              break; 
  case REQCOM:
               if(msg.AllMsg.reqcom.status==1)
               {
                msg.hdr.opcode=Disc;
                msg.AllMsg.disconnect.status=1;
                fptr1 = fopen(agrv_filename,"w");
                if (fptr1 == NULL)
                      printf("Error in writing to the file\n");
                fprintf(fptr1, "%s", string_substitute);
                fclose(fptr1);
               }
      printf("file recived complted doing deciphertext opcode is Disconnect: %d\n", msg.hdr.opcode);

               status = send(cfd, &msg, sizeof(Msg), 0);
                   if (status == -1) {
                    printf("Disconnect client opcode: %d\n", msg.hdr.opcode);
                      exit(0);
                    }
              //exit(0);
               break;
                                      
  case Disc :
                  printf("Disconnect client opcode: %d\n", msg.hdr.opcode);
                  exit(0);
                  break;
   default: 
            //printf("default\n");
            printf("message received with opcode: %d\n", msg.hdr.opcode);
            exit(0);  
   }
 }
}