2021-03-08 23:00:53 +01:00
|
|
|
#include "TcWinTcpServer.h"
|
|
|
|
#include "../../serviceinterface/ServiceInterface.h"
|
|
|
|
#include <winsock2.h>
|
2021-03-09 00:37:42 +01:00
|
|
|
#include <ws2tcpip.h>
|
2021-03-08 23:00:53 +01:00
|
|
|
|
2021-03-09 00:37:42 +01:00
|
|
|
const std::string TcWinTcpServer::DEFAULT_TCP_SERVER_PORT = "7301";
|
|
|
|
const std::string TcWinTcpServer::DEFAULT_TCP_CLIENT_PORT = "7302";
|
2021-03-08 23:55:58 +01:00
|
|
|
|
|
|
|
TcWinTcpServer::TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge,
|
2021-03-09 00:37:42 +01:00
|
|
|
std::string customTcpServerPort):
|
|
|
|
SystemObject(objectId), tcpPort(customTcpServerPort) {
|
|
|
|
if(tcpPort == "") {
|
|
|
|
tcpPort = DEFAULT_TCP_SERVER_PORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t TcWinTcpServer::initialize() {
|
|
|
|
int retval = 0;
|
|
|
|
struct addrinfo *addrResult = nullptr;
|
|
|
|
struct addrinfo hints;
|
2021-03-08 23:55:58 +01:00
|
|
|
/* Initiates Winsock DLL. */
|
|
|
|
WSAData wsaData;
|
|
|
|
WORD wVersionRequested = MAKEWORD(2, 2);
|
|
|
|
int err = WSAStartup(wVersionRequested, &wsaData);
|
|
|
|
if (err != 0) {
|
2021-03-09 00:37:42 +01:00
|
|
|
/* Tell the user that we could not find a usable Winsock DLL. */
|
2021-03-08 23:55:58 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2021-03-09 00:37:42 +01:00
|
|
|
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: WSAStartup failed with error: " <<
|
|
|
|
err << std::endl;
|
2021-03-08 23:55:58 +01:00
|
|
|
#endif
|
2021-03-09 00:37:42 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
2021-03-08 23:55:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 00:37:42 +01:00
|
|
|
ZeroMemory(&hints, sizeof (hints));
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2021-03-08 23:55:58 +01:00
|
|
|
|
2021-03-09 00:37:42 +01:00
|
|
|
retval = getaddrinfo(nullptr, tcpPort.c_str(), &hints, &addrResult);
|
|
|
|
if (retval != 0) {
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
|
|
|
sif::warning << "TcWinTcpServer::TcWinTcpServer: Retrieving address info failed!" <<
|
|
|
|
std::endl;
|
|
|
|
#endif
|
|
|
|
handleError(ErrorSources::GETADDRINFO_CALL);
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
2021-03-08 23:55:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 00:37:42 +01:00
|
|
|
/* Open TCP (stream) socket */
|
|
|
|
listenerTcpSocket = socket(addrResult->ai_family, addrResult->ai_socktype,
|
|
|
|
addrResult->ai_protocol);
|
|
|
|
if(listenerTcpSocket == INVALID_SOCKET) {
|
2021-03-08 23:00:53 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
|
|
|
sif::warning << "TcWinTcpServer::TcWinTcpServer: Socket creation failed!" << std::endl;
|
|
|
|
#endif
|
2021-03-09 00:37:42 +01:00
|
|
|
freeaddrinfo(addrResult);
|
|
|
|
handleError(ErrorSources::SOCKET_CALL);
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
2021-03-08 23:00:53 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 00:37:42 +01:00
|
|
|
// retval = setsockopt(listenerTcpSocket, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST,
|
|
|
|
// reinterpret_cast<const char*>(&tcpSockOpt), sizeof(tcpSockOpt));
|
|
|
|
// if(retval != 0) {
|
|
|
|
// sif::warning << "TcWinTcpServer::TcWinTcpServer: Setting socket options failed!" <<
|
|
|
|
// std::endl;
|
|
|
|
// handleError(ErrorSources::SETSOCKOPT_CALL);
|
|
|
|
// return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
// }
|
|
|
|
// tcpAddress.sin_family = AF_INET;
|
|
|
|
// tcpAddress.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
retval = bind(listenerTcpSocket, reinterpret_cast<const sockaddr*>(&tcpAddress),
|
2021-03-08 23:55:58 +01:00
|
|
|
tcpAddrLen);
|
2021-03-09 00:37:42 +01:00
|
|
|
if(retval == SOCKET_ERROR) {
|
2021-03-08 23:55:58 +01:00
|
|
|
sif::warning << "TcWinTcpServer::TcWinTcpServer: Binding socket failed!" <<
|
|
|
|
std::endl;
|
2021-03-09 00:37:42 +01:00
|
|
|
freeaddrinfo(addrResult);
|
2021-03-08 23:55:58 +01:00
|
|
|
handleError(ErrorSources::BIND_CALL);
|
2021-03-08 23:14:10 +01:00
|
|
|
}
|
2021-03-08 23:55:58 +01:00
|
|
|
|
2021-03-09 00:37:42 +01:00
|
|
|
freeaddrinfo(addrResult);
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2021-03-08 23:00:53 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 00:37:42 +01:00
|
|
|
|
2021-03-08 23:00:53 +01:00
|
|
|
TcWinTcpServer::~TcWinTcpServer() {
|
2021-03-09 00:37:42 +01:00
|
|
|
closesocket(listenerTcpSocket);
|
2021-03-08 23:55:58 +01:00
|
|
|
WSACleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t TcWinTcpServer::performOperation(uint8_t opCode) {
|
|
|
|
/* If a connection is accepted, the corresponding scoket will be assigned to the new socket */
|
2021-03-09 00:37:42 +01:00
|
|
|
SOCKET clientSocket;
|
|
|
|
sockaddr_in clientSockAddr;
|
2021-03-08 23:55:58 +01:00
|
|
|
int connectorSockAddrLen = 0;
|
2021-03-09 00:37:42 +01:00
|
|
|
int retval = 0;
|
2021-03-08 23:55:58 +01:00
|
|
|
/* Listen for connection requests permanently for lifetime of program */
|
|
|
|
while(true) {
|
2021-03-09 00:37:42 +01:00
|
|
|
retval = listen(listenerTcpSocket, currentBacklog);
|
|
|
|
if(retval == SOCKET_ERROR) {
|
2021-03-08 23:55:58 +01:00
|
|
|
handleError(ErrorSources::LISTEN_CALL);
|
2021-03-09 00:37:42 +01:00
|
|
|
continue;
|
2021-03-08 23:55:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 00:37:42 +01:00
|
|
|
clientSocket = accept(listenerTcpSocket, reinterpret_cast<sockaddr*>(&clientSockAddr),
|
2021-03-08 23:55:58 +01:00
|
|
|
&connectorSockAddrLen);
|
|
|
|
|
2021-03-09 00:37:42 +01:00
|
|
|
if(clientSocket == INVALID_SOCKET) {
|
|
|
|
handleError(ErrorSources::ACCEPT_CALL);
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
retval = recv(clientSocket, reinterpret_cast<char*>(receptionBuffer.data()),
|
|
|
|
receptionBuffer.size(), 0);
|
|
|
|
#if FSFW_TCP_SERVER_WIRETAPPING_ENABLED == 1
|
|
|
|
#endif
|
2021-03-08 23:55:58 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2021-03-08 23:00:53 +01:00
|
|
|
}
|
|
|
|
|
2021-03-08 23:14:10 +01:00
|
|
|
void TcWinTcpServer::handleError(ErrorSources errorSrc) {
|
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2021-03-08 23:00:53 +01:00
|
|
|
int errCode = WSAGetLastError();
|
2021-03-08 23:14:10 +01:00
|
|
|
std::string errorSrcString;
|
|
|
|
if(errorSrc == ErrorSources::SETSOCKOPT_CALL) {
|
|
|
|
errorSrcString = "setsockopt call";
|
|
|
|
}
|
|
|
|
else if(errorSrc == ErrorSources::SOCKET_CALL) {
|
|
|
|
errorSrcString = "socket call";
|
|
|
|
}
|
2021-03-08 23:55:58 +01:00
|
|
|
else if(errorSrc == ErrorSources::LISTEN_CALL) {
|
|
|
|
errorSrcString = "listen call";
|
|
|
|
}
|
|
|
|
else if(errorSrc == ErrorSources::ACCEPT_CALL) {
|
|
|
|
errorSrcString = "accept call";
|
|
|
|
}
|
2021-03-09 00:37:42 +01:00
|
|
|
else if(errorSrc == ErrorSources::GETADDRINFO_CALL) {
|
|
|
|
errorSrcString = "getaddrinfo call";
|
|
|
|
}
|
2021-03-08 23:55:58 +01:00
|
|
|
|
2021-03-08 23:00:53 +01:00
|
|
|
switch(errCode) {
|
|
|
|
case(WSANOTINITIALISED): {
|
2021-03-08 23:14:10 +01:00
|
|
|
sif::warning << "TmTcWinUdpBridge::handleError: " << errorSrcString << " | "
|
|
|
|
"WSANOTINITIALISED: WSAStartup call necessary" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(WSAEINVAL): {
|
|
|
|
sif::warning << "TmTcWinUdpBridge::handleError: " << errorSrcString << " | "
|
|
|
|
"WSAEINVAL: Invalid parameters" << std::endl;
|
2021-03-08 23:00:53 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
/*
|
|
|
|
https://docs.microsoft.com/en-us/windows/win32/winsock/
|
|
|
|
windows-sockets-error-codes-2
|
|
|
|
*/
|
|
|
|
sif::warning << "TmTcWinUdpBridge::handleSocketError: Error code: " << errCode << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 23:14:10 +01:00
|
|
|
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
|
2021-03-08 23:00:53 +01:00
|
|
|
}
|