They wrap poll() which has millisecond timeout resolution anyway.
Signed-off-by: Miod Vallat <miod.vallat@powerdns.com>
}
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)
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;
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());
}
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;
}
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;
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;
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<char, 512> buf;
int ObjectPipe<T>::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;
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;
}
{
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;
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<int>(g_timeoutMsec));
if(res < 0)
throw NetworkError("Error waiting for response");
if(!res) {
sock.writen(tcppacket);
- res = waitForData(sock.getHandle(), 0, 1000 * g_timeoutMsec);
+ res = waitForData(sock.getHandle(), 0, static_cast<int>(g_timeoutMsec));
if(res < 0)
throw NetworkError("Error waiting for response");
if(!res) {
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);
/* we wait until the connection has been established */
bool error = false;
bool disconnected = false;
- int res = waitForRWData(sockfd, false, static_cast<int>(timeout.tv_sec), static_cast<int>(timeout.tv_usec), &error, &disconnected);
+ int res = waitForRWData(sockfd, false, timeout, &error, &disconnected);
if (res == 1) {
if (error) {
savederrno = 0;
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 */
}
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 */
}
}
// 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));
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;
return ret;
}
+int waitForRWData(int fileDesc, bool waitForRead, struct timeval timeout, bool* error, bool* disconnected)
+{
+ return waitForRWData(fileDesc, waitForRead, static_cast<int>(timeout.tv_sec), static_cast<int>(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<int>& fds, const int seconds, const int useconds, int* fdOut) {
+int waitForMultiData(const set<int>& fds, const int seconds, const int mseconds, int* fdOut) {
set<int> realFDs;
for (const auto& fd : fds) {
if (fd >= 0 && realFDs.count(fd) == 0) {
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)
}
// 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<pollfd,2> pfds{};
memset(pfds.data(), 0, pfds.size() * sizeof(struct pollfd));
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);
std::optional<string> 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<int>& 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<int>& 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);
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");
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)
{
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");
}
}
}
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");
}
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));
}
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));
}
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));
}