Skip to content

Commit 337b04a

Browse files
committed
Fix Linux build
1 parent eb94fe8 commit 337b04a

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/delegate-mq/predef/transport/linux-udp/MulticastTransport.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <unistd.h>
1313
#include <arpa/inet.h>
1414
#include <sys/socket.h>
15+
#include <net/if.h>
1516
#include <fcntl.h>
1617
#include <errno.h>
1718

@@ -28,7 +29,7 @@ class MulticastTransport : public ITransport
2829
MulticastTransport() = default;
2930
~MulticastTransport() { Close(); }
3031

31-
int Create(Type type, const char* groupAddr, uint16_t port)
32+
int Create(Type type, const char* groupAddr, uint16_t port, const char* localInterface = "0.0.0.0")
3233
{
3334
m_type = type;
3435

@@ -54,11 +55,21 @@ class MulticastTransport : public ITransport
5455
return -1;
5556
}
5657

58+
// Set the interface for outgoing multicast data
59+
struct in_addr localAddr;
60+
inet_aton(localInterface, &localAddr);
61+
if (setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_IF, &localAddr, sizeof(localAddr)) < 0) {
62+
std::cerr << "IP_MULTICAST_IF failed." << std::endl;
63+
}
64+
5765
int ttl = 3;
5866
if (setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
5967
std::cerr << "IP_MULTICAST_TTL failed." << std::endl;
6068
return -1;
6169
}
70+
71+
int loop = 1;
72+
setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
6273
}
6374
else {
6475
m_addr.sin_addr.s_addr = INADDR_ANY;
@@ -69,7 +80,7 @@ class MulticastTransport : public ITransport
6980

7081
struct ip_mreq mreq;
7182
mreq.imr_multiaddr.s_addr = inet_addr(groupAddr);
72-
mreq.imr_interface.s_addr = INADDR_ANY;
83+
mreq.imr_interface.s_addr = inet_addr(localInterface);
7384
if (setsockopt(m_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
7485
std::cerr << "IP_ADD_MEMBERSHIP failed: " << strerror(errno) << std::endl;
7586
return -1;

0 commit comments

Comments
 (0)