Example #1
0
void avanzar(int *posicion, int *carril, int color)
{
	int valor_devuelto, tpos = 0, tcarril = 0;
	int apos = *posicion;
	int acarril = *carril;
	HANDLE posiciones[3]; //Vamos a añadir los dif. eventos por los que esperaremos

						  //EVENTOS A ESPERAR: EVENTO_ACABAR, MUTEX_POSICION_ADELANTE, MUTEX_POSICION_CAMBIO_DE_CARRIL

						  //Añadimos el evento acabar para detenernos y no avanzar mas*/
	posiciones[0] = IPC.evento_acabar;

	/*Vemos si estamos en alguna de las 4 posiciones especiales para compartir los mutex*/
	if (apos + 1 != traductor(apos + 1, acarril))
		posiciones[1] = IPC.posiciones[traductor(apos + 1, acarril)];
	else if (apos != 136) //Si no estamos en la posicion 136 solicitamos el mutex de la posicion siguiente
		posiciones[1] = IPC.posiciones[apos + acarril * 137 + 1];
	else
		posiciones[1] = IPC.posiciones[acarril * 137]; //Si estamos en la posicion 136 solicitamos el mutex de la primera posicion

													   //Añadimos el evento cambio de carril si procede
	if (cambio(apos, acarril, &tpos, &tcarril))
	{
		posiciones[2] = IPC.posiciones[tpos + tcarril * 137];
		valor_devuelto = WaitForMultipleObjectsEx(3, posiciones, FALSE, INFINITE, TRUE);
	}
	else
		valor_devuelto = WaitForMultipleObjectsEx(2, posiciones, FALSE, INFINITE, TRUE);

	switch (valor_devuelto)
	{
	case WAIT_OBJECT_0 + 0:
		ExitThread(7);
		break;
	case WAIT_OBJECT_0 + 1:
		DLL.avanza_coche(carril, posicion, color);
		break;
	case WAIT_OBJECT_0 + 2:
		DLL.cambio_carril(carril, posicion, color);
		break;
	}

	/*Si estamos en alguna posicion con mutex compartido lo liberamos llamando a traductor*/
	if (traductor(apos, acarril) != apos)
		ReleaseMutex(IPC.posiciones[traductor(apos, acarril)]);
	else
		ReleaseMutex(IPC.posiciones[apos + acarril * 137]);
}
Example #2
0
int main(int argc, char *argv[]){
	int k_iter;				//Key iteration
	int hkdf_iter;		//Derivation Key iteration
	int borne_start;	//create rand iteration between borne_start and borne_stop
	int borne_stop;
	char *alea;				//Alea from PRBG
	char *key_init;		//First key from PRBG
	char *key;					//Final key from Deruvation Function
	char *Ns;					//Serial number, the MAC adress here
	char *code;				//Final returned code
	int space = 6;		//Space between two '-'
	
	/**** If you want to enter the iteration by hand **
	if(argc < 3){printf("./%s <key iteration> <hkdf iteration>",argv[0]);return 1;}
	k_iter = argv[1];
	hkdf_iter = argv[2];
	/******** Otherwise it's a random ************************/
	srand(time(NULL));
	borne_start = 2560; //just to have 3 char with a least 1 alpha
	borne_stop = 4098;
	k_iter = rand()%(borne_stop - borne_start) + borne_start;
	hkdf_iter = rand()%(borne_stop - borne_start) + borne_start;
	/**************************************************/
	
	alea = (char *)malloc(sizeof(char)*ALEA_SIZE);
	key_init = (char *)malloc(sizeof(char)*HASH_SIZE);
	key = (char *)malloc(sizeof(char)*HASH_SIZE);
	Ns = (char *)malloc(sizeof(char)*MAX);
	code = (char *)malloc(sizeof(char)*MAX);
	/*** Gen Alea **/
	PRBG_RSA(&alea, k_iter);
	
	/*** Hash alea **/
	hash_function(alea,&key_init);

	/*** Get MAC adress for Ns ***/
	get_mac_adress(&Ns);
	
	/*** Key derivation **/
	HKDF(key_init, Ns, hkdf_iter, &key);

	/*** Traductor ***/
	traductor(&key, k_iter, hkdf_iter, &code, space);
	
	printf("The serial number is your MAC address...\nYour Serial Number is : %s\nYour Code is : %s\n",Ns,code);
	free(alea);
	free(key_init);
	free(key);
	free(Ns);
	free(code);
	return 0;
}