From 5b5c01862c0ed3a018f8377e57c0e9fcd5e04cc9 Mon Sep 17 00:00:00 2001 From: Graeme Winter Date: Wed, 11 Dec 2024 15:05:00 +0000 Subject: [PATCH] Explicit bind to correct interface --- .../slsDetectorFunctionList.c | 14 +++++++++++ .../include/communication_funcs_UDP.h | 1 + .../src/communication_funcs_UDP.c | 25 ++++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c index 39a84eb28c..911c2dd759 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c @@ -1715,6 +1715,20 @@ int configureMAC() { src_ip2, src_mac2, srcport2, dst_ip2, dst_mac2, dstport2)); } #ifdef VIRTUAL + if (setUDPSourceDetails(iRxEntry, 0, src_ip) == FAIL) { + LOG(logERROR, ("could not set udp source IP for " + "interface 1 [entry:%d] \n", + iRxEntry)); + return FAIL; + } + if (numInterfaces == 2 && + setUDPSourceDetails(iRxEntry, 1, src_ip2) == FAIL) { + LOG(logERROR, ("could not set udp source IP for " + "interface 2 [entry:%d]\n", + iRxEntry)); + return FAIL; + } + if (setUDPDestinationDetails(iRxEntry, 0, dst_ip, dstport) == FAIL) { LOG(logERROR, ("could not set udp destination IP and port for " "interface 1 [entry:%d] \n", diff --git a/slsDetectorServers/slsDetectorServer/include/communication_funcs_UDP.h b/slsDetectorServers/slsDetectorServer/include/communication_funcs_UDP.h index ecd9d5deb3..f119d530de 100644 --- a/slsDetectorServers/slsDetectorServer/include/communication_funcs_UDP.h +++ b/slsDetectorServers/slsDetectorServer/include/communication_funcs_UDP.h @@ -10,6 +10,7 @@ void setNumberOfUDPDestinations(int value); int setUDPDestinationDetails(int iRxEntry, int index, const char *ip, unsigned short int port); +int setUDPSourceDetails(int iRxEntry, int index, const char *ip); int createUDPSocket(int index); int sendUDPPacket(int iRxEntry, int index, const char *buf, int length); diff --git a/slsDetectorServers/slsDetectorServer/src/communication_funcs_UDP.c b/slsDetectorServers/slsDetectorServer/src/communication_funcs_UDP.c index 1904a1804c..ffa56c457b 100644 --- a/slsDetectorServers/slsDetectorServer/src/communication_funcs_UDP.c +++ b/slsDetectorServers/slsDetectorServer/src/communication_funcs_UDP.c @@ -19,6 +19,7 @@ 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] = {}; +char udpSourceIp[MAX_UDP_DESTINATION][2][INET_ADDRSTRLEN] = {}; extern int numUdpDestinations; void setupUDPCommParameters() { @@ -28,12 +29,25 @@ void setupUDPCommParameters() { } memset(udpServerAddrInfo, 0, sizeof(udpServerAddrInfo)); memset(udpDestinationIp, 0, sizeof(udpDestinationIp)); + memset(udpSourceIp, 0, sizeof(udpDestinationIp)); } int getUdPSocketDescriptor(int iRxEntry, int index) { return udpSockfd[iRxEntry][index]; } +int setUDPSourceDetails(int iRxEntry, int index, const char *ip) { + LOG(logDEBUG1, + ("Setting udp source details for socket %d [iRxEntry:%d]\n", index, + iRxEntry)); + size_t len = strlen(ip); + memset(udpSourceIp[iRxEntry][index], 0, INET_ADDRSTRLEN); + strncpy(udpSourceIp[iRxEntry][index], ip, + len > INET_ADDRSTRLEN ? INET_ADDRSTRLEN : len); + + return OK; +} + int setUDPDestinationDetails(int iRxEntry, int index, const char *ip, unsigned short int port) { LOG(logDEBUG1, @@ -86,9 +100,6 @@ int setUDPDestinationDetails(int iRxEntry, int index, const char *ip, int createUDPSocket(int index) { - - - int sendbuff; socklen_t optlen; @@ -136,6 +147,14 @@ int createUDPSocket(int index) { // 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 + + struct sockaddr src_sock; + src_sock.sa_family = AF_INET; + memcpy(src_sock.sa_data, udpSourceIp[iRxEntry][index], strlen(udpSourceIp[iRxEntry][index])); + + bind(udpSockfd[iRxEntry][index], &src_sock, sizeof(struct sockaddr)); + LOG(logINFO, ("UDP socket bound to %s\n", udpSourceIp[iRxEntry][index])); + connect(udpSockfd[iRxEntry][index], udpServerAddrInfo[iRxEntry][index]->ai_addr, udpServerAddrInfo[iRxEntry][index]->ai_addrlen); LOG(logINFO, ("Udp client socket connected [%d, %d, %s]\n", iRxEntry, udpDestinationPort[iRxEntry][index],