fsfw/osal/linux/TmTcUnixUdpBridge.cpp

169 lines
4.8 KiB
C++
Raw Normal View History

2020-07-04 00:51:49 +02:00
#include <framework/osal/linux/TmTcUnixUdpBridge.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <framework/ipc/MutexHelper.h>
2020-07-04 00:51:49 +02:00
#include <errno.h>
2020-07-08 15:06:46 +02:00
#include <arpa/inet.h>
2020-07-04 00:51:49 +02:00
TmTcUnixUdpBridge::TmTcUnixUdpBridge(object_id_t objectId,
2020-07-08 15:06:46 +02:00
object_id_t tcDestination, object_id_t tmStoreId, object_id_t tcStoreId,
uint16_t serverPort, uint16_t clientPort):
TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) {
mutex = MutexFactory::instance()->createMutex();
2020-07-04 23:52:44 +02:00
uint16_t setServerPort = DEFAULT_UDP_SERVER_PORT;
if(serverPort != 0xFFFF) {
setServerPort = serverPort;
}
2020-07-08 15:06:46 +02:00
uint16_t setClientPort = DEFAULT_UDP_CLIENT_PORT;
if(clientPort != 0xFFFF) {
setClientPort = clientPort;
}
2020-07-04 23:52:44 +02:00
// Set up UDP socket: https://man7.org/linux/man-pages/man7/ip.7.html
2020-07-08 15:06:46 +02:00
//clientSocket = socket(AF_INET, SOCK_DGRAM, 0);
serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2020-07-04 00:51:49 +02:00
if(socket < 0) {
sif::error << "TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not open"
" UDP socket!" << std::endl;
2020-07-05 15:31:02 +02:00
handleSocketError();
2020-07-04 00:51:49 +02:00
return;
}
serverAddress.sin_family = AF_INET;
2020-07-08 18:18:54 +02:00
// Accept packets from any interface.
2020-07-08 18:18:54 +02:00
//serverAddress.sin_addr.s_addr = inet_addr("127.73.73.0");
2020-07-04 00:51:49 +02:00
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
2020-07-04 23:52:44 +02:00
serverAddress.sin_port = htons(setServerPort);
2020-07-08 18:18:54 +02:00
serverAddressLen = sizeof(serverAddress);
2020-07-04 00:51:49 +02:00
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &serverSocketOptions,
sizeof(serverSocketOptions));
2020-07-08 15:06:46 +02:00
clientAddress.sin_family = AF_INET;
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
clientAddress.sin_port = htons(setClientPort);
2020-07-08 18:18:54 +02:00
clientAddressLen = sizeof(clientAddress);
2020-07-08 15:06:46 +02:00
2020-07-04 00:51:49 +02:00
int result = bind(serverSocket,
reinterpret_cast<struct sockaddr*>(&serverAddress),
2020-07-08 15:06:46 +02:00
serverAddressLen);
2020-07-05 00:06:30 +02:00
if(result == -1) {
2020-07-04 00:51:49 +02:00
sif::error << "TmTcUnixUdpBridge::TmTcUnixUdpBridge: Could not bind "
2020-07-04 23:52:44 +02:00
"local port " << setServerPort << " to server socket!"
2020-07-04 00:51:49 +02:00
<< std::endl;
2020-07-05 18:22:39 +02:00
handleBindError();
2020-07-04 00:51:49 +02:00
return;
}
}
TmTcUnixUdpBridge::~TmTcUnixUdpBridge() {
}
2020-07-04 23:52:44 +02:00
ReturnValue_t TmTcUnixUdpBridge::sendTm(const uint8_t *data, size_t dataLen) {
2020-07-05 18:58:16 +02:00
int flags = 0;
2020-07-08 18:18:54 +02:00
clientAddress.sin_addr.s_addr = htons(INADDR_ANY);
//clientAddress.sin_addr.s_addr = inet_addr("127.73.73.1");
clientAddressLen = sizeof(serverAddress);
// char ipAddress [15];
// sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET,
// &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
2020-07-08 15:06:46 +02:00
ssize_t bytesSent = sendto(serverSocket, data, dataLen, flags,
reinterpret_cast<sockaddr*>(&clientAddress), clientAddressLen);
if(bytesSent < 0) {
sif::error << "TmTcUnixUdpBridge::sendTm: Send operation failed."
<< std::endl;
handleSendError();
2020-07-05 18:58:16 +02:00
}
2020-07-08 18:18:54 +02:00
// sif::debug << "TmTcUnixUdpBridge::sendTm: " << bytesSent << " bytes were"
// " sent." << std::endl;
2020-07-04 23:52:44 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2020-07-05 15:31:02 +02:00
void TmTcUnixUdpBridge::checkAndSetClientAddress(sockaddr_in newAddress) {
MutexHelper lock(mutex, 10);
2020-07-08 15:06:46 +02:00
2020-07-08 18:18:54 +02:00
// 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;
2020-07-08 15:06:46 +02:00
// 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;
2020-07-08 15:06:46 +02:00
clientAddressLen = sizeof(clientAddress);
}
}
2020-07-05 15:31:02 +02:00
void TmTcUnixUdpBridge::handleSocketError() {
2020-07-05 18:22:39 +02:00
2020-07-05 15:31:02 +02:00
// See: https://man7.org/linux/man-pages/man2/socket.2.html
switch(errno) {
case(EACCES):
case(EINVAL):
case(EMFILE):
case(ENFILE):
case(EAFNOSUPPORT):
case(ENOBUFS):
case(ENOMEM):
case(EPROTONOSUPPORT):
2020-07-08 18:18:54 +02:00
sif::error << "TmTcUnixBridge::handleSocketError: Socket creation failed"
2020-07-05 15:31:02 +02:00
<< " with " << strerror(errno) << std::endl;
2020-07-05 18:22:39 +02:00
break;
default:
2020-07-08 18:18:54 +02:00
sif::error << "TmTcUnixBridge::handleSocketError: Unknown error"
2020-07-05 18:22:39 +02:00
<< std::endl;
break;
}
}
void TmTcUnixUdpBridge::handleBindError() {
// See: https://man7.org/linux/man-pages/man2/bind.2.html
switch(errno) {
case(EACCES): {
2020-07-05 18:22:39 +02:00
/*
Ephermeral ports can be shown with following command:
sysctl -A | grep ip_local_port_range
*/
2020-07-08 18:18:54 +02:00
sif::error << "TmTcUnixBridge::handleBindError: Port access issue."
2020-07-05 18:22:39 +02:00
"Ports 1-1024 are reserved on UNIX systems and require root "
"rights while ephermeral ports should not be used as well."
<< std::endl;
}
break;
2020-07-05 18:22:39 +02:00
case(EADDRINUSE):
case(EBADF):
case(EINVAL):
case(ENOTSOCK):
case(EADDRNOTAVAIL):
case(EFAULT):
case(ELOOP):
case(ENAMETOOLONG):
case(ENOENT):
case(ENOMEM):
case(ENOTDIR):
case(EROFS): {
2020-07-08 18:18:54 +02:00
sif::error << "TmTcUnixBridge::handleBindError: Socket creation failed"
<< " with " << strerror(errno) << std::endl;
2020-07-05 18:22:39 +02:00
break;
}
default:
2020-07-08 18:18:54 +02:00
sif::error << "TmTcUnixBridge::handleBindError: Unknown error"
<< std::endl;
2020-07-05 18:22:39 +02:00
break;
2020-07-05 15:31:02 +02:00
}
2020-07-05 18:58:16 +02:00
}
2020-07-05 18:22:39 +02:00
2020-07-08 15:06:46 +02:00
void TmTcUnixUdpBridge::handleSendError() {
2020-07-08 18:18:54 +02:00
switch(errno) {
default:
sif::error << "Error: " << strerror(errno) << std::endl;
}
2020-07-08 15:06:46 +02:00
}