void ServerMessageProcessor::process_conn_req(LSP_Packet& packet)
{
	fprintf(stderr, "ServerMessageProcessor:: Processing Connection Request from %s : %d\n",
			packet.getHostname(), packet.getPort());

	if(!check_conn_req_validity(packet))
	{
		fprintf(stderr, "ServerMessageProcessor:: Connection already exists! Ignoring request from %s : %d\n",
					packet.getHostname(), packet.getPort());
		return;
	}

	/* Create a new connection info object for this client */
	unsigned conn_id = get_next_conn_id();
	ConnInfo *connInfo =
			new ConnInfo(conn_id, packet.getPort(), packet.getHostname());

	/* Lock before modifying! */
    pthread_mutex_lock (&mutex_connInfos);

    connInfos->push_back(connInfo);

    /* Unlock after modifying! */
    pthread_mutex_unlock (&mutex_connInfos);

	/* Send an acknowledgment packet */
	fprintf(stderr, "ServerMessageProcessor:: Pushing ACK packet to Outbox for conn_id: %u\n", conn_id);
	LSP_Packet p(conn_id, 0, 0, NULL);
	LSP_Packet ack_packet = create_ack_packet(p);
	connInfo->add_to_outMsgs(ack_packet);
}
void ServerMessageProcessor::process_found_packet(LSP_Packet& packet)
{
	fprintf(stderr, "ServerMessageProcessor:: Processing Found Request from %s : %d\n", packet.getHostname(), packet.getPort());
	/* Payload of form
	f pass */
	ConnInfo* cInfo = get_conn_info(packet.getConnId());

	/* Send ack to worker. This can come to server only from a worker. */
	assert(cInfo->isIsWorker() == true);
	LSP_Packet ack_packet = create_ack_packet(packet);
	cInfo->add_to_outMsgs(ack_packet);
	fprintf(stderr, "ServerMessageProcessor:: Pushing ACK packet to Outbox for conn_id: %u\n", packet.getConnId());

	if(testing) return;

	/* Remove map entry. */
	int clientId = cInfo->popClients();
	/* Only if client is still alive, send. Otherwise ignore. */
	if(clientWorkerInfo.find(clientId)!=clientWorkerInfo.end());
	{
		clientWorkerInfo.erase(clientId);
		/* Send result to client. */
		ConnInfo* conInfo = get_conn_info(clientId);
		conInfo->incrementSeqNo();
		LSP_Packet client_packet(clientId,conInfo->getSeqNo(),packet.getLen(),packet.getBytes());
		conInfo->add_to_outMsgs(client_packet);
	}
}
int RequestMessageProcessor::process_data_packet(LSP_Packet& packet)
{
	LSP_Packet ack_packet = create_ack_packet(packet);
	switch(packet.getDataType())
	{
		case FOUND:
		{
			/* Print message Found: pass and mark COMPLETE */
			get_conn_info()->add_to_outMsgs(ack_packet);
			char* bytes = (char*)packet.getBytes();
			char pass[5], p[20];
			sscanf(bytes,"%s %s",p,pass);
			fprintf( stderr, "Found: %s\n",pass);
			return COMPLETE;
		}
		case NOTFOUND:
		{
			/* Print NOT FOUND and mark COMPLETE */
			get_conn_info()->add_to_outMsgs(ack_packet);
			fprintf( stderr, "Not Found\n");
			return COMPLETE;
		}
		default:
		{
			fprintf( stderr, "Unknown Data Type!\n");
			packet.print();
		}
	}
}
void ServerMessageProcessor::process_alive_packet(LSP_Packet& packet)
{
	fprintf(stderr, "ServerMessageProcessor:: Processing Alive Request from %s : %d\n", packet.getHostname(), packet.getPort());
	ConnInfo* cInfo = get_conn_info(packet.getConnId());
	//	Send ack packet.
	LSP_Packet ack_packet = create_ack_packet(packet);
	cInfo->add_to_outMsgs(ack_packet);
	fprintf(stderr, "ServerMessageProcessor:: Pushing ACK packet to Outbox for conn_id: %u\n", packet.getConnId());
}
void ServerMessageProcessor::process_join_request_packet(LSP_Packet& packet)
{
	fprintf(stderr, "ServerMessageProcessor:: Processing Join Request from %s : %d\n", packet.getHostname(), packet.getPort());
	ConnInfo* cInfo = get_conn_info(packet.getConnId());
	cInfo->setIsWorker(true);
	assert(cInfo->isIsWorker() == true);
	LSP_Packet ack_packet = create_ack_packet(packet);
	cInfo->add_to_outMsgs(ack_packet);
	fprintf(stderr, "ServerMessageProcessor:: Pushing ACK packet to Outbox for conn_id: %u\n", packet.getConnId());
}
void ServerMessageProcessor::process_not_found_packet(LSP_Packet& packet)
{
	fprintf(stderr, "ServerMessageProcessor:: Processing Not Found Request from %s : %d\n", packet.getHostname(), packet.getPort());
	/* Update map entry. If all have returned 'not found', send message to client,
	remove map entry. */
	ConnInfo* cInfo = get_conn_info(packet.getConnId());
	/* Send ack to worker. This can come to server only from a worker. */
	assert(cInfo->isIsWorker() == true);
	LSP_Packet ack_packet = create_ack_packet(packet);
	cInfo->add_to_outMsgs(ack_packet);
	fprintf(stderr, "ServerMessageProcessor:: Pushing ACK packet to Outbox for conn_id: %u\n", packet.getConnId());

	if(testing) return;

	//	Update map entry.
	int clientId = cInfo->popClients();
	//	If clientId not in maps keys, it means that one of the workers
	//	has already found the password.
	if(clientWorkerInfo.find(clientId) == clientWorkerInfo.end())
	{
		return;
	}
	vector<WorkerInfo> &workers = clientWorkerInfo[clientId];
	//	Update processing status of this worker.
	for(vector<WorkerInfo>::iterator it=workers.begin();
						it!=workers.end(); ++it)
	{
		if((*it).getConnId() == packet.getConnId())
		{
			(*it).setProcessingStatus(-1);
			break;
		}
	}
	//	If all workers have -1 as processing status, send a message to client
	//	as message not found. Format is
	//	x
	bool stillProcessing = false;
	for(vector<WorkerInfo>::iterator it=workers.begin();
							it!=workers.end(); ++it)
	{
		if((*it).getProcessingStatus() != -1)
		{
			stillProcessing = true;
			break;
		}
	}
	if(!stillProcessing)
	{
		send_not_found_packet(clientId);
	}
}
Ejemplo n.º 7
0
static int tftp_send_ack(struct net_device *dev,
			 const unsigned char *server_ipaddr,
			 const unsigned short dst_port,
			 const unsigned short block)
{
	struct net_packet pkt;
	unsigned char data[4];
	u32 data_len;

	data_len = create_ack_packet(block, data, 4);
	memzero(&pkt, sizeof(struct net_packet));
	pkt.f4 = data;
	pkt.f4len = data_len;
	return udp_send(dev, server_ipaddr, dst_port, 30000, &pkt);
}
void ServerMessageProcessor::process_crack_request(LSP_Packet& packet)
{
	fprintf(stderr, "ServerMessageProcessor:: Processing crack Request from %s : %d\n", packet.getHostname(), packet.getPort());
	/*crack request to server comes from request and is of the format
	c sha len */
	/* Send ack to request */
	LSP_Packet ack_packet = create_ack_packet(packet);
	ConnInfo* connInfo = get_conn_info(packet.getConnId());
	connInfo->add_to_outMsgs(ack_packet);
	fprintf(stderr, "ServerMessageProcessor:: Pushing ACK packet to Outbox for conn_id: %u\n", packet.getConnId());

	if(testing) return;

	/* Parse and get appropriate entries about the request */
	uint8_t* bytes = packet.getBytes();
	string s((char*)bytes);
	stringstream iss(s);
	string ignore;
	iss >> ignore;
	string hash;
	iss >> hash;
	string startS;
	iss >> startS;
	string endS;
	iss >> endS;
	int length = strlen(startS.c_str());
	/* Get the least busy worker */
	vector<int> workers = get_least_busy_workers(1);
	/* Check some conditions before assigning to the worker */
	if(workers.empty() || hash.length() != 40)
	{
		LSP_Packet c_pkt(create_not_found_packet(connInfo));
		connInfo->add_to_outMsgs(c_pkt);
		return;
	}
	if(length == 0)
	{
		LSP_Packet c_pkt(create_not_found_packet(connInfo));
		connInfo->add_to_outMsgs(c_pkt);
		return;
	}
	if(length > MAX_PWD_LTH)
	{
		LSP_Packet c_pkt(create_not_found_packet(connInfo));
		connInfo->add_to_outMsgs(c_pkt);
		return;
	}

	if(length<4)
	{
		/*Assign only 1 worker. Sending message to more workers will take more time
		than processing by a single worker for 17576 entries.*/

		/* Get least busy worker and create an empty packet with that connId*/
		int worker = workers[0];
		ConnInfo *cInfo = get_conn_info(worker);

		int end = pow(26,length)-1;
		string startString = numToString(0,length);
        string endString = numToString(end,length);
	    string data = "c " + hash + " "+startString + " " +endString;
	    WorkerInfo w(cInfo->getConnectionId(), 0, hash, startString, endString);
	    /* Send crack request to worker. */
		send_crack_worker_request(packet, cInfo,w, data.c_str());
	}
	else if(length >= 4)
	{
		int start = 0;
		int numPoss = pow(26,length)-1;
		vector<int> workers = get_least_busy_workers(5);
		int workersCount = workers.size();
		int each = ceil(numPoss/workersCount);
		/* Split the work among the workers, update the request and worker information and send crack request to worker  */
		for(int i=0; i<workersCount; ++i)
		{
			ConnInfo* cInfo = get_conn_info(workers[i]);
			int last;
			if(start+each>numPoss)
				last = 	numPoss;
			else
				last = 	start+each;
			string startString = numToString(start,length);
			string endString = numToString(last,length);
			string data = "c " + hash + " "  + startString + " " +endString;
			WorkerInfo w(cInfo->getConnectionId(), 0, hash, startString, endString);
			send_crack_worker_request(packet, cInfo, w, data.c_str());
			start = start + each + 1;
		}
	}
}