livoxProto1:bug workaround: for boost:asio:udp async ops on dlopen libs

Updated Boost dependency to version 1.73.0 to address segfault issues with boost::asio in dynamic libraries. Refactored heartbeat socket management to use raw UDP sockets instead of boost::asio, improving compatibility and error handling during socket operations.
This commit is contained in:
2025-09-07 07:27:14 -04:00
parent 9a500e39ab
commit 1ededb85b9
3 changed files with 58 additions and 18 deletions
+4 -1
View File
@@ -36,7 +36,10 @@ include_directories(
) )
# Find core dependencies # Find core dependencies
find_package(Boost 1.69.0 REQUIRED COMPONENTS system) # Boost 1.72.0 is required to ensure that a certain bug where boost::asio
# objects depend on specific copies of symbols, and boost will cause a segfault
# if boost::asio objects are used inside of a dlopen()'d library, is fixed.
find_package(Boost 1.73.0 REQUIRED COMPONENTS system)
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
find_package(FLEX REQUIRED) find_package(FLEX REQUIRED)
find_package(BISON REQUIRED) find_package(BISON REQUIRED)
+49 -16
View File
@@ -8,6 +8,8 @@
#include <ifaddrs.h> #include <ifaddrs.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <errno.h>
#include <cstring>
#include <netinet/in.h> #include <netinet/in.h>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "device.h" #include "device.h"
@@ -77,6 +79,7 @@ componentThread(componentThread),
handshakeTimeoutMs(handshakeTimeoutMs), retryDelayMs(retryDelayMs), handshakeTimeoutMs(handshakeTimeoutMs), retryDelayMs(retryDelayMs),
smoIp(smoIp), smoSubnetNbits(smoSubnetNbits), smoIp(smoIp), smoSubnetNbits(smoSubnetNbits),
dataPort(dataPort), cmdPort(cmdPort), imuPort(imuPort), dataPort(dataPort), cmdPort(cmdPort), imuPort(imuPort),
heartbeatSocketFd(-1),
heartbeatActive(false) heartbeatActive(false)
{ {
connect(); connect();
@@ -92,7 +95,10 @@ Device::~Device()
} }
heartbeatTimer.reset(); heartbeatTimer.reset();
heartbeatSocket.reset(); if (heartbeatSocketFd >= 0) {
close(heartbeatSocketFd);
heartbeatSocketFd = -1;
}
} }
void Device::connect() void Device::connect()
@@ -426,20 +432,36 @@ void Device::startHeartbeat()
} }
/** EXPLANATION: /** EXPLANATION:
* Create heartbeat socket using io_service of the componentThread that was * Create raw UDP socket for heartbeat sending to avoid boost::asio dlopen
* given to us for use by this device. * issues.
*/ */
heartbeatSocket = std::make_unique<boost::asio::ip::udp::socket>( heartbeatSocketFd = socket(AF_INET, SOCK_DGRAM, 0);
componentThread->getIoService()); if (heartbeatSocketFd < 0)
heartbeatSocket->open(boost::asio::ip::udp::v4()); {
std::cerr << "[" << __func__ << "] Failed to create heartbeat socket: "
<< strerror(errno) << std::endl;
return;
}
/** EXPLANATION: /** EXPLANATION:
* Bind heartbeat socket to cmdPort so heartbeats come from the same port * Bind heartbeat socket to cmdPort so heartbeats come from the same port
* as handshake. * as handshake.
*/ */
boost::asio::ip::udp::endpoint heartbeatLocalEndpoint( struct sockaddr_in localAddr;
boost::asio::ip::address_v4::any(), cmdPort); memset(&localAddr, 0, sizeof(localAddr));
heartbeatSocket->bind(heartbeatLocalEndpoint); localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = INADDR_ANY;
localAddr.sin_port = htons(cmdPort);
if (bind(
heartbeatSocketFd, (struct sockaddr*)&localAddr, sizeof(localAddr)) < 0)
{
std::cerr << "[" << __func__ << "] Failed to bind heartbeat socket to "
"port " << cmdPort << ": " << strerror(errno) << std::endl;
close(heartbeatSocketFd);
heartbeatSocketFd = -1;
return;
}
// Create heartbeat timer // Create heartbeat timer
heartbeatTimer = std::make_unique<boost::asio::deadline_timer>( heartbeatTimer = std::make_unique<boost::asio::deadline_timer>(
@@ -453,7 +475,7 @@ void Device::startHeartbeat()
void Device::sendHeartbeat() void Device::sendHeartbeat()
{ {
if (!heartbeatActive.load() || !heartbeatSocket if (!heartbeatActive.load() || heartbeatSocketFd < 0
|| discoveredDevice.ipAddr.empty()) || discoveredDevice.ipAddr.empty())
{ {
return; return;
@@ -468,13 +490,24 @@ void Device::sendHeartbeat()
heartbeatMsg.footer.crc_32 = heartbeatMsg.calculateCrc32(); heartbeatMsg.footer.crc_32 = heartbeatMsg.calculateCrc32();
heartbeatMsg.footer.swapCrc32ToProtocolEndianness(); heartbeatMsg.footer.swapCrc32ToProtocolEndianness();
boost::asio::ip::udp::endpoint deviceEndpoint( // Set up destination address for raw socket
boost::asio::ip::address::from_string(discoveredDevice.ipAddr), struct sockaddr_in deviceAddr;
65000); // Heartbeats and commands go to port 65000 memset(&deviceAddr, 0, sizeof(deviceAddr));
deviceAddr.sin_family = AF_INET;
deviceAddr.sin_addr.s_addr = inet_addr(discoveredDevice.ipAddr.c_str());
// Heartbeats and commands go to port 65000
deviceAddr.sin_port = htons(65000);
heartbeatSocket->send_to( ssize_t bytesSent = sendto(
boost::asio::buffer(&heartbeatMsg, sizeof(heartbeatMsg)), heartbeatSocketFd, &heartbeatMsg, sizeof(heartbeatMsg), 0,
deviceEndpoint); (struct sockaddr*)&deviceAddr, sizeof(deviceAddr));
if (bytesSent < 0)
{
std::cerr << "[" << __func__ << "] Failed to send heartbeat: "
<< strerror(errno) << std::endl;
return;
}
/** EXPLANATION: /** EXPLANATION:
* Schedule next heartbeat in 1 second, per the spec. * Schedule next heartbeat in 1 second, per the spec.
+5 -1
View File
@@ -6,6 +6,10 @@
#include <memory> #include <memory>
#include <atomic> #include <atomic>
#include <optional> #include <optional>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "protocol.h" #include "protocol.h"
@@ -94,7 +98,7 @@ private:
// Heartbeat state // Heartbeat state
std::unique_ptr<boost::asio::deadline_timer> heartbeatTimer; std::unique_ptr<boost::asio::deadline_timer> heartbeatTimer;
std::unique_ptr<boost::asio::ip::udp::socket> heartbeatSocket; int heartbeatSocketFd; // Raw socket file descriptor
std::atomic<bool> heartbeatActive; std::atomic<bool> heartbeatActive;
}; };