Newer
Older
#include <unistd.h>
#include <netdb.h>
#include <regex>
using namespace inaf::oasbo::ConnectionProtocols;
TCPProtocol::TCPProtocol() :
TCPProtocol("127.0.0.1", 9003) {
memset(&cliaddr, 0, sizeof(cliaddr));
memset(&servaddr, 0, sizeof(servaddr));
TCPProtocol::~TCPProtocol() {
closeConnectionToClient();
int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) {
uint8_t *buff = new uint8_t[pack.getPacketStructureByteSize()];
ssize_t rec = ::recv(this->cli_sock, &buff[0], headerSize, MSG_WAITALL);
pack.copyToBinaryPointer(buff, headerSize);
resetPacket(pack, headerSize);
delete buff;
return -1;
int to_be_rcv = pack.getPayloadSize() + pack.getTailSize();
rec += ::recv(this->cli_sock, &buff[headerSize], to_be_rcv, MSG_WAITALL);
resetPacket(pack, headerSize);
delete buff;
return -1;
}
pack.copyToBinaryPointer(&buff[headerSize],
pack.getPayloadSize() + pack.getTailSize(), headerSize);
return m_connectToCli(ip, port);
}
int TCPProtocol::closeConnectionToClient() {
if (cli_sock != -1) {
::close(cli_sock);
cli_sock = -1;
}
return 1;
}
bool TCPProtocol::isConnectedToClient() const {
}
int TCPProtocol::m_connectToCli(std::string ip, int port) {
if ((srv_sock = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
std::cerr << "TCP socket on " << host << " failed" << std::endl;
exit(EXIT_FAILURE);
}
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = ::inet_addr(ip.c_str());
servaddr.sin_port = htons(port);
if (bind(srv_sock, (struct sockaddr*) &servaddr, sizeof(servaddr))
< 0) {
std::cerr << "TCP CONNECTION on " << host << " Bind failed"
<< std::endl;
srv_sock = -1;
exit(EXIT_FAILURE);
std::cout << "TCP CONNECTION: Waiting for connection on " << host
<< std::endl;
socklen_t clientAddressSize = sizeof(cliaddr);
if ((cli_sock = accept(srv_sock, (struct sockaddr*) &cliaddr,
(socklen_t*) &clientAddressSize)) < 0) {
std::cerr << "TCP CONNECTION: accept error" << std::endl;
cli_sock = -1;
std::cout << "TCP CONNECTION: Connected with " << inet_ntoa(cliaddr.sin_addr) << std::endl;
void TCPProtocol::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.getPacketStructureByteSize()), bytes);
pack.copyToBinaryPointer(buff, toBeReset);
delete buff;
}
std::string TCPProtocol::getHost() {
return ip + ":" + std::to_string(port);
void TCPProtocol::setHost(std::string host) {
std::string ip = { };
} else {
std::cerr
<< "Connection Protocol Error: invalid IP address and port format: "
<< host << std::endl;
std::regex pattern("^(\\d{1,3}\\.){3}\\d{1,3}$");
// Check if the input string matches the expected format
bool ok = true;
if (!std::regex_match(ip, pattern)) {
std::cerr << "Connection Protocol Error: invalid IP address: " << ip
<< ", setting to 127.0.0.1" << std::endl;
ok = false;
this->ip = std::string("127.0.0.1");
}
void TCPProtocol::setPort(int port) {
if (port > 1023 && port < 65535)
std::cerr << "Connection Protocol Error: invalid port: " << port
}
bool TCPProtocol::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)) {
return false;
}
// 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);
return true;
}