cleaner wiretapping handling

This commit is contained in:
Robin Müller 2021-03-08 23:00:53 +01:00
parent bdd66072d1
commit e67ff6a937
7 changed files with 127 additions and 48 deletions

View File

@ -1,11 +1,10 @@
target_sources(${LIB_FSFW_NAME}
PRIVATE
TcWinUdpPollingTask.cpp
TmTcWinUdpBridge.cpp
target_sources(${LIB_FSFW_NAME} PRIVATE
TcWinUdpPollingTask.cpp
TmTcWinUdpBridge.cpp
TcWinTcpServer.cpp
)
target_link_libraries(${LIB_FSFW_NAME}
PRIVATE
wsock32
ws2_32
target_link_libraries(${LIB_FSFW_NAME} PRIVATE
wsock32
ws2_32
)

View File

@ -0,0 +1,46 @@
#include "TcWinTcpServer.h"
#include "../../serviceinterface/ServiceInterface.h"
#include <winsock2.h>
TcWinTcpServer::TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge):
SystemObject(objectId) {
/* Open TCP socket */
serverTcpSocket = socket(AF_INET, SOCK_STREAM, 0);
if(serverTcpSocket == 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TcWinTcpServer::TcWinTcpServer: Socket creation failed!" << std::endl;
handleSocketError();
#endif
}
setsockopt(serverTcpSocket, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST,
&tcpSockOpt, sizeof(tcpSockOpt));
}
TcWinTcpServer::~TcWinTcpServer() {
}
void TcWinTcpServer::handleSocketError() {
int errCode = WSAGetLastError();
switch(errCode) {
case(WSANOTINITIALISED): {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::handleSocketError: WSANOTINITIALISED: WSAStartup"
" call necessary" << std::endl;
#endif
break;
}
default: {
/*
https://docs.microsoft.com/en-us/windows/win32/winsock/
windows-sockets-error-codes-2
*/
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::handleSocketError: Error code: " << errCode << std::endl;
#endif
break;
}
}
}

View File

@ -0,0 +1,30 @@
#ifndef FSFW_OSAL_WINDOWS_TCWINTCPSERVER_H_
#define FSFW_OSAL_WINDOWS_TCWINTCPSERVER_H_
#include "../../objectmanager/SystemObject.h"
#include "../../tasks/ExecutableObjectIF.h"
#include <vector>
class TcWinTcpServer:
public SystemObject,
public ExecutableObjectIF {
public:
/* The ports chosen here should not be used by any other process. */
static constexpr uint16_t DEFAULT_TCP_SERVER_PORT = 7301;
static constexpr uint16_t DEFAULT_TCP_CLIENT_PORT = 7302;
TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBridge);
virtual~ TcWinTcpServer();
private:
SOCKET serverTcpSocket = 0;
std::vector<uint8_t> receptionBuffer;
int tcpSockOpt = 0;
void handleSocketError();
};
#endif /* FSFW_OSAL_WINDOWS_TCWINTCPSERVER_H_ */

View File

