From: Charles-Henri Bruyand Date: Thu, 15 Mar 2018 11:09:47 +0000 (+0100) Subject: recursor: add min-udp-source-port, max-udp-source-port and avoid-udp-source-port... X-Git-Tag: dnsdist-1.3.0~28^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bf6f28ca0b84bbb41cec5ace276cb2ce938d81a1;p=thirdparty%2Fpdns.git recursor: add min-udp-source-port, max-udp-source-port and avoid-udp-source-port variables to tune the range of ports we use --- diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 60fc1ccab7..602d637e6d 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -60,6 +60,7 @@ #include #include #include +#include #ifdef MALLOC_TRACE #include "malloctrace.hh" #endif @@ -160,6 +161,9 @@ static bool g_gettagNeedsEDNSOptions{false}; static time_t g_statisticsInterval; static bool g_useIncomingECS; std::atomic g_maxCacheEntries, g_maxPacketCacheEntries; +static boost::container::flat_set s_avoidUdpSourcePorts; +static uint16_t s_minUdpSourcePort; +static uint16_t s_maxUdpSourcePort; RecursorControlChannel s_rcc; // only active in thread 0 RecursorStats g_stats; @@ -516,8 +520,12 @@ public: if(tries==1) // fall back to kernel 'random' port = 0; - else - port = 1025 + dns_random(64510); + else { + do { + port = s_minUdpSourcePort + dns_random(s_maxUdpSourcePort - s_minUdpSourcePort + 1); + } + while (s_avoidUdpSourcePorts.count(port)); + } sin=getQueryLocalAddress(family, port); // does htons for us @@ -3199,6 +3207,30 @@ static int serviceMain(int argc, char*argv[]) g_snmpAgent->run(); } + int port = ::arg().asNum("min-udp-source-port"); + if(port < 1025 || port > 65535){ + L< 65535 || port < s_minUdpSourcePort){ + L< parts {}; + stringtok(parts, ::arg()["avoid-udp-source-port"], ", "); + for (const auto &part : parts) + { + port = std::stoi(part); + if(port < 1025 || port > 65535){ + L<