dns_cache.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883
  1. /*
  2. * $Id$
  3. *
  4. * resolver related functions
  5. *
  6. * Copyright (C) 2006 iptelorg GmbH
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. /* History:
  30. * --------
  31. * 2006-07-13 created by andrei
  32. * 2006-10-06 port fix (andrei)
  33. * 2007-06-14 dns iterate through A & AAAA records fix (andrei)
  34. * 2007-06-15 srv rr weight based load balancing support (andrei)
  35. * 2007-06-16 naptr support (andrei)
  36. * 2008-07-18 DNS watchdog support -- can be used to inform the core
  37. * that the DNS servers are down (Miklos)
  38. * 2008-07-25 various rpc commands to manipulate the content
  39. * of the cache (Miklos)
  40. * 2007-07-30 DNS cache measurements added (Gergo)
  41. * 2007-08-17 dns_cache_del_nonexp config option is introduced (Miklos)
  42. * 2008-02-04 DNS cache options are adapted for the configuration
  43. * framework (Miklos)
  44. * 2008-02-11 dns_cache_init cfg parameter is introduced (Miklos)
  45. * 2008-10-17 fixed srv continue with 0 hostname (when falling back to
  46. aaaa) (andrei)
  47. * 2009-03-30 TXT record support, more rpcs (andrei)
  48. * 2009-03-30 EBL record support (andrei)
  49. * 2009-04-01 PTR record support (andrei)
  50. */
  51. /*!
  52. * \file
  53. * \brief SIP-router core ::
  54. * \ingroup core
  55. * Module: \ref core
  56. */
  57. #ifdef USE_DNS_CACHE
  58. #ifdef DNS_SRV_LB
  59. #include <stdlib.h> /* FIXME: rand() */
  60. #endif
  61. #include <string.h>
  62. #include "globals.h"
  63. #include "cfg_core.h"
  64. #include "dns_cache.h"
  65. #include "dns_wrappers.h"
  66. #include "compiler_opt.h"
  67. #include "mem/shm_mem.h"
  68. #include "hashes.h"
  69. #include "clist.h"
  70. #include "locking.h"
  71. #include "atomic_ops.h"
  72. #include "ut.h"
  73. #include "timer.h"
  74. #include "timer_ticks.h"
  75. #include "error.h"
  76. #include "rpc.h"
  77. #include "rand/fastrand.h"
  78. #ifdef USE_DNS_CACHE_STATS
  79. #include "pt.h"
  80. #endif
  81. #define DNS_CACHE_DEBUG /* extra sanity checks and debugging */
  82. #ifndef MAX
  83. #define MAX(a,b) ( ((a)>(b))?(a):(b))
  84. #endif
  85. #define MAX_DNS_RECORDS 255 /* maximum dns records number received in a
  86. dns answer*/
  87. #define DNS_HASH_SIZE 1024 /* must be <= 65535 */
  88. #define DEFAULT_DNS_TIMER_INTERVAL 120 /* 2 min. */
  89. #define DNS_HE_MAX_ADDR 10 /* maxium addresses returne in a hostent struct */
  90. #define MAX_CNAME_CHAIN 10
  91. #define SPACE_FORMAT " " /* format of view output */
  92. #define DNS_SRV_ZERO_W_CHANCE 1000 /* one in a 1000*weight_sum chance for
  93. selecting a 0-weight record */
  94. int dns_cache_init=1; /* if 0, the DNS cache is not initialized at startup */
  95. static gen_lock_t* dns_hash_lock=0;
  96. static volatile unsigned int *dns_cache_mem_used=0; /* current mem. use */
  97. unsigned int dns_timer_interval=DEFAULT_DNS_TIMER_INTERVAL; /* in s */
  98. int dns_flags=0; /* default flags used for the dns_*resolvehost
  99. (compatibility wrappers) */
  100. #ifdef USE_DNS_CACHE_STATS
  101. struct t_dns_cache_stats* dns_cache_stats=0;
  102. #endif
  103. #define LOCK_DNS_HASH() lock_get(dns_hash_lock)
  104. #define UNLOCK_DNS_HASH() lock_release(dns_hash_lock)
  105. #define FIX_TTL(t) \
  106. (((t)<cfg_get(core, core_cfg, dns_cache_min_ttl))? \
  107. cfg_get(core, core_cfg, dns_cache_min_ttl): \
  108. (((t)>cfg_get(core, core_cfg, dns_cache_max_ttl))? \
  109. cfg_get(core, core_cfg, dns_cache_max_ttl): \
  110. (t)))
  111. struct dns_hash_head{
  112. struct dns_hash_entry* next;
  113. struct dns_hash_entry* prev;
  114. };
  115. #ifdef DNS_LU_LST
  116. struct dns_lu_lst* dns_last_used_lst=0;
  117. #endif
  118. static struct dns_hash_head* dns_hash=0;
  119. static struct timer_ln* dns_timer_h=0;
  120. #ifdef DNS_WATCHDOG_SUPPORT
  121. static atomic_t *dns_servers_up = NULL;
  122. #endif
  123. static const char* dns_str_errors[]={
  124. "no error",
  125. "no more records", /* not an error, but and end condition */
  126. "unknown error",
  127. "internal error",
  128. "bad SRV entry",
  129. "unresolvable SRV request",
  130. "bad A or AAAA entry",
  131. "unresolvable A or AAAA request",
  132. "invalid ip in A or AAAA record",
  133. "blacklisted ip",
  134. "name too long ", /* try again with a shorter name */
  135. "ip AF mismatch", /* address family mismatch */
  136. "unresolvable NAPTR request",
  137. "bug - critical error"
  138. };
  139. /* param: err (negative error number) */
  140. const char* dns_strerror(int err)
  141. {
  142. err=-err;
  143. if ((err>=0) && (err<sizeof(dns_str_errors)/sizeof(char*)))
  144. return dns_str_errors[err];
  145. return "bug -- bad error number";
  146. }
  147. /* "internal" only, don't use unless you really know waht you're doing */
  148. inline static void dns_destroy_entry(struct dns_hash_entry* e)
  149. {
  150. #ifdef DNS_CACHE_DEBUG
  151. memset(e, 0, e->total_size);
  152. #endif
  153. shm_free(e); /* nice having it in one block isn't it? :-) */
  154. }
  155. /* "internal" only, same as above, asumes shm_lock() held (tm optimization) */
  156. inline static void dns_destroy_entry_shm_unsafe(struct dns_hash_entry* e)
  157. {
  158. #ifdef DNS_CACHE_DEBUG
  159. memset(e, 0, e->total_size);
  160. #endif
  161. shm_free_unsafe(e); /* nice having it in one block isn't it? :-) */
  162. }
  163. /* dec. the internal refcnt and if 0 deletes the entry */
  164. void dns_hash_put(struct dns_hash_entry* e)
  165. {
  166. if(e && atomic_dec_and_test(&e->refcnt)){
  167. /* atomic_sub_long(dns_cache_total_used, e->total_size); */
  168. dns_destroy_entry(e);
  169. }
  170. }
  171. /* same as above but uses dns_destroy_unsafe (assumes shm_lock held -- tm
  172. * optimization) */
  173. void dns_hash_put_shm_unsafe(struct dns_hash_entry* e)
  174. {
  175. if(e && atomic_dec_and_test(&e->refcnt)){
  176. /* atomic_sub_long(dns_cache_total_used, e->total_size); */
  177. dns_destroy_entry_shm_unsafe(e);
  178. }
  179. }
  180. inline static int dns_cache_clean(unsigned int no, int expired_only);
  181. inline static int dns_cache_free_mem(unsigned int target, int expired_only);
  182. static ticks_t dns_timer(ticks_t ticks, struct timer_ln* tl, void* data)
  183. {
  184. #ifdef DNS_WATCHDOG_SUPPORT
  185. /* do not clean the hash table if the servers are down */
  186. if (atomic_get(dns_servers_up) == 0)
  187. return (ticks_t)(-1);
  188. #endif
  189. if (*dns_cache_mem_used>12*(cfg_get(core, core_cfg, dns_cache_max_mem)/16)){ /* ~ 75% used */
  190. dns_cache_free_mem(cfg_get(core, core_cfg, dns_cache_max_mem)/2, 1);
  191. }else{
  192. dns_cache_clean(-1, 1); /* all the table, only expired entries */
  193. /* TODO: better strategy? */
  194. }
  195. return (ticks_t)(-1);
  196. }
  197. void destroy_dns_cache()
  198. {
  199. if (dns_timer_h){
  200. timer_del(dns_timer_h);
  201. timer_free(dns_timer_h);
  202. dns_timer_h=0;
  203. }
  204. #ifdef DNS_WATCHDOG_SUPPORT
  205. if (dns_servers_up){
  206. shm_free(dns_servers_up);
  207. dns_servers_up=0;
  208. }
  209. #endif
  210. if (dns_hash_lock){
  211. lock_destroy(dns_hash_lock);
  212. lock_dealloc(dns_hash_lock);
  213. dns_hash_lock=0;
  214. }
  215. if (dns_hash){
  216. shm_free(dns_hash);
  217. dns_hash=0;
  218. }
  219. #ifdef DNS_LU_LST
  220. if (dns_last_used_lst){
  221. shm_free(dns_last_used_lst);
  222. dns_last_used_lst=0;
  223. }
  224. #endif
  225. #ifdef USE_DNS_CACHE_STATS
  226. if (dns_cache_stats)
  227. shm_free(dns_cache_stats);
  228. #endif
  229. if (dns_cache_mem_used){
  230. shm_free((void*)dns_cache_mem_used);
  231. dns_cache_mem_used=0;
  232. }
  233. }
  234. /* set the value of dns_flags */
  235. void fix_dns_flags(str *gname, str *name)
  236. {
  237. /* restore the original value of dns_cache_flags first
  238. * (DNS_IPV4_ONLY may have been set only because dns_try_ipv6
  239. * was disabled, and the flag must be cleared when
  240. * dns_try_ipv6 is enabled) (Miklos)
  241. */
  242. dns_flags = cfg_get(core, core_cfg, dns_cache_flags) & 7;
  243. if (cfg_get(core, core_cfg, dns_try_ipv6)==0){
  244. dns_flags|=DNS_IPV4_ONLY;
  245. }
  246. if (dns_flags & DNS_IPV4_ONLY){
  247. dns_flags&=~(DNS_IPV6_ONLY|DNS_IPV6_FIRST);
  248. }
  249. if (cfg_get(core, core_cfg, dns_srv_lb)){
  250. #ifdef DNS_SRV_LB
  251. dns_flags|=DNS_SRV_RR_LB;
  252. #else
  253. LOG(L_WARN, "WARNING: fix_dns_flags: SRV loadbalaning is set, but"
  254. " support for it is not compiled -- ignoring\n");
  255. #endif
  256. }
  257. if (cfg_get(core, core_cfg, dns_try_naptr)) {
  258. #ifndef USE_NAPTR
  259. LOG(L_WARN, "WARNING: fix_dns_flags: NAPTR support is enabled, but"
  260. " support for it is not compiled -- ignoring\n");
  261. #endif
  262. dns_flags|=DNS_TRY_NAPTR;
  263. }
  264. }
  265. /* fixup function for use_dns_failover
  266. * verifies that use_dns_cache is set to 1
  267. */
  268. int use_dns_failover_fixup(void *handle, str *gname, str *name, void **val)
  269. {
  270. if ((int)(long)(*val) && !cfg_get(core, handle, use_dns_cache)) {
  271. LOG(L_ERR, "ERROR: use_dns_failover_fixup(): "
  272. "DNS cache is turned off, failover cannot be enabled. "
  273. "(set use_dns_cache to 1)\n");
  274. return -1;
  275. }
  276. return 0;
  277. }
  278. /* fixup function for use_dns_cache
  279. * verifies that dns_cache_init is set to 1
  280. */
  281. int use_dns_cache_fixup(void *handle, str *gname, str *name, void **val)
  282. {
  283. if ((int)(long)(*val) && !dns_cache_init) {
  284. LOG(L_ERR, "ERROR: use_dns_cache_fixup(): "
  285. "DNS cache is turned off by dns_cache_init=0, "
  286. "it cannot be enabled runtime.\n");
  287. return -1;
  288. }
  289. if (((int)(long)(*val)==0) && cfg_get(core, handle, use_dns_failover)) {
  290. LOG(L_ERR, "ERROR: use_dns_failover_fixup(): "
  291. "DNS failover depends on use_dns_cache, set use_dns_failover "
  292. "to 0 before disabling the DNS cache\n");
  293. return -1;
  294. }
  295. return 0;
  296. }
  297. /* KByte to Byte conversion */
  298. int dns_cache_max_mem_fixup(void *handle, str *gname, str *name, void **val)
  299. {
  300. unsigned int u;
  301. u = ((unsigned int)(long)(*val))<<10;
  302. (*val) = (void *)(long)u;
  303. return 0;
  304. }
  305. int init_dns_cache()
  306. {
  307. int r;
  308. int ret;
  309. if (dns_cache_init==0) {
  310. /* the DNS cache is turned off */
  311. default_core_cfg.use_dns_cache=0;
  312. default_core_cfg.use_dns_failover=0;
  313. return 0;
  314. }
  315. ret=0;
  316. /* sanity check */
  317. if (E_DNS_CRITICAL>=sizeof(dns_str_errors)/sizeof(char*)){
  318. LOG(L_CRIT, "BUG: dns_cache_init: bad dns error table\n");
  319. ret=E_BUG;
  320. goto error;
  321. }
  322. dns_cache_mem_used=shm_malloc(sizeof(*dns_cache_mem_used));
  323. if (dns_cache_mem_used==0){
  324. ret=E_OUT_OF_MEM;
  325. goto error;
  326. }
  327. #ifdef DNS_LU_LST
  328. dns_last_used_lst=shm_malloc(sizeof(*dns_last_used_lst));
  329. if (dns_last_used_lst==0){
  330. ret=E_OUT_OF_MEM;
  331. goto error;
  332. }
  333. clist_init(dns_last_used_lst, next, prev);
  334. #endif
  335. dns_hash=shm_malloc(sizeof(struct dns_hash_head)*DNS_HASH_SIZE);
  336. if (dns_hash==0){
  337. ret=E_OUT_OF_MEM;
  338. goto error;
  339. }
  340. for (r=0; r<DNS_HASH_SIZE; r++)
  341. clist_init(&dns_hash[r], next, prev);
  342. dns_hash_lock=lock_alloc();
  343. if (dns_hash_lock==0){
  344. ret=E_OUT_OF_MEM;
  345. goto error;
  346. }
  347. if (lock_init(dns_hash_lock)==0){
  348. lock_dealloc(dns_hash_lock);
  349. dns_hash_lock=0;
  350. ret=-1;
  351. goto error;
  352. }
  353. #ifdef DNS_WATCHDOG_SUPPORT
  354. dns_servers_up=shm_malloc(sizeof(atomic_t));
  355. if (dns_servers_up==0){
  356. ret=E_OUT_OF_MEM;
  357. goto error;
  358. }
  359. atomic_set(dns_servers_up, 1);
  360. #endif
  361. /* fix options */
  362. default_core_cfg.dns_cache_max_mem<<=10; /* Kb */ /* TODO: test with 0 */
  363. if (default_core_cfg.use_dns_cache==0)
  364. default_core_cfg.use_dns_failover=0; /* cannot work w/o dns_cache support */
  365. /* fix flags */
  366. fix_dns_flags(NULL, NULL);
  367. dns_timer_h=timer_alloc();
  368. if (dns_timer_h==0){
  369. ret=E_OUT_OF_MEM;
  370. goto error;
  371. }
  372. if (dns_timer_interval){
  373. timer_init(dns_timer_h, dns_timer, 0, 0); /* "slow" timer */
  374. if (timer_add(dns_timer_h, S_TO_TICKS(dns_timer_interval))<0){
  375. LOG(L_CRIT, "BUG: dns_cache_init: failed to add the timer\n");
  376. timer_free(dns_timer_h);
  377. dns_timer_h=0;
  378. goto error;
  379. }
  380. }
  381. return 0;
  382. error:
  383. destroy_dns_cache();
  384. return ret;
  385. }
  386. #ifdef USE_DNS_CACHE_STATS
  387. int init_dns_cache_stats(int iproc_num)
  388. {
  389. /* do not initialize the stats array if the DNS cache will not be used */
  390. if (dns_cache_init==0) return 0;
  391. /* if it is already initialized */
  392. if (dns_cache_stats)
  393. shm_free(dns_cache_stats);
  394. dns_cache_stats=shm_malloc(sizeof(*dns_cache_stats) * iproc_num);
  395. if (dns_cache_stats==0){
  396. return E_OUT_OF_MEM;
  397. }
  398. memset(dns_cache_stats, 0, sizeof(*dns_cache_stats) * iproc_num);
  399. return 0;
  400. }
  401. #endif
  402. /* hash function, type is not used (obsolete)
  403. * params: char* s, int len, int type
  404. * returns the hash value
  405. */
  406. #define dns_hash_no(s, len, type) \
  407. (get_hash1_case_raw((s),(len)) % DNS_HASH_SIZE)
  408. #ifdef DNS_CACHE_DEBUG
  409. #define DEBUG_LU_LST
  410. #ifdef DEBUG_LU_LST
  411. #include <stdlib.h> /* abort() */
  412. #define check_lu_lst(l) ((((l)->next==(l)) || ((l)->prev==(l))) && \
  413. ((l)!=dns_last_used_lst))
  414. #define dbg_lu_lst(txt, l) \
  415. LOG(L_CRIT, "BUG: %s: crt(%p, %p, %p)," \
  416. " prev(%p, %p, %p), next(%p, %p, %p)\n", txt, \
  417. (l), (l)->next, (l)->prev, \
  418. (l)->prev, (l)->prev->next, (l)->prev->prev, \
  419. (l)->next, (l)->next->next, (l)->next->prev \
  420. )
  421. #define debug_lu_lst( txt, l) \
  422. do{ \
  423. if (check_lu_lst((l))){ \
  424. dbg_lu_lst(txt " crt:", (l)); \
  425. abort(); \
  426. } \
  427. if (check_lu_lst((l)->next)){ \
  428. dbg_lu_lst(txt " next:", (l)); \
  429. abort(); \
  430. } \
  431. if (check_lu_lst((l)->prev)){ \
  432. dbg_lu_lst(txt " prev:", (l)); \
  433. abort(); \
  434. } \
  435. }while(0)
  436. #endif
  437. #endif /* DNS_CACHE_DEBUG */
  438. /* must be called with the DNS_LOCK hold
  439. * remove and entry from the hash, dec. its refcnt and if not referenced
  440. * anymore deletes it */
  441. inline static void _dns_hash_remove(struct dns_hash_entry* e)
  442. {
  443. clist_rm(e, next, prev);
  444. #ifdef DNS_CACHE_DEBUG
  445. e->next=e->prev=0;
  446. #endif
  447. #ifdef DNS_LU_LST
  448. #ifdef DEBUG_LU_LST
  449. debug_lu_lst("_dns_hash_remove: pre rm:", &e->last_used_lst);
  450. #endif
  451. clist_rm(&e->last_used_lst, next, prev);
  452. #ifdef DEBUG_LU_LST
  453. debug_lu_lst("_dns_hash_remove: post rm:", &e->last_used_lst);
  454. #endif
  455. #ifdef DNS_CACHE_DEBUG
  456. e->last_used_lst.next=e->last_used_lst.prev=0;
  457. #endif
  458. #endif
  459. *dns_cache_mem_used-=e->total_size;
  460. dns_hash_put(e);
  461. }
  462. /* non locking version (the dns hash must _be_ locked externally)
  463. * returns 0 when not found, or the entry on success (an entry with a
  464. * similar name but with a CNAME type will always match).
  465. * it doesn't increase the internal refcnt
  466. * returns the entry when found, 0 when not found and sets *err to !=0
  467. * on error (e.g. recursive cnames)
  468. * WARNING: - internal use only
  469. * - always check if the returned entry type is CNAME */
  470. inline static struct dns_hash_entry* _dns_hash_find(str* name, int type,
  471. int* h, int* err)
  472. {
  473. struct dns_hash_entry* e;
  474. struct dns_hash_entry* tmp;
  475. struct dns_hash_entry* ret;
  476. ticks_t now;
  477. int cname_chain;
  478. str cname;
  479. #ifdef DNS_WATCHDOG_SUPPORT
  480. int servers_up;
  481. servers_up = atomic_get(dns_servers_up);
  482. #endif
  483. cname_chain=0;
  484. ret=0;
  485. now=get_ticks_raw();
  486. *err=0;
  487. again:
  488. *h=dns_hash_no(name->s, name->len, type);
  489. #ifdef DNS_CACHE_DEBUG
  490. DBG("dns_hash_find(%.*s(%d), %d), h=%d\n", name->len, name->s,
  491. name->len, type, *h);
  492. #endif
  493. clist_foreach_safe(&dns_hash[*h], e, tmp, next){
  494. if (
  495. #ifdef DNS_WATCHDOG_SUPPORT
  496. /* remove expired elements only when the dns servers are up */
  497. servers_up &&
  498. #endif
  499. /* automatically remove expired elements */
  500. ((e->ent_flags & DNS_FLAG_PERMANENT) == 0) &&
  501. ((s_ticks_t)(now-e->expire)>=0)
  502. ) {
  503. _dns_hash_remove(e);
  504. }else if ((e->type==type) && (e->name_len==name->len) &&
  505. (strncasecmp(e->name, name->s, e->name_len)==0)){
  506. e->last_used=now;
  507. #ifdef DNS_LU_LST
  508. /* add it at the end */
  509. #ifdef DEBUG_LU_LST
  510. debug_lu_lst("_dns_hash_find: pre rm:", &e->last_used_lst);
  511. #endif
  512. clist_rm(&e->last_used_lst, next, prev);
  513. clist_append(dns_last_used_lst, &e->last_used_lst, next, prev);
  514. #ifdef DEBUG_LU_LST
  515. debug_lu_lst("_dns_hash_find: post append:", &e->last_used_lst);
  516. #endif
  517. #endif
  518. return e;
  519. }else if ((e->type==T_CNAME) &&
  520. !((e->rr_lst==0) || (e->ent_flags & DNS_FLAG_BAD_NAME)) &&
  521. (e->name_len==name->len) &&
  522. (strncasecmp(e->name, name->s, e->name_len)==0)){
  523. /*if CNAME matches and CNAME is entry is not a neg. cache entry
  524. (could be produced by a specific CNAME lookup)*/
  525. e->last_used=now;
  526. #ifdef DNS_LU_LST
  527. /* add it at the end */
  528. #ifdef DEBUG_LU_LST
  529. debug_lu_lst("_dns_hash_find: cname: pre rm:", &e->last_used_lst);
  530. #endif
  531. clist_rm(&e->last_used_lst, next, prev);
  532. clist_append(dns_last_used_lst, &e->last_used_lst, next, prev);
  533. #ifdef DEBUG_LU_LST
  534. debug_lu_lst("_dns_hash_find: cname: post append:",
  535. &e->last_used_lst);
  536. #endif
  537. #endif
  538. ret=e; /* if this is an unfinished cname chain, we try to
  539. return the last cname */
  540. /* this is a cname => retry using its value */
  541. if (cname_chain> MAX_CNAME_CHAIN){
  542. LOG(L_ERR, "ERROR: _dns_hash_find: cname chain too long "
  543. "or recursive (\"%.*s\")\n", name->len, name->s);
  544. ret=0; /* error*/
  545. *err=-1;
  546. break;
  547. }
  548. cname_chain++;
  549. cname.s=((struct cname_rdata*)e->rr_lst->rdata)->name;
  550. cname.len= ((struct cname_rdata*)e->rr_lst->rdata)->name_len;
  551. name=&cname;
  552. goto again;
  553. }
  554. }
  555. return ret;
  556. }
  557. /* frees cache entries, if expired_only=0 only expired entries will be
  558. * removed, else all of them
  559. * it will process maximum no entries (to process all of them use -1)
  560. * returns the number of deleted entries
  561. * This should be called from a timer process*/
  562. inline static int dns_cache_clean(unsigned int no, int expired_only)
  563. {
  564. struct dns_hash_entry* e;
  565. ticks_t now;
  566. unsigned int n;
  567. unsigned int deleted;
  568. #ifdef DNS_LU_LST
  569. struct dns_lu_lst* l;
  570. struct dns_lu_lst* tmp;
  571. #else
  572. struct dns_hash_entry* t;
  573. unsigned int h;
  574. static unsigned int start=0;
  575. #endif
  576. n=0;
  577. deleted=0;
  578. now=get_ticks_raw();
  579. LOCK_DNS_HASH();
  580. #ifdef DNS_LU_LST
  581. clist_foreach_safe(dns_last_used_lst, l, tmp, next){
  582. e=(struct dns_hash_entry*)(((char*)l)-
  583. (char*)&((struct dns_hash_entry*)(0))->last_used_lst);
  584. if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  585. && (!expired_only || ((s_ticks_t)(now-e->expire)>=0))
  586. ) {
  587. _dns_hash_remove(e);
  588. deleted++;
  589. }
  590. n++;
  591. if (n>=no) break;
  592. }
  593. #else
  594. for(h=start; h!=(start+DNS_HASH_SIZE); h++){
  595. clist_foreach_safe(&dns_hash[h%DNS_HASH_SIZE], e, t, next){
  596. if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  597. && ((s_ticks_t)(now-e->expire)>=0)
  598. ) {
  599. _dns_hash_remove(e);
  600. deleted++;
  601. }
  602. n++;
  603. if (n>=no) goto skip;
  604. }
  605. }
  606. /* not fair, but faster then random() */
  607. if (!expired_only){
  608. for(h=start; h!=(start+DNS_HASH_SIZE); h++){
  609. clist_foreach_safe(&dns_hash[h%DNS_HASH_SIZE], e, t, next){
  610. if ((e->ent_flags & DNS_FLAG_PERMANENT) == 0) {
  611. _dns_hash_remove(e);
  612. deleted++;
  613. }
  614. n++;
  615. if (n>=no) goto skip;
  616. }
  617. }
  618. }
  619. skip:
  620. start=h;
  621. #endif
  622. UNLOCK_DNS_HASH();
  623. return deleted;
  624. }
  625. /* frees cache entries, if expired_only=0 only expired entries will be
  626. * removed, else all of them
  627. * it will stop when the dns cache used memory reaches target (to process all
  628. * of them use 0)
  629. * returns the number of deleted entries */
  630. inline static int dns_cache_free_mem(unsigned int target, int expired_only)
  631. {
  632. struct dns_hash_entry* e;
  633. ticks_t now;
  634. unsigned int deleted;
  635. #ifdef DNS_LU_LST
  636. struct dns_lu_lst* l;
  637. struct dns_lu_lst* tmp;
  638. #else
  639. struct dns_hash_entry* t;
  640. unsigned int h;
  641. static unsigned int start=0;
  642. #endif
  643. deleted=0;
  644. now=get_ticks_raw();
  645. LOCK_DNS_HASH();
  646. #ifdef DNS_LU_LST
  647. clist_foreach_safe(dns_last_used_lst, l, tmp, next){
  648. if (*dns_cache_mem_used<=target) break;
  649. e=(struct dns_hash_entry*)(((char*)l)-
  650. (char*)&((struct dns_hash_entry*)(0))->last_used_lst);
  651. if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  652. && (!expired_only || ((s_ticks_t)(now-e->expire)>=0))
  653. ) {
  654. _dns_hash_remove(e);
  655. deleted++;
  656. }
  657. }
  658. #else
  659. for(h=start; h!=(start+DNS_HASH_SIZE); h++){
  660. clist_foreach_safe(&dns_hash[h%DNS_HASH_SIZE], e, t, next){
  661. if (*dns_cache_mem_used<=target)
  662. goto skip;
  663. if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  664. && ((s_ticks_t)(now-e->expire)>=0)
  665. ) {
  666. _dns_hash_remove(e);
  667. deleted++;
  668. }
  669. }
  670. }
  671. /* not fair, but faster then random() */
  672. if (!expired_only){
  673. for(h=start; h!=(start+DNS_HASH_SIZE); h++){
  674. clist_foreach_safe(&dns_hash[h%DNS_HASH_SIZE], e, t, next){
  675. if (*dns_cache_mem_used<=target)
  676. goto skip;
  677. if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  678. && ((s_ticks_t)(now-e->expire)>=0)
  679. ) {
  680. _dns_hash_remove(e);
  681. deleted++;
  682. }
  683. }
  684. }
  685. }
  686. skip:
  687. start=h;
  688. #endif
  689. UNLOCK_DNS_HASH();
  690. return deleted;
  691. }
  692. /* locking version (the dns hash must _not_be locked externally)
  693. * returns 0 when not found, the searched entry on success (with CNAMEs
  694. * followed) or the last CNAME entry from an unfinished CNAME chain,
  695. * if the search matches a CNAME. On error sets *err (e.g. recursive CNAMEs).
  696. * it increases the internal refcnt => when finished dns_hash_put() must
  697. * be called on the returned entry
  698. * WARNING: - the return might be a CNAME even if type!=CNAME, see above */
  699. inline static struct dns_hash_entry* dns_hash_get(str* name, int type, int* h,
  700. int* err)
  701. {
  702. struct dns_hash_entry* e;
  703. LOCK_DNS_HASH();
  704. e=_dns_hash_find(name, type, h, err);
  705. if (e){
  706. atomic_inc(&e->refcnt);
  707. }
  708. UNLOCK_DNS_HASH();
  709. return e;
  710. }
  711. /* adds a fully created and init. entry (see dns_cache_mk_entry()) to the hash
  712. * table
  713. * returns 0 on success, -1 on error */
  714. inline static int dns_cache_add(struct dns_hash_entry* e)
  715. {
  716. int h;
  717. /* check space */
  718. /* atomic_add_long(dns_cache_total_used, e->size); */
  719. if ((*dns_cache_mem_used+e->total_size)>=cfg_get(core, core_cfg, dns_cache_max_mem)){
  720. #ifdef USE_DNS_CACHE_STATS
  721. dns_cache_stats[process_no].dc_lru_cnt++;
  722. #endif
  723. LOG(L_WARN, "WARNING: dns_cache_add: cache full, trying to free...\n");
  724. /* free ~ 12% of the cache */
  725. dns_cache_free_mem(*dns_cache_mem_used/16*14,
  726. !cfg_get(core, core_cfg, dns_cache_del_nonexp));
  727. if ((*dns_cache_mem_used+e->total_size)>=cfg_get(core, core_cfg, dns_cache_max_mem)){
  728. LOG(L_ERR, "ERROR: dns_cache_add: max. cache mem size exceeded\n");
  729. return -1;
  730. }
  731. }
  732. atomic_inc(&e->refcnt);
  733. h=dns_hash_no(e->name, e->name_len, e->type);
  734. #ifdef DNS_CACHE_DEBUG
  735. DBG("dns_cache_add: adding %.*s(%d) %d (flags=%0x) at %d\n",
  736. e->name_len, e->name, e->name_len, e->type, e->ent_flags, h);
  737. #endif
  738. LOCK_DNS_HASH();
  739. *dns_cache_mem_used+=e->total_size; /* no need for atomic ops, written
  740. only from within a lock */
  741. clist_append(&dns_hash[h], e, next, prev);
  742. #ifdef DNS_LU_LST
  743. clist_append(dns_last_used_lst, &e->last_used_lst, next, prev);
  744. #endif
  745. UNLOCK_DNS_HASH();
  746. return 0;
  747. }
  748. /* same as above, but it must be called with the dns hash lock held
  749. * returns 0 on success, -1 on error */
  750. inline static int dns_cache_add_unsafe(struct dns_hash_entry* e)
  751. {
  752. int h;
  753. /* check space */
  754. /* atomic_add_long(dns_cache_total_used, e->size); */
  755. if ((*dns_cache_mem_used+e->total_size)>=cfg_get(core, core_cfg, dns_cache_max_mem)){
  756. #ifdef USE_DNS_CACHE_STATS
  757. dns_cache_stats[process_no].dc_lru_cnt++;
  758. #endif
  759. LOG(L_WARN, "WARNING: dns_cache_add: cache full, trying to free...\n");
  760. /* free ~ 12% of the cache */
  761. UNLOCK_DNS_HASH();
  762. dns_cache_free_mem(*dns_cache_mem_used/16*14,
  763. !cfg_get(core, core_cfg, dns_cache_del_nonexp));
  764. LOCK_DNS_HASH();
  765. if ((*dns_cache_mem_used+e->total_size)>=cfg_get(core, core_cfg, dns_cache_max_mem)){
  766. LOG(L_ERR, "ERROR: dns_cache_add: max. cache mem size exceeded\n");
  767. return -1;
  768. }
  769. }
  770. atomic_inc(&e->refcnt);
  771. h=dns_hash_no(e->name, e->name_len, e->type);
  772. #ifdef DNS_CACHE_DEBUG
  773. DBG("dns_cache_add: adding %.*s(%d) %d (flags=%0x) at %d\n",
  774. e->name_len, e->name, e->name_len, e->type, e->ent_flags, h);
  775. #endif
  776. *dns_cache_mem_used+=e->total_size; /* no need for atomic ops, written
  777. only from within a lock */
  778. clist_append(&dns_hash[h], e, next, prev);
  779. #ifdef DNS_LU_LST
  780. clist_append(dns_last_used_lst, &e->last_used_lst, next, prev);
  781. #endif
  782. return 0;
  783. }
  784. /* creates a "negative" entry which will be valid for ttl seconds */
  785. inline static struct dns_hash_entry* dns_cache_mk_bad_entry(str* name,
  786. int type,
  787. int ttl,
  788. int flags)
  789. {
  790. struct dns_hash_entry* e;
  791. int size;
  792. ticks_t now;
  793. #ifdef DNS_CACHE_DEBUG
  794. DBG("dns_cache_mk_bad_entry(%.*s, %d, %d, %d)\n", name->len, name->s,
  795. type, ttl, flags);
  796. #endif
  797. size=sizeof(struct dns_hash_entry)+name->len-1+1;
  798. e=shm_malloc(size);
  799. if (e==0){
  800. LOG(L_ERR, "ERROR: dns_cache_mk_bad_entry: out of memory\n");
  801. return 0;
  802. }
  803. memset(e, 0, size); /* init with 0*/
  804. e->total_size=size;
  805. e->name_len=name->len;
  806. e->type=type;
  807. now=get_ticks_raw();
  808. e->last_used=now;
  809. e->expire=now+S_TO_TICKS(ttl);
  810. memcpy(e->name, name->s, name->len);
  811. e->ent_flags=flags;
  812. return e;
  813. }
  814. /* create a a/aaaa hash entry from a name and ip address
  815. * returns 0 on error */
  816. inline static struct dns_hash_entry* dns_cache_mk_ip_entry(str* name,
  817. struct ip_addr* ip)
  818. {
  819. struct dns_hash_entry* e;
  820. int size;
  821. ticks_t now;
  822. /* everything is allocated in one block: dns_hash_entry + name +
  823. * + dns_rr + rdata; dns_rr must start at an aligned adress,
  824. * hence we need to round dns_hash_entry+name size to a sizeof(long)
  825. * multiple.
  826. * Memory image:
  827. * struct dns_hash_entry
  828. * name (name_len+1 bytes)
  829. * padding to multiple of sizeof(long)
  830. * dns_rr
  831. * rdata (no padding needed, since for ip is just an array of chars)
  832. */
  833. size=ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1)+
  834. sizeof(struct dns_rr)+ ip->len;
  835. e=shm_malloc(size);
  836. if (e==0){
  837. LOG(L_ERR, "ERROR: dns_cache_mk_ip_entry: out of memory\n");
  838. return 0;
  839. }
  840. memset(e, 0, size); /* init with 0*/
  841. e->total_size=size;
  842. e->name_len=name->len;
  843. e->type=(ip->af==AF_INET)?T_A:T_AAAA;
  844. now=get_ticks_raw();
  845. e->last_used=now;
  846. e->expire=now-1; /* maximum expire */
  847. memcpy(e->name, name->s, name->len); /* memset makes sure is 0-term. */
  848. e->rr_lst=(void*)((char*)e+
  849. ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1));
  850. e->rr_lst->rdata=(void*)((char*)e->rr_lst+sizeof(struct dns_rr));
  851. e->rr_lst->expire=now-1; /* maximum expire */
  852. /* no need to align rr_lst->rdata for a or aaaa records */
  853. memcpy(e->rr_lst->rdata, ip->u.addr, ip->len);
  854. return e;
  855. }
  856. /* creates an srv hash entry from the given parameters
  857. * returns 0 on error */
  858. static struct dns_hash_entry* dns_cache_mk_srv_entry(str* name,
  859. unsigned short priority,
  860. unsigned short weight,
  861. unsigned short port,
  862. str* rr_name,
  863. int ttl)
  864. {
  865. struct dns_hash_entry* e;
  866. int size;
  867. ticks_t now;
  868. /* everything is allocated in one block: dns_hash_entry + name +
  869. * + dns_rr + rdata; dns_rr must start at an aligned adress,
  870. * hence we need to round dns_hash_entry+name size to a sizeof(long),
  871. * and similarly, dns_rr must be rounded to sizeof(short).
  872. * multiple.
  873. * Memory image:
  874. * struct dns_hash_entry
  875. * name (name_len+1 bytes)
  876. * padding to multiple of sizeof(long)
  877. * dns_rr
  878. * padding to multiple of sizeof(short)
  879. * rdata
  880. */
  881. size=ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1) +
  882. ROUND_SHORT(sizeof(struct dns_rr)) +
  883. sizeof(struct srv_rdata)-1 +
  884. rr_name->len+1;
  885. e=shm_malloc(size);
  886. if (e==0){
  887. LOG(L_ERR, "ERROR: dns_cache_srv_ip_entry: out of memory\n");
  888. return 0;
  889. }
  890. memset(e, 0, size); /* init with 0*/
  891. e->total_size=size;
  892. e->name_len=name->len;
  893. e->type=T_SRV;
  894. now=get_ticks_raw();
  895. e->last_used=now;
  896. e->expire=now+S_TO_TICKS(ttl);
  897. memcpy(e->name, name->s, name->len); /* memset makes sure is 0-term. */
  898. e->rr_lst=(void*)((char*)e+
  899. ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1));
  900. e->rr_lst->rdata=(void*)((char*)e->rr_lst+ROUND_SHORT(sizeof(struct dns_rr)));
  901. e->rr_lst->expire=e->expire;
  902. ((struct srv_rdata*)e->rr_lst->rdata)->priority = priority;
  903. ((struct srv_rdata*)e->rr_lst->rdata)->weight = weight;
  904. ((struct srv_rdata*)e->rr_lst->rdata)->port = port;
  905. ((struct srv_rdata*)e->rr_lst->rdata)->name_len = rr_name->len;
  906. memcpy(((struct srv_rdata*)e->rr_lst->rdata)->name, rr_name->s, rr_name->len);
  907. return e;
  908. }
  909. /* create a dns hash entry from a name and a rdata list (pkg_malloc'ed)
  910. * (it will use only the type records with the name "name" from the
  911. * rdata list with one exception: if a matching CNAME with the same
  912. * name is found, the search will stop and this will be the record used)
  913. * returns 0 on error and removes the used elements from the rdata list*/
  914. inline static struct dns_hash_entry* dns_cache_mk_rd_entry(str* name, int type,
  915. struct rdata** rd_lst)
  916. {
  917. struct dns_hash_entry* e;
  918. struct dns_rr* rr;
  919. struct dns_rr** tail_rr;
  920. struct rdata** p;
  921. struct rdata* tmp_lst;
  922. struct rdata** tail;
  923. struct rdata* l;
  924. int size;
  925. ticks_t now;
  926. unsigned int max_ttl;
  927. unsigned int ttl;
  928. int i;
  929. #define rec_matches(rec, t, n) /*(struct rdata* record, int type, str* name)*/\
  930. ( ((rec)->name_len==(n)->len) && ((rec)->type==(t)) && \
  931. (strncasecmp((rec)->name, (n)->s, (n)->len)==0))
  932. /* init */
  933. tmp_lst=0;
  934. tail=&tmp_lst;
  935. /* everything is allocated in one block: dns_hash_entry + name +
  936. * + dns_rr + rdata_raw+ ....; dns_rr must start at an aligned adress,
  937. * hence we need to round dns_hash_entry+name size to a sizeof(long)
  938. * multiple. If rdata type requires it, rdata_raw might need to be also
  939. * aligned.
  940. * Memory image:
  941. * struct dns_hash_entry (e)
  942. * name (name_len+1 bytes) (&e->name[0])
  943. * padding to multiple of sizeof(char*)
  944. * dns_rr1 (e->rr_lst)
  945. * possible padding: no padding for a_rdata or aaaa_rdata,
  946. * multipe of sizeof(short) for srv_rdata,
  947. * multiple of sizeof(long) for naptr_rdata and others
  948. * dns_rr1->rdata (e->rr_lst->rdata)
  949. * padding to multipe of sizeof long
  950. * dns_rr2 (e->rr_lst->next)
  951. * ....
  952. *
  953. */
  954. size=0;
  955. if (*rd_lst==0)
  956. return 0;
  957. /* find the first matching rr, if it's a CNAME use CNAME as type,
  958. * if not continue with the original type */
  959. for(p=rd_lst; *p; p=&(*p)->next){
  960. if (((*p)->name_len==name->len) &&
  961. (((*p)->type==type) || ((*p)->type==T_CNAME)) &&
  962. (strncasecmp((*p)->name, name->s, name->len)==0)){
  963. type=(*p)->type;
  964. break;
  965. }
  966. }
  967. /* continue, we found the type we are looking for */
  968. switch(type){
  969. case T_A:
  970. for(; *p;){
  971. if (!rec_matches((*p), type, name)){
  972. /* skip this record */
  973. p=&(*p)->next; /* advance */
  974. continue;
  975. }
  976. size+=ROUND_POINTER(sizeof(struct dns_rr)+
  977. sizeof(struct a_rdata));
  978. /* add it to our tmp. lst */
  979. *tail=*p;
  980. tail=&(*p)->next;
  981. /* detach it from the rd list */
  982. *p=(*p)->next;
  983. /* don't advance p, because the crt. elem. has
  984. * just been elimintated */
  985. }
  986. break;
  987. case T_AAAA:
  988. for(; *p;){
  989. if (!rec_matches((*p), type, name)){
  990. /* skip this record */
  991. p=&(*p)->next; /* advance */
  992. continue;
  993. }
  994. /* no padding */
  995. size+=ROUND_POINTER(sizeof(struct dns_rr)+
  996. sizeof(struct aaaa_rdata));
  997. /* add it to our tmp. lst */
  998. *tail=*p;
  999. tail=&(*p)->next;
  1000. /* detach it from the rd list */
  1001. *p=(*p)->next;
  1002. /* don't advance p, because the crt. elem. has
  1003. * just been elimintated */
  1004. }
  1005. break;
  1006. case T_SRV:
  1007. for(; *p;){
  1008. if (!rec_matches((*p), type, name)){
  1009. /* skip this record */
  1010. p=&(*p)->next; /* advance */
  1011. continue;
  1012. }
  1013. /* padding to short */
  1014. size+=ROUND_POINTER(ROUND_SHORT(sizeof(struct dns_rr))+
  1015. SRV_RDATA_SIZE(*(struct srv_rdata*)(*p)->rdata));
  1016. /* add it to our tmp. lst */
  1017. *tail=*p;
  1018. tail=&(*p)->next;
  1019. /* detach it from the rd list */
  1020. *p=(*p)->next;
  1021. /* don't advance p, because the crt. elem. has
  1022. * just been elimintated */
  1023. }
  1024. break;
  1025. case T_NAPTR:
  1026. for(; *p;){
  1027. if (!rec_matches((*p), type, name)){
  1028. /* skip this record */
  1029. p=&(*p)->next; /* advance */
  1030. continue;
  1031. }
  1032. /* padding to char* */
  1033. size+=ROUND_POINTER(ROUND_POINTER(sizeof(struct dns_rr))+
  1034. NAPTR_RDATA_SIZE(*(struct naptr_rdata*)(*p)->rdata));
  1035. /* add it to our tmp. lst */
  1036. *tail=*p;
  1037. tail=&(*p)->next;
  1038. /* detach it from the rd list */
  1039. *p=(*p)->next;
  1040. /* don't advance p, because the crt. elem. has
  1041. * just been elimintated */
  1042. }
  1043. break;
  1044. case T_CNAME:
  1045. for(; *p;){
  1046. if (!rec_matches((*p), type, name)){
  1047. /* skip this record */
  1048. p=&(*p)->next; /* advance */
  1049. continue;
  1050. }
  1051. /* no padding */
  1052. size+=ROUND_POINTER(sizeof(struct dns_rr)+
  1053. CNAME_RDATA_SIZE(*(struct cname_rdata*)(*p)->rdata));
  1054. /* add it to our tmp. lst */
  1055. *tail=*p;
  1056. tail=&(*p)->next;
  1057. /* detach it from the rd list */
  1058. *p=(*p)->next;
  1059. /* don't advance p, because the crt. elem. has
  1060. * just been elimintated */
  1061. }
  1062. break;
  1063. case T_TXT:
  1064. for(; *p;){
  1065. if (!rec_matches((*p), type, name)){
  1066. /* skip this record */
  1067. p=&(*p)->next; /* advance */
  1068. continue;
  1069. }
  1070. /* padding to char* (because of txt[]->cstr*/
  1071. size+=ROUND_POINTER(ROUND_POINTER(sizeof(struct dns_rr))+
  1072. TXT_RDATA_SIZE(*(struct txt_rdata*)(*p)->rdata));
  1073. /* add it to our tmp. lst */
  1074. *tail=*p;
  1075. tail=&(*p)->next;
  1076. /* detach it from the rd list */
  1077. *p=(*p)->next;
  1078. /* don't advance p, because the crt. elem. has
  1079. * just been elimintated */
  1080. }
  1081. break;
  1082. case T_EBL:
  1083. for(; *p;){
  1084. if (!rec_matches((*p), type, name)){
  1085. /* skip this record */
  1086. p=&(*p)->next; /* advance */
  1087. continue;
  1088. }
  1089. /* padding to char* (because of the char* pointers */
  1090. size+=ROUND_POINTER(ROUND_POINTER(sizeof(struct dns_rr))+
  1091. EBL_RDATA_SIZE(*(struct ebl_rdata*)(*p)->rdata));
  1092. /* add it to our tmp. lst */
  1093. *tail=*p;
  1094. tail=&(*p)->next;
  1095. /* detach it from the rd list */
  1096. *p=(*p)->next;
  1097. /* don't advance p, because the crt. elem. has
  1098. * just been elimintated */
  1099. }
  1100. break;
  1101. case T_PTR:
  1102. for(; *p;){
  1103. if (!rec_matches((*p), type, name)){
  1104. /* skip this record */
  1105. p=&(*p)->next; /* advance */
  1106. continue;
  1107. }
  1108. /* no padding */
  1109. size+=ROUND_POINTER(sizeof(struct dns_rr)+
  1110. PTR_RDATA_SIZE(*(struct ptr_rdata*)(*p)->rdata));
  1111. /* add it to our tmp. lst */
  1112. *tail=*p;
  1113. tail=&(*p)->next;
  1114. /* detach it from the rd list */
  1115. *p=(*p)->next;
  1116. /* don't advance p, because the crt. elem. has
  1117. * just been elimintated */
  1118. }
  1119. break;
  1120. default:
  1121. LOG(L_CRIT, "BUG: dns_cache_mk_rd_entry: type %d not "
  1122. "supported\n", type);
  1123. /* we don't know what to do with it, so don't
  1124. * add it to the tmp_lst */
  1125. return 0; /* error */
  1126. }
  1127. *tail=0; /* mark the end of our tmp_lst */
  1128. if (size==0){
  1129. #ifdef DNS_CACHE_DEBUG
  1130. DBG("dns_cache_mk_rd_entry: entry %.*s (%d) not found\n",
  1131. name->len, name->s, type);
  1132. #endif
  1133. return 0;
  1134. }
  1135. /* compute size */
  1136. size+=ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1);
  1137. e=shm_malloc(size);
  1138. if (e==0){
  1139. LOG(L_ERR, "ERROR: dns_cache_mk_rd_entry: out of memory\n");
  1140. return 0;
  1141. }
  1142. memset(e, 0, size); /* init with 0 */
  1143. clist_init(e, next, prev);
  1144. e->total_size=size;
  1145. e->name_len=name->len;
  1146. e->type=type;
  1147. now=get_ticks_raw();
  1148. e->last_used=now;
  1149. memcpy(e->name, name->s, name->len); /* memset makes sure is 0-term. */
  1150. e->rr_lst=(struct dns_rr*)((char*)e+
  1151. ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1));
  1152. tail_rr=&(e->rr_lst);
  1153. rr=e->rr_lst;
  1154. max_ttl=0;
  1155. /* copy the actual data */
  1156. switch(type){
  1157. case T_A:
  1158. for(l=tmp_lst; l; l=l->next){
  1159. ttl=FIX_TTL(l->ttl);
  1160. rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1161. max_ttl=MAX(max_ttl, ttl);
  1162. rr->rdata=(void*)((char*)rr+sizeof(struct dns_rr));
  1163. memcpy(rr->rdata, l->rdata, sizeof(struct a_rdata));
  1164. rr->next=(void*)((char*)rr+ROUND_POINTER(sizeof(struct dns_rr)+
  1165. sizeof(struct a_rdata)));
  1166. tail_rr=&(rr->next);
  1167. rr=rr->next;
  1168. }
  1169. break;
  1170. case T_AAAA:
  1171. for(l=tmp_lst; l; l=l->next){
  1172. ttl=FIX_TTL(l->ttl);
  1173. rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1174. max_ttl=MAX(max_ttl, ttl);
  1175. rr->rdata=(void*)((char*)rr+sizeof(struct dns_rr));
  1176. memcpy(rr->rdata, l->rdata, sizeof(struct aaaa_rdata));
  1177. rr->next=(void*)((char*)rr+ROUND_POINTER(sizeof(struct dns_rr)+
  1178. sizeof(struct aaaa_rdata)));
  1179. tail_rr=&(rr->next);
  1180. rr=rr->next;
  1181. }
  1182. break;
  1183. case T_SRV:
  1184. for(l=tmp_lst; l; l=l->next){
  1185. ttl=FIX_TTL(l->ttl);
  1186. rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1187. max_ttl=MAX(max_ttl, ttl);
  1188. rr->rdata=(void*)((char*)rr+
  1189. ROUND_SHORT(sizeof(struct dns_rr)));
  1190. /* copy the whole srv_rdata block*/
  1191. memcpy(rr->rdata, l->rdata,
  1192. SRV_RDATA_SIZE(*(struct srv_rdata*)l->rdata) );
  1193. rr->next=(void*)((char*)rr+
  1194. ROUND_POINTER( ROUND_SHORT(sizeof(struct dns_rr))+
  1195. SRV_RDATA_SIZE(
  1196. *(struct srv_rdata*)l->rdata)));
  1197. tail_rr=&(rr->next);
  1198. rr=rr->next;
  1199. }
  1200. break;
  1201. case T_NAPTR:
  1202. for(l=tmp_lst; l; l=l->next){
  1203. ttl=FIX_TTL(l->ttl);
  1204. rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1205. max_ttl=MAX(max_ttl, ttl);
  1206. rr->rdata=(void*)((char*)rr+
  1207. ROUND_POINTER(sizeof(struct dns_rr)));
  1208. /* copy the whole naptr_rdata block*/
  1209. memcpy(rr->rdata, l->rdata,
  1210. NAPTR_RDATA_SIZE(*(struct naptr_rdata*)l->rdata) );
  1211. /* adjust the string pointer */
  1212. ((struct naptr_rdata*)rr->rdata)->flags=
  1213. translate_pointer((char*)rr->rdata, (char*)l->rdata,
  1214. (((struct naptr_rdata*)l->rdata)->flags));
  1215. ((struct naptr_rdata*)rr->rdata)->services=
  1216. translate_pointer((char*)rr->rdata, (char*)l->rdata,
  1217. (((struct naptr_rdata*)l->rdata)->services));
  1218. ((struct naptr_rdata*)rr->rdata)->regexp=
  1219. translate_pointer((char*)rr->rdata, (char*)l->rdata,
  1220. (((struct naptr_rdata*)l->rdata)->regexp));
  1221. ((struct naptr_rdata*)rr->rdata)->repl=
  1222. translate_pointer((char*)rr->rdata, (char*)l->rdata,
  1223. (((struct naptr_rdata*)l->rdata)->repl));
  1224. rr->next=(void*)((char*)rr+
  1225. ROUND_POINTER(ROUND_POINTER(sizeof(struct dns_rr))+
  1226. NAPTR_RDATA_SIZE(
  1227. *(struct naptr_rdata*)l->rdata)));
  1228. tail_rr=&(rr->next);
  1229. rr=rr->next;
  1230. }
  1231. break;
  1232. case T_CNAME:
  1233. for(l=tmp_lst; l; l=l->next){
  1234. ttl=FIX_TTL(l->ttl);
  1235. rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1236. max_ttl=MAX(max_ttl, ttl);
  1237. rr->rdata=(void*)((char*)rr+sizeof(struct dns_rr));
  1238. memcpy(rr->rdata, l->rdata,
  1239. CNAME_RDATA_SIZE(*(struct cname_rdata*)l->rdata));
  1240. rr->next=(void*)((char*)rr+ROUND_POINTER(sizeof(struct dns_rr)+
  1241. CNAME_RDATA_SIZE(*(struct cname_rdata*)l->rdata)));
  1242. tail_rr=&(rr->next);
  1243. rr=rr->next;
  1244. }
  1245. break;
  1246. case T_TXT:
  1247. for(l=tmp_lst; l; l=l->next){
  1248. ttl=FIX_TTL(l->ttl);
  1249. rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1250. max_ttl=MAX(max_ttl, ttl);
  1251. rr->rdata=(void*)((char*)rr+
  1252. ROUND_POINTER(sizeof(struct dns_rr)));
  1253. memcpy(rr->rdata, l->rdata,
  1254. TXT_RDATA_SIZE(*(struct txt_rdata*)l->rdata));
  1255. /* adjust the string pointers */
  1256. for (i=0; i<((struct txt_rdata*)l->rdata)->cstr_no; i++){
  1257. ((struct txt_rdata*)rr->rdata)->txt[i].cstr=
  1258. translate_pointer((char*)rr->rdata, (char*)l->rdata,
  1259. ((struct txt_rdata*)l->rdata)->txt[i].cstr);
  1260. }
  1261. rr->next=(void*)((char*)rr+
  1262. ROUND_POINTER(ROUND_POINTER(sizeof(struct dns_rr))+
  1263. TXT_RDATA_SIZE(*(struct txt_rdata*)l->rdata)));
  1264. tail_rr=&(rr->next);
  1265. rr=rr->next;
  1266. }
  1267. break;
  1268. case T_EBL:
  1269. for(l=tmp_lst; l; l=l->next){
  1270. ttl=FIX_TTL(l->ttl);
  1271. rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1272. max_ttl=MAX(max_ttl, ttl);
  1273. rr->rdata=(void*)((char*)rr+
  1274. ROUND_POINTER(sizeof(struct dns_rr)));
  1275. memcpy(rr->rdata, l->rdata,
  1276. EBL_RDATA_SIZE(*(struct ebl_rdata*)l->rdata));
  1277. /* adjust the string pointers */
  1278. ((struct ebl_rdata*)rr->rdata)->separator=
  1279. translate_pointer((char*)rr->rdata, (char*)l->rdata,
  1280. ((struct ebl_rdata*)l->rdata)->separator);
  1281. ((struct ebl_rdata*)rr->rdata)->separator=
  1282. translate_pointer((char*)rr->rdata, (char*)l->rdata,
  1283. ((struct ebl_rdata*)l->rdata)->separator);
  1284. ((struct ebl_rdata*)rr->rdata)->apex=
  1285. translate_pointer((char*)rr->rdata, (char*)l->rdata,
  1286. ((struct ebl_rdata*)l->rdata)->apex);
  1287. rr->next=(void*)((char*)rr+
  1288. ROUND_POINTER(ROUND_POINTER(sizeof(struct dns_rr))+
  1289. EBL_RDATA_SIZE(*(struct ebl_rdata*)l->rdata)));
  1290. tail_rr=&(rr->next);
  1291. rr=rr->next;
  1292. }
  1293. break;
  1294. case T_PTR:
  1295. for(l=tmp_lst; l; l=l->next){
  1296. ttl=FIX_TTL(l->ttl);
  1297. rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1298. max_ttl=MAX(max_ttl, ttl);
  1299. rr->rdata=(void*)((char*)rr+sizeof(struct dns_rr));
  1300. memcpy(rr->rdata, l->rdata,
  1301. PTR_RDATA_SIZE(*(struct ptr_rdata*)l->rdata));
  1302. rr->next=(void*)((char*)rr+ROUND_POINTER(sizeof(struct dns_rr)+
  1303. PTR_RDATA_SIZE(*(struct ptr_rdata*)l->rdata)));
  1304. tail_rr=&(rr->next);
  1305. rr=rr->next;
  1306. }
  1307. break;
  1308. default:
  1309. /* do nothing */
  1310. LOG(L_CRIT, "BUG: dns_cache_mk_rd_entry: create: type %d not "
  1311. "supported\n", type);
  1312. ;
  1313. }
  1314. *tail_rr=0; /* terminate the list */
  1315. e->expire=now+S_TO_TICKS(max_ttl);
  1316. free_rdata_list(tmp_lst);
  1317. return e;
  1318. }
  1319. /* structure used only inside dns_cache_mk_rd_entry2 to break
  1320. * the list of records into records of the same type */
  1321. struct tmp_rec{
  1322. struct rdata* rd;
  1323. struct dns_hash_entry* e;
  1324. struct dns_rr* rr;
  1325. struct dns_rr** tail_rr;
  1326. int max_ttl;
  1327. int size;
  1328. };
  1329. /* create several dns hash entries from a list of rdata structs
  1330. * returns 0 on error */
  1331. inline static struct dns_hash_entry* dns_cache_mk_rd_entry2(struct rdata* rd)
  1332. {
  1333. struct rdata* l;
  1334. ticks_t now;
  1335. struct tmp_rec rec[MAX_DNS_RECORDS];
  1336. int rec_idx[MAX_DNS_RECORDS];
  1337. int r, i, j;
  1338. int no_records; /* number of different records */
  1339. unsigned int ttl;
  1340. no_records=0;
  1341. rec[0].e=0;
  1342. /* everything is allocated in one block: dns_hash_entry + name +
  1343. * + dns_rr + rdata_raw+ ....; dns_rr must start at an aligned adress,
  1344. * hence we need to round dns_hash_entry+name size to a sizeof(long)
  1345. * multiple. If rdata type requires it, rdata_raw might need to be also
  1346. * aligned.
  1347. * Memory image:
  1348. * struct dns_hash_entry (e)
  1349. * name (name_len+1 bytes) (&e->name[0])
  1350. * padding to multiple of sizeof(char*)
  1351. * dns_rr1 (e->rr_lst)
  1352. * possible padding: no padding for a_rdata or aaaa_rdata,
  1353. * multipe of sizeof(short) for srv_rdata,
  1354. * multiple of sizeof(long) for naptr_rdata and others
  1355. * dns_rr1->rdata (e->rr_lst->rdata)
  1356. * padding to multipe of sizeof long
  1357. * dns_rr2 (e->rr_lst->next)
  1358. * ....
  1359. *
  1360. */
  1361. /* compute size */
  1362. for(l=rd, i=0; l && (i<MAX_DNS_RECORDS); l=l->next, i++){
  1363. for (r=0; r<no_records; r++){
  1364. if ((l->type==rec[r].rd->type) &&
  1365. (l->name_len==rec[r].rd->name_len)
  1366. && (strncasecmp(l->name, rec[r].rd->name, l->name_len)==0)){
  1367. /* found */
  1368. goto found;
  1369. }
  1370. }
  1371. /* not found, create new */
  1372. if (no_records<MAX_DNS_RECORDS){
  1373. rec[r].rd=l;
  1374. rec[r].e=0;
  1375. rec[r].size=ROUND_POINTER(sizeof(struct dns_hash_entry)+
  1376. rec[r].rd->name_len-1+1);
  1377. no_records++;
  1378. }else{
  1379. LOG(L_ERR, "ERROR: dns_cache_mk_rd_entry2: too many records: %d\n",
  1380. no_records);
  1381. /* skip */
  1382. continue;
  1383. }
  1384. found:
  1385. rec_idx[i]=r;
  1386. switch(l->type){
  1387. case T_A:
  1388. /* no padding */
  1389. rec[r].size+=ROUND_POINTER(sizeof(struct dns_rr)+
  1390. sizeof(struct a_rdata));
  1391. break;
  1392. case T_AAAA:
  1393. /* no padding */
  1394. rec[r].size+=ROUND_POINTER(sizeof(struct dns_rr)+
  1395. sizeof(struct aaaa_rdata));
  1396. break;
  1397. case T_SRV:
  1398. /* padding to short */
  1399. rec[r].size+=ROUND_POINTER(ROUND_SHORT(sizeof(struct dns_rr))+
  1400. SRV_RDATA_SIZE(*(struct srv_rdata*)l->rdata));
  1401. break;
  1402. case T_NAPTR:
  1403. /* padding to char* */
  1404. rec[r].size+=ROUND_POINTER(ROUND_POINTER(
  1405. sizeof(struct dns_rr))+
  1406. NAPTR_RDATA_SIZE(*(struct naptr_rdata*)l->rdata));
  1407. break;
  1408. case T_CNAME:
  1409. /* no padding */
  1410. rec[r].size+=ROUND_POINTER(sizeof(struct dns_rr)+
  1411. CNAME_RDATA_SIZE(*(struct cname_rdata*)l->rdata));
  1412. break;
  1413. case T_TXT:
  1414. /* padding to char* (because of txt[]->cstr)*/
  1415. rec[r].size+=ROUND_POINTER(ROUND_POINTER(
  1416. sizeof(struct dns_rr))+
  1417. TXT_RDATA_SIZE(*(struct txt_rdata*)l->rdata));
  1418. break;
  1419. case T_EBL:
  1420. /* padding to char* (because of char* pointers)*/
  1421. rec[r].size+=ROUND_POINTER(ROUND_POINTER(
  1422. sizeof(struct dns_rr))+
  1423. EBL_RDATA_SIZE(*(struct ebl_rdata*)l->rdata));
  1424. break;
  1425. case T_PTR:
  1426. /* no padding */
  1427. rec[r].size+=ROUND_POINTER(sizeof(struct dns_rr)+
  1428. PTR_RDATA_SIZE(*(struct ptr_rdata*)l->rdata));
  1429. break;
  1430. default:
  1431. LOG(L_CRIT, "BUG: dns_cache_mk_rd_entry: type %d not "
  1432. "supported\n", l->type);
  1433. }
  1434. }
  1435. now=get_ticks_raw();
  1436. /* alloc & init the entries */
  1437. for (r=0; r<no_records; r++){
  1438. rec[r].e=shm_malloc(rec[r].size);
  1439. if (rec[r].e==0){
  1440. LOG(L_ERR, "ERROR: dns_cache_mk_rd_entry: out of memory\n");
  1441. goto error;
  1442. }
  1443. memset(rec[r].e, 0, rec[r].size); /* init with 0*/
  1444. rec[r].e->total_size=rec[r].size;
  1445. rec[r].e->name_len=rec[r].rd->name_len;
  1446. rec[r].e->type=rec[r].rd->type;
  1447. rec[r].e->last_used=now;
  1448. /* memset makes sure is 0-term. */
  1449. memcpy(rec[r].e->name, rec[r].rd->name, rec[r].rd->name_len);
  1450. rec[r].e->rr_lst=(struct dns_rr*)((char*)rec[r].e+
  1451. ROUND_POINTER(sizeof(struct dns_hash_entry)+rec[r].e->name_len
  1452. -1+1));
  1453. rec[r].tail_rr=&(rec[r].e->rr_lst);
  1454. rec[r].rr=rec[r].e->rr_lst;
  1455. rec[r].max_ttl=0;
  1456. /* link them in a list */
  1457. if (r==0){
  1458. clist_init(rec[r].e, next, prev);
  1459. }else{
  1460. clist_append(rec[0].e, rec[r].e, next, prev);
  1461. }
  1462. }
  1463. /* copy the actual data */
  1464. for(l=rd, i=0; l && (i<MAX_DNS_RECORDS); l=l->next, i++){
  1465. r=rec_idx[i];
  1466. ttl=FIX_TTL(l->ttl);
  1467. switch(l->type){
  1468. case T_A:
  1469. rec[r].rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1470. rec[r].max_ttl=MAX(rec[r].max_ttl, ttl);
  1471. rec[r].rr->rdata=(void*)((char*)rec[r].rr+
  1472. sizeof(struct dns_rr));
  1473. memcpy(rec[r].rr->rdata, l->rdata, sizeof(struct a_rdata));
  1474. rec[r].rr->next=(void*)((char*)rec[r].rr+
  1475. ROUND_POINTER(sizeof(struct dns_rr)+
  1476. sizeof(struct a_rdata)));
  1477. rec[r].tail_rr=&(rec[r].rr->next);
  1478. rec[r].rr=rec[r].rr->next;
  1479. break;
  1480. case T_AAAA:
  1481. rec[r].rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1482. rec[r].max_ttl=MAX(rec[r].max_ttl, ttl);
  1483. rec[r].rr->rdata=(void*)((char*)rec[r].rr+
  1484. sizeof(struct dns_rr));
  1485. memcpy(rec[r].rr->rdata, l->rdata, sizeof(struct aaaa_rdata));
  1486. rec[r].rr->next=(void*)((char*)rec[r].rr+
  1487. ROUND_POINTER(sizeof(struct dns_rr)+
  1488. sizeof(struct aaaa_rdata)));
  1489. rec[r].tail_rr=&(rec[r].rr->next);
  1490. rec[r].rr=rec[r].rr->next;
  1491. break;
  1492. case T_SRV:
  1493. rec[r].rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1494. rec[r].max_ttl=MAX(rec[r].max_ttl, ttl);
  1495. rec[r].rr->rdata=(void*)((char*)rec[r].rr+
  1496. ROUND_SHORT(sizeof(struct dns_rr)));
  1497. /* copy the whole srv_rdata block*/
  1498. memcpy(rec[r].rr->rdata, l->rdata,
  1499. SRV_RDATA_SIZE(*(struct srv_rdata*)l->rdata) );
  1500. rec[r].rr->next=(void*)((char*)rec[r].rr+
  1501. ROUND_POINTER( ROUND_SHORT(sizeof(struct dns_rr))+
  1502. SRV_RDATA_SIZE(
  1503. *(struct srv_rdata*)l->rdata)));
  1504. rec[r].tail_rr=&(rec[r].rr->next);
  1505. rec[r].rr=rec[r].rr->next;
  1506. break;
  1507. case T_NAPTR:
  1508. rec[r].rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1509. rec[r].max_ttl=MAX(rec[r].max_ttl, ttl);
  1510. rec[r].rr->rdata=(void*)((char*)rec[r].rr+
  1511. ROUND_POINTER(sizeof(struct dns_rr)));
  1512. /* copy the whole srv_rdata block*/
  1513. memcpy(rec[r].rr->rdata, l->rdata,
  1514. NAPTR_RDATA_SIZE(*(struct naptr_rdata*)l->rdata) );
  1515. /* adjust the string pointer */
  1516. ((struct naptr_rdata*)rec[r].rr->rdata)->flags=
  1517. translate_pointer((char*)rec[r].rr->rdata, (char*)l->rdata,
  1518. (((struct naptr_rdata*)l->rdata)->flags));
  1519. ((struct naptr_rdata*)rec[r].rr->rdata)->services=
  1520. translate_pointer((char*)rec[r].rr->rdata, (char*)l->rdata,
  1521. (((struct naptr_rdata*)l->rdata)->services));
  1522. ((struct naptr_rdata*)rec[r].rr->rdata)->regexp=
  1523. translate_pointer((char*)rec[r].rr->rdata, (char*)l->rdata,
  1524. (((struct naptr_rdata*)l->rdata)->regexp));
  1525. ((struct naptr_rdata*)rec[r].rr->rdata)->repl=
  1526. translate_pointer((char*)rec[r].rr->rdata, (char*)l->rdata,
  1527. (((struct naptr_rdata*)l->rdata)->repl));
  1528. rec[r].rr->next=(void*)((char*)rec[r].rr+
  1529. ROUND_POINTER(ROUND_POINTER(sizeof(struct dns_rr))+
  1530. NAPTR_RDATA_SIZE(
  1531. *(struct naptr_rdata*)l->rdata)));
  1532. rec[r].tail_rr=&(rec[r].rr->next);
  1533. rec[r].rr=rec[r].rr->next;
  1534. break;
  1535. case T_CNAME:
  1536. rec[r].rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1537. rec[r].max_ttl=MAX(rec[r].max_ttl, ttl);
  1538. rec[r].rr->rdata=(void*)((char*)rec[r].rr
  1539. +sizeof(struct dns_rr));
  1540. memcpy(rec[r].rr->rdata, l->rdata,
  1541. CNAME_RDATA_SIZE(*(struct cname_rdata*)l->rdata));
  1542. rec[r].rr->next=(void*)((char*)rec[r].rr+
  1543. ROUND_POINTER(sizeof(struct dns_rr)+
  1544. CNAME_RDATA_SIZE(*(struct cname_rdata*)l->rdata)));
  1545. rec[r].tail_rr=&(rec[r].rr->next);
  1546. rec[r].rr=rec[r].rr->next;
  1547. break;
  1548. case T_TXT:
  1549. rec[r].rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1550. rec[r].max_ttl=MAX(rec[r].max_ttl, ttl);
  1551. rec[r].rr->rdata=(void*)((char*)rec[r].rr+
  1552. ROUND_POINTER(sizeof(struct dns_rr)));
  1553. memcpy(rec[r].rr->rdata, l->rdata,
  1554. TXT_RDATA_SIZE(*(struct txt_rdata*)l->rdata));
  1555. /* adjust the string pointers */
  1556. for (j=0; j<((struct txt_rdata*)l->rdata)->cstr_no; j++){
  1557. ((struct txt_rdata*)rec[r].rr->rdata)->txt[j].cstr=
  1558. translate_pointer((char*)rec[r].rr->rdata,
  1559. (char*)l->rdata,
  1560. ((struct txt_rdata*)l->rdata)->txt[j].cstr);
  1561. }
  1562. rec[r].rr->next=(void*)((char*)rec[r].rr+
  1563. ROUND_POINTER(ROUND_POINTER(sizeof(struct dns_rr))+
  1564. TXT_RDATA_SIZE(*(struct txt_rdata*)l->rdata)));
  1565. rec[r].tail_rr=&(rec[r].rr->next);
  1566. rec[r].rr=rec[r].rr->next;
  1567. break;
  1568. case T_EBL:
  1569. rec[r].rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1570. rec[r].max_ttl=MAX(rec[r].max_ttl, ttl);
  1571. rec[r].rr->rdata=(void*)((char*)rec[r].rr+
  1572. ROUND_POINTER(sizeof(struct dns_rr)));
  1573. memcpy(rec[r].rr->rdata, l->rdata,
  1574. EBL_RDATA_SIZE(*(struct ebl_rdata*)l->rdata));
  1575. /* adjust the string pointers */
  1576. ((struct ebl_rdata*)rec[r].rr->rdata)->separator=
  1577. translate_pointer((char*)rec[r].rr->rdata,
  1578. (char*)l->rdata,
  1579. ((struct ebl_rdata*)l->rdata)->separator);
  1580. ((struct ebl_rdata*)rec[r].rr->rdata)->apex=
  1581. translate_pointer((char*)rec[r].rr->rdata,
  1582. (char*)l->rdata,
  1583. ((struct ebl_rdata*)l->rdata)->apex);
  1584. rec[r].rr->next=(void*)((char*)rec[r].rr+
  1585. ROUND_POINTER(ROUND_POINTER(sizeof(struct dns_rr))+
  1586. EBL_RDATA_SIZE(*(struct ebl_rdata*)l->rdata)));
  1587. rec[r].tail_rr=&(rec[r].rr->next);
  1588. rec[r].rr=rec[r].rr->next;
  1589. break;
  1590. case T_PTR:
  1591. rec[r].rr->expire=now+S_TO_TICKS(ttl); /* maximum expire */
  1592. rec[r].max_ttl=MAX(rec[r].max_ttl, ttl);
  1593. rec[r].rr->rdata=(void*)((char*)rec[r].rr
  1594. +sizeof(struct dns_rr));
  1595. memcpy(rec[r].rr->rdata, l->rdata,
  1596. PTR_RDATA_SIZE(*(struct ptr_rdata*)l->rdata));
  1597. rec[r].rr->next=(void*)((char*)rec[r].rr+
  1598. ROUND_POINTER(sizeof(struct dns_rr)+
  1599. PTR_RDATA_SIZE(*(struct ptr_rdata*)l->rdata)));
  1600. rec[r].tail_rr=&(rec[r].rr->next);
  1601. rec[r].rr=rec[r].rr->next;
  1602. break;
  1603. default:
  1604. /* do nothing */
  1605. ;
  1606. }
  1607. }
  1608. for (r=0; r<no_records; r++){
  1609. *rec[r].tail_rr=0; /* terminate the list */
  1610. rec[r].e->expire=now+S_TO_TICKS(rec[r].max_ttl);
  1611. }
  1612. return rec[0].e;
  1613. error:
  1614. for (r=0; r<no_records; r++){
  1615. dns_destroy_entry(rec[r].e);
  1616. }
  1617. return 0;
  1618. }
  1619. inline static struct dns_hash_entry* dns_get_entry(str* name, int type);
  1620. #define CACHE_RELEVANT_RECS_ONLY
  1621. #ifdef CACHE_RELEVANT_RECS_ONLY
  1622. /* internal only: gets related entries from a rdata list, appends them
  1623. * to e (list) and returns:
  1624. * - e if e is of the requested type
  1625. * - if e is a CNAME, tries to get to the end of the CNAME chain and returns
  1626. * the final entry if the types match or 0 if the chain is unfinished
  1627. * - 0 on error/not found
  1628. * records is modified (the used records are removed from the list and freed)
  1629. *
  1630. * WARNING: - records must be pkg_malloc'ed
  1631. * Notes: - if the return is 0 and e->type==T_CNAME, the list will contain
  1632. * the CNAME chain (the last element being the last CNAME)
  1633. * */
  1634. inline static struct dns_hash_entry* dns_get_related(struct dns_hash_entry* e,
  1635. int type,
  1636. struct rdata** records)
  1637. {
  1638. struct dns_hash_entry* ret;
  1639. struct dns_hash_entry* l;
  1640. struct dns_hash_entry* t;
  1641. struct dns_hash_entry* lst_end;
  1642. struct dns_rr* rr;
  1643. static int cname_chain_len=0;
  1644. str tmp;
  1645. ret=0;
  1646. l=e;
  1647. #ifdef DNS_CACHE_DEBUG
  1648. DBG("dns_get_related(%p (%.*s, %d), %d, *%p) (%d)\n", e,
  1649. e->name_len, e->name, e->type, type, *records, cname_chain_len);
  1650. #endif
  1651. clist_init(l, next, prev);
  1652. if (type==e->type){
  1653. ret=e;
  1654. switch(e->type){
  1655. case T_SRV:
  1656. for (rr=e->rr_lst; rr && *records; rr=rr->next){
  1657. tmp.s=((struct srv_rdata*)rr->rdata)->name;
  1658. tmp.len=((struct srv_rdata*)rr->rdata)->name_len;
  1659. if (!(dns_flags&DNS_IPV6_ONLY)){
  1660. t=dns_cache_mk_rd_entry(&tmp, T_A, records);
  1661. if (t){
  1662. if ((t->type==T_CNAME) && *records)
  1663. dns_get_related(t, T_A, records);
  1664. lst_end=t->prev; /* needed for clist_append*/
  1665. clist_append_sublist(l, t, lst_end, next, prev);
  1666. }
  1667. }
  1668. if (!(dns_flags&DNS_IPV4_ONLY)){
  1669. t=dns_cache_mk_rd_entry(&tmp, T_AAAA, records);
  1670. if (t){
  1671. if ((t->type==T_CNAME) && *records)
  1672. dns_get_related(t, T_AAAA, records);
  1673. lst_end=t->prev; /* needed for clist_append*/
  1674. clist_append_sublist(l, t, lst_end, next, prev);
  1675. }
  1676. }
  1677. }
  1678. break;
  1679. #ifdef USE_NAPTR
  1680. case T_NAPTR:
  1681. #ifdef NAPTR_CACHE_ALL_ARS
  1682. if (*records)
  1683. dns_cache_mk_rd_entry2(*records);
  1684. #else
  1685. for (rr=e->rr_lst; rr && *records; rr=rr->next){
  1686. if (naptr_get_sip_proto((struct naptr_rdata*)rr->rdata)>0){
  1687. tmp.s=((struct naptr_rdata*)rr->rdata)->repl;
  1688. tmp.len=((struct naptr_rdata*)rr->rdata)->repl_len;
  1689. t=dns_cache_mk_rd_entry(&tmp, T_SRV, records);
  1690. if (t){
  1691. if (*records)
  1692. dns_get_related(t, T_SRV, records);
  1693. lst_end=t->prev; /* needed for clist_append*/
  1694. clist_append_sublist(l, t, lst_end, next, prev);
  1695. }
  1696. }
  1697. }
  1698. #endif /* NAPTR_CACHE_ALL_ARS */
  1699. #endif /* USE_NAPTR */
  1700. break;
  1701. default:
  1702. /* nothing extra */
  1703. break;
  1704. }
  1705. }else if ((e->type==T_CNAME) && (cname_chain_len<MAX_CNAME_CHAIN)){
  1706. /* only one cname is allowed (rfc2181), so we ignore
  1707. * the others (we take only the first one) */
  1708. tmp.s=((struct cname_rdata*)e->rr_lst->rdata)->name;
  1709. tmp.len=((struct cname_rdata*)e->rr_lst->rdata)->name_len;
  1710. t=dns_cache_mk_rd_entry(&tmp, type, records);
  1711. if (t){
  1712. if (*records){
  1713. cname_chain_len++;
  1714. ret=dns_get_related(t, type, records);
  1715. cname_chain_len--;
  1716. lst_end=t->prev;
  1717. clist_append_sublist(l, t, lst_end, next, prev);
  1718. }else{
  1719. /* if no more recs, but we found the orig. target anyway,
  1720. * return it (e.g. recs are only CNAME x & x A 1.2.3.4 or
  1721. * CNAME & SRV) */
  1722. if (t->type==type)
  1723. ret=t;
  1724. clist_append(l, t, next, prev);
  1725. }
  1726. }
  1727. }
  1728. return ret;
  1729. }
  1730. #endif
  1731. /* calls the external resolver and populates the cache with the result
  1732. * returns: 0 on error, pointer to hash entry on success
  1733. * WARNING: make sure you use dns_hash_entry_put() when you're
  1734. * finished with the result)
  1735. * */
  1736. inline static struct dns_hash_entry* dns_cache_do_request(str* name, int type)
  1737. {
  1738. struct rdata* records;
  1739. struct dns_hash_entry* e;
  1740. struct dns_hash_entry* l;
  1741. struct dns_hash_entry* r;
  1742. struct dns_hash_entry* t;
  1743. struct ip_addr* ip;
  1744. str cname_val;
  1745. char name_buf[MAX_DNS_NAME];
  1746. struct dns_hash_entry* old;
  1747. str rec_name;
  1748. int add_record, h, err;
  1749. e=0;
  1750. l=0;
  1751. cname_val.s=0;
  1752. old = NULL;
  1753. #ifdef USE_DNS_CACHE_STATS
  1754. if (dns_cache_stats)
  1755. dns_cache_stats[process_no].dns_req_cnt++;
  1756. #endif /* USE_DNS_CACHE_STATS */
  1757. if (type==T_A){
  1758. #ifdef USE_IPV6
  1759. if (str2ip6(name)!=0)
  1760. goto end;
  1761. #endif /* USE_IPV6 */
  1762. if ((ip=str2ip(name))!=0){
  1763. e=dns_cache_mk_ip_entry(name, ip);
  1764. if (likely(e))
  1765. atomic_set(&e->refcnt, 1);/* because we ret. a ref. to it*/
  1766. goto end; /* we do not cache obvious stuff */
  1767. }
  1768. }
  1769. #ifdef USE_IPV6
  1770. else if (type==T_AAAA){
  1771. if (str2ip(name)!=0)
  1772. goto end;
  1773. if ((ip=str2ip6(name))!=0){
  1774. e=dns_cache_mk_ip_entry(name, ip);
  1775. if (likely(e))
  1776. atomic_set(&e->refcnt, 1);/* because we ret. a ref. to it*/
  1777. goto end;/* we do not cache obvious stuff */
  1778. }
  1779. }
  1780. #endif /* USE_IPV6 */
  1781. #ifdef DNS_WATCHDOG_SUPPORT
  1782. if (atomic_get(dns_servers_up)==0)
  1783. goto end; /* the servers are down, needless to perform the query */
  1784. #endif
  1785. if (name->len>=MAX_DNS_NAME){
  1786. LOG(L_ERR, "ERROR: dns_cache_do_request: name too long (%d chars)\n",
  1787. name->len);
  1788. goto end;
  1789. }
  1790. /* null terminate the string, needed by get_record */
  1791. memcpy(name_buf, name->s, name->len);
  1792. name_buf[name->len]=0;
  1793. records=get_record(name_buf, type, RES_AR);
  1794. if (records){
  1795. #ifdef CACHE_RELEVANT_RECS_ONLY
  1796. e=dns_cache_mk_rd_entry(name, type, &records);
  1797. if (likely(e)){
  1798. l=e;
  1799. e=dns_get_related(l, type, &records);
  1800. /* e should contain the searched entry (if found) and l
  1801. * all the entries (e and related) */
  1802. if (likely(e)){
  1803. atomic_set(&e->refcnt, 1); /* 1 because we return a
  1804. ref. to it */
  1805. }else{
  1806. /* e==0 => l contains a cname list => we use the last
  1807. * cname from the chain for a new resolve attempt (l->prev) */
  1808. /* only one cname record is allowed (rfc2181), so we ignore
  1809. * the others (we take only the first one) */
  1810. cname_val.s=
  1811. ((struct cname_rdata*)l->prev->rr_lst->rdata)->name;
  1812. cname_val.len=
  1813. ((struct cname_rdata*)l->prev->rr_lst->rdata)->name_len;
  1814. DBG("dns_cache_do_request: cname detected: %.*s (%d)\n",
  1815. cname_val.len, cname_val.s, cname_val.len);
  1816. }
  1817. /* add all the records to the hash */
  1818. l->prev->next=0; /* we break the double linked list for easier
  1819. searching */
  1820. LOCK_DNS_HASH(); /* optimization */
  1821. for (r=l; r; r=t){
  1822. t=r->next;
  1823. /* add the new record to the cache by default */
  1824. add_record = 1;
  1825. if (cfg_get(core, core_cfg, dns_cache_rec_pref) > 0) {
  1826. /* check whether there is an old record with the
  1827. * same type in the cache */
  1828. rec_name.s = r->name;
  1829. rec_name.len = r->name_len;
  1830. old = _dns_hash_find(&rec_name, r->type, &h, &err);
  1831. if (old) {
  1832. if (old->type != r->type) {
  1833. /* probably CNAME found */
  1834. old = NULL;
  1835. } else if (old->ent_flags & DNS_FLAG_PERMANENT) {
  1836. /* never overwrite permanent entries */
  1837. add_record = 0;
  1838. } else if ((old->ent_flags & DNS_FLAG_BAD_NAME) == 0) {
  1839. /* Non-negative, non-permanent entry found with
  1840. * the same type. */
  1841. add_record =
  1842. /* prefer new records */
  1843. ((cfg_get(core, core_cfg, dns_cache_rec_pref) == 2)
  1844. /* prefer the record with the longer lifetime */
  1845. || ((cfg_get(core, core_cfg, dns_cache_rec_pref) == 3)
  1846. && TICKS_LT(old->expire, r->expire)));
  1847. }
  1848. }
  1849. }
  1850. if (add_record) {
  1851. dns_cache_add_unsafe(r); /* refcnt++ inside */
  1852. if (atomic_get(&r->refcnt)==0){
  1853. /* if cache adding failed and nobody else is interested
  1854. * destroy this entry */
  1855. dns_destroy_entry(r);
  1856. }
  1857. if (old) {
  1858. _dns_hash_remove(old);
  1859. old = NULL;
  1860. }
  1861. } else {
  1862. if (old) {
  1863. if (r == e) {
  1864. /* this entry has to be returned */
  1865. e = old;
  1866. atomic_inc(&e->refcnt);
  1867. }
  1868. old = NULL;
  1869. }
  1870. dns_destroy_entry(r);
  1871. }
  1872. }
  1873. UNLOCK_DNS_HASH();
  1874. /* if only cnames found => try to resolve the last one */
  1875. if (cname_val.s){
  1876. DBG("dns_cache_do_request: dns_get_entry(cname: %.*s (%d))\n",
  1877. cname_val.len, cname_val.s, cname_val.len);
  1878. e=dns_get_entry(&cname_val, type);
  1879. }
  1880. }
  1881. #else
  1882. l=dns_cache_mk_rd_entry2(records);
  1883. #endif
  1884. free_rdata_list(records);
  1885. }else if (cfg_get(core, core_cfg, dns_neg_cache_ttl)){
  1886. e=dns_cache_mk_bad_entry(name, type,
  1887. cfg_get(core, core_cfg, dns_neg_cache_ttl), DNS_FLAG_BAD_NAME);
  1888. if (likely(e)) {
  1889. atomic_set(&e->refcnt, 1); /* 1 because we return a ref. to it */
  1890. dns_cache_add(e); /* refcnt++ inside*/
  1891. }
  1892. goto end;
  1893. }
  1894. #ifndef CACHE_RELEVANT_RECS_ONLY
  1895. if (l){
  1896. /* add all the records to the cache, but return only the record
  1897. * we are looking for */
  1898. l->prev->next=0; /* we break the double linked list for easier
  1899. searching */
  1900. LOCK_DNS_HASH(); /* optimization */
  1901. for (r=l; r; r=t){
  1902. t=r->next;
  1903. if (e==0){ /* no entry found yet */
  1904. if (r->type==T_CNAME){
  1905. if ((r->name_len==name->len) && (r->rr_lst) &&
  1906. (strncasecmp(r->name, name->s, name->len)==0)){
  1907. /* update the name with the name from the cname rec. */
  1908. cname_val.s=
  1909. ((struct cname_rdata*)r->rr_lst->rdata)->name;
  1910. cname_val.len=
  1911. ((struct cname_rdata*)r->rr_lst->rdata)->name_len;
  1912. name=&cname_val;
  1913. }
  1914. }else if ((r->type==type) && (r->name_len==name->len) &&
  1915. (strncasecmp(r->name, name->s, name->len)==0)){
  1916. e=r;
  1917. atomic_set(&e->refcnt, 1); /* 1 because we return a ref.
  1918. to it */
  1919. }
  1920. }
  1921. /* add the new record to the cache by default */
  1922. add_record = 1;
  1923. if (cfg_get(core, core_cfg, dns_cache_rec_pref) > 0) {
  1924. /* check whether there is an old record with the
  1925. * same type in the cache */
  1926. rec_name.s = r->name;
  1927. rec_name.len = r->name_len;
  1928. old = _dns_hash_find(&rec_name, r->type, &h, &err);
  1929. if (old) {
  1930. if (old->type != r->type) {
  1931. /* probably CNAME found */
  1932. old = NULL;
  1933. } else if (old->ent_flags & DNS_FLAG_PERMANENT) {
  1934. /* never overwrite permanent entries */
  1935. add_record = 0;
  1936. } else if ((old->ent_flags & DNS_FLAG_BAD_NAME) == 0) {
  1937. /* Non-negative, non-permanent entry found with
  1938. * the same type. */
  1939. add_record =
  1940. /* prefer new records */
  1941. ((cfg_get(core, core_cfg, dns_cache_rec_pref) == 2)
  1942. /* prefer the record with the longer lifetime */
  1943. || ((cfg_get(core, core_cfg, dns_cache_rec_pref) == 3)
  1944. && TICKS_LT(old->expire, r->expire)));
  1945. }
  1946. }
  1947. }
  1948. if (add_record) {
  1949. dns_cache_add_unsafe(r); /* refcnt++ inside */
  1950. if (atomic_get(&r->refcnt)==0){
  1951. /* if cache adding failed and nobody else is interested
  1952. * destroy this entry */
  1953. dns_destroy_entry(r);
  1954. }
  1955. if (old) {
  1956. _dns_hash_remove(old);
  1957. old = NULL;
  1958. }
  1959. } else {
  1960. if (old) {
  1961. if (r == e) {
  1962. /* this entry has to be returned */
  1963. e = old;
  1964. atomic_inc(&e->refcnt);
  1965. }
  1966. old = NULL;
  1967. }
  1968. dns_destroy_entry(r);
  1969. }
  1970. }
  1971. UNLOCK_DNS_HASH();
  1972. if ((e==0) && (cname_val.s)){ /* not found, but found a cname */
  1973. /* only one cname is allowed (rfc2181), so we ignore the
  1974. * others (we take only the first one) */
  1975. e=dns_get_entry(&cname_val, type);
  1976. }
  1977. }
  1978. #endif
  1979. end:
  1980. return e;
  1981. }
  1982. /* tries to lookup (name, type) in the hash and if not found tries to make
  1983. * a dns request
  1984. * return: 0 on error, pointer to a dns_hash_entry on success
  1985. * WARNING: when not needed anymore dns_hash_put() must be called! */
  1986. inline static struct dns_hash_entry* dns_get_entry(str* name, int type)
  1987. {
  1988. int h;
  1989. struct dns_hash_entry* e;
  1990. str cname_val;
  1991. int err;
  1992. static int rec_cnt=0; /* recursion protection */
  1993. e=0;
  1994. if (rec_cnt>MAX_CNAME_CHAIN){
  1995. LOG(L_WARN, "WARNING: dns_get_entry: CNAME chain too long or"
  1996. " recursive CNAMEs (\"%.*s\")\n", name->len, name->s);
  1997. goto error;
  1998. }
  1999. rec_cnt++;
  2000. e=dns_hash_get(name, type, &h, &err);
  2001. #ifdef USE_DNS_CACHE_STATS
  2002. if (e) {
  2003. if ((e->ent_flags & DNS_FLAG_BAD_NAME) && dns_cache_stats)
  2004. /* negative DNS cache hit */
  2005. dns_cache_stats[process_no].dc_neg_hits_cnt++;
  2006. else if (((e->ent_flags & DNS_FLAG_BAD_NAME) == 0)
  2007. && dns_cache_stats
  2008. ) /* DNS cache hit */
  2009. dns_cache_stats[process_no].dc_hits_cnt++;
  2010. if (dns_cache_stats)
  2011. dns_cache_stats[process_no].dns_req_cnt++;
  2012. }
  2013. #endif /* USE_DNS_CACHE_STATS */
  2014. if ((e==0) && ((err) || ((e=dns_cache_do_request(name, type))==0))){
  2015. goto error;
  2016. }else if ((e->type==T_CNAME) && (type!=T_CNAME)){
  2017. /* cname found instead which couldn't be resolved with the cached
  2018. * info => try a dns request */
  2019. /* only one cname record is allowed (rfc2181), so we ignore
  2020. * the others (we take only the first one) */
  2021. cname_val.s= ((struct cname_rdata*)e->rr_lst->rdata)->name;
  2022. cname_val.len=((struct cname_rdata*)e->rr_lst->rdata)->name_len;
  2023. dns_hash_put(e); /* not interested in the cname anymore */
  2024. if ((e=dns_cache_do_request(&cname_val, type))==0)
  2025. goto error; /* could not resolve cname */
  2026. }
  2027. /* found */
  2028. if ((e->rr_lst==0) || (e->ent_flags & DNS_FLAG_BAD_NAME)){
  2029. /* negative cache => not resolvable */
  2030. dns_hash_put(e);
  2031. e=0;
  2032. }
  2033. error:
  2034. rec_cnt--;
  2035. return e;
  2036. }
  2037. /* gets the first non-expired record starting with record no
  2038. * from the dns_hash_entry struct e
  2039. * params: e - dns_hash_entry struct
  2040. * *no - it must contain the start record number (0 initially);
  2041. * it will be filled with the returned record number
  2042. * now - current time/ticks value
  2043. * returns pointer to the rr on success and sets no to the rr number
  2044. * 0 on error and fills the error flags
  2045. *
  2046. * Example usage:
  2047. * list all non-expired non-bad-marked ips for name:
  2048. * e=dns_get_entry(name, T_A);
  2049. * if (e){
  2050. * *no=0;
  2051. * now=get_ticks_raw();
  2052. * while(rr=dns_entry_get_rr(e, no, now){
  2053. * DBG("address %d\n", *no);
  2054. * *no++; ( get the next address next time )
  2055. * }
  2056. * }
  2057. */
  2058. inline static struct dns_rr* dns_entry_get_rr( struct dns_hash_entry* e,
  2059. unsigned char* no, ticks_t now)
  2060. {
  2061. struct dns_rr* rr;
  2062. int n;
  2063. #ifdef DNS_WATCHDOG_SUPPORT
  2064. int servers_up;
  2065. servers_up = atomic_get(dns_servers_up);
  2066. #endif
  2067. for(rr=e->rr_lst, n=0;rr && (n<*no);rr=rr->next, n++);/* skip *no records*/
  2068. for(;rr;rr=rr->next){
  2069. if (
  2070. #ifdef DNS_WATCHDOG_SUPPORT
  2071. /* check the expiration time only when the servers are up */
  2072. servers_up &&
  2073. #endif
  2074. ((e->ent_flags & DNS_FLAG_PERMANENT) == 0) &&
  2075. ((s_ticks_t)(now-rr->expire)>=0) /* expired rr */
  2076. )
  2077. continue;
  2078. /* everything is ok now */
  2079. *no=n;
  2080. return rr;
  2081. }
  2082. *no=n;
  2083. return 0;
  2084. }
  2085. #ifdef DNS_SRV_LB
  2086. #define srv_reset_tried(p) (*(p)=0)
  2087. #define srv_marked(p, i) (*(p)&(1UL<<(i)))
  2088. #define srv_mark_tried(p, i) \
  2089. do{ \
  2090. (*(p)|=(1UL<<(i))); \
  2091. }while(0)
  2092. #define srv_next_rr(n, f, i) srv_mark_tried(f, i)
  2093. /* returns a random number between 0 and max inclusive (0<=r<=max) */
  2094. inline static unsigned dns_srv_random(unsigned max)
  2095. {
  2096. return fastrand_max(max);
  2097. }
  2098. /* for a SRV record it will return the next entry to be tried according
  2099. * to the RFC2782 server selection mechanism
  2100. * params:
  2101. * e is a dns srv hash entry
  2102. * no is the start index of the current group (a group is a set of SRV
  2103. * rrs with the same priority)
  2104. * tried is a bitmap where the tried srv rrs of the same priority are
  2105. * marked
  2106. * now - current time/ticks value
  2107. * returns pointer to the rr on success and sets no to the rr number
  2108. * 0 on error and fills the error flags
  2109. * WARNING: unlike dns_entry_get_rr() this will always return another
  2110. * another rr automatically (*no must not be incremented)
  2111. *
  2112. * Example usage:
  2113. * list all non-expired, non-bad-marked, never tried before srv records
  2114. * using the rfc2782 algo:
  2115. * e=dns_get_entry(name, T_SRV);
  2116. * if (e){
  2117. * no=0;
  2118. * srv_reset_tried(&tried);
  2119. * now=get_ticks_raw();
  2120. * while(rr=dns_srv_get_nxt_rr(e, &tried, &no, now){
  2121. * DBG("address %d\n", *no);
  2122. * }
  2123. * }
  2124. *
  2125. */
  2126. inline static struct dns_rr* dns_srv_get_nxt_rr(struct dns_hash_entry* e,
  2127. srv_flags_t* tried,
  2128. unsigned char* no, ticks_t now)
  2129. {
  2130. #define MAX_SRV_GRP_IDX (sizeof(srv_flags_t)*8)
  2131. struct dns_rr* rr;
  2132. struct dns_rr* start_grp;
  2133. int n;
  2134. unsigned sum;
  2135. unsigned prio;
  2136. unsigned rand_w;
  2137. int found;
  2138. int saved_idx;
  2139. int zero_weight; /* number of records with 0 weight */
  2140. int i, idx;
  2141. struct r_sums_entry{
  2142. unsigned r_sum;
  2143. struct dns_rr* rr;
  2144. }r_sums[MAX_SRV_GRP_IDX];
  2145. #ifdef DNS_WATCHDOG_SUPPORT
  2146. int servers_up;
  2147. servers_up = atomic_get(dns_servers_up);
  2148. #endif
  2149. memset(r_sums, 0, sizeof(struct r_sums_entry) * MAX_SRV_GRP_IDX);
  2150. rand_w=0;
  2151. for(rr=e->rr_lst, n=0;rr && (n<*no);rr=rr->next, n++);/* skip *no records*/
  2152. retry:
  2153. if (unlikely(rr==0))
  2154. goto no_more_rrs;
  2155. start_grp=rr;
  2156. prio=((struct srv_rdata*)start_grp->rdata)->priority;
  2157. sum=0;
  2158. saved_idx=-1;
  2159. zero_weight = 0;
  2160. found=0;
  2161. for (idx=0;rr && (prio==((struct srv_rdata*)rr->rdata)->priority) &&
  2162. (idx < MAX_SRV_GRP_IDX); idx++, rr=rr->next){
  2163. if ((
  2164. #ifdef DNS_WATCHDOG_SUPPORT
  2165. /* check the expiration time only when the servers are up */
  2166. servers_up &&
  2167. #endif
  2168. ((e->ent_flags & DNS_FLAG_PERMANENT) == 0) &&
  2169. ((s_ticks_t)(now-rr->expire)>=0) /* expired entry */) ||
  2170. (srv_marked(tried, idx)) ) /* already tried */{
  2171. r_sums[idx].r_sum=0; /* 0 sum, to skip over it */
  2172. r_sums[idx].rr=0; /* debug: mark it as unused */
  2173. continue;
  2174. }
  2175. /* special case, 0 weight records should be "first":
  2176. * remember the first rr int the "virtual" list: A 0 weight must
  2177. * come first if present, else get the first one */
  2178. if ((saved_idx==-1) || (((struct srv_rdata*)rr->rdata)->weight==0)){
  2179. saved_idx=idx;
  2180. }
  2181. zero_weight += (((struct srv_rdata*)rr->rdata)->weight == 0);
  2182. sum+=((struct srv_rdata*)rr->rdata)->weight;
  2183. r_sums[idx].r_sum=sum;
  2184. r_sums[idx].rr=rr;
  2185. found++;
  2186. }
  2187. if (found==0){
  2188. /* try in the next priority group */
  2189. n+=idx; /* next group start idx, last rr */
  2190. srv_reset_tried(tried);
  2191. goto retry;
  2192. }else if ((found==1) || (sum==0) ||
  2193. (((rand_w=(dns_srv_random(sum-1)+1))==1) && zero_weight &&
  2194. (dns_srv_random(DNS_SRV_ZERO_W_CHANCE)==0))){
  2195. /* 1. if only one found, avoid a useless random() call
  2196. and select it (saved_idx will point to it).
  2197. * 2. if the sum of weights is 0 (all have 0 weight) or
  2198. * 3. rand_w==1 and there are records with 0 weight and
  2199. * random(probab. of selecting a 0-weight)
  2200. * immediately select a 0 weight record.
  2201. * (this takes care of the 0-weight at the beginning requirement) */
  2202. i=saved_idx; /* saved idx contains either first 0 weight or first
  2203. valid record */
  2204. goto found;
  2205. }
  2206. /* if we are here => rand_w is not 0 and we have at least 2 valid options
  2207. * => we can safely iterate on the whole r_sums[] whithout any other
  2208. * extra checks */
  2209. for (i=0; (i<idx) && (r_sums[i].r_sum<rand_w); i++);
  2210. found:
  2211. #ifdef DNS_CACHE_DEBUG
  2212. DBG("dns_srv_get_nxt_rr(%p, %lx, %d, %u): selected %d/%d in grp. %d"
  2213. " (rand_w=%d, rr=%p p=%d w=%d rsum=%d)\n",
  2214. e, (unsigned long)*tried, *no, now, i, idx, n, rand_w, r_sums[i].rr,
  2215. ((struct srv_rdata*)r_sums[i].rr->rdata)->priority,
  2216. ((struct srv_rdata*)r_sums[i].rr->rdata)->weight, r_sums[i].r_sum);
  2217. #endif
  2218. /* i is the winner */
  2219. *no=n; /* grp. start */
  2220. srv_mark_tried(tried, i); /* mark it */
  2221. return r_sums[i].rr;
  2222. no_more_rrs:
  2223. *no=n;
  2224. return 0;
  2225. }
  2226. #endif /* DNS_SRV_LB */
  2227. /* gethostbyname compatibility: converts a dns_hash_entry structure
  2228. * to a statical internal hostent structure
  2229. * returns a pointer to the internal hostent structure on success or
  2230. * 0 on error
  2231. */
  2232. inline static struct hostent* dns_entry2he(struct dns_hash_entry* e)
  2233. {
  2234. static struct hostent he;
  2235. static char hostname[256];
  2236. static char* p_aliases[1];
  2237. static char* p_addr[DNS_HE_MAX_ADDR+1];
  2238. static char address[16*DNS_HE_MAX_ADDR]; /* max 10 ipv6 addresses */
  2239. int af, len;
  2240. struct dns_rr* rr;
  2241. unsigned char rr_no;
  2242. ticks_t now;
  2243. int i;
  2244. switch(e->type){
  2245. case T_A:
  2246. af=AF_INET;
  2247. len=4;
  2248. break;
  2249. case T_AAAA:
  2250. #ifdef USE_IPV6
  2251. af=AF_INET6;
  2252. len=16;
  2253. break;
  2254. #else /* USE_IPV6 */
  2255. LOG(L_ERR, "ERROR: dns_entry2he: IPv6 dns cache entry, but "
  2256. "IPv6 support disabled at compile time"
  2257. " (recompile with -DUSE_IPV6)\n");
  2258. return 0;
  2259. #endif /* USE_IPV6 */
  2260. default:
  2261. LOG(L_CRIT, "BUG: dns_entry2he: wrong entry type %d for %.*s\n",
  2262. e->type, e->name_len, e->name);
  2263. return 0;
  2264. }
  2265. rr_no=0;
  2266. now=get_ticks_raw();
  2267. /* if the entry has already expired use the time at the end of lifetime */
  2268. if (unlikely((s_ticks_t)(now-e->expire)>=0)) now=e->expire-1;
  2269. rr=dns_entry_get_rr(e, &rr_no, now);
  2270. for(i=0; rr && (i<DNS_HE_MAX_ADDR); i++,
  2271. rr=dns_entry_get_rr(e, &rr_no, now)){
  2272. p_addr[i]=&address[i*len];
  2273. memcpy(p_addr[i], ((struct a_rdata*)rr->rdata)->ip, len);
  2274. }
  2275. if (i==0){
  2276. DBG("DEBUG: dns_entry2he: no good records found (%d) for %.*s (%d)\n",
  2277. rr_no, e->name_len, e->name, e->type);
  2278. return 0; /* no good record found */
  2279. }
  2280. p_addr[i]=0; /* mark the end of the addresses */
  2281. p_aliases[0]=0; /* no aliases */
  2282. memcpy(hostname, e->name, e->name_len);
  2283. hostname[e->name_len]=0;
  2284. he.h_addrtype=af;
  2285. he.h_length=len;
  2286. he.h_addr_list=p_addr;
  2287. he.h_aliases=p_aliases;
  2288. he.h_name=hostname;
  2289. return &he;
  2290. }
  2291. /* gethostbyname compatibility: performs an a_lookup and returns a pointer
  2292. * to a statical internal hostent structure
  2293. * returns 0 on success, <0 on error (see the error codes)
  2294. */
  2295. inline static struct hostent* dns_a_get_he(str* name)
  2296. {
  2297. struct dns_hash_entry* e;
  2298. struct ip_addr* ip;
  2299. struct hostent* he;
  2300. e=0;
  2301. #ifdef USE_IPV6
  2302. if (str2ip6(name)!=0)
  2303. return 0;
  2304. #endif
  2305. if ((ip=str2ip(name))!=0){
  2306. return ip_addr2he(name, ip);
  2307. }
  2308. if ((e=dns_get_entry(name, T_A))==0)
  2309. return 0;
  2310. /* found */
  2311. he=dns_entry2he(e);
  2312. dns_hash_put(e);
  2313. return he;
  2314. }
  2315. #ifdef USE_IPV6
  2316. /* gethostbyname compatibility: performs an aaaa_lookup and returns a pointer
  2317. * to a statical internal hostent structure
  2318. * returns 0 on success, <0 on error (see the error codes)
  2319. */
  2320. inline static struct hostent* dns_aaaa_get_he(str* name)
  2321. {
  2322. struct dns_hash_entry* e;
  2323. struct ip_addr* ip;
  2324. struct hostent* he;
  2325. e=0;
  2326. if (str2ip(name)!=0)
  2327. return 0;
  2328. if ((ip=str2ip6(name))!=0){
  2329. return ip_addr2he(name, ip);
  2330. }
  2331. if ((e=dns_get_entry(name, T_AAAA))==0)
  2332. return 0;
  2333. /* found */
  2334. he=dns_entry2he(e);
  2335. dns_hash_put(e);
  2336. return he;
  2337. }
  2338. #endif
  2339. /* returns 0 on success, -1 on error (rr type does not contain an ip) */
  2340. inline static int dns_rr2ip(int type, struct dns_rr* rr, struct ip_addr* ip)
  2341. {
  2342. switch(type){
  2343. case T_A:
  2344. ip->af=AF_INET;
  2345. ip->len=4;
  2346. memcpy(ip->u.addr, ((struct a_rdata*)rr->rdata)->ip, 4);
  2347. return 0;
  2348. break;
  2349. case T_AAAA:
  2350. #ifdef USE_IPV6
  2351. ip->af=AF_INET6;
  2352. ip->len=16;
  2353. memcpy(ip->u.addr, ((struct aaaa_rdata*)rr->rdata)->ip6, 16);
  2354. return 0;
  2355. #else /* USE_IPV6 */
  2356. LOG(L_ERR, "ERROR: dns_rr2ip: IPv6 dns rr, but IPv6 support"
  2357. "disabled at compile time (recompile with "
  2358. "-DUSE_IPV6)\n" );
  2359. #endif /*USE_IPV6 */
  2360. break;
  2361. }
  2362. return -1;
  2363. }
  2364. /* gethostbyname compatibility:
  2365. * performs an a or aaaa dns lookup, returns 0 on error and a pointer to a
  2366. * static hostent structure on success
  2367. * flags: - none set: tries first an a_lookup and if it fails an aaaa_lookup
  2368. * - DNS_IPV6_FIRST: tries first an aaaa_lookup and then an a_lookup
  2369. * - DNS_IPV4_ONLY: tries only an a_lookup
  2370. * - DNS_IPV6_ONLY: tries only an aaaa_lookup
  2371. */
  2372. struct hostent* dns_get_he(str* name, int flags)
  2373. {
  2374. #ifdef USE_IPV6
  2375. struct hostent* he;
  2376. if ((flags&(DNS_IPV6_FIRST|DNS_IPV6_ONLY))){
  2377. he=dns_aaaa_get_he(name);
  2378. if (he) return he;
  2379. }else{
  2380. he=dns_a_get_he(name);
  2381. if (he) return he;
  2382. }
  2383. if (flags&DNS_IPV6_FIRST){
  2384. he=dns_a_get_he(name);
  2385. }else if (!(flags&(DNS_IPV6_ONLY|DNS_IPV4_ONLY))){
  2386. he=dns_aaaa_get_he(name);
  2387. }
  2388. return he;
  2389. #else /* USE_IPV6 */
  2390. return dns_a_get_he(name);
  2391. #endif /* USE_IPV6 */
  2392. }
  2393. /* sip_resolvehost helper: gets the first good hostent/port combination
  2394. * returns 0 on error, pointer to static hostent structure on success
  2395. * (and sets port)*/
  2396. struct hostent* dns_srv_get_he(str* name, unsigned short* port, int flags)
  2397. {
  2398. struct dns_hash_entry* e;
  2399. struct dns_rr* rr;
  2400. str rr_name;
  2401. struct hostent* he;
  2402. ticks_t now;
  2403. unsigned char rr_no;
  2404. rr=0;
  2405. he=0;
  2406. now=get_ticks_raw();
  2407. if ((e=dns_get_entry(name, T_SRV))==0)
  2408. goto error;
  2409. /* look inside the RRs for a good one (not expired or marked bad) */
  2410. rr_no=0;
  2411. while( (rr=dns_entry_get_rr(e, &rr_no, now))!=0){
  2412. /* everything is ok now, we can try to resolve the ip */
  2413. rr_name.s=((struct srv_rdata*)rr->rdata)->name;
  2414. rr_name.len=((struct srv_rdata*)rr->rdata)->name_len;
  2415. if ((he=dns_get_he(&rr_name, flags))!=0){
  2416. /* success, at least one good ip found */
  2417. *port=((struct srv_rdata*)rr->rdata)->port;
  2418. goto end;
  2419. }
  2420. rr_no++; /* try from the next record, the current one was not good */
  2421. }
  2422. /* if we reach this point => error, we couldn't find any good rr */
  2423. end:
  2424. if (e) dns_hash_put(e);
  2425. error:
  2426. return he;
  2427. }
  2428. struct hostent* dns_resolvehost(char* name)
  2429. {
  2430. str host;
  2431. struct hostent* ret;
  2432. if ((cfg_get(core, core_cfg, use_dns_cache)==0) || (dns_hash==0)){ /* not init yet */
  2433. ret = _resolvehost(name);
  2434. if(unlikely(!ret)){
  2435. /* increment dns error counter */
  2436. counter_inc(dns_cnts_h.failed_dns_req);
  2437. }
  2438. return ret;
  2439. }
  2440. host.s=name;
  2441. host.len=strlen(name);
  2442. return dns_get_he(&host, dns_flags);
  2443. }
  2444. #if 0
  2445. /* resolves a host name trying NAPTR, SRV, A & AAAA lookups, for details
  2446. * see dns_sip_resolve()
  2447. * FIXME: this version will return only the first ip
  2448. * returns: hostent struct & *port filled with the port from the SRV record;
  2449. * 0 on error
  2450. */
  2451. struct hostent* dns_sip_resolvehost(str* name, unsigned short* port,
  2452. char* proto)
  2453. {
  2454. struct dns_srv_handle h;
  2455. struct ip_addr ip;
  2456. int ret;
  2457. if ((cfg_get(core, core_cfg, use_dns_cache==0)) || (dns_hash==0)){
  2458. /* not init or off => use normal, non-cached version */
  2459. return _sip_resolvehost(name, port, proto);
  2460. }
  2461. dns_srv_handle_init(&h);
  2462. ret=dns_sip_resolve(&h, name, &ip, port, proto, dns_flags);
  2463. dns_srv_handle_put(&h);
  2464. if (ret>=0)
  2465. return ip_addr2he(name, &ip);
  2466. return 0;
  2467. }
  2468. #endif
  2469. /* resolves a host name trying SRV lookup if *port==0 or normal A/AAAA lookup
  2470. * if *port!=0.
  2471. * when performing SRV lookup (*port==0) it will use proto to look for
  2472. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  2473. * returns: hostent struct & *port filled with the port from the SRV record;
  2474. * 0 on error
  2475. */
  2476. struct hostent* dns_srv_sip_resolvehost(str* name, unsigned short* port,
  2477. char* proto)
  2478. {
  2479. struct hostent* he;
  2480. struct ip_addr* ip;
  2481. static char tmp[MAX_DNS_NAME]; /* tmp. buff. for SRV lookups */
  2482. int len;
  2483. str srv_name;
  2484. char srv_proto;
  2485. if ((cfg_get(core, core_cfg, use_dns_cache)==0) || (dns_hash==0)){
  2486. /* not init or off => use normal, non-cached version */
  2487. return _sip_resolvehost(name, port, proto);
  2488. }
  2489. len=0;
  2490. if (proto){ /* makes sure we have a protocol set*/
  2491. if (*proto==0)
  2492. *proto=srv_proto=PROTO_UDP; /* default */
  2493. else
  2494. srv_proto=*proto;
  2495. }else{
  2496. srv_proto=PROTO_UDP;
  2497. }
  2498. /* try SRV if no port specified (draft-ietf-sip-srv-06) */
  2499. if ((port)&&(*port==0)){
  2500. *port=(srv_proto==PROTO_TLS)?SIPS_PORT:SIP_PORT; /* just in case we
  2501. don't find another */
  2502. if ((name->len+SRV_MAX_PREFIX_LEN+1)>MAX_DNS_NAME){
  2503. LOG(L_WARN, "WARNING: dns_sip_resolvehost: domain name too long"
  2504. " (%d), unable to perform SRV lookup\n", name->len);
  2505. }else{
  2506. /* check if it's an ip address */
  2507. if ( ((ip=str2ip(name))!=0)
  2508. #ifdef USE_IPV6
  2509. || ((ip=str2ip6(name))!=0)
  2510. #endif
  2511. ){
  2512. /* we are lucky, this is an ip address */
  2513. return ip_addr2he(name,ip);
  2514. }
  2515. switch(srv_proto){
  2516. case PROTO_NONE: /* no proto specified, use udp */
  2517. if (proto)
  2518. *proto=PROTO_UDP;
  2519. /* no break */
  2520. case PROTO_UDP:
  2521. memcpy(tmp, SRV_UDP_PREFIX, SRV_UDP_PREFIX_LEN);
  2522. memcpy(tmp+SRV_UDP_PREFIX_LEN, name->s, name->len);
  2523. tmp[SRV_UDP_PREFIX_LEN + name->len] = '\0';
  2524. len=SRV_UDP_PREFIX_LEN + name->len;
  2525. break;
  2526. case PROTO_TCP:
  2527. memcpy(tmp, SRV_TCP_PREFIX, SRV_TCP_PREFIX_LEN);
  2528. memcpy(tmp+SRV_TCP_PREFIX_LEN, name->s, name->len);
  2529. tmp[SRV_TCP_PREFIX_LEN + name->len] = '\0';
  2530. len=SRV_TCP_PREFIX_LEN + name->len;
  2531. break;
  2532. case PROTO_TLS:
  2533. memcpy(tmp, SRV_TLS_PREFIX, SRV_TLS_PREFIX_LEN);
  2534. memcpy(tmp+SRV_TLS_PREFIX_LEN, name->s, name->len);
  2535. tmp[SRV_TLS_PREFIX_LEN + name->len] = '\0';
  2536. len=SRV_TLS_PREFIX_LEN + name->len;
  2537. break;
  2538. case PROTO_SCTP:
  2539. memcpy(tmp, SRV_SCTP_PREFIX, SRV_SCTP_PREFIX_LEN);
  2540. memcpy(tmp+SRV_SCTP_PREFIX_LEN, name->s, name->len);
  2541. tmp[SRV_SCTP_PREFIX_LEN + name->len] = '\0';
  2542. len=SRV_SCTP_PREFIX_LEN + name->len;
  2543. break;
  2544. default:
  2545. LOG(L_CRIT, "BUG: sip_resolvehost: unknown proto %d\n",
  2546. (int)srv_proto);
  2547. return 0;
  2548. }
  2549. srv_name.s=tmp;
  2550. srv_name.len=len;
  2551. if ((he=dns_srv_get_he(&srv_name, port, dns_flags))!=0)
  2552. return he;
  2553. }
  2554. }
  2555. /*skip_srv:*/
  2556. if (name->len >= MAX_DNS_NAME) {
  2557. LOG(L_ERR, "dns_sip_resolvehost: domain name too long\n");
  2558. return 0;
  2559. }
  2560. he=dns_get_he(name, dns_flags);
  2561. return he;
  2562. }
  2563. #ifdef USE_NAPTR
  2564. /* iterates over a naptr rr list, returning each time a "good" naptr record
  2565. * is found.( srv type, no regex and a supported protocol)
  2566. * params:
  2567. * naptr_head - naptr dns_rr list head
  2568. * tried - bitmap used to keep track of the already tried records
  2569. * (no more then sizeof(tried)*8 valid records are
  2570. * ever walked
  2571. * srv_name - if succesfull, it will be set to the selected record
  2572. * srv name (naptr repl.)
  2573. * proto - if succesfull it will be set to the selected record
  2574. * protocol
  2575. * returns 0 if no more records found or a pointer to the selected record
  2576. * and sets protocol and srv_name
  2577. * WARNING: when calling first time make sure you run first
  2578. * naptr_iterate_init(&tried)
  2579. */
  2580. struct naptr_rdata* dns_naptr_sip_iterate(struct dns_rr* naptr_head,
  2581. naptr_bmp_t* tried,
  2582. str* srv_name, char* proto)
  2583. {
  2584. int i, idx;
  2585. struct dns_rr* l;
  2586. struct naptr_rdata* naptr;
  2587. struct naptr_rdata* naptr_saved;
  2588. char saved_proto;
  2589. char naptr_proto;
  2590. idx=0;
  2591. naptr_proto=PROTO_NONE;
  2592. naptr_saved=0;
  2593. saved_proto=0;
  2594. i=0;
  2595. for(l=naptr_head; l && (i<MAX_NAPTR_RRS); l=l->next){
  2596. naptr=(struct naptr_rdata*) l->rdata;
  2597. if (naptr==0){
  2598. LOG(L_CRIT, "naptr_iterate: BUG: null rdata\n");
  2599. goto end;
  2600. }
  2601. /* check if valid and get proto */
  2602. if ((naptr_proto=naptr_get_sip_proto(naptr))<=0) continue;
  2603. if (*tried& (1<<i)){
  2604. i++;
  2605. continue; /* already tried */
  2606. }
  2607. #ifdef DNS_CACHE_DEBUG
  2608. DBG("naptr_iterate: found a valid sip NAPTR rr %.*s,"
  2609. " proto %d\n", naptr->repl_len, naptr->repl,
  2610. (int)naptr_proto);
  2611. #endif
  2612. if ((naptr_proto_supported(naptr_proto))){
  2613. if (naptr_choose(&naptr_saved, &saved_proto,
  2614. naptr, naptr_proto))
  2615. idx=i;
  2616. }
  2617. i++;
  2618. }
  2619. if (naptr_saved){
  2620. /* found something */
  2621. #ifdef DNS_CACHE_DEBUG
  2622. DBG("naptr_iterate: choosed NAPTR rr %.*s, proto %d"
  2623. " tried: 0x%x\n", naptr_saved->repl_len,
  2624. naptr_saved->repl, (int)saved_proto, *tried);
  2625. #endif
  2626. *tried|=1<<idx;
  2627. *proto=saved_proto;
  2628. srv_name->s=naptr_saved->repl;
  2629. srv_name->len=naptr_saved->repl_len;
  2630. return naptr_saved;
  2631. }
  2632. end:
  2633. return 0;
  2634. }
  2635. /* resolves a host name trying NAPTR lookup if *proto==0 and *port==0, SRV
  2636. * lookup if *port==0 or normal A/AAAA lookup
  2637. * if *port!=0.
  2638. * when performing SRV lookup (*port==0) it will use proto to look for
  2639. * tcp or udp hosts; if proto==0 => no SRV lookup
  2640. * returns: hostent struct & *port filled with the port from the SRV record;
  2641. * 0 on error
  2642. */
  2643. struct hostent* dns_naptr_sip_resolvehost(str* name, unsigned short* port,
  2644. char* proto)
  2645. {
  2646. struct hostent* he;
  2647. struct ip_addr* tmp_ip;
  2648. naptr_bmp_t tried_bmp;
  2649. struct dns_hash_entry* e;
  2650. char n_proto;
  2651. char origproto;
  2652. str srv_name;
  2653. origproto=*proto;
  2654. he=0;
  2655. if (dns_hash==0){ /* not init => use normal, non-cached version */
  2656. LOG(L_WARN, "WARNING: dns_sip_resolvehost: called before dns cache"
  2657. " initialization\n");
  2658. return _sip_resolvehost(name, port, proto);
  2659. }
  2660. if (proto && port && (*proto==0) && (*port==0)){
  2661. *proto=PROTO_UDP; /* just in case we don't find another */
  2662. /* check if it's an ip address */
  2663. if ( ((tmp_ip=str2ip(name))!=0)
  2664. #ifdef USE_IPV6
  2665. || ((tmp_ip=str2ip6(name))!=0)
  2666. #endif
  2667. ){
  2668. /* we are lucky, this is an ip address */
  2669. #ifdef USE_IPV6
  2670. if (((dns_flags&DNS_IPV4_ONLY) && (tmp_ip->af==AF_INET6))||
  2671. ((dns_flags&DNS_IPV6_ONLY) && (tmp_ip->af==AF_INET))){
  2672. return 0;
  2673. }
  2674. #endif
  2675. *port=SIP_PORT;
  2676. return ip_addr2he(name, tmp_ip);
  2677. }
  2678. /* do naptr lookup */
  2679. if ((e=dns_get_entry(name, T_NAPTR))==0)
  2680. goto naptr_not_found;
  2681. naptr_iterate_init(&tried_bmp);
  2682. while(dns_naptr_sip_iterate(e->rr_lst, &tried_bmp,
  2683. &srv_name, &n_proto)){
  2684. if ((he=dns_srv_get_he(&srv_name, port, dns_flags))!=0){
  2685. #ifdef DNS_CACHE_DEBUG
  2686. DBG("dns_naptr_sip_resolvehost(%.*s, %d, %d) srv, ret=%p\n",
  2687. name->len, name->s, (int)*port, (int)*proto, he);
  2688. #endif
  2689. dns_hash_put(e);
  2690. *proto=n_proto;
  2691. return he;
  2692. }
  2693. }
  2694. /* no acceptable naptr record found, fallback to srv */
  2695. dns_hash_put(e);
  2696. }
  2697. naptr_not_found:
  2698. *proto = origproto;
  2699. he = no_naptr_srv_sip_resolvehost(name,port,proto);
  2700. /* fallback all the way down to A/AAAA */
  2701. if (he==0) {
  2702. he=dns_get_he(name,dns_flags);
  2703. }
  2704. return he;
  2705. }
  2706. #endif /* USE_NAPTR */
  2707. /* resolves a host name trying NAPTR lookup if *proto==0 and *port==0, SRV
  2708. * lookup if *port==0 or normal A/AAAA lookup
  2709. * if *port!=0.
  2710. * when performing SRV lookup (*port==0) it will use proto to look for
  2711. * tcp or udp hosts; if proto==0 => no SRV lookup
  2712. * returns: hostent struct & *port filled with the port from the SRV record;
  2713. * 0 on error
  2714. */
  2715. struct hostent* dns_sip_resolvehost(str* name, unsigned short* port,
  2716. char* proto)
  2717. {
  2718. #ifdef USE_NAPTR
  2719. if (dns_flags&DNS_TRY_NAPTR)
  2720. return dns_naptr_sip_resolvehost(name, port, proto);
  2721. #endif
  2722. return dns_srv_sip_resolvehost(name, port, proto);
  2723. }
  2724. /* performs an a lookup, fills the dns_entry pointer and the ip addr.
  2725. * (with the first good ip). if *e ==0 does the a lookup, and changes it
  2726. * to the result, if not it uses the current value and tries to use
  2727. * the rr_no record from it.
  2728. * params: e - must contain the "in-use" dns_hash_entry pointer (from
  2729. * a previous call) or *e==0 (for the first call)
  2730. * name - host name for which we do the lookup (required only
  2731. * when *e==0)
  2732. * ip - will be filled with the first good resolved ip started
  2733. * at *rr_no
  2734. * rr_no - record number to start searching for a good ip from
  2735. * (e.g. value from previous call + 1), filled on return
  2736. * with the number of the record corresponding to the
  2737. * returned ip
  2738. * returns 0 on success, <0 on error (see the error codes),
  2739. * fills e, ip and rr_no
  2740. * On end of records (when used to iterate on all the ips) it
  2741. * will return E_DNS_EOR (you should not log an error for this
  2742. * value, is just a signal that the address list end has been reached)
  2743. * Note: either e or name must be different from 0 (name.s !=0 also)
  2744. * WARNING: dns_hash_put(*e) must be called when you don't need
  2745. * the entry anymore and *e!=0 (failling to do so => mem. leak)
  2746. * Example:
  2747. * dns_entry=0;
  2748. * ret=dns_a_get_ip(&dns_entry, name, &ip, &rr_no); -- get the first rr.
  2749. * ...
  2750. * rr_no++;
  2751. * while((ret>=0) && dns_entry)
  2752. * dns_a_get_ip(&dns_entry, name, &ip, &rr_no); -- get the next rr
  2753. * if (ret!=-E_DNS_EOR) ERROR(....);
  2754. * ...
  2755. * dns_hash_put(dns_entry); -- finished with the entry
  2756. */
  2757. inline static int dns_a_resolve( struct dns_hash_entry** e,
  2758. unsigned char* rr_no,
  2759. str* name,
  2760. struct ip_addr* ip)
  2761. {
  2762. struct dns_rr* rr;
  2763. int ret;
  2764. ticks_t now;
  2765. struct ip_addr* tmp;
  2766. rr=0;
  2767. ret=-E_DNS_NO_IP;
  2768. if (*e==0){ /* do lookup */
  2769. /* if ip don't set *e */
  2770. #ifdef USE_IPV6
  2771. if (str2ip6(name)!=0)
  2772. goto error;
  2773. #endif
  2774. if ((tmp=str2ip(name))!=0){
  2775. *ip=*tmp;
  2776. *rr_no=0;
  2777. return 0;
  2778. }
  2779. if ((*e=dns_get_entry(name, T_A))==0)
  2780. goto error;
  2781. /* found */
  2782. *rr_no=0;
  2783. ret=-E_DNS_BAD_IP_ENTRY;
  2784. }
  2785. now=get_ticks_raw();
  2786. /* if the entry has already expired use the time at the end of lifetime */
  2787. if (unlikely((s_ticks_t)(now-(*e)->expire)>=0)) now=(*e)->expire-1;
  2788. rr=dns_entry_get_rr(*e, rr_no, now);
  2789. if (rr){
  2790. /* everything is ok now, we can try to "convert" the ip */
  2791. dns_rr2ip((*e)->type, rr, ip);
  2792. ret=0;
  2793. }else{
  2794. ret=-E_DNS_EOR;
  2795. }
  2796. error:
  2797. DBG("dns_a_resolve(%.*s, %d) returning %d\n",
  2798. name->len, name->s, *rr_no, ret);
  2799. return ret;
  2800. }
  2801. #ifdef USE_IPV6
  2802. /* lookup, fills the dns_entry pointer and the ip addr.
  2803. * (with the first good ip). if *e ==0 does the a lookup, and changes it
  2804. * to the result, if not it uses the current value and tries to use
  2805. * Same as dns_a_resolve but for aaaa records (see above).
  2806. */
  2807. inline static int dns_aaaa_resolve( struct dns_hash_entry** e,
  2808. unsigned char* rr_no,
  2809. str* name,
  2810. struct ip_addr* ip)
  2811. {
  2812. struct dns_rr* rr;
  2813. int ret;
  2814. ticks_t now;
  2815. struct ip_addr* tmp;
  2816. rr=0;
  2817. ret=-E_DNS_NO_IP;
  2818. if (*e==0){ /* do lookup */
  2819. /* if ip don't set *e */
  2820. if (str2ip(name)!=0)
  2821. goto error;
  2822. if ((tmp=str2ip6(name))!=0){
  2823. *ip=*tmp;
  2824. *rr_no=0;
  2825. return 0;
  2826. }
  2827. if ((*e=dns_get_entry(name, T_AAAA))==0)
  2828. goto error;
  2829. /* found */
  2830. *rr_no=0;
  2831. ret=-E_DNS_BAD_IP_ENTRY;
  2832. }
  2833. now=get_ticks_raw();
  2834. /* if the entry has already expired use the time at the end of lifetime */
  2835. if (unlikely((s_ticks_t)(now-(*e)->expire)>=0)) now=(*e)->expire-1;
  2836. rr=dns_entry_get_rr(*e, rr_no, now);
  2837. if (rr){
  2838. /* everything is ok now, we can try to "convert" the ip */
  2839. dns_rr2ip((*e)->type, rr, ip);
  2840. ret=0;
  2841. }else{
  2842. ret=-E_DNS_EOR; /* no more records */
  2843. }
  2844. error:
  2845. return ret;
  2846. }
  2847. #endif /* USE_IPV6 */
  2848. /* performs an a or aaaa dns lookup, returns <0 on error (see the
  2849. * dns error codes) and 0 on success
  2850. * flags: - none set: tries first an a_lookup and if it fails an aaaa_lookup
  2851. * - DNS_IPV6_FIRST: tries first an aaaa_lookup and then an a_lookup
  2852. * - DNS_IPV4_ONLY: tries only an a_lookup
  2853. * - DNS_IPV6_ONLY: tries only an aaaa_lookup
  2854. * see dns_a_resolve() for the rest of the params., examples a.s.o
  2855. * WARNING: don't forget dns_hash_put(*e) when e is not needed anymore
  2856. */
  2857. inline static int dns_ip_resolve( struct dns_hash_entry** e,
  2858. unsigned char* rr_no,
  2859. str* name,
  2860. struct ip_addr* ip,
  2861. int flags)
  2862. {
  2863. int ret;
  2864. str host;
  2865. struct dns_hash_entry* orig;
  2866. ret=-E_DNS_NO_IP;
  2867. if (*e==0){ /* first call */
  2868. #ifdef USE_IPV6
  2869. if ((flags&(DNS_IPV6_FIRST|DNS_IPV6_ONLY))){
  2870. ret=dns_aaaa_resolve(e, rr_no, name, ip);
  2871. if (ret>=0) return ret;
  2872. }else{
  2873. ret=dns_a_resolve(e, rr_no, name, ip);
  2874. if (ret>=0) return ret;
  2875. }
  2876. if (flags&DNS_IPV6_FIRST){
  2877. ret=dns_a_resolve(e, rr_no, name, ip);
  2878. }else if (!(flags&(DNS_IPV6_ONLY|DNS_IPV4_ONLY))){
  2879. ret=dns_aaaa_resolve(e, rr_no, name, ip);
  2880. }
  2881. #else /* USE_IPV6 */
  2882. ret=dns_a_resolve(e, rr_no, name, ip);
  2883. #endif /* USE_IPV6 */
  2884. }else if ((*e)->type==T_A){
  2885. /* continue A resolving */
  2886. /* retrieve host name from the hash entry (ignore name which might
  2887. be null when continuing a srv lookup) */
  2888. host.s=(*e)->name;
  2889. host.len=(*e)->name_len;
  2890. ret=dns_a_resolve(e, rr_no, &host, ip);
  2891. #ifdef USE_IPV6
  2892. if (ret>=0) return ret;
  2893. if (!(flags&(DNS_IPV6_ONLY|DNS_IPV6_FIRST|DNS_IPV4_ONLY))){
  2894. /* not found, try with AAAA */
  2895. orig=*e;
  2896. *e=0;
  2897. *rr_no=0;
  2898. ret=dns_aaaa_resolve(e, rr_no, &host, ip);
  2899. /* delay original record release until we're finished with host*/
  2900. dns_hash_put(orig);
  2901. }
  2902. #endif /* USE_IPV6 */
  2903. }else if ((*e)->type==T_AAAA){
  2904. /* retrieve host name from the hash entry (ignore name which might
  2905. be null when continuing a srv lookup) */
  2906. host.s=(*e)->name;
  2907. host.len=(*e)->name_len;
  2908. #ifdef USE_IPV6
  2909. /* continue AAAA resolving */
  2910. ret=dns_aaaa_resolve(e, rr_no, &host, ip);
  2911. if (ret>=0) return ret;
  2912. if ((flags&DNS_IPV6_FIRST) && !(flags&DNS_IPV6_ONLY)){
  2913. /* not found, try with A */
  2914. orig=*e;
  2915. *e=0;
  2916. *rr_no=0;
  2917. ret=dns_a_resolve(e, rr_no, &host, ip);
  2918. /* delay original record release until we're finished with host*/
  2919. dns_hash_put(orig);
  2920. }
  2921. #else /* USE_IPV6 */
  2922. /* ipv6 disabled, try with A */
  2923. orig=*e;
  2924. *e=0;
  2925. *rr_no=0;
  2926. ret=dns_a_resolve(e, rr_no, &host, ip);
  2927. /* delay original record release until we're finished with host*/
  2928. dns_hash_put(orig);
  2929. #endif /* USE_IPV6 */
  2930. }else{
  2931. LOG(L_CRIT, "BUG: dns_ip_resolve: invalid record type %d\n",
  2932. (*e)->type);
  2933. }
  2934. return ret;
  2935. }
  2936. /* gets the first srv record starting at rr_no
  2937. * Next call will return the next record a.s.o.
  2938. * (similar to dns_a_resolve but for srv, sets host, port and automatically
  2939. * switches to the next record in the future)
  2940. *
  2941. * if DNS_SRV_LB and tried!=NULL will do random weight based selection
  2942. * for choosing between SRV RRs with the same priority (as described in
  2943. * RFC2782).
  2944. * If tried==NULL or DNS_SRV_LB is not defined => always returns next
  2945. * record in the priority order and for records with the same priority
  2946. * the record with the higher weight (from the remaining ones)
  2947. */
  2948. inline static int dns_srv_resolve_nxt(struct dns_hash_entry** e,
  2949. #ifdef DNS_SRV_LB
  2950. srv_flags_t* tried,
  2951. #endif
  2952. unsigned char* rr_no,
  2953. str* name, str* host, unsigned short* port)
  2954. {
  2955. struct dns_rr* rr;
  2956. int ret;
  2957. ticks_t now;
  2958. rr=0;
  2959. ret=-E_DNS_NO_SRV;
  2960. if (*e==0){
  2961. if ((*e=dns_get_entry(name, T_SRV))==0)
  2962. goto error;
  2963. /* found it */
  2964. *rr_no=0;
  2965. #ifdef DNS_SRV_LB
  2966. if (tried)
  2967. srv_reset_tried(tried);
  2968. #endif
  2969. ret=-E_DNS_BAD_SRV_ENTRY;
  2970. }
  2971. now=get_ticks_raw();
  2972. /* if the entry has already expired use the time at the end of lifetime */
  2973. if (unlikely((s_ticks_t)(now-(*e)->expire)>=0)) now=(*e)->expire-1;
  2974. #ifdef DNS_SRV_LB
  2975. if (tried){
  2976. rr=dns_srv_get_nxt_rr(*e, tried, rr_no, now);
  2977. }else
  2978. #endif
  2979. {
  2980. rr=dns_entry_get_rr(*e, rr_no, now);
  2981. (*rr_no)++; /* try next record next time */
  2982. }
  2983. if (rr){
  2984. host->s=((struct srv_rdata*)rr->rdata)->name;
  2985. host->len=((struct srv_rdata*)rr->rdata)->name_len;
  2986. *port=((struct srv_rdata*)rr->rdata)->port;
  2987. ret=0;
  2988. }else{
  2989. ret=-E_DNS_EOR; /* no more records */
  2990. }
  2991. error:
  2992. return ret;
  2993. }
  2994. /* gets the first srv record starting at h->srv_no, resolve it
  2995. * and get the first ip address (starting at h->ip_no)
  2996. * (similar to dns_a_resolve but for srv, sets host, port)
  2997. * WARNING: don't forget to init h prior to calling this function the first
  2998. * time and dns_srv_handle_put(h), even if error is returned
  2999. */
  3000. inline static int dns_srv_resolve_ip(struct dns_srv_handle* h,
  3001. str* name, struct ip_addr* ip, unsigned short* port,
  3002. int flags)
  3003. {
  3004. int ret;
  3005. str host;
  3006. host.len=0;
  3007. host.s=0;
  3008. do{
  3009. if (h->a==0){
  3010. #ifdef DNS_SRV_LB
  3011. if ((ret=dns_srv_resolve_nxt(&h->srv,
  3012. (flags & DNS_SRV_RR_LB)?&h->srv_tried_rrs:0,
  3013. &h->srv_no,
  3014. name, &host, port))<0)
  3015. goto error;
  3016. #else
  3017. if ((ret=dns_srv_resolve_nxt(&h->srv, &h->srv_no,
  3018. name, &host, port))<0)
  3019. goto error;
  3020. #endif
  3021. h->port=*port; /* store new port */
  3022. }else{
  3023. *port=h->port; /* return the stored port */
  3024. }
  3025. if ((ret=dns_ip_resolve(&h->a, &h->ip_no, &host, ip, flags))<0){
  3026. /* couldn't find any good ip for this record, try the next one */
  3027. if (h->a){
  3028. dns_hash_put(h->a);
  3029. h->a=0;
  3030. }
  3031. }else if (h->a==0){
  3032. /* this was an ip, try the next srv record in the future */
  3033. }
  3034. }while(ret<0);
  3035. error:
  3036. #ifdef DNS_CACHE_DEBUG
  3037. DBG("dns_srv_resolve_ip(\"%.*s\", %d, %d), ret=%d, ip=%s\n",
  3038. name->len, name->s, h->srv_no, h->ip_no, ret,
  3039. ip?ZSW(ip_addr2a(ip)):"");
  3040. #endif
  3041. return ret;
  3042. }
  3043. /* resolves a host name trying SRV lookup if *port==0 or normal A/AAAA lookup
  3044. * if *port!=0.
  3045. * when performing SRV lookup (*port==0) it will use proto to look for
  3046. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  3047. * h must be initialized prior to calling this function and can be used to
  3048. * get the subsequent ips
  3049. * returns: <0 on error
  3050. * 0 on success and it fills *ip, *port, dns_sip_resolve_h
  3051. * WARNING: when finished, dns_sip_resolve_put(h) must be called!
  3052. */
  3053. inline static int dns_srv_sip_resolve(struct dns_srv_handle* h, str* name,
  3054. struct ip_addr* ip, unsigned short* port, char* proto,
  3055. int flags)
  3056. {
  3057. static char tmp[MAX_DNS_NAME]; /* tmp. buff. for SRV lookups */
  3058. int len;
  3059. str srv_name;
  3060. struct ip_addr* tmp_ip;
  3061. int ret;
  3062. struct hostent* he;
  3063. char srv_proto;
  3064. if (dns_hash==0){ /* not init => use normal, non-cached version */
  3065. LOG(L_WARN, "WARNING: dns_sip_resolve: called before dns cache"
  3066. " initialization\n");
  3067. h->srv=h->a=0;
  3068. he=_sip_resolvehost(name, port, proto);
  3069. if (he){
  3070. hostent2ip_addr(ip, he, 0);
  3071. return 0;
  3072. }
  3073. return -E_DNS_NO_SRV;
  3074. }
  3075. len=0;
  3076. if ((h->srv==0) && (h->a==0)){ /* first call */
  3077. if (proto){ /* makes sure we have a protocol set*/
  3078. if (*proto==0)
  3079. *proto=srv_proto=PROTO_UDP; /* default */
  3080. else
  3081. srv_proto=*proto;
  3082. }else{
  3083. srv_proto=PROTO_UDP;
  3084. }
  3085. h->port=(srv_proto==PROTO_TLS)?SIPS_PORT:SIP_PORT; /* just in case we
  3086. don't find another */
  3087. h->proto=srv_proto; /* store initial protocol */
  3088. if (port){
  3089. if (*port==0){
  3090. /* try SRV if initial call & no port specified
  3091. * (draft-ietf-sip-srv-06) */
  3092. if ((name->len+SRV_MAX_PREFIX_LEN+1)>MAX_DNS_NAME){
  3093. LOG(L_WARN, "WARNING: dns_sip_resolvehost: domain name too"
  3094. " long (%d), unable to perform SRV lookup\n",
  3095. name->len);
  3096. }else{
  3097. /* check if it's an ip address */
  3098. if ( ((tmp_ip=str2ip(name))!=0)
  3099. #ifdef USE_IPV6
  3100. || ((tmp_ip=str2ip6(name))!=0)
  3101. #endif
  3102. ){
  3103. /* we are lucky, this is an ip address */
  3104. #ifdef USE_IPV6
  3105. if (((flags&DNS_IPV4_ONLY) && (tmp_ip->af==AF_INET6))||
  3106. ((flags&DNS_IPV6_ONLY) && (tmp_ip->af==AF_INET))){
  3107. return -E_DNS_AF_MISMATCH;
  3108. }
  3109. #endif
  3110. *ip=*tmp_ip;
  3111. *port=h->port;
  3112. /* proto already set */
  3113. return 0;
  3114. }
  3115. switch(srv_proto){
  3116. case PROTO_NONE: /* no proto specified, use udp */
  3117. if (proto)
  3118. *proto=PROTO_UDP;
  3119. /* no break */
  3120. case PROTO_UDP:
  3121. memcpy(tmp, SRV_UDP_PREFIX, SRV_UDP_PREFIX_LEN);
  3122. memcpy(tmp+SRV_UDP_PREFIX_LEN, name->s, name->len);
  3123. tmp[SRV_UDP_PREFIX_LEN + name->len] = '\0';
  3124. len=SRV_UDP_PREFIX_LEN + name->len;
  3125. break;
  3126. case PROTO_TCP:
  3127. memcpy(tmp, SRV_TCP_PREFIX, SRV_TCP_PREFIX_LEN);
  3128. memcpy(tmp+SRV_TCP_PREFIX_LEN, name->s, name->len);
  3129. tmp[SRV_TCP_PREFIX_LEN + name->len] = '\0';
  3130. len=SRV_TCP_PREFIX_LEN + name->len;
  3131. break;
  3132. case PROTO_TLS:
  3133. memcpy(tmp, SRV_TLS_PREFIX, SRV_TLS_PREFIX_LEN);
  3134. memcpy(tmp+SRV_TLS_PREFIX_LEN, name->s, name->len);
  3135. tmp[SRV_TLS_PREFIX_LEN + name->len] = '\0';
  3136. len=SRV_TLS_PREFIX_LEN + name->len;
  3137. break;
  3138. case PROTO_SCTP:
  3139. memcpy(tmp, SRV_SCTP_PREFIX, SRV_SCTP_PREFIX_LEN);
  3140. memcpy(tmp+SRV_SCTP_PREFIX_LEN, name->s, name->len);
  3141. tmp[SRV_SCTP_PREFIX_LEN + name->len] = '\0';
  3142. len=SRV_SCTP_PREFIX_LEN + name->len;
  3143. break;
  3144. default:
  3145. LOG(L_CRIT, "BUG: sip_resolvehost: "
  3146. "unknown proto %d\n", (int)srv_proto);
  3147. return -E_DNS_CRITICAL;
  3148. }
  3149. srv_name.s=tmp;
  3150. srv_name.len=len;
  3151. if ((ret=dns_srv_resolve_ip(h, &srv_name, ip,
  3152. port, flags))>=0)
  3153. {
  3154. #ifdef DNS_CACHE_DEBUG
  3155. DBG("dns_sip_resolve(%.*s, %d, %d), srv0, ret=%d\n",
  3156. name->len, name->s, h->srv_no, h->ip_no, ret);
  3157. #endif
  3158. /* proto already set */
  3159. return ret;
  3160. }
  3161. }
  3162. }else{ /* if (*port==0) */
  3163. h->port=*port; /* store initial port */
  3164. /* proto already set */
  3165. }
  3166. } /* if (port) */
  3167. }else if (h->srv){
  3168. srv_name.s=h->srv->name;
  3169. srv_name.len=h->srv->name_len;
  3170. /* continue srv resolving */
  3171. ret=dns_srv_resolve_ip(h, &srv_name, ip, port, flags);
  3172. if (proto)
  3173. *proto=h->proto;
  3174. DBG("dns_sip_resolve(%.*s, %d, %d), srv, ret=%d\n",
  3175. name->len, name->s, h->srv_no, h->ip_no, ret);
  3176. return ret;
  3177. }
  3178. /*skip_srv:*/
  3179. if (name->len >= MAX_DNS_NAME) {
  3180. LOG(L_ERR, "dns_sip_resolve: domain name too long\n");
  3181. return -E_DNS_NAME_TOO_LONG;
  3182. }
  3183. ret=dns_ip_resolve(&h->a, &h->ip_no, name, ip, flags);
  3184. if (port)
  3185. *port=h->port;
  3186. if (proto)
  3187. *proto=h->proto;
  3188. #ifdef DNS_CACHE_DEBUG
  3189. DBG("dns_sip_resolve(%.*s, %d, %d), ip, ret=%d\n",
  3190. name->len, name->s, h->srv_no, h->ip_no, ret);
  3191. #endif
  3192. return ret;
  3193. }
  3194. #ifdef USE_NAPTR
  3195. /* resolves a host name trying:
  3196. * - NAPTR lookup if the address is not an ip and proto!=0, port!=0
  3197. * *port==0 and *proto=0 and if flags allow NAPTR lookups
  3198. * -SRV lookup if port!=0 and *port==0
  3199. * - normal A/AAAA lookup if *port!=0, or port==0
  3200. * when performing SRV lookup (*port==0) it will use proto to look for
  3201. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  3202. * h must be initialized prior to calling this function and can be used to
  3203. * get the subsequent ips
  3204. * returns: <0 on error
  3205. * 0 on success and it fills *ip, *port, dns_sip_resolve_h
  3206. * WARNING: when finished, dns_sip_resolve_put(h) must be called!
  3207. */
  3208. inline static int dns_naptr_sip_resolve(struct dns_srv_handle* h, str* name,
  3209. struct ip_addr* ip, unsigned short* port, char* proto,
  3210. int flags)
  3211. {
  3212. struct hostent* he;
  3213. struct ip_addr* tmp_ip;
  3214. naptr_bmp_t tried_bmp;
  3215. struct dns_hash_entry* e;
  3216. char n_proto;
  3217. str srv_name;
  3218. int ret;
  3219. ret=-E_DNS_NO_NAPTR;
  3220. if (dns_hash==0){ /* not init => use normal, non-cached version */
  3221. LOG(L_WARN, "WARNING: dns_sip_resolve: called before dns cache"
  3222. " initialization\n");
  3223. h->srv=h->a=0;
  3224. he=_sip_resolvehost(name, port, proto);
  3225. if (he){
  3226. hostent2ip_addr(ip, he, 0);
  3227. return 0;
  3228. }
  3229. return -E_DNS_NO_NAPTR;
  3230. }
  3231. if (((h->srv==0) && (h->a==0)) && /* first call */
  3232. proto && port && (*proto==0) && (*port==0)){
  3233. *proto=PROTO_UDP; /* just in case we don't find another */
  3234. /* check if it's an ip address */
  3235. if ( ((tmp_ip=str2ip(name))!=0)
  3236. #ifdef USE_IPV6
  3237. || ((tmp_ip=str2ip6(name))!=0)
  3238. #endif
  3239. ){
  3240. /* we are lucky, this is an ip address */
  3241. #ifdef USE_IPV6
  3242. if (((flags&DNS_IPV4_ONLY) && (tmp_ip->af==AF_INET6))||
  3243. ((flags&DNS_IPV6_ONLY) && (tmp_ip->af==AF_INET))){
  3244. return -E_DNS_AF_MISMATCH;
  3245. }
  3246. #endif
  3247. *ip=*tmp_ip;
  3248. h->port=SIP_PORT;
  3249. h->proto=*proto;
  3250. *port=h->port;
  3251. return 0;
  3252. }
  3253. /* do naptr lookup */
  3254. if ((e=dns_get_entry(name, T_NAPTR))==0)
  3255. goto naptr_not_found;
  3256. naptr_iterate_init(&tried_bmp);
  3257. while(dns_naptr_sip_iterate(e->rr_lst, &tried_bmp,
  3258. &srv_name, &n_proto)){
  3259. dns_srv_handle_init(h); /* make sure h does not contain garbage
  3260. from previous dns_srv_sip_resolve calls */
  3261. if ((ret=dns_srv_resolve_ip(h, &srv_name, ip, port, flags))>=0){
  3262. #ifdef DNS_CACHE_DEBUG
  3263. DBG("dns_naptr_sip_resolve(%.*s, %d, %d), srv0, ret=%d\n",
  3264. name->len, name->s, h->srv_no, h->ip_no, ret);
  3265. #endif
  3266. dns_hash_put(e);
  3267. *proto=n_proto;
  3268. h->proto=*proto;
  3269. return ret;
  3270. }
  3271. }
  3272. /* no acceptable naptr record found, fallback to srv */
  3273. dns_hash_put(e);
  3274. dns_srv_handle_init(h); /* make sure h does not contain garbage
  3275. from previous dns_srv_sip_resolve calls */
  3276. }
  3277. naptr_not_found:
  3278. return dns_srv_sip_resolve(h, name, ip, port, proto, flags);
  3279. }
  3280. #endif /* USE_NAPTR */
  3281. /* resolves a host name trying:
  3282. * - NAPTR lookup if the address is not an ip and proto!=0, port!=0
  3283. * *port==0 and *proto=0 and if flags allow NAPTR lookups
  3284. * -SRV lookup if port!=0 and *port==0
  3285. * - normal A/AAAA lookup if *port!=0, or port==0
  3286. * when performing SRV lookup (*port==0) it will use proto to look for
  3287. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  3288. * h must be initialized prior to calling this function and can be used to
  3289. * get the subsequent ips
  3290. * returns: <0 on error
  3291. * 0 on success and it fills *ip, *port, dns_sip_resolve_h
  3292. * WARNING: when finished, dns_sip_resolve_put(h) must be called!
  3293. */
  3294. int dns_sip_resolve(struct dns_srv_handle* h, str* name,
  3295. struct ip_addr* ip, unsigned short* port, char* proto,
  3296. int flags)
  3297. {
  3298. #ifdef USE_NAPTR
  3299. if (flags&DNS_TRY_NAPTR)
  3300. return dns_naptr_sip_resolve(h, name, ip, port, proto, flags);
  3301. #endif
  3302. return dns_srv_sip_resolve(h, name, ip, port, proto, flags);
  3303. }
  3304. /* performs an a lookup and fills ip with the first good ip address
  3305. * returns 0 on success, <0 on error (see the error codes)
  3306. */
  3307. inline static int dns_a_get_ip(str* name, struct ip_addr* ip)
  3308. {
  3309. struct dns_hash_entry* e;
  3310. int ret;
  3311. unsigned char rr_no;
  3312. e=0;
  3313. rr_no=0;
  3314. ret=dns_a_resolve(&e, &rr_no, name, ip);
  3315. if (e) dns_hash_put(e);
  3316. return ret;
  3317. }
  3318. #ifdef USE_IPV6
  3319. inline static int dns_aaaa_get_ip(str* name, struct ip_addr* ip)
  3320. {
  3321. struct dns_hash_entry* e;
  3322. int ret;
  3323. unsigned char rr_no;
  3324. e=0;
  3325. rr_no=0;
  3326. ret=dns_aaaa_resolve(&e, &rr_no, name, ip);
  3327. if (e) dns_hash_put(e);
  3328. return ret;
  3329. }
  3330. #endif /* USE_IPV6 */
  3331. /* performs an a or aaaa dns lookup, returns <0 on error (see the
  3332. * dns error codes) and 0 on success
  3333. * flags: - none set: tries first an a_lookup and if it fails an aaaa_lookup
  3334. * - DNS_IPV6_FIRST: tries first an aaaa_lookup and then an a_lookup
  3335. * - DNS_IPV4_ONLY: tries only an a_lookup
  3336. * - DNS_IPV6_ONLY: tries only an aaaa_lookup
  3337. */
  3338. int dns_get_ip(str* name, struct ip_addr* ip, int flags)
  3339. {
  3340. int ret;
  3341. struct dns_hash_entry* e;
  3342. unsigned char rr_no;
  3343. e=0;
  3344. rr_no=0;
  3345. ret=dns_ip_resolve(&e, &rr_no, name, ip, flags);
  3346. if (e)
  3347. dns_hash_put(e);
  3348. return ret;
  3349. }
  3350. /* fast "inline" version, gets the first good ip:port */
  3351. int dns_srv_get_ip(str* name, struct ip_addr* ip, unsigned short* port,
  3352. int flags)
  3353. {
  3354. int ret;
  3355. struct dns_srv_handle h;
  3356. dns_srv_handle_init(&h);
  3357. ret=dns_srv_resolve_ip(&h, name, ip, port, flags);
  3358. dns_srv_handle_put(&h);
  3359. return ret;
  3360. }
  3361. #ifdef DNS_WATCHDOG_SUPPORT
  3362. /* sets the state of the DNS servers:
  3363. * 1: at least one server is up
  3364. * 0: all the servers are down
  3365. */
  3366. void dns_set_server_state(int state)
  3367. {
  3368. atomic_set(dns_servers_up, state);
  3369. }
  3370. /* returns the state of the DNS servers */
  3371. int dns_get_server_state(void)
  3372. {
  3373. return atomic_get(dns_servers_up);
  3374. }
  3375. #endif /* DNS_WATCHDOG_SUPPORT */
  3376. /* rpc functions */
  3377. void dns_cache_mem_info(rpc_t* rpc, void* ctx)
  3378. {
  3379. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3380. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3381. return;
  3382. }
  3383. rpc->add(ctx, "dd", *dns_cache_mem_used, cfg_get(core, core_cfg, dns_cache_max_mem));
  3384. }
  3385. void dns_cache_debug(rpc_t* rpc, void* ctx)
  3386. {
  3387. int h;
  3388. struct dns_hash_entry* e;
  3389. ticks_t now;
  3390. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3391. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3392. return;
  3393. }
  3394. now=get_ticks_raw();
  3395. LOCK_DNS_HASH();
  3396. for (h=0; h<DNS_HASH_SIZE; h++){
  3397. clist_foreach(&dns_hash[h], e, next){
  3398. rpc->add(ctx, "sdddddd",
  3399. e->name, e->type, e->total_size, e->refcnt.val,
  3400. (s_ticks_t)(e->expire-now)<0?-1:
  3401. TICKS_TO_S(e->expire-now),
  3402. TICKS_TO_S(now-e->last_used),
  3403. e->ent_flags);
  3404. }
  3405. }
  3406. UNLOCK_DNS_HASH();
  3407. }
  3408. #ifdef USE_DNS_CACHE_STATS
  3409. static unsigned long stat_sum(int ivar, int breset)
  3410. {
  3411. unsigned long isum=0;
  3412. int i1=0;
  3413. for (; i1 < get_max_procs(); i1++)
  3414. switch (ivar) {
  3415. case 0:
  3416. isum+=dns_cache_stats[i1].dns_req_cnt;
  3417. if (breset)
  3418. dns_cache_stats[i1].dns_req_cnt=0;
  3419. break;
  3420. case 1:
  3421. isum+=dns_cache_stats[i1].dc_hits_cnt;
  3422. if (breset)
  3423. dns_cache_stats[i1].dc_hits_cnt=0;
  3424. break;
  3425. case 2:
  3426. isum+=dns_cache_stats[i1].dc_neg_hits_cnt;
  3427. if (breset)
  3428. dns_cache_stats[i1].dc_neg_hits_cnt=0;
  3429. break;
  3430. case 3:
  3431. isum+=dns_cache_stats[i1].dc_lru_cnt;
  3432. if (breset)
  3433. dns_cache_stats[i1].dc_lru_cnt=0;
  3434. break;
  3435. }
  3436. return isum;
  3437. }
  3438. void dns_cache_stats_get(rpc_t* rpc, void* c)
  3439. {
  3440. char *name=NULL;
  3441. void *handle;
  3442. int found=0,i=0;
  3443. int reset=0;
  3444. char* dns_cache_stats_names[] = {
  3445. "dns_req_cnt",
  3446. "dc_hits_cnt",
  3447. "dc_neg_hits_cnt",
  3448. "dc_lru_cnt",
  3449. NULL
  3450. };
  3451. if (!cfg_get(core, core_cfg, use_dns_cache)) {
  3452. rpc->fault(c, 500, "dns cache support disabled");
  3453. return;
  3454. }
  3455. if (rpc->scan(c, "s", &name) < 0)
  3456. return;
  3457. if (rpc->scan(c, "d", &reset) < 0)
  3458. return;
  3459. if (!strcasecmp(name, DNS_CACHE_ALL_STATS)) {
  3460. /* dump all the dns cache stat values */
  3461. rpc->add(c, "{", &handle);
  3462. for (i=0; dns_cache_stats_names[i]; i++)
  3463. rpc->struct_add(handle, "d",
  3464. dns_cache_stats_names[i],
  3465. stat_sum(i, reset));
  3466. found=1;
  3467. } else {
  3468. for (i=0; dns_cache_stats_names[i]; i++)
  3469. if (!strcasecmp(dns_cache_stats_names[i], name)) {
  3470. rpc->add(c, "{", &handle);
  3471. rpc->struct_add(handle, "d",
  3472. dns_cache_stats_names[i],
  3473. stat_sum(i, reset));
  3474. found=1;
  3475. break;
  3476. }
  3477. }
  3478. if(!found)
  3479. rpc->fault(c, 500, "unknown dns cache stat parameter");
  3480. return;
  3481. }
  3482. #endif /* USE_DNS_CACHE_STATS */
  3483. /* rpc functions */
  3484. void dns_cache_debug_all(rpc_t* rpc, void* ctx)
  3485. {
  3486. int h;
  3487. struct dns_hash_entry* e;
  3488. struct dns_rr* rr;
  3489. struct ip_addr ip;
  3490. int i;
  3491. ticks_t now;
  3492. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3493. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3494. return;
  3495. }
  3496. now=get_ticks_raw();
  3497. LOCK_DNS_HASH();
  3498. for (h=0; h<DNS_HASH_SIZE; h++){
  3499. clist_foreach(&dns_hash[h], e, next){
  3500. for (i=0, rr=e->rr_lst; rr; i++, rr=rr->next){
  3501. rpc->add(ctx, "sddddddd",
  3502. e->name, (int)e->type, i, (int)e->total_size,
  3503. (int)e->refcnt.val,
  3504. (int)(s_ticks_t)(e->expire-now)<0?-1:
  3505. TICKS_TO_S(e->expire-now),
  3506. (int)TICKS_TO_S(now-e->last_used),
  3507. (int)e->ent_flags);
  3508. switch(e->type){
  3509. case T_A:
  3510. case T_AAAA:
  3511. if (dns_rr2ip(e->type, rr, &ip)==0){
  3512. rpc->add(ctx, "ss", "ip", ip_addr2a(&ip) );
  3513. }else{
  3514. rpc->add(ctx, "ss", "ip", "<error: bad rr>");
  3515. }
  3516. break;
  3517. case T_SRV:
  3518. rpc->add(ctx, "ss", "srv",
  3519. ((struct srv_rdata*)(rr->rdata))->name);
  3520. break;
  3521. case T_NAPTR:
  3522. rpc->add(ctx, "ss", "naptr ",
  3523. ((struct naptr_rdata*)(rr->rdata))->flags);
  3524. break;
  3525. case T_CNAME:
  3526. rpc->add(ctx, "ss", "cname",
  3527. ((struct cname_rdata*)(rr->rdata))->name);
  3528. break;
  3529. case T_TXT:
  3530. rpc->add(ctx, "ss", "txt",
  3531. ((struct txt_rdata*)(rr->rdata))->cstr_no?
  3532. ((struct txt_rdata*)(rr->rdata))->txt[0].cstr:
  3533. "");
  3534. break;
  3535. case T_EBL:
  3536. rpc->add(ctx, "ss", "ebl",
  3537. ((struct ebl_rdata*)(rr->rdata))->apex);
  3538. break;
  3539. case T_PTR:
  3540. rpc->add(ctx, "ss", "ptr",
  3541. ((struct ptr_rdata*)(rr->rdata))->ptrdname);
  3542. break;
  3543. default:
  3544. rpc->add(ctx, "ss", "unknown", "?");
  3545. }
  3546. rpc->add(ctx, "d",
  3547. (int)(s_ticks_t)(rr->expire-now)<0?-1:
  3548. TICKS_TO_S(rr->expire-now));
  3549. }
  3550. }
  3551. }
  3552. UNLOCK_DNS_HASH();
  3553. }
  3554. static char *print_type(unsigned short type)
  3555. {
  3556. switch (type) {
  3557. case T_A:
  3558. return "A";
  3559. case T_AAAA:
  3560. return "AAAA";
  3561. case T_SRV:
  3562. return "SRV";
  3563. case T_NAPTR:
  3564. return "NAPTR";
  3565. case T_CNAME:
  3566. return "CNAME";
  3567. case T_TXT:
  3568. return "TXT";
  3569. case T_EBL:
  3570. return "EBL";
  3571. case T_PTR:
  3572. return "PTR";
  3573. default:
  3574. return "unknown";
  3575. }
  3576. }
  3577. /** convert string type to dns integer T_*.
  3578. * used for rpc type translation.
  3579. * @return T_* on success, -1 on error.
  3580. */
  3581. static int dns_get_type(str* s)
  3582. {
  3583. char *t;
  3584. int len;
  3585. t=s->s;
  3586. len=s->len;
  3587. /* skip over a T_ or t_ prefix */
  3588. if ((len>2) && (t[0]=='T' || t[0]=='t') && (t[1]=='_')){
  3589. t+=2;
  3590. len-=2;
  3591. }
  3592. switch(len){
  3593. case 1:
  3594. if (t[0]=='A' || t[0]=='a')
  3595. return T_A;
  3596. break;
  3597. case 4:
  3598. if (strncasecmp(t, "AAAA", len)==0)
  3599. return T_AAAA;
  3600. break;
  3601. case 3:
  3602. if (strncasecmp(t, "SRV", len)==0)
  3603. return T_SRV;
  3604. else if (strncasecmp(t, "TXT", len)==0)
  3605. return T_TXT;
  3606. else if (strncasecmp(t, "EBL", len)==0)
  3607. return T_EBL;
  3608. else if (strncasecmp(t, "PTR", len)==0)
  3609. return T_PTR;
  3610. break;
  3611. case 5:
  3612. if (strncasecmp(t, "NAPTR", len)==0)
  3613. return T_NAPTR;
  3614. else if (strncasecmp(t, "CNAME", len)==0)
  3615. return T_CNAME;
  3616. break;
  3617. }
  3618. return -1;
  3619. }
  3620. /** rpc-prints a dns cache entry.
  3621. */
  3622. void dns_cache_print_entry(rpc_t* rpc, void* ctx, struct dns_hash_entry* e)
  3623. {
  3624. int expires;
  3625. struct dns_rr* rr;
  3626. struct ip_addr ip;
  3627. ticks_t now;
  3628. str s;
  3629. int i;
  3630. now=get_ticks_raw();
  3631. expires = (s_ticks_t)(e->expire-now)<0?-1: TICKS_TO_S(e->expire-now);
  3632. rpc->printf(ctx, "%sname: %s", SPACE_FORMAT, e->name);
  3633. rpc->printf(ctx, "%stype: %s", SPACE_FORMAT, print_type(e->type));
  3634. rpc->printf(ctx, "%ssize (bytes): %d", SPACE_FORMAT,
  3635. e->total_size);
  3636. rpc->printf(ctx, "%sreference counter: %d", SPACE_FORMAT,
  3637. e->refcnt.val);
  3638. if (e->ent_flags & DNS_FLAG_PERMANENT) {
  3639. rpc->printf(ctx, "%spermanent: yes", SPACE_FORMAT);
  3640. } else {
  3641. rpc->printf(ctx, "%spermanent: no", SPACE_FORMAT);
  3642. rpc->printf(ctx, "%sexpires in (s): %d", SPACE_FORMAT, expires);
  3643. }
  3644. rpc->printf(ctx, "%slast used (s): %d", SPACE_FORMAT,
  3645. TICKS_TO_S(now-e->last_used));
  3646. rpc->printf(ctx, "%snegative entry: %s", SPACE_FORMAT,
  3647. (e->ent_flags & DNS_FLAG_BAD_NAME) ? "yes" : "no");
  3648. for (rr=e->rr_lst; rr; rr=rr->next) {
  3649. switch(e->type) {
  3650. case T_A:
  3651. case T_AAAA:
  3652. if (dns_rr2ip(e->type, rr, &ip)==0){
  3653. rpc->printf(ctx, "%srr ip: %s", SPACE_FORMAT,
  3654. ip_addr2a(&ip) );
  3655. }else{
  3656. rpc->printf(ctx, "%srr ip: <error: bad rr>",
  3657. SPACE_FORMAT);
  3658. }
  3659. break;
  3660. case T_SRV:
  3661. rpc->printf(ctx, "%srr name: %s", SPACE_FORMAT,
  3662. ((struct srv_rdata*)(rr->rdata))->name);
  3663. rpc->printf(ctx, "%srr port: %d", SPACE_FORMAT,
  3664. ((struct srv_rdata*)(rr->rdata))->port);
  3665. rpc->printf(ctx, "%srr priority: %d", SPACE_FORMAT,
  3666. ((struct srv_rdata*)(rr->rdata))->priority);
  3667. rpc->printf(ctx, "%srr weight: %d", SPACE_FORMAT,
  3668. ((struct srv_rdata*)(rr->rdata))->weight);
  3669. break;
  3670. case T_NAPTR:
  3671. rpc->printf(ctx, "%srr order: %d", SPACE_FORMAT,
  3672. ((struct naptr_rdata*)(rr->rdata))->order);
  3673. rpc->printf(ctx, "%srr preference: %d", SPACE_FORMAT,
  3674. ((struct naptr_rdata*)(rr->rdata))->pref);
  3675. s.s = ((struct naptr_rdata*)(rr->rdata))->flags;
  3676. s.len = ((struct naptr_rdata*)(rr->rdata))->flags_len;
  3677. rpc->printf(ctx, "%srr flags: %.*s", SPACE_FORMAT,
  3678. s.len, s.s);
  3679. s.s=((struct naptr_rdata*)(rr->rdata))->services;
  3680. s.len=((struct naptr_rdata*)(rr->rdata))->services_len;
  3681. rpc->printf(ctx, "%srr service: %.*s", SPACE_FORMAT,
  3682. s.len, s.s);
  3683. s.s = ((struct naptr_rdata*)(rr->rdata))->regexp;
  3684. s.len = ((struct naptr_rdata*)(rr->rdata))->regexp_len;
  3685. rpc->printf(ctx, "%srr regexp: %.*s", SPACE_FORMAT,
  3686. s.len, s.s);
  3687. s.s = ((struct naptr_rdata*)(rr->rdata))->repl;
  3688. s.len = ((struct naptr_rdata*)(rr->rdata))->repl_len;
  3689. rpc->printf(ctx, "%srr replacement: %.*s",
  3690. SPACE_FORMAT, s.len, s.s);
  3691. break;
  3692. case T_CNAME:
  3693. rpc->printf(ctx, "%srr name: %s", SPACE_FORMAT,
  3694. ((struct cname_rdata*)(rr->rdata))->name);
  3695. break;
  3696. case T_TXT:
  3697. for (i=0; i<((struct txt_rdata*)(rr->rdata))->cstr_no;
  3698. i++){
  3699. rpc->printf(ctx, "%stxt[%d]: %s", SPACE_FORMAT, i,
  3700. ((struct txt_rdata*)(rr->rdata))->txt[i].cstr);
  3701. }
  3702. break;
  3703. case T_EBL:
  3704. rpc->printf(ctx, "%srr position: %d", SPACE_FORMAT,
  3705. ((struct ebl_rdata*)(rr->rdata))->position);
  3706. rpc->printf(ctx, "%srr separator: %s", SPACE_FORMAT,
  3707. ((struct ebl_rdata*)(rr->rdata))->separator);
  3708. rpc->printf(ctx, "%srr apex: %s", SPACE_FORMAT,
  3709. ((struct ebl_rdata*)(rr->rdata))->apex);
  3710. break;
  3711. case T_PTR:
  3712. rpc->printf(ctx, "%srr name: %s", SPACE_FORMAT,
  3713. ((struct ptr_rdata*)(rr->rdata))->ptrdname);
  3714. break;
  3715. default:
  3716. rpc->printf(ctx, "%sresource record: unknown",
  3717. SPACE_FORMAT);
  3718. }
  3719. if ((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  3720. rpc->printf(ctx, "%srr expires in (s): %d", SPACE_FORMAT,
  3721. (s_ticks_t)(rr->expire-now)<0?-1 :
  3722. TICKS_TO_S(rr->expire-now));
  3723. }
  3724. }
  3725. /* dumps the content of the cache in a human-readable format */
  3726. void dns_cache_view(rpc_t* rpc, void* ctx)
  3727. {
  3728. int h;
  3729. struct dns_hash_entry* e;
  3730. ticks_t now;
  3731. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3732. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3733. return;
  3734. }
  3735. now=get_ticks_raw();
  3736. LOCK_DNS_HASH();
  3737. for (h=0; h<DNS_HASH_SIZE; h++){
  3738. clist_foreach(&dns_hash[h], e, next){
  3739. if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  3740. && TICKS_LT(e->expire, now)
  3741. ) {
  3742. continue;
  3743. }
  3744. rpc->printf(ctx, "{\n");
  3745. dns_cache_print_entry(rpc, ctx, e);
  3746. rpc->printf(ctx, "}");
  3747. }
  3748. }
  3749. UNLOCK_DNS_HASH();
  3750. }
  3751. /* Delete all the entries from the cache.
  3752. * If del_permanent is 0, then only the
  3753. * non-permanent entries are deleted.
  3754. */
  3755. void dns_cache_flush(int del_permanent)
  3756. {
  3757. int h;
  3758. struct dns_hash_entry* e;
  3759. struct dns_hash_entry* tmp;
  3760. DBG("dns_cache_flush(): removing elements from the cache\n");
  3761. LOCK_DNS_HASH();
  3762. for (h=0; h<DNS_HASH_SIZE; h++){
  3763. clist_foreach_safe(&dns_hash[h], e, tmp, next){
  3764. if (del_permanent || ((e->ent_flags & DNS_FLAG_PERMANENT) == 0))
  3765. _dns_hash_remove(e);
  3766. }
  3767. }
  3768. UNLOCK_DNS_HASH();
  3769. }
  3770. /* deletes all the non-permanent entries from the cache */
  3771. void dns_cache_delete_all(rpc_t* rpc, void* ctx)
  3772. {
  3773. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3774. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3775. return;
  3776. }
  3777. dns_cache_flush(0);
  3778. }
  3779. /* deletes all the entries from the cache,
  3780. * even the permanent ones */
  3781. void dns_cache_delete_all_force(rpc_t* rpc, void* ctx)
  3782. {
  3783. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3784. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3785. return;
  3786. }
  3787. dns_cache_flush(1);
  3788. }
  3789. /* clones an entry and extends its memory area to hold a new rr.
  3790. * if rdata_size>0 the new dns_rr struct is initialized, but the rdata is
  3791. * only filled with 0.
  3792. */
  3793. static struct dns_hash_entry *dns_cache_clone_entry(struct dns_hash_entry *e,
  3794. int rdata_size,
  3795. int ttl,
  3796. struct dns_rr **_new_rr)
  3797. {
  3798. struct dns_hash_entry *new;
  3799. struct dns_rr *rr, *last_rr, *new_rr;
  3800. int size, rounded_size, rr_size;
  3801. ticks_t now;
  3802. int i;
  3803. now=get_ticks_raw();
  3804. size = e->total_size;
  3805. if (rdata_size) {
  3806. /* we have to extend the entry */
  3807. rounded_size = ROUND_POINTER(size); /* size may not have been
  3808. rounded previously */
  3809. switch (e->type) {
  3810. case T_A:
  3811. case T_AAAA:
  3812. case T_CNAME:
  3813. rr_size = sizeof(struct dns_rr);
  3814. break;
  3815. case T_SRV:
  3816. rr_size = ROUND_SHORT(sizeof(struct dns_rr));
  3817. break;
  3818. case T_NAPTR:
  3819. rr_size = ROUND_POINTER(sizeof(struct dns_rr));
  3820. break;
  3821. case T_TXT:
  3822. rr_size = ROUND_POINTER(sizeof(struct dns_rr));
  3823. break;
  3824. case T_EBL:
  3825. rr_size = ROUND_POINTER(sizeof(struct dns_rr));
  3826. break;
  3827. case T_PTR:
  3828. rr_size = sizeof(struct dns_rr);
  3829. break;
  3830. default:
  3831. LOG(L_ERR, "ERROR: dns_cache_clone_entry: type %d not "
  3832. "supported\n", e->type);
  3833. return NULL;
  3834. }
  3835. } else {
  3836. rounded_size = size; /* no need to round the size, we just clone
  3837. the entry without extending it */
  3838. rr_size = 0;
  3839. }
  3840. new=shm_malloc(rounded_size+rr_size+rdata_size);
  3841. if (!new) {
  3842. LOG(L_ERR, "ERROR: dns_cache_clone_entry: out of memory\n");
  3843. return NULL;
  3844. }
  3845. memset(new, 0, rounded_size+rr_size+rdata_size);
  3846. /* clone the entry */
  3847. memcpy(new, e, size);
  3848. /* fix the values and pointers */
  3849. new->next = new->prev = NULL;
  3850. #ifdef DNS_LU_LST
  3851. new->last_used_lst.next = new->last_used_lst.prev = NULL;
  3852. #endif
  3853. new->rr_lst = (struct dns_rr*)translate_pointer((char*)new, (char*)e,
  3854. (char*)new->rr_lst);
  3855. atomic_set(&new->refcnt, 0);
  3856. new->last_used = now;
  3857. /* expire and total_size are fixed later if needed */
  3858. /* fix the pointers inside the rr structures */
  3859. last_rr = NULL;
  3860. for (rr=new->rr_lst; rr; rr=rr->next) {
  3861. rr->rdata = (void*)translate_pointer((char*)new, (char*)e,
  3862. (char*)rr->rdata);
  3863. if (rr->next)
  3864. rr->next = (struct dns_rr*)translate_pointer((char*)new, (char*)e,
  3865. (char*)rr->next);
  3866. else
  3867. last_rr = rr;
  3868. switch(e->type){
  3869. case T_NAPTR:
  3870. /* there are pointers inside the NAPTR rdata stucture */
  3871. ((struct naptr_rdata*)rr->rdata)->flags =
  3872. translate_pointer((char*)new, (char*)e,
  3873. ((struct naptr_rdata*)rr->rdata)->flags);
  3874. ((struct naptr_rdata*)rr->rdata)->services =
  3875. translate_pointer((char*)new, (char*)e,
  3876. ((struct naptr_rdata*)rr->rdata)->services);
  3877. ((struct naptr_rdata*)rr->rdata)->regexp =
  3878. translate_pointer((char*)new, (char*)e,
  3879. ((struct naptr_rdata*)rr->rdata)->regexp);
  3880. ((struct naptr_rdata*)rr->rdata)->repl =
  3881. translate_pointer((char*)new, (char*)e,
  3882. ((struct naptr_rdata*)rr->rdata)->repl);
  3883. break;
  3884. case T_TXT:
  3885. /* there are pointers inside the TXT structure */
  3886. for (i=0; i<((struct txt_rdata*)rr->rdata)->cstr_no; i++){
  3887. ((struct txt_rdata*)rr->rdata)->txt[i].cstr=
  3888. translate_pointer((char*) new, (char*) e,
  3889. ((struct txt_rdata*)rr->rdata)->txt[i].cstr);
  3890. }
  3891. break;
  3892. case T_EBL:
  3893. /* there are pointers inside the EBL structure */
  3894. ((struct ebl_rdata*)rr->rdata)->separator =
  3895. translate_pointer((char*)new, (char*)e,
  3896. ((struct ebl_rdata*)rr->rdata)->separator);
  3897. ((struct ebl_rdata*)rr->rdata)->apex =
  3898. translate_pointer((char*)new, (char*)e,
  3899. ((struct ebl_rdata*)rr->rdata)->apex);
  3900. break;
  3901. }
  3902. }
  3903. if (rdata_size) {
  3904. /* set the pointer to the new rr structure */
  3905. new_rr = (void*)((char*)new + rounded_size);
  3906. new_rr->rdata = (void*)((char*)new_rr+rr_size);
  3907. new_rr->expire = now + S_TO_TICKS(ttl);
  3908. /* link the rr to the previous one */
  3909. last_rr->next = new_rr;
  3910. /* fix the total_size and expires values */
  3911. new->total_size=rounded_size+rr_size+rdata_size;
  3912. new->expire = MAX(new->expire, new_rr->expire);
  3913. if (_new_rr)
  3914. *_new_rr = new_rr;
  3915. } else {
  3916. if (_new_rr)
  3917. *_new_rr = NULL;
  3918. }
  3919. return new;
  3920. }
  3921. /* Adds a new record to the cache.
  3922. * If there is an existing record with the same name and value
  3923. * (ip address in case of A/AAAA record, name in case of SRV record)
  3924. * only the remaining fields are updated.
  3925. *
  3926. * Note that permanent records cannot be overwritten unless
  3927. * the new record is also permanent. A permanent record
  3928. * completely replaces a non-permanent one.
  3929. *
  3930. * Currently only A, AAAA, and SRV records are supported.
  3931. */
  3932. int dns_cache_add_record(unsigned short type,
  3933. str *name,
  3934. int ttl,
  3935. str *value,
  3936. int priority,
  3937. int weight,
  3938. int port,
  3939. int flags)
  3940. {
  3941. struct dns_hash_entry *old=NULL, *new=NULL;
  3942. struct dns_rr *rr;
  3943. str rr_name;
  3944. struct ip_addr *ip_addr;
  3945. ticks_t expire;
  3946. int err, h;
  3947. int size;
  3948. struct dns_rr *new_rr, **rr_p, **rr_iter;
  3949. struct srv_rdata *srv_rd;
  3950. /* eliminate gcc warnings */
  3951. ip_addr = 0;
  3952. size = 0;
  3953. rr_name.s = NULL;
  3954. rr_name.len = 0;
  3955. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3956. LOG(L_ERR, "ERROR: dns cache support disabled (see use_dns_cache)\n");
  3957. return -1;
  3958. }
  3959. if ((type != T_A) && (type != T_AAAA) && (type != T_SRV)) {
  3960. LOG(L_ERR, "ERROR: rr type %d is not implemented\n",
  3961. type);
  3962. return -1;
  3963. }
  3964. if ((flags & DNS_FLAG_BAD_NAME) == 0) {
  3965. /* fix-up the values */
  3966. switch(type) {
  3967. case T_A:
  3968. ip_addr = str2ip(value);
  3969. if (!ip_addr) {
  3970. LOG(L_ERR, "ERROR: Malformed ip address: %.*s\n",
  3971. value->len, value->s);
  3972. return -1;
  3973. }
  3974. break;
  3975. case T_AAAA:
  3976. #ifdef USE_IPV6
  3977. ip_addr = str2ip6(value);
  3978. if (!ip_addr) {
  3979. LOG(L_ERR, "ERROR: Malformed ip address: %.*s\n",
  3980. value->len, value->s);
  3981. return -1;
  3982. }
  3983. break;
  3984. #else /* USE_IPV6 */
  3985. LOG(L_ERR, "ERROR: IPv6 support is disabled\n");
  3986. return -1;
  3987. #endif /* USE_IPV6 */
  3988. case T_SRV:
  3989. rr_name = *value;
  3990. break;
  3991. }
  3992. }
  3993. /* check whether there is a matching entry in the cache */
  3994. old = dns_hash_get(name, type, &h, &err);
  3995. if (old && old->type!=type) {
  3996. /* probably we found a CNAME instead of the specified type,
  3997. it is not needed */
  3998. dns_hash_put(old);
  3999. old=NULL;
  4000. }
  4001. if (old
  4002. && (old->ent_flags & DNS_FLAG_PERMANENT)
  4003. && ((flags & DNS_FLAG_PERMANENT) == 0)
  4004. ) {
  4005. LOG(L_ERR, "ERROR: A non-permanent record cannot overwrite "
  4006. "a permanent entry\n");
  4007. goto error;
  4008. }
  4009. /* prepare the entry */
  4010. if (flags & DNS_FLAG_BAD_NAME) {
  4011. /* negative entry */
  4012. new = dns_cache_mk_bad_entry(name, type, ttl, flags);
  4013. if (!new) {
  4014. LOG(L_ERR, "ERROR: Failed to create a negative "
  4015. "DNS cache entry\n");
  4016. goto error;
  4017. }
  4018. } else {
  4019. if (!old
  4020. || (old->ent_flags & DNS_FLAG_BAD_NAME)
  4021. || (((old->ent_flags & DNS_FLAG_PERMANENT) == 0)
  4022. && (flags & DNS_FLAG_PERMANENT))
  4023. ) {
  4024. /* There was no matching entry in the hash table,
  4025. * the entry is a negative record with inefficient space,
  4026. * or a permanent entry overwrites a non-permanent one.
  4027. * Let us create a new one.
  4028. */
  4029. switch(type) {
  4030. case T_A:
  4031. case T_AAAA:
  4032. new = dns_cache_mk_ip_entry(name, ip_addr);
  4033. if (!new) {
  4034. LOG(L_ERR, "ERROR: Failed to create an A/AAAA record\n");
  4035. goto error;
  4036. }
  4037. /* fix the expiration time, dns_cache_mk_ip_entry() sets it
  4038. * to now-1 */
  4039. expire = get_ticks_raw() + S_TO_TICKS(ttl);
  4040. new->expire = expire;
  4041. new->rr_lst->expire = expire;
  4042. break;
  4043. case T_SRV:
  4044. new = dns_cache_mk_srv_entry(name, priority, weight, port,
  4045. &rr_name, ttl);
  4046. if (!new) {
  4047. LOG(L_ERR, "ERROR: Failed to create an SRV record\n");
  4048. goto error;
  4049. }
  4050. }
  4051. new->ent_flags = flags;
  4052. } else {
  4053. /* we must modify the entry, so better to clone it, modify the new
  4054. * one, and replace the old with the new entry in the hash table,
  4055. * because the entry might be in use (even if the dns hash is
  4056. * locked). The old entry will be removed from the hash and
  4057. * automatically destroyed when its refcnt will be 0*/
  4058. /* check whether there is an rr with the same value */
  4059. for (rr=old->rr_lst; rr; rr=rr->next)
  4060. if ((((type == T_A) || (type == T_AAAA)) &&
  4061. (memcmp(ip_addr->u.addr, ((struct a_rdata*)rr->rdata)->ip,
  4062. ip_addr->len)==0))
  4063. || ((type == T_SRV) &&
  4064. (((struct srv_rdata*)rr->rdata)->name_len == rr_name.len)&&
  4065. (memcmp(rr_name.s, ((struct srv_rdata*)rr->rdata)->name,
  4066. rr_name.len)==0)))
  4067. break;
  4068. if (rr) {
  4069. /* the rr was found in the list */
  4070. new = dns_cache_clone_entry(old, 0, 0, 0);
  4071. if (!new) {
  4072. LOG(L_ERR, "ERROR: Failed to clone an existing "
  4073. "DNS cache entry\n");
  4074. goto error;
  4075. }
  4076. /* let the rr point to the new structure */
  4077. rr = (struct dns_rr*)translate_pointer((char*)new, (char*)old,
  4078. (char*)rr);
  4079. new_rr = rr;
  4080. if (type == T_SRV) {
  4081. /* fix the priority, weight, and port */
  4082. ((struct srv_rdata*)rr->rdata)->priority = priority;
  4083. ((struct srv_rdata*)rr->rdata)->weight = weight;
  4084. ((struct srv_rdata*)rr->rdata)->port = port;
  4085. }
  4086. /* fix the expire value */
  4087. rr->expire = get_ticks_raw() + S_TO_TICKS(ttl);
  4088. new->expire = 0;
  4089. for (rr=new->rr_lst; rr; rr=rr->next)
  4090. new->expire = MAX(new->expire, rr->expire);
  4091. } else {
  4092. /* there was no matching rr, extend the structure with a new
  4093. * one */
  4094. switch(type) {
  4095. case T_A:
  4096. size = sizeof(struct a_rdata);
  4097. break;
  4098. case T_AAAA:
  4099. size = sizeof(struct aaaa_rdata);
  4100. break;
  4101. case T_SRV:
  4102. size = sizeof(struct srv_rdata)-1 +
  4103. rr_name.len+1;
  4104. break;
  4105. }
  4106. new = dns_cache_clone_entry(old, size, ttl, &rr);
  4107. if (!new) {
  4108. LOG(L_ERR, "ERROR: Failed to clone an existing "
  4109. "DNS cache entry\n");
  4110. goto error;
  4111. }
  4112. new_rr = rr;
  4113. switch(type) {
  4114. case T_A:
  4115. case T_AAAA:
  4116. memcpy(rr->rdata, ip_addr->u.addr, ip_addr->len);
  4117. break;
  4118. case T_SRV:
  4119. ((struct srv_rdata*)rr->rdata)->priority = priority;
  4120. ((struct srv_rdata*)rr->rdata)->weight = weight;
  4121. ((struct srv_rdata*)rr->rdata)->port = port;
  4122. ((struct srv_rdata*)rr->rdata)->name_len = rr_name.len;
  4123. memcpy(((struct srv_rdata*)rr->rdata)->name, rr_name.s,
  4124. rr_name.len);
  4125. }
  4126. /* maximum expire value has been already fixed by
  4127. * dns_cache_clone_entry() */
  4128. }
  4129. if (type == T_SRV) {
  4130. /* SRV records must be ordered by their priority and weight.
  4131. * With modifying an exising rr, or adding new rr to the DNS entry,
  4132. * the ordered list might got broken which needs to be fixed.
  4133. */
  4134. rr_p = NULL;
  4135. for ( rr_iter = &new->rr_lst;
  4136. *rr_iter;
  4137. rr_iter = &((*rr_iter)->next)
  4138. ) {
  4139. if (*rr_iter == new_rr) {
  4140. rr_p = rr_iter;
  4141. continue;
  4142. }
  4143. srv_rd = (struct srv_rdata*)(*rr_iter)->rdata;
  4144. if ((priority < srv_rd->priority) ||
  4145. ((priority == srv_rd->priority) && (weight > srv_rd->weight))
  4146. )
  4147. break; /* insert here */
  4148. }
  4149. if (!rr_p)
  4150. for ( rr_p = rr_iter;
  4151. *rr_p && (*rr_p != new_rr);
  4152. rr_p = &((*rr_p)->next)
  4153. );
  4154. if (!rr_p) {
  4155. LOG(L_ERR, "ERROR: Failed to correct the orderd list of SRV resource records\n");
  4156. goto error;
  4157. }
  4158. if (*rr_iter != new_rr->next) {
  4159. /* unlink rr from the list */
  4160. *rr_p = (*rr_p)->next;
  4161. /* link it before *rr_iter */
  4162. new_rr->next = *rr_iter;
  4163. *rr_iter = new_rr;
  4164. }
  4165. }
  4166. }
  4167. }
  4168. LOCK_DNS_HASH();
  4169. if (dns_cache_add_unsafe(new)) {
  4170. LOG(L_ERR, "ERROR: Failed to add the entry to the cache\n");
  4171. UNLOCK_DNS_HASH();
  4172. goto error;
  4173. } else {
  4174. /* remove the old entry from the list */
  4175. if (old)
  4176. _dns_hash_remove(old);
  4177. }
  4178. UNLOCK_DNS_HASH();
  4179. if (old)
  4180. dns_hash_put(old);
  4181. return 0;
  4182. error:
  4183. /* leave the old entry in the list, and free the new one */
  4184. if (old)
  4185. dns_hash_put(old);
  4186. if (new)
  4187. dns_destroy_entry(new);
  4188. return -1;
  4189. }
  4190. /* deletes a record from the cache */
  4191. static void dns_cache_delete_record(rpc_t* rpc, void* ctx, unsigned short type)
  4192. {
  4193. struct dns_hash_entry *e;
  4194. str name;
  4195. int err, h, found=0, permanent=0;
  4196. if (!cfg_get(core, core_cfg, use_dns_cache)){
  4197. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  4198. return;
  4199. }
  4200. if (rpc->scan(ctx, "S", &name) < 1)
  4201. return;
  4202. LOCK_DNS_HASH();
  4203. e=_dns_hash_find(&name, type, &h, &err);
  4204. if (e && (e->type==type)) {
  4205. if ((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  4206. _dns_hash_remove(e);
  4207. else
  4208. permanent = 1;
  4209. found = 1;
  4210. }
  4211. UNLOCK_DNS_HASH();
  4212. if (permanent)
  4213. rpc->fault(ctx, 400, "Permanent entries cannot be deleted");
  4214. else if (!found)
  4215. rpc->fault(ctx, 400, "Not found");
  4216. }
  4217. /* Delete a single record from the cache,
  4218. * i.e. the record with the same name and value
  4219. * (ip address in case of A/AAAA record, name in case of SRV record).
  4220. *
  4221. * Currently only A, AAAA, and SRV records are supported.
  4222. */
  4223. int dns_cache_delete_single_record(unsigned short type,
  4224. str *name,
  4225. str *value,
  4226. int flags)
  4227. {
  4228. struct dns_hash_entry *old=NULL, *new=NULL;
  4229. struct dns_rr *rr, **next_p;
  4230. str rr_name;
  4231. struct ip_addr *ip_addr;
  4232. int err, h;
  4233. /* eliminate gcc warnings */
  4234. rr_name.s = NULL;
  4235. rr_name.len = 0;
  4236. ip_addr = 0;
  4237. if (!cfg_get(core, core_cfg, use_dns_cache)){
  4238. LOG(L_ERR, "ERROR: dns cache support disabled (see use_dns_cache)\n");
  4239. return -1;
  4240. }
  4241. if ((type != T_A) && (type != T_AAAA) && (type != T_SRV)) {
  4242. LOG(L_ERR, "ERROR: rr type %d is not implemented\n",
  4243. type);
  4244. return -1;
  4245. }
  4246. if ((flags & DNS_FLAG_BAD_NAME) == 0) {
  4247. /* fix-up the values */
  4248. switch(type) {
  4249. case T_A:
  4250. ip_addr = str2ip(value);
  4251. if (!ip_addr) {
  4252. LOG(L_ERR, "ERROR: Malformed ip address: %.*s\n",
  4253. value->len, value->s);
  4254. return -1;
  4255. }
  4256. break;
  4257. case T_AAAA:
  4258. #ifdef USE_IPV6
  4259. ip_addr = str2ip6(value);
  4260. if (!ip_addr) {
  4261. LOG(L_ERR, "ERROR: Malformed ip address: %.*s\n",
  4262. value->len, value->s);
  4263. return -1;
  4264. }
  4265. break;
  4266. #else /* USE_IPV6 */
  4267. LOG(L_ERR, "ERROR: IPv6 support is disabled\n");
  4268. return -1;
  4269. #endif /* USE_IPV6 */
  4270. case T_SRV:
  4271. rr_name = *value;
  4272. break;
  4273. }
  4274. }
  4275. /* check whether there is a matching entry in the cache */
  4276. if ((old = dns_hash_get(name, type, &h, &err)) == NULL)
  4277. goto not_found;
  4278. if ((old->type != type) /* may be CNAME */
  4279. || (old->ent_flags != flags)
  4280. )
  4281. goto not_found;
  4282. if (flags & DNS_FLAG_BAD_NAME) /* negative record, there is no value */
  4283. goto delete;
  4284. /* check whether there is an rr with the same value */
  4285. for (rr=old->rr_lst, next_p=&old->rr_lst;
  4286. rr;
  4287. next_p=&rr->next, rr=rr->next
  4288. )
  4289. if ((((type == T_A) || (type == T_AAAA)) &&
  4290. (memcmp(ip_addr->u.addr, ((struct a_rdata*)rr->rdata)->ip,
  4291. ip_addr->len)==0))
  4292. || ((type == T_SRV) &&
  4293. (((struct srv_rdata*)rr->rdata)->name_len == rr_name.len) &&
  4294. (memcmp(rr_name.s, ((struct srv_rdata*)rr->rdata)->name,
  4295. rr_name.len)==0)))
  4296. break;
  4297. if (!rr)
  4298. goto not_found;
  4299. if ((rr == old->rr_lst) && (rr->next == NULL)) {
  4300. /* There is a single rr value, hence the whole
  4301. * hash entry can be deleted */
  4302. goto delete;
  4303. } else {
  4304. /* we must modify the entry, so better to clone it, modify the new
  4305. * one, and replace the old with the new entry in the hash table,
  4306. * because the entry might be in use (even if the dns hash is
  4307. * locked). The old entry will be removed from the hash and
  4308. * automatically destroyed when its refcnt will be 0*/
  4309. new = dns_cache_clone_entry(old, 0, 0, 0);
  4310. if (!new) {
  4311. LOG(L_ERR, "ERROR: Failed to clone an existing "
  4312. "DNS cache entry\n");
  4313. dns_hash_put(old);
  4314. return -1;
  4315. }
  4316. /* let rr and next_p point to the new structure */
  4317. rr = (struct dns_rr*)translate_pointer((char*)new,
  4318. (char*)old,
  4319. (char*)rr);
  4320. next_p = (struct dns_rr**)translate_pointer((char*)new,
  4321. (char*)old,
  4322. (char*)next_p);
  4323. /* unlink rr from the list. The memory will be freed
  4324. * when the whole record is freed */
  4325. *next_p = rr->next;
  4326. }
  4327. delete:
  4328. LOCK_DNS_HASH();
  4329. if (new) {
  4330. /* delete the old entry only if the new one can be added */
  4331. if (dns_cache_add_unsafe(new)) {
  4332. LOG(L_ERR, "ERROR: Failed to add the entry to the cache\n");
  4333. UNLOCK_DNS_HASH();
  4334. if (old)
  4335. dns_hash_put(old);
  4336. return -1;
  4337. } else {
  4338. /* remove the old entry from the list */
  4339. if (old)
  4340. _dns_hash_remove(old);
  4341. }
  4342. } else if (old) {
  4343. _dns_hash_remove(old);
  4344. }
  4345. UNLOCK_DNS_HASH();
  4346. if (old)
  4347. dns_hash_put(old);
  4348. return 0;
  4349. not_found:
  4350. LOG(L_ERR, "ERROR: No matching record found\n");
  4351. if (old)
  4352. dns_hash_put(old);
  4353. return -1;
  4354. }
  4355. /* performs a dns lookup over rpc */
  4356. void dns_cache_rpc_lookup(rpc_t* rpc, void* ctx)
  4357. {
  4358. struct dns_hash_entry *e;
  4359. str name;
  4360. str type;
  4361. int t;
  4362. if (!cfg_get(core, core_cfg, use_dns_cache)){
  4363. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  4364. return;
  4365. }
  4366. if (rpc->scan(ctx, "SS", &type, &name) < 1)
  4367. return;
  4368. t=dns_get_type(&type);
  4369. if (t<0){
  4370. rpc->fault(ctx, 400, "Invalid type");
  4371. return;
  4372. }
  4373. e=dns_get_entry(&name, t);
  4374. if (e==0){
  4375. rpc->fault(ctx, 400, "Not found");
  4376. return;
  4377. }
  4378. dns_cache_print_entry(rpc, ctx, e);
  4379. dns_hash_put(e);
  4380. }
  4381. /* wrapper functions for adding and deleting records */
  4382. void dns_cache_add_a(rpc_t* rpc, void* ctx)
  4383. {
  4384. str name;
  4385. int ttl;
  4386. str ip;
  4387. int flags;
  4388. if (rpc->scan(ctx, "SdSd", &name, &ttl, &ip, &flags) < 4)
  4389. return;
  4390. if (dns_cache_add_record(T_A,
  4391. &name,
  4392. ttl,
  4393. &ip,
  4394. 0 /* priority */,
  4395. 0 /* weight */,
  4396. 0 /* port */,
  4397. flags)
  4398. )
  4399. rpc->fault(ctx, 400, "Failed to add the entry to the cache");
  4400. }
  4401. void dns_cache_add_aaaa(rpc_t* rpc, void* ctx)
  4402. {
  4403. str name;
  4404. int ttl;
  4405. str ip;
  4406. int flags;
  4407. if (rpc->scan(ctx, "SdSd", &name, &ttl, &ip, &flags) < 4)
  4408. return;
  4409. if (dns_cache_add_record(T_AAAA,
  4410. &name,
  4411. ttl,
  4412. &ip,
  4413. 0 /* priority */,
  4414. 0 /* weight */,
  4415. 0 /* port */,
  4416. flags)
  4417. )
  4418. rpc->fault(ctx, 400, "Failed to add the entry to the cache");
  4419. }
  4420. void dns_cache_add_srv(rpc_t* rpc, void* ctx)
  4421. {
  4422. str name;
  4423. int ttl, priority, weight, port;
  4424. str rr_name;
  4425. int flags;
  4426. if (rpc->scan(ctx, "SddddSd", &name, &ttl, &priority, &weight, &port,
  4427. &rr_name, &flags) < 7
  4428. )
  4429. return;
  4430. if (dns_cache_add_record(T_SRV,
  4431. &name,
  4432. ttl,
  4433. &rr_name,
  4434. priority,
  4435. weight,
  4436. port,
  4437. flags)
  4438. )
  4439. rpc->fault(ctx, 400, "Failed to add the entry to the cache");
  4440. }
  4441. void dns_cache_delete_a(rpc_t* rpc, void* ctx)
  4442. {
  4443. dns_cache_delete_record(rpc, ctx, T_A);
  4444. }
  4445. void dns_cache_delete_aaaa(rpc_t* rpc, void* ctx)
  4446. {
  4447. dns_cache_delete_record(rpc, ctx, T_AAAA);
  4448. }
  4449. void dns_cache_delete_srv(rpc_t* rpc, void* ctx)
  4450. {
  4451. dns_cache_delete_record(rpc, ctx, T_SRV);
  4452. }
  4453. void dns_cache_delete_naptr(rpc_t* rpc, void* ctx)
  4454. {
  4455. dns_cache_delete_record(rpc, ctx, T_NAPTR);
  4456. }
  4457. void dns_cache_delete_cname(rpc_t* rpc, void* ctx)
  4458. {
  4459. dns_cache_delete_record(rpc, ctx, T_CNAME);
  4460. }
  4461. void dns_cache_delete_txt(rpc_t* rpc, void* ctx)
  4462. {
  4463. dns_cache_delete_record(rpc, ctx, T_TXT);
  4464. }
  4465. void dns_cache_delete_ebl(rpc_t* rpc, void* ctx)
  4466. {
  4467. dns_cache_delete_record(rpc, ctx, T_EBL);
  4468. }
  4469. void dns_cache_delete_ptr(rpc_t* rpc, void* ctx)
  4470. {
  4471. dns_cache_delete_record(rpc, ctx, T_PTR);
  4472. }
  4473. #ifdef DNS_WATCHDOG_SUPPORT
  4474. /* sets the DNS server states */
  4475. void dns_set_server_state_rpc(rpc_t* rpc, void* ctx)
  4476. {
  4477. int state;
  4478. if (!cfg_get(core, core_cfg, use_dns_cache)){
  4479. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  4480. return;
  4481. }
  4482. if (rpc->scan(ctx, "d", &state) < 1)
  4483. return;
  4484. dns_set_server_state(state);
  4485. }
  4486. /* prints the DNS server state */
  4487. void dns_get_server_state_rpc(rpc_t* rpc, void* ctx)
  4488. {
  4489. if (!cfg_get(core, core_cfg, use_dns_cache)){
  4490. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  4491. return;
  4492. }
  4493. rpc->add(ctx, "d", dns_get_server_state());
  4494. }
  4495. #endif /* DNS_WATCHDOG_SUPPORT */
  4496. #endif