From 6714b6ac8585cb645f0eaaaf433a4e2ff60c4f49 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Wed, 15 May 2019 11:06:42 +0200 Subject: [PATCH] dnsdist: Remove unused code since we moved to an event-based logic Unused code will only get worse with the time, let's not keep it around. --- pdns/dnsdist-tcp.cc | 2 +- pdns/iputils.cc | 146 +++---------------------------------------- pdns/iputils.hh | 4 +- pdns/tcpiohandler.hh | 14 ----- 4 files changed, 10 insertions(+), 156 deletions(-) diff --git a/pdns/dnsdist-tcp.cc b/pdns/dnsdist-tcp.cc index 3a3ecbdd2e..6c437e270f 100644 --- a/pdns/dnsdist-tcp.cc +++ b/pdns/dnsdist-tcp.cc @@ -905,7 +905,7 @@ static void handleDownstreamIO(std::shared_ptr& stat } #endif /* MSG_FASTOPEN */ - size_t sent = sendMsgWithTimeout(fd, reinterpret_cast(&state->d_buffer.at(state->d_currentPos)), state->d_buffer.size() - state->d_currentPos, 0, &state->d_ds->remote, &state->d_ds->sourceAddr, state->d_ds->sourceItf, 0, socketFlags); + size_t sent = sendMsgWithOptions(fd, reinterpret_cast(&state->d_buffer.at(state->d_currentPos)), state->d_buffer.size() - state->d_currentPos, &state->d_ds->remote, &state->d_ds->sourceAddr, state->d_ds->sourceItf, socketFlags); if (sent == state->d_buffer.size()) { /* request sent ! */ state->d_downstreamConnection->incQueries(); diff --git a/pdns/iputils.cc b/pdns/iputils.cc index 474b8b484b..f58d0e31b2 100644 --- a/pdns/iputils.cc +++ b/pdns/iputils.cc @@ -290,14 +290,8 @@ void ComboAddress::truncate(unsigned int bits) noexcept *place &= (~((1<(reinterpret_cast(iov.iov_base) + written); written = 0; } + else if (res == 0) { + return res; + } else if (res == -1) { if (errno == EINTR) { continue; @@ -360,150 +358,20 @@ size_t sendMsgWithTimeout(int fd, const char* buffer, size_t len, int idleTimeou else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS || errno == ENOTCONN) { /* EINPROGRESS might happen with non blocking socket, especially with TCP Fast Open */ - if (totalTimeout <= 0 && idleTimeout <= 0) { - return sent; - } - - if (firstTry) { - int res = waitForRWData(fd, false, (totalTimeout == 0 || idleTimeout <= remainingTime) ? idleTimeout : remainingTime, 0); - if (res > 0) { - /* there is room available */ - firstTry = false; - } - else if (res == 0) { - throw runtime_error("Timeout while waiting to write data"); - } else { - throw runtime_error("Error while waiting for room to write data"); - } - } - else { - throw runtime_error("Timeout while waiting to write data"); - } + return sent; } else { unixDie("failed in sendMsgWithTimeout"); } } - if (totalTimeout) { - time_t now = time(nullptr); - int elapsed = now - start; - if (elapsed >= remainingTime) { - throw runtime_error("Timeout while sending data"); - } - start = now; - remainingTime -= elapsed; - } } - while (firstTry); + while (true); return 0; } template class NetmaskTree; -bool sendSizeAndMsgWithTimeout(int sock, uint16_t bufferLen, const char* buffer, int idleTimeout, const ComboAddress* dest, const ComboAddress* local, unsigned int localItf, int totalTimeout, int flags) -{ - uint16_t size = htons(bufferLen); - char cbuf[256]; - struct msghdr msgh; - struct iovec iov[2]; - int remainingTime = totalTimeout; - time_t start = 0; - if (totalTimeout) { - start = time(NULL); - } - - /* Set up iov and msgh structures. */ - memset(&msgh, 0, sizeof(struct msghdr)); - msgh.msg_control = nullptr; - msgh.msg_controllen = 0; - if (dest) { - msgh.msg_name = reinterpret_cast(const_cast(dest)); - msgh.msg_namelen = dest->getSocklen(); - } - else { - msgh.msg_name = nullptr; - msgh.msg_namelen = 0; - } - - msgh.msg_flags = 0; - - if (localItf != 0 && local) { - addCMsgSrcAddr(&msgh, cbuf, local, localItf); - } - - iov[0].iov_base = &size; - iov[0].iov_len = sizeof(size); - iov[1].iov_base = reinterpret_cast(const_cast(buffer)); - iov[1].iov_len = bufferLen; - - size_t pos = 0; - size_t sent = 0; - size_t nbElements = sizeof(iov)/sizeof(*iov); - while (true) { - msgh.msg_iov = &iov[pos]; - msgh.msg_iovlen = nbElements - pos; - - ssize_t res = sendmsg(sock, &msgh, flags); - if (res > 0) { - size_t written = static_cast(res); - sent += written; - - if (sent == (sizeof(size) + bufferLen)) { - return true; - } - /* partial write, we need to keep only the (parts of) elements - that have not been written. - */ - do { - if (written < iov[pos].iov_len) { - iov[pos].iov_len -= written; - iov[pos].iov_base = reinterpret_cast(reinterpret_cast(iov[pos].iov_base) + written); - written = 0; - } - else { - written -= iov[pos].iov_len; - iov[pos].iov_len = 0; - pos++; - } - } - while (written > 0 && pos < nbElements); - } - else if (res == -1) { - if (errno == EINTR) { - continue; - } - else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS) { - /* EINPROGRESS might happen with non blocking socket, - especially with TCP Fast Open */ - int ret = waitForRWData(sock, false, (totalTimeout == 0 || idleTimeout <= remainingTime) ? idleTimeout : remainingTime, 0); - if (ret > 0) { - /* there is room available */ - } - else if (ret == 0) { - throw runtime_error("Timeout while waiting to send data"); - } else { - throw runtime_error("Error while waiting for room to send data"); - } - } - else { - unixDie("failed in sendSizeAndMsgWithTimeout"); - } - } - if (totalTimeout) { - time_t now = time(NULL); - int elapsed = now - start; - if (elapsed >= remainingTime) { - throw runtime_error("Timeout while sending data"); - } - start = now; - remainingTime -= elapsed; - } - } - - return false; -} - /* requires a non-blocking socket. On Linux, we could use MSG_DONTWAIT on a blocking socket but this is not portable. diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 457b2b5971..18d6e88194 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -1059,8 +1059,8 @@ bool HarvestDestinationAddress(const struct msghdr* msgh, ComboAddress* destinat bool HarvestTimestamp(struct msghdr* msgh, struct timeval* tv); void fillMSGHdr(struct msghdr* msgh, struct iovec* iov, char* cbuf, size_t cbufsize, char* data, size_t datalen, ComboAddress* addr); ssize_t sendfromto(int sock, const char* data, size_t len, int flags, const ComboAddress& from, const ComboAddress& to); -size_t sendMsgWithTimeout(int fd, const char* buffer, size_t len, int idleTimeout, const ComboAddress* dest, const ComboAddress* local, unsigned int localItf, int totalTimeout, int flags); -bool sendSizeAndMsgWithTimeout(int sock, uint16_t bufferLen, const char* buffer, int idleTimeout, const ComboAddress* dest, const ComboAddress* local, unsigned int localItf, int totalTimeout, int flags); +size_t sendMsgWithOptions(int fd, const char* 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); diff --git a/pdns/tcpiohandler.hh b/pdns/tcpiohandler.hh index ec31ecff8d..061ab884a4 100644 --- a/pdns/tcpiohandler.hh +++ b/pdns/tcpiohandler.hh @@ -275,20 +275,6 @@ public: } } - bool writeSizeAndMsg(const void* buffer, size_t bufferSize, unsigned int writeTimeout) - { - if (d_conn) { - uint16_t size = htons(bufferSize); - if (d_conn->write(&size, sizeof(size), writeTimeout) != sizeof(size)) { - return false; - } - return (d_conn->write(buffer, bufferSize, writeTimeout) == bufferSize); - } - else { - return sendSizeAndMsgWithTimeout(d_socket, bufferSize, static_cast(buffer), writeTimeout, nullptr, nullptr, 0, 0, 0); - } - } - private: std::unique_ptr d_conn{nullptr}; int d_socket{-1}; -- 2.39.2