@ -20,8 +20,8 @@ TcWinUdpPollingTask::TcWinUdpPollingTask(object_id_t objectId,
this->frameSize = DEFAULT_MAX_FRAME_SIZE;
}
// Set up reception buffer with specified frame size.
// For now, it is assumed that only one frame is held in the buffer!
/* Set up reception buffer with specified frame size.
For now, it is assumed that only one frame is held in the buffer! */
receptionBuffer.reserve(this->frameSize);
receptionBuffer.resize(this->frameSize);
@ -36,7 +36,7 @@ TcWinUdpPollingTask::TcWinUdpPollingTask(object_id_t objectId,
TcWinUdpPollingTask::~TcWinUdpPollingTask() {}
ReturnValue_t TcWinUdpPollingTask::performOperation(uint8_t opCode) {
// Poll for new UDP datagrams in permanent loop.
/* Poll for new UDP datagrams in permanent loop. */
while(true) {
//! Sender Address is cached here.
struct sockaddr_in senderAddress;
@ -46,7 +46,7 @@ ReturnValue_t TcWinUdpPollingTask::performOperation(uint8_t opCode) {
receptionFlags, reinterpret_cast<sockaddr*>(&senderAddress),
&senderAddressSize);
if(bytesReceived == SOCKET_ERROR) {
// handle error
/* Handle error */
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcWinUdpPollingTask::performOperation: Reception"
" error." << std::endl;
@ -54,9 +54,9 @@ ReturnValue_t TcWinUdpPollingTask::performOperation(uint8_t opCode) {
handleReadError();
continue;
}
#if FSFW_CPP_OSTREAM_ENABLED == 1
//sif::debug << "TcWinUdpPollingTask::performOperation: " << bytesReceived
// << " bytes received" << std::endl;
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_WIRETAPPING_ENABLED == 1
sif::debug << "TcWinUdpPollingTask::performOperation: " << bytesReceived <<
" bytes received" << std::endl;
#endif
ReturnValue_t result = handleSuccessfullTcRead(bytesReceived);
@ -74,12 +74,14 @@ ReturnValue_t TcWinUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) {
store_address_t storeId;
ReturnValue_t result = tcStore->addData(&storeId,
receptionBuffer.data(), bytesRead);
// arrayprinter::print(receptionBuffer.data(), bytesRead);
#if FSFW_UDP_WIRETAPPING_ENABLED == 1
arrayprinter::print(receptionBuffer.data(), bytesRead);#
#endif
if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TcSerialPollingTask::transferPusToSoftwareBus: Data "
"storage failed" << std::endl;
sif::error << "Packet size: " << bytesRead << std::endl;
sif::warning<< "TcSerialPollingTask::transferPusToSoftwareBus: Data "
"storage failed" << std::endl;
sif::warning << "Packet size: " << bytesRead << std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
@ -89,8 +91,7 @@ ReturnValue_t TcWinUdpPollingTask::handleSuccessfullTcRead(size_t bytesRead) {
result = MessageQueueSenderIF::sendMessage(targetTcDestination, &message);
if (result != HasReturnvaluesIF::RETURN_OK) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "Serial Polling: Sending message to queue failed"
<< std::endl;
sif::warning << "Serial Polling: Sending message to queue failed" << std::endl;
#endif
tcStore->deleteData(storeId);
}
@ -117,9 +118,9 @@ ReturnValue_t TcWinUdpPollingTask::initialize() {
}
serverUdpSocket = tmtcBridge->serverSocket;
#if FSFW_CPP_OSTREAM_ENABLED == 1
//sif::info << "TcWinUdpPollingTask::initialize: Server UDP socket "
// << serverUdpSocket << std::endl;
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_WIRETAPPING_ENABLED == 1
sif::info << "TcWinUdpPollingTask::initialize: Server UDP socket " << serverUdpSocket <<
std::endl;
#endif
return HasReturnvaluesIF::RETURN_OK;

View File

@ -8,6 +8,9 @@
#include <vector>
//! Debugging preprocessor define.
#define FSFW_UDP_RCV_WIRETAPPING_ENABLED 0
/**
* @brief This class can be used to implement the polling of a Unix socket,
* using UDP for now.
@ -51,8 +54,7 @@ private:
//! Reception flags: https://linux.die.net/man/2/recvfrom.
int receptionFlags = 0;
//! Server socket, which is member of TMTC bridge and is assigned in
//! constructor
//! Server socket, which is member of TMTC bridge and is assigned in constructor
SOCKET serverUdpSocket = 0;
std::vector<uint8_t> receptionBuffer;

View File

@ -85,13 +85,12 @@ TmTcWinUdpBridge::~TmTcWinUdpBridge() {
ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) {
int flags = 0;
//clientAddress.sin_addr.s_addr = htons(INADDR_ANY);
//clientAddressLen = sizeof(serverAddress);
// char ipAddress [15];
#if FSFW_CPP_OSTREAM_ENABLED == 1
// sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET,
// &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
clientAddress.sin_addr.s_addr = htons(INADDR_ANY);
clientAddressLen = sizeof(serverAddress);
char ipAddress [15];
sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET,
&clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
#endif
ssize_t bytesSent = sendto(serverSocket,
@ -104,9 +103,9 @@ ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) {
#endif
handleSendError();
}
#if FSFW_CPP_OSTREAM_ENABLED == 1
// sif::debug << "TmTcUnixUdpBridge::sendTm: " << bytesSent << " bytes were"
// " sent." << std::endl;
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
sif::debug << "TmTcUnixUdpBridge::sendTm: " << bytesSent << " bytes were"
" sent." << std::endl;
#endif
return HasReturnvaluesIF::RETURN_OK;
}
@ -114,16 +113,16 @@ ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) {
void TmTcWinUdpBridge::checkAndSetClientAddress(sockaddr_in newAddress) {
MutexHelper lock(mutex, MutexIF::TimeoutType::WAITING, 10);
// char ipAddress [15];
#if FSFW_CPP_OSTREAM_ENABLED == 1
// sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET,
// &newAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
// sif::debug << "IP Address Old: " << inet_ntop(AF_INET,
// &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
#if FSFW_CPP_OSTREAM_ENABLED == 1 && FSFW_UDP_SEND_WIRETAPPING_ENABLED == 1
char ipAddress [15];
sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET,
&newAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
sif::debug << "IP Address Old: " << inet_ntop(AF_INET,
&clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
#endif
registerCommConnect();
// Set new IP address if it has changed.
/* Set new IP address if it has changed. */
if(clientAddress.sin_addr.s_addr != newAddress.sin_addr.s_addr) {
clientAddress.sin_addr.s_addr = newAddress.sin_addr.s_addr;
clientAddressLen = sizeof(clientAddress);
@ -135,8 +134,8 @@ void TmTcWinUdpBridge::handleSocketError() {
switch(errCode) {
case(WSANOTINITIALISED): {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "TmTcWinUdpBridge::handleSocketError: WSANOTINITIALISED: "
<< "WSAStartup(...) call necessary" << std::endl;
sif::warning << "TmTcWinUdpBridge::handleSocketError: WSANOTINITIALISED: WSAStartup"
" call necessary" << std::endl;
#endif
break;
}
@ -146,8 +145,7 @@ void TmTcWinUdpBridge::handleSocketError() {
windows-sockets-error-codes-2
*/
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "TmTcWinUdpBridge::handleSocketError: Error code: "
<< errCode << std::endl;
sif::warning << "TmTcWinUdpBridge::handleSocketError: Error code: " << errCode << std::endl;
#endif
break;
}

View File

@ -6,10 +6,13 @@
#include <winsock2.h>
#include <windows.h>
//! Debugging preprocessor define.
#define FSFW_UDP_SEND_WIRETAPPING_ENABLED 0
class TmTcWinUdpBridge: public TmTcBridge {
friend class TcWinUdpPollingTask;
public:
// The ports chosen here should not be used by any other process.
/* The ports chosen here should not be used by any other process. */
static constexpr uint16_t DEFAULT_UDP_SERVER_PORT = 7301;
static constexpr uint16_t DEFAULT_UDP_CLIENT_PORT = 7302;