Skip to content
UDP_Protocol.cpp 6.81 KiB
Newer Older
#include <iostream>
#include <string>
#include <sys/socket.h>
Valerio Pastore's avatar
Valerio Pastore committed
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <regex>
#include <limits>
#include <algorithm>
Valerio Pastore's avatar
Valerio Pastore committed

#include <UDP_Protocol.h>
#include <RecognizedPacket.h>
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
using namespace inaf::oasbo::ConnectionProtocols;
Valerio Pastore's avatar
Valerio Pastore committed

UDPProtocol::UDPProtocol() {
	this->host = std::string("127.0.0.1:9003");
	std::string port { };
	split_ip_port(host, ip, port);
	this->port = std::stoi(port);
	this->srv_sock = -1;
	this->cli_sock = -1;
Valerio Pastore's avatar
Valerio Pastore committed
	memset(&cliaddr, 0, sizeof(cliaddr));
	memset(&srvaddr, 0, sizeof(srvaddr));
	// Filling server information
	srvaddr.sin_family = AF_INET; // IPv4
	srvaddr.sin_addr.s_addr = inet_addr(ip.c_str());
	srvaddr.sin_port = htons(this->port);

}
UDPProtocol::UDPProtocol(std::string ip, int port) {
	this->host = std::string(ip).append(":").append(std::to_string(port));
	this->ip = ip;
	this->port = port;
	this->srv_sock = -1;
	this->cli_sock = -1;
	memset(&cliaddr, 0, sizeof(cliaddr));
	memset(&srvaddr, 0, sizeof(srvaddr));
	// Filling server information
	srvaddr.sin_family = AF_INET; // IPv4
	srvaddr.sin_addr.s_addr = inet_addr(ip.c_str());
	srvaddr.sin_port = htons(this->port);
}
UDPProtocol::~UDPProtocol() {
	closeConnectionToClient();
	closeConnectionToServer();
}

int UDPProtocol::sendToServer(PacketLib::BasePacket &pack) {
	int val = ::sendto(cli_sock, pack.getBinaryPointer(),
			pack.getHeaderSize() + pack.getPayloadSize() + pack.getTailSize(),
			MSG_CONFIRM, (struct sockaddr*) &srvaddr, sizeof(srvaddr));
	return val;
}
int UDPProtocol::receiveAtLeastHeaderSizeBytes(uint8_t *buff, int headerSize, int packetSize) {
	int bytercv = 0;
	socklen_t len = sizeof(cliaddr);
	while (bytercv < headerSize) {
		int rcv = recvfrom(srv_sock, &buff[bytercv], packetSize,
		MSG_WAITFORONE, (struct sockaddr*) &cliaddr, &len);
		bytercv += rcv;
		if (rcv < 0) { // error in receiving, reset the buffer and try again
			std::memset(buff, 0 , bytercv+1);
			bytercv = 0;
			continue;
		}
	}
	return bytercv;
}


