mirror of
https://github.com/LizardByte/Sunshine.git
synced 2026-12-01 17:41:12 +00:00
refactor: resolve Sonar cpp:S5945 findings (#5897)
This commit is contained in:
1 parent
88428ca4ee
commit
31392866b1
43 files changed
+858
-524
No files matched your search
+81
-111
@@ -386,35 +386,35 @@ namespace platf {
|
||||
* @brief Convert a socket address to a printable IP address.
|
||||
*/
|
||||
std::string from_sockaddr(const sockaddr *const ip_addr) {
|
||||
char data[INET6_ADDRSTRLEN] = {};
|
||||
std::array<char, INET6_ADDRSTRLEN> data = {};
|
||||
|
||||
auto family = ip_addr->sa_family;
|
||||
if (family == AF_INET6) {
|
||||
inet_ntop(AF_INET6, &((sockaddr_in6 *) ip_addr)->sin6_addr, data, INET6_ADDRSTRLEN);
|
||||
inet_ntop(AF_INET6, &((sockaddr_in6 *) ip_addr)->sin6_addr, data.data(), INET6_ADDRSTRLEN);
|
||||
} else if (family == AF_INET) {
|
||||
inet_ntop(AF_INET, &((sockaddr_in *) ip_addr)->sin_addr, data, INET_ADDRSTRLEN);
|
||||
inet_ntop(AF_INET, &((sockaddr_in *) ip_addr)->sin_addr, data.data(), INET_ADDRSTRLEN);
|
||||
}
|
||||
|
||||
return std::string {data};
|
||||
return std::string {data.data()};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert a socket address to a port and printable IP address.
|
||||
*/
|
||||
std::pair<std::uint16_t, std::string> from_sockaddr_ex(const sockaddr *const ip_addr) {
|
||||
char data[INET6_ADDRSTRLEN] = {};
|
||||
std::array<char, INET6_ADDRSTRLEN> data = {};
|
||||
|
||||
auto family = ip_addr->sa_family;
|
||||
std::uint16_t port = 0;
|
||||
if (family == AF_INET6) {
|
||||
inet_ntop(AF_INET6, &((sockaddr_in6 *) ip_addr)->sin6_addr, data, INET6_ADDRSTRLEN);
|
||||
inet_ntop(AF_INET6, &((sockaddr_in6 *) ip_addr)->sin6_addr, data.data(), INET6_ADDRSTRLEN);
|
||||
port = ((sockaddr_in6 *) ip_addr)->sin6_port;
|
||||
} else if (family == AF_INET) {
|
||||
inet_ntop(AF_INET, &((sockaddr_in *) ip_addr)->sin_addr, data, INET_ADDRSTRLEN);
|
||||
inet_ntop(AF_INET, &((sockaddr_in *) ip_addr)->sin_addr, data.data(), INET_ADDRSTRLEN);
|
||||
port = ((sockaddr_in *) ip_addr)->sin_port;
|
||||
}
|
||||
|
||||
return {port, std::string {data}};
|
||||
return {port, std::string {data.data()}};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -611,8 +611,8 @@ namespace platf {
|
||||
* @brief Request a Sunshine process restart on exit.
|
||||
*/
|
||||
void restart_on_exit() {
|
||||
char executable[PATH_MAX];
|
||||
ssize_t len = readlink("/proc/self/exe", executable, PATH_MAX - 1);
|
||||
std::array<char, PATH_MAX> executable;
|
||||
ssize_t len = readlink("/proc/self/exe", executable.data(), executable.size() - 1);
|
||||
if (len == -1) {
|
||||
BOOST_LOG(fatal) << "readlink() failed: "sv << errno;
|
||||
return;
|
||||
@@ -626,7 +626,7 @@ namespace platf {
|
||||
}
|
||||
|
||||
// Re-exec ourselves with the same arguments
|
||||
if (execv(executable, lifetime::get_argv()) < 0) {
|
||||
if (execv(executable.data(), lifetime::get_argv()) < 0) {
|
||||
BOOST_LOG(fatal) << "execv() failed: "sv << errno;
|
||||
return;
|
||||
}
|
||||
@@ -694,6 +694,60 @@ namespace platf {
|
||||
return saddr_v6;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Write the source IP address into the first ancillary control message.
|
||||
*
|
||||
* @param msg Socket message with control storage sized for packet information.
|
||||
* @param source_address Local IP address to use for outgoing packets.
|
||||
* @return Bytes occupied by the packet-information control message.
|
||||
*/
|
||||
static socklen_t populate_source_pktinfo(msghdr &msg, const boost::asio::ip::address &source_address) {
|
||||
socklen_t cmbuflen = 0;
|
||||
auto pktinfo_cm = CMSG_FIRSTHDR(&msg);
|
||||
if (source_address.is_v6()) {
|
||||
struct in6_pktinfo pktInfo;
|
||||
|
||||
struct sockaddr_in6 saddr_v6 = to_sockaddr(source_address.to_v6(), 0);
|
||||
pktInfo.ipi6_addr = saddr_v6.sin6_addr;
|
||||
pktInfo.ipi6_ifindex = 0;
|
||||
|
||||
cmbuflen += CMSG_SPACE(sizeof(pktInfo));
|
||||
|
||||
pktinfo_cm->cmsg_level = IPPROTO_IPV6;
|
||||
pktinfo_cm->cmsg_type = IPV6_PKTINFO;
|
||||
pktinfo_cm->cmsg_len = CMSG_LEN(sizeof(pktInfo));
|
||||
memcpy(CMSG_DATA(pktinfo_cm), &pktInfo, sizeof(pktInfo));
|
||||
} else {
|
||||
#ifdef IP_PKTINFO
|
||||
struct in_pktinfo pktInfo;
|
||||
|
||||
struct sockaddr_in saddr_v4 = to_sockaddr(source_address.to_v4(), 0);
|
||||
pktInfo.ipi_spec_dst = saddr_v4.sin_addr;
|
||||
pktInfo.ipi_ifindex = 0;
|
||||
|
||||
cmbuflen += CMSG_SPACE(sizeof(pktInfo));
|
||||
|
||||
pktinfo_cm->cmsg_level = IPPROTO_IP;
|
||||
pktinfo_cm->cmsg_type = IP_PKTINFO;
|
||||
pktinfo_cm->cmsg_len = CMSG_LEN(sizeof(pktInfo));
|
||||
memcpy(CMSG_DATA(pktinfo_cm), &pktInfo, sizeof(pktInfo));
|
||||
#elif defined(IP_SENDSRCADDR)
|
||||
// FreeBSD uses IP_SENDSRCADDR with struct in_addr instead of IP_PKTINFO
|
||||
struct sockaddr_in saddr_v4 = to_sockaddr(source_address.to_v4(), 0);
|
||||
struct in_addr src_addr = saddr_v4.sin_addr;
|
||||
|
||||
cmbuflen += CMSG_SPACE(sizeof(src_addr));
|
||||
|
||||
pktinfo_cm->cmsg_level = IPPROTO_IP;
|
||||
pktinfo_cm->cmsg_type = IP_SENDSRCADDR;
|
||||
pktinfo_cm->cmsg_len = CMSG_LEN(sizeof(src_addr));
|
||||
memcpy(CMSG_DATA(pktinfo_cm), &src_addr, sizeof(src_addr));
|
||||
#endif
|
||||
}
|
||||
|
||||
return cmbuflen;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send multiple fixed-size UDP payload blocks using the platform backend.
|
||||
*/
|
||||
@@ -718,62 +772,20 @@ namespace platf {
|
||||
|
||||
union {
|
||||
#ifdef IP_PKTINFO
|
||||
char buf[CMSG_SPACE(sizeof(uint16_t)) + std::max(CMSG_SPACE(sizeof(struct in_pktinfo)), CMSG_SPACE(sizeof(struct in6_pktinfo)))];
|
||||
std::array<char, CMSG_SPACE(sizeof(uint16_t)) + std::max(CMSG_SPACE(sizeof(struct in_pktinfo)), CMSG_SPACE(sizeof(struct in6_pktinfo)))> buf;
|
||||
#elif defined(IP_SENDSRCADDR)
|
||||
// FreeBSD uses IP_SENDSRCADDR with struct in_addr instead of IP_PKTINFO with struct in_pktinfo
|
||||
char buf[CMSG_SPACE(sizeof(uint16_t)) + std::max(CMSG_SPACE(sizeof(struct in_addr)), CMSG_SPACE(sizeof(struct in6_pktinfo)))];
|
||||
std::array<char, CMSG_SPACE(sizeof(uint16_t)) + std::max(CMSG_SPACE(sizeof(struct in_addr)), CMSG_SPACE(sizeof(struct in6_pktinfo)))> buf;
|
||||
#endif
|
||||
struct cmsghdr alignment;
|
||||
} cmbuf = {}; // Must be zeroed for CMSG_NXTHDR()
|
||||
|
||||
socklen_t cmbuflen = 0;
|
||||
|
||||
msg.msg_control = cmbuf.buf;
|
||||
msg.msg_controllen = sizeof(cmbuf.buf);
|
||||
msg.msg_control = cmbuf.buf.data();
|
||||
msg.msg_controllen = cmbuf.buf.size();
|
||||
|
||||
// The PKTINFO option will always be first, then we will conditionally
|
||||
// append the UDP_SEGMENT option next if applicable.
|
||||
auto pktinfo_cm = CMSG_FIRSTHDR(&msg);
|
||||
if (send_info.source_address.is_v6()) {
|
||||
struct in6_pktinfo pktInfo;
|
||||
|
||||
struct sockaddr_in6 saddr_v6 = to_sockaddr(send_info.source_address.to_v6(), 0);
|
||||
pktInfo.ipi6_addr = saddr_v6.sin6_addr;
|
||||
pktInfo.ipi6_ifindex = 0;
|
||||
|
||||
cmbuflen += CMSG_SPACE(sizeof(pktInfo));
|
||||
|
||||
pktinfo_cm->cmsg_level = IPPROTO_IPV6;
|
||||
pktinfo_cm->cmsg_type = IPV6_PKTINFO;
|
||||
pktinfo_cm->cmsg_len = CMSG_LEN(sizeof(pktInfo));
|
||||
memcpy(CMSG_DATA(pktinfo_cm), &pktInfo, sizeof(pktInfo));
|
||||
} else {
|
||||
#ifdef IP_PKTINFO
|
||||
struct in_pktinfo pktInfo;
|
||||
|
||||
struct sockaddr_in saddr_v4 = to_sockaddr(send_info.source_address.to_v4(), 0);
|
||||
pktInfo.ipi_spec_dst = saddr_v4.sin_addr;
|
||||
pktInfo.ipi_ifindex = 0;
|
||||
|
||||
cmbuflen += CMSG_SPACE(sizeof(pktInfo));
|
||||
|
||||
pktinfo_cm->cmsg_level = IPPROTO_IP;
|
||||
pktinfo_cm->cmsg_type = IP_PKTINFO;
|
||||
pktinfo_cm->cmsg_len = CMSG_LEN(sizeof(pktInfo));
|
||||
memcpy(CMSG_DATA(pktinfo_cm), &pktInfo, sizeof(pktInfo));
|
||||
#elif defined(IP_SENDSRCADDR)
|
||||
// FreeBSD uses IP_SENDSRCADDR with struct in_addr instead of IP_PKTINFO
|
||||
struct sockaddr_in saddr_v4 = to_sockaddr(send_info.source_address.to_v4(), 0);
|
||||
struct in_addr src_addr = saddr_v4.sin_addr;
|
||||
|
||||
cmbuflen += CMSG_SPACE(sizeof(src_addr));
|
||||
|
||||
pktinfo_cm->cmsg_level = IPPROTO_IP;
|
||||
pktinfo_cm->cmsg_type = IP_SENDSRCADDR;
|
||||
pktinfo_cm->cmsg_len = CMSG_LEN(sizeof(src_addr));
|
||||
memcpy(CMSG_DATA(pktinfo_cm), &src_addr, sizeof(src_addr));
|
||||
#endif
|
||||
}
|
||||
auto cmbuflen = populate_source_pktinfo(msg, send_info.source_address);
|
||||
|
||||
auto const max_iovs_per_msg = send_info.payload_buffers.size() + (send_info.headers ? 1 : 0);
|
||||
|
||||
@@ -782,7 +794,7 @@ namespace platf {
|
||||
// UDP GSO on Linux currently only supports sending 64K or 64 segments at a time
|
||||
size_t seg_index = 0;
|
||||
const size_t seg_max = 65536 / 1500;
|
||||
struct iovec iovs[(send_info.headers ? std::min(seg_max, send_info.block_count) : 1) * max_iovs_per_msg];
|
||||
std::vector<iovec> iovs((send_info.headers ? std::min(seg_max, send_info.block_count) : 1) * max_iovs_per_msg);
|
||||
auto msg_size = send_info.header_size + send_info.payload_size;
|
||||
while (seg_index < send_info.block_count) {
|
||||
int iovlen = 0;
|
||||
@@ -811,7 +823,7 @@ namespace platf {
|
||||
}
|
||||
}
|
||||
|
||||
msg.msg_iov = iovs;
|
||||
msg.msg_iov = iovs.data();
|
||||
msg.msg_iovlen = iovlen;
|
||||
|
||||
// We should not use GSO if the data is <= one full block size
|
||||
@@ -819,7 +831,7 @@ namespace platf {
|
||||
msg.msg_controllen = cmbuflen + CMSG_SPACE(sizeof(uint16_t));
|
||||
|
||||
// Enable GSO to perform segmentation of our buffer for us
|
||||
auto cm = CMSG_NXTHDR(&msg, pktinfo_cm);
|
||||
auto cm = CMSG_NXTHDR(&msg, CMSG_FIRSTHDR(&msg));
|
||||
cm->cmsg_level = SOL_UDP;
|
||||
cm->cmsg_type = UDP_SEGMENT;
|
||||
cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
|
||||
@@ -885,7 +897,7 @@ namespace platf {
|
||||
|
||||
msgs[i].msg_hdr.msg_name = msg.msg_name;
|
||||
msgs[i].msg_hdr.msg_namelen = msg.msg_namelen;
|
||||
msgs[i].msg_hdr.msg_control = cmbuf.buf;
|
||||
msgs[i].msg_hdr.msg_control = cmbuf.buf.data();
|
||||
msgs[i].msg_hdr.msg_controllen = cmbuflen;
|
||||
msgs[i].msg_hdr.msg_flags = 0;
|
||||
}
|
||||
@@ -946,62 +958,20 @@ namespace platf {
|
||||
|
||||
union {
|
||||
#ifdef IP_PKTINFO
|
||||
char buf[std::max(CMSG_SPACE(sizeof(struct in_pktinfo)), CMSG_SPACE(sizeof(struct in6_pktinfo)))];
|
||||
std::array<char, std::max(CMSG_SPACE(sizeof(struct in_pktinfo)), CMSG_SPACE(sizeof(struct in6_pktinfo)))> buf;
|
||||
#elif defined(IP_SENDSRCADDR)
|
||||
// FreeBSD uses IP_SENDSRCADDR with struct in_addr instead of IP_PKTINFO with struct in_pktinfo
|
||||
char buf[std::max(CMSG_SPACE(sizeof(struct in_addr)), CMSG_SPACE(sizeof(struct in6_pktinfo)))];
|
||||
std::array<char, std::max(CMSG_SPACE(sizeof(struct in_addr)), CMSG_SPACE(sizeof(struct in6_pktinfo)))> buf;
|
||||
#endif
|
||||
struct cmsghdr alignment;
|
||||
} cmbuf;
|
||||
|
||||
socklen_t cmbuflen = 0;
|
||||
msg.msg_control = cmbuf.buf.data();
|
||||
msg.msg_controllen = cmbuf.buf.size();
|
||||
|
||||
msg.msg_control = cmbuf.buf;
|
||||
msg.msg_controllen = sizeof(cmbuf.buf);
|
||||
auto cmbuflen = populate_source_pktinfo(msg, send_info.source_address);
|
||||
|
||||
auto pktinfo_cm = CMSG_FIRSTHDR(&msg);
|
||||
if (send_info.source_address.is_v6()) {
|
||||
struct in6_pktinfo pktInfo;
|
||||
|
||||
struct sockaddr_in6 saddr_v6 = to_sockaddr(send_info.source_address.to_v6(), 0);
|
||||
pktInfo.ipi6_addr = saddr_v6.sin6_addr;
|
||||
pktInfo.ipi6_ifindex = 0;
|
||||
|
||||
cmbuflen += CMSG_SPACE(sizeof(pktInfo));
|
||||
|
||||
pktinfo_cm->cmsg_level = IPPROTO_IPV6;
|
||||
pktinfo_cm->cmsg_type = IPV6_PKTINFO;
|
||||
pktinfo_cm->cmsg_len = CMSG_LEN(sizeof(pktInfo));
|
||||
memcpy(CMSG_DATA(pktinfo_cm), &pktInfo, sizeof(pktInfo));
|
||||
} else {
|
||||
#ifdef IP_PKTINFO
|
||||
struct in_pktinfo pktInfo;
|
||||
|
||||
struct sockaddr_in saddr_v4 = to_sockaddr(send_info.source_address.to_v4(), 0);
|
||||
pktInfo.ipi_spec_dst = saddr_v4.sin_addr;
|
||||
pktInfo.ipi_ifindex = 0;
|
||||
|
||||
cmbuflen += CMSG_SPACE(sizeof(pktInfo));
|
||||
|
||||
pktinfo_cm->cmsg_level = IPPROTO_IP;
|
||||
pktinfo_cm->cmsg_type = IP_PKTINFO;
|
||||
pktinfo_cm->cmsg_len = CMSG_LEN(sizeof(pktInfo));
|
||||
memcpy(CMSG_DATA(pktinfo_cm), &pktInfo, sizeof(pktInfo));
|
||||
#elif defined(IP_SENDSRCADDR)
|
||||
// FreeBSD uses IP_SENDSRCADDR with struct in_addr instead of IP_PKTINFO
|
||||
struct sockaddr_in saddr_v4 = to_sockaddr(send_info.source_address.to_v4(), 0);
|
||||
struct in_addr src_addr = saddr_v4.sin_addr;
|
||||
|
||||
cmbuflen += CMSG_SPACE(sizeof(src_addr));
|
||||
|
||||
pktinfo_cm->cmsg_level = IPPROTO_IP;
|
||||
pktinfo_cm->cmsg_type = IP_SENDSRCADDR;
|
||||
pktinfo_cm->cmsg_len = CMSG_LEN(sizeof(src_addr));
|
||||
memcpy(CMSG_DATA(pktinfo_cm), &src_addr, sizeof(src_addr));
|
||||
#endif
|
||||
}
|
||||
|
||||
struct iovec iovs[2];
|
||||
std::array<iovec, 2> iovs;
|
||||
int iovlen = 0;
|
||||
if (send_info.header) {
|
||||
iovs[iovlen].iov_base = (void *) send_info.header;
|
||||
@@ -1012,7 +982,7 @@ namespace platf {
|
||||
iovs[iovlen].iov_len = send_info.payload_size;
|
||||
iovlen++;
|
||||
|
||||
msg.msg_iov = iovs;
|
||||
msg.msg_iov = iovs.data();
|
||||
msg.msg_iovlen = iovlen;
|
||||
|
||||
msg.msg_controllen = cmbuflen;
|
||||
|
||||
Reference in new issue
Block a user