Skip to content

Commit 22b987b

Browse files
authored
Make multicast-group helpers and vsock fallback portable to QNX (#20)
* Make multicast-group helpers and vsock fallback portable to QNX Two small portability fixes so this builds on QNX (and stays building on macOS / BSDs): * UDPSocket::JoinMulticastGroup / LeaveMulticastGroup used the Linux-only struct ip_mreqn (with imr_ifindex). Switch to the POSIX-portable struct ip_mreq. Behaviour is unchanged on Linux: the previous code passed imr_ifindex = 0, which is equivalent to INADDR_ANY in ip_mreq.imr_interface. * The fallback definition of struct sockaddr_vm in sockets.h had a typo `_APPLE__` (single leading underscore). This is harmless on Apple, where <sys/vsock.h> is used instead, but on a system that actually hits the fallback (e.g. QNX) a true `__APPLE__` macro would never have inserted svm_len. Fix the spelling. Made-with: Cursor * Fix bazel test //...
1 parent 9ec3f8d commit 22b987b

5 files changed

Lines changed: 16 additions & 10 deletions

File tree

.bazelignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
bazel-toolbelt
2+
_deps
3+
build

MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

toolbelt/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ cc_test(
8282
)
8383
cc_test(
8484
name = "pipe_test",
85-
size = "small",
85+
size = "medium",
8686
srcs = ["pipe_test.cc"],
8787
deps = [
8888
":toolbelt",

toolbelt/sockets.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,13 @@ absl::Status UDPSocket::Bind(const InetAddress &addr) {
799799
}
800800

801801
absl::Status UDPSocket::JoinMulticastGroup(const InetAddress &addr) {
802-
ip_mreqn membership_request{.imr_multiaddr = addr.GetAddress().sin_addr,
803-
.imr_address = {INADDR_ANY},
804-
.imr_ifindex = 0};
802+
// Use POSIX-portable ip_mreq instead of Linux-specific ip_mreqn so this
803+
// builds on macOS, BSDs and QNX as well. We always join via the default
804+
// interface (INADDR_ANY); callers needing per-interface control should
805+
// extend this API.
806+
struct ip_mreq membership_request = {};
807+
membership_request.imr_multiaddr = addr.GetAddress().sin_addr;
808+
membership_request.imr_interface.s_addr = htonl(INADDR_ANY);
805809
int setsockopt_ret =
806810
::setsockopt(fd_.Fd(), IPPROTO_IP, IP_ADD_MEMBERSHIP, &membership_request,
807811
sizeof(membership_request));
@@ -815,9 +819,9 @@ absl::Status UDPSocket::JoinMulticastGroup(const InetAddress &addr) {
815819
}
816820

817821
absl::Status UDPSocket::LeaveMulticastGroup(const InetAddress &addr) {
818-
ip_mreqn membership_request{.imr_multiaddr = addr.GetAddress().sin_addr,
819-
.imr_address = {INADDR_ANY},
820-
.imr_ifindex = 0};
822+
struct ip_mreq membership_request = {};
823+
membership_request.imr_multiaddr = addr.GetAddress().sin_addr;
824+
membership_request.imr_interface.s_addr = htonl(INADDR_ANY);
821825
int setsockopt_ret =
822826
::setsockopt(fd_.Fd(), IPPROTO_IP, IP_DROP_MEMBERSHIP,
823827
&membership_request, sizeof(membership_request));

toolbelt/sockets.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// Older systems may not have the header file.
3838
#if !HAS_VM_SOCKETS
3939
struct sockaddr_vm {
40-
#if defined(_APPLE__)
40+
#if defined(__APPLE__)
4141
uint8_t svm_len; /* total length of sockaddr */
4242
#endif
4343
sa_family_t svm_family; /* AF_VSOCK */

0 commit comments

Comments
 (0)