}
DNSProxy::~DNSProxy() {
- if (d_sock>-1) closesocket(d_sock);
+ if (d_sock>-1) {
+ try {
+ closesocket(d_sock);
+ }
+ catch(const PDNSException& e) {
+ }
+ }
+
d_sock=-1;
}
if(connect(*fd, (struct sockaddr*)(&toaddr), toaddr.getSocklen()) < 0) {
int err = errno;
// returnSocket(*fd);
- closesocket(*fd);
+ try {
+ closesocket(*fd);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing UDP socket after connect() failed: "<<e.reason<<endl;
+ }
+
if(err==ENETUNREACH) // Seth "My Interfaces Are Like A Yo Yo" Arnold special
return -2;
return -1;
catch(FDMultiplexerException& e) {
// we sometimes return a socket that has not yet been assigned to t_fdm
}
- closesocket(*i);
+ try {
+ closesocket(*i);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing returned UDP socket: "<<e.reason<<endl;
+ }
d_socks.erase(i++);
--d_numsocks;
TCPConnection::~TCPConnection()
{
- if(closesocket(d_fd) < 0)
- unixDie("closing socket for TCPConnection");
+ try {
+ if(closesocket(d_fd) < 0)
+ L<<Logger::Error<<"Error closing socket for TCPConnection"<<endl;
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing TCPConnection socket: "<<e.reason<<endl;
+ }
+
if(t_tcpClientCounts->count(d_remote) && !(*t_tcpClientCounts)[d_remote]--)
t_tcpClientCounts->erase(d_remote);
--s_currentConnections;
if(newsock>=0) {
if(MT->numProcesses() > g_maxMThreads) {
g_stats.overCapacityDrops++;
- closesocket(newsock);
+ try {
+ closesocket(newsock);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing TCP socket after an over capacity drop: "<<e.reason<<endl;
+ }
return;
}
L<<Logger::Error<<"["<<MT->getTid()<<"] dropping TCP query from "<<addr.toString()<<", address not matched by allow-from"<<endl;
g_stats.unauthorizedTCP++;
- closesocket(newsock);
+ try {
+ closesocket(newsock);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing TCP socket after an ACL drop: "<<e.reason<<endl;
+ }
return;
}
if(g_maxTCPPerClient && t_tcpClientCounts->count(addr) && (*t_tcpClientCounts)[addr] >= g_maxTCPPerClient) {
g_stats.tcpClientOverflow++;
- closesocket(newsock); // don't call TCPConnection::closeAndCleanup here - did not enter it in the counts yet!
+ try {
+ closesocket(newsock); // don't call TCPConnection::closeAndCleanup here - did not enter it in the counts yet!
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing TCP socket after an overflow drop: "<<e.reason<<endl;
+ }
return;
}
int err;
if((err=::connect(d_sock,(struct sockaddr*)&d_remote, d_remote.getSocklen()))<0 && errno!=EINPROGRESS) {
- closesocket(d_sock);
- d_sock=-1;
+ try {
+ closesocket(d_sock);
+ }
+ catch(const PDNSException& e) {
+ d_sock=-1;
+ throw ResolverException("Error closing AXFR socket after connect() failed: "+e.reason);
+ }
+
throw ResolverException("connect: "+stringerror());
}
err=waitForRWData(d_sock, false, 10, 0); // wait for writeability
if(!err) {
- closesocket(d_sock); // timeout
+ try {
+ closesocket(d_sock); // timeout
+ }
+ catch(const PDNSException& e) {
+ d_sock=-1;
+ throw ResolverException("Error closing AXFR socket after timeout: "+e.reason);
+ }
+
d_sock=-1;
errno=ETIMEDOUT;
if( connect(sock, (struct sockaddr*)&remote, remote.getSocklen()) < 0 ) {
L<<Logger::Error<<msgPrefix<<"Failed to connect to "<<remote.toStringWithPort()<<": "<<stringerror()<<endl;
- closesocket(sock);
+ try {
+ closesocket(sock);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing master forwarding socket after connect() failed: "<<e.reason<<endl;
+ }
continue;
}
buffer.append(forwardPacket.getString());
if(write(sock, buffer.c_str(), buffer.length()) < 0) {
L<<Logger::Error<<msgPrefix<<"Unable to forward update message to "<<remote.toStringWithPort()<<", error:"<<stringerror()<<endl;
- closesocket(sock);
+ try {
+ closesocket(sock);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing master forwarding socket after write() failed: "<<e.reason<<endl;
+ }
continue;
}
int res = waitForData(sock, 10, 0);
if (!res) {
L<<Logger::Error<<msgPrefix<<"Timeout waiting for reply from master at "<<remote.toStringWithPort()<<endl;
- closesocket(sock);
+ try {
+ closesocket(sock);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing master forwarding socket after a timeout occured: "<<e.reason<<endl;
+ }
continue;
}
if (res < 0) {
L<<Logger::Error<<msgPrefix<<"Error waiting for answer from master at "<<remote.toStringWithPort()<<", error:"<<stringerror()<<endl;
- closesocket(sock);
+ try {
+ closesocket(sock);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing master forwarding socket after an error occured: "<<e.reason<<endl;
+ }
continue;
}
recvRes = recv(sock, &lenBuf, sizeof(lenBuf), 0);
if (recvRes < 0) {
L<<Logger::Error<<msgPrefix<<"Could not receive data (length) from master at "<<remote.toStringWithPort()<<", error:"<<stringerror()<<endl;
- closesocket(sock);
+ try {
+ closesocket(sock);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing master forwarding socket after recv() failed: "<<e.reason<<endl;
+ }
continue;
}
int packetLen = lenBuf[0]*256+lenBuf[1];
recvRes = recv(sock, &buf, packetLen, 0);
if (recvRes < 0) {
L<<Logger::Error<<msgPrefix<<"Could not receive data (dnspacket) from master at "<<remote.toStringWithPort()<<", error:"<<stringerror()<<endl;
- closesocket(sock);
+ try {
+ closesocket(sock);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing master forwarding socket after recv() failed: "<<e.reason<<endl;
+ }
continue;
}
- closesocket(sock);
+ try {
+ closesocket(sock);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing master forwarding socket: "<<e.reason<<endl;
+ }
try {
MOADNSParser mdp(false, buf, recvRes);
~Socket()
{
- closesocket(d_socket);
+ try {
+ closesocket(d_socket);
+ }
+ catch(const PDNSException& e) {
+ }
+
delete[] d_buffer;
}
if(getpeername(fd, (struct sockaddr *)&remote, &remotelen) < 0) {
L<<Logger::Warning<<"Received question from socket which had no remote address, dropping ("<<stringerror()<<")"<<endl;
d_connectionroom_sem->post();
- closesocket(fd);
+ try {
+ closesocket(fd);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing TCP socket: "<<e.reason<<endl;
+ }
return 0;
}
L << Logger::Error << "TCP Connection Thread caught unknown exception." << endl;
}
d_connectionroom_sem->post();
- closesocket(fd);
+
+ try {
+ closesocket(fd);
+ }
+ catch(const PDNSException& e) {
+ L<<Logger::Error<<"Error closing TCP socket: "<<e.reason<<endl;
+ }
return 0;
}
typedef int sock_t;
typedef ::socklen_t socklen_t;
- //! Closes a socket.
- static int closesocket( sock_t socket );
-
//! Connect with timeout
// Returns:
// > 0 on success