Skip to content

Commit

Permalink
Explicit bind to correct interface
Browse files Browse the repository at this point in the history
  • Loading branch information
graeme-winter committed Dec 11, 2024
1 parent 40f78e5 commit 5b5c018
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 22 additions & 3 deletions slsDetectorServers/slsDetectorServer/src/communication_funcs_UDP.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
Expand Down Expand Up @@ -86,9 +100,6 @@ int setUDPDestinationDetails(int iRxEntry, int index, const char *ip,

int createUDPSocket(int index) {




int sendbuff;
socklen_t optlen;

Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit 5b5c018

Please sign in to comment.