Skip to content

Commit 6bbde3f

Browse files
committed
Fix unittest macos failure
1 parent b1d13bf commit 6bbde3f

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- Add GitHub CI workflow
55
- Fix MacOS build
66
- Fix example log macros doesn't work without format args
7+
- Added socket binding (lines 165-176) - Bind to a local address before setting multicast options. This is required on some platforms like macOS for multicast senders.
8+
- Fixed setsockopt calls (lines 180, 189, 203) - Changed from passing raw pointer to using reinterpret_cast<const char*>(&value). While this is technically the same thing, it's more explicit about the expected type.
79

810
#1.0.0.0 - [09/26/2025]
911
- Initial Release

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,6 @@ This design uses CRTP (Curiously Recurring Template Pattern) for compile-time po
221221

222222
## License
223223

224-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
224+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
225+
226+
**Made with ⚡ by [SlickQuant](https://github.com/SlickQuant)**

include/slick/socket/multicast_sender_unix.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,22 @@ void MulticastSenderBase<DerivedT>::cleanup_socket()
162162
template<typename DerivedT>
163163
bool MulticastSenderBase<DerivedT>::setup_multicast_options()
164164
{
165+
// Bind to local address for sending (required on some platforms like macOS)
166+
sockaddr_in local_addr{};
167+
local_addr.sin_family = AF_INET;
168+
local_addr.sin_port = 0; // Let OS choose ephemeral port
169+
local_addr.sin_addr.s_addr = INADDR_ANY;
170+
171+
if (bind(socket_, reinterpret_cast<const sockaddr*>(&local_addr), sizeof(local_addr)) < 0)
172+
{
173+
int error = errno;
174+
LOG_WARN("Failed to bind socket to local address. error={} ({})", error, strerror(error));
175+
// Don't fail on this, as it might work without binding
176+
}
177+
165178
// Set TTL for multicast packets
166179
int ttl = config_.ttl;
167-
if (setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
180+
if (setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_TTL, reinterpret_cast<const char*>(&ttl), sizeof(ttl)) < 0)
168181
{
169182
int error = errno;
170183
LOG_ERROR("Failed to set multicast TTL. error={} ({})", error, strerror(error));
@@ -173,7 +186,7 @@ bool MulticastSenderBase<DerivedT>::setup_multicast_options()
173186

174187
// Set multicast loopback
175188
int loopback = config_.enable_loopback ? 1 : 0;
176-
if (setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_LOOP, &loopback, sizeof(loopback)) < 0)
189+
if (setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_LOOP, reinterpret_cast<const char*>(&loopback), sizeof(loopback)) < 0)
177190
{
178191
int error = errno;
179192
LOG_ERROR("Failed to set multicast loopback. error={} ({})", error, strerror(error));
@@ -187,7 +200,7 @@ bool MulticastSenderBase<DerivedT>::setup_multicast_options()
187200
int result = inet_pton(AF_INET, config_.interface_address.c_str(), &interface_addr);
188201
if (result == 1)
189202
{
190-
if (setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr, sizeof(interface_addr)) < 0)
203+
if (setsockopt(socket_, IPPROTO_IP, IP_MULTICAST_IF, reinterpret_cast<const char*>(&interface_addr), sizeof(interface_addr)) < 0)
191204
{
192205
int error = errno;
193206
LOG_ERROR("Failed to set multicast interface. error={} ({})", error, strerror(error));

0 commit comments

Comments
 (0)