From: Remi Gacogne Date: Mon, 24 Jun 2019 08:13:53 +0000 (+0200) Subject: dnsdist: Fix short IOs over TCP X-Git-Tag: dnsdist-1.4.0-rc1~88^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d78c148430b8e11e8458f38f61eb89c24e0abee;p=thirdparty%2Fpdns.git dnsdist: Fix short IOs over TCP --- diff --git a/pdns/dnsdist-tcp.cc b/pdns/dnsdist-tcp.cc index 8419011661..231a28c281 100644 --- a/pdns/dnsdist-tcp.cc +++ b/pdns/dnsdist-tcp.cc @@ -1042,7 +1042,7 @@ static void handleIO(std::shared_ptr& state, struct } if (state->d_state == IncomingTCPConnectionState::State::readingQuerySize) { - iostate = state->d_handler.tryRead(state->d_buffer, state->d_currentPos, sizeof(uint16_t) - state->d_currentPos); + iostate = state->d_handler.tryRead(state->d_buffer, state->d_currentPos, sizeof(uint16_t)); if (iostate == IOState::Done) { state->d_state = IncomingTCPConnectionState::State::readingQuery; state->d_querySizeReadTime = now; diff --git a/pdns/tcpiohandler.hh b/pdns/tcpiohandler.hh index dd82281a7a..33fc402a7f 100644 --- a/pdns/tcpiohandler.hh +++ b/pdns/tcpiohandler.hh @@ -192,7 +192,7 @@ public: } } - /* Tries to read exactly toRead bytes into the buffer, starting at position pos. + /* Tries to read exactly toRead - pos bytes into the buffer, starting at position pos. Updates pos everytime a successful read occurs, throws an std::runtime_error in case of IO error, return Done when toRead bytes have been read, needRead or needWrite if the IO operation @@ -208,9 +208,8 @@ public: return d_conn->tryRead(buffer, pos, toRead); } - size_t got = 0; do { - ssize_t res = ::read(d_socket, reinterpret_cast(&buffer.at(pos)), toRead - got); + ssize_t res = ::read(d_socket, reinterpret_cast(&buffer.at(pos)), toRead - pos); if (res == 0) { throw runtime_error("EOF while reading message"); } @@ -224,14 +223,13 @@ public: } pos += static_cast(res); - got += static_cast(res); } - while (got < toRead); + while (pos < toRead); return IOState::Done; } - /* Tries to write exactly toWrite bytes from the buffer, starting at position pos. + /* Tries to write exactly toWrite - pos bytes from the buffer, starting at position pos. Updates pos everytime a successful write occurs, throws an std::runtime_error in case of IO error, return Done when toWrite bytes have been written, needRead or needWrite if the IO operation @@ -243,9 +241,8 @@ public: return d_conn->tryWrite(buffer, pos, toWrite); } - size_t sent = 0; do { - ssize_t res = ::write(d_socket, reinterpret_cast(&buffer.at(pos)), toWrite - sent); + ssize_t res = ::write(d_socket, reinterpret_cast(&buffer.at(pos)), toWrite - pos); if (res == 0) { throw runtime_error("EOF while sending message"); } @@ -259,9 +256,8 @@ public: } pos += static_cast(res); - sent += static_cast(res); } - while (sent < toWrite); + while (pos < toWrite); return IOState::Done; }