trying new udp stuff

This commit is contained in:
Robin Müller 2021-03-12 00:34:30 +01:00
parent 67b05fee2e
commit 6e5b032dbb
5 changed files with 85 additions and 51 deletions

View File

@ -24,5 +24,7 @@ MutexIF* MutexFactory::createMutex() {
} }
void MutexFactory::deleteMutex(MutexIF* mutex) { void MutexFactory::deleteMutex(MutexIF* mutex) {
delete mutex; if(mutex != nullptr) {
delete mutex;
}
} }

View File

@ -71,8 +71,7 @@ ReturnValue_t TcWinTcpServer::initialize() {
// tcpAddress.sin_family = AF_INET; // tcpAddress.sin_family = AF_INET;
// tcpAddress.sin_addr.s_addr = htonl(INADDR_ANY); // tcpAddress.sin_addr.s_addr = htonl(INADDR_ANY);
retval = bind(listenerTcpSocket, reinterpret_cast<const sockaddr*>(&tcpAddress), retval = bind(listenerTcpSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen));
tcpAddrLen);
if(retval == SOCKET_ERROR) { if(retval == SOCKET_ERROR) {
sif::warning << "TcWinTcpServer::TcWinTcpServer: Binding socket failed!" << sif::warning << "TcWinTcpServer::TcWinTcpServer: Binding socket failed!" <<
std::endl; std::endl;

View File

@ -33,7 +33,7 @@ TcWinUdpPollingTask::~TcWinUdpPollingTask() {}
ReturnValue_t TcWinUdpPollingTask::performOperation(uint8_t opCode) { 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) { while(true) {
//! Sender Address is cached here. /* Sender Address is cached here. */
struct sockaddr_in senderAddress; struct sockaddr_in senderAddress;
int senderAddressSize = sizeof(senderAddress); int senderAddressSize = sizeof(senderAddress);
int bytesReceived = recvfrom(serverUdpSocket, int bytesReceived = recvfrom(serverUdpSocket,

View File

@ -1,19 +1,34 @@
#include "TmTcWinUdpBridge.h" #include "TmTcWinUdpBridge.h"
#include <fsfw/serviceinterface/ServiceInterface.h>
#include <fsfw/ipc/MutexGuard.h> #include <fsfw/ipc/MutexGuard.h>
#include <ws2tcpip.h>
#if defined(_MSC_VER) const std::string TmTcWinUdpBridge::DEFAULT_UDP_SERVER_PORT = "7301";
#include <BaseTsd.h> const std::string TmTcWinUdpBridge::DEFAULT_UDP_CLIENT_PORT = "7302";
typedef SSIZE_T ssize_t;
#endif
TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId, TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId,
object_id_t tcDestination, object_id_t tmStoreId, object_id_t tcStoreId, object_id_t tcDestination, object_id_t tmStoreId, object_id_t tcStoreId,
uint16_t serverPort, uint16_t clientPort): std::string udpServerPort, std::string udpClientPort):
TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) { TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) {
if(udpServerPort == "") {
udpServerPort = DEFAULT_UDP_SERVER_PORT;
}
else {
this->udpServerPort = udpServerPort;
}
if(udpClientPort == "") {
udpClientPort = DEFAULT_UDP_CLIENT_PORT;
}
else {
this->udpClientPort = udpClientPort;
}
mutex = MutexFactory::instance()->createMutex(); mutex = MutexFactory::instance()->createMutex();
communicationLinkUp = false; communicationLinkUp = false;
}
ReturnValue_t TmTcWinUdpBridge::initialize() {
/* Initiates Winsock DLL. */ /* Initiates Winsock DLL. */
WSAData wsaData; WSAData wsaData;
WORD wVersionRequested = MAKEWORD(2, 2); WORD wVersionRequested = MAKEWORD(2, 2);
@ -22,71 +37,85 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId,
/* Tell the user that we could not find a usable */ /* Tell the user that we could not find a usable */
/* Winsock DLL. */ /* Winsock DLL. */
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge:" sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: WSAStartup failed with error: " <<
"WSAStartup failed with error: " << err << std::endl; err << std::endl;
#else
sif::printError("TmTcWinUdpBridge::TmTcWinUdpBridge: WSAStartup failed with error: %d\n",
err);
#endif #endif
return; return HasReturnvaluesIF::RETURN_FAILED;
} }
uint16_t setServerPort = DEFAULT_UDP_SERVER_PORT; struct addrinfo *addrResult = nullptr;
if(serverPort != 0xFFFF) { struct addrinfo hints;
setServerPort = serverPort;
}
uint16_t setClientPort = DEFAULT_UDP_CLIENT_PORT; ZeroMemory(&hints, sizeof (hints));
if(clientPort != 0xFFFF) { hints.ai_family = AF_INET;
setClientPort = clientPort; hints.ai_socktype = SOCK_DGRAM;
} hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_PASSIVE;
/* Set up UDP socket: /* Set up UDP socket:
https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket https://en.wikipedia.org/wiki/Getaddrinfo
Passing nullptr as the first parameter and specifying AI_PASSIVE in hints will cause
getaddrinfo to assign the address 0.0.0.0 (any address)
*/ */
serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); int retval = getaddrinfo(nullptr, udpServerPort.c_str(), &hints, &addrResult);
if (retval != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Retrieving address info failed!" <<
std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
serverSocket = socket(addrResult->ai_family, addrResult->ai_socktype, addrResult->ai_protocol);
if(serverSocket == INVALID_SOCKET) { if(serverSocket == INVALID_SOCKET) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open UDP socket!" << sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open UDP socket!" <<
std::endl; std::endl;
#endif #endif
handleSocketError(); handleSocketError();
return; return HasReturnvaluesIF::RETURN_FAILED;
}
serverAddress.sin_family = AF_INET;
/* Accept packets from any interface. (potentially insecure). */
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(setServerPort);
serverAddressLen = sizeof(serverAddress);
int result = setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<const char*>(&serverSocketOptions),
sizeof(serverSocketOptions));
if(result != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not set socket options!" <<
std::endl;
#endif
handleSocketError();
} }
// serverAddress.sin_family = AF_INET;
//
// /* Accept packets from any interface. (potentially insecure). */
// serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
// serverAddress.sin_port = htons(setServerPort);
// serverAddressLen = sizeof(serverAddress);
// int result = setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR,
// reinterpret_cast<const char*>(&serverSocketOptions),
// sizeof(serverSocketOptions));
// if(result != 0) {
//#if FSFW_CPP_OSTREAM_ENABLED == 1
// sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not set socket options!" <<
// std::endl;
//#endif
// handleSocketError();
// }
//
clientAddress.sin_family = AF_INET; clientAddress.sin_family = AF_INET;
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY); clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
clientAddress.sin_port = htons(setClientPort); clientAddress.sin_port = htons(7302);
clientAddressLen = sizeof(clientAddress); clientAddressLen = sizeof(clientAddress);
result = bind(serverSocket, retval = bind(serverSocket, addrResult->ai_addr, static_cast<int>(addrResult->ai_addrlen));
reinterpret_cast<struct sockaddr*>(&serverAddress), if(retval != 0) {
serverAddressLen);
if(result != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind " sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind "
"local port " << setServerPort << " to server socket!" "local port " << udpServerPort << " to server socket!" << std::endl;
<< std::endl;
#endif #endif
handleBindError(); handleBindError();
} }
return HasReturnvaluesIF::RETURN_OK;
} }
TmTcWinUdpBridge::~TmTcWinUdpBridge() { TmTcWinUdpBridge::~TmTcWinUdpBridge() {
if(mutex != nullptr) {
MutexFactory::instance()->deleteMutex(mutex);
}
closesocket(serverSocket); closesocket(serverSocket);
WSACleanup(); WSACleanup();
} }
@ -102,7 +131,7 @@ ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) {
&clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl; &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
#endif #endif
ssize_t bytesSent = sendto(serverSocket, int bytesSent = sendto(serverSocket,
reinterpret_cast<const char*>(data), dataLen, flags, reinterpret_cast<const char*>(data), dataLen, flags,
reinterpret_cast<sockaddr*>(&clientAddress), clientAddressLen); reinterpret_cast<sockaddr*>(&clientAddress), clientAddressLen);
if(bytesSent == SOCKET_ERROR) { if(bytesSent == SOCKET_ERROR) {

View File

@ -13,14 +13,16 @@ class TmTcWinUdpBridge: public TmTcBridge {
friend class TcWinUdpPollingTask; friend class TcWinUdpPollingTask;
public: 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 const std::string DEFAULT_UDP_SERVER_PORT;
static constexpr uint16_t DEFAULT_UDP_CLIENT_PORT = 7302; static const std::string DEFAULT_UDP_CLIENT_PORT;
TmTcWinUdpBridge(object_id_t objectId, object_id_t tcDestination, TmTcWinUdpBridge(object_id_t objectId, object_id_t tcDestination,
object_id_t tmStoreId, object_id_t tcStoreId, object_id_t tmStoreId, object_id_t tcStoreId,
uint16_t serverPort = 0xFFFF,uint16_t clientPort = 0xFFFF); std::string udpServerPort = "", std::string udpClientPort = "");
virtual~ TmTcWinUdpBridge(); virtual~ TmTcWinUdpBridge();
ReturnValue_t initialize() override;
void checkAndSetClientAddress(sockaddr_in clientAddress); void checkAndSetClientAddress(sockaddr_in clientAddress);
protected: protected:
@ -28,6 +30,8 @@ protected:
private: private:
SOCKET serverSocket = 0; SOCKET serverSocket = 0;
std::string udpServerPort;
std::string udpClientPort;
const int serverSocketOptions = 0; const int serverSocketOptions = 0;