Skip to content

Commit

Permalink
linux fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rex-schilasky committed Dec 6, 2023
1 parent f406a3f commit 59dc218
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
77 changes: 40 additions & 37 deletions ecal/core/src/io/udp/sendreceive/linux/socket_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,61 @@
#include <vector>


namespace eCAL
namespace IO
{
inline static std::vector<int> get_interface_index_list()
namespace UDP
{
std::vector<int> interface_index_list;
ifaddrs* ifa = nullptr;
ifaddrs* ifap = nullptr;
inline static std::vector<int> get_interface_index_list()
{
std::vector<int> interface_index_list;
ifaddrs* ifa = nullptr;
ifaddrs* ifap = nullptr;

// get a list of network interfaces
getifaddrs(&ifap);
// get a list of network interfaces
getifaddrs(&ifap);

// create a list of network interfaces indexes
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_PACKET)
// create a list of network interfaces indexes
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
{
int index = if_nametoindex(ifa->ifa_name);
if (index)
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_PACKET)
{
interface_index_list.push_back(index);
int index = if_nametoindex(ifa->ifa_name);
if (index)
{
interface_index_list.push_back(index);
}
}
}
}

freeifaddrs(ifap);
freeifaddrs(ifap);

return interface_index_list;
}
return interface_index_list;
}

inline static bool set_socket_mcast_group_option(int socket, const char* ipaddr_, int option)
{
// set the multicast socket option on all interfaces
for (int iface : get_interface_index_list())
inline static bool set_socket_mcast_group_option(int socket, const char* ipaddr_, int option)
{
group_req group_req = {};
sockaddr_in *group = nullptr;
// set the multicast socket option on all interfaces
for (int iface : get_interface_index_list())
{
group_req group_req = {};
sockaddr_in* group = nullptr;

memset(&group_req, 0, sizeof(group_req));
group_req.gr_interface = iface;
group = reinterpret_cast<sockaddr_in*>(&group_req.gr_group);
group->sin_family = AF_INET;
group->sin_addr.s_addr = inet_addr(ipaddr_);
group->sin_port = 0;
memset(&group_req, 0, sizeof(group_req));
group_req.gr_interface = iface;
group = reinterpret_cast<sockaddr_in*>(&group_req.gr_group);
group->sin_family = AF_INET;
group->sin_addr.s_addr = inet_addr(ipaddr_);
group->sin_port = 0;

int rc = setsockopt(socket, IPPROTO_IP, option, &group_req, sizeof(group_source_req));
if (rc != 0)
{
std::cerr << "setsockopt failed. Unable to set multicast group option: " << strerror(errno) << std::endl;
return(false);
int rc = setsockopt(socket, IPPROTO_IP, option, &group_req, sizeof(group_source_req));
if (rc != 0)
{
std::cerr << "setsockopt failed. Unable to set multicast group option: " << strerror(errno) << std::endl;
return(false);
}
}
}

return(true);
return(true);
}
}
}
4 changes: 2 additions & 2 deletions ecal/core/src/io/udp/sendreceive/udp_receiver_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace IO
#ifdef __linux__
if (eCAL::Config::IsUdpMulticastJoinAllIfEnabled())
{
if (!set_socket_mcast_group_option(m_socket.native_handle(), ipaddr_, MCAST_JOIN_GROUP))
if (!IO::UDP::set_socket_mcast_group_option(m_socket.native_handle(), ipaddr_, MCAST_JOIN_GROUP))
{
return(false);
}
Expand Down Expand Up @@ -149,7 +149,7 @@ namespace IO
#ifdef __linux__
if (eCAL::Config::IsUdpMulticastJoinAllIfEnabled())
{
if (!set_socket_mcast_group_option(m_socket.native_handle(), ipaddr_, MCAST_LEAVE_GROUP))
if (!IO::UDP::set_socket_mcast_group_option(m_socket.native_handle(), ipaddr_, MCAST_LEAVE_GROUP))
{
return(false);
}
Expand Down

0 comments on commit 59dc218

Please sign in to comment.