This commit is contained in:
2021-08-18 19:05:05 +02:00
parent d9f969ae6f
commit f8d8fcf48a
13 changed files with 405 additions and 330 deletions

View File

@ -1,35 +1,15 @@
#pragma once
/**
* Get UDP socket desicriptor
* @param udp port index
*/
int getUdPSocketDescriptor(int index);
/**
* Set udp destination
* @param index udp port index
* @param ip udp destination ip
* @param port udp destination port
*/
int setUDPDestinationDetails(int index, const char *ip,
void setupUDPCommParameters();
int getUdPSocketDescriptor(int iRxEntry, int index);
void setNumberOfUDPDestinations(int value);
int setUDPDestinationDetails(int iRxEntry, int index, const char *ip,
unsigned short int port);
/**
* Create udp socket
* @param index udp port index
*/
int createUDPSocket(int index);
/**
* Writes to socket file descriptor
* @param index udp port index
* @param buf pointer to memory to write
* @param length length of buffer to write to socket
*/
int sendUDPPacket(int index, const char *buf, int length);
int sendUDPPacket(int iRxEntry, int index, const char *buf, int length);
/**
* Close udp socket
* @index udp port index
*/
void closeUDPSocket(int index);

View File

@ -13,25 +13,35 @@
#include <sys/types.h>
#include <unistd.h>
int udpSockfd[2] = {-1, -1};
struct addrinfo *udpServerAddrInfo[2] = {0, 0};
unsigned short int udpDestinationPort[2] = {0, 0};
char udpDestinationIp[2][INET_ADDRSTRLEN] = {"", ""};
int udpSockfd[MAX_UDP_DESTINATION][2] = {};
struct addrinfo *udpServerAddrInfo[MAX_UDP_DESTINATION][2] = {};
unsigned short int udpDestinationPort[MAX_UDP_DESTINATION][2] = {};
char udpDestinationIp[MAX_UDP_DESTINATION][2][INET_ADDRSTRLEN] = {};
extern int numUdpDestinations;
// DEFAULT_TX_UDP_PORT;// src port
int getUdPSocketDescriptor(int index) { return udpSockfd[index]; }
void setupUDPCommParameters() {
for (int i = 0; i != MAX_UDP_DESTINATION; i++) {
udpSockfd[i][0] = -1;
udpSockfd[i][1] = -1;
}
memset(udpServerAddrInfo, 0, sizeof(udpServerAddrInfo));
memset(udpDestinationIp, 0, sizeof(udpDestinationIp));
}
int setUDPDestinationDetails(int index, const char *ip,
int getUdPSocketDescriptor(int iRxEntry, int index) { return udpSockfd[iRxEntry][index]; }
int setUDPDestinationDetails(int iRxEntry, int index, const char *ip,
unsigned short int port) {
udpDestinationPort[index] = port;
udpDestinationPort[iRxEntry][index] = port;
size_t len = strlen(ip);
memset(udpDestinationIp[index], 0, INET_ADDRSTRLEN);
strncpy(udpDestinationIp[index], ip,
memset(udpDestinationIp[iRxEntry][index], 0, INET_ADDRSTRLEN);
strncpy(udpDestinationIp[iRxEntry][index], ip,
len > INET_ADDRSTRLEN ? INET_ADDRSTRLEN : len);
if (udpServerAddrInfo[index]) {
freeaddrinfo(udpServerAddrInfo[index]);
udpServerAddrInfo[index] = 0;
if (udpServerAddrInfo[iRxEntry][index]) {
freeaddrinfo(udpServerAddrInfo[iRxEntry][index]);
udpServerAddrInfo[iRxEntry][index] = 0;
}
// convert ip to internet address
@ -43,21 +53,21 @@ int setUDPDestinationDetails(int index, const char *ip,
hints.ai_protocol = 0;
char sport[100];
memset(sport, 0, 100);
sprintf(sport, "%d", udpDestinationPort[index]);
int err = getaddrinfo(udpDestinationIp[index], sport, &hints,
&udpServerAddrInfo[index]);
sprintf(sport, "%d", udpDestinationPort[iRxEntry][index]);
int err = getaddrinfo(udpDestinationIp[iRxEntry][index], sport, &hints,
&udpServerAddrInfo[iRxEntry][index]);
if (err != 0) {
LOG(logERROR, ("Failed to resolve remote socket address %s at port %d. "
LOG(logERROR, ("Failed to resolve remote socket address %s at port %d [entry:%d]. "
"(Error code:%d, %s)\n",
udpDestinationIp[index], udpDestinationPort[index], err,
udpDestinationIp[iRxEntry][index], udpDestinationPort[iRxEntry][index], iRxEntry, err,
gai_strerror(err)));
return FAIL;
}
if (udpServerAddrInfo[index] == NULL) {
LOG(logERROR, ("Failed to resolve remote socket address %s at port %d "
if (udpServerAddrInfo[iRxEntry][index] == NULL) {
LOG(logERROR, ("Failed to resolve remote socket address %s at port %d [entry:%d]."
"(getaddrinfo returned NULL)\n",
udpDestinationIp[index], udpDestinationPort[index]));
udpServerAddrInfo[index] = 0;
udpDestinationIp[iRxEntry][index], udpDestinationPort[iRxEntry][index], iRxEntry));
udpServerAddrInfo[iRxEntry][index] = 0;
return FAIL;
}
@ -65,49 +75,54 @@ int setUDPDestinationDetails(int index, const char *ip,
}
int createUDPSocket(int index) {
LOG(logDEBUG2, ("Creating UDP Socket %d\n", index));
if (!strlen(udpDestinationIp[index])) {
LOG(logERROR, ("No destination UDP ip specified.\n"));
return FAIL;
}
for (int iRxEntry = 0; iRxEntry != numUdpDestinations; ++iRxEntry) {
if (udpSockfd[index] != -1) {
LOG(logERROR, ("Strange that Udp socket was still open. Closing it to "
"create a new one\n"));
close(udpSockfd[index]);
udpSockfd[index] = -1;
}
LOG(logDEBUG2, ("Creating UDP Socket %d [entry:%d]\n", index, iRxEntry));
if (!strlen(udpDestinationIp[iRxEntry][index])) {
LOG(logERROR, ("No destination UDP ip specified for socket %d [entry:%d].\n", index, iRxEntry));
return FAIL;
}
// Creating socket file descriptor
udpSockfd[index] = socket(udpServerAddrInfo[index]->ai_family,
udpServerAddrInfo[index]->ai_socktype,
udpServerAddrInfo[index]->ai_protocol);
if (udpSockfd[index] == -1) {
LOG(logERROR, ("UDP socket at port %d failed. (Error code:%d, %s)\n",
udpDestinationPort[index], errno, gai_strerror(errno)));
return FAIL;
}
LOG(logINFO, ("Udp client socket created for server (port %d, ip:%s)\n",
udpDestinationPort[index], udpDestinationIp[index]));
if (udpSockfd[iRxEntry][index] != -1) {
LOG(logERROR, ("Strange that Udp socket was still open [socket:%d, entry:%d]. Closing it to "
"create a new one.\n", index, iRxEntry));
close(udpSockfd[iRxEntry][index]);
udpSockfd[iRxEntry][index] = -1;
}
// Using connect expects that the receiver (udp server) exists to listen to
// these packets connecting allows to use "send/write" instead of "sendto",
// avoiding checking for server address for each packet using write without
// a connect will end in segv
LOG(logINFO, ("Udp client socket connected\n", udpDestinationPort[index],
udpDestinationIp[index]));
// Creating socket file descriptor
udpSockfd[iRxEntry][index] = socket(udpServerAddrInfo[iRxEntry][index]->ai_family,
udpServerAddrInfo[iRxEntry][index]->ai_socktype,
udpServerAddrInfo[iRxEntry][index]->ai_protocol);
if (udpSockfd[iRxEntry][index] == -1) {
LOG(logERROR, ("UDP socket at port %d failed [entry:%d]. (Error code:%d, %s)\n",
udpDestinationPort[iRxEntry][index], iRxEntry, errno, gai_strerror(errno)));
return FAIL;
}
LOG(logINFO, ("Udp client socket created for server (entry:%d, port %d, ip:%s)\n",
iRxEntry, udpDestinationPort[iRxEntry][index], udpDestinationIp[iRxEntry][index]));
// Using connect expects that the receiver (udp server) exists to listen to
// these packets connecting allows to use "send/write" instead of "sendto",
// avoiding checking for server address for each packet using write without
// a connect will end in segv
LOG(logINFO, ("Udp client socket connected [%d, %d, %s]\n", iRxEntry, udpDestinationPort[iRxEntry][index],
udpDestinationIp[iRxEntry][index]));
}
return OK;
}
int sendUDPPacket(int index, const char *buf, int length) {
int n = sendto(udpSockfd[index], buf, length, 0,
udpServerAddrInfo[index]->ai_addr,
udpServerAddrInfo[index]->ai_addrlen);
int sendUDPPacket(int iRxEntry, int index, const char *buf, int length) {
int n = sendto(udpSockfd[iRxEntry][index], buf, length, 0,
udpServerAddrInfo[iRxEntry][index]->ai_addr,
udpServerAddrInfo[iRxEntry][index]->ai_addrlen);
// udp sends atomically, no need to handle partial data
if (n == -1) {
LOG(logERROR,
("Could not send udp packet for socket %d. (Error code:%d, %s)\n",
index, n, errno, gai_strerror(errno)));
("Could not send udp packet for socket %d [entry:%d]. (Error code:%d, %s)\n",
index, iRxEntry, errno, gai_strerror(errno)));
} else {
LOG(logDEBUG2, ("%d bytes sent\n", n));
}
@ -115,9 +130,11 @@ int sendUDPPacket(int index, const char *buf, int length) {
}
void closeUDPSocket(int index) {
if (udpSockfd[index] != -1) {
LOG(logINFO, ("Udp client socket closed\n"));
close(udpSockfd[index]);
udpSockfd[index] = -1;
for (int iRxEntry = 0; iRxEntry != numUdpDestinations; ++iRxEntry) {
if (udpSockfd[iRxEntry][index] != -1) {
LOG(logINFO, ("Udp client socket closed\n"));
close(udpSockfd[iRxEntry][index]);
udpSockfd[iRxEntry][index] = -1;
}
}
}

View File

@ -50,9 +50,9 @@ int debugflag = 0;
int updateFlag = 0;
int checkModuleFlag = 1;
const int MAX_UDP_DESTINATION = 32;
udpStruct udpDetails[MAX_UDP_DESTINATION] = {32410, 32411, 50001, 50002, 0, 0,
0, 0, 0, 0, 0, 0};
udpStruct udpDetails[MAX_UDP_DESTINATION];
int numUdpDestinations = 1;
int configured = FAIL;
char configureMessage[MAX_STR_LENGTH] = "udp parameters not configured yet";
int maxydet = -1;
@ -82,6 +82,11 @@ void init_detector() {
#ifdef VIRTUAL
LOG(logINFO, ("This is a VIRTUAL detector\n"));
#endif
memset(udpDetails, 0, sizeof(udpDetails));
udpDetails[0].srcport = DEFAULT_UDP_SRC_PORTNO;
udpDetails[0].srcport2 = DEFAULT_UDP_SRC_PORTNO + 1;
udpDetails[0].dstport = DEFAULT_UDP_DST_PORTNO;
udpDetails[0].dstport2 = DEFAULT_UDP_DST_PORTNO + 1;
if (isControlServer) {
basictests();
initControlServer();
@ -4918,50 +4923,52 @@ int check_detector_idle(const char *s) {
}
int is_udp_configured() {
if (udpDetails[0].dstip == 0) {
strcpy(configureMessage, "udp destination ip not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[0].srcip == 0) {
strcpy(configureMessage, "udp source ip not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[0].srcmac == 0) {
strcpy(configureMessage, "udp source mac not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[0].dstmac == 0) {
strcpy(configureMessage, "udp destination mac not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
for (int i = 0; i != numUdpDestinations; ++i) {
if (udpDetails[i].dstip == 0) {
strcpy(configureMessage, "udp destination ip not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[i].srcip == 0) {
strcpy(configureMessage, "udp source ip not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[i].srcmac == 0) {
strcpy(configureMessage, "udp source mac not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[i].dstmac == 0) {
strcpy(configureMessage, "udp destination mac not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
#if defined(JUNGFRAUD) || defined(GOTTHARD2D)
if (getNumberofUDPInterfaces() == 2) {
if (udpDetails[0].srcip2 == 0) {
strcpy(configureMessage, "udp source ip2 not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
if (getNumberofUDPInterfaces() == 2) {
if (udpDetails[i].srcip2 == 0) {
strcpy(configureMessage, "udp source ip2 not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[i].dstip2 == 0) {
strcpy(configureMessage, "udp destination ip2 not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[i].srcmac2 == 0) {
strcpy(configureMessage, "udp source mac2 not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[i].dstmac2 == 0) {
strcpy(configureMessage, "udp destination mac2 not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
}
if (udpDetails[0].dstip2 == 0) {
strcpy(configureMessage, "udp destination ip2 not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[0].srcmac2 == 0) {
strcpy(configureMessage, "udp source mac2 not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
if (udpDetails[0].dstmac2 == 0) {
strcpy(configureMessage, "udp destination mac2 not configured\n");
LOG(logWARNING, ("%s", configureMessage));
return FAIL;
}
}
#endif
}
return OK;
}
@ -8988,14 +8995,17 @@ int get_dest_udp_list(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
uint32_t arg = 0;
uint32_t retvals[5]{};
uint64_t retvals64[2]{};
uint32_t retvals[5] = {};
uint64_t retvals64[2] = {};
if (receiveData(file_des, &arg, sizeof(arg), INT32) < 0)
return printSocketReadError();
LOG(logDEBUG1, ("Getting udp destination list for entry %d\n", arg));
#if !defined(EIGERD) && !defined(JUNGFRAUD)
functionNotImplemented();
#else
if (arg < 0 || arg > MAX_UDP_DESTINATION) {
if (arg > MAX_UDP_DESTINATION) {
ret = FAIL;
sprintf(
mess,
@ -9008,24 +9018,30 @@ int get_dest_udp_list(int file_des) {
retvals[2] = udpDetails[arg].dstport2;
retvals[3] = udpDetails[arg].dstip;
retvals[4] = udpDetails[arg].dstip2;
retvals_64[0] = udpDetails[arg].dstmac;
retvals_64[1] = udpDetails[arg].dstmac2;
retvals64[0] = udpDetails[arg].dstmac;
retvals64[1] = udpDetails[arg].dstmac2;
// swap ip
retvals[3] = __builtin_bswap32(retvals[3]);
retvals[4] = __builtin_bswap32(retvals[4]);
// convert to string
char ip[INET_ADDRSTRLEN], ip2[INET_ADDRSTRLEN];
getIpAddressinString(ip, retvals[3]);
getIpAddressinString(ip2, retvals[4]);
char mac[50], mac2[50];
getMacAddressinString(mac, 50, retvals_64[0]);
getMacAddressinString(mac2, 50, retvals_64[1]);
getMacAddressinString(mac, 50, retvals64[0]);
getMacAddressinString(mac2, 50, retvals64[1]);
LOG(logDEBUG1,
("Udp Dest. retval [%d]: [port:%d, port2:%d, ip:%s, ip2:%s, "
"mac:%s, mac2:%s]\n",
("Udp Dest. retval [%d]: [port %d, port2 %d, ip %s, ip2 %s, "
"mac %s, mac2 %s]\n",
retvals[0], retvals[1], retvals[2], ip, ip2, mac, mac2));
}
#endif
Server_SendResult(file_des, INT32, NULL, 0);
if (ret != FAIL) {
sendData(file_des, retvals, sizeof(retvals), INT32);
sendData(file_des, retvals_64, sizeof(retvals_64), INT64);
sendData(file_des, retvals64, sizeof(retvals64), INT64);
}
return ret;
}
@ -9033,33 +9049,65 @@ int get_dest_udp_list(int file_des) {
int set_dest_udp_list(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
uint32_t args[5]{};
uint64_t args_64[2]{};
uint32_t args[5] = {};
uint64_t args64[2] = {};
if (receiveData(file_des, args, sizeof(args), INT32) < 0)
return printSocketReadError();
if (receiveData(file_des, args_64, sizeof(args_64), INT64) < 0)
if (receiveData(file_des, args64, sizeof(args64), INT64) < 0)
return printSocketReadError();
// swap ip
args[3] = __builtin_bswap32(args[3]);
args[4] = __builtin_bswap32(args[4]);
LOG(logINFOBLUE, ("Setting current source [enable:%d, fix:%d, select:%lld, "
"normal:%d]\n",
enable, fix, (long long int)select, normal));
// convert to string
char ip[INET_ADDRSTRLEN], ip2[INET_ADDRSTRLEN];
getIpAddressinString(ip, args[3]);
getIpAddressinString(ip2, args[4]);
char mac[50], mac2[50];
getMacAddressinString(mac, 50, args64[0]);
getMacAddressinString(mac2, 50, args64[1]);
#if !defined(EIGERD) && !defined(JUNGFRAUD)
functionNotImplemented();
#else
// only set
if (Server_VerifyLock() == OK) {
if (arg < 1 || arg > MAX_UDP_DESTINATION) {
ret = FAIL;
sprintf(
mess,
"Could not set udp destination. Invalid entry. Options: 1 - %d\n",
MAX_UDP_DESTINATION);
LOG(logERROR, (mess));
} else {
int entry = args[0];
LOG(logINFOBLUE,
("Setting udp dest. [%d]: [port %d, port2 %d, ip %s, ip2 %s, "
"mac %s, mac2 %s]\n",
entry, args[1], args[2], ip, ip2, mac, mac2));
if (entry < 1 || entry > MAX_UDP_DESTINATION) {
ret = FAIL;
sprintf(
mess,
"Could not set udp destination. Invalid entry. Options: 1 - %d\n",
MAX_UDP_DESTINATION);
LOG(logERROR, (mess));
}
#ifdef EIGERD
else if (args[4] != 0 || args64[1] != 0) {
ret = FAIL;
strcpy(
mess,
"Could not set udp destination. ip2 and mac2 not implemented for this detector.\n");
LOG(logERROR, (mess));
}
#endif
else {
if (check_detector_idle("set udp destination list entries") == OK) {
udpDetails[entry].dstport = args[1];
udpDetails[entry].dstport2 = args[2];
udpDetails[entry].dstip = args[3];
udpDetails[entry].dstip2 = args[4];
udpDetails[entry].dstmac = args64[0];
udpDetails[entry].dstmac2 = args64[1];
configure_mac();
}
}
}
#endif
return Server_SendResult(file_des, INT32, NULL, 0);