Skip to content

Commit 5755fc8

Browse files
authored
Port socket helpers to QNX (#22)
1 parent ba9623a commit 5755fc8

2 files changed

Lines changed: 25 additions & 34 deletions

File tree

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module(
22
name = "cpp_toolbelt",
3-
version = "2.1.1",
3+
version = "2.1.2",
44
)
55

66
bazel_dep(name = "platforms", version = "1.0.0")

toolbelt/sockets.cc

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,24 @@ InetAddress InetAddress::AnyAddress(int port) { return InetAddress(port); }
3232

3333
InetAddress::InetAddress(const in_addr &ip, int port) {
3434
valid_ = true;
35-
addr_ = {
35+
addr_ = {};
3636
#if defined(__APPLE__)
37-
.sin_len = sizeof(struct sockaddr_in),
37+
addr_.sin_len = sizeof(struct sockaddr_in);
3838
#endif
39-
.sin_family = AF_INET,
40-
.sin_port = htons(port),
41-
.sin_addr = {.s_addr = ip.s_addr},
42-
.sin_zero = {0}
43-
};
39+
addr_.sin_family = AF_INET;
40+
addr_.sin_port = htons(port);
41+
addr_.sin_addr.s_addr = ip.s_addr;
4442
}
4543

4644
InetAddress::InetAddress(int port) {
4745
valid_ = true;
48-
addr_ = {
46+
addr_ = {};
4947
#if defined(__APPLE__)
50-
.sin_len = sizeof(struct sockaddr_in),
48+
addr_.sin_len = sizeof(struct sockaddr_in);
5149
#endif
52-
.sin_family = AF_INET,
53-
.sin_port = htons(port),
54-
.sin_addr = {.s_addr = INADDR_ANY},
55-
.sin_zero = {0}
56-
};
50+
addr_.sin_family = AF_INET;
51+
addr_.sin_port = htons(port);
52+
addr_.sin_addr.s_addr = INADDR_ANY;
5753
}
5854

5955
InetAddress::InetAddress(const std::string &hostname, int port) {
@@ -71,15 +67,13 @@ InetAddress::InetAddress(const std::string &hostname, int port) {
7167
}
7268
}
7369
valid_ = true;
74-
addr_ = {
70+
addr_ = {};
7571
#if defined(__APPLE__)
76-
.sin_len = sizeof(struct sockaddr_in),
72+
addr_.sin_len = sizeof(struct sockaddr_in);
7773
#endif
78-
.sin_family = AF_INET,
79-
.sin_port = htons(port),
80-
.sin_addr = {.s_addr = ipaddr},
81-
.sin_zero = {0}
82-
};
74+
addr_.sin_family = AF_INET;
75+
addr_.sin_port = htons(port);
76+
addr_.sin_addr.s_addr = ipaddr;
8377
}
8478

8579
std::string InetAddress::ToString() const {
@@ -468,10 +462,7 @@ absl::Status UnixSocket::SendFds(const std::vector<FileDescriptor> &fds,
468462
return absl::InternalError("Socket is not connected");
469463
}
470464
constexpr size_t kMaxFds = 252;
471-
union {
472-
char buf[CMSG_SPACE(kMaxFds * sizeof(int))];
473-
struct cmsghdr align;
474-
} u;
465+
std::vector<char> control_buf(CMSG_SPACE(kMaxFds * sizeof(int)));
475466

476467
// We send the total number file descriptors. There is a limit to the
477468
// number we can send in one message.
@@ -480,7 +471,7 @@ absl::Status UnixSocket::SendFds(const std::vector<FileDescriptor> &fds,
480471

481472
// We need to send at least one message, even if there are no fds to send.
482473
do {
483-
memset(u.buf, 0, sizeof(u.buf));
474+
std::fill(control_buf.begin(), control_buf.end(), 0);
484475

485476
int32_t num_fds = static_cast<int32_t>(fds.size());
486477
size_t fds_to_send = remaining_fds > kMaxFds ? kMaxFds : remaining_fds;
@@ -496,7 +487,7 @@ absl::Status UnixSocket::SendFds(const std::vector<FileDescriptor> &fds,
496487
#endif
497488
struct msghdr msg = {.msg_iov = &iov,
498489
.msg_iovlen = 1,
499-
.msg_control = u.buf,
490+
.msg_control = control_buf.data(),
500491
.msg_controllen =
501492
static_cast<socklen_t>(CMSG_SPACE(fds_size))};
502493
#if defined(__clang__)
@@ -536,13 +527,12 @@ absl::Status UnixSocket::ReceiveFds(std::vector<FileDescriptor> &fds,
536527
return absl::InternalError("Socket is not connected");
537528
}
538529
constexpr size_t kMaxFds = 252;
539-
union {
540-
char buf[CMSG_SPACE(kMaxFds * sizeof(int))];
541-
struct cmsghdr align;
542-
} u;
530+
std::vector<char> control_buf(CMSG_SPACE(kMaxFds * sizeof(int)));
543531

544532
int32_t num_fds_received = 0;
545533
for (;;) {
534+
std::fill(control_buf.begin(), control_buf.end(), 0);
535+
546536
// The total number of fds we need to see. This is
547537
// sent in each message, but each message contains only portion
548538
// of the total (there's a limit per message).
@@ -559,8 +549,9 @@ absl::Status UnixSocket::ReceiveFds(std::vector<FileDescriptor> &fds,
559549
#endif
560550
struct msghdr msg = {.msg_iov = &iov,
561551
.msg_iovlen = 1,
562-
.msg_control = u.buf,
563-
.msg_controllen = sizeof(u.buf)};
552+
.msg_control = control_buf.data(),
553+
.msg_controllen =
554+
static_cast<socklen_t>(control_buf.size())};
564555
#if defined(__clang__)
565556
#pragma clang diagnostic pop
566557
#elif defined(__GNUC__)

0 commit comments

Comments
 (0)