}
}
-static bool handleTCPReadResult(int fd, ssize_t bytes)
-{
- if (bytes == 0) {
- /* EOF */
- terminateTCPConnection(fd);
- return false;
- }
- else if (bytes < 0) {
- if (errno != EAGAIN && errno != EWOULDBLOCK) {
- terminateTCPConnection(fd);
- return false;
- }
- }
-
- return true;
-}
-
static bool checkForCacheHit(bool qnameParsed, unsigned int tag, const string& data,
DNSName& qname, uint16_t& qtype, uint16_t& qclass,
const struct timeval& now,
}
}
-/* A helper class that by default closes the incoming TCP connection on destruct */
+/*
+ * A helper class that by default closes the incoming TCP connection on destruct
+ * If you want to keep the connection aliave, call keep() on the guard object
+ */
class RunningTCPQuestionGuard {
public:
- RunningTCPQuestionGuard(int fd) {
+ RunningTCPQuestionGuard(int fd)
+ {
d_fd = fd;
}
- ~RunningTCPQuestionGuard() {
+ ~RunningTCPQuestionGuard()
+ {
if (d_fd != -1) {
terminateTCPConnection(d_fd);
d_fd = -1;
}
}
- void keep() {
+ void keep()
+ {
d_fd = -1;
}
+ bool handleTCPReadResult(int fd, ssize_t bytes)
+ {
+ if (bytes == 0) {
+ /* EOF */
+ return false;
+ }
+ else if (bytes < 0) {
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ return false;
+ }
+ }
+ keep();
+ return true;
+ }
+
private:
int d_fd{-1};
};
if (conn->state == TCPConnection::PROXYPROTOCOLHEADER) {
ssize_t bytes = recv(conn->getFD(), &conn->data.at(conn->proxyProtocolGot), conn->proxyProtocolNeed, 0);
if (bytes <= 0) {
- handleTCPReadResult(fd, bytes);
- tcpGuard.keep();
+ tcpGuard.handleTCPReadResult(fd, bytes);
return;
}
conn->state=TCPConnection::GETQUESTION;
}
if (bytes <= 0) {
- handleTCPReadResult(fd, bytes);
- tcpGuard.keep();
+ tcpGuard.handleTCPReadResult(fd, bytes);
return;
}
}
conn->bytesread=0;
}
if (bytes <= 0) {
- if (!handleTCPReadResult(fd, bytes)) {
+ if (!tcpGuard.handleTCPReadResult(fd, bytes)) {
if(g_logCommonErrors) {
g_log<<Logger::Error<<"TCP client "<< conn->d_remote.toStringWithPort() <<" disconnected after first byte"<<endl;
}
}
- tcpGuard.keep();
return;
}
}
if(conn->state==TCPConnection::GETQUESTION) {
ssize_t bytes=recv(conn->getFD(), &conn->data[conn->bytesread], conn->qlen - conn->bytesread, 0);
if (bytes <= 0) {
- if (!handleTCPReadResult(fd, bytes)) {
+ if (!tcpGuard.handleTCPReadResult(fd, bytes)) {
if(g_logCommonErrors) {
g_log<<Logger::Error<<"TCP client "<< conn->d_remote.toStringWithPort() <<" disconnected while reading question body"<<endl;
}
- }
- tcpGuard.keep();
+ }
return;
}
else if (bytes > std::numeric_limits<std::uint16_t>::max()) {
} // good query
} // read full query
} // reading query
+
+ // more to come
+ tcpGuard.keep();
}
static bool expectProxyProtocol(const ComboAddress& from)