host_resolver_manager.cc 163 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/dns/host_resolver_manager.h"
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <iterator>
  8. #include <limits>
  9. #include <memory>
  10. #include <numeric>
  11. #include <set>
  12. #include <string>
  13. #include <tuple>
  14. #include <unordered_set>
  15. #include <utility>
  16. #include <vector>
  17. #include "base/bind.h"
  18. #include "base/callback.h"
  19. #include "base/callback_helpers.h"
  20. #include "base/check_op.h"
  21. #include "base/compiler_specific.h"
  22. #include "base/containers/circular_deque.h"
  23. #include "base/containers/contains.h"
  24. #include "base/containers/cxx20_erase.h"
  25. #include "base/containers/flat_set.h"
  26. #include "base/containers/linked_list.h"
  27. #include "base/debug/debugger.h"
  28. #include "base/feature_list.h"
  29. #include "base/functional/identity.h"
  30. #include "base/logging.h"
  31. #include "base/memory/ptr_util.h"
  32. #include "base/memory/raw_ptr.h"
  33. #include "base/memory/safe_ref.h"
  34. #include "base/memory/weak_ptr.h"
  35. #include "base/metrics/field_trial.h"
  36. #include "base/metrics/field_trial_params.h"
  37. #include "base/metrics/histogram_functions.h"
  38. #include "base/metrics/histogram_macros.h"
  39. #include "base/no_destructor.h"
  40. #include "base/numerics/safe_conversions.h"
  41. #include "base/observer_list.h"
  42. #include "base/ranges/algorithm.h"
  43. #include "base/sequence_checker.h"
  44. #include "base/stl_util.h"
  45. #include "base/strings/strcat.h"
  46. #include "base/strings/string_number_conversions.h"
  47. #include "base/strings/string_piece.h"
  48. #include "base/strings/string_split.h"
  49. #include "base/strings/string_util.h"
  50. #include "base/strings/stringprintf.h"
  51. #include "base/strings/utf_string_conversions.h"
  52. #include "base/task/single_thread_task_runner.h"
  53. #include "base/task/thread_pool.h"
  54. #include "base/threading/scoped_blocking_call.h"
  55. #include "base/threading/thread_task_runner_handle.h"
  56. #include "base/time/default_tick_clock.h"
  57. #include "base/time/time.h"
  58. #include "base/trace_event/trace_event.h"
  59. #include "base/values.h"
  60. #include "build/build_config.h"
  61. #include "net/base/address_family.h"
  62. #include "net/base/address_list.h"
  63. #include "net/base/completion_once_callback.h"
  64. #include "net/base/features.h"
  65. #include "net/base/host_port_pair.h"
  66. #include "net/base/ip_address.h"
  67. #include "net/base/ip_endpoint.h"
  68. #include "net/base/net_errors.h"
  69. #include "net/base/network_interfaces.h"
  70. #include "net/base/network_isolation_key.h"
  71. #include "net/base/prioritized_dispatcher.h"
  72. #include "net/base/request_priority.h"
  73. #include "net/base/trace_constants.h"
  74. #include "net/base/url_util.h"
  75. #include "net/dns/address_sorter.h"
  76. #include "net/dns/dns_alias_utility.h"
  77. #include "net/dns/dns_client.h"
  78. #include "net/dns/dns_reloader.h"
  79. #include "net/dns/dns_response.h"
  80. #include "net/dns/dns_response_result_extractor.h"
  81. #include "net/dns/dns_transaction.h"
  82. #include "net/dns/dns_util.h"
  83. #include "net/dns/host_cache.h"
  84. #include "net/dns/host_resolver_mdns_listener_impl.h"
  85. #include "net/dns/host_resolver_mdns_task.h"
  86. #include "net/dns/host_resolver_proc.h"
  87. #include "net/dns/host_resolver_results.h"
  88. #include "net/dns/httpssvc_metrics.h"
  89. #include "net/dns/mdns_client.h"
  90. #include "net/dns/public/dns_protocol.h"
  91. #include "net/dns/public/dns_query_type.h"
  92. #include "net/dns/public/resolve_error_info.h"
  93. #include "net/dns/public/secure_dns_mode.h"
  94. #include "net/dns/public/secure_dns_policy.h"
  95. #include "net/dns/public/util.h"
  96. #include "net/dns/record_parsed.h"
  97. #include "net/dns/resolve_context.h"
  98. #include "net/dns/test_dns_config_service.h"
  99. #include "net/log/net_log.h"
  100. #include "net/log/net_log_capture_mode.h"
  101. #include "net/log/net_log_event_type.h"
  102. #include "net/log/net_log_source.h"
  103. #include "net/log/net_log_source_type.h"
  104. #include "net/log/net_log_with_source.h"
  105. #include "net/socket/client_socket_factory.h"
  106. #include "net/socket/datagram_client_socket.h"
  107. #include "third_party/abseil-cpp/absl/types/optional.h"
  108. #include "third_party/abseil-cpp/absl/types/variant.h"
  109. #include "url/scheme_host_port.h"
  110. #include "url/url_constants.h"
  111. #if BUILDFLAG(ENABLE_MDNS)
  112. #include "net/dns/mdns_client_impl.h"
  113. #endif
  114. #if BUILDFLAG(IS_WIN)
  115. #include <Winsock2.h>
  116. #include "net/base/winsock_init.h"
  117. #endif
  118. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  119. #include <net/if.h>
  120. #include "net/base/sys_addrinfo.h"
  121. #if BUILDFLAG(IS_ANDROID)
  122. #include "base/android/build_info.h"
  123. #include "net/android/network_library.h"
  124. #else // !BUILDFLAG(IS_ANDROID)
  125. #include <ifaddrs.h>
  126. #endif // BUILDFLAG(IS_ANDROID)
  127. #endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  128. namespace net {
  129. namespace {
  130. // Limit the size of hostnames that will be resolved to combat issues in
  131. // some platform's resolvers.
  132. const size_t kMaxHostLength = 4096;
  133. // Default TTL for successful resolutions with ProcTask.
  134. const unsigned kCacheEntryTTLSeconds = 60;
  135. // Default TTL for unsuccessful resolutions with ProcTask.
  136. const unsigned kNegativeCacheEntryTTLSeconds = 0;
  137. // Minimum TTL for successful resolutions with DnsTask.
  138. const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds;
  139. // Time between IPv6 probes, i.e. for how long results of each IPv6 probe are
  140. // cached.
  141. const int kIPv6ProbePeriodMs = 1000;
  142. // Google DNS address used for IPv6 probes.
  143. const uint8_t kIPv6ProbeAddress[] = {0x20, 0x01, 0x48, 0x60, 0x48, 0x60,
  144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x00, 0x00, 0x88, 0x88};
  146. enum DnsResolveStatus {
  147. RESOLVE_STATUS_DNS_SUCCESS = 0,
  148. RESOLVE_STATUS_PROC_SUCCESS,
  149. RESOLVE_STATUS_FAIL,
  150. RESOLVE_STATUS_SUSPECT_NETBIOS,
  151. RESOLVE_STATUS_MAX
  152. };
  153. // ICANN uses this localhost address to indicate a name collision.
  154. //
  155. // The policy in Chromium is to fail host resolving if it resolves to
  156. // this special address.
  157. //
  158. // Not however that IP literals are exempt from this policy, so it is still
  159. // possible to navigate to http://127.0.53.53/ directly.
  160. //
  161. // For more details: https://www.icann.org/news/announcement-2-2014-08-01-en
  162. const uint8_t kIcanNameCollisionIp[] = {127, 0, 53, 53};
  163. bool ContainsIcannNameCollisionIp(const std::vector<IPEndPoint>& endpoints) {
  164. for (const auto& endpoint : endpoints) {
  165. const IPAddress& addr = endpoint.address();
  166. if (addr.IsIPv4() && IPAddressStartsWith(addr, kIcanNameCollisionIp)) {
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. // True if |hostname| ends with either ".local" or ".local.".
  173. bool ResemblesMulticastDNSName(base::StringPiece hostname) {
  174. const char kSuffix[] = ".local.";
  175. const size_t kSuffixLen = sizeof(kSuffix) - 1;
  176. const size_t kSuffixLenTrimmed = kSuffixLen - 1;
  177. if (!hostname.empty() && hostname.back() == '.') {
  178. return hostname.size() > kSuffixLen &&
  179. !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix);
  180. }
  181. return hostname.size() > kSuffixLenTrimmed &&
  182. !hostname.compare(hostname.size() - kSuffixLenTrimmed,
  183. kSuffixLenTrimmed, kSuffix, kSuffixLenTrimmed);
  184. }
  185. bool ConfigureAsyncDnsNoFallbackFieldTrial() {
  186. const bool kDefault = false;
  187. // Configure the AsyncDns field trial as follows:
  188. // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true,
  189. // groups AsyncDnsA and AsyncDnsB: return false,
  190. // groups SystemDnsA and SystemDnsB: return false,
  191. // otherwise (trial absent): return default.
  192. std::string group_name = base::FieldTrialList::FindFullName("AsyncDns");
  193. if (!group_name.empty()) {
  194. return base::StartsWith(group_name, "AsyncDnsNoFallback",
  195. base::CompareCase::INSENSITIVE_ASCII);
  196. }
  197. return kDefault;
  198. }
  199. const base::FeatureParam<base::TaskPriority>::Option prio_modes[] = {
  200. {base::TaskPriority::USER_VISIBLE, "default"},
  201. {base::TaskPriority::USER_BLOCKING, "user_blocking"}};
  202. const base::Feature kSystemResolverPriorityExperiment = {
  203. "SystemResolverPriorityExperiment", base::FEATURE_DISABLED_BY_DEFAULT};
  204. const base::FeatureParam<base::TaskPriority> priority_mode{
  205. &kSystemResolverPriorityExperiment, "mode",
  206. base::TaskPriority::USER_VISIBLE, &prio_modes};
  207. //-----------------------------------------------------------------------------
  208. // Returns true if it can determine that only loopback addresses are configured.
  209. // i.e. if only 127.0.0.1 and ::1 are routable.
  210. // Also returns false if it cannot determine this.
  211. bool HaveOnlyLoopbackAddresses() {
  212. base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
  213. base::BlockingType::WILL_BLOCK);
  214. #if BUILDFLAG(IS_WIN)
  215. // TODO(wtc): implement with the GetAdaptersAddresses function.
  216. NOTIMPLEMENTED();
  217. return false;
  218. #elif BUILDFLAG(IS_ANDROID)
  219. return android::HaveOnlyLoopbackAddresses();
  220. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  221. struct ifaddrs* interface_addr = nullptr;
  222. int rv = getifaddrs(&interface_addr);
  223. if (rv != 0) {
  224. DVPLOG(1) << "getifaddrs() failed";
  225. return false;
  226. }
  227. bool result = true;
  228. for (struct ifaddrs* interface = interface_addr; interface != nullptr;
  229. interface = interface->ifa_next) {
  230. if (!(IFF_UP & interface->ifa_flags))
  231. continue;
  232. if (IFF_LOOPBACK & interface->ifa_flags)
  233. continue;
  234. const struct sockaddr* addr = interface->ifa_addr;
  235. if (!addr)
  236. continue;
  237. if (addr->sa_family == AF_INET6) {
  238. // Safe cast since this is AF_INET6.
  239. const struct sockaddr_in6* addr_in6 =
  240. reinterpret_cast<const struct sockaddr_in6*>(addr);
  241. const struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
  242. if (IN6_IS_ADDR_LOOPBACK(sin6_addr) || IN6_IS_ADDR_LINKLOCAL(sin6_addr))
  243. continue;
  244. }
  245. if (addr->sa_family != AF_INET6 && addr->sa_family != AF_INET)
  246. continue;
  247. result = false;
  248. break;
  249. }
  250. freeifaddrs(interface_addr);
  251. return result;
  252. #endif // defined(various platforms)
  253. }
  254. // Creates NetLog parameters when the resolve failed.
  255. base::Value NetLogProcTaskFailedParams(uint32_t attempt_number,
  256. int net_error,
  257. int os_error) {
  258. base::Value::Dict dict;
  259. if (attempt_number)
  260. dict.Set("attempt_number", base::saturated_cast<int>(attempt_number));
  261. dict.Set("net_error", net_error);
  262. if (os_error) {
  263. dict.Set("os_error", os_error);
  264. #if BUILDFLAG(IS_WIN)
  265. // Map the error code to a human-readable string.
  266. LPWSTR error_string = nullptr;
  267. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  268. nullptr, // Use the internal message table.
  269. os_error,
  270. 0, // Use default language.
  271. (LPWSTR)&error_string,
  272. 0, // Buffer size.
  273. nullptr); // Arguments (unused).
  274. dict.Set("os_error_string", base::WideToUTF8(error_string));
  275. LocalFree(error_string);
  276. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  277. dict.Set("os_error_string", gai_strerror(os_error));
  278. #endif
  279. }
  280. return base::Value(std::move(dict));
  281. }
  282. // Creates NetLog parameters when the DnsTask failed.
  283. base::Value NetLogDnsTaskFailedParams(
  284. int net_error,
  285. absl::optional<DnsQueryType> failed_transaction_type,
  286. absl::optional<base::TimeDelta> ttl,
  287. const HostCache::Entry* saved_results) {
  288. base::Value::Dict dict;
  289. if (failed_transaction_type) {
  290. dict.Set("dns_query_type",
  291. base::strict_cast<int>(failed_transaction_type.value()));
  292. }
  293. if (ttl)
  294. dict.Set("error_ttl_sec",
  295. base::saturated_cast<int>(ttl.value().InSeconds()));
  296. dict.Set("net_error", net_error);
  297. if (saved_results)
  298. dict.Set("saved_results", saved_results->NetLogParams());
  299. return base::Value(std::move(dict));
  300. }
  301. base::Value NetLogDnsTaskExtractionFailureParams(
  302. DnsResponseResultExtractor::ExtractionError extraction_error,
  303. DnsQueryType dns_query_type,
  304. const HostCache::Entry& results) {
  305. base::Value::Dict dict;
  306. dict.Set("extraction_error", base::strict_cast<int>(extraction_error));
  307. dict.Set("dns_query_type", base::strict_cast<int>(dns_query_type));
  308. dict.Set("results", results.NetLogParams());
  309. return base::Value(std::move(dict));
  310. }
  311. // Creates NetLog parameters for HOST_RESOLVER_MANAGER_JOB_ATTACH/DETACH events.
  312. base::Value NetLogJobAttachParams(const NetLogSource& source,
  313. RequestPriority priority) {
  314. base::Value::Dict dict;
  315. source.AddToEventParameters(dict);
  316. dict.Set("priority", RequestPriorityToString(priority));
  317. return base::Value(std::move(dict));
  318. }
  319. base::Value NetLogIPv6AvailableParams(bool ipv6_available, bool cached) {
  320. base::Value::Dict dict;
  321. dict.Set("ipv6_available", ipv6_available);
  322. dict.Set("cached", cached);
  323. return base::Value(std::move(dict));
  324. }
  325. // The logging routines are defined here because some requests are resolved
  326. // without a Request object.
  327. //-----------------------------------------------------------------------------
  328. // Maximum of 64 concurrent resolver threads (excluding retries).
  329. // Between 2010 and 2020, the limit was set to 6 because of a report of a broken
  330. // home router that would fail in the presence of more simultaneous queries.
  331. // In 2020, we conducted an experiment to see if this kind of router was still
  332. // present on the Internet, and found no evidence of any remaining issues, so
  333. // we increased the limit to 64 at that time.
  334. const size_t kDefaultMaxProcTasks = 64u;
  335. PrioritizedDispatcher::Limits GetDispatcherLimits(
  336. const HostResolver::ManagerOptions& options) {
  337. PrioritizedDispatcher::Limits limits(NUM_PRIORITIES,
  338. options.max_concurrent_resolves);
  339. // If not using default, do not use the field trial.
  340. if (limits.total_jobs != HostResolver::ManagerOptions::kDefaultParallelism)
  341. return limits;
  342. // Default, without trial is no reserved slots.
  343. limits.total_jobs = kDefaultMaxProcTasks;
  344. // Parallelism is determined by the field trial.
  345. std::string group =
  346. base::FieldTrialList::FindFullName("HostResolverDispatch");
  347. if (group.empty())
  348. return limits;
  349. // The format of the group name is a list of non-negative integers separated
  350. // by ':'. Each of the elements in the list corresponds to an element in
  351. // |reserved_slots|, except the last one which is the |total_jobs|.
  352. std::vector<base::StringPiece> group_parts = base::SplitStringPiece(
  353. group, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  354. if (group_parts.size() != NUM_PRIORITIES + 1) {
  355. NOTREACHED();
  356. return limits;
  357. }
  358. std::vector<size_t> parsed(group_parts.size());
  359. for (size_t i = 0; i < group_parts.size(); ++i) {
  360. if (!base::StringToSizeT(group_parts[i], &parsed[i])) {
  361. NOTREACHED();
  362. return limits;
  363. }
  364. }
  365. const size_t total_jobs = parsed.back();
  366. parsed.pop_back();
  367. const size_t total_reserved_slots =
  368. std::accumulate(parsed.begin(), parsed.end(), 0u);
  369. // There must be some unreserved slots available for the all priorities.
  370. if (total_reserved_slots > total_jobs ||
  371. (total_reserved_slots == total_jobs && parsed[MINIMUM_PRIORITY] == 0)) {
  372. NOTREACHED();
  373. return limits;
  374. }
  375. limits.total_jobs = total_jobs;
  376. limits.reserved_slots = parsed;
  377. return limits;
  378. }
  379. // Keeps track of the highest priority.
  380. class PriorityTracker {
  381. public:
  382. explicit PriorityTracker(RequestPriority initial_priority)
  383. : highest_priority_(initial_priority) {}
  384. RequestPriority highest_priority() const { return highest_priority_; }
  385. size_t total_count() const { return total_count_; }
  386. void Add(RequestPriority req_priority) {
  387. ++total_count_;
  388. ++counts_[req_priority];
  389. if (highest_priority_ < req_priority)
  390. highest_priority_ = req_priority;
  391. }
  392. void Remove(RequestPriority req_priority) {
  393. DCHECK_GT(total_count_, 0u);
  394. DCHECK_GT(counts_[req_priority], 0u);
  395. --total_count_;
  396. --counts_[req_priority];
  397. size_t i;
  398. for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i) {
  399. }
  400. highest_priority_ = static_cast<RequestPriority>(i);
  401. // In absence of requests, default to MINIMUM_PRIORITY.
  402. if (total_count_ == 0)
  403. DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_);
  404. }
  405. private:
  406. RequestPriority highest_priority_;
  407. size_t total_count_ = 0;
  408. size_t counts_[NUM_PRIORITIES] = {};
  409. };
  410. base::Value NetLogResults(const HostCache::Entry& results) {
  411. base::Value::Dict dict;
  412. dict.Set("results", results.NetLogParams());
  413. return base::Value(std::move(dict));
  414. }
  415. base::Value ToLogStringValue(const HostResolver::Host& host) {
  416. if (absl::holds_alternative<url::SchemeHostPort>(host))
  417. return base::Value(absl::get<url::SchemeHostPort>(host).Serialize());
  418. return base::Value(absl::get<HostPortPair>(host).ToString());
  419. }
  420. base::Value ToLogStringValue(
  421. const absl::variant<url::SchemeHostPort, std::string>& host) {
  422. if (absl::holds_alternative<url::SchemeHostPort>(host)) {
  423. return base::Value(absl::get<url::SchemeHostPort>(host).Serialize());
  424. }
  425. return base::Value(absl::get<std::string>(host));
  426. }
  427. // Returns empty string if `host` has no known scheme.
  428. base::StringPiece GetScheme(
  429. const absl::variant<url::SchemeHostPort, std::string>& host) {
  430. if (absl::holds_alternative<url::SchemeHostPort>(host))
  431. return absl::get<url::SchemeHostPort>(host).scheme();
  432. return base::StringPiece();
  433. }
  434. base::StringPiece GetHostname(const HostResolver::Host& host) {
  435. if (absl::holds_alternative<url::SchemeHostPort>(host)) {
  436. base::StringPiece hostname = absl::get<url::SchemeHostPort>(host).host();
  437. if (hostname.size() >= 2 && hostname.front() == '[' &&
  438. hostname.back() == ']') {
  439. hostname = hostname.substr(1, hostname.size() - 2);
  440. }
  441. return hostname;
  442. }
  443. return absl::get<HostPortPair>(host).host();
  444. }
  445. base::StringPiece GetHostname(
  446. const absl::variant<url::SchemeHostPort, std::string>& host) {
  447. if (absl::holds_alternative<url::SchemeHostPort>(host)) {
  448. base::StringPiece hostname = absl::get<url::SchemeHostPort>(host).host();
  449. if (hostname.size() >= 2 && hostname.front() == '[' &&
  450. hostname.back() == ']') {
  451. hostname = hostname.substr(1, hostname.size() - 2);
  452. }
  453. return hostname;
  454. }
  455. return absl::get<std::string>(host);
  456. }
  457. uint16_t GetPort(const HostResolver::Host& host) {
  458. if (absl::holds_alternative<url::SchemeHostPort>(host)) {
  459. return absl::get<url::SchemeHostPort>(host).port();
  460. }
  461. return absl::get<HostPortPair>(host).port();
  462. }
  463. // Only use scheme/port in JobKey if `https_svcb_options_enabled` is true
  464. // (or the query is explicitly for HTTPS). Otherwise DNS will not give different
  465. // results for the same hostname.
  466. absl::variant<url::SchemeHostPort, std::string> CreateHostForJobKey(
  467. const HostResolver::Host& input,
  468. DnsQueryType query_type,
  469. bool https_svcb_options_enabled) {
  470. if ((https_svcb_options_enabled || query_type == DnsQueryType::HTTPS) &&
  471. absl::holds_alternative<url::SchemeHostPort>(input)) {
  472. return absl::get<url::SchemeHostPort>(input);
  473. }
  474. return std::string(GetHostname(input));
  475. }
  476. DnsResponse CreateFakeEmptyResponse(base::StringPiece hostname,
  477. DnsQueryType query_type) {
  478. std::string qname;
  479. CHECK(DNSDomainFromDot(hostname, &qname));
  480. return DnsResponse::CreateEmptyNoDataResponse(
  481. /*id=*/0u, /*is_authoritative=*/true, qname,
  482. DnsQueryTypeToQtype(query_type));
  483. }
  484. std::vector<IPEndPoint> FilterAddresses(std::vector<IPEndPoint> addresses,
  485. DnsQueryTypeSet query_types) {
  486. DCHECK(!query_types.Has(DnsQueryType::UNSPECIFIED));
  487. DCHECK(!query_types.Empty());
  488. const AddressFamily want_family =
  489. HostResolver::DnsQueryTypeSetToAddressFamily(query_types);
  490. if (want_family == ADDRESS_FAMILY_UNSPECIFIED)
  491. return addresses;
  492. // Keep only the endpoints that match `want_family`.
  493. addresses.erase(
  494. base::ranges::remove_if(
  495. addresses,
  496. [want_family](AddressFamily family) { return family != want_family; },
  497. &IPEndPoint::GetFamily),
  498. addresses.end());
  499. return addresses;
  500. }
  501. } // namespace
  502. //-----------------------------------------------------------------------------
  503. bool ResolveLocalHostname(base::StringPiece host,
  504. std::vector<IPEndPoint>* address_list) {
  505. address_list->clear();
  506. if (!IsLocalHostname(host))
  507. return false;
  508. address_list->emplace_back(IPAddress::IPv6Localhost(), 0);
  509. address_list->emplace_back(IPAddress::IPv4Localhost(), 0);
  510. return true;
  511. }
  512. // Holds the callback and request parameters for an outstanding request.
  513. //
  514. // The RequestImpl is owned by the end user of host resolution. Deletion prior
  515. // to the request having completed means the request was cancelled by the
  516. // caller.
  517. //
  518. // Both the RequestImpl and its associated Job hold non-owning pointers to each
  519. // other. Care must be taken to clear the corresponding pointer when
  520. // cancellation is initiated by the Job (OnJobCancelled) vs by the end user
  521. // (~RequestImpl).
  522. class HostResolverManager::RequestImpl
  523. : public HostResolver::ResolveHostRequest,
  524. public base::LinkNode<HostResolverManager::RequestImpl> {
  525. public:
  526. RequestImpl(NetLogWithSource source_net_log,
  527. HostResolver::Host request_host,
  528. NetworkIsolationKey network_isolation_key,
  529. absl::optional<ResolveHostParameters> optional_parameters,
  530. base::WeakPtr<ResolveContext> resolve_context,
  531. HostCache* host_cache,
  532. base::WeakPtr<HostResolverManager> resolver,
  533. const base::TickClock* tick_clock)
  534. : source_net_log_(std::move(source_net_log)),
  535. request_host_(std::move(request_host)),
  536. network_isolation_key_(
  537. base::FeatureList::IsEnabled(
  538. features::kSplitHostCacheByNetworkIsolationKey)
  539. ? std::move(network_isolation_key)
  540. : NetworkIsolationKey()),
  541. parameters_(optional_parameters ? std::move(optional_parameters).value()
  542. : ResolveHostParameters()),
  543. resolve_context_(std::move(resolve_context)),
  544. host_cache_(host_cache),
  545. host_resolver_flags_(
  546. HostResolver::ParametersToHostResolverFlags(parameters_)),
  547. priority_(parameters_.initial_priority),
  548. resolver_(std::move(resolver)),
  549. tick_clock_(tick_clock) {}
  550. RequestImpl(const RequestImpl&) = delete;
  551. RequestImpl& operator=(const RequestImpl&) = delete;
  552. ~RequestImpl() override;
  553. int Start(CompletionOnceCallback callback) override {
  554. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  555. DCHECK(callback);
  556. // Start() may only be called once per request.
  557. CHECK(!job_.has_value());
  558. DCHECK(!complete_);
  559. DCHECK(!callback_);
  560. // Parent HostResolver must still be alive to call Start().
  561. DCHECK(resolver_);
  562. if (!resolve_context_) {
  563. complete_ = true;
  564. resolver_.reset();
  565. set_error_info(ERR_CONTEXT_SHUT_DOWN, false);
  566. return ERR_NAME_NOT_RESOLVED;
  567. }
  568. LogStartRequest();
  569. int rv = resolver_->Resolve(this);
  570. DCHECK(!complete_);
  571. if (rv == ERR_IO_PENDING) {
  572. CHECK(job_.has_value());
  573. callback_ = std::move(callback);
  574. } else {
  575. CHECK(!job_.has_value());
  576. complete_ = true;
  577. LogFinishRequest(rv, false /* async_completion */);
  578. }
  579. resolver_.reset();
  580. return rv;
  581. }
  582. const AddressList* GetAddressResults() const override {
  583. DCHECK(complete_);
  584. return base::OptionalOrNullptr(legacy_address_results_);
  585. }
  586. const std::vector<HostResolverEndpointResult>* GetEndpointResults()
  587. const override {
  588. DCHECK(complete_);
  589. return base::OptionalOrNullptr(endpoint_results_);
  590. }
  591. const absl::optional<std::vector<std::string>>& GetTextResults()
  592. const override {
  593. DCHECK(complete_);
  594. static const base::NoDestructor<absl::optional<std::vector<std::string>>>
  595. nullopt_result;
  596. return results_ ? results_.value().text_records() : *nullopt_result;
  597. }
  598. const absl::optional<std::vector<HostPortPair>>& GetHostnameResults()
  599. const override {
  600. DCHECK(complete_);
  601. static const base::NoDestructor<absl::optional<std::vector<HostPortPair>>>
  602. nullopt_result;
  603. return results_ ? results_.value().hostnames() : *nullopt_result;
  604. }
  605. const std::set<std::string>* GetDnsAliasResults() const override {
  606. DCHECK(complete_);
  607. // If `include_canonical_name` param was true, should only ever have at most
  608. // a single alias, representing the expected "canonical name".
  609. #if DCHECK_IS_ON()
  610. if (parameters().include_canonical_name && fixed_up_dns_alias_results_) {
  611. DCHECK_LE(fixed_up_dns_alias_results_->size(), 1u);
  612. if (GetAddressResults()) {
  613. std::set<std::string> address_list_aliases_set(
  614. GetAddressResults()->dns_aliases().begin(),
  615. GetAddressResults()->dns_aliases().end());
  616. DCHECK(address_list_aliases_set == fixed_up_dns_alias_results_.value());
  617. }
  618. }
  619. #endif // DCHECK_IS_ON()
  620. return base::OptionalOrNullptr(fixed_up_dns_alias_results_);
  621. }
  622. const std::vector<bool>* GetExperimentalResultsForTesting() const override {
  623. DCHECK(complete_);
  624. return results_ ? results_.value().https_record_compatibility() : nullptr;
  625. }
  626. net::ResolveErrorInfo GetResolveErrorInfo() const override {
  627. DCHECK(complete_);
  628. return error_info_;
  629. }
  630. const absl::optional<HostCache::EntryStaleness>& GetStaleInfo()
  631. const override {
  632. DCHECK(complete_);
  633. return stale_info_;
  634. }
  635. void ChangeRequestPriority(RequestPriority priority) override;
  636. void set_results(HostCache::Entry results) {
  637. // Should only be called at most once and before request is marked
  638. // completed.
  639. DCHECK(!complete_);
  640. DCHECK(!results_);
  641. DCHECK(!parameters_.is_speculative);
  642. results_ = std::move(results);
  643. FixUpEndpointAndAliasResults();
  644. }
  645. void set_error_info(int error, bool is_secure_network_error) {
  646. error_info_ = ResolveErrorInfo(error, is_secure_network_error);
  647. }
  648. void set_stale_info(HostCache::EntryStaleness stale_info) {
  649. // Should only be called at most once and before request is marked
  650. // completed.
  651. DCHECK(!complete_);
  652. DCHECK(!stale_info_);
  653. DCHECK(!parameters_.is_speculative);
  654. stale_info_ = std::move(stale_info);
  655. }
  656. void AssignJob(base::SafeRef<Job> job) {
  657. CHECK(!job_.has_value());
  658. job_ = std::move(job);
  659. }
  660. bool HasJob() const { return job_.has_value(); }
  661. // Gets the Job's key. Crashes if no Job has been assigned.
  662. const JobKey& GetJobKey() const;
  663. // Unassigns the Job without calling completion callback.
  664. void OnJobCancelled(const JobKey& key);
  665. // Cleans up Job assignment, marks request completed, and calls the completion
  666. // callback. |is_secure_network_error| indicates whether |error| came from a
  667. // secure DNS lookup.
  668. void OnJobCompleted(const JobKey& job_key,
  669. int error,
  670. bool is_secure_network_error);
  671. // NetLog for the source, passed in HostResolver::Resolve.
  672. const NetLogWithSource& source_net_log() { return source_net_log_; }
  673. const HostResolver::Host& request_host() const { return request_host_; }
  674. const NetworkIsolationKey& network_isolation_key() const {
  675. return network_isolation_key_;
  676. }
  677. const ResolveHostParameters& parameters() const { return parameters_; }
  678. ResolveContext* resolve_context() const { return resolve_context_.get(); }
  679. HostCache* host_cache() const { return host_cache_; }
  680. HostResolverFlags host_resolver_flags() const { return host_resolver_flags_; }
  681. RequestPriority priority() const { return priority_; }
  682. void set_priority(RequestPriority priority) { priority_ = priority; }
  683. bool complete() const { return complete_; }
  684. private:
  685. void FixUpEndpointAndAliasResults() {
  686. DCHECK(results_.has_value());
  687. DCHECK(!legacy_address_results_.has_value());
  688. DCHECK(!endpoint_results_.has_value());
  689. DCHECK(!fixed_up_dns_alias_results_.has_value());
  690. endpoint_results_ = results_.value().GetEndpoints();
  691. if (endpoint_results_.has_value()) {
  692. DCHECK(results_.value().aliases());
  693. fixed_up_dns_alias_results_ = *results_.value().aliases();
  694. if (parameters().include_canonical_name) {
  695. DCHECK_LE(fixed_up_dns_alias_results_.value().size(), 1u);
  696. } else {
  697. // Expect `aliases()` results to already be fixed up.
  698. DCHECK(dns_alias_utility::FixUpDnsAliases(
  699. fixed_up_dns_alias_results_.value()) ==
  700. fixed_up_dns_alias_results_.value());
  701. }
  702. legacy_address_results_ = HostResolver::EndpointResultToAddressList(
  703. endpoint_results_.value(), fixed_up_dns_alias_results_.value());
  704. }
  705. }
  706. // Logging and metrics for when a request has just been started.
  707. void LogStartRequest() {
  708. DCHECK(request_time_.is_null());
  709. request_time_ = tick_clock_->NowTicks();
  710. source_net_log_.BeginEvent(
  711. NetLogEventType::HOST_RESOLVER_MANAGER_REQUEST, [this] {
  712. base::Value::Dict dict;
  713. dict.Set("host", ToLogStringValue(request_host_));
  714. dict.Set("dns_query_type",
  715. base::strict_cast<int>(parameters_.dns_query_type));
  716. dict.Set("allow_cached_response",
  717. parameters_.cache_usage !=
  718. ResolveHostParameters::CacheUsage::DISALLOWED);
  719. dict.Set("is_speculative", parameters_.is_speculative);
  720. dict.Set("network_isolation_key",
  721. network_isolation_key_.ToDebugString());
  722. dict.Set("secure_dns_policy",
  723. base::strict_cast<int>(parameters_.secure_dns_policy));
  724. return base::Value(std::move(dict));
  725. });
  726. }
  727. // Logging and metrics for when a request has just completed (before its
  728. // callback is run).
  729. void LogFinishRequest(int net_error, bool async_completion) {
  730. source_net_log_.EndEventWithNetErrorCode(
  731. NetLogEventType::HOST_RESOLVER_MANAGER_REQUEST, net_error);
  732. if (!parameters_.is_speculative) {
  733. DCHECK(!request_time_.is_null());
  734. base::TimeDelta duration = tick_clock_->NowTicks() - request_time_;
  735. UMA_HISTOGRAM_MEDIUM_TIMES("Net.DNS.Request.TotalTime", duration);
  736. if (async_completion)
  737. UMA_HISTOGRAM_MEDIUM_TIMES("Net.DNS.Request.TotalTimeAsync", duration);
  738. }
  739. }
  740. // Logs when a request has been cancelled.
  741. void LogCancelRequest() {
  742. source_net_log_.AddEvent(NetLogEventType::CANCELLED);
  743. source_net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_REQUEST);
  744. }
  745. const NetLogWithSource source_net_log_;
  746. const HostResolver::Host request_host_;
  747. const NetworkIsolationKey network_isolation_key_;
  748. ResolveHostParameters parameters_;
  749. base::WeakPtr<ResolveContext> resolve_context_;
  750. const raw_ptr<HostCache> host_cache_;
  751. const HostResolverFlags host_resolver_flags_;
  752. RequestPriority priority_;
  753. // The resolve job that this request is dependent on.
  754. absl::optional<base::SafeRef<Job>> job_;
  755. base::WeakPtr<HostResolverManager> resolver_ = nullptr;
  756. // The user's callback to invoke when the request completes.
  757. CompletionOnceCallback callback_;
  758. bool complete_ = false;
  759. absl::optional<HostCache::Entry> results_;
  760. absl::optional<HostCache::EntryStaleness> stale_info_;
  761. absl::optional<AddressList> legacy_address_results_;
  762. absl::optional<std::vector<HostResolverEndpointResult>> endpoint_results_;
  763. absl::optional<std::set<std::string>> fixed_up_dns_alias_results_;
  764. ResolveErrorInfo error_info_;
  765. const raw_ptr<const base::TickClock> tick_clock_;
  766. base::TimeTicks request_time_;
  767. SEQUENCE_CHECKER(sequence_checker_);
  768. };
  769. class HostResolverManager::ProbeRequestImpl
  770. : public HostResolver::ProbeRequest,
  771. public ResolveContext::DohStatusObserver {
  772. public:
  773. ProbeRequestImpl(base::WeakPtr<ResolveContext> context,
  774. base::WeakPtr<HostResolverManager> resolver)
  775. : context_(std::move(context)), resolver_(std::move(resolver)) {}
  776. ProbeRequestImpl(const ProbeRequestImpl&) = delete;
  777. ProbeRequestImpl& operator=(const ProbeRequestImpl&) = delete;
  778. ~ProbeRequestImpl() override {
  779. // Ensure that observers are deregistered to avoid wasting memory.
  780. if (context_)
  781. context_->UnregisterDohStatusObserver(this);
  782. }
  783. int Start() override {
  784. DCHECK(resolver_);
  785. DCHECK(!runner_);
  786. if (!context_)
  787. return ERR_CONTEXT_SHUT_DOWN;
  788. context_->RegisterDohStatusObserver(this);
  789. StartRunner(false /* network_change */);
  790. return ERR_IO_PENDING;
  791. }
  792. // ResolveContext::DohStatusObserver
  793. void OnSessionChanged() override { CancelRunner(); }
  794. void OnDohServerUnavailable(bool network_change) override {
  795. // Start the runner asynchronously, as this may trigger reentrant calls into
  796. // HostResolverManager, which are not allowed during notification handling.
  797. base::SequencedTaskRunnerHandle::Get()->PostTask(
  798. FROM_HERE,
  799. base::BindOnce(&ProbeRequestImpl::StartRunner,
  800. weak_ptr_factory_.GetWeakPtr(), network_change));
  801. }
  802. private:
  803. void StartRunner(bool network_change) {
  804. DCHECK(resolver_);
  805. DCHECK(!resolver_->invalidation_in_progress_);
  806. if (!context_)
  807. return; // Reachable if the context ends before a posted task runs.
  808. if (!runner_)
  809. runner_ = resolver_->CreateDohProbeRunner(context_.get());
  810. if (runner_)
  811. runner_->Start(network_change);
  812. }
  813. void CancelRunner() {
  814. runner_.reset();
  815. // Cancel any asynchronous StartRunner() calls.
  816. weak_ptr_factory_.InvalidateWeakPtrs();
  817. }
  818. base::WeakPtr<ResolveContext> context_;
  819. std::unique_ptr<DnsProbeRunner> runner_;
  820. base::WeakPtr<HostResolverManager> resolver_;
  821. base::WeakPtrFactory<ProbeRequestImpl> weak_ptr_factory_{this};
  822. };
  823. //------------------------------------------------------------------------------
  824. // Calls HostResolverProc in ThreadPool. Performs retries if necessary.
  825. //
  826. // In non-test code, the HostResolverProc is always SystemHostResolverProc,
  827. // which calls a platform API that implements host resolution.
  828. //
  829. // Whenever we try to resolve the host, we post a delayed task to check if host
  830. // resolution (OnLookupComplete) is completed or not. If the original attempt
  831. // hasn't completed, then we start another attempt for host resolution. We take
  832. // the results from the first attempt that finishes and ignore the results from
  833. // all other attempts.
  834. //
  835. // TODO(szym): Move to separate source file for testing and mocking.
  836. //
  837. class HostResolverManager::ProcTask {
  838. public:
  839. typedef base::OnceCallback<void(int net_error, const AddressList& addr_list)>
  840. Callback;
  841. ProcTask(std::string hostname,
  842. AddressFamily address_family,
  843. HostResolverFlags flags,
  844. const ProcTaskParams& params,
  845. Callback callback,
  846. scoped_refptr<base::TaskRunner> proc_task_runner,
  847. const NetLogWithSource& job_net_log,
  848. const base::TickClock* tick_clock,
  849. handles::NetworkHandle network)
  850. : hostname_(std::move(hostname)),
  851. address_family_(address_family),
  852. flags_(flags),
  853. params_(params),
  854. callback_(std::move(callback)),
  855. network_task_runner_(base::ThreadTaskRunnerHandle::Get()),
  856. proc_task_runner_(std::move(proc_task_runner)),
  857. net_log_(job_net_log),
  858. tick_clock_(tick_clock),
  859. network_(network) {
  860. DCHECK(callback_);
  861. if (!params_.resolver_proc.get())
  862. params_.resolver_proc = HostResolverProc::GetDefault();
  863. // If default is unset, use the system proc.
  864. if (!params_.resolver_proc.get())
  865. params_.resolver_proc = base::MakeRefCounted<SystemHostResolverProc>();
  866. }
  867. ProcTask(const ProcTask&) = delete;
  868. ProcTask& operator=(const ProcTask&) = delete;
  869. // Cancels this ProcTask. Any outstanding resolve attempts running on worker
  870. // thread will continue running, but they will post back to the network thread
  871. // before checking their WeakPtrs to find that this task is cancelled.
  872. ~ProcTask() {
  873. DCHECK(network_task_runner_->BelongsToCurrentThread());
  874. // If this is cancellation, log the EndEvent (otherwise this was logged in
  875. // OnLookupComplete()).
  876. if (!was_completed())
  877. net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_PROC_TASK);
  878. }
  879. void Start() {
  880. DCHECK(network_task_runner_->BelongsToCurrentThread());
  881. DCHECK(!was_completed());
  882. net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_MANAGER_PROC_TASK);
  883. StartLookupAttempt();
  884. }
  885. bool was_completed() const {
  886. DCHECK(network_task_runner_->BelongsToCurrentThread());
  887. return callback_.is_null();
  888. }
  889. private:
  890. using AttemptCompletionCallback = base::OnceCallback<
  891. void(const AddressList& results, int error, const int os_error)>;
  892. void StartLookupAttempt() {
  893. DCHECK(network_task_runner_->BelongsToCurrentThread());
  894. DCHECK(!was_completed());
  895. base::TimeTicks start_time = tick_clock_->NowTicks();
  896. ++attempt_number_;
  897. // Dispatch the lookup attempt to a worker thread.
  898. AttemptCompletionCallback completion_callback = base::BindOnce(
  899. &ProcTask::OnLookupAttemptComplete, weak_ptr_factory_.GetWeakPtr(),
  900. start_time, attempt_number_, tick_clock_);
  901. proc_task_runner_->PostTask(
  902. FROM_HERE,
  903. base::BindOnce(&ProcTask::DoLookup, hostname_, address_family_, flags_,
  904. params_.resolver_proc, network_task_runner_,
  905. std::move(completion_callback), network_));
  906. net_log_.AddEventWithIntParams(
  907. NetLogEventType::HOST_RESOLVER_MANAGER_ATTEMPT_STARTED,
  908. "attempt_number", attempt_number_);
  909. // If the results aren't received within a given time, RetryIfNotComplete
  910. // will start a new attempt if none of the outstanding attempts have
  911. // completed yet.
  912. // Use a WeakPtr to avoid keeping the ProcTask alive after completion or
  913. // cancellation.
  914. if (attempt_number_ <= params_.max_retry_attempts) {
  915. network_task_runner_->PostDelayedTask(
  916. FROM_HERE,
  917. base::BindOnce(&ProcTask::StartLookupAttempt,
  918. weak_ptr_factory_.GetWeakPtr()),
  919. params_.unresponsive_delay *
  920. std::pow(params_.retry_factor, attempt_number_ - 1));
  921. }
  922. }
  923. // WARNING: This code runs in ThreadPool with CONTINUE_ON_SHUTDOWN. The
  924. // shutdown code cannot wait for it to finish, so this code must be very
  925. // careful about using other objects (like MessageLoops, Singletons, etc).
  926. // During shutdown these objects may no longer exist.
  927. static void DoLookup(
  928. std::string hostname,
  929. AddressFamily address_family,
  930. HostResolverFlags flags,
  931. scoped_refptr<HostResolverProc> resolver_proc,
  932. scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
  933. AttemptCompletionCallback completion_callback,
  934. handles::NetworkHandle network) {
  935. AddressList results;
  936. int os_error = 0;
  937. int error = resolver_proc->Resolve(hostname, address_family, flags,
  938. &results, &os_error, network);
  939. network_task_runner->PostTask(
  940. FROM_HERE, base::BindOnce(std::move(completion_callback), results,
  941. error, os_error));
  942. }
  943. // Callback for when DoLookup() completes (runs on task runner thread). Now
  944. // that we're back in the network thread, checks that |proc_task| is still
  945. // valid, and if so, passes back to the object.
  946. static void OnLookupAttemptComplete(base::WeakPtr<ProcTask> proc_task,
  947. const base::TimeTicks& start_time,
  948. const uint32_t attempt_number,
  949. const base::TickClock* tick_clock,
  950. const AddressList& results,
  951. int error,
  952. const int os_error) {
  953. TRACE_EVENT0(NetTracingCategory(), "ProcTask::OnLookupComplete");
  954. // If results are empty, we should return an error.
  955. bool empty_list_on_ok = (error == OK && results.empty());
  956. if (empty_list_on_ok)
  957. error = ERR_NAME_NOT_RESOLVED;
  958. // Ideally the following code would be part of host_resolver_proc.cc,
  959. // however it isn't safe to call NetworkChangeNotifier from worker threads.
  960. // So do it here on the IO thread instead.
  961. if (error != OK && NetworkChangeNotifier::IsOffline())
  962. error = ERR_INTERNET_DISCONNECTED;
  963. if (!proc_task)
  964. return;
  965. proc_task->OnLookupComplete(results, start_time, attempt_number, error,
  966. os_error);
  967. }
  968. void OnLookupComplete(const AddressList& results,
  969. const base::TimeTicks& start_time,
  970. const uint32_t attempt_number,
  971. int error,
  972. const int os_error) {
  973. DCHECK(network_task_runner_->BelongsToCurrentThread());
  974. DCHECK(!was_completed());
  975. // Invalidate WeakPtrs to cancel handling of all outstanding lookup attempts
  976. // and retries.
  977. weak_ptr_factory_.InvalidateWeakPtrs();
  978. if (error != OK) {
  979. net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_PROC_TASK, [&] {
  980. return NetLogProcTaskFailedParams(0, error, os_error);
  981. });
  982. net_log_.AddEvent(
  983. NetLogEventType::HOST_RESOLVER_MANAGER_ATTEMPT_FINISHED, [&] {
  984. return NetLogProcTaskFailedParams(attempt_number, error, os_error);
  985. });
  986. } else {
  987. net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_PROC_TASK,
  988. [&] { return results.NetLogParams(); });
  989. net_log_.AddEventWithIntParams(
  990. NetLogEventType::HOST_RESOLVER_MANAGER_ATTEMPT_FINISHED,
  991. "attempt_number", attempt_number);
  992. }
  993. std::move(callback_).Run(error, results);
  994. }
  995. const std::string hostname_;
  996. const AddressFamily address_family_;
  997. const HostResolverFlags flags_;
  998. // Holds an owning reference to the HostResolverProc that we are going to use.
  999. // This may not be the current resolver procedure by the time we call
  1000. // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning
  1001. // reference ensures that it remains valid until we are done.
  1002. ProcTaskParams params_;
  1003. // The listener to the results of this ProcTask.
  1004. Callback callback_;
  1005. // Used to post events onto the network thread.
  1006. scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
  1007. // Used to post blocking HostResolverProc tasks.
  1008. scoped_refptr<base::TaskRunner> proc_task_runner_;
  1009. // Keeps track of the number of attempts we have made so far to resolve the
  1010. // host. Whenever we start an attempt to resolve the host, we increase this
  1011. // number.
  1012. uint32_t attempt_number_ = 0;
  1013. NetLogWithSource net_log_;
  1014. raw_ptr<const base::TickClock> tick_clock_;
  1015. // Network to perform DNS lookups for.
  1016. handles::NetworkHandle network_;
  1017. // Used to loop back from the blocking lookup attempt tasks as well as from
  1018. // delayed retry tasks. Invalidate WeakPtrs on completion and cancellation to
  1019. // cancel handling of such posted tasks.
  1020. base::WeakPtrFactory<ProcTask> weak_ptr_factory_{this};
  1021. };
  1022. //-----------------------------------------------------------------------------
  1023. // Resolves the hostname using DnsTransaction, which is a full implementation of
  1024. // a DNS stub resolver. One DnsTransaction is created for each resolution
  1025. // needed, which for AF_UNSPEC resolutions includes both A and AAAA. The
  1026. // transactions are scheduled separately and started separately.
  1027. //
  1028. // TODO(szym): This could be moved to separate source file as well.
  1029. class HostResolverManager::DnsTask : public base::SupportsWeakPtr<DnsTask> {
  1030. public:
  1031. class Delegate {
  1032. public:
  1033. virtual void OnDnsTaskComplete(base::TimeTicks start_time,
  1034. bool allow_fallback,
  1035. HostCache::Entry results,
  1036. bool secure) = 0;
  1037. // Called when one or more transactions complete or get cancelled, but only
  1038. // if more transactions are needed. If no more transactions are needed,
  1039. // expect `OnDnsTaskComplete()` to be called instead.
  1040. virtual void OnIntermediateTransactionsComplete() = 0;
  1041. virtual RequestPriority priority() const = 0;
  1042. virtual void AddTransactionTimeQueued(base::TimeDelta time_queued) = 0;
  1043. protected:
  1044. Delegate() = default;
  1045. virtual ~Delegate() = default;
  1046. };
  1047. DnsTask(DnsClient* client,
  1048. absl::variant<url::SchemeHostPort, std::string> host,
  1049. DnsQueryTypeSet query_types,
  1050. ResolveContext* resolve_context,
  1051. bool secure,
  1052. SecureDnsMode secure_dns_mode,
  1053. Delegate* delegate,
  1054. const NetLogWithSource& job_net_log,
  1055. const base::TickClock* tick_clock,
  1056. bool fallback_available,
  1057. const HostResolver::HttpsSvcbOptions& https_svcb_options)
  1058. : client_(client),
  1059. host_(std::move(host)),
  1060. resolve_context_(resolve_context->AsSafeRef()),
  1061. secure_(secure),
  1062. secure_dns_mode_(secure_dns_mode),
  1063. delegate_(delegate),
  1064. net_log_(job_net_log),
  1065. tick_clock_(tick_clock),
  1066. task_start_time_(tick_clock_->NowTicks()),
  1067. fallback_available_(fallback_available),
  1068. https_svcb_options_(https_svcb_options) {
  1069. DCHECK(client_);
  1070. DCHECK(delegate_);
  1071. if (!secure_) {
  1072. DCHECK(client_->CanUseInsecureDnsTransactions());
  1073. }
  1074. PushTransactionsNeeded(MaybeDisableAdditionalQueries(query_types));
  1075. }
  1076. DnsTask(const DnsTask&) = delete;
  1077. DnsTask& operator=(const DnsTask&) = delete;
  1078. int num_additional_transactions_needed() const {
  1079. return base::checked_cast<int>(transactions_needed_.size());
  1080. }
  1081. int num_transactions_in_progress() const {
  1082. return base::checked_cast<int>(transactions_in_progress_.size());
  1083. }
  1084. bool secure() const { return secure_; }
  1085. void StartNextTransaction() {
  1086. DCHECK_GE(num_additional_transactions_needed(), 1);
  1087. if (!any_transaction_started_) {
  1088. net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_MANAGER_DNS_TASK,
  1089. [&] { return NetLogDnsTaskCreationParams(); });
  1090. }
  1091. any_transaction_started_ = true;
  1092. TransactionInfo transaction_info = std::move(transactions_needed_.front());
  1093. transactions_needed_.pop_front();
  1094. DCHECK(IsAddressType(transaction_info.type) || secure_ ||
  1095. client_->CanQueryAdditionalTypesViaInsecureDns());
  1096. // Record how long this transaction has been waiting to be created.
  1097. base::TimeDelta time_queued = tick_clock_->NowTicks() - task_start_time_;
  1098. UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.JobQueueTime.PerTransaction",
  1099. time_queued);
  1100. delegate_->AddTransactionTimeQueued(time_queued);
  1101. CreateAndStartTransaction(std::move(transaction_info));
  1102. }
  1103. private:
  1104. enum class TransactionErrorBehavior {
  1105. // Errors lead to task fallback (immediately unless another pending/started
  1106. // transaction has the `kFatalOrEmpty` behavior).
  1107. kFallback,
  1108. // Transaction errors are treated as if a NOERROR response were received,
  1109. // allowing task success if other transactions complete successfully.
  1110. kSynthesizeEmpty,
  1111. // Transaction errors are potentially fatal (determined by
  1112. // `OnTransactionComplete` and often its helper
  1113. // `IsFatalTransactionFailure()`) for the entire Job and may disallow
  1114. // fallback. Otherwise, same as `kSynthesizeEmpty`.
  1115. // TODO(crbug.com/1264933): Implement the fatality behavior.
  1116. kFatalOrEmpty,
  1117. };
  1118. struct TransactionInfo {
  1119. explicit TransactionInfo(DnsQueryType type,
  1120. TransactionErrorBehavior error_behavior =
  1121. TransactionErrorBehavior::kFallback)
  1122. : type(type), error_behavior(error_behavior) {}
  1123. TransactionInfo(TransactionInfo&&) = default;
  1124. TransactionInfo& operator=(TransactionInfo&&) = default;
  1125. bool operator<(const TransactionInfo& other) const {
  1126. return std::tie(type, error_behavior, transaction) <
  1127. std::tie(other.type, other.error_behavior, other.transaction);
  1128. }
  1129. DnsQueryType type;
  1130. TransactionErrorBehavior error_behavior;
  1131. std::unique_ptr<DnsTransaction> transaction;
  1132. };
  1133. base::Value NetLogDnsTaskCreationParams() {
  1134. base::Value::Dict dict;
  1135. dict.Set("secure", secure());
  1136. base::Value::List transactions_needed_value;
  1137. for (const TransactionInfo& info : transactions_needed_) {
  1138. base::Value::Dict transaction_dict;
  1139. transaction_dict.Set("dns_query_type", base::strict_cast<int>(info.type));
  1140. transactions_needed_value.Append(std::move(transaction_dict));
  1141. }
  1142. dict.Set("transactions_needed", std::move(transactions_needed_value));
  1143. return base::Value(std::move(dict));
  1144. }
  1145. base::Value NetLogDnsTaskTimeoutParams() {
  1146. base::Value::Dict dict;
  1147. if (!transactions_in_progress_.empty()) {
  1148. base::Value::List list;
  1149. for (const TransactionInfo& info : transactions_in_progress_) {
  1150. base::Value::Dict transaction_dict;
  1151. transaction_dict.Set("dns_query_type",
  1152. base::strict_cast<int>(info.type));
  1153. list.Append(std::move(transaction_dict));
  1154. }
  1155. dict.Set("started_transactions", std::move(list));
  1156. }
  1157. if (!transactions_needed_.empty()) {
  1158. base::Value::List list;
  1159. for (const TransactionInfo& info : transactions_needed_) {
  1160. base::Value::Dict transaction_dict;
  1161. transaction_dict.Set("dns_query_type",
  1162. base::strict_cast<int>(info.type));
  1163. list.Append(std::move(transaction_dict));
  1164. }
  1165. dict.Set("queued_transactions", std::move(list));
  1166. }
  1167. return base::Value(std::move(dict));
  1168. }
  1169. DnsQueryTypeSet MaybeDisableAdditionalQueries(DnsQueryTypeSet types) {
  1170. DCHECK(!types.Empty());
  1171. DCHECK(!types.Has(DnsQueryType::UNSPECIFIED));
  1172. // No-op if the caller explicitly requested this one query type.
  1173. if (types.Size() == 1)
  1174. return types;
  1175. if (types.Has(DnsQueryType::HTTPS)) {
  1176. if (!secure_ && (!https_svcb_options_.enable_insecure ||
  1177. !client_->CanQueryAdditionalTypesViaInsecureDns())) {
  1178. types.Remove(DnsQueryType::HTTPS);
  1179. } else {
  1180. DCHECK(!httpssvc_metrics_);
  1181. httpssvc_metrics_.emplace(secure_, /*expect_intact=*/false);
  1182. }
  1183. }
  1184. if (types.Has(DnsQueryType::INTEGRITY) ||
  1185. types.Has(DnsQueryType::HTTPS_EXPERIMENTAL)) {
  1186. if (!secure_ && (!features::kDnsHttpssvcEnableQueryOverInsecure.Get() ||
  1187. !client_->CanQueryAdditionalTypesViaInsecureDns())) {
  1188. types.RemoveAll(
  1189. {DnsQueryType::INTEGRITY, DnsQueryType::HTTPS_EXPERIMENTAL});
  1190. } else {
  1191. DCHECK(!httpssvc_metrics_)
  1192. << "Caller requested multiple experimental types";
  1193. httpssvc_metrics_.emplace(
  1194. secure_,
  1195. /*expect_intact=*/httpssvc_domain_cache_.IsExperimental(
  1196. GetHostname(host_)));
  1197. }
  1198. }
  1199. DCHECK(!types.Empty());
  1200. return types;
  1201. }
  1202. void PushTransactionsNeeded(DnsQueryTypeSet query_types) {
  1203. DCHECK(transactions_needed_.empty());
  1204. if (query_types.Has(DnsQueryType::HTTPS) &&
  1205. features::kUseDnsHttpsSvcbEnforceSecureResponse.Get() && secure_) {
  1206. query_types.Remove(DnsQueryType::HTTPS);
  1207. transactions_needed_.emplace_back(
  1208. DnsQueryType::HTTPS, TransactionErrorBehavior::kFatalOrEmpty);
  1209. }
  1210. // Give some queries a head start by pushing them to the queue first.
  1211. constexpr DnsQueryType kHighPriorityQueries[] = {DnsQueryType::A,
  1212. DnsQueryType::AAAA};
  1213. for (DnsQueryType high_priority_query : kHighPriorityQueries) {
  1214. if (query_types.Has(high_priority_query)) {
  1215. query_types.Remove(high_priority_query);
  1216. transactions_needed_.emplace_back(high_priority_query);
  1217. }
  1218. }
  1219. for (DnsQueryType remaining_query : query_types) {
  1220. if (remaining_query == DnsQueryType::HTTPS ||
  1221. remaining_query == DnsQueryType::HTTPS_EXPERIMENTAL ||
  1222. remaining_query == DnsQueryType::INTEGRITY) {
  1223. // Ignore errors for these types. In most cases treating them normally
  1224. // would only result in fallback to resolution without querying the
  1225. // type. Instead, synthesize empty results.
  1226. transactions_needed_.emplace_back(
  1227. remaining_query, TransactionErrorBehavior::kSynthesizeEmpty);
  1228. } else {
  1229. transactions_needed_.emplace_back(remaining_query);
  1230. }
  1231. }
  1232. }
  1233. void CreateAndStartTransaction(TransactionInfo transaction_info) {
  1234. DCHECK(!transaction_info.transaction);
  1235. DCHECK_NE(DnsQueryType::UNSPECIFIED, transaction_info.type);
  1236. std::string transaction_hostname(GetHostname(host_));
  1237. // For HTTPS, prepend "_<port>._https." for any non-default port.
  1238. uint16_t request_port = 0;
  1239. if (transaction_info.type == DnsQueryType::HTTPS &&
  1240. absl::holds_alternative<url::SchemeHostPort>(host_)) {
  1241. const auto& scheme_host_port = absl::get<url::SchemeHostPort>(host_);
  1242. transaction_hostname =
  1243. dns_util::GetNameForHttpsQuery(scheme_host_port, &request_port);
  1244. }
  1245. transaction_info.transaction =
  1246. client_->GetTransactionFactory()->CreateTransaction(
  1247. std::move(transaction_hostname),
  1248. DnsQueryTypeToQtype(transaction_info.type), net_log_, secure_,
  1249. secure_dns_mode_, &*resolve_context_,
  1250. fallback_available_ /* fast_timeout */);
  1251. transaction_info.transaction->SetRequestPriority(delegate_->priority());
  1252. auto transaction_info_it =
  1253. transactions_in_progress_.insert(std::move(transaction_info)).first;
  1254. // Safe to pass `transaction_info_it` because it is only modified/removed
  1255. // after async completion of this call or by destruction (which cancels the
  1256. // transaction and prevents callback because it owns the `DnsTransaction`
  1257. // object).
  1258. transaction_info_it->transaction->Start(base::BindOnce(
  1259. &DnsTask::OnDnsTransactionComplete, base::Unretained(this),
  1260. tick_clock_->NowTicks(), transaction_info_it, request_port));
  1261. }
  1262. void OnTimeout() {
  1263. net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_DNS_TASK_TIMEOUT,
  1264. [&] { return NetLogDnsTaskTimeoutParams(); });
  1265. for (const TransactionInfo& transaction : transactions_in_progress_) {
  1266. base::TimeDelta elapsed_time = tick_clock_->NowTicks() - task_start_time_;
  1267. switch (transaction.type) {
  1268. case DnsQueryType::INTEGRITY:
  1269. DCHECK(httpssvc_metrics_);
  1270. httpssvc_metrics_->SaveForIntegrity(HttpssvcDnsRcode::kTimedOut,
  1271. /*condensed_records=*/{},
  1272. elapsed_time);
  1273. break;
  1274. case DnsQueryType::HTTPS:
  1275. DCHECK(!secure_ ||
  1276. !features::kUseDnsHttpsSvcbEnforceSecureResponse.Get());
  1277. [[fallthrough]];
  1278. case DnsQueryType::HTTPS_EXPERIMENTAL:
  1279. if (httpssvc_metrics_) {
  1280. // Don't record provider ID for timeouts. It is not precisely known
  1281. // at this level which provider is actually to blame for the
  1282. // timeout, and breaking metrics out by provider is no longer
  1283. // important for current experimentation goals.
  1284. httpssvc_metrics_->SaveForHttps(HttpssvcDnsRcode::kTimedOut,
  1285. /*condensed_records=*/{},
  1286. elapsed_time);
  1287. }
  1288. break;
  1289. default:
  1290. // The timeout timer is only started when all other transactions have
  1291. // completed.
  1292. NOTREACHED();
  1293. }
  1294. }
  1295. transactions_needed_.clear();
  1296. transactions_in_progress_.clear();
  1297. OnTransactionsFinished();
  1298. }
  1299. // Called on completion of a `DnsTransaction`, but not necessarily completion
  1300. // of all work for the individual transaction in this task (see
  1301. // `OnTransactionsFinished()`).
  1302. void OnDnsTransactionComplete(
  1303. const base::TimeTicks& start_time,
  1304. std::set<TransactionInfo>::iterator transaction_info_it,
  1305. uint16_t request_port,
  1306. int net_error,
  1307. const DnsResponse* response) {
  1308. DCHECK(transaction_info_it != transactions_in_progress_.end());
  1309. DCHECK(transactions_in_progress_.find(*transaction_info_it) !=
  1310. transactions_in_progress_.end());
  1311. // Pull the TransactionInfo out of `transactions_in_progress_` now, so it
  1312. // and its underlying DnsTransaction will be deleted on completion of
  1313. // OnTransactionComplete. Note: Once control leaves OnTransactionComplete,
  1314. // there's no further need for the transaction object. On the other hand,
  1315. // since it owns `*response`, it should stay around while
  1316. // OnTransactionComplete executes.
  1317. TransactionInfo transaction_info = std::move(
  1318. transactions_in_progress_.extract(transaction_info_it).value());
  1319. base::TimeDelta elapsed_time = tick_clock_->NowTicks() - task_start_time_;
  1320. enum HttpssvcDnsRcode rcode_for_httpssvc = HttpssvcDnsRcode::kNoError;
  1321. if (httpssvc_metrics_) {
  1322. if (net_error == ERR_DNS_TIMED_OUT) {
  1323. rcode_for_httpssvc = HttpssvcDnsRcode::kTimedOut;
  1324. } else if (net_error == ERR_NAME_NOT_RESOLVED) {
  1325. rcode_for_httpssvc = HttpssvcDnsRcode::kNoError;
  1326. } else if (response == nullptr) {
  1327. rcode_for_httpssvc = HttpssvcDnsRcode::kMissingDnsResponse;
  1328. } else {
  1329. rcode_for_httpssvc =
  1330. TranslateDnsRcodeForHttpssvcExperiment(response->rcode());
  1331. }
  1332. }
  1333. // Handle network errors. Note that for NXDOMAIN, DnsTransaction returns
  1334. // ERR_NAME_NOT_RESOLVED, so that is not a network error if received with a
  1335. // valid response.
  1336. bool fatal_error =
  1337. IsFatalTransactionFailure(net_error, transaction_info, response);
  1338. absl::optional<DnsResponse> fake_response;
  1339. if (net_error != OK && !(net_error == ERR_NAME_NOT_RESOLVED && response &&
  1340. response->IsValid())) {
  1341. if (transaction_info.error_behavior ==
  1342. TransactionErrorBehavior::kFallback ||
  1343. fatal_error) {
  1344. // Fail task (or maybe Job) completely on network failure.
  1345. OnFailure(net_error, /*allow_fallback=*/!fatal_error,
  1346. /*ttl=*/absl::nullopt, transaction_info.type);
  1347. return;
  1348. } else {
  1349. DCHECK((transaction_info.error_behavior ==
  1350. TransactionErrorBehavior::kFatalOrEmpty &&
  1351. !fatal_error) ||
  1352. transaction_info.error_behavior ==
  1353. TransactionErrorBehavior::kSynthesizeEmpty);
  1354. // For non-fatal failures, synthesize an empty response.
  1355. fake_response =
  1356. CreateFakeEmptyResponse(GetHostname(host_), transaction_info.type);
  1357. response = &fake_response.value();
  1358. }
  1359. }
  1360. HostCache::Entry results(ERR_FAILED, HostCache::Entry::SOURCE_UNKNOWN);
  1361. DnsResponseResultExtractor extractor(response);
  1362. DnsResponseResultExtractor::ExtractionError extraction_error =
  1363. extractor.ExtractDnsResults(transaction_info.type,
  1364. /*original_domain_name=*/GetHostname(host_),
  1365. request_port, &results);
  1366. DCHECK_NE(extraction_error,
  1367. DnsResponseResultExtractor::ExtractionError::kUnexpected);
  1368. if (results.error() != OK && results.error() != ERR_NAME_NOT_RESOLVED) {
  1369. net_log_.AddEvent(
  1370. NetLogEventType::HOST_RESOLVER_MANAGER_DNS_TASK_EXTRACTION_FAILURE,
  1371. [&] {
  1372. return NetLogDnsTaskExtractionFailureParams(
  1373. extraction_error, transaction_info.type, results);
  1374. });
  1375. if (transaction_info.error_behavior ==
  1376. TransactionErrorBehavior::kFatalOrEmpty ||
  1377. transaction_info.error_behavior ==
  1378. TransactionErrorBehavior::kSynthesizeEmpty) {
  1379. // No extraction errors are currently considered fatal, otherwise, there
  1380. // would need to be a call to some sort of
  1381. // IsFatalTransactionExtractionError() function.
  1382. DCHECK(!fatal_error);
  1383. results = DnsResponseResultExtractor::CreateEmptyResult(
  1384. transaction_info.type);
  1385. } else {
  1386. OnFailure(results.error(), /*allow_fallback=*/true,
  1387. results.GetOptionalTtl(), transaction_info.type);
  1388. return;
  1389. }
  1390. }
  1391. if (httpssvc_metrics_) {
  1392. if (transaction_info.type == DnsQueryType::INTEGRITY) {
  1393. const std::vector<bool>* experimental_results =
  1394. results.https_record_compatibility();
  1395. CHECK(experimental_results);
  1396. // INTEGRITY queries can time out the normal way (here), or when the
  1397. // experimental query timer runs out (OnExperimentalQueryTimeout).
  1398. httpssvc_metrics_->SaveForIntegrity(
  1399. rcode_for_httpssvc, *experimental_results, elapsed_time);
  1400. } else if (transaction_info.type == DnsQueryType::HTTPS ||
  1401. transaction_info.type == DnsQueryType::HTTPS_EXPERIMENTAL) {
  1402. const std::vector<bool>* record_compatibility =
  1403. results.https_record_compatibility();
  1404. CHECK(record_compatibility);
  1405. httpssvc_metrics_->SaveForHttps(rcode_for_httpssvc,
  1406. *record_compatibility, elapsed_time);
  1407. } else {
  1408. httpssvc_metrics_->SaveForAddressQuery(elapsed_time,
  1409. rcode_for_httpssvc);
  1410. }
  1411. }
  1412. // Trigger HTTP->HTTPS upgrade if an HTTPS record is received for an "http"
  1413. // or "ws" request.
  1414. if (transaction_info.type == DnsQueryType::HTTPS &&
  1415. ShouldTriggerHttpToHttpsUpgrade(results)) {
  1416. // Disallow fallback. Otherwise DNS could be reattempted without HTTPS
  1417. // queries, and that would hide this error instead of triggering upgrade.
  1418. OnFailure(ERR_DNS_NAME_HTTPS_ONLY, /*allow_fallback=*/false,
  1419. results.GetOptionalTtl(), transaction_info.type);
  1420. return;
  1421. }
  1422. HideMetadataResultsIfNotDesired(results);
  1423. // Merge results with saved results from previous transactions.
  1424. if (saved_results_) {
  1425. // If saved result is a deferred failure, try again to complete with that
  1426. // failure.
  1427. if (saved_results_is_failure_) {
  1428. OnFailure(saved_results_.value().error(), /*allow_fallback=*/true,
  1429. saved_results_.value().GetOptionalTtl());
  1430. return;
  1431. }
  1432. switch (transaction_info.type) {
  1433. case DnsQueryType::A:
  1434. // Canonical names from A results have lower priority than those
  1435. // from AAAA results, so merge to the back.
  1436. results = HostCache::Entry::MergeEntries(
  1437. std::move(saved_results_).value(), std::move(results));
  1438. break;
  1439. case DnsQueryType::AAAA:
  1440. // Canonical names from AAAA results take priority over those
  1441. // from A results, so merge to the front.
  1442. results = HostCache::Entry::MergeEntries(
  1443. std::move(results), std::move(saved_results_).value());
  1444. break;
  1445. case DnsQueryType::INTEGRITY:
  1446. case DnsQueryType::HTTPS:
  1447. case DnsQueryType::HTTPS_EXPERIMENTAL:
  1448. // No particular importance to order.
  1449. results = HostCache::Entry::MergeEntries(
  1450. std::move(results), std::move(saved_results_).value());
  1451. break;
  1452. default:
  1453. // Only expect address query types with multiple transactions.
  1454. NOTREACHED();
  1455. }
  1456. }
  1457. saved_results_ = std::move(results);
  1458. OnTransactionsFinished();
  1459. }
  1460. bool IsFatalTransactionFailure(int transaction_error,
  1461. const TransactionInfo& transaction_info,
  1462. const DnsResponse* response) {
  1463. if (transaction_info.type != DnsQueryType::HTTPS) {
  1464. DCHECK(transaction_info.error_behavior !=
  1465. TransactionErrorBehavior::kFatalOrEmpty);
  1466. return false;
  1467. }
  1468. // These values are logged to UMA. Entries should not be renumbered and
  1469. // numeric values should never be reused. Please keep in sync with
  1470. // "DNS.SvcbHttpsTransactionError" in
  1471. // src/tools/metrics/histograms/enums.xml.
  1472. enum class HttpsTransactionError {
  1473. kNoError = 0,
  1474. kInsecureError = 1,
  1475. kNonFatalError = 2,
  1476. kFatalErrorDisabled = 3,
  1477. kFatalErrorEnabled = 4,
  1478. kMaxValue = kFatalErrorEnabled
  1479. } error;
  1480. if (transaction_error == OK ||
  1481. (transaction_error == ERR_NAME_NOT_RESOLVED && response &&
  1482. response->IsValid())) {
  1483. error = HttpsTransactionError::kNoError;
  1484. } else if (!secure_) {
  1485. // HTTPS failures are never fatal via insecure DNS.
  1486. DCHECK(transaction_info.error_behavior !=
  1487. TransactionErrorBehavior::kFatalOrEmpty);
  1488. error = HttpsTransactionError::kInsecureError;
  1489. } else if (transaction_error == ERR_DNS_SERVER_FAILED && response &&
  1490. response->rcode() != dns_protocol::kRcodeSERVFAIL) {
  1491. // For server failures, only SERVFAIL is fatal.
  1492. error = HttpsTransactionError::kNonFatalError;
  1493. } else if (features::kUseDnsHttpsSvcbEnforceSecureResponse.Get()) {
  1494. DCHECK(transaction_info.error_behavior ==
  1495. TransactionErrorBehavior::kFatalOrEmpty);
  1496. error = HttpsTransactionError::kFatalErrorEnabled;
  1497. } else {
  1498. DCHECK(transaction_info.error_behavior !=
  1499. TransactionErrorBehavior::kFatalOrEmpty);
  1500. error = HttpsTransactionError::kFatalErrorDisabled;
  1501. }
  1502. UMA_HISTOGRAM_ENUMERATION("Net.DNS.DnsTask.SvcbHttpsTransactionError",
  1503. error);
  1504. return error == HttpsTransactionError::kFatalErrorEnabled;
  1505. }
  1506. // Called on processing for one or more individual transaction being
  1507. // completed/cancelled. Processes overall results if all transactions are
  1508. // finished.
  1509. void OnTransactionsFinished() {
  1510. if (!transactions_in_progress_.empty() || !transactions_needed_.empty()) {
  1511. delegate_->OnIntermediateTransactionsComplete();
  1512. MaybeStartTimeoutTimer();
  1513. return;
  1514. }
  1515. DCHECK(saved_results_.has_value());
  1516. HostCache::Entry results = std::move(*saved_results_);
  1517. timeout_timer_.Stop();
  1518. absl::optional<std::vector<IPEndPoint>> ip_endpoints;
  1519. ip_endpoints = base::OptionalFromPtr(results.ip_endpoints());
  1520. if (ip_endpoints.has_value()) {
  1521. // If there are multiple addresses, and at least one is IPv6, need to
  1522. // sort them.
  1523. bool at_least_one_ipv6_address = base::ranges::any_of(
  1524. ip_endpoints.value(),
  1525. [](auto& e) { return e.GetFamily() == ADDRESS_FAMILY_IPV6; });
  1526. if (at_least_one_ipv6_address) {
  1527. // Sort addresses if needed. Sort could complete synchronously.
  1528. client_->GetAddressSorter()->Sort(
  1529. ip_endpoints.value(),
  1530. base::BindOnce(&DnsTask::OnSortComplete, AsWeakPtr(),
  1531. tick_clock_->NowTicks(), std::move(results),
  1532. secure_));
  1533. return;
  1534. }
  1535. }
  1536. OnSuccess(std::move(results));
  1537. }
  1538. void OnSortComplete(base::TimeTicks sort_start_time,
  1539. HostCache::Entry results,
  1540. bool secure,
  1541. bool success,
  1542. std::vector<IPEndPoint> sorted) {
  1543. DCHECK(results.ip_endpoints());
  1544. results.set_ip_endpoints(std::move(sorted));
  1545. if (!success) {
  1546. OnFailure(ERR_DNS_SORT_ERROR, /*allow_fallback=*/true,
  1547. results.GetOptionalTtl());
  1548. return;
  1549. }
  1550. // AddressSorter prunes unusable destinations.
  1551. if ((!results.ip_endpoints() || results.ip_endpoints()->empty()) &&
  1552. results.text_records().value_or(std::vector<std::string>()).empty() &&
  1553. results.hostnames().value_or(std::vector<HostPortPair>()).empty()) {
  1554. LOG(WARNING) << "Address list empty after RFC3484 sort";
  1555. OnFailure(ERR_NAME_NOT_RESOLVED, /*allow_fallback=*/true,
  1556. results.GetOptionalTtl());
  1557. return;
  1558. }
  1559. OnSuccess(std::move(results));
  1560. }
  1561. bool AnyPotentiallyFatalTransactionsRemain() {
  1562. auto is_fatal_or_empty_error = [](TransactionErrorBehavior behavior) {
  1563. return behavior == TransactionErrorBehavior::kFatalOrEmpty;
  1564. };
  1565. return base::ranges::any_of(transactions_needed_, is_fatal_or_empty_error,
  1566. &TransactionInfo::error_behavior) ||
  1567. base::ranges::any_of(transactions_in_progress_,
  1568. is_fatal_or_empty_error,
  1569. &TransactionInfo::error_behavior);
  1570. }
  1571. void CancelNonFatalTransactions() {
  1572. auto has_non_fatal_or_empty_error = [](const TransactionInfo& info) {
  1573. return info.error_behavior != TransactionErrorBehavior::kFatalOrEmpty;
  1574. };
  1575. base::EraseIf(transactions_needed_, has_non_fatal_or_empty_error);
  1576. base::EraseIf(transactions_in_progress_, has_non_fatal_or_empty_error);
  1577. }
  1578. void OnFailure(
  1579. int net_error,
  1580. bool allow_fallback,
  1581. absl::optional<base::TimeDelta> ttl = absl::nullopt,
  1582. absl::optional<DnsQueryType> failed_transaction_type = absl::nullopt) {
  1583. if (httpssvc_metrics_ && failed_transaction_type.has_value() &&
  1584. IsAddressType(failed_transaction_type.value())) {
  1585. httpssvc_metrics_->SaveAddressQueryFailure();
  1586. }
  1587. DCHECK_NE(OK, net_error);
  1588. HostCache::Entry results(net_error, HostCache::Entry::SOURCE_UNKNOWN, ttl);
  1589. // On non-fatal errors, if any potentially fatal transactions remain, need
  1590. // to defer ending the task in case any of those remaining transactions end
  1591. // with a fatal failure.
  1592. if (allow_fallback && AnyPotentiallyFatalTransactionsRemain()) {
  1593. saved_results_ = std::move(results);
  1594. saved_results_is_failure_ = true;
  1595. CancelNonFatalTransactions();
  1596. OnTransactionsFinished();
  1597. return;
  1598. }
  1599. net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_DNS_TASK, [&] {
  1600. return NetLogDnsTaskFailedParams(net_error, failed_transaction_type, ttl,
  1601. base::OptionalOrNullptr(saved_results_));
  1602. });
  1603. // Expect this to result in destroying `this` and thus cancelling any
  1604. // remaining transactions.
  1605. delegate_->OnDnsTaskComplete(task_start_time_, allow_fallback,
  1606. std::move(results), secure_);
  1607. }
  1608. void OnSuccess(HostCache::Entry results) {
  1609. net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_DNS_TASK,
  1610. [&] { return NetLogResults(results); });
  1611. delegate_->OnDnsTaskComplete(task_start_time_, /*allow_fallback=*/true,
  1612. std::move(results), secure_);
  1613. }
  1614. // Returns whether any transactions left to finish are of a transaction type
  1615. // in `types`. Used for logging and starting the timeout timer (see
  1616. // MaybeStartTimeoutTimer()).
  1617. bool AnyOfTypeTransactionsRemain(
  1618. std::initializer_list<DnsQueryType> types) const {
  1619. // Should only be called if some transactions are still running or waiting
  1620. // to run.
  1621. DCHECK(!transactions_needed_.empty() || !transactions_in_progress_.empty());
  1622. // Check running transactions.
  1623. if (base::ranges::find_first_of(transactions_in_progress_, types,
  1624. /*pred=*/{},
  1625. /*proj1=*/&TransactionInfo::type) !=
  1626. transactions_in_progress_.end()) {
  1627. return true;
  1628. }
  1629. // Check queued transactions, in case it ever becomes possible to get here
  1630. // without the transactions being started first.
  1631. return base::ranges::find_first_of(transactions_needed_, types, /*pred=*/{},
  1632. /*proj1=*/&TransactionInfo::type) !=
  1633. transactions_needed_.end();
  1634. }
  1635. void MaybeStartTimeoutTimer() {
  1636. // Should only be called if some transactions are still running or waiting
  1637. // to run.
  1638. DCHECK(!transactions_in_progress_.empty() || !transactions_needed_.empty());
  1639. // Timer already running.
  1640. if (timeout_timer_.IsRunning())
  1641. return;
  1642. // Always wait for address transactions.
  1643. if (AnyOfTypeTransactionsRemain({DnsQueryType::A, DnsQueryType::AAAA}))
  1644. return;
  1645. base::TimeDelta timeout_max;
  1646. int extra_time_percent = 0;
  1647. base::TimeDelta timeout_min;
  1648. if (AnyOfTypeTransactionsRemain({DnsQueryType::HTTPS})) {
  1649. DCHECK(https_svcb_options_.enable);
  1650. if (secure_) {
  1651. timeout_max = https_svcb_options_.secure_extra_time_max;
  1652. extra_time_percent = https_svcb_options_.secure_extra_time_percent;
  1653. timeout_min = https_svcb_options_.secure_extra_time_min;
  1654. } else {
  1655. timeout_max = https_svcb_options_.insecure_extra_time_max;
  1656. extra_time_percent = https_svcb_options_.insecure_extra_time_percent;
  1657. timeout_min = https_svcb_options_.insecure_extra_time_min;
  1658. }
  1659. if (timeout_max.is_zero() && extra_time_percent == 0 &&
  1660. timeout_min.is_zero()) {
  1661. timeout_max = https_svcb_options_.extra_time_absolute;
  1662. extra_time_percent = https_svcb_options_.extra_time_percent;
  1663. }
  1664. // Skip timeout for secure requests if the timeout would be a fatal
  1665. // failure.
  1666. if (secure_ && features::kUseDnsHttpsSvcbEnforceSecureResponse.Get()) {
  1667. timeout_max = base::TimeDelta();
  1668. extra_time_percent = 0;
  1669. timeout_min = base::TimeDelta();
  1670. }
  1671. } else if (AnyOfTypeTransactionsRemain(
  1672. {DnsQueryType::INTEGRITY,
  1673. DnsQueryType::HTTPS_EXPERIMENTAL})) {
  1674. DCHECK(base::FeatureList::IsEnabled(features::kDnsHttpssvc));
  1675. timeout_max = features::dns_httpssvc_experiment::GetExtraTimeAbsolute();
  1676. extra_time_percent = features::kDnsHttpssvcExtraTimePercent.Get();
  1677. } else {
  1678. // Unhandled supplemental type.
  1679. NOTREACHED();
  1680. }
  1681. base::TimeDelta timeout;
  1682. if (extra_time_percent > 0) {
  1683. base::TimeDelta total_time_for_other_transactions =
  1684. tick_clock_->NowTicks() - task_start_time_;
  1685. timeout = total_time_for_other_transactions * extra_time_percent / 100;
  1686. // Use at least 1ms to ensure timeout doesn't occur immediately in tests.
  1687. timeout = std::max(timeout, base::Milliseconds(1));
  1688. if (!timeout_max.is_zero())
  1689. timeout = std::min(timeout, timeout_max);
  1690. if (!timeout_min.is_zero())
  1691. timeout = std::max(timeout, timeout_min);
  1692. } else {
  1693. // If no relative timeout, use a non-zero min/max as timeout. If both are
  1694. // non-zero, that's not very sensible, but arbitrarily take the higher
  1695. // timeout.
  1696. timeout = std::max(timeout_min, timeout_max);
  1697. }
  1698. if (!timeout.is_zero())
  1699. timeout_timer_.Start(
  1700. FROM_HERE, timeout,
  1701. base::BindOnce(&DnsTask::OnTimeout, base::Unretained(this)));
  1702. }
  1703. bool ShouldTriggerHttpToHttpsUpgrade(const HostCache::Entry& results) {
  1704. // These values are logged to UMA. Entries should not be renumbered and
  1705. // numeric values should never be reused. Please keep in sync with
  1706. // "DNS.HttpUpgradeResult" in src/tools/metrics/histograms/enums.xml.
  1707. enum class UpgradeResult {
  1708. kUpgradeTriggered = 0,
  1709. kNoHttpsRecord = 1,
  1710. kHttpsScheme = 2,
  1711. kOtherScheme = 3,
  1712. kUpgradeDisabled = 4,
  1713. kMaxValue = kUpgradeDisabled
  1714. } upgrade_result;
  1715. if (!results.https_record_compatibility() ||
  1716. base::ranges::none_of(*results.https_record_compatibility(),
  1717. base::identity())) {
  1718. upgrade_result = UpgradeResult::kNoHttpsRecord;
  1719. } else if (GetScheme(host_) == url::kHttpsScheme ||
  1720. GetScheme(host_) == url::kWssScheme) {
  1721. upgrade_result = UpgradeResult::kHttpsScheme;
  1722. } else if (GetScheme(host_) != url::kHttpScheme &&
  1723. GetScheme(host_) != url::kWsScheme) {
  1724. // This is an unusual case because HTTPS would normally not be requested
  1725. // if the scheme is not http(s):// or ws(s)://.
  1726. upgrade_result = UpgradeResult::kOtherScheme;
  1727. } else if (!features::kUseDnsHttpsSvcbHttpUpgrade.Get()) {
  1728. upgrade_result = UpgradeResult::kUpgradeDisabled;
  1729. } else {
  1730. upgrade_result = UpgradeResult::kUpgradeTriggered;
  1731. }
  1732. UMA_HISTOGRAM_ENUMERATION("Net.DNS.DnsTask.HttpUpgrade", upgrade_result);
  1733. return upgrade_result == UpgradeResult::kUpgradeTriggered;
  1734. }
  1735. // Only keep metadata results (from HTTPS records) for appropriate schemes.
  1736. // This is needed to ensure metadata isn't included in results if the current
  1737. // Feature setup allows querying HTTPS for http:// or ws:// but doesn't enable
  1738. // scheme upgrade to error out on finding an HTTPS record.
  1739. //
  1740. // TODO(crbug.com/1206455): Remove once all requests that query HTTPS will
  1741. // either allow metadata results or error out.
  1742. void HideMetadataResultsIfNotDesired(HostCache::Entry& results) {
  1743. if (GetScheme(host_) == url::kHttpsScheme ||
  1744. GetScheme(host_) == url::kWssScheme) {
  1745. return;
  1746. }
  1747. results.ClearMetadatas();
  1748. }
  1749. raw_ptr<DnsClient> client_;
  1750. absl::variant<url::SchemeHostPort, std::string> host_;
  1751. base::SafeRef<ResolveContext> resolve_context_;
  1752. // Whether lookups in this DnsTask should occur using DoH or plaintext.
  1753. const bool secure_;
  1754. const SecureDnsMode secure_dns_mode_;
  1755. // The listener to the results of this DnsTask.
  1756. raw_ptr<Delegate> delegate_;
  1757. const NetLogWithSource net_log_;
  1758. bool any_transaction_started_ = false;
  1759. base::circular_deque<TransactionInfo> transactions_needed_;
  1760. // Active transactions have iterators pointing to their entry in this set, so
  1761. // individual entries should not be modified or removed until completion or
  1762. // cancellation of the transaction.
  1763. std::set<TransactionInfo> transactions_in_progress_;
  1764. absl::optional<HostCache::Entry> saved_results_;
  1765. bool saved_results_is_failure_ = false;
  1766. raw_ptr<const base::TickClock> tick_clock_;
  1767. base::TimeTicks task_start_time_;
  1768. HttpssvcExperimentDomainCache httpssvc_domain_cache_;
  1769. absl::optional<HttpssvcMetrics> httpssvc_metrics_;
  1770. // Timer for task timeout. Generally started after completion of address
  1771. // transactions to allow aborting experimental or supplemental transactions.
  1772. base::OneShotTimer timeout_timer_;
  1773. // If true, there are still significant fallback options available if this
  1774. // task completes unsuccessfully. Used as a signal that underlying
  1775. // transactions should timeout more quickly.
  1776. bool fallback_available_;
  1777. const HostResolver::HttpsSvcbOptions https_svcb_options_;
  1778. };
  1779. //-----------------------------------------------------------------------------
  1780. struct HostResolverManager::JobKey {
  1781. explicit JobKey(ResolveContext* resolve_context)
  1782. : resolve_context(resolve_context->AsSafeRef()) {}
  1783. bool operator<(const JobKey& other) const {
  1784. return std::forward_as_tuple(query_types.ToEnumBitmask(), flags, source,
  1785. secure_dns_mode, &*resolve_context, host,
  1786. network_isolation_key) <
  1787. std::forward_as_tuple(other.query_types.ToEnumBitmask(), other.flags,
  1788. other.source, other.secure_dns_mode,
  1789. &*other.resolve_context, other.host,
  1790. other.network_isolation_key);
  1791. }
  1792. bool operator==(const JobKey& other) const {
  1793. return !(*this < other || other < *this);
  1794. }
  1795. absl::variant<url::SchemeHostPort, std::string> host;
  1796. NetworkIsolationKey network_isolation_key;
  1797. DnsQueryTypeSet query_types;
  1798. HostResolverFlags flags;
  1799. HostResolverSource source;
  1800. SecureDnsMode secure_dns_mode;
  1801. base::SafeRef<ResolveContext> resolve_context;
  1802. HostCache::Key ToCacheKey(bool secure) const {
  1803. if (query_types.Size() != 1) {
  1804. // This function will produce identical cache keys for `JobKey` structs
  1805. // that differ only in their (non-singleton) `query_types` fields. When we
  1806. // enable new query types, this behavior could lead to subtle bugs. That
  1807. // is why the following DCHECK restricts the allowable query types.
  1808. DCHECK(Difference(query_types,
  1809. DnsQueryTypeSet(DnsQueryType::A, DnsQueryType::AAAA,
  1810. DnsQueryType::HTTPS,
  1811. DnsQueryType::HTTPS_EXPERIMENTAL,
  1812. DnsQueryType::INTEGRITY))
  1813. .Empty());
  1814. }
  1815. const DnsQueryType query_type_for_key = query_types.Size() == 1
  1816. ? *query_types.begin()
  1817. : DnsQueryType::UNSPECIFIED;
  1818. HostCache::Key key(host, query_type_for_key, flags, source,
  1819. network_isolation_key);
  1820. key.secure = secure;
  1821. return key;
  1822. }
  1823. handles::NetworkHandle GetTargetNetwork() const {
  1824. return resolve_context->GetTargetNetwork();
  1825. }
  1826. };
  1827. // Aggregates all Requests for the same Key. Dispatched via
  1828. // PrioritizedDispatcher.
  1829. class HostResolverManager::Job : public PrioritizedDispatcher::Job,
  1830. public HostResolverManager::DnsTask::Delegate {
  1831. public:
  1832. // Creates new job for |key| where |request_net_log| is bound to the
  1833. // request that spawned it.
  1834. Job(const base::WeakPtr<HostResolverManager>& resolver,
  1835. JobKey key,
  1836. ResolveHostParameters::CacheUsage cache_usage,
  1837. HostCache* host_cache,
  1838. std::deque<TaskType> tasks,
  1839. RequestPriority priority,
  1840. scoped_refptr<base::TaskRunner> proc_task_runner,
  1841. const NetLogWithSource& source_net_log,
  1842. const base::TickClock* tick_clock,
  1843. const HostResolver::HttpsSvcbOptions& https_svcb_options)
  1844. : resolver_(resolver),
  1845. key_(std::move(key)),
  1846. cache_usage_(cache_usage),
  1847. host_cache_(host_cache),
  1848. tasks_(tasks),
  1849. priority_tracker_(priority),
  1850. proc_task_runner_(std::move(proc_task_runner)),
  1851. tick_clock_(tick_clock),
  1852. https_svcb_options_(https_svcb_options),
  1853. net_log_(
  1854. NetLogWithSource::Make(source_net_log.net_log(),
  1855. NetLogSourceType::HOST_RESOLVER_IMPL_JOB)) {
  1856. source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_CREATE_JOB);
  1857. net_log_.BeginEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB, [&] {
  1858. return NetLogJobCreationParams(source_net_log.source());
  1859. });
  1860. }
  1861. ~Job() override {
  1862. bool was_queued = is_queued();
  1863. bool was_running = is_running();
  1864. // Clean up now for nice NetLog.
  1865. Finish();
  1866. if (was_running) {
  1867. // This Job was destroyed while still in flight.
  1868. net_log_.EndEventWithNetErrorCode(
  1869. NetLogEventType::HOST_RESOLVER_MANAGER_JOB, ERR_ABORTED);
  1870. } else if (was_queued) {
  1871. // Job was cancelled before it could run.
  1872. // TODO(szym): is there any benefit in having this distinction?
  1873. net_log_.AddEvent(NetLogEventType::CANCELLED);
  1874. net_log_.EndEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB);
  1875. }
  1876. // else CompleteRequests logged EndEvent.
  1877. while (!requests_.empty()) {
  1878. // Log any remaining Requests as cancelled.
  1879. RequestImpl* req = requests_.head()->value();
  1880. req->RemoveFromList();
  1881. CHECK(key_ == req->GetJobKey());
  1882. req->OnJobCancelled(key_);
  1883. }
  1884. }
  1885. // Add this job to the dispatcher. If "at_head" is true, adds at the front
  1886. // of the queue.
  1887. void Schedule(bool at_head) {
  1888. DCHECK(!is_queued());
  1889. PrioritizedDispatcher::Handle handle;
  1890. DCHECK(dispatched_);
  1891. if (!at_head) {
  1892. handle = resolver_->dispatcher_->Add(this, priority());
  1893. } else {
  1894. handle = resolver_->dispatcher_->AddAtHead(this, priority());
  1895. }
  1896. // The dispatcher could have started |this| in the above call to Add, which
  1897. // could have called Schedule again. In that case |handle| will be null,
  1898. // but |handle_| may have been set by the other nested call to Schedule.
  1899. if (!handle.is_null()) {
  1900. DCHECK(handle_.is_null());
  1901. handle_ = handle;
  1902. }
  1903. }
  1904. void AddRequest(RequestImpl* request) {
  1905. // Job currently assumes a 1:1 correspondence between ResolveContext and
  1906. // HostCache. Since the ResolveContext is part of the JobKey, any request
  1907. // added to any existing Job should share the same HostCache.
  1908. DCHECK_EQ(host_cache_, request->host_cache());
  1909. // TODO(crbug.com/1206799): Check equality of whole host once Jobs are
  1910. // separated by scheme/port.
  1911. DCHECK_EQ(GetHostname(key_.host), GetHostname(request->request_host()));
  1912. request->AssignJob(weak_ptr_factory_.GetSafeRef());
  1913. priority_tracker_.Add(request->priority());
  1914. request->source_net_log().AddEventReferencingSource(
  1915. NetLogEventType::HOST_RESOLVER_MANAGER_JOB_ATTACH, net_log_.source());
  1916. net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB_REQUEST_ATTACH,
  1917. [&] {
  1918. return NetLogJobAttachParams(
  1919. request->source_net_log().source(), priority());
  1920. });
  1921. if (!request->parameters().is_speculative)
  1922. had_non_speculative_request_ = true;
  1923. requests_.Append(request);
  1924. UpdatePriority();
  1925. }
  1926. void ChangeRequestPriority(RequestImpl* req, RequestPriority priority) {
  1927. // TODO(crbug.com/1206799): Check equality of whole host once Jobs are
  1928. // separated by scheme/port.
  1929. DCHECK_EQ(GetHostname(key_.host), GetHostname(req->request_host()));
  1930. priority_tracker_.Remove(req->priority());
  1931. req->set_priority(priority);
  1932. priority_tracker_.Add(req->priority());
  1933. UpdatePriority();
  1934. }
  1935. // Detach cancelled request. If it was the last active Request, also finishes
  1936. // this Job.
  1937. void CancelRequest(RequestImpl* request) {
  1938. // TODO(crbug.com/1206799): Check equality of whole host once Jobs are
  1939. // separated by scheme/port.
  1940. DCHECK_EQ(GetHostname(key_.host), GetHostname(request->request_host()));
  1941. DCHECK(!requests_.empty());
  1942. priority_tracker_.Remove(request->priority());
  1943. net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB_REQUEST_DETACH,
  1944. [&] {
  1945. return NetLogJobAttachParams(
  1946. request->source_net_log().source(), priority());
  1947. });
  1948. if (num_active_requests() > 0) {
  1949. UpdatePriority();
  1950. request->RemoveFromList();
  1951. } else {
  1952. // If we were called from a Request's callback within CompleteRequests,
  1953. // that Request could not have been cancelled, so num_active_requests()
  1954. // could not be 0. Therefore, we are not in CompleteRequests().
  1955. CompleteRequestsWithError(ERR_DNS_REQUEST_CANCELLED);
  1956. }
  1957. }
  1958. // Called from AbortJobsWithoutTargetNetwork(). Completes all requests and
  1959. // destroys the job. This currently assumes the abort is due to a network
  1960. // change.
  1961. // TODO This should not delete |this|.
  1962. void Abort() {
  1963. CompleteRequestsWithError(ERR_NETWORK_CHANGED);
  1964. }
  1965. // Gets a closure that will abort an insecure DnsTask (see
  1966. // AbortInsecureDnsTask()) iff |this| is still valid. Useful if aborting a
  1967. // list of Jobs as some may be cancelled while aborting others.
  1968. base::OnceClosure GetAbortInsecureDnsTaskClosure(int error,
  1969. bool fallback_only) {
  1970. return base::BindOnce(&Job::AbortInsecureDnsTask,
  1971. weak_ptr_factory_.GetWeakPtr(), error, fallback_only);
  1972. }
  1973. // Aborts or removes any current/future insecure DnsTasks if a ProcTask is
  1974. // available for fallback. If no fallback is available and |fallback_only| is
  1975. // false, a job that is currently running an insecure DnsTask will be
  1976. // completed with |error|.
  1977. void AbortInsecureDnsTask(int error, bool fallback_only) {
  1978. bool has_proc_fallback =
  1979. std::find(tasks_.begin(), tasks_.end(), TaskType::PROC) != tasks_.end();
  1980. if (has_proc_fallback) {
  1981. for (auto it = tasks_.begin(); it != tasks_.end();) {
  1982. if (*it == TaskType::DNS)
  1983. it = tasks_.erase(it);
  1984. else
  1985. ++it;
  1986. }
  1987. }
  1988. if (dns_task_ && !dns_task_->secure()) {
  1989. if (has_proc_fallback) {
  1990. KillDnsTask();
  1991. dns_task_error_ = OK;
  1992. RunNextTask();
  1993. } else if (!fallback_only) {
  1994. CompleteRequestsWithError(error);
  1995. }
  1996. }
  1997. }
  1998. // Called by HostResolverManager when this job is evicted due to queue
  1999. // overflow. Completes all requests and destroys the job. The job could have
  2000. // waiting requests that will receive completion callbacks, so cleanup
  2001. // asynchronously to avoid reentrancy.
  2002. void OnEvicted() {
  2003. DCHECK(!is_running());
  2004. DCHECK(is_queued());
  2005. handle_.Reset();
  2006. net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB_EVICTED);
  2007. // This signals to CompleteRequests that parts of this job never ran.
  2008. // Job must be saved in |resolver_| to be completed asynchronously.
  2009. // Otherwise the job will be destroyed with requests silently cancelled
  2010. // before completion runs.
  2011. DCHECK(self_iterator_);
  2012. base::SequencedTaskRunnerHandle::Get()->PostTask(
  2013. FROM_HERE, base::BindOnce(&Job::CompleteRequestsWithError,
  2014. weak_ptr_factory_.GetWeakPtr(),
  2015. ERR_HOST_RESOLVER_QUEUE_TOO_LARGE));
  2016. }
  2017. // Attempts to serve the job from HOSTS. Returns true if succeeded and
  2018. // this Job was destroyed.
  2019. bool ServeFromHosts() {
  2020. DCHECK_GT(num_active_requests(), 0u);
  2021. absl::optional<HostCache::Entry> results = resolver_->ServeFromHosts(
  2022. GetHostname(key_.host), key_.query_types,
  2023. key_.flags & HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6, tasks_);
  2024. if (results) {
  2025. // This will destroy the Job.
  2026. CompleteRequests(results.value(), base::TimeDelta(),
  2027. true /* allow_cache */, true /* secure */);
  2028. return true;
  2029. }
  2030. return false;
  2031. }
  2032. void OnAddedToJobMap(JobMap::iterator iterator) {
  2033. DCHECK(!self_iterator_);
  2034. DCHECK(iterator != resolver_->jobs_.end());
  2035. self_iterator_ = iterator;
  2036. }
  2037. void OnRemovedFromJobMap() {
  2038. DCHECK(self_iterator_);
  2039. self_iterator_ = absl::nullopt;
  2040. }
  2041. void RunNextTask() {
  2042. // If there are no tasks left to try, cache any stored results and complete
  2043. // the request with the last stored result. All stored results should be
  2044. // errors.
  2045. if (tasks_.empty()) {
  2046. // If there are no stored results, complete with an error.
  2047. if (completion_results_.size() == 0) {
  2048. CompleteRequestsWithError(ERR_NAME_NOT_RESOLVED);
  2049. return;
  2050. }
  2051. // Cache all but the last result here. The last result will be cached
  2052. // as part of CompleteRequests.
  2053. for (size_t i = 0; i < completion_results_.size() - 1; ++i) {
  2054. const auto& result = completion_results_[i];
  2055. DCHECK_NE(OK, result.entry.error());
  2056. MaybeCacheResult(result.entry, result.ttl, result.secure);
  2057. }
  2058. const auto& last_result = completion_results_.back();
  2059. DCHECK_NE(OK, last_result.entry.error());
  2060. CompleteRequests(last_result.entry, last_result.ttl,
  2061. true /* allow_cache */, last_result.secure);
  2062. return;
  2063. }
  2064. TaskType next_task = tasks_.front();
  2065. // Schedule insecure DnsTasks and ProcTasks with the dispatcher.
  2066. if (!dispatched_ &&
  2067. (next_task == TaskType::DNS || next_task == TaskType::PROC ||
  2068. next_task == TaskType::MDNS)) {
  2069. dispatched_ = true;
  2070. job_running_ = false;
  2071. Schedule(false);
  2072. DCHECK(is_running() || is_queued());
  2073. // Check for queue overflow.
  2074. PrioritizedDispatcher& dispatcher = *resolver_->dispatcher_;
  2075. if (dispatcher.num_queued_jobs() > resolver_->max_queued_jobs_) {
  2076. Job* evicted = static_cast<Job*>(dispatcher.EvictOldestLowest());
  2077. DCHECK(evicted);
  2078. evicted->OnEvicted();
  2079. }
  2080. return;
  2081. }
  2082. if (start_time_ == base::TimeTicks()) {
  2083. net_log_.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_JOB_STARTED);
  2084. start_time_ = tick_clock_->NowTicks();
  2085. }
  2086. tasks_.pop_front();
  2087. job_running_ = true;
  2088. switch (next_task) {
  2089. case TaskType::PROC:
  2090. StartProcTask();
  2091. break;
  2092. case TaskType::DNS:
  2093. StartDnsTask(false /* secure */);
  2094. break;
  2095. case TaskType::SECURE_DNS:
  2096. StartDnsTask(true /* secure */);
  2097. break;
  2098. case TaskType::MDNS:
  2099. StartMdnsTask();
  2100. break;
  2101. case TaskType::INSECURE_CACHE_LOOKUP:
  2102. InsecureCacheLookup();
  2103. break;
  2104. case TaskType::SECURE_CACHE_LOOKUP:
  2105. case TaskType::CACHE_LOOKUP:
  2106. case TaskType::CONFIG_PRESET:
  2107. // These task types should have been handled synchronously in
  2108. // ResolveLocally() prior to Job creation.
  2109. NOTREACHED();
  2110. break;
  2111. }
  2112. }
  2113. const JobKey& key() const { return key_; }
  2114. bool is_queued() const { return !handle_.is_null(); }
  2115. bool is_running() const { return job_running_; }
  2116. bool HasTargetNetwork() const {
  2117. return key_.GetTargetNetwork() != handles::kInvalidNetworkHandle;
  2118. }
  2119. private:
  2120. base::Value NetLogJobCreationParams(const NetLogSource& source) {
  2121. base::Value::Dict dict;
  2122. source.AddToEventParameters(dict);
  2123. dict.Set("host", ToLogStringValue(key_.host));
  2124. base::Value::List query_types_list;
  2125. for (DnsQueryType query_type : key_.query_types)
  2126. query_types_list.Append(kDnsQueryTypes.at(query_type));
  2127. dict.Set("dns_query_types", std::move(query_types_list));
  2128. dict.Set("secure_dns_mode", base::strict_cast<int>(key_.secure_dns_mode));
  2129. dict.Set("network_isolation_key",
  2130. key_.network_isolation_key.ToDebugString());
  2131. return base::Value(std::move(dict));
  2132. }
  2133. void Finish() {
  2134. if (is_running()) {
  2135. // Clean up but don't run any callbacks.
  2136. proc_task_ = nullptr;
  2137. KillDnsTask();
  2138. mdns_task_ = nullptr;
  2139. job_running_ = false;
  2140. if (dispatched_) {
  2141. // Job should only ever occupy one slot after any tasks that may have
  2142. // required additional slots, e.g. DnsTask, have been killed, and
  2143. // additional slots are expected to be vacated as part of killing the
  2144. // task.
  2145. DCHECK_EQ(1, num_occupied_job_slots_);
  2146. if (resolver_)
  2147. resolver_->dispatcher_->OnJobFinished();
  2148. num_occupied_job_slots_ = 0;
  2149. }
  2150. } else if (is_queued()) {
  2151. DCHECK(dispatched_);
  2152. if (resolver_)
  2153. resolver_->dispatcher_->Cancel(handle_);
  2154. handle_.Reset();
  2155. }
  2156. }
  2157. void KillDnsTask() {
  2158. if (dns_task_) {
  2159. if (dispatched_) {
  2160. while (num_occupied_job_slots_ > 1 || is_queued()) {
  2161. ReduceByOneJobSlot();
  2162. }
  2163. }
  2164. dns_task_.reset();
  2165. }
  2166. }
  2167. // Reduce the number of job slots occupied and queued in the dispatcher by
  2168. // one. If the next Job slot is queued in the dispatcher, cancels the queued
  2169. // job. Otherwise, the next Job has been started by the PrioritizedDispatcher,
  2170. // so signals it is complete.
  2171. void ReduceByOneJobSlot() {
  2172. DCHECK_GE(num_occupied_job_slots_, 1);
  2173. DCHECK(dispatched_);
  2174. if (is_queued()) {
  2175. if (resolver_)
  2176. resolver_->dispatcher_->Cancel(handle_);
  2177. handle_.Reset();
  2178. } else if (num_occupied_job_slots_ > 1) {
  2179. if (resolver_)
  2180. resolver_->dispatcher_->OnJobFinished();
  2181. --num_occupied_job_slots_;
  2182. } else {
  2183. NOTREACHED();
  2184. }
  2185. }
  2186. void UpdatePriority() {
  2187. if (is_queued())
  2188. handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority());
  2189. }
  2190. // PrioritizedDispatcher::Job:
  2191. void Start() override {
  2192. handle_.Reset();
  2193. ++num_occupied_job_slots_;
  2194. if (num_occupied_job_slots_ >= 2) {
  2195. if (!dns_task_) {
  2196. resolver_->dispatcher_->OnJobFinished();
  2197. return;
  2198. }
  2199. StartNextDnsTransaction();
  2200. DCHECK_EQ(num_occupied_job_slots_,
  2201. dns_task_->num_transactions_in_progress());
  2202. if (dns_task_->num_additional_transactions_needed() >= 1) {
  2203. Schedule(true);
  2204. }
  2205. return;
  2206. }
  2207. DCHECK(!is_running());
  2208. DCHECK(!tasks_.empty());
  2209. RunNextTask();
  2210. // Caution: Job::Start must not complete synchronously.
  2211. }
  2212. // TODO(szym): Since DnsTransaction does not consume threads, we can increase
  2213. // the limits on |dispatcher_|. But in order to keep the number of
  2214. // ThreadPool threads low, we will need to use an "inner"
  2215. // PrioritizedDispatcher with tighter limits.
  2216. void StartProcTask() {
  2217. DCHECK(dispatched_);
  2218. DCHECK_EQ(1, num_occupied_job_slots_);
  2219. DCHECK(HasAddressType(key_.query_types));
  2220. proc_task_ = std::make_unique<ProcTask>(
  2221. std::string(GetHostname(key_.host)),
  2222. HostResolver::DnsQueryTypeSetToAddressFamily(key_.query_types),
  2223. key_.flags, resolver_->proc_params_,
  2224. base::BindOnce(&Job::OnProcTaskComplete, base::Unretained(this),
  2225. tick_clock_->NowTicks()),
  2226. proc_task_runner_, net_log_, tick_clock_, key_.GetTargetNetwork());
  2227. // Start() could be called from within Resolve(), hence it must NOT directly
  2228. // call OnProcTaskComplete, for example, on synchronous failure.
  2229. proc_task_->Start();
  2230. }
  2231. // Called by ProcTask when it completes.
  2232. void OnProcTaskComplete(base::TimeTicks start_time,
  2233. int net_error,
  2234. const AddressList& addr_list) {
  2235. DCHECK(proc_task_);
  2236. if (dns_task_error_ != OK && net_error == OK) {
  2237. // This ProcTask was a fallback resolution after a failed insecure
  2238. // DnsTask.
  2239. resolver_->OnFallbackResolve(dns_task_error_);
  2240. }
  2241. if (ContainsIcannNameCollisionIp(addr_list.endpoints()))
  2242. net_error = ERR_ICANN_NAME_COLLISION;
  2243. base::TimeDelta ttl = base::Seconds(kNegativeCacheEntryTTLSeconds);
  2244. if (net_error == OK)
  2245. ttl = base::Seconds(kCacheEntryTTLSeconds);
  2246. auto aliases = std::set<std::string>(addr_list.dns_aliases().begin(),
  2247. addr_list.dns_aliases().end());
  2248. if (!(key_.flags & HOST_RESOLVER_CANONNAME)) {
  2249. aliases = dns_alias_utility::FixUpDnsAliases(aliases);
  2250. }
  2251. // Source unknown because the system resolver could have gotten it from a
  2252. // hosts file, its own cache, a DNS lookup or somewhere else.
  2253. // Don't store the |ttl| in cache since it's not obtained from the server.
  2254. CompleteRequests(
  2255. HostCache::Entry(
  2256. net_error,
  2257. net_error == OK ? addr_list.endpoints() : std::vector<IPEndPoint>(),
  2258. std::move(aliases), HostCache::Entry::SOURCE_UNKNOWN),
  2259. ttl, /*allow_cache=*/true, /*secure=*/false);
  2260. }
  2261. void InsecureCacheLookup() {
  2262. // Insecure cache lookups for requests allowing stale results should have
  2263. // occurred prior to Job creation.
  2264. DCHECK(cache_usage_ != ResolveHostParameters::CacheUsage::STALE_ALLOWED);
  2265. absl::optional<HostCache::EntryStaleness> stale_info;
  2266. absl::optional<HostCache::Entry> resolved = resolver_->MaybeServeFromCache(
  2267. host_cache_, key_.ToCacheKey(/*secure=*/false), cache_usage_,
  2268. false /* ignore_secure */, net_log_, &stale_info);
  2269. if (resolved) {
  2270. DCHECK(stale_info);
  2271. DCHECK(!stale_info.value().is_stale());
  2272. CompleteRequestsWithoutCache(resolved.value(), std::move(stale_info));
  2273. } else {
  2274. RunNextTask();
  2275. }
  2276. }
  2277. void StartDnsTask(bool secure) {
  2278. DCHECK_EQ(secure, !dispatched_);
  2279. DCHECK_EQ(dispatched_ ? 1 : 0, num_occupied_job_slots_);
  2280. DCHECK(!resolver_->ShouldForceSystemResolverDueToTestOverride());
  2281. // Need to create the task even if we're going to post a failure instead of
  2282. // running it, as a "started" job needs a task to be properly cleaned up.
  2283. dns_task_ = std::make_unique<DnsTask>(
  2284. resolver_->dns_client_.get(), key_.host, key_.query_types,
  2285. &*key_.resolve_context, secure, key_.secure_dns_mode, this, net_log_,
  2286. tick_clock_, !tasks_.empty() /* fallback_available */,
  2287. https_svcb_options_);
  2288. dns_task_->StartNextTransaction();
  2289. // Schedule a second transaction, if needed. DoH queries can bypass the
  2290. // dispatcher and start all of their transactions immediately.
  2291. if (secure) {
  2292. while (dns_task_->num_additional_transactions_needed() >= 1)
  2293. dns_task_->StartNextTransaction();
  2294. DCHECK_EQ(dns_task_->num_additional_transactions_needed(), 0);
  2295. } else if (dns_task_->num_additional_transactions_needed() >= 1) {
  2296. Schedule(true);
  2297. }
  2298. }
  2299. void StartNextDnsTransaction() {
  2300. DCHECK(dns_task_);
  2301. DCHECK_EQ(dns_task_->secure(), !dispatched_);
  2302. DCHECK(!dispatched_ || num_occupied_job_slots_ ==
  2303. dns_task_->num_transactions_in_progress() + 1);
  2304. DCHECK_GE(dns_task_->num_additional_transactions_needed(), 1);
  2305. dns_task_->StartNextTransaction();
  2306. }
  2307. // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be
  2308. // deleted before this callback. In this case dns_task is deleted as well,
  2309. // so we use it as indicator whether Job is still valid.
  2310. void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task,
  2311. base::TimeDelta duration,
  2312. bool allow_fallback,
  2313. const HostCache::Entry& failure_results,
  2314. bool secure) {
  2315. DCHECK_NE(OK, failure_results.error());
  2316. if (key_.secure_dns_mode == SecureDnsMode::kSecure) {
  2317. DCHECK(secure);
  2318. UMA_HISTOGRAM_LONG_TIMES_100(
  2319. "Net.DNS.SecureDnsTask.DnsModeSecure.FailureTime", duration);
  2320. } else if (key_.secure_dns_mode == SecureDnsMode::kAutomatic && secure) {
  2321. UMA_HISTOGRAM_LONG_TIMES_100(
  2322. "Net.DNS.SecureDnsTask.DnsModeAutomatic.FailureTime", duration);
  2323. } else {
  2324. UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.InsecureDnsTask.FailureTime",
  2325. duration);
  2326. }
  2327. if (!dns_task)
  2328. return;
  2329. UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.JobQueueTime.Failure",
  2330. total_transaction_time_queued_);
  2331. // If one of the fallback tasks doesn't complete the request, store a result
  2332. // to use during request completion.
  2333. base::TimeDelta ttl =
  2334. failure_results.has_ttl() ? failure_results.ttl() : base::Seconds(0);
  2335. completion_results_.push_back({failure_results, ttl, secure});
  2336. dns_task_error_ = failure_results.error();
  2337. KillDnsTask();
  2338. if (!allow_fallback)
  2339. tasks_.clear();
  2340. RunNextTask();
  2341. }
  2342. // HostResolverManager::DnsTask::Delegate implementation:
  2343. void OnDnsTaskComplete(base::TimeTicks start_time,
  2344. bool allow_fallback,
  2345. HostCache::Entry results,
  2346. bool secure) override {
  2347. DCHECK(dns_task_);
  2348. // Tasks containing address queries are only considered successful overall
  2349. // if they find address results. However, DnsTask may claim success if any
  2350. // transaction, e.g. a supplemental HTTPS transaction, finds results.
  2351. DCHECK(!key_.query_types.Has(DnsQueryType::UNSPECIFIED));
  2352. if (HasAddressType(key_.query_types) && results.error() == OK &&
  2353. (!results.ip_endpoints() || results.ip_endpoints()->empty())) {
  2354. results.set_error(ERR_NAME_NOT_RESOLVED);
  2355. }
  2356. base::TimeDelta duration = tick_clock_->NowTicks() - start_time;
  2357. if (results.error() != OK) {
  2358. OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, allow_fallback,
  2359. results, secure);
  2360. return;
  2361. }
  2362. UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.DnsTask.SuccessTime", duration);
  2363. UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.JobQueueTime.Success",
  2364. total_transaction_time_queued_);
  2365. // Reset the insecure DNS failure counter if an insecure DnsTask completed
  2366. // successfully.
  2367. if (!secure)
  2368. resolver_->dns_client_->ClearInsecureFallbackFailures();
  2369. base::TimeDelta bounded_ttl =
  2370. std::max(results.ttl(), base::Seconds(kMinimumTTLSeconds));
  2371. if ((results.ip_endpoints() &&
  2372. ContainsIcannNameCollisionIp(*results.ip_endpoints()))) {
  2373. CompleteRequestsWithError(ERR_ICANN_NAME_COLLISION);
  2374. return;
  2375. }
  2376. CompleteRequests(results, bounded_ttl, true /* allow_cache */, secure);
  2377. }
  2378. void OnIntermediateTransactionsComplete() override {
  2379. if (dispatched_) {
  2380. DCHECK_GE(num_occupied_job_slots_,
  2381. dns_task_->num_transactions_in_progress());
  2382. int unused_slots =
  2383. num_occupied_job_slots_ - dns_task_->num_transactions_in_progress();
  2384. // Reuse vacated slots for any remaining transactions.
  2385. while (unused_slots > 0 &&
  2386. dns_task_->num_additional_transactions_needed() > 0) {
  2387. dns_task_->StartNextTransaction();
  2388. --unused_slots;
  2389. }
  2390. // If all remaining transactions found a slot, no more needed from the
  2391. // dispatcher.
  2392. if (is_queued() && dns_task_->num_additional_transactions_needed() == 0) {
  2393. resolver_->dispatcher_->Cancel(handle_);
  2394. handle_.Reset();
  2395. }
  2396. // Relinquish any remaining extra slots.
  2397. while (unused_slots > 0) {
  2398. ReduceByOneJobSlot();
  2399. --unused_slots;
  2400. }
  2401. } else if (dns_task_->num_additional_transactions_needed() >= 1) {
  2402. dns_task_->StartNextTransaction();
  2403. }
  2404. }
  2405. void AddTransactionTimeQueued(base::TimeDelta time_queued) override {
  2406. total_transaction_time_queued_ += time_queued;
  2407. }
  2408. void StartMdnsTask() {
  2409. // No flags are supported for MDNS except
  2410. // HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6 (which is not actually an
  2411. // input flag).
  2412. DCHECK_EQ(0, key_.flags & ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6);
  2413. MDnsClient* client = nullptr;
  2414. int rv = resolver_->GetOrCreateMdnsClient(&client);
  2415. mdns_task_ = std::make_unique<HostResolverMdnsTask>(
  2416. client, std::string{GetHostname(key_.host)}, key_.query_types);
  2417. if (rv == OK) {
  2418. mdns_task_->Start(
  2419. base::BindOnce(&Job::OnMdnsTaskComplete, base::Unretained(this)));
  2420. } else {
  2421. // Could not create an mDNS client. Since we cannot complete synchronously
  2422. // from here, post a failure without starting the task.
  2423. base::SequencedTaskRunnerHandle::Get()->PostTask(
  2424. FROM_HERE, base::BindOnce(&Job::OnMdnsImmediateFailure,
  2425. weak_ptr_factory_.GetWeakPtr(), rv));
  2426. }
  2427. }
  2428. void OnMdnsTaskComplete() {
  2429. DCHECK(mdns_task_);
  2430. // TODO(crbug.com/846423): Consider adding MDNS-specific logging.
  2431. HostCache::Entry results = mdns_task_->GetResults();
  2432. if ((results.ip_endpoints() &&
  2433. ContainsIcannNameCollisionIp(*results.ip_endpoints()))) {
  2434. CompleteRequestsWithError(ERR_ICANN_NAME_COLLISION);
  2435. return;
  2436. }
  2437. // MDNS uses a separate cache, so skip saving result to cache.
  2438. // TODO(crbug.com/926300): Consider merging caches.
  2439. CompleteRequestsWithoutCache(results, absl::nullopt /* stale_info */);
  2440. }
  2441. void OnMdnsImmediateFailure(int rv) {
  2442. DCHECK(mdns_task_);
  2443. DCHECK_NE(OK, rv);
  2444. CompleteRequestsWithError(rv);
  2445. }
  2446. void RecordJobHistograms(int error) {
  2447. // Used in UMA_HISTOGRAM_ENUMERATION. Do not renumber entries or reuse
  2448. // deprecated values.
  2449. enum Category {
  2450. RESOLVE_SUCCESS = 0,
  2451. RESOLVE_FAIL = 1,
  2452. RESOLVE_SPECULATIVE_SUCCESS = 2,
  2453. RESOLVE_SPECULATIVE_FAIL = 3,
  2454. RESOLVE_ABORT = 4,
  2455. RESOLVE_SPECULATIVE_ABORT = 5,
  2456. RESOLVE_MAX, // Bounding value.
  2457. };
  2458. Category category = RESOLVE_MAX; // Illegal value for later DCHECK only.
  2459. base::TimeDelta duration = tick_clock_->NowTicks() - start_time_;
  2460. if (error == OK) {
  2461. if (had_non_speculative_request_) {
  2462. category = RESOLVE_SUCCESS;
  2463. UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveSuccessTime", duration);
  2464. } else {
  2465. category = RESOLVE_SPECULATIVE_SUCCESS;
  2466. }
  2467. } else if (error == ERR_NETWORK_CHANGED ||
  2468. error == ERR_HOST_RESOLVER_QUEUE_TOO_LARGE) {
  2469. category = had_non_speculative_request_ ? RESOLVE_ABORT
  2470. : RESOLVE_SPECULATIVE_ABORT;
  2471. } else {
  2472. if (had_non_speculative_request_) {
  2473. category = RESOLVE_FAIL;
  2474. UMA_HISTOGRAM_LONG_TIMES_100("Net.DNS.ResolveFailureTime", duration);
  2475. } else {
  2476. category = RESOLVE_SPECULATIVE_FAIL;
  2477. }
  2478. }
  2479. DCHECK_LT(static_cast<int>(category),
  2480. static_cast<int>(RESOLVE_MAX)); // Be sure it was set.
  2481. UMA_HISTOGRAM_ENUMERATION("Net.DNS.ResolveCategory", category, RESOLVE_MAX);
  2482. if (category == RESOLVE_FAIL ||
  2483. (start_time_ != base::TimeTicks() && category == RESOLVE_ABORT)) {
  2484. if (duration < base::Milliseconds(10))
  2485. base::UmaHistogramSparse("Net.DNS.ResolveError.Fast", std::abs(error));
  2486. else
  2487. base::UmaHistogramSparse("Net.DNS.ResolveError.Slow", std::abs(error));
  2488. }
  2489. if (had_non_speculative_request_) {
  2490. UmaHistogramMediumTimes(
  2491. base::StringPrintf(
  2492. "Net.DNS.SecureDnsMode.%s.ResolveTime",
  2493. SecureDnsModeToString(key_.secure_dns_mode).c_str()),
  2494. duration);
  2495. }
  2496. }
  2497. void MaybeCacheResult(const HostCache::Entry& results,
  2498. base::TimeDelta ttl,
  2499. bool secure) {
  2500. // If the request did not complete, don't cache it.
  2501. if (!results.did_complete())
  2502. return;
  2503. resolver_->CacheResult(host_cache_, key_.ToCacheKey(secure), results, ttl);
  2504. }
  2505. // Performs Job's last rites. Completes all Requests. Deletes this.
  2506. //
  2507. // If not |allow_cache|, result will not be stored in the host cache, even if
  2508. // result would otherwise allow doing so. Update the key to reflect |secure|,
  2509. // which indicates whether or not the result was obtained securely.
  2510. void CompleteRequests(const HostCache::Entry& results,
  2511. base::TimeDelta ttl,
  2512. bool allow_cache,
  2513. bool secure) {
  2514. CHECK(resolver_.get());
  2515. // This job must be removed from resolver's |jobs_| now to make room for a
  2516. // new job with the same key in case one of the OnComplete callbacks decides
  2517. // to spawn one. Consequently, if the job was owned by |jobs_|, the job
  2518. // deletes itself when CompleteRequests is done.
  2519. std::unique_ptr<Job> self_deleter;
  2520. if (self_iterator_)
  2521. self_deleter = resolver_->RemoveJob(self_iterator_.value());
  2522. Finish();
  2523. if (results.error() == ERR_DNS_REQUEST_CANCELLED) {
  2524. net_log_.AddEvent(NetLogEventType::CANCELLED);
  2525. net_log_.EndEventWithNetErrorCode(
  2526. NetLogEventType::HOST_RESOLVER_MANAGER_JOB, OK);
  2527. return;
  2528. }
  2529. net_log_.EndEventWithNetErrorCode(
  2530. NetLogEventType::HOST_RESOLVER_MANAGER_JOB, results.error());
  2531. // Handle all caching before completing requests as completing requests may
  2532. // start new requests that rely on cached results.
  2533. if (allow_cache)
  2534. MaybeCacheResult(results, ttl, secure);
  2535. RecordJobHistograms(results.error());
  2536. // Complete all of the requests that were attached to the job and
  2537. // detach them.
  2538. while (!requests_.empty()) {
  2539. RequestImpl* req = requests_.head()->value();
  2540. req->RemoveFromList();
  2541. CHECK(key_ == req->GetJobKey());
  2542. if (results.error() == OK && !req->parameters().is_speculative) {
  2543. req->set_results(
  2544. results.CopyWithDefaultPort(GetPort(req->request_host())));
  2545. }
  2546. req->OnJobCompleted(
  2547. key_, results.error(),
  2548. secure && results.error() != OK /* is_secure_network_error */);
  2549. // Check if the resolver was destroyed as a result of running the
  2550. // callback. If it was, we could continue, but we choose to bail.
  2551. if (!resolver_.get())
  2552. return;
  2553. }
  2554. // TODO(crbug.com/1200908): Call StartBootstrapFollowup() if any of the
  2555. // requests have the Bootstrap policy. Note: A naive implementation could
  2556. // cause an infinite loop if the bootstrap result has TTL=0.
  2557. }
  2558. void CompleteRequestsWithoutCache(
  2559. const HostCache::Entry& results,
  2560. absl::optional<HostCache::EntryStaleness> stale_info) {
  2561. // Record the stale_info for all non-speculative requests, if it exists.
  2562. if (stale_info) {
  2563. for (auto* node = requests_.head(); node != requests_.end();
  2564. node = node->next()) {
  2565. if (!node->value()->parameters().is_speculative)
  2566. node->value()->set_stale_info(stale_info.value());
  2567. }
  2568. }
  2569. CompleteRequests(results, base::TimeDelta(), false /* allow_cache */,
  2570. false /* secure */);
  2571. }
  2572. // Convenience wrapper for CompleteRequests in case of failure.
  2573. void CompleteRequestsWithError(int net_error) {
  2574. DCHECK_NE(OK, net_error);
  2575. CompleteRequests(
  2576. HostCache::Entry(net_error, HostCache::Entry::SOURCE_UNKNOWN),
  2577. base::TimeDelta(), true /* allow_cache */, false /* secure */);
  2578. }
  2579. RequestPriority priority() const override {
  2580. return priority_tracker_.highest_priority();
  2581. }
  2582. // Number of non-canceled requests in |requests_|.
  2583. size_t num_active_requests() const { return priority_tracker_.total_count(); }
  2584. base::WeakPtr<HostResolverManager> resolver_;
  2585. const JobKey key_;
  2586. const ResolveHostParameters::CacheUsage cache_usage_;
  2587. // TODO(crbug.com/969847): Consider allowing requests within a single Job to
  2588. // have different HostCaches.
  2589. const raw_ptr<HostCache> host_cache_;
  2590. struct CompletionResult {
  2591. const HostCache::Entry entry;
  2592. base::TimeDelta ttl;
  2593. bool secure;
  2594. };
  2595. // Results to use in last-ditch attempt to complete request.
  2596. std::vector<CompletionResult> completion_results_;
  2597. // The sequence of tasks to run in this Job. Tasks may be aborted and removed
  2598. // from the sequence, but otherwise the tasks will run in order until a
  2599. // successful result is found.
  2600. std::deque<TaskType> tasks_;
  2601. // Whether the job is running.
  2602. bool job_running_ = false;
  2603. // Tracks the highest priority across |requests_|.
  2604. PriorityTracker priority_tracker_;
  2605. // Task runner used for HostResolverProc.
  2606. scoped_refptr<base::TaskRunner> proc_task_runner_;
  2607. bool had_non_speculative_request_ = false;
  2608. // Number of slots occupied by this Job in |dispatcher_|. Should be 0 when
  2609. // the job is not registered with any dispatcher.
  2610. int num_occupied_job_slots_ = 0;
  2611. // True once this Job has been sent to `resolver_->dispatcher_`.
  2612. bool dispatched_ = false;
  2613. // Result of DnsTask.
  2614. int dns_task_error_ = OK;
  2615. raw_ptr<const base::TickClock> tick_clock_;
  2616. base::TimeTicks start_time_;
  2617. HostResolver::HttpsSvcbOptions https_svcb_options_;
  2618. NetLogWithSource net_log_;
  2619. // Resolves the host using a HostResolverProc.
  2620. std::unique_ptr<ProcTask> proc_task_;
  2621. // Resolves the host using a DnsTransaction.
  2622. std::unique_ptr<DnsTask> dns_task_;
  2623. // Resolves the host using MDnsClient.
  2624. std::unique_ptr<HostResolverMdnsTask> mdns_task_;
  2625. // All Requests waiting for the result of this Job. Some can be canceled.
  2626. base::LinkedList<RequestImpl> requests_;
  2627. // A handle used for |dispatcher_|.
  2628. PrioritizedDispatcher::Handle handle_;
  2629. // Iterator to |this| in the JobMap. |nullopt| if not owned by the JobMap.
  2630. absl::optional<JobMap::iterator> self_iterator_;
  2631. base::TimeDelta total_transaction_time_queued_;
  2632. base::WeakPtrFactory<Job> weak_ptr_factory_{this};
  2633. };
  2634. //-----------------------------------------------------------------------------
  2635. HostResolverManager::HostResolverManager(
  2636. const HostResolver::ManagerOptions& options,
  2637. SystemDnsConfigChangeNotifier* system_dns_config_notifier,
  2638. NetLog* net_log)
  2639. : HostResolverManager(PassKey(),
  2640. options,
  2641. system_dns_config_notifier,
  2642. handles::kInvalidNetworkHandle,
  2643. net_log) {}
  2644. HostResolverManager::HostResolverManager(
  2645. base::PassKey<HostResolverManager>,
  2646. const HostResolver::ManagerOptions& options,
  2647. SystemDnsConfigChangeNotifier* system_dns_config_notifier,
  2648. handles::NetworkHandle target_network,
  2649. NetLog* net_log)
  2650. : proc_params_(nullptr, options.max_system_retry_attempts),
  2651. net_log_(net_log),
  2652. system_dns_config_notifier_(system_dns_config_notifier),
  2653. target_network_(target_network),
  2654. check_ipv6_on_wifi_(options.check_ipv6_on_wifi),
  2655. tick_clock_(base::DefaultTickClock::GetInstance()),
  2656. https_svcb_options_(
  2657. options.https_svcb_options
  2658. ? *options.https_svcb_options
  2659. : HostResolver::HttpsSvcbOptions::FromFeatures()) {
  2660. PrioritizedDispatcher::Limits job_limits = GetDispatcherLimits(options);
  2661. dispatcher_ = std::make_unique<PrioritizedDispatcher>(job_limits);
  2662. max_queued_jobs_ = job_limits.total_jobs * 100u;
  2663. DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES));
  2664. proc_task_runner_ = base::ThreadPool::CreateTaskRunner(
  2665. {base::MayBlock(), priority_mode.Get(),
  2666. base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN});
  2667. #if BUILDFLAG(IS_WIN)
  2668. EnsureWinsockInit();
  2669. #endif
  2670. #if (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID)) || \
  2671. BUILDFLAG(IS_FUCHSIA)
  2672. RunLoopbackProbeJob();
  2673. #endif
  2674. // Network-bound HostResolverManagers don't need to act on network changes.
  2675. if (!IsBoundToNetwork()) {
  2676. NetworkChangeNotifier::AddIPAddressObserver(this);
  2677. NetworkChangeNotifier::AddConnectionTypeObserver(this);
  2678. }
  2679. if (system_dns_config_notifier_)
  2680. system_dns_config_notifier_->AddObserver(this);
  2681. #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_OPENBSD) && \
  2682. !BUILDFLAG(IS_ANDROID)
  2683. EnsureDnsReloaderInit();
  2684. #endif
  2685. auto connection_type =
  2686. IsBoundToNetwork()
  2687. ? NetworkChangeNotifier::GetNetworkConnectionType(target_network)
  2688. : NetworkChangeNotifier::GetConnectionType();
  2689. UpdateConnectionType(connection_type);
  2690. #if defined(ENABLE_BUILT_IN_DNS)
  2691. dns_client_ = DnsClient::CreateClient(net_log_);
  2692. dns_client_->SetInsecureEnabled(
  2693. options.insecure_dns_client_enabled,
  2694. options.additional_types_via_insecure_dns_enabled);
  2695. dns_client_->SetConfigOverrides(options.dns_config_overrides);
  2696. #else
  2697. DCHECK(options.dns_config_overrides == DnsConfigOverrides());
  2698. #endif
  2699. allow_fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial();
  2700. }
  2701. HostResolverManager::~HostResolverManager() {
  2702. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  2703. // Prevent the dispatcher from starting new jobs.
  2704. dispatcher_->SetLimitsToZero();
  2705. // It's now safe for Jobs to call KillDnsTask on destruction, because
  2706. // OnJobComplete will not start any new jobs.
  2707. jobs_.clear();
  2708. if (target_network_ == handles::kInvalidNetworkHandle) {
  2709. NetworkChangeNotifier::RemoveIPAddressObserver(this);
  2710. NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
  2711. }
  2712. if (system_dns_config_notifier_)
  2713. system_dns_config_notifier_->RemoveObserver(this);
  2714. }
  2715. // static
  2716. std::unique_ptr<HostResolverManager>
  2717. HostResolverManager::CreateNetworkBoundHostResolverManager(
  2718. const HostResolver::ManagerOptions& options,
  2719. handles::NetworkHandle target_network,
  2720. NetLog* net_log) {
  2721. #if BUILDFLAG(IS_ANDROID)
  2722. DCHECK(NetworkChangeNotifier::AreNetworkHandlesSupported());
  2723. return std::make_unique<HostResolverManager>(
  2724. PassKey(), options, nullptr /* system_dns_config_notifier */,
  2725. target_network, net_log);
  2726. #else // !BUILDFLAG(IS_ANDROID)
  2727. NOTIMPLEMENTED();
  2728. return nullptr;
  2729. #endif // BUILDFLAG(IS_ANDROID)
  2730. }
  2731. std::unique_ptr<HostResolver::ResolveHostRequest>
  2732. HostResolverManager::CreateRequest(
  2733. HostResolver::Host host,
  2734. NetworkIsolationKey network_isolation_key,
  2735. NetLogWithSource net_log,
  2736. absl::optional<ResolveHostParameters> optional_parameters,
  2737. ResolveContext* resolve_context,
  2738. HostCache* host_cache) {
  2739. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  2740. DCHECK(!invalidation_in_progress_);
  2741. DCHECK_EQ(resolve_context->GetTargetNetwork(), target_network_);
  2742. // ResolveContexts must register (via RegisterResolveContext()) before use to
  2743. // ensure cached data is invalidated on network and configuration changes.
  2744. DCHECK(registered_contexts_.HasObserver(resolve_context));
  2745. return std::make_unique<RequestImpl>(
  2746. std::move(net_log), std::move(host), std::move(network_isolation_key),
  2747. std::move(optional_parameters), resolve_context->GetWeakPtr(), host_cache,
  2748. weak_ptr_factory_.GetWeakPtr(), tick_clock_);
  2749. }
  2750. std::unique_ptr<HostResolver::ProbeRequest>
  2751. HostResolverManager::CreateDohProbeRequest(ResolveContext* context) {
  2752. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  2753. return std::make_unique<ProbeRequestImpl>(context->GetWeakPtr(),
  2754. weak_ptr_factory_.GetWeakPtr());
  2755. }
  2756. std::unique_ptr<HostResolver::MdnsListener>
  2757. HostResolverManager::CreateMdnsListener(const HostPortPair& host,
  2758. DnsQueryType query_type) {
  2759. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  2760. DCHECK_NE(DnsQueryType::UNSPECIFIED, query_type);
  2761. auto listener =
  2762. std::make_unique<HostResolverMdnsListenerImpl>(host, query_type);
  2763. MDnsClient* client;
  2764. int rv = GetOrCreateMdnsClient(&client);
  2765. if (rv == OK) {
  2766. std::unique_ptr<net::MDnsListener> inner_listener = client->CreateListener(
  2767. DnsQueryTypeToQtype(query_type), host.host(), listener.get());
  2768. listener->set_inner_listener(std::move(inner_listener));
  2769. } else {
  2770. listener->set_initialization_error(rv);
  2771. }
  2772. return listener;
  2773. }
  2774. void HostResolverManager::SetInsecureDnsClientEnabled(
  2775. bool enabled,
  2776. bool additional_dns_types_enabled) {
  2777. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  2778. if (!dns_client_)
  2779. return;
  2780. bool enabled_before = dns_client_->CanUseInsecureDnsTransactions();
  2781. bool additional_types_before =
  2782. enabled_before && dns_client_->CanQueryAdditionalTypesViaInsecureDns();
  2783. dns_client_->SetInsecureEnabled(enabled, additional_dns_types_enabled);
  2784. // Abort current tasks if `CanUseInsecureDnsTransactions()` changes or if
  2785. // insecure transactions are enabled and
  2786. // `CanQueryAdditionalTypesViaInsecureDns()` changes. Changes to allowing
  2787. // additional types don't matter if insecure transactions are completely
  2788. // disabled.
  2789. if (dns_client_->CanUseInsecureDnsTransactions() != enabled_before ||
  2790. (dns_client_->CanUseInsecureDnsTransactions() &&
  2791. dns_client_->CanQueryAdditionalTypesViaInsecureDns() !=
  2792. additional_types_before)) {
  2793. AbortInsecureDnsTasks(ERR_NETWORK_CHANGED, false /* fallback_only */);
  2794. }
  2795. }
  2796. base::Value HostResolverManager::GetDnsConfigAsValue() const {
  2797. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  2798. return dns_client_ ? dns_client_->GetDnsConfigAsValueForNetLog()
  2799. : base::Value(base::Value::Dict());
  2800. }
  2801. void HostResolverManager::SetDnsConfigOverrides(DnsConfigOverrides overrides) {
  2802. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  2803. if (!dns_client_ && overrides == DnsConfigOverrides())
  2804. return;
  2805. // Not allowed to set overrides if compiled without DnsClient.
  2806. DCHECK(dns_client_);
  2807. bool transactions_allowed_before =
  2808. dns_client_->CanUseSecureDnsTransactions() ||
  2809. dns_client_->CanUseInsecureDnsTransactions();
  2810. bool changed = dns_client_->SetConfigOverrides(std::move(overrides));
  2811. if (changed) {
  2812. NetworkChangeNotifier::TriggerNonSystemDnsChange();
  2813. // Only invalidate cache if new overrides have resulted in a config change.
  2814. InvalidateCaches();
  2815. // Need to update jobs iff transactions were previously allowed because
  2816. // in-progress jobs may be running using a now-invalid configuration.
  2817. if (transactions_allowed_before) {
  2818. UpdateJobsForChangedConfig();
  2819. }
  2820. }
  2821. }
  2822. void HostResolverManager::RegisterResolveContext(ResolveContext* context) {
  2823. registered_contexts_.AddObserver(context);
  2824. context->InvalidateCachesAndPerSessionData(
  2825. dns_client_ ? dns_client_->GetCurrentSession() : nullptr,
  2826. false /* network_change */);
  2827. }
  2828. void HostResolverManager::DeregisterResolveContext(
  2829. const ResolveContext* context) {
  2830. registered_contexts_.RemoveObserver(context);
  2831. // Destroy Jobs when their context is closed.
  2832. RemoveAllJobs(context);
  2833. }
  2834. void HostResolverManager::SetTickClockForTesting(
  2835. const base::TickClock* tick_clock) {
  2836. tick_clock_ = tick_clock;
  2837. }
  2838. void HostResolverManager::SetMaxQueuedJobsForTesting(size_t value) {
  2839. DCHECK_EQ(0u, dispatcher_->num_queued_jobs());
  2840. DCHECK_GE(value, 0u);
  2841. max_queued_jobs_ = value;
  2842. }
  2843. void HostResolverManager::SetHaveOnlyLoopbackAddresses(bool result) {
  2844. if (result) {
  2845. additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY;
  2846. } else {
  2847. additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY;
  2848. }
  2849. }
  2850. void HostResolverManager::SetMdnsSocketFactoryForTesting(
  2851. std::unique_ptr<MDnsSocketFactory> socket_factory) {
  2852. DCHECK(!mdns_client_);
  2853. mdns_socket_factory_ = std::move(socket_factory);
  2854. }
  2855. void HostResolverManager::SetMdnsClientForTesting(
  2856. std::unique_ptr<MDnsClient> client) {
  2857. mdns_client_ = std::move(client);
  2858. }
  2859. void HostResolverManager::SetDnsClientForTesting(
  2860. std::unique_ptr<DnsClient> dns_client) {
  2861. DCHECK(dns_client);
  2862. if (dns_client_) {
  2863. if (!dns_client->GetSystemConfigForTesting())
  2864. dns_client->SetSystemConfig(dns_client_->GetSystemConfigForTesting());
  2865. dns_client->SetConfigOverrides(dns_client_->GetConfigOverridesForTesting());
  2866. }
  2867. dns_client_ = std::move(dns_client);
  2868. // Inform `registered_contexts_` of the new `DnsClient`.
  2869. InvalidateCaches();
  2870. }
  2871. void HostResolverManager::SetLastIPv6ProbeResultForTesting(
  2872. bool last_ipv6_probe_result) {
  2873. SetLastIPv6ProbeResult(last_ipv6_probe_result);
  2874. }
  2875. void HostResolverManager::SetTaskRunnerForTesting(
  2876. scoped_refptr<base::TaskRunner> task_runner) {
  2877. proc_task_runner_ = std::move(task_runner);
  2878. }
  2879. // static
  2880. bool HostResolverManager::IsLocalTask(TaskType task) {
  2881. switch (task) {
  2882. case TaskType::SECURE_CACHE_LOOKUP:
  2883. case TaskType::INSECURE_CACHE_LOOKUP:
  2884. case TaskType::CACHE_LOOKUP:
  2885. case TaskType::CONFIG_PRESET:
  2886. return true;
  2887. default:
  2888. return false;
  2889. }
  2890. }
  2891. int HostResolverManager::Resolve(RequestImpl* request) {
  2892. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  2893. // Request should not yet have a scheduled Job.
  2894. DCHECK(!request->HasJob());
  2895. // Request may only be resolved once.
  2896. DCHECK(!request->complete());
  2897. // MDNS requests do not support skipping cache or stale lookups.
  2898. // TODO(crbug.com/926300): Either add support for skipping the MDNS cache, or
  2899. // merge to use the normal host cache for MDNS requests.
  2900. DCHECK(request->parameters().source != HostResolverSource::MULTICAST_DNS ||
  2901. request->parameters().cache_usage ==
  2902. ResolveHostParameters::CacheUsage::ALLOWED);
  2903. DCHECK(!invalidation_in_progress_);
  2904. const auto& parameters = request->parameters();
  2905. JobKey job_key(request->resolve_context());
  2906. job_key.host =
  2907. CreateHostForJobKey(request->request_host(), parameters.dns_query_type,
  2908. https_svcb_options_.enable);
  2909. job_key.network_isolation_key = request->network_isolation_key();
  2910. job_key.source = parameters.source;
  2911. IPAddress ip_address;
  2912. bool is_ip = ip_address.AssignFromIPLiteral(GetHostname(job_key.host));
  2913. GetEffectiveParametersForRequest(
  2914. job_key.host, parameters.dns_query_type, request->host_resolver_flags(),
  2915. parameters.secure_dns_policy, is_ip, request->source_net_log(),
  2916. &job_key.query_types, &job_key.flags, &job_key.secure_dns_mode);
  2917. std::deque<TaskType> tasks;
  2918. absl::optional<HostCache::EntryStaleness> stale_info;
  2919. HostCache::Entry results = ResolveLocally(
  2920. job_key, ip_address, parameters.cache_usage, parameters.secure_dns_policy,
  2921. request->source_net_log(), request->host_cache(), &tasks, &stale_info);
  2922. if (results.error() != ERR_DNS_CACHE_MISS ||
  2923. request->parameters().source == HostResolverSource::LOCAL_ONLY ||
  2924. tasks.empty()) {
  2925. if (results.error() == OK && !request->parameters().is_speculative) {
  2926. request->set_results(
  2927. results.CopyWithDefaultPort(GetPort(request->request_host())));
  2928. }
  2929. if (stale_info && !request->parameters().is_speculative)
  2930. request->set_stale_info(std::move(stale_info).value());
  2931. request->set_error_info(results.error(),
  2932. false /* is_secure_network_error */);
  2933. return HostResolver::SquashErrorCode(results.error());
  2934. }
  2935. CreateAndStartJob(std::move(job_key), std::move(tasks), request);
  2936. return ERR_IO_PENDING;
  2937. }
  2938. HostCache::Entry HostResolverManager::ResolveLocally(
  2939. const JobKey& job_key,
  2940. const IPAddress& ip_address,
  2941. ResolveHostParameters::CacheUsage cache_usage,
  2942. SecureDnsPolicy secure_dns_policy,
  2943. const NetLogWithSource& source_net_log,
  2944. HostCache* cache,
  2945. std::deque<TaskType>* out_tasks,
  2946. absl::optional<HostCache::EntryStaleness>* out_stale_info) {
  2947. DCHECK(out_stale_info);
  2948. *out_stale_info = absl::nullopt;
  2949. CreateTaskSequence(job_key, cache_usage, secure_dns_policy, out_tasks);
  2950. if (!ip_address.IsValid()) {
  2951. // Check that the caller supplied a valid hostname to resolve. For
  2952. // MULTICAST_DNS, we are less restrictive.
  2953. // TODO(ericorth): Control validation based on an explicit flag rather
  2954. // than implicitly based on |source|.
  2955. const bool is_valid_hostname =
  2956. job_key.source == HostResolverSource::MULTICAST_DNS
  2957. ? IsValidUnrestrictedDNSDomain(GetHostname(job_key.host))
  2958. : IsValidDNSDomain(GetHostname(job_key.host));
  2959. if (!is_valid_hostname) {
  2960. return HostCache::Entry(ERR_NAME_NOT_RESOLVED,
  2961. HostCache::Entry::SOURCE_UNKNOWN);
  2962. }
  2963. }
  2964. bool resolve_canonname = job_key.flags & HOST_RESOLVER_CANONNAME;
  2965. bool default_family_due_to_no_ipv6 =
  2966. job_key.flags & HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
  2967. // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
  2968. // On Windows it gives the default interface's address, whereas on Linux it
  2969. // gives an error. We will make it fail on all platforms for consistency.
  2970. if (GetHostname(job_key.host).empty() ||
  2971. GetHostname(job_key.host).size() > kMaxHostLength) {
  2972. return HostCache::Entry(ERR_NAME_NOT_RESOLVED,
  2973. HostCache::Entry::SOURCE_UNKNOWN);
  2974. }
  2975. if (ip_address.IsValid())
  2976. return ResolveAsIP(job_key.query_types, resolve_canonname, ip_address);
  2977. // Special-case localhost names, as per the recommendations in
  2978. // https://tools.ietf.org/html/draft-west-let-localhost-be-localhost.
  2979. absl::optional<HostCache::Entry> resolved =
  2980. ServeLocalhost(GetHostname(job_key.host), job_key.query_types,
  2981. default_family_due_to_no_ipv6);
  2982. if (resolved)
  2983. return resolved.value();
  2984. // Do initial cache lookups.
  2985. while (!out_tasks->empty() && IsLocalTask(out_tasks->front())) {
  2986. TaskType task = out_tasks->front();
  2987. out_tasks->pop_front();
  2988. if (task == TaskType::SECURE_CACHE_LOOKUP ||
  2989. task == TaskType::INSECURE_CACHE_LOOKUP ||
  2990. task == TaskType::CACHE_LOOKUP) {
  2991. bool secure = task == TaskType::SECURE_CACHE_LOOKUP;
  2992. HostCache::Key key = job_key.ToCacheKey(secure);
  2993. bool ignore_secure = task == TaskType::CACHE_LOOKUP;
  2994. resolved = MaybeServeFromCache(cache, key, cache_usage, ignore_secure,
  2995. source_net_log, out_stale_info);
  2996. if (resolved) {
  2997. // |MaybeServeFromCache()| will update |*out_stale_info| as needed.
  2998. DCHECK(out_stale_info->has_value());
  2999. source_net_log.AddEvent(
  3000. NetLogEventType::HOST_RESOLVER_MANAGER_CACHE_HIT,
  3001. [&] { return NetLogResults(resolved.value()); });
  3002. // TODO(crbug.com/1200908): Call StartBootstrapFollowup() if the Secure
  3003. // DNS Policy is kBootstrap and the result is not secure. Note: A naive
  3004. // implementation could cause an infinite loop if |resolved| always
  3005. // expires or is evicted before the followup runs.
  3006. return resolved.value();
  3007. }
  3008. DCHECK(!out_stale_info->has_value());
  3009. } else if (task == TaskType::CONFIG_PRESET) {
  3010. resolved = MaybeReadFromConfig(job_key);
  3011. if (resolved) {
  3012. source_net_log.AddEvent(
  3013. NetLogEventType::HOST_RESOLVER_MANAGER_CONFIG_PRESET_MATCH,
  3014. [&] { return NetLogResults(resolved.value()); });
  3015. StartBootstrapFollowup(job_key, cache, source_net_log);
  3016. return resolved.value();
  3017. }
  3018. } else {
  3019. NOTREACHED();
  3020. }
  3021. }
  3022. // TODO(szym): Do not do this if nsswitch.conf instructs not to.
  3023. // http://crbug.com/117655
  3024. resolved = ServeFromHosts(GetHostname(job_key.host), job_key.query_types,
  3025. default_family_due_to_no_ipv6, *out_tasks);
  3026. if (resolved) {
  3027. source_net_log.AddEvent(NetLogEventType::HOST_RESOLVER_MANAGER_HOSTS_HIT,
  3028. [&] { return NetLogResults(resolved.value()); });
  3029. return resolved.value();
  3030. }
  3031. return HostCache::Entry(ERR_DNS_CACHE_MISS, HostCache::Entry::SOURCE_UNKNOWN);
  3032. }
  3033. void HostResolverManager::CreateAndStartJob(JobKey key,
  3034. std::deque<TaskType> tasks,
  3035. RequestImpl* request) {
  3036. DCHECK(!tasks.empty());
  3037. auto jobit = jobs_.find(key);
  3038. Job* job;
  3039. if (jobit == jobs_.end()) {
  3040. job = AddJobWithoutRequest(key, request->parameters().cache_usage,
  3041. request->host_cache(), std::move(tasks),
  3042. request->priority(), request->source_net_log());
  3043. job->AddRequest(request);
  3044. job->RunNextTask();
  3045. } else {
  3046. job = jobit->second.get();
  3047. job->AddRequest(request);
  3048. }
  3049. }
  3050. HostResolverManager::Job* HostResolverManager::AddJobWithoutRequest(
  3051. JobKey key,
  3052. ResolveHostParameters::CacheUsage cache_usage,
  3053. HostCache* host_cache,
  3054. std::deque<TaskType> tasks,
  3055. RequestPriority priority,
  3056. const NetLogWithSource& source_net_log) {
  3057. auto new_job = std::make_unique<Job>(
  3058. weak_ptr_factory_.GetWeakPtr(), key, cache_usage, host_cache,
  3059. std::move(tasks), priority, proc_task_runner_, source_net_log,
  3060. tick_clock_, https_svcb_options_);
  3061. auto insert_result = jobs_.emplace(std::move(key), std::move(new_job));
  3062. auto& iterator = insert_result.first;
  3063. bool is_new = insert_result.second;
  3064. DCHECK(is_new);
  3065. auto& job = iterator->second;
  3066. job->OnAddedToJobMap(iterator);
  3067. return job.get();
  3068. }
  3069. HostCache::Entry HostResolverManager::ResolveAsIP(DnsQueryTypeSet query_types,
  3070. bool resolve_canonname,
  3071. const IPAddress& ip_address) {
  3072. DCHECK(ip_address.IsValid());
  3073. DCHECK(!query_types.Has(DnsQueryType::UNSPECIFIED));
  3074. // IP literals cannot resolve unless the query type is an address query that
  3075. // allows addresses with the same address family as the literal. E.g., don't
  3076. // return IPv6 addresses for IPv4 queries or anything for a non-address query.
  3077. AddressFamily family = GetAddressFamily(ip_address);
  3078. if (!query_types.Has(AddressFamilyToDnsQueryType(family))) {
  3079. return HostCache::Entry(ERR_NAME_NOT_RESOLVED,
  3080. HostCache::Entry::SOURCE_UNKNOWN);
  3081. }
  3082. std::set<std::string> aliases;
  3083. if (resolve_canonname) {
  3084. aliases = {ip_address.ToString()};
  3085. }
  3086. return HostCache::Entry(OK, {IPEndPoint(ip_address, 0)}, std::move(aliases),
  3087. HostCache::Entry::SOURCE_UNKNOWN);
  3088. }
  3089. absl::optional<HostCache::Entry> HostResolverManager::MaybeServeFromCache(
  3090. HostCache* cache,
  3091. const HostCache::Key& key,
  3092. ResolveHostParameters::CacheUsage cache_usage,
  3093. bool ignore_secure,
  3094. const NetLogWithSource& source_net_log,
  3095. absl::optional<HostCache::EntryStaleness>* out_stale_info) {
  3096. DCHECK(out_stale_info);
  3097. *out_stale_info = absl::nullopt;
  3098. if (!cache)
  3099. return absl::nullopt;
  3100. if (cache_usage == ResolveHostParameters::CacheUsage::DISALLOWED)
  3101. return absl::nullopt;
  3102. // Local-only requests search the cache for non-local-only results.
  3103. HostCache::Key effective_key = key;
  3104. if (effective_key.host_resolver_source == HostResolverSource::LOCAL_ONLY)
  3105. effective_key.host_resolver_source = HostResolverSource::ANY;
  3106. const std::pair<const HostCache::Key, HostCache::Entry>* cache_result;
  3107. HostCache::EntryStaleness staleness;
  3108. if (cache_usage == ResolveHostParameters::CacheUsage::STALE_ALLOWED) {
  3109. cache_result = cache->LookupStale(effective_key, tick_clock_->NowTicks(),
  3110. &staleness, ignore_secure);
  3111. } else {
  3112. DCHECK(cache_usage == ResolveHostParameters::CacheUsage::ALLOWED);
  3113. cache_result =
  3114. cache->Lookup(effective_key, tick_clock_->NowTicks(), ignore_secure);
  3115. staleness = HostCache::kNotStale;
  3116. }
  3117. if (cache_result) {
  3118. *out_stale_info = std::move(staleness);
  3119. source_net_log.AddEvent(
  3120. NetLogEventType::HOST_RESOLVER_MANAGER_CACHE_HIT,
  3121. [&] { return NetLogResults(cache_result->second); });
  3122. return cache_result->second;
  3123. }
  3124. return absl::nullopt;
  3125. }
  3126. absl::optional<HostCache::Entry> HostResolverManager::MaybeReadFromConfig(
  3127. const JobKey& key) {
  3128. DCHECK(HasAddressType(key.query_types));
  3129. if (!absl::holds_alternative<url::SchemeHostPort>(key.host))
  3130. return absl::nullopt;
  3131. absl::optional<std::vector<IPEndPoint>> preset_addrs =
  3132. dns_client_->GetPresetAddrs(absl::get<url::SchemeHostPort>(key.host));
  3133. if (!preset_addrs)
  3134. return absl::nullopt;
  3135. std::vector<IPEndPoint> filtered_addresses =
  3136. FilterAddresses(std::move(*preset_addrs), key.query_types);
  3137. if (filtered_addresses.empty())
  3138. return absl::nullopt;
  3139. return HostCache::Entry(OK, std::move(filtered_addresses), /*aliases=*/{},
  3140. HostCache::Entry::SOURCE_CONFIG);
  3141. }
  3142. void HostResolverManager::StartBootstrapFollowup(
  3143. JobKey key,
  3144. HostCache* host_cache,
  3145. const NetLogWithSource& source_net_log) {
  3146. DCHECK_EQ(SecureDnsMode::kOff, key.secure_dns_mode);
  3147. DCHECK(host_cache);
  3148. key.secure_dns_mode = SecureDnsMode::kSecure;
  3149. if (jobs_.count(key) != 0)
  3150. return;
  3151. Job* job = AddJobWithoutRequest(
  3152. key, ResolveHostParameters::CacheUsage::ALLOWED, host_cache,
  3153. {TaskType::SECURE_DNS}, RequestPriority::LOW, source_net_log);
  3154. job->RunNextTask();
  3155. }
  3156. absl::optional<HostCache::Entry> HostResolverManager::ServeFromHosts(
  3157. base::StringPiece hostname,
  3158. DnsQueryTypeSet query_types,
  3159. bool default_family_due_to_no_ipv6,
  3160. const std::deque<TaskType>& tasks) {
  3161. DCHECK(!query_types.Has(DnsQueryType::UNSPECIFIED));
  3162. // Don't attempt a HOSTS lookup if there is no DnsConfig or the HOSTS lookup
  3163. // is going to be done next as part of a system lookup.
  3164. if (!dns_client_ || !HasAddressType(query_types) ||
  3165. (!tasks.empty() && tasks.front() == TaskType::PROC))
  3166. return absl::nullopt;
  3167. const DnsHosts* hosts = dns_client_->GetHosts();
  3168. if (!hosts || hosts->empty())
  3169. return absl::nullopt;
  3170. // HOSTS lookups are case-insensitive.
  3171. std::string effective_hostname = base::ToLowerASCII(hostname);
  3172. // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
  3173. // (glibc and c-ares) return the first matching line. We have more
  3174. // flexibility, but lose implicit ordering.
  3175. // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
  3176. // necessary.
  3177. std::vector<IPEndPoint> addresses;
  3178. if (query_types.Has(DnsQueryType::AAAA)) {
  3179. auto it = hosts->find(DnsHostsKey(effective_hostname, ADDRESS_FAMILY_IPV6));
  3180. if (it != hosts->end())
  3181. addresses.push_back(IPEndPoint(it->second, 0));
  3182. }
  3183. if (query_types.Has(DnsQueryType::A)) {
  3184. auto it = hosts->find(DnsHostsKey(effective_hostname, ADDRESS_FAMILY_IPV4));
  3185. if (it != hosts->end())
  3186. addresses.push_back(IPEndPoint(it->second, 0));
  3187. }
  3188. // If got only loopback addresses and the family was restricted, resolve
  3189. // again, without restrictions. See SystemHostResolverCall for rationale.
  3190. if (default_family_due_to_no_ipv6 &&
  3191. base::ranges::all_of(addresses, &IPAddress::IsIPv4,
  3192. &IPEndPoint::address) &&
  3193. base::ranges::all_of(addresses, &IPAddress::IsLoopback,
  3194. &IPEndPoint::address)) {
  3195. query_types.Put(DnsQueryType::AAAA);
  3196. return ServeFromHosts(hostname, query_types, false, tasks);
  3197. }
  3198. if (addresses.empty())
  3199. return absl::nullopt;
  3200. return HostCache::Entry(OK, std::move(addresses),
  3201. /*aliases=*/{}, HostCache::Entry::SOURCE_HOSTS);
  3202. }
  3203. absl::optional<HostCache::Entry> HostResolverManager::ServeLocalhost(
  3204. base::StringPiece hostname,
  3205. DnsQueryTypeSet query_types,
  3206. bool default_family_due_to_no_ipv6) {
  3207. DCHECK(!query_types.Has(DnsQueryType::UNSPECIFIED));
  3208. std::vector<IPEndPoint> resolved_addresses;
  3209. if (!HasAddressType(query_types) ||
  3210. !ResolveLocalHostname(hostname, &resolved_addresses)) {
  3211. return absl::nullopt;
  3212. }
  3213. if (default_family_due_to_no_ipv6 && query_types.Has(DnsQueryType::A) &&
  3214. !query_types.Has(DnsQueryType::AAAA)) {
  3215. // The caller disabled the AAAA query due to lack of detected IPv6 support.
  3216. // (See SystemHostResolverCall for rationale).
  3217. query_types.Put(DnsQueryType::AAAA);
  3218. }
  3219. std::vector<IPEndPoint> filtered_addresses =
  3220. FilterAddresses(std::move(resolved_addresses), query_types);
  3221. return HostCache::Entry(OK, std::move(filtered_addresses), /*aliases=*/{},
  3222. HostCache::Entry::SOURCE_UNKNOWN);
  3223. }
  3224. void HostResolverManager::CacheResult(HostCache* cache,
  3225. const HostCache::Key& key,
  3226. const HostCache::Entry& entry,
  3227. base::TimeDelta ttl) {
  3228. // Don't cache an error unless it has a positive TTL.
  3229. if (cache && (entry.error() == OK || ttl.is_positive()))
  3230. cache->Set(key, entry, tick_clock_->NowTicks(), ttl);
  3231. }
  3232. std::unique_ptr<HostResolverManager::Job> HostResolverManager::RemoveJob(
  3233. JobMap::iterator job_it) {
  3234. DCHECK(job_it != jobs_.end());
  3235. DCHECK(job_it->second);
  3236. DCHECK_EQ(1u, jobs_.count(job_it->first));
  3237. std::unique_ptr<Job> job;
  3238. job_it->second.swap(job);
  3239. jobs_.erase(job_it);
  3240. job->OnRemovedFromJobMap();
  3241. return job;
  3242. }
  3243. SecureDnsMode HostResolverManager::GetEffectiveSecureDnsMode(
  3244. SecureDnsPolicy secure_dns_policy) {
  3245. // Use switch() instead of if() to ensure that all policies are handled.
  3246. switch (secure_dns_policy) {
  3247. case SecureDnsPolicy::kDisable:
  3248. case SecureDnsPolicy::kBootstrap:
  3249. return SecureDnsMode::kOff;
  3250. case SecureDnsPolicy::kAllow:
  3251. break;
  3252. }
  3253. const DnsConfig* config =
  3254. dns_client_ ? dns_client_->GetEffectiveConfig() : nullptr;
  3255. SecureDnsMode secure_dns_mode = SecureDnsMode::kOff;
  3256. if (config) {
  3257. secure_dns_mode = config->secure_dns_mode;
  3258. }
  3259. return secure_dns_mode;
  3260. }
  3261. bool HostResolverManager::ShouldForceSystemResolverDueToTestOverride() const {
  3262. // If tests have provided a catch-all DNS block and then disabled it, check
  3263. // that we are not at risk of sending queries beyond the local network.
  3264. if (HostResolverProc::GetDefault() && system_resolver_disabled_for_testing_) {
  3265. DCHECK(dns_client_);
  3266. DCHECK(dns_client_->GetEffectiveConfig());
  3267. DCHECK(base::ranges::none_of(dns_client_->GetEffectiveConfig()->nameservers,
  3268. &IPAddress::IsPubliclyRoutable,
  3269. &IPEndPoint::address))
  3270. << "Test could query a publicly-routable address.";
  3271. }
  3272. return !proc_params_.resolver_proc && HostResolverProc::GetDefault() &&
  3273. !system_resolver_disabled_for_testing_;
  3274. }
  3275. void HostResolverManager::PushDnsTasks(bool proc_task_allowed,
  3276. SecureDnsMode secure_dns_mode,
  3277. bool insecure_tasks_allowed,
  3278. bool allow_cache,
  3279. bool prioritize_local_lookups,
  3280. ResolveContext* resolve_context,
  3281. std::deque<TaskType>* out_tasks) {
  3282. DCHECK(dns_client_);
  3283. DCHECK(dns_client_->GetEffectiveConfig());
  3284. // If a catch-all DNS block has been set for unit tests, we shouldn't send
  3285. // DnsTasks. It is still necessary to call this method, however, so that the
  3286. // correct cache tasks for the secure dns mode are added.
  3287. const bool dns_tasks_allowed = !ShouldForceSystemResolverDueToTestOverride();
  3288. // Upgrade the insecure DnsTask depending on the secure dns mode.
  3289. switch (secure_dns_mode) {
  3290. case SecureDnsMode::kSecure:
  3291. DCHECK(!allow_cache ||
  3292. out_tasks->front() == TaskType::SECURE_CACHE_LOOKUP);
  3293. // Policy misconfiguration can put us in secure DNS mode without any DoH
  3294. // servers to query. See https://crbug.com/1326526.
  3295. if (dns_tasks_allowed && dns_client_->CanUseSecureDnsTransactions())
  3296. out_tasks->push_back(TaskType::SECURE_DNS);
  3297. break;
  3298. case SecureDnsMode::kAutomatic:
  3299. DCHECK(!allow_cache || out_tasks->front() == TaskType::CACHE_LOOKUP);
  3300. if (dns_client_->FallbackFromSecureTransactionPreferred(
  3301. resolve_context)) {
  3302. // Don't run a secure DnsTask if there are no available DoH servers.
  3303. if (dns_tasks_allowed && insecure_tasks_allowed)
  3304. out_tasks->push_back(TaskType::DNS);
  3305. } else if (prioritize_local_lookups) {
  3306. // If local lookups are prioritized, the cache should be checked for
  3307. // both secure and insecure results prior to running a secure DnsTask.
  3308. // The task sequence should already contain the appropriate cache task.
  3309. if (dns_tasks_allowed) {
  3310. out_tasks->push_back(TaskType::SECURE_DNS);
  3311. if (insecure_tasks_allowed)
  3312. out_tasks->push_back(TaskType::DNS);
  3313. }
  3314. } else {
  3315. if (allow_cache) {
  3316. // Remove the initial cache lookup task so that the secure and
  3317. // insecure lookups can be separated.
  3318. out_tasks->pop_front();
  3319. out_tasks->push_back(TaskType::SECURE_CACHE_LOOKUP);
  3320. }
  3321. if (dns_tasks_allowed)
  3322. out_tasks->push_back(TaskType::SECURE_DNS);
  3323. if (allow_cache)
  3324. out_tasks->push_back(TaskType::INSECURE_CACHE_LOOKUP);
  3325. if (dns_tasks_allowed && insecure_tasks_allowed)
  3326. out_tasks->push_back(TaskType::DNS);
  3327. }
  3328. break;
  3329. case SecureDnsMode::kOff:
  3330. DCHECK(!allow_cache || IsLocalTask(out_tasks->front()));
  3331. if (dns_tasks_allowed && insecure_tasks_allowed)
  3332. out_tasks->push_back(TaskType::DNS);
  3333. break;
  3334. default:
  3335. NOTREACHED();
  3336. break;
  3337. }
  3338. constexpr TaskType kWantTasks[] = {TaskType::DNS, TaskType::SECURE_DNS};
  3339. const bool no_dns_or_secure_tasks =
  3340. base::ranges::find_first_of(*out_tasks, kWantTasks) == out_tasks->end();
  3341. // The system resolver can be used as a fallback for a non-existent or
  3342. // failing DnsTask if allowed by the request parameters.
  3343. if (proc_task_allowed &&
  3344. (no_dns_or_secure_tasks || allow_fallback_to_proctask_))
  3345. out_tasks->push_back(TaskType::PROC);
  3346. }
  3347. void HostResolverManager::CreateTaskSequence(
  3348. const JobKey& job_key,
  3349. ResolveHostParameters::CacheUsage cache_usage,
  3350. SecureDnsPolicy secure_dns_policy,
  3351. std::deque<TaskType>* out_tasks) {
  3352. DCHECK(out_tasks->empty());
  3353. // A cache lookup should generally be performed first. For jobs involving a
  3354. // DnsTask, this task may be replaced.
  3355. bool allow_cache =
  3356. cache_usage != ResolveHostParameters::CacheUsage::DISALLOWED;
  3357. if (secure_dns_policy == SecureDnsPolicy::kBootstrap) {
  3358. DCHECK_EQ(SecureDnsMode::kOff, job_key.secure_dns_mode);
  3359. if (allow_cache)
  3360. out_tasks->push_front(TaskType::INSECURE_CACHE_LOOKUP);
  3361. out_tasks->push_front(TaskType::CONFIG_PRESET);
  3362. if (allow_cache)
  3363. out_tasks->push_front(TaskType::SECURE_CACHE_LOOKUP);
  3364. } else if (allow_cache) {
  3365. if (job_key.secure_dns_mode == SecureDnsMode::kSecure) {
  3366. out_tasks->push_front(TaskType::SECURE_CACHE_LOOKUP);
  3367. } else {
  3368. out_tasks->push_front(TaskType::CACHE_LOOKUP);
  3369. }
  3370. }
  3371. // Determine what type of task a future Job should start.
  3372. bool prioritize_local_lookups =
  3373. cache_usage ==
  3374. HostResolver::ResolveHostParameters::CacheUsage::STALE_ALLOWED;
  3375. const bool has_address_type = HasAddressType(job_key.query_types);
  3376. switch (job_key.source) {
  3377. case HostResolverSource::ANY:
  3378. // Force address queries with canonname to use ProcTask to counter poor
  3379. // CNAME support in DnsTask. See https://crbug.com/872665
  3380. //
  3381. // Otherwise, default to DnsTask (with allowed fallback to ProcTask for
  3382. // address queries). But if hostname appears to be an MDNS name (ends in
  3383. // *.local), go with ProcTask for address queries and MdnsTask for non-
  3384. // address queries.
  3385. if ((job_key.flags & HOST_RESOLVER_CANONNAME) && has_address_type) {
  3386. out_tasks->push_back(TaskType::PROC);
  3387. } else if (!ResemblesMulticastDNSName(GetHostname(job_key.host))) {
  3388. bool proc_task_allowed = has_address_type && job_key.secure_dns_mode !=
  3389. SecureDnsMode::kSecure;
  3390. if (dns_client_ && dns_client_->GetEffectiveConfig()) {
  3391. bool insecure_allowed =
  3392. dns_client_->CanUseInsecureDnsTransactions() &&
  3393. !dns_client_->FallbackFromInsecureTransactionPreferred() &&
  3394. (has_address_type ||
  3395. dns_client_->CanQueryAdditionalTypesViaInsecureDns());
  3396. PushDnsTasks(proc_task_allowed, job_key.secure_dns_mode,
  3397. insecure_allowed, allow_cache, prioritize_local_lookups,
  3398. &*job_key.resolve_context, out_tasks);
  3399. } else if (proc_task_allowed) {
  3400. out_tasks->push_back(TaskType::PROC);
  3401. }
  3402. } else if (has_address_type) {
  3403. // For *.local address queries, try the system resolver even if the
  3404. // secure dns mode is SECURE. Public recursive resolvers aren't expected
  3405. // to handle these queries.
  3406. out_tasks->push_back(TaskType::PROC);
  3407. } else {
  3408. out_tasks->push_back(TaskType::MDNS);
  3409. }
  3410. break;
  3411. case HostResolverSource::SYSTEM:
  3412. out_tasks->push_back(TaskType::PROC);
  3413. break;
  3414. case HostResolverSource::DNS:
  3415. if (dns_client_ && dns_client_->GetEffectiveConfig()) {
  3416. bool insecure_allowed =
  3417. dns_client_->CanUseInsecureDnsTransactions() &&
  3418. (has_address_type ||
  3419. dns_client_->CanQueryAdditionalTypesViaInsecureDns());
  3420. PushDnsTasks(false /* proc_task_allowed */, job_key.secure_dns_mode,
  3421. insecure_allowed, allow_cache, prioritize_local_lookups,
  3422. &*job_key.resolve_context, out_tasks);
  3423. }
  3424. break;
  3425. case HostResolverSource::MULTICAST_DNS:
  3426. out_tasks->push_back(TaskType::MDNS);
  3427. break;
  3428. case HostResolverSource::LOCAL_ONLY:
  3429. // If no external source allowed, a job should not be created or started
  3430. break;
  3431. }
  3432. // `HOST_RESOLVER_CANONNAME` is only supported through system resolution.
  3433. if (job_key.flags & HOST_RESOLVER_CANONNAME) {
  3434. DCHECK(base::ranges::find(*out_tasks, TaskType::DNS) == out_tasks->end());
  3435. DCHECK(base::ranges::find(*out_tasks, TaskType::MDNS) == out_tasks->end());
  3436. }
  3437. }
  3438. void HostResolverManager::GetEffectiveParametersForRequest(
  3439. const absl::variant<url::SchemeHostPort, std::string>& host,
  3440. DnsQueryType dns_query_type,
  3441. HostResolverFlags flags,
  3442. SecureDnsPolicy secure_dns_policy,
  3443. bool is_ip,
  3444. const NetLogWithSource& net_log,
  3445. DnsQueryTypeSet* out_effective_types,
  3446. HostResolverFlags* out_effective_flags,
  3447. SecureDnsMode* out_effective_secure_dns_mode) {
  3448. const SecureDnsMode secure_dns_mode =
  3449. GetEffectiveSecureDnsMode(secure_dns_policy);
  3450. *out_effective_secure_dns_mode = secure_dns_mode;
  3451. *out_effective_flags = flags | additional_resolver_flags_;
  3452. if (dns_query_type != DnsQueryType::UNSPECIFIED) {
  3453. *out_effective_types = dns_query_type;
  3454. return;
  3455. }
  3456. DnsQueryTypeSet effective_types(DnsQueryType::A, DnsQueryType::AAAA);
  3457. // Disable AAAA queries when we cannot do anything with the results.
  3458. bool use_local_ipv6 = true;
  3459. if (dns_client_) {
  3460. const DnsConfig* config = dns_client_->GetEffectiveConfig();
  3461. if (config)
  3462. use_local_ipv6 = config->use_local_ipv6;
  3463. }
  3464. // When resolving IPv4 literals, there's no need to probe for IPv6. When
  3465. // resolving IPv6 literals, there's no benefit to artificially limiting our
  3466. // resolution based on a probe. Prior logic ensures that this is an automatic
  3467. // query, so the code requesting the resolution should be amenable to
  3468. // receiving an IPv6 resolution.
  3469. if (!use_local_ipv6 && !is_ip && !IsIPv6Reachable(net_log)) {
  3470. *out_effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
  3471. effective_types.Remove(DnsQueryType::AAAA);
  3472. }
  3473. // Optimistically enable feature-controlled queries. These queries may be
  3474. // skipped at a later point.
  3475. // `https_svcb_options_.enable` has precedence, so if enabled, ignore any
  3476. // other related features.
  3477. if (https_svcb_options_.enable) {
  3478. static const char* const kSchemesForHttpsQuery[] = {
  3479. url::kHttpScheme, url::kHttpsScheme, url::kWsScheme, url::kWssScheme};
  3480. if (base::Contains(kSchemesForHttpsQuery, GetScheme(host)))
  3481. effective_types.Put(DnsQueryType::HTTPS);
  3482. } else if (base::FeatureList::IsEnabled(features::kDnsHttpssvc) &&
  3483. (httpssvc_domain_cache_.IsExperimental(GetHostname(host)) ||
  3484. httpssvc_domain_cache_.IsControl(GetHostname(host)))) {
  3485. if (features::kDnsHttpssvcUseIntegrity.Get())
  3486. effective_types.Put(DnsQueryType::INTEGRITY);
  3487. if (features::kDnsHttpssvcUseHttpssvc.Get())
  3488. effective_types.Put(DnsQueryType::HTTPS_EXPERIMENTAL);
  3489. }
  3490. *out_effective_types = effective_types;
  3491. }
  3492. namespace {
  3493. bool RequestWillUseWiFi(handles::NetworkHandle network) {
  3494. NetworkChangeNotifier::ConnectionType connection_type;
  3495. if (network == handles::kInvalidNetworkHandle)
  3496. connection_type = NetworkChangeNotifier::GetConnectionType();
  3497. else
  3498. connection_type = NetworkChangeNotifier::GetNetworkConnectionType(network);
  3499. return connection_type == NetworkChangeNotifier::CONNECTION_WIFI;
  3500. }
  3501. } // namespace
  3502. bool HostResolverManager::IsIPv6Reachable(const NetLogWithSource& net_log) {
  3503. // Don't bother checking if the request will use WiFi and IPv6 is assumed to
  3504. // not work on WiFi.
  3505. if (!check_ipv6_on_wifi_ && RequestWillUseWiFi(target_network_))
  3506. return false;
  3507. // Cache the result for kIPv6ProbePeriodMs (measured from after
  3508. // IsGloballyReachable() completes).
  3509. bool cached = true;
  3510. if (last_ipv6_probe_time_.is_null() ||
  3511. (tick_clock_->NowTicks() - last_ipv6_probe_time_).InMilliseconds() >
  3512. kIPv6ProbePeriodMs) {
  3513. SetLastIPv6ProbeResult(
  3514. IsGloballyReachable(IPAddress(kIPv6ProbeAddress), net_log));
  3515. cached = false;
  3516. }
  3517. net_log.AddEvent(
  3518. NetLogEventType::HOST_RESOLVER_MANAGER_IPV6_REACHABILITY_CHECK, [&] {
  3519. return NetLogIPv6AvailableParams(last_ipv6_probe_result_, cached);
  3520. });
  3521. return last_ipv6_probe_result_;
  3522. }
  3523. void HostResolverManager::SetLastIPv6ProbeResult(bool last_ipv6_probe_result) {
  3524. last_ipv6_probe_result_ = last_ipv6_probe_result;
  3525. last_ipv6_probe_time_ = tick_clock_->NowTicks();
  3526. }
  3527. bool HostResolverManager::IsGloballyReachable(const IPAddress& dest,
  3528. const NetLogWithSource& net_log) {
  3529. std::unique_ptr<DatagramClientSocket> socket(
  3530. ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket(
  3531. DatagramSocket::DEFAULT_BIND, net_log.net_log(), net_log.source()));
  3532. int rv = socket->Connect(IPEndPoint(dest, 53));
  3533. if (rv != OK)
  3534. return false;
  3535. IPEndPoint endpoint;
  3536. rv = socket->GetLocalAddress(&endpoint);
  3537. if (rv != OK)
  3538. return false;
  3539. DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily());
  3540. const IPAddress& address = endpoint.address();
  3541. bool is_link_local =
  3542. (address.bytes()[0] == 0xFE) && ((address.bytes()[1] & 0xC0) == 0x80);
  3543. if (is_link_local)
  3544. return false;
  3545. const uint8_t kTeredoPrefix[] = {0x20, 0x01, 0, 0};
  3546. if (IPAddressStartsWith(address, kTeredoPrefix))
  3547. return false;
  3548. return true;
  3549. }
  3550. void HostResolverManager::RunLoopbackProbeJob() {
  3551. // Run this asynchronously as it can take 40-100ms and should not block
  3552. // initialization.
  3553. base::ThreadPool::PostTaskAndReplyWithResult(
  3554. FROM_HERE,
  3555. {base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
  3556. base::BindOnce(&HaveOnlyLoopbackAddresses),
  3557. base::BindOnce(&HostResolverManager::SetHaveOnlyLoopbackAddresses,
  3558. weak_ptr_factory_.GetWeakPtr()));
  3559. }
  3560. void HostResolverManager::RemoveAllJobs(const ResolveContext* context) {
  3561. for (auto it = jobs_.begin(); it != jobs_.end();) {
  3562. const JobKey& key = it->first;
  3563. if (&*key.resolve_context == context) {
  3564. RemoveJob(it++);
  3565. } else {
  3566. ++it;
  3567. }
  3568. }
  3569. }
  3570. void HostResolverManager::AbortJobsWithoutTargetNetwork(bool in_progress_only) {
  3571. // In Abort, a Request callback could spawn new Jobs with matching keys, so
  3572. // first collect and remove all running jobs from `jobs_`.
  3573. std::vector<std::unique_ptr<Job>> jobs_to_abort;
  3574. for (auto it = jobs_.begin(); it != jobs_.end();) {
  3575. Job* job = it->second.get();
  3576. if (!job->HasTargetNetwork() && (!in_progress_only || job->is_running())) {
  3577. jobs_to_abort.push_back(RemoveJob(it++));
  3578. } else {
  3579. ++it;
  3580. }
  3581. }
  3582. // Pause the dispatcher so it won't start any new dispatcher jobs while
  3583. // aborting the old ones. This is needed so that it won't start the second
  3584. // DnsTransaction for a job in `jobs_to_abort` if the DnsConfig just became
  3585. // invalid.
  3586. PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
  3587. dispatcher_->SetLimits(
  3588. PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
  3589. // Life check to bail once `this` is deleted.
  3590. base::WeakPtr<HostResolverManager> self = weak_ptr_factory_.GetWeakPtr();
  3591. // Then Abort them.
  3592. for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) {
  3593. jobs_to_abort[i]->Abort();
  3594. }
  3595. if (self)
  3596. dispatcher_->SetLimits(limits);
  3597. }
  3598. void HostResolverManager::AbortInsecureDnsTasks(int error, bool fallback_only) {
  3599. // Aborting jobs potentially modifies |jobs_| and may even delete some jobs.
  3600. // Create safe closures of all current jobs.
  3601. std::vector<base::OnceClosure> job_abort_closures;
  3602. for (auto& job : jobs_) {
  3603. job_abort_closures.push_back(
  3604. job.second->GetAbortInsecureDnsTaskClosure(error, fallback_only));
  3605. }
  3606. // Pause the dispatcher so it won't start any new dispatcher jobs while
  3607. // aborting the old ones. This is needed so that it won't start the second
  3608. // DnsTransaction for a job if the DnsConfig just changed.
  3609. PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
  3610. dispatcher_->SetLimits(
  3611. PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
  3612. for (base::OnceClosure& closure : job_abort_closures)
  3613. std::move(closure).Run();
  3614. dispatcher_->SetLimits(limits);
  3615. }
  3616. // TODO(crbug.com/995984): Consider removing this and its usage.
  3617. void HostResolverManager::TryServingAllJobsFromHosts() {
  3618. if (!dns_client_ || !dns_client_->GetEffectiveConfig())
  3619. return;
  3620. // TODO(szym): Do not do this if nsswitch.conf instructs not to.
  3621. // http://crbug.com/117655
  3622. // Life check to bail once |this| is deleted.
  3623. base::WeakPtr<HostResolverManager> self = weak_ptr_factory_.GetWeakPtr();
  3624. for (auto it = jobs_.begin(); self.get() && it != jobs_.end();) {
  3625. Job* job = it->second.get();
  3626. ++it;
  3627. // This could remove |job| from |jobs_|, but iterator will remain valid.
  3628. job->ServeFromHosts();
  3629. }
  3630. }
  3631. void HostResolverManager::OnIPAddressChanged() {
  3632. DCHECK(!IsBoundToNetwork());
  3633. last_ipv6_probe_time_ = base::TimeTicks();
  3634. // Abandon all ProbeJobs.
  3635. probe_weak_ptr_factory_.InvalidateWeakPtrs();
  3636. InvalidateCaches();
  3637. #if (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_ANDROID)) || \
  3638. BUILDFLAG(IS_FUCHSIA)
  3639. RunLoopbackProbeJob();
  3640. #endif
  3641. AbortJobsWithoutTargetNetwork(true /* in_progress_only */);
  3642. // `this` may be deleted inside AbortJobsWithoutTargetNetwork().
  3643. }
  3644. void HostResolverManager::OnConnectionTypeChanged(
  3645. NetworkChangeNotifier::ConnectionType type) {
  3646. DCHECK(!IsBoundToNetwork());
  3647. UpdateConnectionType(type);
  3648. }
  3649. void HostResolverManager::OnSystemDnsConfigChanged(
  3650. absl::optional<DnsConfig> config) {
  3651. DCHECK(!IsBoundToNetwork());
  3652. // If tests have provided a catch-all DNS block and then disabled it, check
  3653. // that we are not at risk of sending queries beyond the local network.
  3654. if (HostResolverProc::GetDefault() && system_resolver_disabled_for_testing_ &&
  3655. config.has_value()) {
  3656. DCHECK(base::ranges::none_of(config->nameservers,
  3657. &IPAddress::IsPubliclyRoutable,
  3658. &IPEndPoint::address))
  3659. << "Test could query a publicly-routable address.";
  3660. }
  3661. bool changed = false;
  3662. bool transactions_allowed_before = false;
  3663. if (dns_client_) {
  3664. transactions_allowed_before = dns_client_->CanUseSecureDnsTransactions() ||
  3665. dns_client_->CanUseInsecureDnsTransactions();
  3666. changed = dns_client_->SetSystemConfig(std::move(config));
  3667. }
  3668. // Always invalidate cache, even if no change is seen.
  3669. InvalidateCaches();
  3670. if (changed) {
  3671. // Need to update jobs iff transactions were previously allowed because
  3672. // in-progress jobs may be running using a now-invalid configuration.
  3673. if (transactions_allowed_before)
  3674. UpdateJobsForChangedConfig();
  3675. }
  3676. }
  3677. void HostResolverManager::UpdateJobsForChangedConfig() {
  3678. // Life check to bail once `this` is deleted.
  3679. base::WeakPtr<HostResolverManager> self = weak_ptr_factory_.GetWeakPtr();
  3680. // Existing jobs that were set up using the nameservers and secure dns mode
  3681. // from the original config need to be aborted (does not apply to jobs
  3682. // targeting a specific network).
  3683. AbortJobsWithoutTargetNetwork(false /* in_progress_only */);
  3684. // `this` may be deleted inside AbortJobsWithoutTargetNetwork().
  3685. if (self.get())
  3686. TryServingAllJobsFromHosts();
  3687. }
  3688. void HostResolverManager::OnFallbackResolve(int dns_task_error) {
  3689. DCHECK(dns_client_);
  3690. DCHECK_NE(OK, dns_task_error);
  3691. // Nothing to do if DnsTask is already not preferred.
  3692. if (dns_client_->FallbackFromInsecureTransactionPreferred())
  3693. return;
  3694. dns_client_->IncrementInsecureFallbackFailures();
  3695. // If DnsClient became not preferred, fallback all fallback-allowed insecure
  3696. // DnsTasks to ProcTasks.
  3697. if (dns_client_->FallbackFromInsecureTransactionPreferred())
  3698. AbortInsecureDnsTasks(ERR_FAILED, true /* fallback_only */);
  3699. }
  3700. int HostResolverManager::GetOrCreateMdnsClient(MDnsClient** out_client) {
  3701. #if BUILDFLAG(ENABLE_MDNS)
  3702. if (!mdns_client_) {
  3703. if (!mdns_socket_factory_)
  3704. mdns_socket_factory_ = std::make_unique<MDnsSocketFactoryImpl>(net_log_);
  3705. mdns_client_ = MDnsClient::CreateDefault();
  3706. }
  3707. int rv = OK;
  3708. if (!mdns_client_->IsListening())
  3709. rv = mdns_client_->StartListening(mdns_socket_factory_.get());
  3710. DCHECK_NE(ERR_IO_PENDING, rv);
  3711. DCHECK(rv != OK || mdns_client_->IsListening());
  3712. if (rv == OK)
  3713. *out_client = mdns_client_.get();
  3714. return rv;
  3715. #else
  3716. // Should not request MDNS resoltuion unless MDNS is enabled.
  3717. NOTREACHED();
  3718. return ERR_UNEXPECTED;
  3719. #endif
  3720. }
  3721. void HostResolverManager::InvalidateCaches(bool network_change) {
  3722. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  3723. DCHECK(!invalidation_in_progress_);
  3724. #if DCHECK_IS_ON()
  3725. base::WeakPtr<HostResolverManager> self_ptr = weak_ptr_factory_.GetWeakPtr();
  3726. size_t num_jobs = jobs_.size();
  3727. #endif
  3728. invalidation_in_progress_ = true;
  3729. for (auto& context : registered_contexts_) {
  3730. context.InvalidateCachesAndPerSessionData(
  3731. dns_client_ ? dns_client_->GetCurrentSession() : nullptr,
  3732. network_change);
  3733. }
  3734. invalidation_in_progress_ = false;
  3735. #if DCHECK_IS_ON()
  3736. // Sanity checks that invalidation does not have reentrancy issues.
  3737. DCHECK(self_ptr);
  3738. DCHECK_EQ(num_jobs, jobs_.size());
  3739. #endif
  3740. }
  3741. void HostResolverManager::UpdateConnectionType(
  3742. NetworkChangeNotifier::ConnectionType type) {
  3743. proc_params_.unresponsive_delay =
  3744. GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault(
  3745. "DnsUnresponsiveDelayMsByConnectionType",
  3746. ProcTaskParams::kDnsDefaultUnresponsiveDelay, type);
  3747. // Note that NetworkChangeNotifier always sends a CONNECTION_NONE notification
  3748. // before non-NONE notifications. This check therefore just ensures each
  3749. // connection change notification is handled once and has nothing to do with
  3750. // whether the change is to offline or online.
  3751. if (type == NetworkChangeNotifier::CONNECTION_NONE && dns_client_) {
  3752. dns_client_->ReplaceCurrentSession();
  3753. InvalidateCaches(true /* network_change */);
  3754. }
  3755. }
  3756. std::unique_ptr<DnsProbeRunner> HostResolverManager::CreateDohProbeRunner(
  3757. ResolveContext* resolve_context) {
  3758. DCHECK(resolve_context);
  3759. DCHECK(registered_contexts_.HasObserver(resolve_context));
  3760. if (!dns_client_->CanUseSecureDnsTransactions())
  3761. return nullptr;
  3762. return dns_client_->GetTransactionFactory()->CreateDohProbeRunner(
  3763. resolve_context);
  3764. }
  3765. HostResolverManager::RequestImpl::~RequestImpl() {
  3766. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  3767. if (!job_.has_value())
  3768. return;
  3769. job_.value()->CancelRequest(this);
  3770. LogCancelRequest();
  3771. }
  3772. void HostResolverManager::RequestImpl::ChangeRequestPriority(
  3773. RequestPriority priority) {
  3774. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  3775. CHECK(job_.has_value());
  3776. job_.value()->ChangeRequestPriority(this, priority);
  3777. }
  3778. const HostResolverManager::JobKey& HostResolverManager::RequestImpl::GetJobKey()
  3779. const {
  3780. CHECK(job_.has_value());
  3781. return job_.value()->key();
  3782. }
  3783. void HostResolverManager::RequestImpl::OnJobCancelled(const JobKey& job_key) {
  3784. CHECK(job_.has_value());
  3785. CHECK(job_key == job_.value()->key());
  3786. job_.reset();
  3787. DCHECK(!complete_);
  3788. DCHECK(callback_);
  3789. callback_.Reset();
  3790. // No results should be set.
  3791. DCHECK(!results_);
  3792. LogCancelRequest();
  3793. }
  3794. void HostResolverManager::RequestImpl::OnJobCompleted(
  3795. const JobKey& job_key,
  3796. int error,
  3797. bool is_secure_network_error) {
  3798. set_error_info(error, is_secure_network_error);
  3799. CHECK(job_.has_value());
  3800. CHECK(job_key == job_.value()->key());
  3801. job_.reset();
  3802. DCHECK(!complete_);
  3803. complete_ = true;
  3804. LogFinishRequest(error, true /* async_completion */);
  3805. DCHECK(callback_);
  3806. std::move(callback_).Run(HostResolver::SquashErrorCode(error));
  3807. }
  3808. } // namespace net