예제 #1
0
void bind_udp(int sender,int sockfd,struct sockaddr *addr)
{

	uint16_t hostport;
	uint16_t dstport;
	uint32_t host_IP_netformat;
	uint32_t dst_IP_netformat;
	int index;


	struct sockaddr_in *address;
	address = (struct sockaddr_in *) addr;
	/** TODO lock access to the jinnisockets */

	index = findjinniSocket(sender,sockfd);
	/** TODO unlock access to the jinnisockets */
		if (index == -1)
				{
					PRINT_DEBUG("socket descriptor not found into jinni sockets");
					exit(1);
				}

	if (address->sin_family != AF_INET )
	{
		PRINT_DEBUG("Wrong address family");
		sem_wait(jinniSockets[index].s);
		nack_write(jinniSockets[index].jinniside_pipe_ds,sender,sockfd);
			sem_post(jinniSockets[index].as);
		sem_post(jinniSockets[index].s);
	}

	/**TODO lock the jinni sockets */



	if (index == -1)
			{
				PRINT_DEBUG("socket descriptor not found into jinni sockets");
				exit(1);
			}
/** TODO fix host port below, it is not initialized with any variable !!! */
/** the check below is to make sure that the port is not previously allocated */
	hostport = ntohs(address->sin_port);
	host_IP_netformat = (address->sin_addr).s_addr;
/** check if the same port and address have been both used earlier or not
 * it returns (-1) in case they already exist, so that we should not reuse them
 * */
	if (checkjinniports(hostport, host_IP_netformat) == -1)
		{
			PRINT_DEBUG("this port is not free");
			sem_wait(jinniSockets[index].s);
			nack_write(jinniSockets[index].jinniside_pipe_ds,sender,sockfd);
				sem_post(jinniSockets[index].as);
			sem_post(jinniSockets[index].s);

				free(addr);
				return;
		}

	/**TODO check if the port is free for binding or previously allocated
	 * Current code assume that the port is authorized to be accessed
	 * and also available
	 * */
	/** Reverse again because it was reversed by the application itself */
		//hostport = ntohs(address->sin_port);
/** TODO lock and unlock the protecting semaphores before making
 * any modiciations to the contents of the jinniSockets database
 */
PRINT_DEBUG("%d,%d,%d",(address->sin_addr).s_addr, ntohs(address->sin_port),
		address->sin_family);

	jinniSockets[index].hostport = ntohs(address->sin_port);
	jinniSockets[index].host_IP = (address->sin_addr).s_addr;

	/** Reverse again because it was reversed by the application itself
	 * In our example it is not reversed */
	//jinniSockets[index].host_IP.s_addr = ntohl(jinniSockets[index].host_IP.s_addr);

	/** TODO convert back to the network endian form before
	 * sending to the fins core
	 */

	sem_wait(jinniSockets[index].s);
	ack_write(jinniSockets[index].jinniside_pipe_ds,sender,sockfd);
		sem_post(jinniSockets[index].as);
	sem_post(jinniSockets[index].s);

	free(addr);
	return;
}
예제 #2
0
void send_udp(unsigned long long uniqueSockID, int socketCallType, int datalen,
		u_char *data, int flags) {

	uint16_t hostport;
	uint16_t dstport;
	uint32_t host_IP;
	uint32_t dst_IP;
	int len = datalen;
	int index;

	if (flags == -1000) {

		return (write_udp(uniqueSockID, socketCallType, datalen, data));

	}
	/** TODO handle flags cases */
	switch (flags) {
	case MSG_CONFIRM:
	case MSG_DONTROUTE:
	case MSG_DONTWAIT:
	case MSG_EOR:
	case MSG_MORE:
	case MSG_NOSIGNAL:
	case MSG_OOB: /** case of recieving a (write call)*/
	default:
		break;
	} // end of the switch clause

	PRINT_DEBUG("");

	index = findjinniSocket(uniqueSockID);
	if (index == -1) {
		PRINT_DEBUG("CRASH !! socket descriptor not found into jinni sockets");
		return;
	}

	/** copying the data passed to be able to free the old memory location
	 * the new created location is the one to be included into the newly created finsFrame*/
	PRINT_DEBUG("");
	/** check if this socket already connected to a destined address or not */

	if (jinniSockets[index].connection_status == 0) {
		/** socket is not connected to an address. Send call will fail */

		PRINT_DEBUG("socketjinni failed to accomplish send");
		nack_send(uniqueSockID, socketCallType);
		return;
	}

	/** Keep all ports and addresses in host order until later  action taken */
	dstport = jinniSockets[index].dstport;

	dst_IP = jinniSockets[index].dst_IP;

	//hostport = jinniSockets[index].hostport;
	//hostport = 3000;

	/** addresses are in host format given that there are by default already filled
	 * host IP and host port. Otherwise, a port and IP has to be assigned explicitly below */

	/**
	 * the current value of host_IP is zero but to be filled later with
	 * the current IP using the IPv4 modules unless a binding has occured earlier
	 */
	host_IP = jinniSockets[index].host_IP;

	/**
	 * Default current host port to be assigned is 58088
	 * It is supposed to be randomly selected from the range found in
	 * /proc/sys/net/ipv4/ip_local_port_range
	 * default range in Ubuntu is 32768 - 61000
	 * The value has been chosen randomly when the socket firstly inserted into the jinnisockets
	 * check insertjinniSocket(processid, sockfd, fakeID, type, protocol);
	 */
	hostport = jinniSockets[index].hostport;
	if (hostport == (uint16_t)(-1)) {
		while (1) {
			hostport = randoming(MIN_port, MAX_port);
			if (checkjinniports(hostport, host_IP)) {
				break;
			}
		}
		jinniSockets[index].hostport = hostport;
	}

	PRINT_DEBUG("");

	PRINT_DEBUG("addr %d,%d,%d,%d", dst_IP, dstport, host_IP, hostport);
	//free(data);
	//free(addr);
	PRINT_DEBUG("");

	/** the meta-data paraters are all passes by copy starting from this point
	 *
	 */
	if (jinni_UDP_to_fins(data, len, dstport, dst_IP, hostport, host_IP) == 1)

	{
		PRINT_DEBUG("");
		/** TODO prevent the socket interceptor from holding this semaphore before we reach this point */
		PRINT_DEBUG("");

		ack_send(uniqueSockID, socketCallType);
		PRINT_DEBUG("");

	} else {
		PRINT_DEBUG("socketjinni failed to accomplish send");
		nack_send(uniqueSockID, socketCallType);
	}
}// end of send_udp
예제 #3
0
void sendto_udp(unsigned long long uniqueSockID, int socketCallType,
		int datalen, u_char *data, int flags, struct sockaddr_in *addr,
		socklen_t addrlen) {

	uint16_t hostport;
	uint16_t dstport;
	uint32_t host_IP;
	uint32_t dst_IP;

	int len = datalen;
	int index;
	int i;

	struct in_addr *temp;

	PRINT_DEBUG();

	/** TODO handle flags cases */
	switch (flags) {
	case MSG_CONFIRM:
	case MSG_DONTROUTE:
	case MSG_DONTWAIT:
	case MSG_EOR:
	case MSG_MORE:
	case MSG_NOSIGNAL:
	case MSG_OOB: /** case of recieving a (write call)*/
	default:
		break;

	}
	PRINT_DEBUG("");

	index = findjinniSocket(uniqueSockID);
	if (index == -1) {
		PRINT_DEBUG("CRASH !! socket descriptor not found into jinni sockets");
		return;
	}

	if (addr->sin_family != AF_INET) {
		PRINT_DEBUG("Wrong address family");
		nack_send(uniqueSockID, socketCallType);
		return;
	}

	/** copying the data passed to be able to free the old memory location
	 * the new created location is the one to be included into the newly created finsFrame*/
	PRINT_DEBUG("");

	dst_IP = ntohl(addr->sin_addr.s_addr);/** it is in network format since application used htonl */
	/** addresses are in host format given that there are by default already filled
	 * host IP and host port. Otherwise, a port and IP has to be assigned explicitly below */

	/** Keep all ports and addresses in host order until later  action taken */
	dstport = ntohs(addr->sin_port); /** reverse it since it is in network order after application used htons */

	/**
	 * the current value of host_IP is zero but to be filled later with
	 * the current IP using the IPv4 modules unless a binding has occured earlier
	 */
	host_IP = jinniSockets[index].host_IP;

	/**
	 * Default current host port to be assigned is 58088
	 * It is supposed to be randomly selected from the range found in
	 * /proc/sys/net/ipv4/ip_local_port_range
	 * default range in Ubuntu is 32768 - 61000
	 * The value has been chosen randomly when the socket firstly inserted into the jinnisockets
	 * check insertjinniSocket(processid, sockfd, fakeID, type, protocol);
	 */
	hostport = jinniSockets[index].hostport;
	if (hostport == (uint16_t)(-1)) {
		while (1) {
			hostport = randoming(MIN_port, MAX_port);
			if (checkjinniports(hostport, host_IP)) {
				break;
			}
		}
		jinniSockets[index].hostport = hostport;
	}

	PRINT_DEBUG("");

	PRINT_DEBUG("index=%d, dst=%d/%d, host=%d/%d", index, dst_IP, dstport,
			host_IP, hostport);

	temp = (struct in_addr *) malloc(sizeof(struct in_addr));
	temp->s_addr = host_IP;
	PRINT_DEBUG("index=%d, dst=%s/%d, host=%s/%d", index, inet_ntoa(
			addr->sin_addr), dstport, inet_ntoa(*temp), hostport);
	//free(data);
	//free(addr);
	PRINT_DEBUG("");

	/** the meta-data parameters are all passes by copy starting from this point
	 *
	 */
	if (jinni_UDP_to_fins(data, len, dstport, dst_IP, hostport, host_IP) == 1)

	{
		/** TODO prevent the socket interceptor from holding this semaphore before we reach this point */
		PRINT_DEBUG("");

		ack_send(uniqueSockID, socketCallType);
		PRINT_DEBUG("");

	} else {
		PRINT_DEBUG("socketjinni failed to accomplish sendto");
		nack_send(uniqueSockID, socketCallType);
	}

	return;

} //end of sendto_udp
예제 #4
0
void bind_udp(unsigned long long uniqueSockID, struct sockaddr_in *addr) {

	uint16_t hostport;
	uint16_t dstport;
	uint32_t host_IP_netformat;
	uint32_t dst_IP_netformat;
	int index;

	index = findjinniSocket(uniqueSockID);
	if (index == -1) {
		PRINT_DEBUG("socket descriptor not found into jinni sockets");
		return;
	}

	if (addr->sin_family != AF_INET) {
		PRINT_DEBUG("Wrong address family");
		nack_send(uniqueSockID, bind_call);
		return;
	}

	/** TODO fix host port below, it is not initialized with any variable !!! */
	/** the check below is to make sure that the port is not previously allocated */
	hostport = ntohs(addr->sin_port);
	host_IP_netformat = (addr->sin_addr).s_addr;
	/** check if the same port and address have been both used earlier or not
	 * it returns (-1) in case they already exist, so that we should not reuse them
	 * */
	if (!checkjinniports(hostport, host_IP_netformat)
			&& !jinniSockets[index].sockopts.FSO_REUSEADDR) {
		PRINT_DEBUG("this port is not free");
		nack_send(uniqueSockID, bind_call);

		free(addr);
		return;
	}

	/**TODO check if the port is free for binding or previously allocated
	 * Current code assume that the port is authorized to be accessed
	 * and also available
	 * */
	/** Reverse again because it was reversed by the application itself */
	//hostport = ntohs(addr->sin_port);


	/** TODO lock and unlock the protecting semaphores before making
	 * any modifications to the contents of the jinniSockets database
	 */
	PRINT_DEBUG("bind address: %d,%d,%d", (addr->sin_addr).s_addr, ntohs(
			addr->sin_port), addr->sin_family);
	PRINT_DEBUG("bind address: %d, %s/%d", addr->sin_family, inet_ntoa(
			addr->sin_addr), ntohs(addr->sin_port));
	/**
	 * Binding
	 */
	sem_wait(&jinniSockets_sem);
	jinniSockets[index].hostport = ntohs(addr->sin_port);
	jinniSockets[index].host_IP = (addr->sin_addr).s_addr;
	sem_post(&jinniSockets_sem);

	/** Reverse again because it was reversed by the application itself
	 * In our example it is not reversed */
	//jinniSockets[index].host_IP.s_addr = ntohl(jinniSockets[index].host_IP.s_addr);

	/** TODO convert back to the network endian form before
	 * sending to the fins core
	 */

	PRINT_DEBUG("bind: index:%d, host:%d/%d, dst:%d/%d", index,
			jinniSockets[index].host_IP, jinniSockets[index].hostport,
			jinniSockets[index].dst_IP, jinniSockets[index].dstport);
	ack_send(uniqueSockID, bind_call);

	free(addr);
	return;

} // end of bind_udp