DnsServer.cs 233 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2023 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using DnsServerCore.ApplicationCommon;
  16. using DnsServerCore.Dns.Applications;
  17. using DnsServerCore.Dns.ResourceRecords;
  18. using DnsServerCore.Dns.Trees;
  19. using DnsServerCore.Dns.ZoneManagers;
  20. using DnsServerCore.Dns.Zones;
  21. using Microsoft.AspNetCore.Builder;
  22. using Microsoft.AspNetCore.Hosting;
  23. using Microsoft.AspNetCore.Http;
  24. using Microsoft.AspNetCore.Server.Kestrel.Core;
  25. using Microsoft.AspNetCore.StaticFiles;
  26. using Microsoft.Extensions.FileProviders;
  27. using Microsoft.Extensions.Logging;
  28. using System;
  29. using System.Collections.Concurrent;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Net;
  33. using System.Net.Quic;
  34. using System.Net.Security;
  35. using System.Net.Sockets;
  36. using System.Runtime.ExceptionServices;
  37. using System.Security.Cryptography.X509Certificates;
  38. using System.Threading;
  39. using System.Threading.Tasks;
  40. using TechnitiumLibrary;
  41. using TechnitiumLibrary.Net;
  42. using TechnitiumLibrary.Net.Dns;
  43. using TechnitiumLibrary.Net.Dns.ClientConnection;
  44. using TechnitiumLibrary.Net.Dns.EDnsOptions;
  45. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  46. using TechnitiumLibrary.Net.Proxy;
  47. using TechnitiumLibrary.Net.ProxyProtocol;
  48. namespace DnsServerCore.Dns
  49. {
  50. #pragma warning disable CA2252 // This API requires opting into preview features
  51. #pragma warning disable CA1416 // Validate platform compatibility
  52. public enum DnsServerRecursion : byte
  53. {
  54. Deny = 0,
  55. Allow = 1,
  56. AllowOnlyForPrivateNetworks = 2,
  57. UseSpecifiedNetworks = 3
  58. }
  59. public enum DnsServerBlockingType : byte
  60. {
  61. AnyAddress = 0,
  62. NxDomain = 1,
  63. CustomAddress = 2
  64. }
  65. public sealed class DnsServer : IAsyncDisposable, IDisposable, IDnsClient
  66. {
  67. #region enum
  68. enum ServiceState
  69. {
  70. Stopped = 0,
  71. Starting = 1,
  72. Running = 2,
  73. Stopping = 3
  74. }
  75. #endregion
  76. #region variables
  77. internal const int MAX_CNAME_HOPS = 16;
  78. const int SERVE_STALE_WAIT_TIME = 1800;
  79. static readonly IPEndPoint IPENDPOINT_ANY_0 = new IPEndPoint(IPAddress.Any, 0);
  80. static readonly IReadOnlyCollection<DnsARecordData> _aRecords = new DnsARecordData[] { new DnsARecordData(IPAddress.Any) };
  81. static readonly IReadOnlyCollection<DnsAAAARecordData> _aaaaRecords = new DnsAAAARecordData[] { new DnsAAAARecordData(IPAddress.IPv6Any) };
  82. static readonly List<SslApplicationProtocol> quicApplicationProtocols = new List<SslApplicationProtocol>() { new SslApplicationProtocol("doq") };
  83. string _serverDomain;
  84. readonly string _configFolder;
  85. readonly string _dohwwwFolder;
  86. IReadOnlyList<IPEndPoint> _localEndPoints;
  87. LogManager _log;
  88. NameServerAddress _thisServer;
  89. readonly List<Socket> _udpListeners = new List<Socket>();
  90. readonly List<Socket> _udpProxyListeners = new List<Socket>();
  91. readonly List<Socket> _tcpListeners = new List<Socket>();
  92. readonly List<Socket> _tcpProxyListeners = new List<Socket>();
  93. readonly List<Socket> _tlsListeners = new List<Socket>();
  94. readonly List<QuicListener> _quicListeners = new List<QuicListener>();
  95. WebApplication _dohWebService;
  96. readonly AuthZoneManager _authZoneManager;
  97. readonly AllowedZoneManager _allowedZoneManager;
  98. readonly BlockedZoneManager _blockedZoneManager;
  99. readonly BlockListZoneManager _blockListZoneManager;
  100. readonly CacheZoneManager _cacheZoneManager;
  101. readonly DnsApplicationManager _dnsApplicationManager;
  102. readonly ResolverDnsCache _dnsCache;
  103. readonly StatsManager _stats;
  104. bool _preferIPv6;
  105. ushort _udpPayloadSize = DnsDatagram.EDNS_DEFAULT_UDP_PAYLOAD_SIZE;
  106. bool _dnssecValidation = true;
  107. bool _eDnsClientSubnet;
  108. byte _eDnsClientSubnetIPv4PrefixLength = 24;
  109. byte _eDnsClientSubnetIPv6PrefixLength = 56;
  110. int _qpmLimitRequests = 0;
  111. int _qpmLimitErrors = 0;
  112. int _qpmLimitSampleMinutes = 5;
  113. int _qpmLimitIPv4PrefixLength = 24;
  114. int _qpmLimitIPv6PrefixLength = 56;
  115. int _clientTimeout = 4000;
  116. int _tcpSendTimeout = 10000;
  117. int _tcpReceiveTimeout = 10000;
  118. int _quicIdleTimeout = 60000;
  119. int _quicMaxInboundStreams = 100;
  120. int _listenBacklog = 100;
  121. bool _enableDnsOverUdpProxy;
  122. bool _enableDnsOverTcpProxy;
  123. bool _enableDnsOverHttp;
  124. bool _enableDnsOverTls;
  125. bool _enableDnsOverHttps;
  126. bool _enableDnsOverQuic;
  127. int _dnsOverUdpProxyPort = 538;
  128. int _dnsOverTcpProxyPort = 538;
  129. int _dnsOverHttpPort = 80;
  130. int _dnsOverTlsPort = 853;
  131. int _dnsOverHttpsPort = 443;
  132. int _dnsOverQuicPort = 853;
  133. X509Certificate2Collection _certificateCollection;
  134. SslServerAuthenticationOptions _sslServerAuthenticationOptions;
  135. SslServerAuthenticationOptions _quicSslServerAuthenticationOptions;
  136. IReadOnlyDictionary<string, TsigKey> _tsigKeys;
  137. DnsServerRecursion _recursion;
  138. IReadOnlyCollection<NetworkAddress> _recursionDeniedNetworks;
  139. IReadOnlyCollection<NetworkAddress> _recursionAllowedNetworks;
  140. bool _randomizeName;
  141. bool _qnameMinimization;
  142. bool _nsRevalidation;
  143. int _resolverRetries = 2;
  144. int _resolverTimeout = 2000;
  145. int _resolverMaxStackCount = 16;
  146. bool _serveStale = true;
  147. int _cachePrefetchEligibility = 2;
  148. int _cachePrefetchTrigger = 9;
  149. int _cachePrefetchSampleIntervalInMinutes = 5;
  150. int _cachePrefetchSampleEligibilityHitsPerHour = 30;
  151. bool _enableBlocking = true;
  152. bool _allowTxtBlockingReport = true;
  153. DnsServerBlockingType _blockingType = DnsServerBlockingType.NxDomain;
  154. IReadOnlyCollection<DnsARecordData> _customBlockingARecords = Array.Empty<DnsARecordData>();
  155. IReadOnlyCollection<DnsAAAARecordData> _customBlockingAAAARecords = Array.Empty<DnsAAAARecordData>();
  156. NetProxy _proxy;
  157. IReadOnlyList<NameServerAddress> _forwarders;
  158. int _forwarderRetries = 3;
  159. int _forwarderTimeout = 2000;
  160. int _forwarderConcurrency = 2;
  161. LogManager _queryLog;
  162. Timer _cachePrefetchSamplingTimer;
  163. readonly object _cachePrefetchSamplingTimerLock = new object();
  164. const int CACHE_PREFETCH_SAMPLING_TIMER_INITIAL_INTEVAL = 5000;
  165. Timer _cachePrefetchRefreshTimer;
  166. readonly object _cachePrefetchRefreshTimerLock = new object();
  167. const int CACHE_PREFETCH_REFRESH_TIMER_INITIAL_INTEVAL = 10000;
  168. DateTime _cachePrefetchSamplingTimerTriggersOn;
  169. IList<CacheRefreshSample> _cacheRefreshSampleList;
  170. Timer _cacheMaintenanceTimer;
  171. readonly object _cacheMaintenanceTimerLock = new object();
  172. const int CACHE_MAINTENANCE_TIMER_INITIAL_INTEVAL = 5 * 60 * 1000;
  173. const int CACHE_MAINTENANCE_TIMER_PERIODIC_INTERVAL = 5 * 60 * 1000;
  174. Timer _qpmLimitSamplingTimer;
  175. readonly object _qpmLimitSamplingTimerLock = new object();
  176. const int QPM_LIMIT_SAMPLING_TIMER_INTERVAL = 10000;
  177. IReadOnlyDictionary<IPAddress, long> _qpmLimitClientSubnetStats;
  178. IReadOnlyDictionary<IPAddress, long> _qpmLimitErrorClientSubnetStats;
  179. readonly IndependentTaskScheduler _queryTaskScheduler = new IndependentTaskScheduler();
  180. readonly IndependentTaskScheduler _resolverTaskScheduler = new IndependentTaskScheduler(ThreadPriority.AboveNormal);
  181. readonly ConcurrentDictionary<string, Task<RecursiveResolveResponse>> _resolverTasks = new ConcurrentDictionary<string, Task<RecursiveResolveResponse>>();
  182. volatile ServiceState _state = ServiceState.Stopped;
  183. #endregion
  184. #region constructor
  185. static DnsServer()
  186. {
  187. //set min threads since the default value is too small
  188. {
  189. ThreadPool.GetMinThreads(out int minWorker, out int minIOC);
  190. int minThreads = Environment.ProcessorCount * 16;
  191. if (minWorker < minThreads)
  192. minWorker = minThreads;
  193. if (minIOC < minThreads)
  194. minIOC = minThreads;
  195. ThreadPool.SetMinThreads(minWorker, minIOC);
  196. }
  197. }
  198. public DnsServer(string serverDomain, string configFolder, string dohwwwFolder, LogManager log = null)
  199. : this(serverDomain, configFolder, dohwwwFolder, new IPEndPoint[] { new IPEndPoint(IPAddress.Any, 53), new IPEndPoint(IPAddress.IPv6Any, 53) }, log)
  200. { }
  201. public DnsServer(string serverDomain, string configFolder, string dohwwwFolder, IPEndPoint localEndPoint, LogManager log = null)
  202. : this(serverDomain, configFolder, dohwwwFolder, new IPEndPoint[] { localEndPoint }, log)
  203. { }
  204. public DnsServer(string serverDomain, string configFolder, string dohwwwFolder, IReadOnlyList<IPEndPoint> localEndPoints, LogManager log = null)
  205. {
  206. _serverDomain = serverDomain;
  207. _configFolder = configFolder;
  208. _dohwwwFolder = dohwwwFolder;
  209. _localEndPoints = localEndPoints;
  210. _log = log;
  211. _authZoneManager = new AuthZoneManager(this);
  212. _allowedZoneManager = new AllowedZoneManager(this);
  213. _blockedZoneManager = new BlockedZoneManager(this);
  214. _blockListZoneManager = new BlockListZoneManager(this);
  215. _cacheZoneManager = new CacheZoneManager(this);
  216. _dnsApplicationManager = new DnsApplicationManager(this);
  217. _dnsCache = new ResolverDnsCache(_dnsApplicationManager, _authZoneManager, _cacheZoneManager, _log, false);
  218. //init stats
  219. _stats = new StatsManager(this);
  220. //init udp socket pool async for port randomization
  221. ThreadPool.QueueUserWorkItem(delegate (object state)
  222. {
  223. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  224. UdpClientConnection.CreateSocketPool(_preferIPv6);
  225. });
  226. }
  227. #endregion
  228. #region IDisposable
  229. bool _disposed;
  230. public async ValueTask DisposeAsync()
  231. {
  232. if (_disposed)
  233. return;
  234. await StopAsync();
  235. _authZoneManager?.Dispose();
  236. _dnsApplicationManager?.Dispose();
  237. _stats?.Dispose();
  238. _disposed = true;
  239. }
  240. public void Dispose()
  241. {
  242. DisposeAsync().Sync();
  243. }
  244. #endregion
  245. #region private
  246. private async Task ReadUdpRequestAsync(Socket udpListener, DnsTransportProtocol protocol)
  247. {
  248. byte[] recvBuffer;
  249. if (protocol == DnsTransportProtocol.UdpProxy)
  250. recvBuffer = new byte[DnsDatagram.EDNS_MAX_UDP_PAYLOAD_SIZE + 256];
  251. else
  252. recvBuffer = new byte[DnsDatagram.EDNS_MAX_UDP_PAYLOAD_SIZE];
  253. using MemoryStream recvBufferStream = new MemoryStream(recvBuffer);
  254. try
  255. {
  256. EndPoint epAny;
  257. switch (udpListener.AddressFamily)
  258. {
  259. case AddressFamily.InterNetwork:
  260. epAny = new IPEndPoint(IPAddress.Any, 0);
  261. break;
  262. case AddressFamily.InterNetworkV6:
  263. epAny = new IPEndPoint(IPAddress.IPv6Any, 0);
  264. break;
  265. default:
  266. throw new NotSupportedException("AddressFamily not supported.");
  267. }
  268. SocketReceiveFromResult result;
  269. while (true)
  270. {
  271. recvBufferStream.SetLength(DnsDatagram.EDNS_MAX_UDP_PAYLOAD_SIZE); //resetting length before using buffer
  272. try
  273. {
  274. result = await udpListener.ReceiveFromAsync(recvBuffer, SocketFlags.None, epAny);
  275. }
  276. catch (SocketException ex)
  277. {
  278. switch (ex.SocketErrorCode)
  279. {
  280. case SocketError.ConnectionReset:
  281. case SocketError.HostUnreachable:
  282. case SocketError.MessageSize:
  283. case SocketError.NetworkReset:
  284. result = default;
  285. break;
  286. default:
  287. throw;
  288. }
  289. }
  290. if (result.ReceivedBytes > 0)
  291. {
  292. if (result.RemoteEndPoint is not IPEndPoint remoteEP)
  293. continue;
  294. try
  295. {
  296. recvBufferStream.Position = 0;
  297. recvBufferStream.SetLength(result.ReceivedBytes);
  298. IPEndPoint returnEP = remoteEP;
  299. if (protocol == DnsTransportProtocol.UdpProxy)
  300. {
  301. if (!NetUtilities.IsPrivateIP(remoteEP.Address))
  302. {
  303. //intentionally blocking public IP addresses from using DNS-over-UDP-PROXY
  304. //this feature is intended to be used with a reverse proxy or load balancer on private network
  305. continue;
  306. }
  307. ProxyProtocolStream proxyStream = await ProxyProtocolStream.CreateAsServerAsync(recvBufferStream);
  308. remoteEP = new IPEndPoint(proxyStream.SourceAddress, proxyStream.SourcePort);
  309. recvBufferStream.Position = proxyStream.DataOffset;
  310. }
  311. if (IsQpmLimitCrossed(remoteEP.Address))
  312. continue;
  313. DnsDatagram request = DnsDatagram.ReadFrom(recvBufferStream);
  314. _ = ProcessUdpRequestAsync(udpListener, remoteEP, returnEP, protocol, request);
  315. }
  316. catch (EndOfStreamException)
  317. {
  318. //ignore incomplete udp datagrams
  319. }
  320. catch (Exception ex)
  321. {
  322. _log?.Write(remoteEP, protocol, ex);
  323. }
  324. }
  325. }
  326. }
  327. catch (ObjectDisposedException)
  328. {
  329. //server stopping
  330. }
  331. catch (SocketException ex)
  332. {
  333. switch (ex.SocketErrorCode)
  334. {
  335. case SocketError.OperationAborted:
  336. case SocketError.Interrupted:
  337. break; //server stopping
  338. default:
  339. if ((_state == ServiceState.Stopping) || (_state == ServiceState.Stopped))
  340. return; //server stopping
  341. _log?.Write(ex);
  342. break;
  343. }
  344. }
  345. catch (Exception ex)
  346. {
  347. if ((_state == ServiceState.Stopping) || (_state == ServiceState.Stopped))
  348. return; //server stopping
  349. _log?.Write(ex);
  350. }
  351. }
  352. private async Task ProcessUdpRequestAsync(Socket udpListener, IPEndPoint remoteEP, IPEndPoint returnEP, DnsTransportProtocol protocol, DnsDatagram request)
  353. {
  354. try
  355. {
  356. DnsDatagram response = await PreProcessQueryAsync(request, remoteEP, protocol, IsRecursionAllowed(remoteEP.Address));
  357. if (response is null)
  358. return; //drop request
  359. //send response
  360. byte[] sendBuffer;
  361. if (request.EDNS is null)
  362. sendBuffer = new byte[512];
  363. else if (request.EDNS.UdpPayloadSize > _udpPayloadSize)
  364. sendBuffer = new byte[_udpPayloadSize];
  365. else
  366. sendBuffer = new byte[request.EDNS.UdpPayloadSize];
  367. using (MemoryStream sendBufferStream = new MemoryStream(sendBuffer))
  368. {
  369. try
  370. {
  371. response.WriteTo(sendBufferStream);
  372. }
  373. catch (NotSupportedException)
  374. {
  375. if (response.IsSigned)
  376. {
  377. //rfc8945 section 5.3
  378. response = new DnsDatagram(response.Identifier, true, response.OPCODE, response.AuthoritativeAnswer, true, response.RecursionDesired, response.RecursionAvailable, response.AuthenticData, response.CheckingDisabled, DnsResponseCode.NoError, response.Question, null, null, new DnsResourceRecord[] { response.Additional[response.Additional.Count - 1] }, request.EDNS is null ? ushort.MinValue : _udpPayloadSize) { Tag = DnsServerResponseType.Authoritative };
  379. }
  380. else
  381. {
  382. switch (response.Question[0].Type)
  383. {
  384. case DnsResourceRecordType.MX:
  385. case DnsResourceRecordType.SRV:
  386. case DnsResourceRecordType.SVCB:
  387. case DnsResourceRecordType.HTTPS:
  388. //removing glue records and trying again since some mail servers fail to fallback to TCP on truncation
  389. //removing glue records to prevent truncation for SRV/SVCB/HTTPS
  390. response = response.CloneWithoutGlueRecords();
  391. sendBufferStream.Position = 0;
  392. try
  393. {
  394. response.WriteTo(sendBufferStream);
  395. }
  396. catch (NotSupportedException)
  397. {
  398. //send TC since response is still big even after removing glue records
  399. response = new DnsDatagram(response.Identifier, true, response.OPCODE, response.AuthoritativeAnswer, true, response.RecursionDesired, response.RecursionAvailable, response.AuthenticData, response.CheckingDisabled, response.RCODE, response.Question, null, null, null, request.EDNS is null ? ushort.MinValue : _udpPayloadSize) { Tag = DnsServerResponseType.Authoritative };
  400. }
  401. break;
  402. case DnsResourceRecordType.IXFR:
  403. response = new DnsDatagram(response.Identifier, true, response.OPCODE, response.AuthoritativeAnswer, false, response.RecursionDesired, response.RecursionAvailable, response.AuthenticData, response.CheckingDisabled, response.RCODE, response.Question, new DnsResourceRecord[] { response.Answer[0] }, null, null, request.EDNS is null ? ushort.MinValue : _udpPayloadSize) { Tag = DnsServerResponseType.Authoritative }; //truncate response
  404. break;
  405. default:
  406. response = new DnsDatagram(response.Identifier, true, response.OPCODE, response.AuthoritativeAnswer, true, response.RecursionDesired, response.RecursionAvailable, response.AuthenticData, response.CheckingDisabled, response.RCODE, response.Question, null, null, null, request.EDNS is null ? ushort.MinValue : _udpPayloadSize) { Tag = DnsServerResponseType.Authoritative };
  407. break;
  408. }
  409. }
  410. sendBufferStream.Position = 0;
  411. response.WriteTo(sendBufferStream);
  412. }
  413. //send dns datagram async
  414. await udpListener.SendToAsync(new ArraySegment<byte>(sendBuffer, 0, (int)sendBufferStream.Position), SocketFlags.None, returnEP);
  415. }
  416. _queryLog?.Write(remoteEP, protocol, request, response);
  417. _stats.QueueUpdate(request, remoteEP, protocol, response);
  418. }
  419. catch (Exception ex)
  420. {
  421. if ((_state == ServiceState.Stopping) || (_state == ServiceState.Stopped))
  422. return; //server stopping
  423. _queryLog?.Write(remoteEP, protocol, request, null);
  424. _log?.Write(remoteEP, protocol, ex);
  425. }
  426. }
  427. private async Task AcceptConnectionAsync(Socket tcpListener, DnsTransportProtocol protocol)
  428. {
  429. IPEndPoint localEP = tcpListener.LocalEndPoint as IPEndPoint;
  430. try
  431. {
  432. tcpListener.SendTimeout = _tcpSendTimeout;
  433. tcpListener.ReceiveTimeout = _tcpReceiveTimeout;
  434. tcpListener.NoDelay = true;
  435. while (true)
  436. {
  437. Socket socket = await tcpListener.AcceptAsync();
  438. _ = ProcessConnectionAsync(socket, protocol);
  439. }
  440. }
  441. catch (SocketException ex)
  442. {
  443. if (ex.SocketErrorCode == SocketError.OperationAborted)
  444. return; //server stopping
  445. _log?.Write(localEP, protocol, ex);
  446. }
  447. catch (ObjectDisposedException)
  448. {
  449. //server stopped
  450. }
  451. catch (Exception ex)
  452. {
  453. if ((_state == ServiceState.Stopping) || (_state == ServiceState.Stopped))
  454. return; //server stopping
  455. _log?.Write(localEP, protocol, ex);
  456. }
  457. }
  458. private async Task ProcessConnectionAsync(Socket socket, DnsTransportProtocol protocol)
  459. {
  460. IPEndPoint remoteEP = null;
  461. try
  462. {
  463. remoteEP = socket.RemoteEndPoint as IPEndPoint;
  464. switch (protocol)
  465. {
  466. case DnsTransportProtocol.Tcp:
  467. await ReadStreamRequestAsync(new NetworkStream(socket), remoteEP, protocol);
  468. break;
  469. case DnsTransportProtocol.Tls:
  470. SslStream tlsStream = new SslStream(new NetworkStream(socket));
  471. await tlsStream.AuthenticateAsServerAsync(_sslServerAuthenticationOptions).WithTimeout(_tcpReceiveTimeout);
  472. await ReadStreamRequestAsync(tlsStream, remoteEP, protocol);
  473. break;
  474. case DnsTransportProtocol.TcpProxy:
  475. if (!NetUtilities.IsPrivateIP(remoteEP.Address))
  476. {
  477. //intentionally blocking public IP addresses from using DNS-over-TCP-PROXY
  478. //this feature is intended to be used with a reverse proxy or load balancer on private network
  479. return;
  480. }
  481. ProxyProtocolStream proxyStream = await ProxyProtocolStream.CreateAsServerAsync(new NetworkStream(socket)).WithTimeout(_tcpReceiveTimeout);
  482. remoteEP = new IPEndPoint(proxyStream.SourceAddress, proxyStream.SourcePort);
  483. await ReadStreamRequestAsync(proxyStream, remoteEP, protocol);
  484. break;
  485. default:
  486. throw new InvalidOperationException();
  487. }
  488. }
  489. catch (TimeoutException)
  490. {
  491. //ignore timeout exception on TLS auth
  492. }
  493. catch (IOException)
  494. {
  495. //ignore IO exceptions
  496. }
  497. catch (Exception ex)
  498. {
  499. _log?.Write(remoteEP, protocol, ex);
  500. }
  501. finally
  502. {
  503. socket.Dispose();
  504. }
  505. }
  506. private async Task ReadStreamRequestAsync(Stream stream, IPEndPoint remoteEP, DnsTransportProtocol protocol)
  507. {
  508. try
  509. {
  510. using MemoryStream readBuffer = new MemoryStream(64);
  511. using MemoryStream writeBuffer = new MemoryStream(2048);
  512. using SemaphoreSlim writeSemaphore = new SemaphoreSlim(1, 1);
  513. while (true)
  514. {
  515. if (IsQpmLimitCrossed(remoteEP.Address))
  516. break;
  517. DnsDatagram request;
  518. //read dns datagram with timeout
  519. using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource())
  520. {
  521. Task<DnsDatagram> task = DnsDatagram.ReadFromTcpAsync(stream, readBuffer, cancellationTokenSource.Token);
  522. if (await Task.WhenAny(task, Task.Delay(_tcpReceiveTimeout, cancellationTokenSource.Token)) != task)
  523. {
  524. //read timed out
  525. await stream.DisposeAsync();
  526. return;
  527. }
  528. cancellationTokenSource.Cancel(); //cancel delay task
  529. request = await task;
  530. }
  531. //process request async
  532. _ = ProcessStreamRequestAsync(stream, writeBuffer, writeSemaphore, remoteEP, request, protocol);
  533. }
  534. }
  535. catch (ObjectDisposedException)
  536. {
  537. //ignore
  538. }
  539. catch (IOException)
  540. {
  541. //ignore IO exceptions
  542. }
  543. catch (Exception ex)
  544. {
  545. _log?.Write(remoteEP, protocol, ex);
  546. }
  547. }
  548. private async Task ProcessStreamRequestAsync(Stream stream, MemoryStream writeBuffer, SemaphoreSlim writeSemaphore, IPEndPoint remoteEP, DnsDatagram request, DnsTransportProtocol protocol)
  549. {
  550. try
  551. {
  552. DnsDatagram response = await PreProcessQueryAsync(request, remoteEP, protocol, IsRecursionAllowed(remoteEP.Address));
  553. if (response is null)
  554. {
  555. await stream.DisposeAsync();
  556. return; //drop request
  557. }
  558. //send response
  559. await writeSemaphore.WaitAsync();
  560. try
  561. {
  562. //send dns datagram
  563. await response.WriteToTcpAsync(stream, writeBuffer);
  564. await stream.FlushAsync();
  565. }
  566. finally
  567. {
  568. writeSemaphore.Release();
  569. }
  570. _queryLog?.Write(remoteEP, protocol, request, response);
  571. _stats.QueueUpdate(request, remoteEP, protocol, response);
  572. }
  573. catch (ObjectDisposedException)
  574. {
  575. //ignore
  576. }
  577. catch (IOException)
  578. {
  579. //ignore IO exceptions
  580. }
  581. catch (Exception ex)
  582. {
  583. if (request is not null)
  584. _queryLog.Write(remoteEP, protocol, request, null);
  585. _log?.Write(remoteEP, protocol, ex);
  586. }
  587. }
  588. private async Task AcceptQuicConnectionAsync(QuicListener quicListener)
  589. {
  590. try
  591. {
  592. while (true)
  593. {
  594. QuicConnection quicConnection = await quicListener.AcceptConnectionAsync();
  595. _ = ProcessQuicConnectionAsync(quicConnection);
  596. }
  597. }
  598. catch (ObjectDisposedException)
  599. {
  600. //server stopped
  601. }
  602. catch (Exception ex)
  603. {
  604. if ((_state == ServiceState.Stopping) || (_state == ServiceState.Stopped))
  605. return; //server stopping
  606. _log?.Write(quicListener.LocalEndPoint, DnsTransportProtocol.Quic, ex);
  607. }
  608. }
  609. private async Task ProcessQuicConnectionAsync(QuicConnection quicConnection)
  610. {
  611. try
  612. {
  613. while (true)
  614. {
  615. if (IsQpmLimitCrossed(quicConnection.RemoteEndPoint.Address))
  616. break;
  617. QuicStream quicStream = await quicConnection.AcceptInboundStreamAsync();
  618. _ = ProcessQuicStreamRequestAsync(quicStream, quicConnection.RemoteEndPoint);
  619. }
  620. }
  621. catch (QuicException ex)
  622. {
  623. switch (ex.QuicError)
  624. {
  625. case QuicError.ConnectionIdle:
  626. case QuicError.ConnectionAborted:
  627. case QuicError.ConnectionTimeout:
  628. break;
  629. default:
  630. _log?.Write(quicConnection.RemoteEndPoint, DnsTransportProtocol.Quic, ex);
  631. break;
  632. }
  633. }
  634. catch (Exception ex)
  635. {
  636. _log?.Write(quicConnection.RemoteEndPoint, DnsTransportProtocol.Quic, ex);
  637. }
  638. finally
  639. {
  640. await quicConnection.DisposeAsync();
  641. }
  642. }
  643. private async Task ProcessQuicStreamRequestAsync(QuicStream quicStream, IPEndPoint remoteEP)
  644. {
  645. MemoryStream sharedBuffer = new MemoryStream(512);
  646. DnsDatagram request = null;
  647. try
  648. {
  649. //read dns datagram with timeout
  650. using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource())
  651. {
  652. Task<DnsDatagram> task = DnsDatagram.ReadFromTcpAsync(quicStream, sharedBuffer, cancellationTokenSource.Token);
  653. if (await Task.WhenAny(task, Task.Delay(_tcpReceiveTimeout, cancellationTokenSource.Token)) != task)
  654. {
  655. //read timed out
  656. quicStream.Abort(QuicAbortDirection.Both, (long)DnsOverQuicErrorCodes.DOQ_UNSPECIFIED_ERROR);
  657. return;
  658. }
  659. cancellationTokenSource.Cancel(); //cancel delay task
  660. request = await task;
  661. }
  662. //process request async
  663. DnsDatagram response = await PreProcessQueryAsync(request, remoteEP, DnsTransportProtocol.Quic, IsRecursionAllowed(remoteEP.Address));
  664. if (response is null)
  665. return; //drop request
  666. //send response
  667. await response.WriteToTcpAsync(quicStream, sharedBuffer);
  668. _queryLog?.Write(remoteEP, DnsTransportProtocol.Quic, request, response);
  669. _stats.QueueUpdate(request, remoteEP, DnsTransportProtocol.Quic, response);
  670. }
  671. catch (IOException)
  672. {
  673. //ignore QuicException / IOException
  674. }
  675. catch (Exception ex)
  676. {
  677. if (request is not null)
  678. _queryLog.Write(remoteEP, DnsTransportProtocol.Quic, request, null);
  679. _log?.Write(remoteEP, DnsTransportProtocol.Quic, ex);
  680. }
  681. finally
  682. {
  683. await sharedBuffer.DisposeAsync();
  684. await quicStream.DisposeAsync();
  685. }
  686. }
  687. private async Task ProcessDoHRequestAsync(HttpContext context)
  688. {
  689. IPEndPoint remoteEP = context.GetRemoteEndPoint();
  690. DnsDatagram dnsRequest = null;
  691. try
  692. {
  693. HttpRequest request = context.Request;
  694. HttpResponse response = context.Response;
  695. if (IsQpmLimitCrossed(remoteEP.Address))
  696. {
  697. response.StatusCode = 429;
  698. await response.WriteAsync("Too Many Requests");
  699. return;
  700. }
  701. if (!request.IsHttps)
  702. {
  703. //get the actual connection remote EP
  704. IPEndPoint connectionEp = context.GetRemoteEndPoint(true);
  705. if (!NetUtilities.IsPrivateIP(connectionEp.Address))
  706. {
  707. //intentionally blocking public IP addresses from using DNS-over-HTTP (without TLS)
  708. //this feature is intended to be used with an SSL terminated reverse proxy like nginx on private network
  709. response.StatusCode = 403;
  710. await response.WriteAsync("DNS-over-HTTPS (DoH) queries are supported only on HTTPS.");
  711. return;
  712. }
  713. }
  714. switch (request.Method)
  715. {
  716. case "GET":
  717. bool acceptsDoH = false;
  718. string requestAccept = request.Headers["Accept"];
  719. if (string.IsNullOrEmpty(requestAccept))
  720. {
  721. acceptsDoH = true;
  722. }
  723. else
  724. {
  725. foreach (string mediaType in requestAccept.Split(','))
  726. {
  727. if (mediaType.Equals("application/dns-message", StringComparison.OrdinalIgnoreCase))
  728. {
  729. acceptsDoH = true;
  730. break;
  731. }
  732. }
  733. }
  734. if (!acceptsDoH)
  735. {
  736. response.Redirect((request.IsHttps ? "https://" : "http://") + request.Headers["Host"]);
  737. return;
  738. }
  739. string dnsRequestBase64Url = request.Query["dns"];
  740. if (string.IsNullOrEmpty(dnsRequestBase64Url))
  741. {
  742. response.StatusCode = 400;
  743. await response.WriteAsync("Bad Request");
  744. return;
  745. }
  746. //convert from base64url to base64
  747. dnsRequestBase64Url = dnsRequestBase64Url.Replace('-', '+');
  748. dnsRequestBase64Url = dnsRequestBase64Url.Replace('_', '/');
  749. //add padding
  750. int x = dnsRequestBase64Url.Length % 4;
  751. if (x > 0)
  752. dnsRequestBase64Url = dnsRequestBase64Url.PadRight(dnsRequestBase64Url.Length - x + 4, '=');
  753. using (MemoryStream mS = new MemoryStream(Convert.FromBase64String(dnsRequestBase64Url)))
  754. {
  755. dnsRequest = DnsDatagram.ReadFrom(mS);
  756. }
  757. break;
  758. case "POST":
  759. if (!string.Equals(request.Headers["Content-Type"], "application/dns-message", StringComparison.OrdinalIgnoreCase))
  760. {
  761. response.StatusCode = 415;
  762. await response.WriteAsync("Unsupported Media Type");
  763. return;
  764. }
  765. using (MemoryStream mS = new MemoryStream(32))
  766. {
  767. await request.Body.CopyToAsync(mS, 32);
  768. mS.Position = 0;
  769. dnsRequest = DnsDatagram.ReadFrom(mS);
  770. }
  771. break;
  772. default:
  773. throw new InvalidOperationException();
  774. }
  775. DnsDatagram dnsResponse = await PreProcessQueryAsync(dnsRequest, remoteEP, DnsTransportProtocol.Https, IsRecursionAllowed(remoteEP.Address));
  776. if (dnsResponse is null)
  777. {
  778. //drop request
  779. context.Connection.RequestClose();
  780. return;
  781. }
  782. using (MemoryStream mS = new MemoryStream(512))
  783. {
  784. dnsResponse.WriteTo(mS);
  785. mS.Position = 0;
  786. response.ContentType = "application/dns-message";
  787. response.ContentLength = mS.Length;
  788. using (Stream s = response.Body)
  789. {
  790. await mS.CopyToAsync(s, 512);
  791. }
  792. }
  793. _queryLog?.Write(remoteEP, DnsTransportProtocol.Https, dnsRequest, dnsResponse);
  794. _stats.QueueUpdate(dnsRequest, remoteEP, DnsTransportProtocol.Https, dnsResponse);
  795. }
  796. catch (Exception ex)
  797. {
  798. if (dnsRequest is not null)
  799. _queryLog?.Write(remoteEP, DnsTransportProtocol.Https, dnsRequest, null);
  800. _log?.Write(remoteEP, DnsTransportProtocol.Https, ex);
  801. }
  802. }
  803. private bool IsRecursionAllowed(IPAddress remoteIP)
  804. {
  805. switch (_recursion)
  806. {
  807. case DnsServerRecursion.Allow:
  808. return true;
  809. case DnsServerRecursion.AllowOnlyForPrivateNetworks:
  810. switch (remoteIP.AddressFamily)
  811. {
  812. case AddressFamily.InterNetwork:
  813. case AddressFamily.InterNetworkV6:
  814. return NetUtilities.IsPrivateIP(remoteIP);
  815. default:
  816. return false;
  817. }
  818. case DnsServerRecursion.UseSpecifiedNetworks:
  819. if (_recursionDeniedNetworks is not null)
  820. {
  821. foreach (NetworkAddress deniedNetworkAddress in _recursionDeniedNetworks)
  822. {
  823. if (deniedNetworkAddress.Contains(remoteIP))
  824. return false;
  825. }
  826. }
  827. if (_recursionAllowedNetworks is not null)
  828. {
  829. foreach (NetworkAddress allowedNetworkAddress in _recursionAllowedNetworks)
  830. {
  831. if (allowedNetworkAddress.Contains(remoteIP))
  832. return true;
  833. }
  834. }
  835. if (IPAddress.IsLoopback(remoteIP))
  836. return true;
  837. return false;
  838. default:
  839. return false;
  840. }
  841. }
  842. private async Task<DnsDatagram> PreProcessQueryAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed)
  843. {
  844. foreach (IDnsRequestController requestController in _dnsApplicationManager.DnsRequestControllers)
  845. {
  846. try
  847. {
  848. DnsRequestControllerAction action = await requestController.GetRequestActionAsync(request, remoteEP, protocol);
  849. switch (action)
  850. {
  851. case DnsRequestControllerAction.DropSilently:
  852. return null; //drop request
  853. case DnsRequestControllerAction.DropWithRefused:
  854. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.Refused, request.Question, null, null, null, request.EDNS is null ? ushort.MinValue : _udpPayloadSize, request.DnssecOk ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None) { Tag = DnsServerResponseType.Authoritative }; //drop request with refused
  855. }
  856. }
  857. catch (Exception ex)
  858. {
  859. _log?.Write(remoteEP, protocol, ex);
  860. }
  861. }
  862. if (request.ParsingException is not null)
  863. {
  864. //format error
  865. if (request.ParsingException is not IOException)
  866. _log?.Write(remoteEP, protocol, request.ParsingException);
  867. //format error response
  868. return new DnsDatagram(request.Identifier, true, request.OPCODE, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.FormatError, request.Question, null, null, null, request.EDNS is null ? ushort.MinValue : _udpPayloadSize, request.DnssecOk ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None) { Tag = DnsServerResponseType.Authoritative };
  869. }
  870. if (request.IsSigned)
  871. {
  872. if (!request.VerifySignedRequest(_tsigKeys, out DnsDatagram unsignedRequest, out DnsDatagram errorResponse))
  873. {
  874. _log?.Write(remoteEP, protocol, "DNS Server received a request that failed TSIG signature verification (RCODE: " + errorResponse.RCODE + "; TSIG Error: " + errorResponse.TsigError + ")");
  875. errorResponse.Tag = DnsServerResponseType.Authoritative;
  876. return errorResponse;
  877. }
  878. DnsDatagram unsignedResponse = await PostProcessQueryAsync(request, remoteEP, protocol, await ProcessQueryAsync(unsignedRequest, remoteEP, protocol, isRecursionAllowed, false, request.TsigKeyName));
  879. return unsignedResponse.SignResponse(request, _tsigKeys);
  880. }
  881. if (request.EDNS is not null)
  882. {
  883. if (request.EDNS.Version != 0)
  884. return new DnsDatagram(request.Identifier, true, request.OPCODE, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.BADVERS, request.Question, null, null, null, _udpPayloadSize, request.DnssecOk ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None) { Tag = DnsServerResponseType.Authoritative };
  885. }
  886. return await PostProcessQueryAsync(request, remoteEP, protocol, await ProcessQueryAsync(request, remoteEP, protocol, isRecursionAllowed, false, null));
  887. }
  888. private async Task<DnsDatagram> PostProcessQueryAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response)
  889. {
  890. foreach (IDnsPostProcessor postProcessor in _dnsApplicationManager.DnsPostProcessors)
  891. {
  892. try
  893. {
  894. response = await postProcessor.PostProcessAsync(request, remoteEP, protocol, response);
  895. }
  896. catch (Exception ex)
  897. {
  898. _log?.Write(remoteEP, protocol, ex);
  899. }
  900. }
  901. if (request.EDNS is null)
  902. {
  903. if (response.EDNS is not null)
  904. response = response.CloneWithoutEDns();
  905. return response;
  906. }
  907. if (response.EDNS is not null)
  908. return response;
  909. IReadOnlyList<EDnsOption> options = null;
  910. EDnsClientSubnetOptionData requestECS = request.GetEDnsClientSubnetOption(true);
  911. if (requestECS is not null)
  912. options = EDnsClientSubnetOptionData.GetEDnsClientSubnetOption(requestECS.SourcePrefixLength, 0, requestECS.Address);
  913. if (response.Additional.Count == 0)
  914. return response.Clone(null, null, new DnsResourceRecord[] { DnsDatagramEdns.GetOPTFor(_udpPayloadSize, response.RCODE, 0, request.DnssecOk ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, options) });
  915. if (response.IsSigned)
  916. return response;
  917. DnsResourceRecord[] newAdditional = new DnsResourceRecord[response.Additional.Count + 1];
  918. for (int i = 0; i < response.Additional.Count; i++)
  919. newAdditional[i] = response.Additional[i];
  920. newAdditional[response.Additional.Count] = DnsDatagramEdns.GetOPTFor(_udpPayloadSize, response.RCODE, 0, request.DnssecOk ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, options);
  921. return response.Clone(null, null, newAdditional);
  922. }
  923. private async Task<DnsDatagram> ProcessQueryAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, bool skipDnsAppAuthoritativeRequestHandlers, string tsigAuthenticatedKeyName)
  924. {
  925. if (request.IsResponse)
  926. return null; //drop response datagram to avoid loops in rare scenarios
  927. switch (request.OPCODE)
  928. {
  929. case DnsOpcode.StandardQuery:
  930. if (request.Question.Count != 1)
  931. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  932. if (request.Question[0].Class != DnsClass.IN)
  933. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.Refused, request.Question) { Tag = DnsServerResponseType.Authoritative };
  934. try
  935. {
  936. DnsQuestionRecord question = request.Question[0];
  937. switch (question.Type)
  938. {
  939. case DnsResourceRecordType.AXFR:
  940. if (protocol == DnsTransportProtocol.Udp)
  941. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  942. return await ProcessZoneTransferQueryAsync(request, remoteEP, protocol, tsigAuthenticatedKeyName);
  943. case DnsResourceRecordType.IXFR:
  944. return await ProcessZoneTransferQueryAsync(request, remoteEP, protocol, tsigAuthenticatedKeyName);
  945. case DnsResourceRecordType.FWD:
  946. case DnsResourceRecordType.APP:
  947. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.Refused, request.Question) { Tag = DnsServerResponseType.Authoritative };
  948. }
  949. //query authoritative zone
  950. DnsDatagram response = await ProcessAuthoritativeQueryAsync(request, remoteEP, protocol, isRecursionAllowed, skipDnsAppAuthoritativeRequestHandlers);
  951. if (response is not null)
  952. {
  953. if ((question.Type == DnsResourceRecordType.ANY) && (protocol == DnsTransportProtocol.Udp)) //force TCP for ANY request
  954. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, true, true, request.RecursionDesired, isRecursionAllowed, false, false, response.RCODE, request.Question) { Tag = DnsServerResponseType.Authoritative };
  955. return response;
  956. }
  957. if (!request.RecursionDesired || !isRecursionAllowed)
  958. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.Refused, request.Question) { Tag = DnsServerResponseType.Authoritative };
  959. //do recursive query
  960. if ((question.Type == DnsResourceRecordType.ANY) && (protocol == DnsTransportProtocol.Udp)) //force TCP for ANY request
  961. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, true, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  962. return await ProcessRecursiveQueryAsync(request, remoteEP, protocol, null, _dnssecValidation, false, skipDnsAppAuthoritativeRequestHandlers);
  963. }
  964. catch (InvalidDomainNameException)
  965. {
  966. //format error response
  967. return new DnsDatagram(request.Identifier, true, request.OPCODE, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  968. }
  969. catch (Exception ex)
  970. {
  971. _log?.Write(remoteEP, protocol, ex);
  972. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.ServerFailure, request.Question) { Tag = DnsServerResponseType.Authoritative };
  973. }
  974. case DnsOpcode.Notify:
  975. return await ProcessNotifyQueryAsync(request, remoteEP, protocol);
  976. case DnsOpcode.Update:
  977. return await ProcessUpdateQueryAsync(request, remoteEP, protocol, tsigAuthenticatedKeyName);
  978. default:
  979. return new DnsDatagram(request.Identifier, true, request.OPCODE, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NotImplemented, request.Question) { Tag = DnsServerResponseType.Authoritative };
  980. }
  981. }
  982. private async Task<DnsDatagram> ProcessNotifyQueryAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol)
  983. {
  984. AuthZoneInfo authZoneInfo = _authZoneManager.GetAuthZoneInfo(request.Question[0].Name);
  985. if ((authZoneInfo is null) || (authZoneInfo.Type != AuthZoneType.Secondary) || authZoneInfo.Disabled)
  986. return new DnsDatagram(request.Identifier, true, DnsOpcode.Notify, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.Refused, request.Question) { Tag = DnsServerResponseType.Authoritative };
  987. IPAddress remoteAddress = remoteEP.Address;
  988. bool remoteVerified = false;
  989. IReadOnlyList<NameServerAddress> primaryNameServers = await authZoneInfo.GetPrimaryNameServerAddressesAsync(this);
  990. foreach (NameServerAddress primaryNameServer in primaryNameServers)
  991. {
  992. if (primaryNameServer.IPEndPoint.Address.Equals(remoteAddress))
  993. {
  994. remoteVerified = true;
  995. break;
  996. }
  997. }
  998. if (!remoteVerified)
  999. {
  1000. _log?.Write(remoteEP, protocol, "DNS Server refused a NOTIFY request since the request IP address was not recognized by the secondary zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1001. return new DnsDatagram(request.Identifier, true, DnsOpcode.Notify, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.Refused, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1002. }
  1003. _log?.Write(remoteEP, protocol, "DNS Server received a NOTIFY request for secondary zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1004. if ((request.Answer.Count > 0) && (request.Answer[0].Type == DnsResourceRecordType.SOA))
  1005. {
  1006. IReadOnlyList<DnsResourceRecord> localSoaRecords = authZoneInfo.GetApexRecords(DnsResourceRecordType.SOA);
  1007. if (!DnsSOARecordData.IsZoneUpdateAvailable((localSoaRecords[0].RDATA as DnsSOARecordData).Serial, (request.Answer[0].RDATA as DnsSOARecordData).Serial))
  1008. {
  1009. //no update was available
  1010. return new DnsDatagram(request.Identifier, true, DnsOpcode.Notify, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NoError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1011. }
  1012. }
  1013. authZoneInfo.TriggerRefresh();
  1014. return new DnsDatagram(request.Identifier, true, DnsOpcode.Notify, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NoError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1015. }
  1016. private async Task<DnsDatagram> ProcessUpdateQueryAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, string tsigAuthenticatedKeyName)
  1017. {
  1018. if ((request.Question.Count != 1) || (request.Question[0].Type != DnsResourceRecordType.SOA))
  1019. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1020. if (request.Question[0].Class != DnsClass.IN)
  1021. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NotAuth, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1022. AuthZoneInfo authZoneInfo = _authZoneManager.GetAuthZoneInfo(request.Question[0].Name);
  1023. if ((authZoneInfo is null) || authZoneInfo.Disabled)
  1024. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NotAuth, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1025. _log?.Write(remoteEP, protocol, "DNS Server received a zone UPDATE request for zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1026. async Task<bool> IsZoneNameServerAllowedAsync()
  1027. {
  1028. IPAddress remoteAddress = remoteEP.Address;
  1029. IReadOnlyList<NameServerAddress> secondaryNameServers = await authZoneInfo.GetSecondaryNameServerAddressesAsync(this);
  1030. foreach (NameServerAddress secondaryNameServer in secondaryNameServers)
  1031. {
  1032. if (secondaryNameServer.IPEndPoint.Address.Equals(remoteAddress))
  1033. return true;
  1034. }
  1035. return false;
  1036. }
  1037. bool IsSpecifiedIpAddressAllowed()
  1038. {
  1039. IPAddress remoteAddress = remoteEP.Address;
  1040. IReadOnlyCollection<IPAddress> specifiedIpAddresses = authZoneInfo.UpdateIpAddresses;
  1041. if (specifiedIpAddresses is not null)
  1042. {
  1043. foreach (IPAddress specifiedIpAddress in specifiedIpAddresses)
  1044. {
  1045. if (specifiedIpAddress.Equals(remoteAddress))
  1046. return true;
  1047. }
  1048. }
  1049. return false;
  1050. }
  1051. async Task<bool> IsUpdatePermittedAsync()
  1052. {
  1053. bool isUpdateAllowed;
  1054. switch (authZoneInfo.Update)
  1055. {
  1056. case AuthZoneUpdate.Allow:
  1057. isUpdateAllowed = true;
  1058. break;
  1059. case AuthZoneUpdate.AllowOnlyZoneNameServers:
  1060. isUpdateAllowed = await IsZoneNameServerAllowedAsync();
  1061. break;
  1062. case AuthZoneUpdate.AllowOnlySpecifiedIpAddresses:
  1063. isUpdateAllowed = IsSpecifiedIpAddressAllowed();
  1064. break;
  1065. case AuthZoneUpdate.AllowBothZoneNameServersAndSpecifiedIpAddresses:
  1066. isUpdateAllowed = IsSpecifiedIpAddressAllowed() || await IsZoneNameServerAllowedAsync();
  1067. break;
  1068. case AuthZoneUpdate.Deny:
  1069. default:
  1070. isUpdateAllowed = false;
  1071. break;
  1072. }
  1073. if (!isUpdateAllowed)
  1074. {
  1075. _log?.Write(remoteEP, protocol, "DNS Server refused a zone UPDATE request since the request IP address is not allowed by the zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1076. return false;
  1077. }
  1078. //check security policies
  1079. if ((authZoneInfo.UpdateSecurityPolicies is not null) && (authZoneInfo.UpdateSecurityPolicies.Count > 0))
  1080. {
  1081. if ((tsigAuthenticatedKeyName is null) || !authZoneInfo.UpdateSecurityPolicies.TryGetValue(tsigAuthenticatedKeyName.ToLower(), out IReadOnlyDictionary<string, IReadOnlyList<DnsResourceRecordType>> policyMap))
  1082. {
  1083. _log?.Write(remoteEP, protocol, "DNS Server refused a zone UPDATE request since the request is missing TSIG auth required by the zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1084. return false;
  1085. }
  1086. //check policy
  1087. foreach (DnsResourceRecord uRecord in request.Authority)
  1088. {
  1089. bool isPermitted = false;
  1090. foreach (KeyValuePair<string, IReadOnlyList<DnsResourceRecordType>> policy in policyMap)
  1091. {
  1092. if (
  1093. uRecord.Name.Equals(policy.Key, StringComparison.OrdinalIgnoreCase) ||
  1094. (policy.Key.StartsWith("*.") && uRecord.Name.EndsWith(policy.Key.Substring(1), StringComparison.OrdinalIgnoreCase))
  1095. )
  1096. {
  1097. foreach (DnsResourceRecordType allowedType in policy.Value)
  1098. {
  1099. if ((allowedType == DnsResourceRecordType.ANY) || (allowedType == uRecord.Type))
  1100. {
  1101. isPermitted = true;
  1102. break;
  1103. }
  1104. }
  1105. if (isPermitted)
  1106. break;
  1107. }
  1108. }
  1109. if (!isPermitted)
  1110. {
  1111. _log?.Write(remoteEP, protocol, "DNS Server refused a zone UPDATE request [" + uRecord.Name.ToLowerInvariant() + " " + uRecord.Type.ToString() + " " + uRecord.Class.ToString() + "] due to Dynamic Updates Security Policy for zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1112. return false;
  1113. }
  1114. }
  1115. }
  1116. return true;
  1117. }
  1118. switch (authZoneInfo.Type)
  1119. {
  1120. case AuthZoneType.Primary:
  1121. //update
  1122. {
  1123. //process prerequisite section
  1124. {
  1125. Dictionary<string, Dictionary<DnsResourceRecordType, List<DnsResourceRecord>>> temp = new Dictionary<string, Dictionary<DnsResourceRecordType, List<DnsResourceRecord>>>();
  1126. foreach (DnsResourceRecord prRecord in request.Answer)
  1127. {
  1128. if (prRecord.TTL != 0)
  1129. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1130. AuthZoneInfo prAuthZoneInfo = _authZoneManager.FindAuthZoneInfo(prRecord.Name);
  1131. if ((prAuthZoneInfo is null) || !prAuthZoneInfo.Name.Equals(authZoneInfo.Name, StringComparison.OrdinalIgnoreCase))
  1132. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NotZone, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1133. if (prRecord.Class == DnsClass.ANY)
  1134. {
  1135. if (prRecord.RDATA.RDLENGTH != 0)
  1136. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1137. if (prRecord.Type == DnsResourceRecordType.ANY)
  1138. {
  1139. //check if name is in use
  1140. if (!_authZoneManager.NameExists(authZoneInfo.Name, prRecord.Name))
  1141. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NxDomain, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1142. }
  1143. else
  1144. {
  1145. //check if RRSet exists (value independent)
  1146. IReadOnlyList<DnsResourceRecord> rrset = _authZoneManager.GetRecords(authZoneInfo.Name, prRecord.Name, prRecord.Type);
  1147. if (rrset.Count == 0)
  1148. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NXRRSet, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1149. }
  1150. }
  1151. else if (prRecord.Class == DnsClass.NONE)
  1152. {
  1153. if (prRecord.RDATA.RDLENGTH != 0)
  1154. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1155. if (prRecord.Type == DnsResourceRecordType.ANY)
  1156. {
  1157. //check if name is not in use
  1158. if (_authZoneManager.NameExists(authZoneInfo.Name, prRecord.Name))
  1159. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.YXDomain, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1160. }
  1161. else
  1162. {
  1163. //check if RRSet does not exists
  1164. IReadOnlyList<DnsResourceRecord> rrset = _authZoneManager.GetRecords(authZoneInfo.Name, prRecord.Name, prRecord.Type);
  1165. if (rrset.Count > 0)
  1166. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.YXRRSet, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1167. }
  1168. }
  1169. else if (prRecord.Class == request.Question[0].Class)
  1170. {
  1171. //check if RRSet exists (value dependent)
  1172. //add to temp for later comparison
  1173. string recordName = prRecord.Name.ToLower();
  1174. if (!temp.TryGetValue(recordName, out Dictionary<DnsResourceRecordType, List<DnsResourceRecord>> rrsetEntry))
  1175. {
  1176. rrsetEntry = new Dictionary<DnsResourceRecordType, List<DnsResourceRecord>>();
  1177. temp.Add(recordName, rrsetEntry);
  1178. }
  1179. if (!rrsetEntry.TryGetValue(prRecord.Type, out List<DnsResourceRecord> rrset))
  1180. {
  1181. rrset = new List<DnsResourceRecord>();
  1182. rrsetEntry.Add(prRecord.Type, rrset);
  1183. }
  1184. rrset.Add(prRecord);
  1185. }
  1186. else
  1187. {
  1188. //FORMERR
  1189. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1190. }
  1191. }
  1192. //compare collected RRSets in temp
  1193. foreach (KeyValuePair<string, Dictionary<DnsResourceRecordType, List<DnsResourceRecord>>> zoneEntry in temp)
  1194. {
  1195. foreach (KeyValuePair<DnsResourceRecordType, List<DnsResourceRecord>> rrsetEntry in zoneEntry.Value)
  1196. {
  1197. IReadOnlyList<DnsResourceRecord> prRRSet = rrsetEntry.Value;
  1198. IReadOnlyList<DnsResourceRecord> rrset = _authZoneManager.GetRecords(authZoneInfo.Name, zoneEntry.Key, rrsetEntry.Key);
  1199. //check if RRSet exists (value dependent)
  1200. //compare RRSets
  1201. if (prRRSet.Count != rrset.Count)
  1202. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NXRRSet, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1203. foreach (DnsResourceRecord prRecord in prRRSet)
  1204. {
  1205. bool found = false;
  1206. foreach (DnsResourceRecord record in rrset)
  1207. {
  1208. if (
  1209. prRecord.Name.Equals(record.Name, StringComparison.OrdinalIgnoreCase) &&
  1210. (prRecord.Class == record.Class) &&
  1211. (prRecord.Type == record.Type) &&
  1212. (prRecord.RDATA.RDLENGTH == record.RDATA.RDLENGTH) &&
  1213. prRecord.RDATA.Equals(record.RDATA)
  1214. )
  1215. {
  1216. found = true;
  1217. break;
  1218. }
  1219. }
  1220. if (!found)
  1221. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NXRRSet, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1222. }
  1223. }
  1224. }
  1225. }
  1226. //check for permissions
  1227. if (!await IsUpdatePermittedAsync())
  1228. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.Refused, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1229. //process update section
  1230. {
  1231. //prescan
  1232. foreach (DnsResourceRecord uRecord in request.Authority)
  1233. {
  1234. AuthZoneInfo prAuthZoneInfo = _authZoneManager.FindAuthZoneInfo(uRecord.Name);
  1235. if ((prAuthZoneInfo is null) || !prAuthZoneInfo.Name.Equals(authZoneInfo.Name, StringComparison.OrdinalIgnoreCase))
  1236. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NotZone, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1237. if (uRecord.Class == request.Question[0].Class)
  1238. {
  1239. switch (uRecord.Type)
  1240. {
  1241. case DnsResourceRecordType.ANY:
  1242. case DnsResourceRecordType.AXFR:
  1243. case DnsResourceRecordType.MAILA:
  1244. case DnsResourceRecordType.MAILB:
  1245. case DnsResourceRecordType.IXFR:
  1246. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1247. }
  1248. }
  1249. else if (uRecord.Class == DnsClass.ANY)
  1250. {
  1251. if ((uRecord.TTL != 0) || (uRecord.RDATA.RDLENGTH != 0))
  1252. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1253. switch (uRecord.Type)
  1254. {
  1255. case DnsResourceRecordType.AXFR:
  1256. case DnsResourceRecordType.MAILA:
  1257. case DnsResourceRecordType.MAILB:
  1258. case DnsResourceRecordType.IXFR:
  1259. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1260. }
  1261. }
  1262. else if (uRecord.Class == DnsClass.NONE)
  1263. {
  1264. if (uRecord.TTL != 0)
  1265. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1266. switch (uRecord.Type)
  1267. {
  1268. case DnsResourceRecordType.ANY:
  1269. case DnsResourceRecordType.AXFR:
  1270. case DnsResourceRecordType.MAILA:
  1271. case DnsResourceRecordType.MAILB:
  1272. case DnsResourceRecordType.IXFR:
  1273. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1274. }
  1275. }
  1276. else
  1277. {
  1278. //FORMERR
  1279. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1280. }
  1281. }
  1282. //update
  1283. Dictionary<string, Dictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>>> originalRRSets = new Dictionary<string, Dictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>>>();
  1284. void AddToOriginalRRSets(string domain, DnsResourceRecordType type, IReadOnlyList<DnsResourceRecord> existingRRSet)
  1285. {
  1286. if (!originalRRSets.TryGetValue(domain, out Dictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> originalRRSetEntries))
  1287. {
  1288. originalRRSetEntries = new Dictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>>();
  1289. originalRRSets.Add(domain, originalRRSetEntries);
  1290. }
  1291. originalRRSetEntries.TryAdd(type, existingRRSet);
  1292. }
  1293. try
  1294. {
  1295. foreach (DnsResourceRecord uRecord in request.Authority)
  1296. {
  1297. if (uRecord.Class == request.Question[0].Class)
  1298. {
  1299. //Add to an RRset
  1300. if (uRecord.Type == DnsResourceRecordType.CNAME)
  1301. {
  1302. if (_authZoneManager.NameExists(authZoneInfo.Name, uRecord.Name) && (_authZoneManager.GetRecords(authZoneInfo.Name, uRecord.Name, DnsResourceRecordType.CNAME).Count == 0))
  1303. continue; //current name exists and has non-CNAME records so cannot add CNAME record
  1304. IReadOnlyList<DnsResourceRecord> existingRRSet = _authZoneManager.GetRecords(authZoneInfo.Name, uRecord.Name, uRecord.Type);
  1305. AddToOriginalRRSets(uRecord.Name, uRecord.Type, existingRRSet);
  1306. _authZoneManager.SetRecord(authZoneInfo.Name, uRecord);
  1307. }
  1308. else if (uRecord.Type == DnsResourceRecordType.DNAME)
  1309. {
  1310. IReadOnlyList<DnsResourceRecord> existingRRSet = _authZoneManager.GetRecords(authZoneInfo.Name, uRecord.Name, uRecord.Type);
  1311. AddToOriginalRRSets(uRecord.Name, uRecord.Type, existingRRSet);
  1312. _authZoneManager.SetRecord(authZoneInfo.Name, uRecord);
  1313. }
  1314. else if (uRecord.Type == DnsResourceRecordType.SOA)
  1315. {
  1316. if (!uRecord.Name.Equals(authZoneInfo.Name, StringComparison.OrdinalIgnoreCase))
  1317. continue; //can add SOA only to apex
  1318. IReadOnlyList<DnsResourceRecord> existingRRSet = _authZoneManager.GetRecords(authZoneInfo.Name, uRecord.Name, uRecord.Type);
  1319. AddToOriginalRRSets(uRecord.Name, uRecord.Type, existingRRSet);
  1320. _authZoneManager.SetRecord(authZoneInfo.Name, uRecord);
  1321. }
  1322. else
  1323. {
  1324. if (_authZoneManager.GetRecords(authZoneInfo.Name, uRecord.Name, DnsResourceRecordType.CNAME).Count > 0)
  1325. continue; //current name contains CNAME so cannot add non-CNAME record
  1326. IReadOnlyList<DnsResourceRecord> existingRRSet = _authZoneManager.GetRecords(authZoneInfo.Name, uRecord.Name, uRecord.Type);
  1327. AddToOriginalRRSets(uRecord.Name, uRecord.Type, existingRRSet);
  1328. if (uRecord.Type == DnsResourceRecordType.NS)
  1329. uRecord.SyncGlueRecords(request.Additional);
  1330. _authZoneManager.AddRecord(authZoneInfo.Name, uRecord);
  1331. }
  1332. }
  1333. else if (uRecord.Class == DnsClass.ANY)
  1334. {
  1335. if (uRecord.Type == DnsResourceRecordType.ANY)
  1336. {
  1337. //Delete all RRsets from a name
  1338. IReadOnlyDictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> existingRRSets = _authZoneManager.GetAllRecords(authZoneInfo.Name, uRecord.Name);
  1339. if (uRecord.Name.Equals(authZoneInfo.Name, StringComparison.OrdinalIgnoreCase))
  1340. {
  1341. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> existingRRSet in existingRRSets)
  1342. {
  1343. switch (existingRRSet.Key)
  1344. {
  1345. case DnsResourceRecordType.SOA:
  1346. case DnsResourceRecordType.NS:
  1347. case DnsResourceRecordType.DNSKEY:
  1348. case DnsResourceRecordType.RRSIG:
  1349. case DnsResourceRecordType.NSEC:
  1350. case DnsResourceRecordType.NSEC3PARAM:
  1351. case DnsResourceRecordType.NSEC3:
  1352. continue; //no apex SOA/NS can be deleted; skip DNSSEC rrsets
  1353. }
  1354. AddToOriginalRRSets(uRecord.Name, existingRRSet.Key, existingRRSet.Value);
  1355. _authZoneManager.DeleteRecords(authZoneInfo.Name, uRecord.Name, existingRRSet.Key);
  1356. }
  1357. }
  1358. else
  1359. {
  1360. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> existingRRSet in existingRRSets)
  1361. {
  1362. switch (existingRRSet.Key)
  1363. {
  1364. case DnsResourceRecordType.DNSKEY:
  1365. case DnsResourceRecordType.RRSIG:
  1366. case DnsResourceRecordType.NSEC:
  1367. case DnsResourceRecordType.NSEC3PARAM:
  1368. case DnsResourceRecordType.NSEC3:
  1369. continue; //skip DNSSEC rrsets
  1370. }
  1371. AddToOriginalRRSets(uRecord.Name, existingRRSet.Key, existingRRSet.Value);
  1372. _authZoneManager.DeleteRecords(authZoneInfo.Name, uRecord.Name, existingRRSet.Key);
  1373. }
  1374. }
  1375. }
  1376. else
  1377. {
  1378. //Delete an RRset
  1379. if (uRecord.Name.Equals(authZoneInfo.Name, StringComparison.OrdinalIgnoreCase))
  1380. {
  1381. switch (uRecord.Type)
  1382. {
  1383. case DnsResourceRecordType.SOA:
  1384. case DnsResourceRecordType.NS:
  1385. case DnsResourceRecordType.DNSKEY:
  1386. case DnsResourceRecordType.RRSIG:
  1387. case DnsResourceRecordType.NSEC:
  1388. case DnsResourceRecordType.NSEC3PARAM:
  1389. case DnsResourceRecordType.NSEC3:
  1390. continue; //no apex SOA/NS can be deleted; skip DNSSEC rrsets
  1391. }
  1392. }
  1393. IReadOnlyList<DnsResourceRecord> existingRRSet = _authZoneManager.GetRecords(authZoneInfo.Name, uRecord.Name, uRecord.Type);
  1394. AddToOriginalRRSets(uRecord.Name, uRecord.Type, existingRRSet);
  1395. _authZoneManager.DeleteRecords(authZoneInfo.Name, uRecord.Name, uRecord.Type);
  1396. }
  1397. }
  1398. else if (uRecord.Class == DnsClass.NONE)
  1399. {
  1400. //Delete an RR from an RRset
  1401. switch (uRecord.Type)
  1402. {
  1403. case DnsResourceRecordType.SOA:
  1404. case DnsResourceRecordType.DNSKEY:
  1405. case DnsResourceRecordType.RRSIG:
  1406. case DnsResourceRecordType.NSEC:
  1407. case DnsResourceRecordType.NSEC3PARAM:
  1408. case DnsResourceRecordType.NSEC3:
  1409. continue; //no SOA can be deleted; skip DNSSEC rrsets
  1410. }
  1411. IReadOnlyList<DnsResourceRecord> existingRRSet = _authZoneManager.GetRecords(authZoneInfo.Name, uRecord.Name, uRecord.Type);
  1412. if ((uRecord.Type == DnsResourceRecordType.NS) && (existingRRSet.Count == 1) && uRecord.Name.Equals(authZoneInfo.Name, StringComparison.OrdinalIgnoreCase))
  1413. continue; //no apex NS can be deleted if only 1 NS exists
  1414. AddToOriginalRRSets(uRecord.Name, uRecord.Type, existingRRSet);
  1415. _authZoneManager.DeleteRecord(authZoneInfo.Name, uRecord.Name, uRecord.Type, uRecord.RDATA);
  1416. }
  1417. }
  1418. }
  1419. catch
  1420. {
  1421. //revert
  1422. foreach (KeyValuePair<string, Dictionary<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>>> originalRRSetEntries in originalRRSets)
  1423. {
  1424. foreach (KeyValuePair<DnsResourceRecordType, IReadOnlyList<DnsResourceRecord>> originalRRSet in originalRRSetEntries.Value)
  1425. {
  1426. if (originalRRSet.Value.Count == 0)
  1427. _authZoneManager.DeleteRecords(authZoneInfo.Name, originalRRSetEntries.Key, originalRRSet.Key);
  1428. else
  1429. _authZoneManager.SetRecords(authZoneInfo.Name, originalRRSet.Value);
  1430. }
  1431. }
  1432. throw;
  1433. }
  1434. }
  1435. _authZoneManager.SaveZoneFile(authZoneInfo.Name);
  1436. _log?.Write(remoteEP, protocol, "DNS Server successfully processed a zone UPDATE request for zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1437. //NOERROR
  1438. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NoError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1439. }
  1440. case AuthZoneType.Secondary:
  1441. //forward to primary
  1442. {
  1443. IReadOnlyList<NameServerAddress> primaryNameServers = await authZoneInfo.GetPrimaryNameServerAddressesAsync(this);
  1444. DnsResourceRecord soaRecord = authZoneInfo.GetApexRecords(DnsResourceRecordType.SOA)[0];
  1445. AuthRecordInfo recordInfo = soaRecord.GetAuthRecordInfo();
  1446. switch (recordInfo.ZoneTransferProtocol)
  1447. {
  1448. case DnsTransportProtocol.Tls:
  1449. case DnsTransportProtocol.Quic:
  1450. {
  1451. //change name server protocol to TLS/QUIC
  1452. List<NameServerAddress> updatedNameServers = new List<NameServerAddress>(primaryNameServers.Count);
  1453. foreach (NameServerAddress primaryNameServer in primaryNameServers)
  1454. {
  1455. if (primaryNameServer.Protocol == recordInfo.ZoneTransferProtocol)
  1456. updatedNameServers.Add(primaryNameServer);
  1457. else
  1458. updatedNameServers.Add(primaryNameServer.ChangeProtocol(recordInfo.ZoneTransferProtocol));
  1459. }
  1460. primaryNameServers = updatedNameServers;
  1461. }
  1462. break;
  1463. default:
  1464. if (protocol == DnsTransportProtocol.Tcp)
  1465. {
  1466. //change name server protocol to TCP
  1467. List<NameServerAddress> updatedNameServers = new List<NameServerAddress>(primaryNameServers.Count);
  1468. foreach (NameServerAddress primaryNameServer in primaryNameServers)
  1469. {
  1470. if (primaryNameServer.Protocol == DnsTransportProtocol.Tcp)
  1471. updatedNameServers.Add(primaryNameServer);
  1472. else
  1473. updatedNameServers.Add(primaryNameServer.ChangeProtocol(DnsTransportProtocol.Tcp));
  1474. }
  1475. primaryNameServers = updatedNameServers;
  1476. }
  1477. break;
  1478. }
  1479. TsigKey key = null;
  1480. if (!string.IsNullOrEmpty(tsigAuthenticatedKeyName) && ((_tsigKeys is null) || !_tsigKeys.TryGetValue(tsigAuthenticatedKeyName, out key)))
  1481. throw new DnsServerException("DNS Server does not have TSIG key '" + tsigAuthenticatedKeyName + "' configured to authenticate dynamic updates for secondary zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1482. DnsClient dnsClient = new DnsClient(primaryNameServers);
  1483. dnsClient.Proxy = _proxy;
  1484. dnsClient.PreferIPv6 = _preferIPv6;
  1485. dnsClient.Retries = _forwarderRetries;
  1486. dnsClient.Timeout = _forwarderTimeout;
  1487. dnsClient.Concurrency = 1;
  1488. DnsDatagram newRequest = request.Clone();
  1489. newRequest.SetRandomIdentifier();
  1490. DnsDatagram newResponse;
  1491. if (key is null)
  1492. newResponse = await dnsClient.ResolveAsync(newRequest);
  1493. else
  1494. newResponse = await dnsClient.ResolveAsync(newRequest, key);
  1495. newResponse.SetIdentifier(request.Identifier);
  1496. return newResponse;
  1497. }
  1498. default:
  1499. return new DnsDatagram(request.Identifier, true, DnsOpcode.Update, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NotAuth, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1500. }
  1501. }
  1502. private async Task<DnsDatagram> ProcessZoneTransferQueryAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, string tsigAuthenticatedKeyName)
  1503. {
  1504. AuthZoneInfo authZoneInfo = _authZoneManager.GetAuthZoneInfo(request.Question[0].Name);
  1505. if ((authZoneInfo is null) || authZoneInfo.Disabled || authZoneInfo.IsExpired)
  1506. {
  1507. _log?.Write(remoteEP, protocol, "DNS Server refused a zone transfer request due to zone not found, zone disabled, or zone expired reasons for zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1508. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.Refused, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1509. }
  1510. switch (authZoneInfo.Type)
  1511. {
  1512. case AuthZoneType.Primary:
  1513. case AuthZoneType.Secondary:
  1514. break;
  1515. default:
  1516. _log?.Write(remoteEP, protocol, "DNS Server refused a zone transfer request since the DNS server is not authoritative for zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1517. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.Refused, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1518. }
  1519. async Task<bool> IsZoneNameServerAllowedAsync()
  1520. {
  1521. IPAddress remoteAddress = remoteEP.Address;
  1522. IReadOnlyList<NameServerAddress> secondaryNameServers = await authZoneInfo.GetSecondaryNameServerAddressesAsync(this);
  1523. foreach (NameServerAddress secondaryNameServer in secondaryNameServers)
  1524. {
  1525. if (secondaryNameServer.IPEndPoint.Address.Equals(remoteAddress))
  1526. return true;
  1527. }
  1528. return false;
  1529. }
  1530. bool IsSpecifiedNameServerAllowed()
  1531. {
  1532. IPAddress remoteAddress = remoteEP.Address;
  1533. IReadOnlyCollection<IPAddress> specifiedNameServers = authZoneInfo.ZoneTransferNameServers;
  1534. if (specifiedNameServers is not null)
  1535. {
  1536. foreach (IPAddress specifiedNameServer in specifiedNameServers)
  1537. {
  1538. if (specifiedNameServer.Equals(remoteAddress))
  1539. return true;
  1540. }
  1541. }
  1542. return false;
  1543. }
  1544. bool isZoneTransferAllowed = false;
  1545. switch (authZoneInfo.ZoneTransfer)
  1546. {
  1547. case AuthZoneTransfer.Deny:
  1548. break;
  1549. case AuthZoneTransfer.Allow:
  1550. isZoneTransferAllowed = true;
  1551. break;
  1552. case AuthZoneTransfer.AllowOnlyZoneNameServers:
  1553. isZoneTransferAllowed = await IsZoneNameServerAllowedAsync();
  1554. break;
  1555. case AuthZoneTransfer.AllowOnlySpecifiedNameServers:
  1556. isZoneTransferAllowed = IsSpecifiedNameServerAllowed();
  1557. break;
  1558. case AuthZoneTransfer.AllowBothZoneAndSpecifiedNameServers:
  1559. isZoneTransferAllowed = IsSpecifiedNameServerAllowed() || await IsZoneNameServerAllowedAsync();
  1560. break;
  1561. }
  1562. if (!isZoneTransferAllowed)
  1563. {
  1564. _log?.Write(remoteEP, protocol, "DNS Server refused a zone transfer request since the request IP address is not allowed by the zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1565. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.Refused, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1566. }
  1567. if ((authZoneInfo.ZoneTransferTsigKeyNames is not null) && (authZoneInfo.ZoneTransferTsigKeyNames.Count > 0))
  1568. {
  1569. if ((tsigAuthenticatedKeyName is null) || !authZoneInfo.ZoneTransferTsigKeyNames.ContainsKey(tsigAuthenticatedKeyName.ToLower()))
  1570. {
  1571. _log?.Write(remoteEP, protocol, "DNS Server refused a zone transfer request since the request is missing TSIG auth required by the zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1572. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.Refused, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1573. }
  1574. }
  1575. _log?.Write(remoteEP, protocol, "DNS Server received zone transfer request for zone: " + (authZoneInfo.Name == "" ? "<root>" : authZoneInfo.Name));
  1576. IReadOnlyList<DnsResourceRecord> xfrRecords;
  1577. if (request.Question[0].Type == DnsResourceRecordType.IXFR)
  1578. {
  1579. if ((request.Authority.Count == 1) && (request.Authority[0].Type == DnsResourceRecordType.SOA))
  1580. xfrRecords = _authZoneManager.QueryIncrementalZoneTransferRecords(request.Question[0].Name, request.Authority[0]);
  1581. else
  1582. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  1583. }
  1584. else
  1585. {
  1586. xfrRecords = _authZoneManager.QueryZoneTransferRecords(request.Question[0].Name);
  1587. }
  1588. DnsDatagram xfrResponse = new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, true, false, request.RecursionDesired, false, false, false, DnsResponseCode.NoError, request.Question, xfrRecords) { Tag = DnsServerResponseType.Authoritative };
  1589. xfrResponse = xfrResponse.Split();
  1590. return xfrResponse;
  1591. }
  1592. private async Task<DnsDatagram> ProcessAuthoritativeQueryAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, bool skipDnsAppAuthoritativeRequestHandlers)
  1593. {
  1594. DnsDatagram response = await AuthoritativeQueryAsync(request, remoteEP, protocol, isRecursionAllowed, skipDnsAppAuthoritativeRequestHandlers);
  1595. if (response is null)
  1596. return null;
  1597. bool reprocessResponse;
  1598. do
  1599. {
  1600. reprocessResponse = false;
  1601. if (response.RCODE == DnsResponseCode.NoError)
  1602. {
  1603. if (response.Answer.Count > 0)
  1604. {
  1605. DnsResourceRecordType questionType = request.Question[0].Type;
  1606. DnsResourceRecord lastRR = response.GetLastAnswerRecord();
  1607. if ((lastRR.Type != questionType) && (questionType != DnsResourceRecordType.ANY))
  1608. {
  1609. switch (lastRR.Type)
  1610. {
  1611. case DnsResourceRecordType.CNAME:
  1612. return await ProcessCNAMEAsync(request, remoteEP, response, isRecursionAllowed, protocol, false, skipDnsAppAuthoritativeRequestHandlers);
  1613. case DnsResourceRecordType.ANAME:
  1614. return await ProcessANAMEAsync(request, remoteEP, response, isRecursionAllowed, protocol, skipDnsAppAuthoritativeRequestHandlers);
  1615. }
  1616. }
  1617. }
  1618. else if (response.Authority.Count > 0)
  1619. {
  1620. DnsResourceRecord firstAuthority = response.FindFirstAuthorityRecord();
  1621. switch (firstAuthority.Type)
  1622. {
  1623. case DnsResourceRecordType.NS:
  1624. if (request.RecursionDesired && isRecursionAllowed)
  1625. {
  1626. //do forced recursive resolution using empty conditional forwarders; name servers will be provided via ResolverDnsCache
  1627. return await ProcessRecursiveQueryAsync(request, remoteEP, protocol, Array.Empty<DnsResourceRecord>(), _dnssecValidation, false, skipDnsAppAuthoritativeRequestHandlers);
  1628. }
  1629. break;
  1630. case DnsResourceRecordType.FWD:
  1631. //do conditional forwarding
  1632. return await ProcessRecursiveQueryAsync(request, remoteEP, protocol, response.Authority, _dnssecValidation, false, skipDnsAppAuthoritativeRequestHandlers);
  1633. case DnsResourceRecordType.APP:
  1634. response = await ProcessAPPAsync(request, remoteEP, response, isRecursionAllowed, protocol);
  1635. reprocessResponse = true;
  1636. break;
  1637. }
  1638. }
  1639. }
  1640. }
  1641. while (reprocessResponse);
  1642. return response;
  1643. }
  1644. private async Task<DnsDatagram> AuthoritativeQueryAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, bool skipDnsAppAuthoritativeRequestHandlers)
  1645. {
  1646. DnsDatagram response = _authZoneManager.Query(request, isRecursionAllowed);
  1647. if (response is not null)
  1648. {
  1649. response.Tag = DnsServerResponseType.Authoritative;
  1650. return response;
  1651. }
  1652. if (!skipDnsAppAuthoritativeRequestHandlers)
  1653. {
  1654. foreach (IDnsAuthoritativeRequestHandler requestHandler in _dnsApplicationManager.DnsAuthoritativeRequestHandlers)
  1655. {
  1656. try
  1657. {
  1658. DnsDatagram appResponse = await requestHandler.ProcessRequestAsync(request, remoteEP, protocol, isRecursionAllowed);
  1659. if (appResponse is not null)
  1660. {
  1661. if (appResponse.Tag is null)
  1662. appResponse.Tag = DnsServerResponseType.Authoritative;
  1663. return appResponse;
  1664. }
  1665. }
  1666. catch (Exception ex)
  1667. {
  1668. _log?.Write(remoteEP, protocol, ex);
  1669. }
  1670. }
  1671. }
  1672. return null;
  1673. }
  1674. private async Task<DnsDatagram> ProcessAPPAsync(DnsDatagram request, IPEndPoint remoteEP, DnsDatagram response, bool isRecursionAllowed, DnsTransportProtocol protocol)
  1675. {
  1676. DnsResourceRecord appResourceRecord = response.Authority[0];
  1677. DnsApplicationRecordData appRecord = appResourceRecord.RDATA as DnsApplicationRecordData;
  1678. if (_dnsApplicationManager.Applications.TryGetValue(appRecord.AppName, out DnsApplication application))
  1679. {
  1680. if (application.DnsAppRecordRequestHandlers.TryGetValue(appRecord.ClassPath, out IDnsAppRecordRequestHandler appRecordRequestHandler))
  1681. {
  1682. AuthZoneInfo zoneInfo = _authZoneManager.FindAuthZoneInfo(appResourceRecord.Name);
  1683. DnsDatagram appResponse = await appRecordRequestHandler.ProcessRequestAsync(request, remoteEP, protocol, isRecursionAllowed, zoneInfo.Name, appResourceRecord.Name, appResourceRecord.TTL, appRecord.Data);
  1684. if (appResponse is null)
  1685. {
  1686. DnsResponseCode rcode;
  1687. IReadOnlyList<DnsResourceRecord> authority = null;
  1688. if (zoneInfo.Type == AuthZoneType.Forwarder)
  1689. {
  1690. //return FWD response
  1691. rcode = DnsResponseCode.NoError;
  1692. if (!zoneInfo.Name.Equals(appResourceRecord.Name, StringComparison.OrdinalIgnoreCase))
  1693. {
  1694. AuthZone authZone = _authZoneManager.GetAuthZone(zoneInfo.Name, appResourceRecord.Name);
  1695. if (authZone is not null)
  1696. authority = authZone.QueryRecords(DnsResourceRecordType.FWD, false);
  1697. }
  1698. if ((authority is null) || (authority.Count == 0))
  1699. authority = zoneInfo.ApexZone.QueryRecords(DnsResourceRecordType.FWD, false);
  1700. }
  1701. else
  1702. {
  1703. //return NODATA/NXDOMAIN response
  1704. if ((request.Question[0].Name.Length == appResourceRecord.Name.Length) || appResourceRecord.Name.StartsWith('*'))
  1705. rcode = DnsResponseCode.NoError;
  1706. else
  1707. rcode = DnsResponseCode.NxDomain;
  1708. authority = zoneInfo.GetApexRecords(DnsResourceRecordType.SOA);
  1709. }
  1710. return new DnsDatagram(request.Identifier, true, request.OPCODE, false, false, request.RecursionDesired, isRecursionAllowed, false, false, rcode, request.Question, null, authority) { Tag = DnsServerResponseType.Authoritative };
  1711. }
  1712. else
  1713. {
  1714. if (appResponse.AuthoritativeAnswer)
  1715. appResponse.Tag = DnsServerResponseType.Authoritative;
  1716. return appResponse; //return app response
  1717. }
  1718. }
  1719. else
  1720. {
  1721. _log?.Write(remoteEP, protocol, "DNS request handler '" + appRecord.ClassPath + "' was not found in the application '" + appRecord.AppName + "': " + appResourceRecord.Name);
  1722. }
  1723. }
  1724. else
  1725. {
  1726. _log?.Write(remoteEP, protocol, "DNS application '" + appRecord.AppName + "' was not found: " + appResourceRecord.Name);
  1727. }
  1728. //return server failure response with SOA
  1729. {
  1730. AuthZoneInfo zoneInfo = _authZoneManager.FindAuthZoneInfo(request.Question[0].Name);
  1731. IReadOnlyList<DnsResourceRecord> authority = zoneInfo.GetApexRecords(DnsResourceRecordType.SOA);
  1732. return new DnsDatagram(request.Identifier, true, request.OPCODE, false, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.ServerFailure, request.Question, null, authority) { Tag = DnsServerResponseType.Authoritative };
  1733. }
  1734. }
  1735. private async Task<DnsDatagram> ProcessCNAMEAsync(DnsDatagram request, IPEndPoint remoteEP, DnsDatagram response, bool isRecursionAllowed, DnsTransportProtocol protocol, bool cacheRefreshOperation, bool skipDnsAppAuthoritativeRequestHandlers)
  1736. {
  1737. List<DnsResourceRecord> newAnswer = new List<DnsResourceRecord>(response.Answer.Count + 4);
  1738. newAnswer.AddRange(response.Answer);
  1739. //copying NSEC/NSEC3 for for wildcard answers
  1740. List<DnsResourceRecord> newAuthority = new List<DnsResourceRecord>(2);
  1741. foreach (DnsResourceRecord record in response.Authority)
  1742. {
  1743. switch (record.Type)
  1744. {
  1745. case DnsResourceRecordType.NSEC:
  1746. case DnsResourceRecordType.NSEC3:
  1747. newAuthority.Add(record);
  1748. break;
  1749. case DnsResourceRecordType.RRSIG:
  1750. switch ((record.RDATA as DnsRRSIGRecordData).TypeCovered)
  1751. {
  1752. case DnsResourceRecordType.NSEC:
  1753. case DnsResourceRecordType.NSEC3:
  1754. newAuthority.Add(record);
  1755. break;
  1756. }
  1757. break;
  1758. }
  1759. }
  1760. DnsDatagram lastResponse = response;
  1761. bool isAuthoritativeAnswer = response.AuthoritativeAnswer;
  1762. DnsResourceRecord lastRR = response.GetLastAnswerRecord();
  1763. EDnsOption[] eDnsClientSubnetOption = null;
  1764. DnsDatagram newResponse = null;
  1765. if (_eDnsClientSubnet)
  1766. {
  1767. EDnsClientSubnetOptionData requestECS = request.GetEDnsClientSubnetOption();
  1768. if (requestECS is not null)
  1769. eDnsClientSubnetOption = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EDNS_CLIENT_SUBNET, requestECS) };
  1770. }
  1771. int queryCount = 0;
  1772. do
  1773. {
  1774. string cnameDomain = (lastRR.RDATA as DnsCNAMERecordData).Domain;
  1775. if (lastRR.Name.Equals(cnameDomain, StringComparison.OrdinalIgnoreCase))
  1776. break; //loop detected
  1777. DnsDatagram newRequest = new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { new DnsQuestionRecord(cnameDomain, request.Question[0].Type, request.Question[0].Class) }, null, null, null, _udpPayloadSize, request.DnssecOk ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, eDnsClientSubnetOption);
  1778. //query authoritative zone first
  1779. newResponse = await AuthoritativeQueryAsync(newRequest, remoteEP, protocol, isRecursionAllowed, skipDnsAppAuthoritativeRequestHandlers);
  1780. if (newResponse is null)
  1781. {
  1782. //not found in auth zone
  1783. if (newRequest.RecursionDesired && isRecursionAllowed)
  1784. {
  1785. //do recursion
  1786. newResponse = await RecursiveResolveAsync(newRequest, remoteEP, null, _dnssecValidation, false, cacheRefreshOperation, skipDnsAppAuthoritativeRequestHandlers);
  1787. isAuthoritativeAnswer = false;
  1788. }
  1789. else
  1790. {
  1791. //break since no recursion allowed/desired
  1792. break;
  1793. }
  1794. }
  1795. else if ((newResponse.Answer.Count > 0) && (newResponse.GetLastAnswerRecord().Type == DnsResourceRecordType.ANAME))
  1796. {
  1797. newResponse = await ProcessANAMEAsync(request, remoteEP, newResponse, isRecursionAllowed, protocol, skipDnsAppAuthoritativeRequestHandlers);
  1798. }
  1799. else if ((newResponse.Answer.Count == 0) && (newResponse.Authority.Count > 0))
  1800. {
  1801. //found delegated/forwarded zone
  1802. DnsResourceRecord firstAuthority = newResponse.FindFirstAuthorityRecord();
  1803. switch (firstAuthority.Type)
  1804. {
  1805. case DnsResourceRecordType.NS:
  1806. if (newRequest.RecursionDesired && isRecursionAllowed)
  1807. {
  1808. //do forced recursive resolution using empty conditional forwarders; name servers will be provided via ResolveDnsCache
  1809. newResponse = await RecursiveResolveAsync(newRequest, remoteEP, Array.Empty<DnsResourceRecord>(), _dnssecValidation, false, false, skipDnsAppAuthoritativeRequestHandlers);
  1810. isAuthoritativeAnswer = false;
  1811. }
  1812. break;
  1813. case DnsResourceRecordType.FWD:
  1814. //do conditional forwarding
  1815. newResponse = await RecursiveResolveAsync(newRequest, remoteEP, newResponse.Authority, _dnssecValidation, false, false, skipDnsAppAuthoritativeRequestHandlers);
  1816. isAuthoritativeAnswer = false;
  1817. break;
  1818. case DnsResourceRecordType.APP:
  1819. newResponse = await ProcessAPPAsync(newRequest, remoteEP, newResponse, isRecursionAllowed, protocol);
  1820. break;
  1821. }
  1822. }
  1823. //check last response
  1824. if (newResponse.Answer.Count == 0)
  1825. break; //cannot proceed to resolve further
  1826. lastRR = newResponse.GetLastAnswerRecord();
  1827. if (lastRR.Type != DnsResourceRecordType.CNAME)
  1828. {
  1829. newAnswer.AddRange(newResponse.Answer);
  1830. break; //cname was resolved
  1831. }
  1832. bool foundRepeat = false;
  1833. foreach (DnsResourceRecord answerRecord in newAnswer)
  1834. {
  1835. if (answerRecord.Type != DnsResourceRecordType.CNAME)
  1836. continue;
  1837. if (answerRecord.RDATA.Equals(lastRR.RDATA))
  1838. {
  1839. foundRepeat = true;
  1840. break;
  1841. }
  1842. }
  1843. if (foundRepeat)
  1844. break; //loop detected
  1845. newAnswer.AddRange(newResponse.Answer);
  1846. lastResponse = newResponse;
  1847. }
  1848. while (++queryCount < MAX_CNAME_HOPS);
  1849. DnsResponseCode rcode;
  1850. IReadOnlyList<DnsResourceRecord> authority;
  1851. IReadOnlyList<DnsResourceRecord> additional;
  1852. if (newResponse is null)
  1853. {
  1854. //no recursion available
  1855. rcode = DnsResponseCode.NoError;
  1856. if (newAuthority.Count == 0)
  1857. {
  1858. authority = lastResponse.Authority;
  1859. }
  1860. else
  1861. {
  1862. newAuthority.AddRange(lastResponse.Authority);
  1863. authority = newAuthority;
  1864. }
  1865. additional = lastResponse.Additional;
  1866. }
  1867. else
  1868. {
  1869. rcode = newResponse.RCODE;
  1870. if (newAuthority.Count == 0)
  1871. {
  1872. authority = newResponse.Authority;
  1873. }
  1874. else
  1875. {
  1876. newAuthority.AddRange(newResponse.Authority);
  1877. authority = newAuthority;
  1878. }
  1879. additional = newResponse.Additional;
  1880. }
  1881. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, isAuthoritativeAnswer, false, request.RecursionDesired, isRecursionAllowed, false, request.CheckingDisabled, rcode, request.Question, newAnswer, authority, additional) { Tag = response.Tag };
  1882. }
  1883. private async Task<DnsDatagram> ProcessANAMEAsync(DnsDatagram request, IPEndPoint remoteEP, DnsDatagram response, bool isRecursionAllowed, DnsTransportProtocol protocol, bool skipDnsAppAuthoritativeRequestHandlers)
  1884. {
  1885. EDnsOption[] eDnsClientSubnetOption = null;
  1886. if (_eDnsClientSubnet)
  1887. {
  1888. EDnsClientSubnetOptionData requestECS = request.GetEDnsClientSubnetOption();
  1889. if (requestECS is not null)
  1890. eDnsClientSubnetOption = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EDNS_CLIENT_SUBNET, requestECS) };
  1891. }
  1892. Queue<Task<IReadOnlyList<DnsResourceRecord>>> resolveQueue = new Queue<Task<IReadOnlyList<DnsResourceRecord>>>();
  1893. async Task<IReadOnlyList<DnsResourceRecord>> ResolveANAMEAsync(DnsResourceRecord anameRR, int queryCount = 0)
  1894. {
  1895. string lastDomain = (anameRR.RDATA as DnsANAMERecordData).Domain;
  1896. if (anameRR.Name.Equals(lastDomain, StringComparison.OrdinalIgnoreCase))
  1897. return null; //loop detected
  1898. do
  1899. {
  1900. DnsDatagram newRequest = new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { new DnsQuestionRecord(lastDomain, request.Question[0].Type, request.Question[0].Class) }, null, null, null, _udpPayloadSize, request.DnssecOk ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, eDnsClientSubnetOption);
  1901. //query authoritative zone first
  1902. DnsDatagram newResponse = await AuthoritativeQueryAsync(newRequest, remoteEP, protocol, isRecursionAllowed, skipDnsAppAuthoritativeRequestHandlers);
  1903. if (newResponse is null)
  1904. {
  1905. //not found in auth zone; do recursion
  1906. newResponse = await RecursiveResolveAsync(newRequest, remoteEP, null, _dnssecValidation, false, false, skipDnsAppAuthoritativeRequestHandlers);
  1907. }
  1908. else if ((newResponse.Answer.Count == 0) && (newResponse.Authority.Count > 0))
  1909. {
  1910. //found delegated/forwarded zone
  1911. DnsResourceRecord firstAuthority = newResponse.FindFirstAuthorityRecord();
  1912. switch (firstAuthority.Type)
  1913. {
  1914. case DnsResourceRecordType.NS:
  1915. //do forced recursive resolution using empty conditional forwarders; name servers will be provided via ResolverDnsCache
  1916. newResponse = await RecursiveResolveAsync(newRequest, remoteEP, Array.Empty<DnsResourceRecord>(), _dnssecValidation, false, false, skipDnsAppAuthoritativeRequestHandlers);
  1917. break;
  1918. case DnsResourceRecordType.FWD:
  1919. //do conditional forwarding
  1920. newResponse = await RecursiveResolveAsync(newRequest, remoteEP, newResponse.Authority, _dnssecValidation, false, false, skipDnsAppAuthoritativeRequestHandlers);
  1921. break;
  1922. case DnsResourceRecordType.APP:
  1923. newResponse = await ProcessAPPAsync(newRequest, remoteEP, newResponse, isRecursionAllowed, protocol);
  1924. break;
  1925. }
  1926. }
  1927. //check new response
  1928. if (newResponse.RCODE != DnsResponseCode.NoError)
  1929. return null; //cannot proceed to resolve further
  1930. if (newResponse.Answer.Count == 0)
  1931. return Array.Empty<DnsResourceRecord>(); //NO DATA
  1932. DnsResourceRecordType questionType = request.Question[0].Type;
  1933. DnsResourceRecord lastRR = newResponse.GetLastAnswerRecord();
  1934. if (lastRR.Type == questionType)
  1935. {
  1936. //found final answer
  1937. List<DnsResourceRecord> answers = new List<DnsResourceRecord>();
  1938. foreach (DnsResourceRecord answer in newResponse.Answer)
  1939. {
  1940. if (answer.Type != questionType)
  1941. continue;
  1942. if (anameRR.TTL < answer.TTL)
  1943. answers.Add(new DnsResourceRecord(anameRR.Name, answer.Type, answer.Class, anameRR.TTL, answer.RDATA));
  1944. else
  1945. answers.Add(new DnsResourceRecord(anameRR.Name, answer.Type, answer.Class, answer.TTL, answer.RDATA));
  1946. }
  1947. return answers;
  1948. }
  1949. if (lastRR.Type == DnsResourceRecordType.ANAME)
  1950. {
  1951. if (newResponse.Answer.Count == 1)
  1952. {
  1953. lastDomain = (lastRR.RDATA as DnsANAMERecordData).Domain;
  1954. }
  1955. else
  1956. {
  1957. //resolve multiple ANAME records async
  1958. queryCount++; //increment since one query was done already
  1959. foreach (DnsResourceRecord newAnswer in newResponse.Answer)
  1960. resolveQueue.Enqueue(ResolveANAMEAsync(newAnswer, queryCount));
  1961. return Array.Empty<DnsResourceRecord>();
  1962. }
  1963. }
  1964. else if (lastRR.Type == DnsResourceRecordType.CNAME)
  1965. {
  1966. lastDomain = (lastRR.RDATA as DnsCNAMERecordData).Domain;
  1967. }
  1968. else
  1969. {
  1970. //aname/cname was resolved, but no answer found
  1971. return Array.Empty<DnsResourceRecord>();
  1972. }
  1973. }
  1974. while (++queryCount < MAX_CNAME_HOPS);
  1975. //max hops limit crossed
  1976. return null;
  1977. }
  1978. List<DnsResourceRecord> responseAnswer = new List<DnsResourceRecord>();
  1979. foreach (DnsResourceRecord answer in response.Answer)
  1980. {
  1981. if (answer.Type == DnsResourceRecordType.ANAME)
  1982. {
  1983. resolveQueue.Enqueue(ResolveANAMEAsync(answer));
  1984. }
  1985. else
  1986. {
  1987. if (resolveQueue.Count == 0)
  1988. responseAnswer.Add(answer);
  1989. }
  1990. }
  1991. bool foundErrors = false;
  1992. while (resolveQueue.Count > 0)
  1993. {
  1994. IReadOnlyList<DnsResourceRecord> records = await resolveQueue.Dequeue();
  1995. if (records is null)
  1996. foundErrors = true;
  1997. else if (records.Count > 0)
  1998. responseAnswer.AddRange(records);
  1999. }
  2000. DnsResponseCode rcode = DnsResponseCode.NoError;
  2001. IReadOnlyList<DnsResourceRecord> authority = null;
  2002. if (responseAnswer.Count == 0)
  2003. {
  2004. if (foundErrors)
  2005. {
  2006. rcode = DnsResponseCode.ServerFailure;
  2007. }
  2008. else
  2009. {
  2010. authority = response.Authority;
  2011. //update last used on
  2012. DateTime utcNow = DateTime.UtcNow;
  2013. foreach (DnsResourceRecord record in authority)
  2014. record.GetAuthRecordInfo().LastUsedOn = utcNow;
  2015. }
  2016. }
  2017. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, true, false, request.RecursionDesired, isRecursionAllowed, false, false, rcode, request.Question, responseAnswer, authority, null) { Tag = response.Tag };
  2018. }
  2019. private DnsDatagram ProcessBlockedQuery(DnsDatagram request)
  2020. {
  2021. DnsDatagram response = _blockedZoneManager.Query(request);
  2022. if (response is null)
  2023. {
  2024. //domain not blocked in blocked zone
  2025. response = _blockListZoneManager.Query(request); //check in block list zone
  2026. if (response is null)
  2027. return null; //domain not blocked in block list zone
  2028. //domain is blocked in block list zone
  2029. response.Tag = DnsServerResponseType.Blocked;
  2030. return response;
  2031. }
  2032. else
  2033. {
  2034. //domain is blocked in blocked zone
  2035. DnsQuestionRecord question = request.Question[0];
  2036. string GetBlockedDomain()
  2037. {
  2038. DnsResourceRecord firstAuthority = response.FindFirstAuthorityRecord();
  2039. if ((firstAuthority is not null) && (firstAuthority.Type == DnsResourceRecordType.SOA))
  2040. return firstAuthority.Name;
  2041. else
  2042. return question.Name;
  2043. }
  2044. if (_allowTxtBlockingReport && (question.Type == DnsResourceRecordType.TXT))
  2045. {
  2046. //return meta data
  2047. string blockedDomain = GetBlockedDomain();
  2048. IReadOnlyList<DnsResourceRecord> answer = new DnsResourceRecord[] { new DnsResourceRecord(question.Name, DnsResourceRecordType.TXT, question.Class, 60, new DnsTXTRecordData("source=blocked-zone; domain=" + blockedDomain)) };
  2049. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NoError, request.Question, answer) { Tag = DnsServerResponseType.Blocked };
  2050. }
  2051. else
  2052. {
  2053. string blockedDomain = null;
  2054. EDnsOption[] options = null;
  2055. if (_allowTxtBlockingReport && (request.EDNS is not null))
  2056. {
  2057. blockedDomain = GetBlockedDomain();
  2058. options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.Blocked, "source=blocked-zone; domain=" + blockedDomain)) };
  2059. }
  2060. IReadOnlyCollection<DnsARecordData> aRecords;
  2061. IReadOnlyCollection<DnsAAAARecordData> aaaaRecords;
  2062. switch (_blockingType)
  2063. {
  2064. case DnsServerBlockingType.AnyAddress:
  2065. aRecords = _aRecords;
  2066. aaaaRecords = _aaaaRecords;
  2067. break;
  2068. case DnsServerBlockingType.CustomAddress:
  2069. aRecords = _customBlockingARecords;
  2070. aaaaRecords = _customBlockingAAAARecords;
  2071. break;
  2072. case DnsServerBlockingType.NxDomain:
  2073. if (blockedDomain is null)
  2074. blockedDomain = GetBlockedDomain();
  2075. string parentDomain = AuthZoneManager.GetParentZone(blockedDomain);
  2076. if (parentDomain is null)
  2077. parentDomain = string.Empty;
  2078. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NxDomain, request.Question, null, new DnsResourceRecord[] { new DnsResourceRecord(parentDomain, DnsResourceRecordType.SOA, question.Class, 60, _blockedZoneManager.DnsSOARecord) }, null, request.EDNS is null ? ushort.MinValue : _udpPayloadSize, EDnsHeaderFlags.None, options) { Tag = DnsServerResponseType.Blocked };
  2079. default:
  2080. throw new InvalidOperationException();
  2081. }
  2082. IReadOnlyList<DnsResourceRecord> answer;
  2083. IReadOnlyList<DnsResourceRecord> authority = null;
  2084. switch (question.Type)
  2085. {
  2086. case DnsResourceRecordType.A:
  2087. {
  2088. List<DnsResourceRecord> rrList = new List<DnsResourceRecord>(aRecords.Count);
  2089. foreach (DnsARecordData record in aRecords)
  2090. rrList.Add(new DnsResourceRecord(question.Name, DnsResourceRecordType.A, question.Class, 60, record));
  2091. answer = rrList;
  2092. }
  2093. break;
  2094. case DnsResourceRecordType.AAAA:
  2095. {
  2096. List<DnsResourceRecord> rrList = new List<DnsResourceRecord>(aaaaRecords.Count);
  2097. foreach (DnsAAAARecordData record in aaaaRecords)
  2098. rrList.Add(new DnsResourceRecord(question.Name, DnsResourceRecordType.AAAA, question.Class, 60, record));
  2099. answer = rrList;
  2100. }
  2101. break;
  2102. default:
  2103. answer = response.Answer;
  2104. authority = response.Authority;
  2105. break;
  2106. }
  2107. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, false, false, false, DnsResponseCode.NoError, request.Question, answer, authority, null, request.EDNS is null ? ushort.MinValue : _udpPayloadSize, EDnsHeaderFlags.None, options) { Tag = DnsServerResponseType.Blocked };
  2108. }
  2109. }
  2110. }
  2111. private async Task<DnsDatagram> ProcessRecursiveQueryAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, IReadOnlyList<DnsResourceRecord> conditionalForwarders, bool dnssecValidation, bool cacheRefreshOperation, bool skipDnsAppAuthoritativeRequestHandlers)
  2112. {
  2113. bool inAllowedZone;
  2114. if (cacheRefreshOperation)
  2115. {
  2116. //cache refresh operation should be able to refresh all the records in cache
  2117. //this is since a blocked CNAME record could still be used by an allowed domain name and so must resolve
  2118. inAllowedZone = true;
  2119. }
  2120. else if (!_enableBlocking)
  2121. {
  2122. inAllowedZone = true;
  2123. }
  2124. else
  2125. {
  2126. inAllowedZone = _allowedZoneManager.IsAllowed(request) || _blockListZoneManager.IsAllowed(request);
  2127. if (!inAllowedZone)
  2128. {
  2129. //check in blocked zone and block list zone
  2130. DnsDatagram blockedResponse = ProcessBlockedQuery(request);
  2131. if (blockedResponse is not null)
  2132. return blockedResponse;
  2133. }
  2134. }
  2135. DnsDatagram response = await RecursiveResolveAsync(request, remoteEP, conditionalForwarders, dnssecValidation, false, cacheRefreshOperation, skipDnsAppAuthoritativeRequestHandlers);
  2136. if (response.Answer.Count > 0)
  2137. {
  2138. DnsResourceRecordType questionType = request.Question[0].Type;
  2139. DnsResourceRecord lastRR = response.GetLastAnswerRecord();
  2140. if ((lastRR.Type != questionType) && (lastRR.Type == DnsResourceRecordType.CNAME) && (questionType != DnsResourceRecordType.ANY))
  2141. response = await ProcessCNAMEAsync(request, remoteEP, response, true, protocol, cacheRefreshOperation, skipDnsAppAuthoritativeRequestHandlers);
  2142. if (!inAllowedZone)
  2143. {
  2144. //check for CNAME cloaking
  2145. for (int i = 0; i < response.Answer.Count; i++)
  2146. {
  2147. DnsResourceRecord record = response.Answer[i];
  2148. if (record.Type != DnsResourceRecordType.CNAME)
  2149. break; //no further CNAME records exists
  2150. DnsDatagram newRequest = new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, true, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { new DnsQuestionRecord((record.RDATA as DnsCNAMERecordData).Domain, request.Question[0].Type, request.Question[0].Class) }, null, null, null, _udpPayloadSize);
  2151. //check allowed zone
  2152. inAllowedZone = _allowedZoneManager.IsAllowed(newRequest) || _blockListZoneManager.IsAllowed(newRequest);
  2153. if (inAllowedZone)
  2154. break; //CNAME is in allowed zone
  2155. //check blocked zone and block list zone
  2156. DnsDatagram blockedResponse = ProcessBlockedQuery(newRequest);
  2157. if (blockedResponse is not null)
  2158. {
  2159. //found cname cloaking
  2160. List<DnsResourceRecord> answer = new List<DnsResourceRecord>();
  2161. //copy current and previous CNAME records
  2162. for (int j = 0; j <= i; j++)
  2163. answer.Add(response.Answer[j]);
  2164. //copy last response answers
  2165. answer.AddRange(blockedResponse.Answer);
  2166. //include blocked response additional section to pass on Extended DNS Errors
  2167. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, true, true, false, false, blockedResponse.RCODE, request.Question, answer, blockedResponse.Authority, blockedResponse.Additional) { Tag = blockedResponse.Tag };
  2168. }
  2169. }
  2170. }
  2171. }
  2172. if (response.Tag is null)
  2173. {
  2174. if (response.IsBlockedResponse())
  2175. response.Tag = DnsServerResponseType.UpstreamBlocked;
  2176. }
  2177. else if ((DnsServerResponseType)response.Tag == DnsServerResponseType.Cached)
  2178. {
  2179. if (response.IsBlockedResponse())
  2180. response.Tag = DnsServerResponseType.CacheBlocked;
  2181. }
  2182. return response;
  2183. }
  2184. private async Task<DnsDatagram> RecursiveResolveAsync(DnsDatagram request, IPEndPoint remoteEP, IReadOnlyList<DnsResourceRecord> conditionalForwarders, bool dnssecValidation, bool cachePrefetchOperation, bool cacheRefreshOperation, bool skipDnsAppAuthoritativeRequestHandlers)
  2185. {
  2186. DnsQuestionRecord question = request.Question[0];
  2187. NetworkAddress eDnsClientSubnet = null;
  2188. bool conditionalForwardingClientSubnet = false;
  2189. if (_eDnsClientSubnet)
  2190. {
  2191. EDnsClientSubnetOptionData requestECS = request.GetEDnsClientSubnetOption();
  2192. if (requestECS is null)
  2193. {
  2194. if (!NetUtilities.IsPrivateIP(remoteEP.Address))
  2195. {
  2196. //set shadow ECS option
  2197. switch (remoteEP.AddressFamily)
  2198. {
  2199. case AddressFamily.InterNetwork:
  2200. eDnsClientSubnet = new NetworkAddress(remoteEP.Address, _eDnsClientSubnetIPv4PrefixLength);
  2201. request.SetShadowEDnsClientSubnetOption(eDnsClientSubnet);
  2202. break;
  2203. case AddressFamily.InterNetworkV6:
  2204. eDnsClientSubnet = new NetworkAddress(remoteEP.Address, _eDnsClientSubnetIPv6PrefixLength);
  2205. request.SetShadowEDnsClientSubnetOption(eDnsClientSubnet);
  2206. break;
  2207. default:
  2208. request.ShadowHideEDnsClientSubnetOption();
  2209. break;
  2210. }
  2211. }
  2212. }
  2213. else if ((requestECS.Family != EDnsClientSubnetAddressFamily.IPv4) && (requestECS.Family != EDnsClientSubnetAddressFamily.IPv6))
  2214. {
  2215. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, true, false, false, DnsResponseCode.FormatError, request.Question) { Tag = DnsServerResponseType.Authoritative };
  2216. }
  2217. else if (requestECS.ConditionalForwardingClientSubnet)
  2218. {
  2219. conditionalForwardingClientSubnet = true;
  2220. eDnsClientSubnet = new NetworkAddress(requestECS.Address, requestECS.SourcePrefixLength);
  2221. }
  2222. else if ((requestECS.SourcePrefixLength == 0) || NetUtilities.IsPrivateIP(requestECS.Address))
  2223. {
  2224. //disable ECS option
  2225. request.ShadowHideEDnsClientSubnetOption();
  2226. }
  2227. else
  2228. {
  2229. //use ECS from client request
  2230. switch (requestECS.Family)
  2231. {
  2232. case EDnsClientSubnetAddressFamily.IPv4:
  2233. eDnsClientSubnet = new NetworkAddress(requestECS.Address, Math.Min(requestECS.SourcePrefixLength, _eDnsClientSubnetIPv4PrefixLength));
  2234. request.SetShadowEDnsClientSubnetOption(eDnsClientSubnet);
  2235. break;
  2236. case EDnsClientSubnetAddressFamily.IPv6:
  2237. eDnsClientSubnet = new NetworkAddress(requestECS.Address, Math.Min(requestECS.SourcePrefixLength, _eDnsClientSubnetIPv6PrefixLength));
  2238. request.SetShadowEDnsClientSubnetOption(eDnsClientSubnet);
  2239. break;
  2240. }
  2241. }
  2242. }
  2243. else
  2244. {
  2245. EDnsClientSubnetOptionData requestECS = request.GetEDnsClientSubnetOption();
  2246. if (requestECS is not null)
  2247. {
  2248. conditionalForwardingClientSubnet = requestECS.ConditionalForwardingClientSubnet;
  2249. if (conditionalForwardingClientSubnet)
  2250. eDnsClientSubnet = new NetworkAddress(requestECS.Address, requestECS.SourcePrefixLength);
  2251. else
  2252. request.ShadowHideEDnsClientSubnetOption(); //hide ECS option
  2253. }
  2254. }
  2255. if (!cachePrefetchOperation && !cacheRefreshOperation)
  2256. {
  2257. //query cache zone to see if answer available
  2258. DnsDatagram cacheResponse = QueryCache(request, false);
  2259. if (cacheResponse is not null)
  2260. {
  2261. if (_cachePrefetchTrigger > 0)
  2262. {
  2263. //inspect response TTL values to decide if prefetch trigger is needed
  2264. foreach (DnsResourceRecord answer in cacheResponse.Answer)
  2265. {
  2266. if ((answer.OriginalTtlValue >= _cachePrefetchEligibility) && (answer.TTL <= _cachePrefetchTrigger))
  2267. {
  2268. //trigger prefetch async
  2269. _ = PrefetchCacheAsync(request, remoteEP, conditionalForwarders);
  2270. break;
  2271. }
  2272. }
  2273. }
  2274. return cacheResponse;
  2275. }
  2276. }
  2277. //recursion with locking
  2278. TaskCompletionSource<RecursiveResolveResponse> resolverTaskCompletionSource = new TaskCompletionSource<RecursiveResolveResponse>();
  2279. Task<RecursiveResolveResponse> resolverTask = _resolverTasks.GetOrAdd(GetResolverQueryKey(question, eDnsClientSubnet), resolverTaskCompletionSource.Task);
  2280. if (resolverTask.Equals(resolverTaskCompletionSource.Task))
  2281. {
  2282. //got new resolver task added so question is not being resolved; do recursive resolution in another task on resolver thread pool
  2283. _ = Task.Factory.StartNew(delegate ()
  2284. {
  2285. return RecursiveResolveAsync(question, eDnsClientSubnet, conditionalForwardingClientSubnet, conditionalForwarders, dnssecValidation, cachePrefetchOperation, cacheRefreshOperation, skipDnsAppAuthoritativeRequestHandlers, resolverTaskCompletionSource);
  2286. }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, _resolverTaskScheduler);
  2287. }
  2288. //request is being recursively resolved by another thread
  2289. if (cachePrefetchOperation)
  2290. return null; //return null as prefetch worker thread does not need valid response and thus does not need to wait
  2291. DateTime resolverWaitStartTime = DateTime.UtcNow;
  2292. //wait till short timeout for response
  2293. if (await Task.WhenAny(resolverTask, Task.Delay(SERVE_STALE_WAIT_TIME)) == resolverTask) //1.8 sec wait as per draft-ietf-dnsop-serve-stale-04
  2294. {
  2295. //resolver signaled
  2296. RecursiveResolveResponse response = await resolverTask;
  2297. if (response is not null)
  2298. return PrepareRecursiveResolveResponse(request, response);
  2299. //resolver had exception and no stale record was found
  2300. }
  2301. else
  2302. {
  2303. //wait timed out
  2304. if (_serveStale)
  2305. {
  2306. //query cache zone to return stale answer (if available) as per draft-ietf-dnsop-serve-stale-04
  2307. DnsDatagram staleResponse = QueryCache(request, true);
  2308. if (staleResponse is not null)
  2309. return staleResponse;
  2310. }
  2311. //wait till full timeout before responding as ServerFailure
  2312. int timeout = Convert.ToInt32(_clientTimeout - (DateTime.UtcNow - resolverWaitStartTime).TotalMilliseconds);
  2313. if (timeout > 0)
  2314. {
  2315. if (await Task.WhenAny(resolverTask, Task.Delay(timeout)) == resolverTask)
  2316. {
  2317. //resolver signaled
  2318. RecursiveResolveResponse response = await resolverTask;
  2319. if (response is not null)
  2320. return PrepareRecursiveResolveResponse(request, response);
  2321. }
  2322. //no response available from resolver or resolver had exception and no stale record was found
  2323. }
  2324. }
  2325. //no response available; respond with ServerFailure
  2326. EDnsOption[] options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.Other, "Waiting for resolver")) };
  2327. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, request.RecursionDesired, true, false, false, DnsResponseCode.ServerFailure, request.Question, null, null, null, _udpPayloadSize, request.DnssecOk ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, options);
  2328. }
  2329. private async Task RecursiveResolveAsync(DnsQuestionRecord question, NetworkAddress eDnsClientSubnet, bool conditionalForwardingClientSubnet, IReadOnlyList<DnsResourceRecord> conditionalForwarders, bool dnssecValidation, bool cachePrefetchOperation, bool cacheRefreshOperation, bool skipDnsAppAuthoritativeRequestHandlers, TaskCompletionSource<RecursiveResolveResponse> taskCompletionSource)
  2330. {
  2331. try
  2332. {
  2333. //recursive resolve and update cache
  2334. IDnsCache dnsCache;
  2335. if (cachePrefetchOperation || cacheRefreshOperation)
  2336. dnsCache = new ResolverPrefetchDnsCache(_dnsApplicationManager, _authZoneManager, _cacheZoneManager, _log, skipDnsAppAuthoritativeRequestHandlers, question);
  2337. else if (skipDnsAppAuthoritativeRequestHandlers || conditionalForwardingClientSubnet)
  2338. dnsCache = new ResolverDnsCache(_dnsApplicationManager, _authZoneManager, _cacheZoneManager, _log, true); //to prevent request reaching apps again
  2339. else
  2340. dnsCache = _dnsCache;
  2341. //check for this-server
  2342. if ((conditionalForwarders is not null) && (conditionalForwarders.Count == 1) && (conditionalForwarders[0].RDATA is DnsForwarderRecordData fwd) && fwd.Forwarder.Equals("this-server", StringComparison.OrdinalIgnoreCase))
  2343. {
  2344. //resolve directly with DNSSEC validation preference
  2345. conditionalForwarders = null;
  2346. dnssecValidation = fwd.DnssecValidation;
  2347. }
  2348. DnsDatagram response;
  2349. if ((conditionalForwarders is not null) && (conditionalForwarders.Count > 0))
  2350. {
  2351. //check for forwarder name server resolution
  2352. foreach (DnsResourceRecord conditionalForwarder in conditionalForwarders)
  2353. {
  2354. if (conditionalForwarder.Type != DnsResourceRecordType.FWD)
  2355. continue;
  2356. DnsForwarderRecordData forwarder = conditionalForwarder.RDATA as DnsForwarderRecordData;
  2357. if (forwarder.Forwarder.Equals("this-server", StringComparison.OrdinalIgnoreCase))
  2358. continue;
  2359. NetProxy proxy = forwarder.Proxy;
  2360. if (proxy is null)
  2361. proxy = _proxy;
  2362. if (proxy is null)
  2363. {
  2364. //recursive resolve name server when proxy is null else let proxy resolve it
  2365. if (forwarder.NameServer.IsIPEndPointStale) //refresh forwarder IPEndPoint if stale
  2366. await forwarder.NameServer.RecursiveResolveIPAddressAsync(dnsCache, null, _preferIPv6, _udpPayloadSize, _randomizeName, _resolverRetries, _resolverTimeout);
  2367. }
  2368. }
  2369. if (conditionalForwarders.Count == 1)
  2370. {
  2371. DnsResourceRecord conditionalForwarder = conditionalForwarders[0];
  2372. response = await ConditionalForwarderResolveAsync(question, eDnsClientSubnet, conditionalForwardingClientSubnet, dnsCache, conditionalForwarder.RDATA as DnsForwarderRecordData, conditionalForwarder.Name);
  2373. }
  2374. else
  2375. {
  2376. using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource())
  2377. {
  2378. CancellationToken cancellationToken = cancellationTokenSource.Token;
  2379. List<Task<DnsDatagram>> tasks = new List<Task<DnsDatagram>>(conditionalForwarders.Count);
  2380. //start worker tasks
  2381. foreach (DnsResourceRecord conditionalForwarder in conditionalForwarders)
  2382. {
  2383. if (conditionalForwarder.Type != DnsResourceRecordType.FWD)
  2384. continue;
  2385. DnsForwarderRecordData forwarder = conditionalForwarder.RDATA as DnsForwarderRecordData;
  2386. if (forwarder.Forwarder.Equals("this-server", StringComparison.OrdinalIgnoreCase))
  2387. continue;
  2388. tasks.Add(Task.Factory.StartNew(delegate ()
  2389. {
  2390. return ConditionalForwarderResolveAsync(question, eDnsClientSubnet, conditionalForwardingClientSubnet, dnsCache, forwarder, conditionalForwarder.Name, cancellationToken);
  2391. }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Current).Unwrap());
  2392. }
  2393. //wait for first positive response, or for all tasks to fault
  2394. response = null;
  2395. DnsDatagram lastResponse = null;
  2396. Exception lastException = null;
  2397. while (tasks.Count > 0)
  2398. {
  2399. Task<DnsDatagram> completedTask = await Task.WhenAny(tasks);
  2400. if (completedTask.Status == TaskStatus.RanToCompletion)
  2401. {
  2402. //resolver task complete
  2403. DnsDatagram taskResponse = await completedTask; //await to get response
  2404. bool foundResponse = false;
  2405. switch (taskResponse.RCODE)
  2406. {
  2407. case DnsResponseCode.NoError:
  2408. case DnsResponseCode.NxDomain:
  2409. case DnsResponseCode.YXDomain:
  2410. cancellationTokenSource.Cancel(); //to stop other resolver tasks
  2411. response = taskResponse;
  2412. foundResponse = true;
  2413. break;
  2414. default:
  2415. //keep response
  2416. lastResponse = taskResponse;
  2417. break;
  2418. }
  2419. if (foundResponse)
  2420. break;
  2421. }
  2422. tasks.Remove(completedTask);
  2423. lastException = completedTask.Exception;
  2424. if (lastException is AggregateException)
  2425. lastException = lastException.InnerException;
  2426. }
  2427. if (response is null)
  2428. {
  2429. if (lastResponse is not null)
  2430. response = lastResponse;
  2431. else if (lastException is not null)
  2432. ExceptionDispatchInfo.Capture(lastException).Throw();
  2433. else
  2434. throw new InvalidOperationException();
  2435. }
  2436. }
  2437. }
  2438. }
  2439. else if ((conditionalForwarders is null) && (_forwarders is not null) && (_forwarders.Count > 0))
  2440. {
  2441. //use forwarders
  2442. if (_proxy is null)
  2443. {
  2444. //recursive resolve name server when proxy is null else let proxy resolve it
  2445. foreach (NameServerAddress nameServerAddress in _forwarders)
  2446. {
  2447. if (nameServerAddress.IsIPEndPointStale) //refresh forwarder IPEndPoint if stale
  2448. await nameServerAddress.RecursiveResolveIPAddressAsync(dnsCache, null, _preferIPv6, _udpPayloadSize, _randomizeName, _resolverRetries, _resolverTimeout);
  2449. }
  2450. }
  2451. //query forwarders and update cache
  2452. DnsClient dnsClient = new DnsClient(_forwarders);
  2453. dnsClient.Cache = dnsCache;
  2454. dnsClient.Proxy = _proxy;
  2455. dnsClient.PreferIPv6 = _preferIPv6;
  2456. dnsClient.RandomizeName = _randomizeName;
  2457. dnsClient.Retries = _forwarderRetries;
  2458. dnsClient.Timeout = _forwarderTimeout;
  2459. dnsClient.Concurrency = _forwarderConcurrency;
  2460. dnsClient.UdpPayloadSize = _udpPayloadSize;
  2461. dnsClient.DnssecValidation = dnssecValidation;
  2462. dnsClient.EDnsClientSubnet = eDnsClientSubnet;
  2463. dnsClient.ConditionalForwardingZoneCut = question.Name; //adding zone cut to allow CNAME domains to be resolved independently to handle cases when private/forwarder zone is configured for them
  2464. response = await dnsClient.ResolveAsync(question);
  2465. }
  2466. else
  2467. {
  2468. //do recursive resolution
  2469. response = await DnsClient.RecursiveResolveAsync(question, dnsCache, _proxy, _preferIPv6, _udpPayloadSize, _randomizeName, _qnameMinimization, _nsRevalidation, dnssecValidation, eDnsClientSubnet, _resolverRetries, _resolverTimeout, _resolverMaxStackCount, true, true);
  2470. }
  2471. switch (response.RCODE)
  2472. {
  2473. case DnsResponseCode.NoError:
  2474. case DnsResponseCode.NxDomain:
  2475. case DnsResponseCode.YXDomain:
  2476. taskCompletionSource.SetResult(new RecursiveResolveResponse(response, response));
  2477. break;
  2478. default:
  2479. throw new DnsServerException("DNS Server received a response for '" + question.ToString() + "' with RCODE=" + response.RCODE.ToString() + " from: " + (response.Metadata is null ? "unknown" : response.Metadata.NameServer));
  2480. }
  2481. }
  2482. catch (Exception ex)
  2483. {
  2484. if (_log is not null)
  2485. {
  2486. string strForwarders = null;
  2487. if ((conditionalForwarders is not null) && (conditionalForwarders.Count > 0))
  2488. {
  2489. foreach (DnsResourceRecord conditionalForwarder in conditionalForwarders)
  2490. {
  2491. NameServerAddress nameServer = (conditionalForwarder.RDATA as DnsForwarderRecordData).NameServer;
  2492. if (strForwarders is null)
  2493. strForwarders = nameServer.ToString();
  2494. else
  2495. strForwarders += ", " + nameServer.ToString();
  2496. }
  2497. }
  2498. else if ((_forwarders is not null) && (_forwarders.Count > 0))
  2499. {
  2500. foreach (NameServerAddress nameServer in _forwarders)
  2501. {
  2502. if (strForwarders is null)
  2503. strForwarders = nameServer.ToString();
  2504. else
  2505. strForwarders += ", " + nameServer.ToString();
  2506. }
  2507. }
  2508. _log.Write("DNS Server failed to resolve the request '" + question.ToString() + "'" + (strForwarders is null ? "" : " using forwarders: " + strForwarders) + ".\r\n" + ex.ToString());
  2509. }
  2510. if (_serveStale)
  2511. {
  2512. //fetch stale record
  2513. DnsDatagram cacheRequest = new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, true, false, false, dnssecValidation, DnsResponseCode.NoError, new DnsQuestionRecord[] { question }, null, null, null, _udpPayloadSize, dnssecValidation ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, EDnsClientSubnetOptionData.GetEDnsClientSubnetOption(eDnsClientSubnet));
  2514. DnsDatagram staleResponse = QueryCache(cacheRequest, true);
  2515. if (staleResponse is not null)
  2516. {
  2517. //signal stale response
  2518. if (!dnssecValidation || staleResponse.AuthenticData)
  2519. {
  2520. taskCompletionSource.SetResult(new RecursiveResolveResponse(staleResponse, staleResponse));
  2521. }
  2522. else
  2523. {
  2524. List<EDnsOption> options;
  2525. if ((staleResponse.EDNS is not null) && (staleResponse.EDNS.Options.Count > 0))
  2526. {
  2527. options = new List<EDnsOption>(staleResponse.EDNS.Options.Count);
  2528. foreach (EDnsOption option in staleResponse.EDNS.Options)
  2529. {
  2530. if (option.Code == EDnsOptionCode.EXTENDED_DNS_ERROR)
  2531. options.Add(option);
  2532. }
  2533. }
  2534. else
  2535. {
  2536. options = null;
  2537. }
  2538. DnsDatagram failureResponse = new DnsDatagram(0, true, DnsOpcode.StandardQuery, false, false, true, true, false, dnssecValidation, DnsResponseCode.ServerFailure, new DnsQuestionRecord[] { question }, null, null, null, _udpPayloadSize, dnssecValidation ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, options);
  2539. taskCompletionSource.SetResult(new RecursiveResolveResponse(failureResponse, staleResponse));
  2540. }
  2541. return;
  2542. }
  2543. }
  2544. //signal failure response to release waiting tasks
  2545. if (ex is DnsClientResponseDnssecValidationException ex2)
  2546. {
  2547. List<EDnsOption> options;
  2548. if (ex2.Response.DnsClientExtendedErrors.Count > 0)
  2549. {
  2550. options = new List<EDnsOption>(ex2.Response.DnsClientExtendedErrors.Count);
  2551. foreach (EDnsExtendedDnsErrorOptionData dnsError in ex2.Response.DnsClientExtendedErrors)
  2552. options.Add(new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, dnsError));
  2553. }
  2554. else
  2555. {
  2556. options = null;
  2557. }
  2558. DnsDatagram failureResponse = new DnsDatagram(0, true, DnsOpcode.StandardQuery, false, false, true, true, false, dnssecValidation, DnsResponseCode.ServerFailure, new DnsQuestionRecord[] { question }, null, null, null, _udpPayloadSize, EDnsHeaderFlags.DNSSEC_OK, options);
  2559. if ((ex2.Response.Question.Count > 0) && ex2.Response.Question[0].Equals(question))
  2560. taskCompletionSource.SetResult(new RecursiveResolveResponse(failureResponse, ex2.Response));
  2561. else
  2562. taskCompletionSource.SetResult(new RecursiveResolveResponse(failureResponse, failureResponse));
  2563. }
  2564. else if (ex is DnsClientNoResponseException ex3)
  2565. {
  2566. IReadOnlyList<EDnsOption> options;
  2567. if (ex.InnerException is SocketException ex3a)
  2568. {
  2569. if (ex3a.SocketErrorCode == SocketError.TimedOut)
  2570. options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.NoReachableAuthority, "Request timed out")) };
  2571. else
  2572. options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.NetworkError, "Socket error: " + ex3a.SocketErrorCode.ToString())) };
  2573. }
  2574. else
  2575. {
  2576. options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.NoReachableAuthority, "No response from name servers for " + question.ToString())) };
  2577. }
  2578. DnsDatagram failureResponse = new DnsDatagram(0, true, DnsOpcode.StandardQuery, false, false, true, true, false, dnssecValidation, DnsResponseCode.ServerFailure, new DnsQuestionRecord[] { question }, null, null, null, _udpPayloadSize, dnssecValidation ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, options);
  2579. taskCompletionSource.SetResult(new RecursiveResolveResponse(failureResponse, failureResponse));
  2580. }
  2581. else if (ex is SocketException ex4)
  2582. {
  2583. IReadOnlyList<EDnsOption> options;
  2584. if (ex4.SocketErrorCode == SocketError.TimedOut)
  2585. options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.NoReachableAuthority, "Request timed out")) };
  2586. else
  2587. options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.NetworkError, "Socket error: " + ex4.SocketErrorCode.ToString())) };
  2588. DnsDatagram failureResponse = new DnsDatagram(0, true, DnsOpcode.StandardQuery, false, false, true, true, false, dnssecValidation, DnsResponseCode.ServerFailure, new DnsQuestionRecord[] { question }, null, null, null, _udpPayloadSize, dnssecValidation ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, options);
  2589. taskCompletionSource.SetResult(new RecursiveResolveResponse(failureResponse, failureResponse));
  2590. }
  2591. else if (ex is IOException ex5)
  2592. {
  2593. IReadOnlyList<EDnsOption> options;
  2594. if (ex5.InnerException is SocketException ex5a)
  2595. {
  2596. if (ex5a.SocketErrorCode == SocketError.TimedOut)
  2597. options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.NoReachableAuthority, "Request timed out")) };
  2598. else
  2599. options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.NetworkError, "Socket error: " + ex5a.SocketErrorCode.ToString())) };
  2600. }
  2601. else
  2602. {
  2603. options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.NetworkError, "IO error: " + ex5.Message)) };
  2604. }
  2605. DnsDatagram failureResponse = new DnsDatagram(0, true, DnsOpcode.StandardQuery, false, false, true, true, false, dnssecValidation, DnsResponseCode.ServerFailure, new DnsQuestionRecord[] { question }, null, null, null, _udpPayloadSize, dnssecValidation ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, options);
  2606. taskCompletionSource.SetResult(new RecursiveResolveResponse(failureResponse, failureResponse));
  2607. }
  2608. else
  2609. {
  2610. IReadOnlyList<EDnsOption> options = new EDnsOption[] { new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, new EDnsExtendedDnsErrorOptionData(EDnsExtendedDnsErrorCode.Other, "Resolver exception")) };
  2611. DnsDatagram failureResponse = new DnsDatagram(0, true, DnsOpcode.StandardQuery, false, false, true, true, false, dnssecValidation, DnsResponseCode.ServerFailure, new DnsQuestionRecord[] { question }, null, null, null, _udpPayloadSize, dnssecValidation ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, options);
  2612. taskCompletionSource.SetResult(new RecursiveResolveResponse(failureResponse, failureResponse));
  2613. }
  2614. }
  2615. finally
  2616. {
  2617. _resolverTasks.TryRemove(GetResolverQueryKey(question, eDnsClientSubnet), out _);
  2618. }
  2619. }
  2620. private Task<DnsDatagram> ConditionalForwarderResolveAsync(DnsQuestionRecord question, NetworkAddress eDnsClientSubnet, bool conditionalForwardingClientSubnet, IDnsCache dnsCache, DnsForwarderRecordData forwarder, string conditionalForwardingZoneCut, CancellationToken cancellationToken = default)
  2621. {
  2622. NetProxy proxy = forwarder.Proxy;
  2623. if (proxy is null)
  2624. proxy = _proxy;
  2625. DnsClient dnsClient = new DnsClient(forwarder.NameServer);
  2626. dnsClient.Cache = dnsCache;
  2627. dnsClient.Proxy = proxy;
  2628. dnsClient.PreferIPv6 = _preferIPv6;
  2629. dnsClient.RandomizeName = _randomizeName;
  2630. dnsClient.Retries = _forwarderRetries;
  2631. dnsClient.Timeout = _forwarderTimeout;
  2632. dnsClient.Concurrency = _forwarderConcurrency;
  2633. dnsClient.UdpPayloadSize = _udpPayloadSize;
  2634. dnsClient.DnssecValidation = forwarder.DnssecValidation;
  2635. dnsClient.EDnsClientSubnet = eDnsClientSubnet;
  2636. dnsClient.ConditionalForwardingClientSubnet = conditionalForwardingClientSubnet;
  2637. dnsClient.ConditionalForwardingZoneCut = conditionalForwardingZoneCut;
  2638. return dnsClient.ResolveAsync(question, cancellationToken);
  2639. }
  2640. private DnsDatagram PrepareRecursiveResolveResponse(DnsDatagram request, RecursiveResolveResponse resolveResponse)
  2641. {
  2642. //get a tailored response for the request
  2643. bool dnssecOk = request.DnssecOk;
  2644. if (dnssecOk && request.CheckingDisabled)
  2645. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, true, true, resolveResponse.CheckingDisabledResponse.AuthenticData, resolveResponse.CheckingDisabledResponse.CheckingDisabled, resolveResponse.CheckingDisabledResponse.RCODE, request.Question, resolveResponse.CheckingDisabledResponse.Answer, resolveResponse.CheckingDisabledResponse.Authority, RemoveOPTFromAdditional(resolveResponse.CheckingDisabledResponse.Additional, true), _udpPayloadSize, EDnsHeaderFlags.DNSSEC_OK, resolveResponse.CheckingDisabledResponse.EDNS?.Options);
  2646. DnsDatagram response = resolveResponse.Response;
  2647. IReadOnlyList<DnsResourceRecord> answer = response.Answer;
  2648. IReadOnlyList<DnsResourceRecord> authority = response.Authority;
  2649. IReadOnlyList<DnsResourceRecord> additional = response.Additional;
  2650. //answer section checks
  2651. if (!dnssecOk && (answer.Count > 0) && (response.Question[0].Type != DnsResourceRecordType.ANY))
  2652. {
  2653. //remove RRSIGs from answer
  2654. bool foundRRSIG = false;
  2655. foreach (DnsResourceRecord record in answer)
  2656. {
  2657. if (record.Type == DnsResourceRecordType.RRSIG)
  2658. {
  2659. foundRRSIG = true;
  2660. break;
  2661. }
  2662. }
  2663. if (foundRRSIG)
  2664. {
  2665. List<DnsResourceRecord> newAnswer = new List<DnsResourceRecord>(answer.Count);
  2666. foreach (DnsResourceRecord record in answer)
  2667. {
  2668. if (record.Type == DnsResourceRecordType.RRSIG)
  2669. continue;
  2670. newAnswer.Add(record);
  2671. }
  2672. answer = newAnswer;
  2673. }
  2674. }
  2675. //authority section checks
  2676. if (!dnssecOk && (authority.Count > 0))
  2677. {
  2678. //remove DNSSEC records
  2679. bool foundDnssecRecords = false;
  2680. bool foundOther = false;
  2681. foreach (DnsResourceRecord record in authority)
  2682. {
  2683. switch (record.Type)
  2684. {
  2685. case DnsResourceRecordType.DS:
  2686. case DnsResourceRecordType.DNSKEY:
  2687. case DnsResourceRecordType.RRSIG:
  2688. case DnsResourceRecordType.NSEC:
  2689. case DnsResourceRecordType.NSEC3:
  2690. foundDnssecRecords = true;
  2691. break;
  2692. default:
  2693. foundOther = true;
  2694. break;
  2695. }
  2696. }
  2697. if (foundDnssecRecords)
  2698. {
  2699. if (foundOther)
  2700. {
  2701. List<DnsResourceRecord> newAuthority = new List<DnsResourceRecord>(2);
  2702. foreach (DnsResourceRecord record in authority)
  2703. {
  2704. switch (record.Type)
  2705. {
  2706. case DnsResourceRecordType.DS:
  2707. case DnsResourceRecordType.DNSKEY:
  2708. case DnsResourceRecordType.RRSIG:
  2709. case DnsResourceRecordType.NSEC:
  2710. case DnsResourceRecordType.NSEC3:
  2711. break;
  2712. default:
  2713. newAuthority.Add(record);
  2714. break;
  2715. }
  2716. }
  2717. authority = newAuthority;
  2718. }
  2719. else
  2720. {
  2721. authority = Array.Empty<DnsResourceRecord>();
  2722. }
  2723. }
  2724. }
  2725. //additional section checks
  2726. if (additional.Count > 0)
  2727. {
  2728. if ((request.EDNS is not null) && (response.EDNS is not null) && ((response.EDNS.Options.Count > 0) || (response.DnsClientExtendedErrors.Count > 0)))
  2729. {
  2730. //copy options as new OPT and keep other records
  2731. List<DnsResourceRecord> newAdditional = new List<DnsResourceRecord>(additional.Count);
  2732. foreach (DnsResourceRecord record in additional)
  2733. {
  2734. switch (record.Type)
  2735. {
  2736. case DnsResourceRecordType.OPT:
  2737. continue;
  2738. case DnsResourceRecordType.RRSIG:
  2739. case DnsResourceRecordType.DNSKEY:
  2740. if (dnssecOk)
  2741. break;
  2742. continue;
  2743. }
  2744. newAdditional.Add(record);
  2745. }
  2746. IReadOnlyList<EDnsOption> options;
  2747. if (response.GetEDnsClientSubnetOption(true) is not null)
  2748. {
  2749. //response contains ECS
  2750. if (request.GetEDnsClientSubnetOption(true) is not null)
  2751. {
  2752. //request has ECS and type is supported; keep ECS in response
  2753. options = response.EDNS.Options;
  2754. }
  2755. else
  2756. {
  2757. //cache does not support the qtype so remove ECS from response
  2758. if (response.EDNS.Options.Count == 1)
  2759. {
  2760. options = Array.Empty<EDnsOption>();
  2761. }
  2762. else
  2763. {
  2764. List<EDnsOption> newOptions = new List<EDnsOption>(response.EDNS.Options.Count);
  2765. foreach (EDnsOption option in response.EDNS.Options)
  2766. {
  2767. if (option.Code != EDnsOptionCode.EDNS_CLIENT_SUBNET)
  2768. newOptions.Add(option);
  2769. }
  2770. options = newOptions;
  2771. }
  2772. }
  2773. }
  2774. else
  2775. {
  2776. options = response.EDNS.Options;
  2777. }
  2778. if (response.DnsClientExtendedErrors.Count > 0)
  2779. {
  2780. //add dns client extended errors
  2781. List<EDnsOption> newOptions = new List<EDnsOption>(options.Count + response.DnsClientExtendedErrors.Count);
  2782. newOptions.AddRange(options);
  2783. foreach (EDnsExtendedDnsErrorOptionData ee in response.DnsClientExtendedErrors)
  2784. newOptions.Add(new EDnsOption(EDnsOptionCode.EXTENDED_DNS_ERROR, ee));
  2785. options = newOptions;
  2786. }
  2787. newAdditional.Add(DnsDatagramEdns.GetOPTFor(_udpPayloadSize, response.RCODE, 0, request.DnssecOk ? EDnsHeaderFlags.DNSSEC_OK : EDnsHeaderFlags.None, options));
  2788. additional = newAdditional;
  2789. }
  2790. else if (response.EDNS is not null)
  2791. {
  2792. //remove OPT from additional
  2793. additional = RemoveOPTFromAdditional(additional, dnssecOk);
  2794. }
  2795. }
  2796. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, false, false, true, true, response.AuthenticData, response.CheckingDisabled, response.RCODE, request.Question, answer, authority, additional);
  2797. }
  2798. private static IReadOnlyList<DnsResourceRecord> RemoveOPTFromAdditional(IReadOnlyList<DnsResourceRecord> additional, bool dnssecOk)
  2799. {
  2800. if (additional.Count == 0)
  2801. return additional;
  2802. if ((additional.Count == 1) && (additional[0].Type == DnsResourceRecordType.OPT))
  2803. return Array.Empty<DnsResourceRecord>();
  2804. List<DnsResourceRecord> newAdditional = new List<DnsResourceRecord>(additional.Count - 1);
  2805. foreach (DnsResourceRecord record in additional)
  2806. {
  2807. switch (record.Type)
  2808. {
  2809. case DnsResourceRecordType.OPT:
  2810. continue;
  2811. case DnsResourceRecordType.RRSIG:
  2812. case DnsResourceRecordType.DNSKEY:
  2813. if (dnssecOk)
  2814. break;
  2815. continue;
  2816. }
  2817. newAdditional.Add(record);
  2818. }
  2819. return newAdditional;
  2820. }
  2821. private static string GetResolverQueryKey(DnsQuestionRecord question, NetworkAddress eDnsClientSubnet)
  2822. {
  2823. if (eDnsClientSubnet is null)
  2824. return question.ToString();
  2825. return question.ToString() + " " + eDnsClientSubnet.ToString();
  2826. }
  2827. private DnsDatagram QueryCache(DnsDatagram request, bool serveStaleAndResetExpiry)
  2828. {
  2829. DnsDatagram cacheResponse = _cacheZoneManager.Query(request, serveStaleAndResetExpiry);
  2830. if (cacheResponse is not null)
  2831. {
  2832. if ((cacheResponse.RCODE != DnsResponseCode.NoError) || (cacheResponse.Answer.Count > 0) || (cacheResponse.Authority.Count == 0) || cacheResponse.IsFirstAuthoritySOA())
  2833. {
  2834. cacheResponse.Tag = DnsServerResponseType.Cached;
  2835. return cacheResponse;
  2836. }
  2837. }
  2838. return null;
  2839. }
  2840. private async Task PrefetchCacheAsync(DnsDatagram request, IPEndPoint remoteEP, IReadOnlyList<DnsResourceRecord> conditionalForwarders)
  2841. {
  2842. try
  2843. {
  2844. await RecursiveResolveAsync(request, remoteEP, conditionalForwarders, _dnssecValidation, true, false, false);
  2845. }
  2846. catch (Exception ex)
  2847. {
  2848. _log?.Write(ex);
  2849. }
  2850. }
  2851. private async Task RefreshCacheAsync(IList<CacheRefreshSample> cacheRefreshSampleList, CacheRefreshSample sample, int sampleQuestionIndex)
  2852. {
  2853. try
  2854. {
  2855. //refresh cache
  2856. DnsDatagram request = new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, true, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { sample.SampleQuestion });
  2857. DnsDatagram response = await ProcessRecursiveQueryAsync(request, IPENDPOINT_ANY_0, DnsTransportProtocol.Udp, sample.ConditionalForwarders, _dnssecValidation, true, false);
  2858. bool addBackToSampleList = false;
  2859. DateTime utcNow = DateTime.UtcNow;
  2860. foreach (DnsResourceRecord answer in response.Answer)
  2861. {
  2862. if ((answer.OriginalTtlValue >= _cachePrefetchEligibility) && (utcNow.AddSeconds(answer.TTL) < _cachePrefetchSamplingTimerTriggersOn))
  2863. {
  2864. //answer expires before next sampling so add back to the list to allow refreshing it
  2865. addBackToSampleList = true;
  2866. break;
  2867. }
  2868. }
  2869. if (addBackToSampleList)
  2870. cacheRefreshSampleList[sampleQuestionIndex] = sample; //put back into sample list to allow refreshing it again
  2871. }
  2872. catch (Exception ex)
  2873. {
  2874. _log?.Write(ex);
  2875. cacheRefreshSampleList[sampleQuestionIndex] = sample; //put back into sample list to allow refreshing it again
  2876. }
  2877. }
  2878. private DnsQuestionRecord GetCacheRefreshNeededQuery(DnsQuestionRecord question, int trigger)
  2879. {
  2880. int queryCount = 0;
  2881. while (true)
  2882. {
  2883. DnsDatagram cacheResponse = QueryCache(new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, true, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { question }), false);
  2884. if (cacheResponse is null)
  2885. return question; //cache expired so refresh question
  2886. if (cacheResponse.Answer.Count == 0)
  2887. return null; //dont refresh empty responses
  2888. //inspect response TTL values to decide if refresh is needed
  2889. foreach (DnsResourceRecord answer in cacheResponse.Answer)
  2890. {
  2891. if ((answer.OriginalTtlValue >= _cachePrefetchEligibility) && (answer.TTL <= trigger))
  2892. return question; //TTL eligible and less than trigger so refresh question
  2893. }
  2894. DnsResourceRecord lastRR = cacheResponse.GetLastAnswerRecord();
  2895. if (lastRR.Type == question.Type)
  2896. return null; //answer was resolved
  2897. if (lastRR.Type != DnsResourceRecordType.CNAME)
  2898. return null; //invalid response so ignore question
  2899. queryCount++;
  2900. if (queryCount >= MAX_CNAME_HOPS)
  2901. return null; //too many hops so ignore question
  2902. //follow CNAME chain to inspect TTL further
  2903. question = new DnsQuestionRecord((lastRR.RDATA as DnsCNAMERecordData).Domain, question.Type, question.Class);
  2904. }
  2905. }
  2906. private bool IsCacheRefreshNeeded(DnsQuestionRecord question, int trigger)
  2907. {
  2908. DnsDatagram cacheResponse = QueryCache(new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, true, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { question }), false);
  2909. if (cacheResponse is null)
  2910. return true; //cache expired so refresh needed
  2911. if (cacheResponse.Answer.Count == 0)
  2912. return false; //dont refresh empty responses
  2913. //inspect response TTL values to decide if refresh is needed
  2914. foreach (DnsResourceRecord answer in cacheResponse.Answer)
  2915. {
  2916. if ((answer.OriginalTtlValue >= _cachePrefetchEligibility) && (answer.TTL <= trigger))
  2917. return true; //TTL eligible less than trigger so refresh
  2918. }
  2919. return false; //no need to refresh for this query
  2920. }
  2921. private async void CachePrefetchSamplingTimerCallback(object state)
  2922. {
  2923. try
  2924. {
  2925. List<KeyValuePair<DnsQuestionRecord, long>> eligibleQueries = _stats.GetLastHourEligibleQueries(_cachePrefetchSampleEligibilityHitsPerHour);
  2926. List<CacheRefreshSample> cacheRefreshSampleList = new List<CacheRefreshSample>(eligibleQueries.Count);
  2927. int cacheRefreshTrigger = (_cachePrefetchSampleIntervalInMinutes + 1) * 60;
  2928. foreach (KeyValuePair<DnsQuestionRecord, long> eligibleQuery in eligibleQueries)
  2929. {
  2930. DnsQuestionRecord eligibleQuerySample = eligibleQuery.Key;
  2931. if (eligibleQuerySample.Type == DnsResourceRecordType.ANY)
  2932. continue; //dont refresh type ANY queries
  2933. DnsQuestionRecord refreshQuery = null;
  2934. IReadOnlyList<DnsResourceRecord> conditionalForwarders = null;
  2935. //query auth zone for refresh query
  2936. int queryCount = 0;
  2937. bool reQueryAuthZone;
  2938. do
  2939. {
  2940. reQueryAuthZone = false;
  2941. DnsDatagram request = new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, false, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { eligibleQuerySample });
  2942. DnsDatagram response = await AuthoritativeQueryAsync(request, IPENDPOINT_ANY_0, DnsTransportProtocol.Tcp, true, false);
  2943. if (response is null)
  2944. {
  2945. //zone not hosted; do refresh
  2946. refreshQuery = GetCacheRefreshNeededQuery(eligibleQuerySample, cacheRefreshTrigger);
  2947. }
  2948. else
  2949. {
  2950. //zone is hosted; check further
  2951. if (response.Answer.Count > 0)
  2952. {
  2953. DnsResourceRecord lastRR = response.GetLastAnswerRecord();
  2954. if ((lastRR.Type == DnsResourceRecordType.CNAME) && (eligibleQuerySample.Type != DnsResourceRecordType.CNAME))
  2955. {
  2956. eligibleQuerySample = new DnsQuestionRecord((lastRR.RDATA as DnsCNAMERecordData).Domain, eligibleQuerySample.Type, eligibleQuerySample.Class);
  2957. reQueryAuthZone = true;
  2958. }
  2959. }
  2960. else if (response.Authority.Count > 0)
  2961. {
  2962. DnsResourceRecord firstAuthority = response.FindFirstAuthorityRecord();
  2963. switch (firstAuthority.Type)
  2964. {
  2965. case DnsResourceRecordType.NS: //zone is delegated
  2966. refreshQuery = GetCacheRefreshNeededQuery(eligibleQuerySample, cacheRefreshTrigger);
  2967. conditionalForwarders = Array.Empty<DnsResourceRecord>(); //do forced recursive resolution using empty conditional forwarders
  2968. break;
  2969. case DnsResourceRecordType.FWD: //zone is conditional forwarder
  2970. refreshQuery = GetCacheRefreshNeededQuery(eligibleQuerySample, cacheRefreshTrigger);
  2971. conditionalForwarders = response.Authority; //do conditional forwarding
  2972. break;
  2973. }
  2974. }
  2975. }
  2976. }
  2977. while (reQueryAuthZone && (++queryCount < MAX_CNAME_HOPS));
  2978. if (refreshQuery is not null)
  2979. cacheRefreshSampleList.Add(new CacheRefreshSample(refreshQuery, conditionalForwarders));
  2980. }
  2981. _cacheRefreshSampleList = cacheRefreshSampleList;
  2982. }
  2983. catch (Exception ex)
  2984. {
  2985. _log?.Write(ex);
  2986. }
  2987. finally
  2988. {
  2989. lock (_cachePrefetchSamplingTimerLock)
  2990. {
  2991. if (_cachePrefetchSamplingTimer is not null)
  2992. {
  2993. _cachePrefetchSamplingTimer.Change(_cachePrefetchSampleIntervalInMinutes * 60 * 1000, Timeout.Infinite);
  2994. _cachePrefetchSamplingTimerTriggersOn = DateTime.UtcNow.AddMinutes(_cachePrefetchSampleIntervalInMinutes);
  2995. }
  2996. }
  2997. }
  2998. }
  2999. private void CachePrefetchRefreshTimerCallback(object state)
  3000. {
  3001. try
  3002. {
  3003. IList<CacheRefreshSample> cacheRefreshSampleList = _cacheRefreshSampleList;
  3004. if (cacheRefreshSampleList is not null)
  3005. {
  3006. for (int i = 0; i < cacheRefreshSampleList.Count; i++)
  3007. {
  3008. CacheRefreshSample sample = cacheRefreshSampleList[i];
  3009. if (sample is null)
  3010. continue;
  3011. if (!IsCacheRefreshNeeded(sample.SampleQuestion, _cachePrefetchTrigger + 1))
  3012. continue;
  3013. cacheRefreshSampleList[i] = null; //remove from sample list to avoid concurrent refresh attempt
  3014. int sampleQuestionIndex = i;
  3015. _ = Task.Run(delegate () { return RefreshCacheAsync(cacheRefreshSampleList, sample, sampleQuestionIndex); }); //run task in threadpool since its long running
  3016. }
  3017. }
  3018. }
  3019. catch (Exception ex)
  3020. {
  3021. _log?.Write(ex);
  3022. }
  3023. finally
  3024. {
  3025. lock (_cachePrefetchRefreshTimerLock)
  3026. {
  3027. _cachePrefetchRefreshTimer?.Change((_cachePrefetchTrigger + 1) * 1000, Timeout.Infinite);
  3028. }
  3029. }
  3030. }
  3031. private void CacheMaintenanceTimerCallback(object state)
  3032. {
  3033. try
  3034. {
  3035. _cacheZoneManager.RemoveExpiredRecords();
  3036. //force GC collection to remove old cache data from memory quickly
  3037. GC.Collect();
  3038. }
  3039. catch (Exception ex)
  3040. {
  3041. _log?.Write(ex);
  3042. }
  3043. finally
  3044. {
  3045. lock (_cacheMaintenanceTimerLock)
  3046. {
  3047. _cacheMaintenanceTimer?.Change(CACHE_MAINTENANCE_TIMER_PERIODIC_INTERVAL, Timeout.Infinite);
  3048. }
  3049. }
  3050. }
  3051. private void ResetPrefetchTimers()
  3052. {
  3053. if ((_cachePrefetchTrigger == 0) || (_recursion == DnsServerRecursion.Deny))
  3054. {
  3055. lock (_cachePrefetchSamplingTimerLock)
  3056. {
  3057. _cachePrefetchSamplingTimer?.Change(Timeout.Infinite, Timeout.Infinite);
  3058. }
  3059. lock (_cachePrefetchRefreshTimerLock)
  3060. {
  3061. _cachePrefetchRefreshTimer?.Change(Timeout.Infinite, Timeout.Infinite);
  3062. }
  3063. }
  3064. else if (_state == ServiceState.Running)
  3065. {
  3066. lock (_cachePrefetchSamplingTimerLock)
  3067. {
  3068. if (_cachePrefetchSamplingTimer is not null)
  3069. {
  3070. _cachePrefetchSamplingTimer.Change(CACHE_PREFETCH_SAMPLING_TIMER_INITIAL_INTEVAL, Timeout.Infinite);
  3071. _cachePrefetchSamplingTimerTriggersOn = DateTime.UtcNow.AddMilliseconds(CACHE_PREFETCH_SAMPLING_TIMER_INITIAL_INTEVAL);
  3072. }
  3073. }
  3074. lock (_cachePrefetchRefreshTimerLock)
  3075. {
  3076. _cachePrefetchRefreshTimer?.Change(CACHE_PREFETCH_REFRESH_TIMER_INITIAL_INTEVAL, Timeout.Infinite);
  3077. }
  3078. }
  3079. }
  3080. private bool IsQpmLimitCrossed(IPAddress remoteIP)
  3081. {
  3082. if ((_qpmLimitRequests < 1) && (_qpmLimitErrors < 1))
  3083. return false;
  3084. if (IPAddress.IsLoopback(remoteIP))
  3085. return false;
  3086. IPAddress remoteSubnet;
  3087. switch (remoteIP.AddressFamily)
  3088. {
  3089. case AddressFamily.InterNetwork:
  3090. remoteSubnet = remoteIP.GetNetworkAddress(_qpmLimitIPv4PrefixLength);
  3091. break;
  3092. case AddressFamily.InterNetworkV6:
  3093. remoteSubnet = remoteIP.GetNetworkAddress(_qpmLimitIPv6PrefixLength);
  3094. break;
  3095. default:
  3096. throw new NotSupportedException("AddressFamily not supported.");
  3097. }
  3098. if ((_qpmLimitErrors > 0) && (_qpmLimitErrorClientSubnetStats is not null) && _qpmLimitErrorClientSubnetStats.TryGetValue(remoteSubnet, out long errorCountPerSample))
  3099. {
  3100. long averageErrorCountPerMinute = errorCountPerSample / _qpmLimitSampleMinutes;
  3101. if (averageErrorCountPerMinute >= _qpmLimitErrors)
  3102. return true;
  3103. }
  3104. if ((_qpmLimitRequests > 0) && (_qpmLimitClientSubnetStats is not null) && _qpmLimitClientSubnetStats.TryGetValue(remoteSubnet, out long countPerSample))
  3105. {
  3106. long averageCountPerMinute = countPerSample / _qpmLimitSampleMinutes;
  3107. if (averageCountPerMinute >= _qpmLimitRequests)
  3108. return true;
  3109. }
  3110. return false;
  3111. }
  3112. private void QpmLimitSamplingTimerCallback(object state)
  3113. {
  3114. try
  3115. {
  3116. _stats.GetLatestClientSubnetStats(_qpmLimitSampleMinutes, _qpmLimitIPv4PrefixLength, _qpmLimitIPv6PrefixLength, out _qpmLimitClientSubnetStats, out _qpmLimitErrorClientSubnetStats);
  3117. }
  3118. catch (Exception ex)
  3119. {
  3120. _log?.Write(ex);
  3121. }
  3122. finally
  3123. {
  3124. lock (_qpmLimitSamplingTimerLock)
  3125. {
  3126. _qpmLimitSamplingTimer?.Change(QPM_LIMIT_SAMPLING_TIMER_INTERVAL, Timeout.Infinite);
  3127. }
  3128. }
  3129. }
  3130. private void ResetQpsLimitTimer()
  3131. {
  3132. if ((_qpmLimitRequests < 1) && (_qpmLimitErrors < 1))
  3133. {
  3134. lock (_qpmLimitSamplingTimerLock)
  3135. {
  3136. _qpmLimitSamplingTimer?.Change(Timeout.Infinite, Timeout.Infinite);
  3137. _qpmLimitClientSubnetStats = null;
  3138. _qpmLimitErrorClientSubnetStats = null;
  3139. }
  3140. }
  3141. else if (_state == ServiceState.Running)
  3142. {
  3143. lock (_qpmLimitSamplingTimerLock)
  3144. {
  3145. _qpmLimitSamplingTimer?.Change(0, Timeout.Infinite);
  3146. }
  3147. }
  3148. }
  3149. private void UpdateThisServer()
  3150. {
  3151. if ((_localEndPoints is null) || (_localEndPoints.Count == 0))
  3152. {
  3153. _thisServer = new NameServerAddress(_serverDomain, IPAddress.Loopback);
  3154. }
  3155. else
  3156. {
  3157. foreach (IPEndPoint localEndPoint in _localEndPoints)
  3158. {
  3159. if (localEndPoint.Address.Equals(IPAddress.Any))
  3160. {
  3161. _thisServer = new NameServerAddress(_serverDomain, new IPEndPoint(IPAddress.Loopback, localEndPoint.Port));
  3162. return;
  3163. }
  3164. if (localEndPoint.Address.Equals(IPAddress.IPv6Any))
  3165. {
  3166. _thisServer = new NameServerAddress(_serverDomain, new IPEndPoint(IPAddress.IPv6Loopback, localEndPoint.Port));
  3167. return;
  3168. }
  3169. }
  3170. _thisServer = new NameServerAddress(_serverDomain, _localEndPoints[0]);
  3171. }
  3172. }
  3173. #endregion
  3174. #region doh web service
  3175. private async Task StartDoHAsync()
  3176. {
  3177. WebApplicationBuilder builder = WebApplication.CreateBuilder();
  3178. builder.Environment.ContentRootFileProvider = new PhysicalFileProvider(Path.GetDirectoryName(_dohwwwFolder))
  3179. {
  3180. UseActivePolling = true,
  3181. UsePollingFileWatcher = true
  3182. };
  3183. builder.Environment.WebRootFileProvider = new PhysicalFileProvider(_dohwwwFolder)
  3184. {
  3185. UseActivePolling = true,
  3186. UsePollingFileWatcher = true
  3187. };
  3188. IReadOnlyList<IPAddress> localAddresses = GetValidKestralLocalAddresses(_localEndPoints.Convert(delegate (IPEndPoint ep) { return ep.Address; }));
  3189. builder.WebHost.ConfigureKestrel(delegate (WebHostBuilderContext context, KestrelServerOptions serverOptions)
  3190. {
  3191. //bind to http port
  3192. if (_enableDnsOverHttp)
  3193. {
  3194. foreach (IPAddress localAddress in localAddresses)
  3195. serverOptions.Listen(localAddress, _dnsOverHttpPort);
  3196. }
  3197. //bind to https port
  3198. if (_enableDnsOverHttps && (_certificateCollection is not null))
  3199. {
  3200. foreach (IPAddress localAddress in localAddresses)
  3201. {
  3202. serverOptions.Listen(localAddress, _dnsOverHttpsPort, delegate (ListenOptions listenOptions)
  3203. {
  3204. listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
  3205. listenOptions.UseHttps(delegate (SslStream stream, SslClientHelloInfo clientHelloInfo, object state, CancellationToken cancellationToken)
  3206. {
  3207. return ValueTask.FromResult(_sslServerAuthenticationOptions);
  3208. }, null);
  3209. });
  3210. }
  3211. }
  3212. serverOptions.AddServerHeader = false;
  3213. serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromMilliseconds(_tcpReceiveTimeout);
  3214. serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromMilliseconds(_tcpReceiveTimeout);
  3215. serverOptions.Limits.MaxRequestHeadersTotalSize = 4096;
  3216. serverOptions.Limits.MaxRequestLineSize = serverOptions.Limits.MaxRequestHeadersTotalSize;
  3217. serverOptions.Limits.MaxRequestBufferSize = serverOptions.Limits.MaxRequestLineSize;
  3218. serverOptions.Limits.MaxRequestBodySize = 64 * 1024;
  3219. serverOptions.Limits.MaxResponseBufferSize = 4096;
  3220. });
  3221. builder.Logging.ClearProviders();
  3222. _dohWebService = builder.Build();
  3223. _dohWebService.UseDefaultFiles();
  3224. _dohWebService.UseStaticFiles(new StaticFileOptions()
  3225. {
  3226. OnPrepareResponse = delegate (StaticFileResponseContext ctx)
  3227. {
  3228. ctx.Context.Response.Headers.Add("X-Robots-Tag", "noindex, nofollow");
  3229. ctx.Context.Response.Headers.Add("Cache-Control", "private, max-age=300");
  3230. },
  3231. ServeUnknownFileTypes = true
  3232. });
  3233. _dohWebService.UseRouting();
  3234. _dohWebService.MapGet("/dns-query", ProcessDoHRequestAsync);
  3235. _dohWebService.MapPost("/dns-query", ProcessDoHRequestAsync);
  3236. try
  3237. {
  3238. await _dohWebService.StartAsync();
  3239. if (_log is not null)
  3240. {
  3241. foreach (IPAddress localAddress in localAddresses)
  3242. {
  3243. if (_enableDnsOverHttp)
  3244. _log?.Write(new IPEndPoint(localAddress, _dnsOverHttpPort), "Http", "DNS Server was bound successfully.");
  3245. if (_enableDnsOverHttps && (_certificateCollection is not null))
  3246. _log?.Write(new IPEndPoint(localAddress, _dnsOverHttpsPort), "Https", "DNS Server was bound successfully.");
  3247. }
  3248. }
  3249. }
  3250. catch (Exception ex)
  3251. {
  3252. await StopDoHAsync();
  3253. if (_log is not null)
  3254. {
  3255. foreach (IPAddress localAddress in localAddresses)
  3256. {
  3257. if (_enableDnsOverHttp)
  3258. _log?.Write(new IPEndPoint(localAddress, _dnsOverHttpPort), "Http", "DNS Server failed to bind.");
  3259. if (_enableDnsOverHttps && (_certificateCollection is not null))
  3260. _log?.Write(new IPEndPoint(localAddress, _dnsOverHttpsPort), "Https", "DNS Server failed to bind.");
  3261. }
  3262. _log?.Write(ex);
  3263. }
  3264. }
  3265. }
  3266. private async Task StopDoHAsync()
  3267. {
  3268. if (_dohWebService is not null)
  3269. {
  3270. await _dohWebService.DisposeAsync();
  3271. _dohWebService = null;
  3272. }
  3273. }
  3274. internal static IReadOnlyList<IPAddress> GetValidKestralLocalAddresses(IReadOnlyList<IPAddress> localAddresses)
  3275. {
  3276. List<IPAddress> supportedLocalAddresses = new List<IPAddress>(localAddresses.Count);
  3277. foreach (IPAddress localAddress in localAddresses)
  3278. {
  3279. switch (localAddress.AddressFamily)
  3280. {
  3281. case AddressFamily.InterNetwork:
  3282. if (Socket.OSSupportsIPv4)
  3283. {
  3284. if (!supportedLocalAddresses.Contains(localAddress))
  3285. supportedLocalAddresses.Add(localAddress);
  3286. }
  3287. break;
  3288. case AddressFamily.InterNetworkV6:
  3289. if (Socket.OSSupportsIPv6)
  3290. {
  3291. if (!supportedLocalAddresses.Contains(localAddress))
  3292. supportedLocalAddresses.Add(localAddress);
  3293. }
  3294. break;
  3295. }
  3296. }
  3297. bool containsUnicastAddress = false;
  3298. foreach (IPAddress localAddress in supportedLocalAddresses)
  3299. {
  3300. if (!localAddress.Equals(IPAddress.Any) && !localAddress.Equals(IPAddress.IPv6Any))
  3301. {
  3302. containsUnicastAddress = true;
  3303. break;
  3304. }
  3305. }
  3306. List<IPAddress> newLocalAddresses = new List<IPAddress>(supportedLocalAddresses.Count);
  3307. if (containsUnicastAddress)
  3308. {
  3309. //replace any with loopback address
  3310. foreach (IPAddress localAddress in supportedLocalAddresses)
  3311. {
  3312. if (localAddress.Equals(IPAddress.Any))
  3313. {
  3314. if (!newLocalAddresses.Contains(IPAddress.Loopback))
  3315. newLocalAddresses.Add(IPAddress.Loopback);
  3316. }
  3317. else if (localAddress.Equals(IPAddress.IPv6Any))
  3318. {
  3319. if (!newLocalAddresses.Contains(IPAddress.IPv6Loopback))
  3320. newLocalAddresses.Add(IPAddress.IPv6Loopback);
  3321. }
  3322. else
  3323. {
  3324. if (!newLocalAddresses.Contains(localAddress))
  3325. newLocalAddresses.Add(localAddress);
  3326. }
  3327. }
  3328. }
  3329. else
  3330. {
  3331. //remove "0.0.0.0" if [::] exists
  3332. foreach (IPAddress localAddress in supportedLocalAddresses)
  3333. {
  3334. if (localAddress.Equals(IPAddress.Any))
  3335. {
  3336. if (!supportedLocalAddresses.Contains(IPAddress.IPv6Any))
  3337. newLocalAddresses.Add(localAddress);
  3338. }
  3339. else
  3340. {
  3341. newLocalAddresses.Add(localAddress);
  3342. }
  3343. }
  3344. }
  3345. return newLocalAddresses;
  3346. }
  3347. #endregion
  3348. #region public
  3349. public async Task StartAsync()
  3350. {
  3351. if (_disposed)
  3352. throw new ObjectDisposedException("DnsServer");
  3353. if (_state != ServiceState.Stopped)
  3354. throw new InvalidOperationException("DNS Server is already running.");
  3355. _state = ServiceState.Starting;
  3356. //bind on all local end points
  3357. foreach (IPEndPoint localEP in _localEndPoints)
  3358. {
  3359. Socket udpListener = null;
  3360. try
  3361. {
  3362. udpListener = new Socket(localEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
  3363. #region this code ignores ICMP port unreachable responses which creates SocketException in ReceiveFrom()
  3364. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  3365. {
  3366. const uint IOC_IN = 0x80000000;
  3367. const uint IOC_VENDOR = 0x18000000;
  3368. const uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
  3369. udpListener.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
  3370. }
  3371. #endregion
  3372. udpListener.ReceiveBufferSize = 512 * 1024;
  3373. udpListener.SendBufferSize = 512 * 1024;
  3374. udpListener.Bind(localEP);
  3375. _udpListeners.Add(udpListener);
  3376. _log?.Write(localEP, DnsTransportProtocol.Udp, "DNS Server was bound successfully.");
  3377. }
  3378. catch (Exception ex)
  3379. {
  3380. _log?.Write(localEP, DnsTransportProtocol.Udp, "DNS Server failed to bind.\r\n" + ex.ToString());
  3381. udpListener?.Dispose();
  3382. }
  3383. if (_enableDnsOverUdpProxy)
  3384. {
  3385. IPEndPoint udpProxyEP = new IPEndPoint(localEP.Address, _dnsOverUdpProxyPort);
  3386. Socket udpProxyListener = null;
  3387. try
  3388. {
  3389. udpProxyListener = new Socket(udpProxyEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
  3390. #region this code ignores ICMP port unreachable responses which creates SocketException in ReceiveFrom()
  3391. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  3392. {
  3393. const uint IOC_IN = 0x80000000;
  3394. const uint IOC_VENDOR = 0x18000000;
  3395. const uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
  3396. udpProxyListener.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
  3397. }
  3398. #endregion
  3399. udpProxyListener.ReceiveBufferSize = 512 * 1024;
  3400. udpProxyListener.SendBufferSize = 512 * 1024;
  3401. udpProxyListener.Bind(udpProxyEP);
  3402. _udpProxyListeners.Add(udpProxyListener);
  3403. _log?.Write(udpProxyEP, DnsTransportProtocol.UdpProxy, "DNS Server was bound successfully.");
  3404. }
  3405. catch (Exception ex)
  3406. {
  3407. _log?.Write(udpProxyEP, DnsTransportProtocol.UdpProxy, "DNS Server failed to bind.\r\n" + ex.ToString());
  3408. udpProxyListener?.Dispose();
  3409. }
  3410. }
  3411. Socket tcpListener = null;
  3412. try
  3413. {
  3414. tcpListener = new Socket(localEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  3415. tcpListener.Bind(localEP);
  3416. tcpListener.Listen(_listenBacklog);
  3417. _tcpListeners.Add(tcpListener);
  3418. _log?.Write(localEP, DnsTransportProtocol.Tcp, "DNS Server was bound successfully.");
  3419. }
  3420. catch (Exception ex)
  3421. {
  3422. _log?.Write(localEP, DnsTransportProtocol.Tcp, "DNS Server failed to bind.\r\n" + ex.ToString());
  3423. tcpListener?.Dispose();
  3424. }
  3425. if (_enableDnsOverTcpProxy)
  3426. {
  3427. IPEndPoint tcpProxyEP = new IPEndPoint(localEP.Address, _dnsOverTcpProxyPort);
  3428. Socket tcpProxyListner = null;
  3429. try
  3430. {
  3431. tcpProxyListner = new Socket(tcpProxyEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  3432. tcpProxyListner.Bind(tcpProxyEP);
  3433. tcpProxyListner.Listen(_listenBacklog);
  3434. _tcpProxyListeners.Add(tcpProxyListner);
  3435. _log?.Write(tcpProxyEP, DnsTransportProtocol.TcpProxy, "DNS Server was bound successfully.");
  3436. }
  3437. catch (Exception ex)
  3438. {
  3439. _log?.Write(tcpProxyEP, DnsTransportProtocol.TcpProxy, "DNS Server failed to bind.\r\n" + ex.ToString());
  3440. tcpProxyListner?.Dispose();
  3441. }
  3442. }
  3443. if (_enableDnsOverTls && (_certificateCollection is not null))
  3444. {
  3445. IPEndPoint tlsEP = new IPEndPoint(localEP.Address, _dnsOverTlsPort);
  3446. Socket tlsListener = null;
  3447. try
  3448. {
  3449. tlsListener = new Socket(tlsEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  3450. tlsListener.Bind(tlsEP);
  3451. tlsListener.Listen(_listenBacklog);
  3452. _tlsListeners.Add(tlsListener);
  3453. _log?.Write(tlsEP, DnsTransportProtocol.Tls, "DNS Server was bound successfully.");
  3454. }
  3455. catch (Exception ex)
  3456. {
  3457. _log?.Write(tlsEP, DnsTransportProtocol.Tls, "DNS Server failed to bind.\r\n" + ex.ToString());
  3458. tlsListener?.Dispose();
  3459. }
  3460. }
  3461. if (_enableDnsOverQuic && (_certificateCollection is not null))
  3462. {
  3463. IPEndPoint quicEP = new IPEndPoint(localEP.Address, _dnsOverQuicPort);
  3464. QuicListener quicListener = null;
  3465. try
  3466. {
  3467. QuicListenerOptions listenerOptions = new QuicListenerOptions()
  3468. {
  3469. ListenEndPoint = quicEP,
  3470. ListenBacklog = _listenBacklog,
  3471. ApplicationProtocols = quicApplicationProtocols,
  3472. ConnectionOptionsCallback = delegate (QuicConnection quicConnection, SslClientHelloInfo sslClientHello, CancellationToken cancellationToken)
  3473. {
  3474. QuicServerConnectionOptions serverConnectionOptions = new QuicServerConnectionOptions()
  3475. {
  3476. DefaultCloseErrorCode = (long)DnsOverQuicErrorCodes.DOQ_NO_ERROR,
  3477. DefaultStreamErrorCode = (long)DnsOverQuicErrorCodes.DOQ_UNSPECIFIED_ERROR,
  3478. MaxInboundUnidirectionalStreams = 0,
  3479. MaxInboundBidirectionalStreams = _quicMaxInboundStreams,
  3480. IdleTimeout = TimeSpan.FromMilliseconds(_quicIdleTimeout),
  3481. ServerAuthenticationOptions = _quicSslServerAuthenticationOptions
  3482. };
  3483. return ValueTask.FromResult(serverConnectionOptions);
  3484. }
  3485. };
  3486. quicListener = await QuicListener.ListenAsync(listenerOptions);
  3487. _quicListeners.Add(quicListener);
  3488. _log?.Write(quicEP, DnsTransportProtocol.Quic, "DNS Server was bound successfully.");
  3489. }
  3490. catch (Exception ex)
  3491. {
  3492. _log?.Write(quicEP, DnsTransportProtocol.Quic, "DNS Server failed to bind.\r\n" + ex.ToString());
  3493. if (quicListener is not null)
  3494. await quicListener.DisposeAsync();
  3495. }
  3496. }
  3497. }
  3498. //start reading query packets
  3499. int listenerTaskCount = Math.Max(1, Environment.ProcessorCount);
  3500. foreach (Socket udpListener in _udpListeners)
  3501. {
  3502. for (int i = 0; i < listenerTaskCount; i++)
  3503. {
  3504. _ = Task.Factory.StartNew(delegate ()
  3505. {
  3506. return ReadUdpRequestAsync(udpListener, DnsTransportProtocol.Udp);
  3507. }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, _queryTaskScheduler);
  3508. }
  3509. }
  3510. foreach (Socket udpProxyListener in _udpProxyListeners)
  3511. {
  3512. for (int i = 0; i < listenerTaskCount; i++)
  3513. {
  3514. _ = Task.Factory.StartNew(delegate ()
  3515. {
  3516. return ReadUdpRequestAsync(udpProxyListener, DnsTransportProtocol.UdpProxy);
  3517. }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, _queryTaskScheduler);
  3518. }
  3519. }
  3520. foreach (Socket tcpListener in _tcpListeners)
  3521. {
  3522. for (int i = 0; i < listenerTaskCount; i++)
  3523. {
  3524. _ = Task.Factory.StartNew(delegate ()
  3525. {
  3526. return AcceptConnectionAsync(tcpListener, DnsTransportProtocol.Tcp);
  3527. }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, _queryTaskScheduler);
  3528. }
  3529. }
  3530. foreach (Socket tcpProxyListener in _tcpProxyListeners)
  3531. {
  3532. for (int i = 0; i < listenerTaskCount; i++)
  3533. {
  3534. _ = Task.Factory.StartNew(delegate ()
  3535. {
  3536. return AcceptConnectionAsync(tcpProxyListener, DnsTransportProtocol.TcpProxy);
  3537. }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, _queryTaskScheduler);
  3538. }
  3539. }
  3540. foreach (Socket tlsListener in _tlsListeners)
  3541. {
  3542. for (int i = 0; i < listenerTaskCount; i++)
  3543. {
  3544. _ = Task.Factory.StartNew(delegate ()
  3545. {
  3546. return AcceptConnectionAsync(tlsListener, DnsTransportProtocol.Tls);
  3547. }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, _queryTaskScheduler);
  3548. }
  3549. }
  3550. foreach (QuicListener quicListener in _quicListeners)
  3551. {
  3552. for (int i = 0; i < listenerTaskCount; i++)
  3553. {
  3554. _ = Task.Factory.StartNew(delegate ()
  3555. {
  3556. return AcceptQuicConnectionAsync(quicListener);
  3557. }, CancellationToken.None, TaskCreationOptions.DenyChildAttach, _queryTaskScheduler);
  3558. }
  3559. }
  3560. if (_enableDnsOverHttp || (_enableDnsOverHttps && (_certificateCollection is not null)))
  3561. await StartDoHAsync();
  3562. _cachePrefetchSamplingTimer = new Timer(CachePrefetchSamplingTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
  3563. _cachePrefetchRefreshTimer = new Timer(CachePrefetchRefreshTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
  3564. _cacheMaintenanceTimer = new Timer(CacheMaintenanceTimerCallback, null, CACHE_MAINTENANCE_TIMER_INITIAL_INTEVAL, Timeout.Infinite);
  3565. _qpmLimitSamplingTimer = new Timer(QpmLimitSamplingTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
  3566. _state = ServiceState.Running;
  3567. UpdateThisServer();
  3568. ResetPrefetchTimers();
  3569. ResetQpsLimitTimer();
  3570. }
  3571. public async Task StopAsync()
  3572. {
  3573. if (_state != ServiceState.Running)
  3574. return;
  3575. _state = ServiceState.Stopping;
  3576. lock (_cachePrefetchSamplingTimerLock)
  3577. {
  3578. if (_cachePrefetchSamplingTimer is not null)
  3579. {
  3580. _cachePrefetchSamplingTimer.Dispose();
  3581. _cachePrefetchSamplingTimer = null;
  3582. }
  3583. }
  3584. lock (_cachePrefetchRefreshTimerLock)
  3585. {
  3586. if (_cachePrefetchRefreshTimer is not null)
  3587. {
  3588. _cachePrefetchRefreshTimer.Dispose();
  3589. _cachePrefetchRefreshTimer = null;
  3590. }
  3591. }
  3592. lock (_cacheMaintenanceTimerLock)
  3593. {
  3594. if (_cacheMaintenanceTimer is not null)
  3595. {
  3596. _cacheMaintenanceTimer.Dispose();
  3597. _cacheMaintenanceTimer = null;
  3598. }
  3599. }
  3600. lock (_qpmLimitSamplingTimerLock)
  3601. {
  3602. if (_qpmLimitSamplingTimer is not null)
  3603. {
  3604. _qpmLimitSamplingTimer.Dispose();
  3605. _qpmLimitSamplingTimer = null;
  3606. }
  3607. }
  3608. foreach (Socket udpListener in _udpListeners)
  3609. udpListener.Dispose();
  3610. foreach (Socket udpProxyListener in _udpProxyListeners)
  3611. udpProxyListener.Dispose();
  3612. foreach (Socket tcpListener in _tcpListeners)
  3613. tcpListener.Dispose();
  3614. foreach (Socket tcpProxyListener in _tcpProxyListeners)
  3615. tcpProxyListener.Dispose();
  3616. foreach (Socket tlsListener in _tlsListeners)
  3617. tlsListener.Dispose();
  3618. foreach (QuicListener quicListener in _quicListeners)
  3619. await quicListener.DisposeAsync();
  3620. _udpListeners.Clear();
  3621. _udpProxyListeners.Clear();
  3622. _tcpListeners.Clear();
  3623. _tcpProxyListeners.Clear();
  3624. _tlsListeners.Clear();
  3625. _quicListeners.Clear();
  3626. await StopDoHAsync();
  3627. _state = ServiceState.Stopped;
  3628. }
  3629. public Task<DnsDatagram> DirectQueryAsync(DnsQuestionRecord question, int timeout = 4000, bool skipDnsAppAuthoritativeRequestHandlers = false)
  3630. {
  3631. return DirectQueryAsync(new DnsDatagram(0, false, DnsOpcode.StandardQuery, false, false, true, false, false, false, DnsResponseCode.NoError, new DnsQuestionRecord[] { question }), timeout, skipDnsAppAuthoritativeRequestHandlers);
  3632. }
  3633. public Task<DnsDatagram> DirectQueryAsync(DnsDatagram request, int timeout = 4000, bool skipDnsAppAuthoritativeRequestHandlers = false)
  3634. {
  3635. return ProcessQueryAsync(request, IPENDPOINT_ANY_0, DnsTransportProtocol.Tcp, true, skipDnsAppAuthoritativeRequestHandlers, null).WithTimeout(timeout);
  3636. }
  3637. Task<DnsDatagram> IDnsClient.ResolveAsync(DnsQuestionRecord question, CancellationToken cancellationToken)
  3638. {
  3639. return DirectQueryAsync(question);
  3640. }
  3641. #endregion
  3642. #region properties
  3643. public string ServerDomain
  3644. {
  3645. get { return _serverDomain; }
  3646. set
  3647. {
  3648. if (!_serverDomain.Equals(value))
  3649. {
  3650. if (DnsClient.IsDomainNameUnicode(value))
  3651. value = DnsClient.ConvertDomainNameToAscii(value);
  3652. DnsClient.IsDomainNameValid(value, true);
  3653. if (IPAddress.TryParse(value, out _))
  3654. throw new DnsServerException("Invalid domain name [" + value + "]: IP address cannot be used for DNS server domain name.");
  3655. _serverDomain = value.ToLower();
  3656. _authZoneManager.ServerDomain = _serverDomain;
  3657. _allowedZoneManager.ServerDomain = _serverDomain;
  3658. _blockedZoneManager.ServerDomain = _serverDomain;
  3659. _blockListZoneManager.ServerDomain = _serverDomain;
  3660. UpdateThisServer();
  3661. }
  3662. }
  3663. }
  3664. public string ConfigFolder
  3665. { get { return _configFolder; } }
  3666. public IReadOnlyList<IPEndPoint> LocalEndPoints
  3667. {
  3668. get { return _localEndPoints; }
  3669. set { _localEndPoints = value; }
  3670. }
  3671. public LogManager LogManager
  3672. {
  3673. get { return _log; }
  3674. set { _log = value; }
  3675. }
  3676. public NameServerAddress ThisServer
  3677. { get { return _thisServer; } }
  3678. public AuthZoneManager AuthZoneManager
  3679. { get { return _authZoneManager; } }
  3680. public AllowedZoneManager AllowedZoneManager
  3681. { get { return _allowedZoneManager; } }
  3682. public BlockedZoneManager BlockedZoneManager
  3683. { get { return _blockedZoneManager; } }
  3684. public BlockListZoneManager BlockListZoneManager
  3685. { get { return _blockListZoneManager; } }
  3686. public CacheZoneManager CacheZoneManager
  3687. { get { return _cacheZoneManager; } }
  3688. public DnsApplicationManager DnsApplicationManager
  3689. { get { return _dnsApplicationManager; } }
  3690. public IDnsCache DnsCache
  3691. { get { return _dnsCache; } }
  3692. public StatsManager StatsManager
  3693. { get { return _stats; } }
  3694. public bool PreferIPv6
  3695. {
  3696. get { return _preferIPv6; }
  3697. set
  3698. {
  3699. if (_preferIPv6 != value)
  3700. {
  3701. _preferIPv6 = value;
  3702. //init udp socket pool async for port randomization
  3703. ThreadPool.QueueUserWorkItem(delegate (object state)
  3704. {
  3705. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  3706. UdpClientConnection.CreateSocketPool(_preferIPv6);
  3707. });
  3708. }
  3709. }
  3710. }
  3711. public ushort UdpPayloadSize
  3712. {
  3713. get { return _udpPayloadSize; }
  3714. set
  3715. {
  3716. if ((value < 512) || (value > 4096))
  3717. throw new ArgumentOutOfRangeException(nameof(UdpPayloadSize), "Invalid EDNS UDP payload size: valid range is 512-4096 bytes.");
  3718. _udpPayloadSize = value;
  3719. }
  3720. }
  3721. public bool DnssecValidation
  3722. {
  3723. get { return _dnssecValidation; }
  3724. set
  3725. {
  3726. if (_dnssecValidation != value)
  3727. {
  3728. if (!_dnssecValidation)
  3729. _cacheZoneManager.Flush(); //flush cache to remove non validated data
  3730. _dnssecValidation = value;
  3731. }
  3732. }
  3733. }
  3734. public bool EDnsClientSubnet
  3735. {
  3736. get { return _eDnsClientSubnet; }
  3737. set
  3738. {
  3739. if (_eDnsClientSubnet != value)
  3740. {
  3741. _eDnsClientSubnet = value;
  3742. if (!_eDnsClientSubnet)
  3743. {
  3744. ThreadPool.QueueUserWorkItem(delegate (object state)
  3745. {
  3746. _cacheZoneManager.DeleteEDnsClientSubnetData();
  3747. });
  3748. }
  3749. }
  3750. }
  3751. }
  3752. public byte EDnsClientSubnetIPv4PrefixLength
  3753. {
  3754. get { return _eDnsClientSubnetIPv4PrefixLength; }
  3755. set
  3756. {
  3757. if (value > 32)
  3758. throw new ArgumentOutOfRangeException(nameof(EDnsClientSubnetIPv4PrefixLength), "EDNS Client Subnet IPv4 prefix length cannot be greater than 32.");
  3759. _eDnsClientSubnetIPv4PrefixLength = value;
  3760. }
  3761. }
  3762. public byte EDnsClientSubnetIPv6PrefixLength
  3763. {
  3764. get { return _eDnsClientSubnetIPv6PrefixLength; }
  3765. set
  3766. {
  3767. if (value > 64)
  3768. throw new ArgumentOutOfRangeException(nameof(EDnsClientSubnetIPv6PrefixLength), "EDNS Client Subnet IPv6 prefix length cannot be greater than 64.");
  3769. _eDnsClientSubnetIPv6PrefixLength = value;
  3770. }
  3771. }
  3772. public int QpmLimitRequests
  3773. {
  3774. get { return _qpmLimitRequests; }
  3775. set
  3776. {
  3777. if (value < 0)
  3778. throw new ArgumentOutOfRangeException(nameof(QpmLimitRequests), "Value cannot be less than 0.");
  3779. if (_qpmLimitRequests != value)
  3780. {
  3781. if ((_qpmLimitRequests == 0) || (value == 0))
  3782. {
  3783. _qpmLimitRequests = value;
  3784. ResetQpsLimitTimer();
  3785. }
  3786. else
  3787. {
  3788. _qpmLimitRequests = value;
  3789. }
  3790. }
  3791. }
  3792. }
  3793. public int QpmLimitErrors
  3794. {
  3795. get { return _qpmLimitErrors; }
  3796. set
  3797. {
  3798. if (value < 0)
  3799. throw new ArgumentOutOfRangeException(nameof(QpmLimitErrors), "Value cannot be less than 0.");
  3800. if (_qpmLimitErrors != value)
  3801. {
  3802. if ((_qpmLimitErrors == 0) || (value == 0))
  3803. {
  3804. _qpmLimitErrors = value;
  3805. ResetQpsLimitTimer();
  3806. }
  3807. else
  3808. {
  3809. _qpmLimitErrors = value;
  3810. }
  3811. }
  3812. }
  3813. }
  3814. public int QpmLimitSampleMinutes
  3815. {
  3816. get { return _qpmLimitSampleMinutes; }
  3817. set
  3818. {
  3819. if ((value < 1) || (value > 60))
  3820. throw new ArgumentOutOfRangeException(nameof(QpmLimitSampleMinutes), "Valid range is between 1 and 60 minutes.");
  3821. _qpmLimitSampleMinutes = value;
  3822. }
  3823. }
  3824. public int QpmLimitIPv4PrefixLength
  3825. {
  3826. get { return _qpmLimitIPv4PrefixLength; }
  3827. set
  3828. {
  3829. if ((value < 0) || (value > 32))
  3830. throw new ArgumentOutOfRangeException(nameof(QpmLimitIPv4PrefixLength), "Valid range is between 0 and 32.");
  3831. _qpmLimitIPv4PrefixLength = value;
  3832. }
  3833. }
  3834. public int QpmLimitIPv6PrefixLength
  3835. {
  3836. get { return _qpmLimitIPv6PrefixLength; }
  3837. set
  3838. {
  3839. if ((value < 0) || (value > 64))
  3840. throw new ArgumentOutOfRangeException(nameof(QpmLimitIPv6PrefixLength), "Valid range is between 0 and 64.");
  3841. _qpmLimitIPv6PrefixLength = value;
  3842. }
  3843. }
  3844. public int ClientTimeout
  3845. {
  3846. get { return _clientTimeout; }
  3847. set
  3848. {
  3849. if ((value < 1000) || (value > 10000))
  3850. throw new ArgumentOutOfRangeException(nameof(ClientTimeout), "Valid range is from 1000 to 10000.");
  3851. _clientTimeout = value;
  3852. }
  3853. }
  3854. public int TcpSendTimeout
  3855. {
  3856. get { return _tcpSendTimeout; }
  3857. set
  3858. {
  3859. if ((value < 1000) || (value > 90000))
  3860. throw new ArgumentOutOfRangeException(nameof(TcpSendTimeout), "Valid range is from 1000 to 90000.");
  3861. _tcpSendTimeout = value;
  3862. }
  3863. }
  3864. public int TcpReceiveTimeout
  3865. {
  3866. get { return _tcpReceiveTimeout; }
  3867. set
  3868. {
  3869. if ((value < 1000) || (value > 90000))
  3870. throw new ArgumentOutOfRangeException(nameof(TcpReceiveTimeout), "Valid range is from 1000 to 90000.");
  3871. _tcpReceiveTimeout = value;
  3872. }
  3873. }
  3874. public int QuicIdleTimeout
  3875. {
  3876. get { return _quicIdleTimeout; }
  3877. set
  3878. {
  3879. if ((value < 1000) || (value > 90000))
  3880. throw new ArgumentOutOfRangeException(nameof(QuicIdleTimeout), "Valid range is from 1000 to 90000.");
  3881. _quicIdleTimeout = value;
  3882. }
  3883. }
  3884. public int QuicMaxInboundStreams
  3885. {
  3886. get { return _quicMaxInboundStreams; }
  3887. set
  3888. {
  3889. if ((value < 0) || (value > 1000))
  3890. throw new ArgumentOutOfRangeException(nameof(QuicMaxInboundStreams), "Valid range is from 1 to 1000.");
  3891. _quicMaxInboundStreams = value;
  3892. }
  3893. }
  3894. public int ListenBacklog
  3895. {
  3896. get { return _listenBacklog; }
  3897. set { _listenBacklog = value; }
  3898. }
  3899. public bool EnableDnsOverUdpProxy
  3900. {
  3901. get { return _enableDnsOverUdpProxy; }
  3902. set { _enableDnsOverUdpProxy = value; }
  3903. }
  3904. public bool EnableDnsOverTcpProxy
  3905. {
  3906. get { return _enableDnsOverTcpProxy; }
  3907. set { _enableDnsOverTcpProxy = value; }
  3908. }
  3909. public bool EnableDnsOverHttp
  3910. {
  3911. get { return _enableDnsOverHttp; }
  3912. set { _enableDnsOverHttp = value; }
  3913. }
  3914. public bool EnableDnsOverTls
  3915. {
  3916. get { return _enableDnsOverTls; }
  3917. set { _enableDnsOverTls = value; }
  3918. }
  3919. public bool EnableDnsOverHttps
  3920. {
  3921. get { return _enableDnsOverHttps; }
  3922. set { _enableDnsOverHttps = value; }
  3923. }
  3924. public bool EnableDnsOverQuic
  3925. {
  3926. get { return _enableDnsOverQuic; }
  3927. set { _enableDnsOverQuic = value; }
  3928. }
  3929. public int DnsOverUdpProxyPort
  3930. {
  3931. get { return _dnsOverUdpProxyPort; }
  3932. set { _dnsOverUdpProxyPort = value; }
  3933. }
  3934. public int DnsOverTcpProxyPort
  3935. {
  3936. get { return _dnsOverTcpProxyPort; }
  3937. set { _dnsOverTcpProxyPort = value; }
  3938. }
  3939. public int DnsOverHttpPort
  3940. {
  3941. get { return _dnsOverHttpPort; }
  3942. set { _dnsOverHttpPort = value; }
  3943. }
  3944. public int DnsOverTlsPort
  3945. {
  3946. get { return _dnsOverTlsPort; }
  3947. set { _dnsOverTlsPort = value; }
  3948. }
  3949. public int DnsOverHttpsPort
  3950. {
  3951. get { return _dnsOverHttpsPort; }
  3952. set { _dnsOverHttpsPort = value; }
  3953. }
  3954. public int DnsOverQuicPort
  3955. {
  3956. get { return _dnsOverQuicPort; }
  3957. set { _dnsOverQuicPort = value; }
  3958. }
  3959. public X509Certificate2Collection CertificateCollection
  3960. {
  3961. get { return _certificateCollection; }
  3962. set
  3963. {
  3964. if (value is null)
  3965. {
  3966. _certificateCollection = null;
  3967. _sslServerAuthenticationOptions = null;
  3968. _quicSslServerAuthenticationOptions = null;
  3969. }
  3970. else
  3971. {
  3972. X509Certificate2 serverCertificate = null;
  3973. foreach (X509Certificate2 certificate in value)
  3974. {
  3975. if (certificate.HasPrivateKey)
  3976. {
  3977. serverCertificate = certificate;
  3978. break;
  3979. }
  3980. }
  3981. if (serverCertificate is null)
  3982. throw new ArgumentException("DNS Server TLS certificate file must contain a certificate with private key.");
  3983. _certificateCollection = value;
  3984. SslStreamCertificateContext certificateContext = SslStreamCertificateContext.Create(serverCertificate, _certificateCollection, false);
  3985. _sslServerAuthenticationOptions = new SslServerAuthenticationOptions()
  3986. {
  3987. ServerCertificateContext = certificateContext
  3988. };
  3989. _quicSslServerAuthenticationOptions = new SslServerAuthenticationOptions()
  3990. {
  3991. ApplicationProtocols = quicApplicationProtocols,
  3992. ServerCertificateContext = certificateContext
  3993. };
  3994. }
  3995. }
  3996. }
  3997. public IReadOnlyDictionary<string, TsigKey> TsigKeys
  3998. {
  3999. get { return _tsigKeys; }
  4000. set { _tsigKeys = value; }
  4001. }
  4002. public DnsServerRecursion Recursion
  4003. {
  4004. get { return _recursion; }
  4005. set
  4006. {
  4007. if (_recursion != value)
  4008. {
  4009. if ((_recursion == DnsServerRecursion.Deny) || (value == DnsServerRecursion.Deny))
  4010. {
  4011. _recursion = value;
  4012. ResetPrefetchTimers();
  4013. }
  4014. else
  4015. {
  4016. _recursion = value;
  4017. }
  4018. }
  4019. }
  4020. }
  4021. public IReadOnlyCollection<NetworkAddress> RecursionDeniedNetworks
  4022. {
  4023. get { return _recursionDeniedNetworks; }
  4024. set
  4025. {
  4026. if ((value is not null) && (value.Count > byte.MaxValue))
  4027. throw new ArgumentOutOfRangeException(nameof(RecursionDeniedNetworks), "Networks cannot be more than 255.");
  4028. _recursionDeniedNetworks = value;
  4029. }
  4030. }
  4031. public IReadOnlyCollection<NetworkAddress> RecursionAllowedNetworks
  4032. {
  4033. get { return _recursionAllowedNetworks; }
  4034. set
  4035. {
  4036. if ((value is not null) && (value.Count > byte.MaxValue))
  4037. throw new ArgumentOutOfRangeException(nameof(RecursionAllowedNetworks), "Networks cannot be more than 255.");
  4038. _recursionAllowedNetworks = value;
  4039. }
  4040. }
  4041. public bool RandomizeName
  4042. {
  4043. get { return _randomizeName; }
  4044. set { _randomizeName = value; }
  4045. }
  4046. public bool QnameMinimization
  4047. {
  4048. get { return _qnameMinimization; }
  4049. set { _qnameMinimization = value; }
  4050. }
  4051. public bool NsRevalidation
  4052. {
  4053. get { return _nsRevalidation; }
  4054. set { _nsRevalidation = value; }
  4055. }
  4056. public int ResolverRetries
  4057. {
  4058. get { return _resolverRetries; }
  4059. set
  4060. {
  4061. if ((value < 1) || (value > 10))
  4062. throw new ArgumentOutOfRangeException(nameof(ResolverRetries), "Valid range is from 1 to 10.");
  4063. _resolverRetries = value;
  4064. }
  4065. }
  4066. public int ResolverTimeout
  4067. {
  4068. get { return _resolverTimeout; }
  4069. set
  4070. {
  4071. if ((value < 1000) || (value > 10000))
  4072. throw new ArgumentOutOfRangeException(nameof(ResolverTimeout), "Valid range is from 1000 to 10000.");
  4073. _resolverTimeout = value;
  4074. }
  4075. }
  4076. public int ResolverMaxStackCount
  4077. {
  4078. get { return _resolverMaxStackCount; }
  4079. set
  4080. {
  4081. if ((value < 10) || (value > 30))
  4082. throw new ArgumentOutOfRangeException(nameof(ResolverMaxStackCount), "Valid range is from 10 to 30.");
  4083. _resolverMaxStackCount = value;
  4084. }
  4085. }
  4086. public bool ServeStale
  4087. {
  4088. get { return _serveStale; }
  4089. set { _serveStale = value; }
  4090. }
  4091. public int CachePrefetchEligibility
  4092. {
  4093. get { return _cachePrefetchEligibility; }
  4094. set
  4095. {
  4096. if (value < 2)
  4097. throw new ArgumentOutOfRangeException(nameof(CachePrefetchEligibility), "Valid value is greater that or equal to 2.");
  4098. _cachePrefetchEligibility = value;
  4099. }
  4100. }
  4101. public int CachePrefetchTrigger
  4102. {
  4103. get { return _cachePrefetchTrigger; }
  4104. set
  4105. {
  4106. if (value < 0)
  4107. throw new ArgumentOutOfRangeException(nameof(CachePrefetchTrigger), "Valid value is greater that or equal to 0.");
  4108. if (_cachePrefetchTrigger != value)
  4109. {
  4110. if ((_cachePrefetchTrigger == 0) || (value == 0))
  4111. {
  4112. _cachePrefetchTrigger = value;
  4113. ResetPrefetchTimers();
  4114. }
  4115. else
  4116. {
  4117. _cachePrefetchTrigger = value;
  4118. }
  4119. }
  4120. }
  4121. }
  4122. public int CachePrefetchSampleIntervalInMinutes
  4123. {
  4124. get { return _cachePrefetchSampleIntervalInMinutes; }
  4125. set
  4126. {
  4127. if ((value < 1) || (value > 60))
  4128. throw new ArgumentOutOfRangeException(nameof(CachePrefetchSampleIntervalInMinutes), "Valid range is between 1 and 60 minutes.");
  4129. _cachePrefetchSampleIntervalInMinutes = value;
  4130. }
  4131. }
  4132. public int CachePrefetchSampleEligibilityHitsPerHour
  4133. {
  4134. get { return _cachePrefetchSampleEligibilityHitsPerHour; }
  4135. set
  4136. {
  4137. if (value < 1)
  4138. throw new ArgumentOutOfRangeException(nameof(CachePrefetchSampleEligibilityHitsPerHour), "Valid value is greater than or equal to 1.");
  4139. _cachePrefetchSampleEligibilityHitsPerHour = value;
  4140. }
  4141. }
  4142. public bool EnableBlocking
  4143. {
  4144. get { return _enableBlocking; }
  4145. set { _enableBlocking = value; }
  4146. }
  4147. public bool AllowTxtBlockingReport
  4148. {
  4149. get { return _allowTxtBlockingReport; }
  4150. set { _allowTxtBlockingReport = value; }
  4151. }
  4152. public DnsServerBlockingType BlockingType
  4153. {
  4154. get { return _blockingType; }
  4155. set { _blockingType = value; }
  4156. }
  4157. public IReadOnlyCollection<DnsARecordData> CustomBlockingARecords
  4158. {
  4159. get { return _customBlockingARecords; }
  4160. set
  4161. {
  4162. if (value is null)
  4163. value = Array.Empty<DnsARecordData>();
  4164. _customBlockingARecords = value;
  4165. }
  4166. }
  4167. public IReadOnlyCollection<DnsAAAARecordData> CustomBlockingAAAARecords
  4168. {
  4169. get { return _customBlockingAAAARecords; }
  4170. set
  4171. {
  4172. if (value is null)
  4173. value = Array.Empty<DnsAAAARecordData>();
  4174. _customBlockingAAAARecords = value;
  4175. }
  4176. }
  4177. public NetProxy Proxy
  4178. {
  4179. get { return _proxy; }
  4180. set { _proxy = value; }
  4181. }
  4182. public IReadOnlyList<NameServerAddress> Forwarders
  4183. {
  4184. get { return _forwarders; }
  4185. set { _forwarders = value; }
  4186. }
  4187. public int ForwarderRetries
  4188. {
  4189. get { return _forwarderRetries; }
  4190. set
  4191. {
  4192. if ((value < 1) || (value > 10))
  4193. throw new ArgumentOutOfRangeException(nameof(ForwarderRetries), "Valid range is from 1 to 10.");
  4194. _forwarderRetries = value;
  4195. }
  4196. }
  4197. public int ForwarderTimeout
  4198. {
  4199. get { return _forwarderTimeout; }
  4200. set
  4201. {
  4202. if ((value < 1000) || (value > 10000))
  4203. throw new ArgumentOutOfRangeException(nameof(ForwarderTimeout), "Valid range is from 1000 to 10000.");
  4204. _forwarderTimeout = value;
  4205. }
  4206. }
  4207. public int ForwarderConcurrency
  4208. {
  4209. get { return _forwarderConcurrency; }
  4210. set
  4211. {
  4212. if ((value < 1) || (value > 10))
  4213. throw new ArgumentOutOfRangeException(nameof(ForwarderConcurrency), "Valid range is from 1 to 10.");
  4214. _forwarderConcurrency = value;
  4215. }
  4216. }
  4217. public LogManager QueryLogManager
  4218. {
  4219. get { return _queryLog; }
  4220. set { _queryLog = value; }
  4221. }
  4222. #endregion
  4223. class CacheRefreshSample
  4224. {
  4225. public CacheRefreshSample(DnsQuestionRecord sampleQuestion, IReadOnlyList<DnsResourceRecord> conditionalForwarders)
  4226. {
  4227. SampleQuestion = sampleQuestion;
  4228. ConditionalForwarders = conditionalForwarders;
  4229. }
  4230. public DnsQuestionRecord SampleQuestion { get; }
  4231. public IReadOnlyList<DnsResourceRecord> ConditionalForwarders { get; }
  4232. }
  4233. class RecursiveResolveResponse
  4234. {
  4235. public RecursiveResolveResponse(DnsDatagram response, DnsDatagram checkingDisabledResponse)
  4236. {
  4237. Response = response;
  4238. CheckingDisabledResponse = checkingDisabledResponse;
  4239. }
  4240. public DnsDatagram Response { get; }
  4241. public DnsDatagram CheckingDisabledResponse { get; }
  4242. }
  4243. }
  4244. #pragma warning restore CA2252 // This API requires opting into preview features
  4245. #pragma warning restore CA1416 // Validate platform compatibility
  4246. }