2020-07-04 00:51:49 +02:00
|
|
|
#include <framework/osal/linux/TmTcUnixUdpBridge.h>
|
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
2020-07-05 23:53:13 +02:00
|
|
|
#include <framework/ipc/MutexHelper.h>
|
2020-07-04 00:51:49 +02:00
|
|
|
|
2020-07-08 03:18:09 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2020-07-04 00:51:49 +02:00
|
|
|
TmTcUnixUdpBridge::TmTcUnixUdpBridge(object_id_t objectId,
|
2020-07-04 23:52:44 +02:00
|
|
|
object_id_t ccsdsPacketDistributor, uint16_t serverPort,
|
|
|
|
uint16_t clientPort):
|
2020-07-04 00:51:49 +02:00
|
|
|
TmTcBridge(objectId, ccsdsPacketDistributor) {
|
2020-07-05 23:53:13 +02:00
|
|
|
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 03:18:09 +02:00
|
|
|
// uint16_t setClientPort = DEFAULT_UDP_CLIENT_PORT;
|
|
|
|
// if(clientPort != 0xFFFF) {
|
|
|
|
// setClientPort = clientPort;
|
|
|
|
// }
|
2020-07-04 23:52:44 +02:00
|
|
|
|
2020-07-05 23:53:13 +02:00
|
|
|
// Set up UDP socket: https://man7.org/linux/man-pages/man7/ip.7.html
|
2020-07-04 00:51:49 +02:00
|
|
|
serverSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
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-05 23:53:13 +02:00
|
|
|
// Accept packets from any interface.
|
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-04 00:51:49 +02:00
|
|
|
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &serverSocketOptions,
|
|
|
|
sizeof(serverSocketOptions));
|
|
|
|
|
2020-07-05 23:53:13 +02:00
|
|
|
serverSocketLen = sizeof(serverAddress);
|
2020-07-04 00:51:49 +02:00
|
|
|
int result = bind(serverSocket,
|
|
|
|
reinterpret_cast<struct sockaddr*>(&serverAddress),
|
2020-07-05 23:53:13 +02:00
|
|
|
serverSocketLen);
|
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;
|
|
|
|
ssize_t result = send(serverSocket, data, dataLen, flags);
|
|
|
|
if(result < 0) {
|
2020-07-08 03:18:09 +02:00
|
|
|
// todo: handle errors
|
2020-07-05 18:58:16 +02:00
|
|
|
sif::error << "TmTcUnixUdpBridge::sendTm: Send operation failed "
|
|
|
|
"with error " << strerror(errno) << std::endl;
|
|
|
|
}
|
2020-07-04 23:52:44 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
2020-07-05 15:31:02 +02:00
|
|
|
|
2020-07-08 03:18:09 +02:00
|
|
|
void TmTcUnixUdpBridge::checkAndSetClientAddress(sockaddr_in newAddress) {
|
|
|
|
MutexHelper lock(mutex, 10);
|
|
|
|
// 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-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):
|
|
|
|
sif::error << "TmTcUnixBridge::TmTcUnixBridge: Socket creation failed"
|
|
|
|
<< " with " << strerror(errno) << std::endl;
|
2020-07-05 18:22:39 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sif::error << "TmTcUnixBridge::TmTcUnixBridge: Unknown error"
|
|
|
|
<< std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TmTcUnixUdpBridge::handleBindError() {
|
|
|
|
// See: https://man7.org/linux/man-pages/man2/bind.2.html
|
|
|
|
switch(errno) {
|
2020-07-08 02:20:38 +02:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
sif::error << "TmTcUnixBridge::TmTcUnixBridge: Port access issue."
|
|
|
|
"Ports 1-1024 are reserved on UNIX systems and require root "
|
|
|
|
"rights while ephermeral ports should not be used as well."
|
|
|
|
<< std::endl;
|
2020-07-08 02:20:38 +02:00
|
|
|
}
|
|
|
|
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):
|
2020-07-08 02:20:38 +02:00
|
|
|
case(EROFS): {
|
2020-07-05 18:22:39 +02:00
|
|
|
sif::error << "TmTcUnixBridge::TmTcUnixBridge: Socket creation failed"
|
2020-07-08 02:20:38 +02:00
|
|
|
<< " with " << strerror(errno) << std::endl;
|
2020-07-05 18:22:39 +02:00
|
|
|
break;
|
2020-07-08 02:20:38 +02:00
|
|
|
}
|
2020-07-08 03:18:09 +02:00
|
|
|
default:
|
2020-07-05 18:22:39 +02:00
|
|
|
sif::error << "TmTcUnixBridge::TmTcUnixBridge: Unknown error"
|
2020-07-08 02:20:38 +02:00
|
|
|
<< 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
|
|
|
|