DnsServer.cs 225 KB

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