dns_cache.c 130 KB

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