From: Miod Vallat Date: Mon, 4 Aug 2025 07:24:34 +0000 (+0200) Subject: Change the waitFor*Data routines to take msec instead of usec. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=03392d17b4bf766816a7471cc42a459a6d940441;p=thirdparty%2Fpdns.git Change the waitFor*Data routines to take msec instead of usec. They wrap poll() which has millisecond timeout resolution anyway. Signed-off-by: Miod Vallat --- diff --git a/modules/pipebackend/coprocess.cc b/modules/pipebackend/coprocess.cc index 88be02209..d1ea2451d 100644 --- a/modules/pipebackend/coprocess.cc +++ b/modules/pipebackend/coprocess.cc @@ -186,7 +186,7 @@ void CoProcess::receive(string& received) } if (saved == EAGAIN) { if (d_timeout) { - int ret = waitForData(d_fd2[0], 0, d_timeout * 1000); + int ret = waitForData(d_fd2[0], 0, d_timeout); if (ret < 0) throw PDNSException("Error waiting on data from coprocess: " + string(strerror(saved))); if (!ret) diff --git a/modules/remotebackend/httpconnector.cc b/modules/remotebackend/httpconnector.cc index 457fdd57f..7903d7a7f 100644 --- a/modules/remotebackend/httpconnector.cc +++ b/modules/remotebackend/httpconnector.cc @@ -356,7 +356,7 @@ int HTTPConnector::send_message(const Json& input) if (this->d_socket != nullptr) { fd = this->d_socket->getHandle(); // there should be no data waiting - if (waitForRWData(fd, true, 0, 1000) < 1) { + if (waitForRWData(fd, true, 0, 1) < 1) { try { d_socket->writenWithTimeout(out.str().c_str(), out.str().size(), timeout); rv = 1; diff --git a/modules/remotebackend/pipeconnector.cc b/modules/remotebackend/pipeconnector.cc index 21055c073..f67884c00 100644 --- a/modules/remotebackend/pipeconnector.cc +++ b/modules/remotebackend/pipeconnector.cc @@ -173,7 +173,7 @@ int PipeConnector::recv_message(Json& output) while (1) { receive.clear(); if (d_timeout != 0) { - int ret = waitForData(fileno(d_fp.get()), 0, d_timeout * 1000); + int ret = waitForData(fileno(d_fp.get()), 0, d_timeout); if (ret < 0) { throw PDNSException("Error waiting on data from coprocess: " + stringerror()); } diff --git a/modules/remotebackend/unixconnector.cc b/modules/remotebackend/unixconnector.cc index 5ac137d4c..e6501ec4a 100644 --- a/modules/remotebackend/unixconnector.cc +++ b/modules/remotebackend/unixconnector.cc @@ -77,7 +77,7 @@ int UnixsocketConnector::recv_message(Json& output) s_output = ""; while ((t.tv_sec - t0.tv_sec) * 1000 + (t.tv_usec - t0.tv_usec) / 1000 < this->timeout) { - int avail = waitForData(this->fd, 0, this->timeout * 500); // use half the timeout as poll timeout + int avail = waitForData(this->fd, 0, this->timeout / 2); // use half the timeout as poll timeout if (avail < 0) { // poll error return -1; } diff --git a/pdns/comfun.cc b/pdns/comfun.cc index 01a1fff2c..3aafd8142 100644 --- a/pdns/comfun.cc +++ b/pdns/comfun.cc @@ -110,7 +110,7 @@ struct SendReceive bool receive(Identifier& id, DNSResult& dr) { - if(waitForData(d_socket, 0, 500000) > 0) { + if(waitForData(d_socket, 0, 500) > 0) { char buf[512]; ComboAddress from; from.sin4.sin_family = AF_INET; @@ -244,7 +244,7 @@ struct SendReceiveRes bool receive(Identifier& id, RESResult& dr) { - if(waitForData(d_socket, 0, 500000) > 0) { + if(waitForData(d_socket, 0, 500) > 0) { char buf[512]; ComboAddress from; from.sin4.sin_family = AF_INET; diff --git a/pdns/dnsbulktest.cc b/pdns/dnsbulktest.cc index 9c9abd063..49794828b 100644 --- a/pdns/dnsbulktest.cc +++ b/pdns/dnsbulktest.cc @@ -141,7 +141,7 @@ struct SendReceive bool receive(Identifier& iden, DNSResult& dnsResult) { - if (waitForData(d_socket.getHandle(), 0, 500000) > 0) { + if (waitForData(d_socket.getHandle(), 0, 500) > 0) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init): no need to initialize the buffer std::array buf; diff --git a/pdns/dnsdistdist/delaypipe.cc b/pdns/dnsdistdist/delaypipe.cc index fdd828862..6b66467c3 100644 --- a/pdns/dnsdistdist/delaypipe.cc +++ b/pdns/dnsdistdist/delaypipe.cc @@ -52,7 +52,7 @@ template int ObjectPipe::readTimeout(T* t, double msec) { while (true) { - int ret = waitForData(d_receiver.getDescriptor(), 0, 1000 * msec); + int ret = waitForData(d_receiver.getDescriptor(), 0, msec); if (ret < 0) { if (errno == EINTR) { continue; diff --git a/pdns/dnsdistdist/dnsdist-actions-factory.cc b/pdns/dnsdistdist/dnsdist-actions-factory.cc index 07e8ea9b1..db27eb063 100644 --- a/pdns/dnsdistdist/dnsdist-actions-factory.cc +++ b/pdns/dnsdistdist/dnsdist-actions-factory.cc @@ -272,7 +272,7 @@ void TeeAction::worker() ssize_t res = 0; const dnsheader_aligned dnsheader(packet.data()); for (;;) { - res = waitForData(d_socket.getHandle(), 0, 250000); + res = waitForData(d_socket.getHandle(), 0, 250); if (d_pleaseQuit) { break; } diff --git a/pdns/dnsreplay.cc b/pdns/dnsreplay.cc index f7290fa93..50325eff3 100644 --- a/pdns/dnsreplay.cc +++ b/pdns/dnsreplay.cc @@ -393,7 +393,7 @@ try { PacketBuffer packet; ComboAddress remote; - int res=waitForData(s_socket->getHandle(), g_timeoutMsec/1000, 1000*(g_timeoutMsec%1000)); + int res=waitForData(s_socket->getHandle(), g_timeoutMsec/1000, g_timeoutMsec%1000); if(res < 0 || res==0) return; diff --git a/pdns/dnstcpbench.cc b/pdns/dnstcpbench.cc index 996dbbd9c..f0b907cb3 100644 --- a/pdns/dnstcpbench.cc +++ b/pdns/dnstcpbench.cc @@ -92,7 +92,7 @@ try udpsock.sendTo(string(packet.begin(), packet.end()), g_dest); ComboAddress origin; - res = waitForData(udpsock.getHandle(), 0, 1000 * g_timeoutMsec); + res = waitForData(udpsock.getHandle(), 0, static_cast(g_timeoutMsec)); if(res < 0) throw NetworkError("Error waiting for response"); if(!res) { @@ -129,7 +129,7 @@ try sock.writen(tcppacket); - res = waitForData(sock.getHandle(), 0, 1000 * g_timeoutMsec); + res = waitForData(sock.getHandle(), 0, static_cast(g_timeoutMsec)); if(res < 0) throw NetworkError("Error waiting for response"); if(!res) { diff --git a/pdns/inflighter.cc b/pdns/inflighter.cc index 6bdc6d74b..efc30b3cf 100644 --- a/pdns/inflighter.cc +++ b/pdns/inflighter.cc @@ -230,7 +230,7 @@ struct SendReceive bool receive(Identifier& id, int& i) { - if(waitForData(d_socket, 0, 500000) > 0) { + if(waitForData(d_socket, 0, 500) > 0) { char buf[512]; int len = recv(d_socket, buf, sizeof(buf), 0); diff --git a/pdns/iputils.cc b/pdns/iputils.cc index 6beebddad..e578f0110 100644 --- a/pdns/iputils.cc +++ b/pdns/iputils.cc @@ -77,7 +77,7 @@ int SConnectWithTimeout(int sockfd, const ComboAddress& remote, const struct tim /* we wait until the connection has been established */ bool error = false; bool disconnected = false; - int res = waitForRWData(sockfd, false, static_cast(timeout.tv_sec), static_cast(timeout.tv_usec), &error, &disconnected); + int res = waitForRWData(sockfd, false, timeout, &error, &disconnected); if (res == 1) { if (error) { savederrno = 0; diff --git a/pdns/misc.cc b/pdns/misc.cc index f8ccfa82d..e3cf96097 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -143,7 +143,7 @@ size_t readn2WithTimeout(int fd, void* buffer, size_t len, const struct timeval& else { if (errno == EAGAIN) { struct timeval w = ((totalTimeout.tv_sec == 0 && totalTimeout.tv_usec == 0) || idleTimeout <= remainingTime) ? idleTimeout : remainingTime; - int res = waitForData(fd, w.tv_sec, w.tv_usec); + int res = waitForData(fd, w); if (res > 0) { /* there is data available */ } @@ -187,7 +187,7 @@ size_t writen2WithTimeout(int fd, const void * buffer, size_t len, const struct throw runtime_error("EOF while writing message"); else { if (errno == EAGAIN) { - int res = waitForRWData(fd, false, timeout.tv_sec, timeout.tv_usec); + int res = waitForRWData(fd, false, timeout); if (res > 0) { /* there is room available */ } @@ -309,12 +309,17 @@ string nowTime() } // returns -1 in case if error, 0 if no data is available, 1 if there is. In the first two cases, errno is set -int waitForData(int fileDesc, int seconds, int useconds) +int waitForData(int fileDesc, int seconds, int mseconds) { - return waitForRWData(fileDesc, true, seconds, useconds); + return waitForRWData(fileDesc, true, seconds, mseconds); } -int waitForRWData(int fileDesc, bool waitForRead, int seconds, int useconds, bool* error, bool* disconnected) +int waitForData(int fileDesc, struct timeval timeout) +{ + return waitForRWData(fileDesc, true, timeout); +} + +int waitForRWData(int fileDesc, bool waitForRead, int seconds, int mseconds, bool* error, bool* disconnected) { struct pollfd pfd{}; memset(&pfd, 0, sizeof(pfd)); @@ -327,7 +332,7 @@ int waitForRWData(int fileDesc, bool waitForRead, int seconds, int useconds, boo pfd.events = POLLOUT; } - int ret = poll(&pfd, 1, seconds * 1000 + useconds/1000); + int ret = poll(&pfd, 1, seconds * 1000 + mseconds); if (ret > 0) { if ((error != nullptr) && (pfd.revents & POLLERR) != 0) { *error = true; @@ -340,8 +345,13 @@ int waitForRWData(int fileDesc, bool waitForRead, int seconds, int useconds, boo return ret; } +int waitForRWData(int fileDesc, bool waitForRead, struct timeval timeout, bool* error, bool* disconnected) +{ + return waitForRWData(fileDesc, waitForRead, static_cast(timeout.tv_sec), static_cast(timeout.tv_usec / 1000), error, disconnected); +} + // returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set -int waitForMultiData(const set& fds, const int seconds, const int useconds, int* fdOut) { +int waitForMultiData(const set& fds, const int seconds, const int mseconds, int* fdOut) { set realFDs; for (const auto& fd : fds) { if (fd >= 0 && realFDs.count(fd) == 0) { @@ -360,7 +370,7 @@ int waitForMultiData(const set& fds, const int seconds, const int useconds, int ret; if(seconds >= 0) - ret = poll(pfds.data(), realFDs.size(), seconds * 1000 + useconds/1000); + ret = poll(pfds.data(), realFDs.size(), seconds * 1000 + mseconds); else ret = poll(pfds.data(), realFDs.size(), -1); if(ret <= 0) @@ -379,7 +389,7 @@ int waitForMultiData(const set& fds, const int seconds, const int useconds, } // returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set -int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int* fdPtr) +int waitFor2Data(int fd1, int fd2, int seconds, int mseconds, int* fdPtr) { std::array pfds{}; memset(pfds.data(), 0, pfds.size() * sizeof(struct pollfd)); @@ -392,7 +402,7 @@ int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int* fdPtr) int ret{}; if (seconds >= 0) { - ret = poll(pfds.data(), nsocks, seconds * 1000 + useconds / 1000); + ret = poll(pfds.data(), nsocks, seconds * 1000 + mseconds); } else { ret = poll(pfds.data(), nsocks, -1); diff --git a/pdns/misc.hh b/pdns/misc.hh index 3c193f150..7752b5f26 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -106,10 +106,12 @@ void stripLine(string &line); std::optional getHostname(); std::string getCarbonHostName(); string urlEncode(const string &text); -int waitForData(int fileDesc, int seconds, int useconds = 0); -int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int* fd); -int waitForMultiData(const set& fds, const int seconds, const int useconds, int* fd); -int waitForRWData(int fileDesc, bool waitForRead, int seconds, int useconds, bool* error = nullptr, bool* disconnected = nullptr); +int waitForData(int fileDesc, int seconds, int mseconds = 0); +int waitForData(int fileDesc, struct timeval timeout); +int waitFor2Data(int fd1, int fd2, int seconds, int mseconds, int* fd); +int waitForMultiData(const set& fds, const int seconds, const int mseconds, int* fd); +int waitForRWData(int fileDesc, bool waitForRead, int seconds, int mseconds, bool* error = nullptr, bool* disconnected = nullptr); +int waitForRWData(int fileDesc, bool waitForRead, struct timeval timeout, bool* error = nullptr, bool* disconnected = nullptr); bool getTSIGHashEnum(const DNSName& algoName, TSIGHashEnum& algoEnum); DNSName getTSIGAlgoName(TSIGHashEnum& algoEnum); diff --git a/pdns/resolver.cc b/pdns/resolver.cc index 530a562ac..cb2d87484 100644 --- a/pdns/resolver.cc +++ b/pdns/resolver.cc @@ -313,7 +313,7 @@ int Resolver::resolve(const ComboAddress& to, const DNSName &domain, int type, R try { int sock = -1; int id = sendResolve(to, local, domain, type, &sock); - int err=waitForData(sock, 0, 3000000); + int err=waitForData(sock, 3, 0); if(!err) { throw ResolverException("Timeout waiting for answer"); diff --git a/pdns/sdig.cc b/pdns/sdig.cc index 887dafb38..90a23e54d 100644 --- a/pdns/sdig.cc +++ b/pdns/sdig.cc @@ -526,7 +526,7 @@ try { Socket sock(dest.sin4.sin_family, SOCK_DGRAM); question = proxyheader + question; sock.sendTo(question, dest); - int result = waitForData(sock.getHandle(), timeout.tv_sec, timeout.tv_usec); + int result = waitForData(sock.getHandle(), timeout); if (result < 0) throw std::runtime_error("Error waiting for data: " + stringerror()); if (!result) diff --git a/pdns/tcpiohandler.cc b/pdns/tcpiohandler.cc index 660b85f02..6c1549f56 100644 --- a/pdns/tcpiohandler.cc +++ b/pdns/tcpiohandler.cc @@ -286,7 +286,7 @@ public: { auto state = convertIORequestToIOState(res); if (state == IOState::NeedRead) { - res = waitForData(d_socket, timeout.tv_sec, timeout.tv_usec); + res = waitForData(d_socket, timeout); if (res == 0) { throw std::runtime_error("Timeout while reading from TLS connection"); } @@ -295,7 +295,7 @@ public: } } else if (state == IOState::NeedWrite) { - res = waitForRWData(d_socket, false, timeout.tv_sec, timeout.tv_usec); + res = waitForRWData(d_socket, false, timeout); if (res == 0) { throw std::runtime_error("Timeout while writing to TLS connection"); } @@ -1308,13 +1308,13 @@ public: return; } else if (state == IOState::NeedRead) { - int result = waitForData(d_socket, remainingTime.tv_sec, remainingTime.tv_usec); + int result = waitForData(d_socket, remainingTime); if (result <= 0) { throw std::runtime_error("Error reading from TLS connection: " + std::to_string(result)); } } else if (state == IOState::NeedWrite) { - int result = waitForRWData(d_socket, false, remainingTime.tv_sec, remainingTime.tv_usec); + int result = waitForRWData(d_socket, false, remainingTime); if (result <= 0) { throw std::runtime_error("Error reading from TLS connection: " + std::to_string(result)); } @@ -1489,7 +1489,7 @@ public: throw std::runtime_error("Fatal error reading from TLS connection: " + std::string(gnutls_strerror(res))); } else if (res == GNUTLS_E_AGAIN) { - int result = waitForData(d_socket, readTimeout.tv_sec, readTimeout.tv_usec); + int result = waitForData(d_socket, readTimeout); if (result <= 0) { throw std::runtime_error("Error while waiting to read from TLS connection: " + std::to_string(result)); } @@ -1532,7 +1532,7 @@ public: throw std::runtime_error("Fatal error writing to TLS connection: " + std::string(gnutls_strerror(res))); } else if (res == GNUTLS_E_AGAIN) { - int result = waitForRWData(d_socket, false, writeTimeout.tv_sec, writeTimeout.tv_usec); + int result = waitForRWData(d_socket, false, writeTimeout); if (result <= 0) { throw std::runtime_error("Error waiting to write to TLS connection: " + std::to_string(result)); }