int UDPProtocol::receiveFromClient(PacketLib::BasePacket &pack) {
	bool headerFlag = false;
	int headerSize = pack.getHeaderSize();
	int packSize = pack.getPacketSize();
	int tailSize = pack.getTailSize();
	ssize_t length = 0;
	ssize_t totPacketSize = 0;
	ssize_t to_be_received = 0;
	uint8_t *buff = new uint8_t[headerSize-1 + packSize]; //max receivable in receiveAtLeast.. = headerSize-1 + packetSize
	int tot_byte_rcv = 0;
	while(true){ // blocking call, the function returns only when a well formed packet has been received
		while(!headerFlag){ // until the header has not received
			tot_byte_rcv = receiveAtLeastHeaderSizeBytes(buff,headerSize, packSize); // receive at least headerSize byte, maximum packSize for each udp rcv.
			if(!isHeaderWellFormed(buff)){ // reset buffer and try again
				std::memset(buff,0,tot_byte_rcv);
				continue;
			headerFlag = true;
			pack.copyToBinaryPointer(buff, headerSize); //copy the header into packet to be able to read it.
			length = pack["length"].value() - 1;
			totPacketSize = headerSize + length + tailSize;
			to_be_received = totPacketSize - tot_byte_rcv; // Calculate how much is still left to read
		}

		if (to_be_received == 0){ // whole packet has been received.
			pack.copyToBinaryPointer(&buff[headerSize],tot_byte_rcv-headerSize, headerSize); // copy the buffer into packet except already copied header
			delete buff;
			return tot_byte_rcv;
		}
		if (to_be_received < 0) {	// error,received more bytes then expected.
			std::memset(buff,0,tot_byte_rcv);
			headerFlag = false;
			continue;
		}
		uint8_t *tmp_buff = new uint8_t[packSize*2 + headerSize]; // *2 + headerSize to avoid overflow;
		int rcv = receiveAtLeastHeaderSizeBytes(tmp_buff, headerSize, packSize);
		tot_byte_rcv+=rcv;
		to_be_received-=rcv;
		if(isHeaderWellFormed(tmp_buff)){ //another header received, save it and discard previous data.
			std::memset(buff,0,tot_byte_rcv-rcv);
			std::memcpy(buff, tmp_buff, rcv);
			headerFlag = true;
			pack.copyToBinaryPointer(buff, headerSize);
			length = pack["length"].value() - 1;
			totPacketSize = headerSize + length + tailSize;
			to_be_received = totPacketSize - rcv;
		}
		else{ // append to current buff
			std::memcpy(&buff[tot_byte_rcv-rcv], tmp_buff, rcv);
		delete tmp_buff;
Valerio Pastore's avatar
Valerio Pastore committed
}

int UDPProtocol::connectToClient() {
	int sockfd;

	// Creating socket file descriptor
	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
Valerio Pastore's avatar
Valerio Pastore committed
		perror("socket creation failed");
		return -1;
	}

	// Set timeout to the socket
	struct timeval tv;
	tv.tv_sec = std::numeric_limits<time_t>::max();
Valerio Pastore's avatar
Valerio Pastore committed
	tv.tv_usec = 0;
	setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv, sizeof tv);
Valerio Pastore's avatar
Valerio Pastore committed

	// Bind the socket with the server address
	if (bind(sockfd, (const struct sockaddr*) &srvaddr, sizeof(srvaddr)) < 0) {
Valerio Pastore's avatar
Valerio Pastore committed
		perror("bind failed");
		return -1;
	}

	this->srv_sock = sockfd;
Valerio Pastore's avatar
Valerio Pastore committed
	return sockfd;

}
int UDPProtocol::connectToServer() {
Valerio Pastore's avatar
Valerio Pastore committed
	int sockfd = 0;
	// Creating socket file descriptor
	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
Valerio Pastore's avatar
Valerio Pastore committed
		perror("socket creation failed");
		exit(EXIT_FAILURE);
	}

	printf("Connection made\n");
	this->cli_sock = sockfd;
Valerio Pastore's avatar
Valerio Pastore committed

	return sockfd;
}

int UDPProtocol::closeConnectionToClient() {
	if (srv_sock != -1) {
		::close(srv_sock);
		srv_sock = -1;
	return 1;
}
int UDPProtocol::closeConnectionToServer() {
	if (cli_sock != -1) {
		::close(cli_sock);
		cli_sock = -1;
	}
	return 1;
bool UDPProtocol::isConnectedToClient() const {
	return srv_sock != -1;
}
bool UDPProtocol::isConnectedToServer() const {
	return cli_sock != -1;
bool UDPProtocol::isHeaderWellFormed(uint8_t *buff) {
	namespace recgnized = inaf::oasbo::ConnectionProtocols::UDProtocolRecognizedPacket;

	size_t type = buff[1] >> 4;;
	size_t subtype =  buff[1] & 0x0F;
	ssize_t length = (buff[4] << 8) + buff[5];
	std::tuple<size_t, size_t, size_t> target = std::make_tuple(type, subtype, length);
	auto it = std::find(recgnized::my_vec.begin(), recgnized::my_vec.end(),
			target);
	if (it != recgnized::my_vec.end())
		return true;
	else
		return false;
}
void UDPProtocol::resetPacket(PacketLib::BasePacket &pack, int bytes) {
	uint8_t *buff = new uint8_t[bytes];
	std::memset(buff, 0, bytes);
	int toBeReset = std::min(static_cast<int>(pack.getPacketSize()), bytes);
	pack.copyToBinaryPointer(buff, toBeReset);
	delete buff;
void UDPProtocol::split_ip_port(const std::string &ip_port,
		std::string &ip_address, std::string &port) {
	// Regex pattern to match IP address and port in the format "xxx.xxx.xxx.xxx:xxxx"
	std::regex pattern("^(\\d{1,3}\\.){3}\\d{1,3}:(\\d{1,5})$");
	// Check if the input string matches the expected format
	if (!std::regex_match(ip_port, pattern)) {
		std::cerr << "Error: invalid IP address and port format: " << ip_port
				<< std::endl;
		exit(EXIT_FAILURE);
	}
Valerio Pastore's avatar
Valerio Pastore committed

	// Split the input string into IP address and port
	int colon_pos = ip_port.find(":");
	ip_address = ip_port.substr(0, colon_pos);
	port = ip_port.substr(colon_pos + 1);
}