示例#1
0
void ZerberhorseAI::HandleEvent(const uint32 Event)
{
    switch (Event)
    {
        case EVT_EAT_PLAYER:
        {
            Unit *victim = SelectTarget(SELECT_TARGET_RANDOM, 0, 40, true);
            if (victim)
                me->CastSpell(victim, EAT_PLAYER, false);
            break;
        }
        case EVT_EAT_BUFFS:
        {
            std::list<Unit *> targets;
            SelectTargetList(targets, 5, SELECT_TARGET_RANDOM, 40, true);
            
            for (std::list<Unit *>::iterator itr = targets.begin(); itr != targets.end(); ++itr)
            {
                Unit *victim = *itr;
                DispelChargesList dispelList;
                victim->GetDispellableAuraList(me, DISPEL_MAGIC, dispelList);

                DispelChargesList::iterator ditr = dispelList.begin();
                std::advance(ditr, urand(0, dispelList.size() - 1));
                Aura *aura = ditr->first;
                if (aura)
                {
                    aura->Remove();
                    ModPower(ADD_BUFF);
                }
            }

            break;
        }
        case EVT_EXPLODE_TIMER:
        {
            if (ModPower(-1))
            {
                events.ScheduleEvent(EVT_EXPLODE_TIMER, TMR_EXPLODE_TIMER);
                break;
            }

            if (round >= MAX_ROUNDS)
            {
                const int32 bp0 = 14000;
                me->CastCustomSpell(me, EXPLODE, &bp0, NULL, NULL, false);
            }
            else
            {
                const int32 bp0 = 3500;
                me->CastCustomSpell(me, EXPLODE, &bp0, NULL, NULL, false);
            }

            events.ScheduleEvent(EVT_EAT_PLAYER, TMR_EAT_PLAYER);
            events.ScheduleEvent(EVT_EAT_BUFFS, TMR_EAT_BUFFS);
            phase = EATING_PHASE;
            break;
        }
    }
}
// Decryption Algorithm(D)
long int DecryptionAlgorithm(long int C, key pvt_key)
{
// Bob retrieves M as M := C^d(mod n)
long int M;
if(print_flag1)
printf("\n Decryption keys= ( %ld,%ld)\n\r",pvt_key.private_key.n,pvt_key.private_key.d);
M = ModPower(C, pvt_key.private_key.d, pvt_key.private_key.n);
return M;
}
// Encryption Algorithm(E)
long int EncryptionAlgorithm(long int M, PubKey pub_key)
{
   // Alice computes ciphertext as C := M^e(MOD n) to Bob.
   long int C;
   if(print_flag1)
      printf("\n Encryption keys= ( %ld,%ld)\n\r",pub_key.n,pub_key.e);
   C = ModPower(M, pub_key.e, pub_key.n);
   return C;
}// Decryption Algorithm(D)
/* Selecting a prime using the Miller-Robin primality test algorithm */ 
boolean MillerRobinTest(long int n, int iteration)
{
	// n is the given integer and k is the given desired
	// number of iterations in this primality test algorithm.
	// Return true if all the iterations test passed to give
	// the higher confidence that n is a prime, otherwise
	// return false if n is composite.

	long int m, t;
        int i,j;
	long int a, u;
        int flag;

         if(n mod 2 == 0)
		 return false;  // n is composite.

	 m=(n-1) div 2;
	 t=1;

	 while( m mod 2 == 0)  // repeat until m is even
	 {
		 m= m div 2;
		 t=t+1;
       	}
     
      for (j=0; j < iteration; j++) {  // Repeat the test for MAX_ITERATION times
         flag = 0;
         srand((unsigned int) time(NULL));
         a = random() mod n + 1;  // select a in {1,2,......,n}
         u = ModPower(a,m,n);
         if (u == 1 || u == n - 1) flag = 1;     

	 for(i=0;i<t;i++) 
	 {
		 
                 if(u == n - 1)  flag = 1;
		  //return true; //n is prime
                 u = (u * u) mod n;
                 
	 }
         if ( flag == 0 ) return false; // n is composite

     }

	 return true;  // n is prime.
}
//KEY GENERATION ALGORITHM IN DIFFIE-HELLMAN KEY EXCHANGE PROTOCOL
void KeyGeneration(long int *q, long int *alpha, long int *private_key, long int *public_key)
{
	
        long int p;
	if(print_flag1)
		printf("\n selecting q->\n\r");

	
        
	while(1)
	{
               srand((unsigned int) time(NULL));
               p = random() % LARGE;
	       if( p <= 10000) continue;
               /* test for even number */
               if ( (p & 0x01) == 0 ) continue;

               /* Trivial divisibility test by primes 3, 5, 7, 11, 13, 17, 19 */
               if ( (p % 3 == 0 ) || (p % 5 == 0) || (p % 7 == 0) || (p % 11 == 0 )
                    || (p % 13 == 0) || ( p % 17 == 0) || ( p% 19 == 0) )
               continue;

               if( MillerRobinTest(p, MAX_ITERATION) )
		break;
		
	}

    *q = p;
    if (verify_prime(p) )
      printf( "q = %ld is prime\n", *q);
     else {
      printf("q = %ld is composite\n", *q);
      exit(0);
     }
     
     /* Select a primitive root of q */
     *alpha = primitive_root( p );
          
     /* Select a private key  < q  randomly */
     *private_key = rand() % p;

    /* Compute the public key */
    *public_key = ModPower(*alpha, *private_key, *q);

} 
/* Interaction with the server */
void Talk_to_server ( int cfd )
{
   int cont;
   int nbytes, status;
   int src_addr, dest_addr;
   char filename[25], id[10], pw[10];
   Msg send_msg;
   Msg recv_msg;
   long int q, alpha, secret_key, public_key, private_key;
   long int X_A, Y_A, Y_B;
   FILE *fp;
   dest_addr = inet_addr("DEFAULT_SERVER");
   src_addr = inet_addr("192.168.1.245");
   
   printf("\n\nRegistration Phase:\n\n");
   printf("\nDo you want to register?(y:1/n:0)\t");
   scanf("%d", &cont);

   while(cont)
   {
	printf("\nEnter the ID:\t");
	scanf("%s", id);
	printf("\nEnter the Password:\t");
	scanf("%s", pw);

	send_msg.hdr.opcode = REGISTER;
   	send_msg.hdr.src_addr = src_addr;
   	send_msg.hdr.dest_addr = dest_addr;
   	strcpy(send_msg.m.auth.id, id);
   	strcpy(send_msg.m.auth.pw, pw);

	/* send the user's response to the server */
   	status = send(cfd, &send_msg, sizeof(Msg), 0);
   	if (status == -1)
   	{
      		fprintf(stderr, "*** Server error: unable to send\n");
      		return;
   	}

	printf("\nContinue with registration?(y:1/n:0)\t");
	scanf("%d", &cont);
   }

   printf("\n\nLogin Phase:\n");
   printf("\nEnter the ID:\t");
   scanf("%s", id);
   printf("\nEnter the Password:\t");
   scanf("%s", pw);

   send_msg.hdr.opcode = LOGINREQ;
   send_msg.hdr.src_addr = src_addr;
   send_msg.hdr.dest_addr = dest_addr;
   strcpy(send_msg.m.auth.id, id);
   strcpy(send_msg.m.auth.pw, pw);
             
   /* send the user's response to the server */
   status = send(cfd, &send_msg, sizeof(Msg), 0);
   if (status == -1)
   {
      fprintf(stderr, "*** Server error: unable to send\n");
      return;
   }
               

   /* Wait for responses from the server (Bob, User B) */
   while ( 1 )
   {

   	/* receive messages from server */
   	nbytes = recv(cfd, &recv_msg, sizeof(Msg), 0);
   	if (nbytes == -1)
	{
      		fprintf(stderr, "*** Client error: unable to receive\n");
      	}
 
   	switch ( recv_msg.hdr.opcode )
	{

		case LOGINREP : /* Login Reply */
				printf("\n\nMessage:: LOGINREP received from source (%d)\n", recv_msg.hdr.src_addr);
				printf("%s\n", recv_msg.m.buf);
				if( (strcmp(recv_msg.m.buf, "Wrong ID/Password!") == 0) )
				{
					exit(0) ;
				}
				else if( (strcmp(recv_msg.m.buf, "Authenticated!") == 0) )
				{
					send_msg.hdr.opcode = PUBKEY; 
                			send_msg.hdr.src_addr = src_addr;
                			send_msg.hdr.dest_addr = dest_addr;
					printf("\nKey Generaion has been started:\n\r");	
   					KeyGeneration(&q, &alpha, &private_key, &public_key);
   					printf("Global public elements are q = %ld, alpha = %ld\n\r", q, alpha);

   					printf("Public Key for Alice (User A) is Y_A: %ld\n\r", public_key);

   					printf("Private key for Alice (User A) is X_A: %ld\n\r", private_key);
   
   					X_A = private_key;
   					Y_A = public_key;
					/* send the public key to Bob */
					printf("Sending the public key to Bob (User B)\n");
   					send_msg.m.dhpubkey.q = q;  /* q */
   					send_msg.m.dhpubkey.g = alpha; /* alpha */
   					send_msg.m.dhpubkey.Y = Y_A;  /* value of public key */	

					status = send(cfd, &send_msg, sizeof(Msg), 0);
   					if (status == -1)
					{
      						fprintf(stderr, "*** Client error: unable to send\n");
      						return;
    					}
				}
				
				break;
    
   		case PUBKEY : 	/* Public key */
                		printf("\n\nMessage:: PUBKEY received from source (%d)\n", recv_msg.hdr.src_addr); 

		               /* Compute the secret shared key based on public key received from Bob (User B) */  
        		        Y_B = recv_msg.m.dhpubkey.Y;
               			printf("Received public values from Bob are q = %ld, alpha = %ld, Y_B = %ld\n\r", 
           	            	recv_msg.m.dhpubkey.q, recv_msg.m.dhpubkey.g, Y_B);
               			secret_key = ModPower(Y_B, X_A, q);
               			printf("Computed secret shared key by Alice (User A) is %ld\n\r", secret_key);

				printf("\n\nEnter the file to be retrieved:\t");
				scanf("%s", filename);

               			/* Send an acknowledgement to Bob that the secret key is computed */       
                		send_msg.hdr.opcode = REQSERV;
                		send_msg.hdr.src_addr = src_addr;
                		send_msg.hdr.dest_addr = dest_addr;
                		strcpy(send_msg.m.buf, filename);

				fp = fopen(filename, "w"); // append mode
				fclose(fp);

				status = send(cfd, &send_msg, sizeof(Msg), 0);
   				if (status == -1)
				{
      					fprintf(stderr, "*** Client error: unable to send\n");
      					return;
    				}
               			
				break;      
 
   		case ENCMSG : 	/* Encrypted msg */
              			printf("\n\nMessage:: ENCMSG received from source (%d)\n", recv_msg.hdr.src_addr);
           		   	printf("\nCiphertext received:\n\n%s\n", recv_msg.m.buf); 
              			/* decrypt the ciphertext */
               			strcpy(buffer, recv_msg.m.buf);
               			DecryptionAlgorithm(buffer, secret_key);
               			printf("\nRecovered plaintext:\n\n%s\n", buffer);   

				char ch;

  				fp = fopen(filename, "a"); // append mode
 
  				if( fp == NULL )
   				{
      					perror("Error while opening the file.\n");
    				  	exit(EXIT_FAILURE);
   				}
 				
				fwrite(buffer, 1, strlen(buffer), fp);
								
				fclose(fp);
    				printf("Written to file.\n");
				
				break;

		case REQCOM :	/* Request Complete */
				printf("\n\nMessage:: REQCOM received from source (%d)\n", recv_msg.hdr.src_addr);
           		   	printf("\n%s\n", recv_msg.m.buf);
				printf("\nDisconnecting from server!\n");
				send_msg.hdr.opcode = DISCONNECT;
                		send_msg.hdr.src_addr = src_addr;
                		send_msg.hdr.dest_addr = dest_addr;

				strcpy(send_msg.m.buf, "Disconnect...");
				status = send(cfd, &send_msg, sizeof(Msg), 0);
   				if (status == -1)
				{
      					fprintf(stderr, "*** Client error: unable to send\n");
      					return;
    				}

				break;

		case DISCONNECT :/* Disconnect */
				printf("\n\nMessage:: DISCONNECT received from source (%d)\n", recv_msg.hdr.src_addr);
				printf("%s\n", recv_msg.m.buf);
				exit(0);

				break;

   	}
   
  }

}
示例#7
0
void ZerberhorseAI::SpellHitTarget(Unit* target, SpellInfo const* spell)
{
    if (target->ToPlayer() && spell->Id == EAT_PLAYER)
        ModPower(ADD_EAT);
}
示例#8
0
void ZerberhorseAI::KilledUnit(Unit* who)
{
    //DoScriptText(SAY_KILLED, me, who);

    ModPower(ADD_KILL);
}
/* Interaction of the child process with the client */
void Talk_to_client ( int cfd )
{
   char id[10], pw[10];
   int nbytes, status;
   int src_addr, dest_addr;
   int abc, xyz=0, maskedpw;
   int i = 0, flag, j;
   Msg send_msg;
   Msg recv_msg;
   FILE *fp;
   long int q, alpha, secret_key, public_key, private_key;
   long int X_B, Y_A, Y_B;

   dest_addr = inet_addr("DEFAULT_SERVER");
   src_addr = inet_addr("192.168.1.245");
   
     
   /* Wait for responses from the clent (Alice, User A) */
   while ( 1 )
   {
   	/* receive messages from server */
   	nbytes = recv(cfd, &recv_msg, sizeof(Msg), 0);
   	if (nbytes == -1)
	{
	      	fprintf(stderr, "*** Client error: unable to receive\n");
   	}
 	switch ( recv_msg.hdr.opcode )
	{
    
		case REGISTER :	/* Registration from Client */
				printf("\n\nMessage:: REGISTER received from source (%d)\n", recv_msg.hdr.src_addr);
				strcpy(id, recv_msg.m.auth.id);
				strcpy(pw, recv_msg.m.auth.pw);
				printf("Received ID:\t%s\nPW:\t%s\n", id, pw);	
				make_clist(id, pw);
				
				fp = fopen("ClientList.txt", "a"); // append mode
 
  				if( fp == NULL )
   				{
      					perror("Error while opening the file.\n");
    				  	exit(EXIT_FAILURE);
   				}
 				
				fwrite(&clist, sizeof(clist), 1, fp);
								
				fclose(fp);
    				printf("Written to file.\n");

				printf("\nUpdated Client List is:");
				display_clist();

				break;

   		case LOGINREQ :	/* Login Request from Client */
				printf("\n\nMessage:: LOGINREQ received from source (%d)\n", recv_msg.hdr.src_addr);
				strcpy(id, recv_msg.m.auth.id);
				strcpy(pw, recv_msg.m.auth.pw);
				printf("Received ID:\t%s\nPW:\t%s\n", id, pw);			
				send_msg.hdr.opcode = LOGINREP;
   				send_msg.hdr.src_addr = src_addr;
   				send_msg.hdr.dest_addr = dest_addr;

				flag = 0;
				fp = fopen("ClientList.txt", "r"); // read mode
 
  				if( fp == NULL )
   				{
					printf("Clientlist does not exist!");
					send_msg.hdr.opcode = REQCOM;
   					send_msg.hdr.src_addr = src_addr;
   					send_msg.hdr.dest_addr = dest_addr;
					strcpy(send_msg.m.buf, "No user registered!");

					status = send(cfd, &send_msg, sizeof(Msg), 0);
  					if (status == -1)
					{
      						fprintf(stderr, "*** Client error: unable to send\n");
     						return;
    					}
   				}
				else
				{
					while(fread(&clist, sizeof(clist), 1, fp) != 0)
					{
						if( strcmp(id, clist.id) == 0 )
						{
						 	 xyz = 0;
    							 for(i = 0; pw[i] != '\0'; i++)
    							 {
								abc = (int) pw[i];
								xyz = xyz ^ abc;
    							 }
    							 maskedpw = clist.salt ^ xyz;
							 if(maskedpw == clist.maskedpw)
							 {
								
								flag = 1;
								break;
		   					 }
						}
					}

					fclose(fp);

                                	if(flag == 0)
					{
						printf("Wrong ID/Password!\n");					
						strcpy(send_msg.m.buf, "Wrong ID/Password!");
					}
					else
					{
						printf("Authenticated!\n");
						strcpy(send_msg.m.buf, "Authenticated!");
					}
					status = send(cfd, &send_msg, sizeof(Msg), 0);
  					if (status == -1)
					{
      						fprintf(stderr, "*** Client error: unable to send\n");
     						return;
    					}
				}
				break;

		case PUBKEY :   /* Public key */
                		printf("\n\nMessage:: PUBKEY received from source (%d)\n", recv_msg.hdr.src_addr); 

		                /* Compute the secret shared key based on public key received from Bob (User B) */
		                q =   recv_msg.m.dhpubkey.q;
		                alpha = recv_msg.m.dhpubkey.g;
        		        Y_A = recv_msg.m.dhpubkey.Y;
       			        printf("Received public values from Alice are q = %ld, alpha = %ld, Y_A = %ld\n\r", 
        	                q, alpha, Y_A);
               
      			        /* Select a private key X_B < q */
               			private_key = rand() % q;
         		        X_B = private_key;
   		                printf("Private key for Bob (User B) is %ld\n\r", X_B);              
 
        		        /* Compute the public key Y_B */
       			        public_key = ModPower(alpha, X_B, q);
        		        Y_B = public_key;
               			printf("Public key for Bob (User B) is %ld\n\r", Y_B); 

		                /* Send the public key to Alice */
   				printf("Sending the public key to Alice (User A)\n");          
   				send_msg.hdr.opcode = PUBKEY;
   				send_msg.hdr.src_addr = src_addr;
   				send_msg.hdr.dest_addr = dest_addr;
   				
   				/* send the public key to Alice */
   				send_msg.m.dhpubkey.q = q;  /* q */
   				send_msg.m.dhpubkey.g = alpha; /* alpha */
   				send_msg.m.dhpubkey.Y = Y_B;  /* value of public key */
	     
               			/* Now compute the secret key shared between Alice and Bob */
            			secret_key = ModPower(Y_A, X_B, q);             
               			printf("Computed secret key between Alice and Bob (by User B, Bob ) is %ld\n\r", secret_key); 
        		        /* Send an acknowledgement to Alice that the secret key is computed */ 
			
				status = send(cfd, &send_msg, sizeof(Msg), 0);
  				if (status == -1)
				{
      					fprintf(stderr, "*** Client error: unable to send\n");
     					return;
    				}    
  
              		        break;      
                               
		case REQSERV :	/* Request from Client for Encrypted data */
				printf("\n\nMessage:: REQSERV received from source (%d)\n", recv_msg.hdr.src_addr);
				printf("%s\n", recv_msg.m.buf);
				
				int count;
   				
  				fp = fopen(recv_msg.m.buf, "r"); // read mode
 
  				if( fp == NULL )
   				{
					send_msg.hdr.opcode = REQCOM;
   					send_msg.hdr.src_addr = src_addr;
   					send_msg.hdr.dest_addr = dest_addr;
					strcpy(send_msg.m.buf, "File does not exist!");

					status = send(cfd, &send_msg, sizeof(Msg), 0);
  					if (status == -1)
					{
      						fprintf(stderr, "*** Client error: unable to send\n");
     						return;
    					}
   				}
				else
				{
   					printf("The contents of %s file are :- \n", recv_msg.m.buf);

 					while( (count = fread(buffer, 1, MAX_LEN - 1, fp) ) != 0)
    					{
						buffer[count] = '\0';
						printf("\nPlain Text is:\n\n%s\n", buffer);
						EncryptionAlgorithm(buffer, secret_key);
						printf("\nEncrypted Text is:\n\n%s\n", buffer);
					
						send_msg.hdr.opcode = ENCMSG;
   						send_msg.hdr.src_addr = src_addr;
   						send_msg.hdr.dest_addr = dest_addr;
						strcpy(send_msg.m.buf, buffer);

						status = send(cfd, &send_msg, sizeof(Msg), 0);
  						if (status == -1)
						{
      							fprintf(stderr, "*** Client error: unable to send\n");
     							return;
    						}
    					}
				  								
   					fclose(fp);
				
					send_msg.hdr.opcode = REQCOM;
   					send_msg.hdr.src_addr = src_addr;
   					send_msg.hdr.dest_addr = dest_addr;
					strcpy(send_msg.m.buf, "Request Complete!");

					status = send(cfd, &send_msg, sizeof(Msg), 0);
  					if (status == -1)
					{
      						fprintf(stderr, "*** Client error: unable to send\n");
     						return;
    					}
				}
				break;

		case DISCONNECT :/* Disconnect */
				printf("\n\nMessage:: DISCONNECT received from source (%d)\n", recv_msg.hdr.src_addr);
				printf("%s\n", recv_msg.m.buf);
				send_msg.hdr.opcode = DISCONNECT;
   				send_msg.hdr.src_addr = src_addr;
   				send_msg.hdr.dest_addr = dest_addr;
				strcpy(send_msg.m.buf, "Disconnect...");
				
				status = send(cfd, &send_msg, sizeof(Msg), 0);
  				if (status == -1)
				{
      					fprintf(stderr, "*** Client error: unable to send\n");
     					return;
    				}
				exit(0);

				break;

   	}
   
  }
}