From 59dc218a8503435d0c616258af2128a400033388 Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Wed, 6 Dec 2023 17:04:13 +0100 Subject: [PATCH] linux fix --- .../src/io/udp/sendreceive/linux/socket_os.h | 77 ++++++++++--------- .../io/udp/sendreceive/udp_receiver_asio.cpp | 4 +- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/ecal/core/src/io/udp/sendreceive/linux/socket_os.h b/ecal/core/src/io/udp/sendreceive/linux/socket_os.h index f4cdafd474..a436413216 100644 --- a/ecal/core/src/io/udp/sendreceive/linux/socket_os.h +++ b/ecal/core/src/io/udp/sendreceive/linux/socket_os.h @@ -28,58 +28,61 @@ #include -namespace eCAL +namespace IO { - inline static std::vector get_interface_index_list() + namespace UDP { - std::vector interface_index_list; - ifaddrs* ifa = nullptr; - ifaddrs* ifap = nullptr; + inline static std::vector get_interface_index_list() + { + std::vector 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(&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(&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); + } } } diff --git a/ecal/core/src/io/udp/sendreceive/udp_receiver_asio.cpp b/ecal/core/src/io/udp/sendreceive/udp_receiver_asio.cpp index f469fae727..855fb33f02 100644 --- a/ecal/core/src/io/udp/sendreceive/udp_receiver_asio.cpp +++ b/ecal/core/src/io/udp/sendreceive/udp_receiver_asio.cpp @@ -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); } @@ -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); }