From: Remi Gacogne Date: Mon, 22 Apr 2024 08:26:51 +0000 (+0200) Subject: dnsdist: Fix clang-tidy warnings X-Git-Tag: rec-5.1.0-alpha1~26^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=77c1af635077857d9de975d198631b3e54913e1d;p=thirdparty%2Fpdns.git dnsdist: Fix clang-tidy warnings --- diff --git a/pdns/dnsdistdist/dnsdist.cc b/pdns/dnsdistdist/dnsdist.cc index 2cc044f54c..80ee94299d 100644 --- a/pdns/dnsdistdist/dnsdist.cc +++ b/pdns/dnsdistdist/dnsdist.cc @@ -157,14 +157,25 @@ static constexpr size_t s_maxUDPResponsePacketSize{4096U}; static size_t const s_initialUDPPacketBufferSize = s_maxUDPResponsePacketSize + DNSCRYPT_MAX_RESPONSE_PADDING_AND_MAC_SIZE; static_assert(s_initialUDPPacketBufferSize <= UINT16_MAX, "Packet size should fit in a uint16_t"); -static ssize_t sendfromto(int sock, const PacketBuffer& buffer, const ComboAddress& from, const ComboAddress& dest) +static void sendfromto(int sock, const PacketBuffer& buffer, const ComboAddress& from, const ComboAddress& dest) { const int flags = 0; if (from.sin4.sin_family == 0) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return sendto(sock, buffer.data(), buffer.size(), flags, reinterpret_cast(&dest), dest.getSocklen()); + auto ret = sendto(sock, buffer.data(), buffer.size(), flags, reinterpret_cast(&dest), dest.getSocklen()); + if (ret == -1) { + int error = errno; + vinfolog("Error sending UDP response to %s: %s", dest.toStringWithPort(), stringerror(error)); + } + return; + } + + try { + sendMsgWithOptions(sock, buffer.data(), buffer.size(), &dest, &from, 0, 0); + } + catch (const std::exception& exp) { + vinfolog("Error sending UDP response from %s to %s: %s", from.toStringWithPort(), dest.toStringWithPort(), exp.what()); } - return sendMsgWithOptions(sock, buffer.data(), buffer.size(), &dest, &from, 0, 0); } static void truncateTC(PacketBuffer& packet, size_t maximumSize, unsigned int qnameWireLength) @@ -203,13 +214,9 @@ struct DelayedPacket PacketBuffer packet; ComboAddress destination; ComboAddress origDest; - void operator()() + void operator()() const { - ssize_t res = sendfromto(fd, packet, origDest, destination); - if (res == -1) { - int err = errno; - vinfolog("Error sending delayed response to %s: %s", destination.toStringWithPort(), stringerror(err)); - } + sendfromto(fd, packet, origDest, destination); } }; @@ -646,12 +653,8 @@ bool sendUDPResponse(int origFD, const PacketBuffer& response, const int delayMs return true; } #endif /* DISABLE_DELAY_PIPE */ - ssize_t res = sendfromto(origFD, response, origDest, origRemote); - if (res == -1) { - int err = errno; - vinfolog("Error sending response to %s: %s", origRemote.toStringWithPort(), stringerror(err)); - } - + // NOLINTNEXTLINE(readability-suspicious-call-argument) + sendfromto(origFD, response, origDest, origRemote); return true; } diff --git a/pdns/dnsdistdist/doq-common.cc b/pdns/dnsdistdist/doq-common.cc index f8ac8d871b..bb79ddc218 100644 --- a/pdns/dnsdistdist/doq-common.cc +++ b/pdns/dnsdistdist/doq-common.cc @@ -126,15 +126,25 @@ std::optional validateToken(const PacketBuffer& token, const Combo } } -static ssize_t sendFromTo(Socket& sock, const ComboAddress& peer, const ComboAddress& local, PacketBuffer& buffer) +static void sendFromTo(Socket& sock, const ComboAddress& peer, const ComboAddress& local, PacketBuffer& buffer) { const int flags = 0; if (local.sin4.sin_family == 0) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return sendto(sock.getHandle(), buffer.data(), buffer.size(), flags, reinterpret_cast(&peer), peer.getSocklen()); + auto ret = sendto(sock.getHandle(), buffer.data(), buffer.size(), flags, reinterpret_cast(&peer), peer.getSocklen()); + if (ret < 0) { + auto error = errno; + vinfolog("Error while sending QUIC datagram of size %d to %s: %s", buffer.size(), peer.toStringWithPort(), stringerror(error)); + } + return; } - return sendMsgWithOptions(sock.getHandle(), buffer.data(), buffer.size(), &peer, &local, 0, 0); + try { + sendMsgWithOptions(sock.getHandle(), buffer.data(), buffer.size(), &peer, &local, 0, 0); + } + catch (const std::exception& exp) { + vinfolog("Error while sending QUIC datagram of size %d from %s to %s: %s", buffer.size(), local.toStringWithPort(), peer.toStringWithPort(), exp.what()); + } } void handleStatelessRetry(Socket& sock, const PacketBuffer& clientConnID, const PacketBuffer& serverConnID, const ComboAddress& peer, const ComboAddress& localAddr, uint32_t version, PacketBuffer& buffer) diff --git a/pdns/iputils.cc b/pdns/iputils.cc index 955b9392c6..bd1204e3ac 100644 --- a/pdns/iputils.cc +++ b/pdns/iputils.cc @@ -366,7 +366,7 @@ void ComboAddress::truncate(unsigned int bits) noexcept *place &= (~((1<(const_cast(dest)); msgh.msg_namelen = dest->getSocklen(); @@ -388,8 +388,8 @@ size_t sendMsgWithOptions(int fd, const void* buffer, size_t len, const ComboAdd msgh.msg_flags = 0; - if (local && local->sin4.sin_family != 0) { - addCMsgSrcAddr(&msgh, &cbuf, local, localItf); + if (local != nullptr && local->sin4.sin_family != 0) { + addCMsgSrcAddr(&msgh, &cbuf, local, static_cast(localItf)); } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast): it's the API @@ -407,15 +407,15 @@ size_t sendMsgWithOptions(int fd, const void* buffer, size_t len, const ComboAdd do { #ifdef MSG_FASTOPEN - if (flags & MSG_FASTOPEN && firstTry == false) { + if ((flags & MSG_FASTOPEN) != 0 && !firstTry) { flags &= ~MSG_FASTOPEN; } #endif /* MSG_FASTOPEN */ - ssize_t res = sendmsg(fd, &msgh, flags); + ssize_t res = sendmsg(socketDesc, &msgh, flags); if (res > 0) { - size_t written = static_cast(res); + auto written = static_cast(res); sent += written; if (sent == len) { @@ -427,7 +427,7 @@ size_t sendMsgWithOptions(int fd, const void* buffer, size_t len, const ComboAdd firstTry = false; #endif iov.iov_len -= written; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): it's the API + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-pointer-arithmetic): it's the API iov.iov_base = reinterpret_cast(reinterpret_cast(iov.iov_base) + written); } else if (res == 0) { @@ -438,14 +438,12 @@ size_t sendMsgWithOptions(int fd, const void* buffer, size_t len, const ComboAdd if (err == EINTR) { continue; } - else if (err == EAGAIN || err == EWOULDBLOCK || err == EINPROGRESS || err == ENOTCONN) { + if (err == EAGAIN || err == EWOULDBLOCK || err == EINPROGRESS || err == ENOTCONN) { /* EINPROGRESS might happen with non blocking socket, especially with TCP Fast Open */ return sent; } - else { - unixDie("failed in sendMsgWithOptions"); - } + unixDie("failed in sendMsgWithOptions"); } } while (true); diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 8a3bd502a7..2a318f7327 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -1736,7 +1736,7 @@ bool HarvestDestinationAddress(const struct msghdr* msgh, ComboAddress* destinat bool HarvestTimestamp(struct msghdr* msgh, struct timeval* tv); void fillMSGHdr(struct msghdr* msgh, struct iovec* iov, cmsgbuf_aligned* cbuf, size_t cbufsize, char* data, size_t datalen, ComboAddress* addr); int sendOnNBSocket(int fd, const struct msghdr *msgh); -size_t sendMsgWithOptions(int fd, const void* buffer, size_t len, const ComboAddress* dest, const ComboAddress* local, unsigned int localItf, int flags); +size_t sendMsgWithOptions(int socketDesc, const void* buffer, size_t len, const ComboAddress* dest, const ComboAddress* local, unsigned int localItf, int flags); /* requires a non-blocking, connected TCP socket */ bool isTCPSocketUsable(int sock);