serverState handshake(){

	fprintf(stderr , "-----------------------------------------------------------\n **** Waiting for handshake ****\n\n");	
	
	char buffer[MSG_MAX_SIZE];
	
	int length = readFromPipe(inputChannel ,(byte *) buffer);
				
	printMsg("Client to server ---> " , buffer , length);	
	
	if(strncmp(buffer , ClientOpenConnection , length) != 0 || length != strlen(ClientOpenConnection))
	{
		fprintf(stderr, "Handshake read error\n");
		return ERROR;
	}
		
	if(writeInPipe(outputChannel,(byte *) ServerOpenConnection , strlen(ServerOpenConnection)) < 0)
	{
		fprintf(stderr, "Handshake write error\n");
		return ERROR;
	}
	
	printMsg("Server to client ---> " , ServerOpenConnection , strlen(ServerOpenConnection));
	
	return HANDSHAKE;
}
void closeConnection(int inputChannel , int outputChannel){
	
	char msg[2048];
	
	fprintf(stderr,"Sending closing connection message : %s\n" , ClientCloseConnection);
	
	if(writeInPipe(outputChannel, (byte *) ClientCloseConnection , strlen(ClientCloseConnection)) < 0)
	{
		perror("Closing client error");
		return;	
	}
	
	fprintf(stderr , "message written , waiting for response\n");
	
	readFromPipe(inputChannel ,(byte *) msg);
	
	if(strncmp(msg , ServerCloseConnection , strlen(ServerCloseConnection)) != 0)
	{
		perror("Closing client error");
		return;
	}
	
	fprintf(stderr , "Connection closed\n\n");
	
	closeChannel(inputChannel);
	closeChannel(outputChannel);

}
int askConnection(int inputChannel, int outputChannel){
	
	char msg[2048];
	
	fprintf(stderr,"Asking for connection\n");
	
	if(writeInPipe(outputChannel, (byte *) ClientOpenConnection , strlen(ClientOpenConnection)) < 0)
	{
		perror("handshake client error");
		return -1;	
	}

	readFromPipe(inputChannel ,(byte *) msg);
	
	if(strncmp(msg , ServerOpenConnection , strlen(ServerOpenConnection)) != 0)
	{
		perror("handshake client error");
		return -1;
	}
		
	return 1;
}
Beispiel #4
0
void Interface::test() {
	std::cout << "Testing Interface Class START!..." << std::endl;
	std::cout << "TData Path: " << dataPath << std::endl;
	std::cout << "Fifo IN Path: " << pathFifoIn << std::endl;
	std::cout << "Fifo OUT Path: " << pathFifoOut << std::endl;

	std::cout << "Max Number of Frames: " << maxNumFrame << " Max Number of Actions: " << numAction << std::endl;
	for(int i = 0; i < numAction; ++i) {
		std::cout << "Index: " << i << " Action: " << ind2Act[i] << std::endl;
		std::cout << "Action: " << ind2Act[i] << " Index: " << act2Ind[ind2Act[i]] << std::endl;
	}

	std::cout << "Reset Button:Interface:: " << resetButton << std::endl;
	std::cout << "Num frame stack: " << numFrmStack << std::endl;
	std::cout << "crop Height: " << cropH << std::endl;
	std::cout << "crop Width: " << cropW << std::endl;
	std::cout << "crop Left: " << cropL << std::endl;
	std::cout << "crop Top: " << cropT << std::endl;

	std::cout << "GEN frame info Path: " << frmInfo << std::endl;
	std::cout << "GEN Episode info Path: " << EpInfo << std::endl;
	std::cout << "Opening and Initializing pipe: " << std::endl;
	openPipe();
	initInPipe();
	initOutPipe();
	std::cout << "Writing Random actions to InPipe and reading from OutPipe with data storage..." << std::endl;
	while(!isToEnd()) {
		resetVals(0);
		int x = rand()%numAction;
		writeInPipe(toString(ind2Act[x]));
		readFromPipe();
		saveFrameInfo();
		saveEpisodeInfo();
	}
	finalizePipe();
	std::cout << "Testing Interface Class END!..." << std::endl;
}
int encrypt_and_send(byte * msg , const int length){
		
	if(encType == PLAIN)	//send plain text
	{
		if(writeInPipe(outputChannel, (byte *) msg , length) < 0)
		{
			fprintf(stderr , " **** Communication phase write error **** \n\n");
			return -1;	
		}	
		return 0;
	}
	
	if(encType == RSA512){
		BIGNUM *message = BN_new();	
		
		byte fullMsg[RSA512_BYTE_LENGTH];
		
		memset(fullMsg , 0 , sizeof(byte) * RSA512_BYTE_LENGTH);
		memcpy(fullMsg , msg , sizeof(byte) * length);
		
		message = BN_bin2bn((const unsigned char *) fullMsg, RSA512_BYTE_LENGTH , NULL);
		
		rsaEXP(message , &client512Rsa);	//Encrypt with server RSA public key
		
		BN_bn2bin(message , fullMsg);

		if(writeInPipe(outputChannel, (byte *) fullMsg , RSA512_BYTE_LENGTH) < 0)
		{
			fprintf(stderr , " **** Communication phase write error **** \n\n");
			return -1;	
		}
		
		printf("RSA512 : %s\n", BN_bn2hex(message));
		BN_free(message);		
		return 0;
	}
	
	if(encType == RSA64){
		BIGNUM *message = BN_new();
		
		byte fullMsg[RSA64_BYTE_LENGTH];
		
		memset(fullMsg , 0 , sizeof(byte) * RSA64_BYTE_LENGTH);
		memcpy(fullMsg , msg , sizeof(byte) * length);
		
		message = BN_bin2bn((const unsigned char *) fullMsg, RSA64_BYTE_LENGTH , NULL);
		
		rsaEXP(message , &client64Rsa);	//Encrypt with server RSA public key
				
		BN_bn2bin(message , fullMsg);

		if(writeInPipe(outputChannel, (byte *) fullMsg , RSA64_BYTE_LENGTH) < 0)
		{
			fprintf(stderr , " **** Communication phase write error **** \n\n");
			return -1;	
		}
		
		printf("RSA64 : %s\n", BN_bn2hex(message));
		BN_free(message);		
		return 0;
	}
	
}