From 29bb743c7c733974f132c35865ab43e8c0eed5c9 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Tue, 5 Mar 2019 16:30:22 +0100 Subject: [PATCH] Set IP(V6)_RECVERR socket option to get notified of more than just port unreachable errors on Linux. See https://sourceware.org/bugzilla/show_bug.cgi?id=24047 --- pdns/misc.cc | 16 ++++++++++++++++ pdns/misc.hh | 1 + pdns/pdns_recursor.cc | 1 + 3 files changed, 18 insertions(+) diff --git a/pdns/misc.cc b/pdns/misc.cc index b9863ed966..08c42251cf 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -1101,6 +1101,22 @@ bool isNonBlocking(int sock) return flags & O_NONBLOCK; } +bool setReceiveSocketErrors(int sock, int af) +{ +#ifdef __linux__ + int tmp = 1, ret; + if (af == AF_INET) { + ret = setsockopt(sock, IPPROTO_IP, IP_RECVERR, &tmp, sizeof(tmp)); + } else { + ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVERR, &tmp, sizeof(tmp)); + } + if (ret < 0) { + throw PDNSException(string("Setsockopt failed: ") + strerror(errno)); + } +#endif + return true; +} + // Closes a socket. int closesocket( int socket ) { diff --git a/pdns/misc.hh b/pdns/misc.hh index f57a010bc6..9adfe35dc4 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -534,6 +534,7 @@ bool setNonBlocking( int sock ); bool setTCPNoDelay(int sock); bool setReuseAddr(int sock); bool isNonBlocking(int sock); +bool setReceiveSocketErrors(int sock, int af); int closesocket(int fd); bool setCloseOnExec(int sock); uint64_t udpErrorStats(const std::string& str); diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 1a278125bf..dcbbf88579 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -623,6 +623,7 @@ public: if(!tries) throw PDNSException("Resolver binding to local query client socket on "+sin.toString()+": "+stringerror()); + setReceiveSocketErrors(ret, family); setNonBlocking(ret); return ret; } -- 2.47.2