dns_cache.c 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765
  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. if(proto) {
  2601. origproto=*proto;
  2602. } else {
  2603. origproto=PROTO_NONE;
  2604. }
  2605. he=0;
  2606. if (dns_hash==0){ /* not init => use normal, non-cached version */
  2607. LM_WARN("called before dns cache initialization\n");
  2608. return _sip_resolvehost(name, port, proto);
  2609. }
  2610. if (proto && port && (*proto==0) && (*port==0)){
  2611. *proto=PROTO_UDP; /* just in case we don't find another */
  2612. /* check if it's an ip address */
  2613. if ( ((tmp_ip=str2ip(name))!=0)
  2614. || ((tmp_ip=str2ip6(name))!=0)
  2615. ){
  2616. /* we are lucky, this is an ip address */
  2617. if (((dns_flags&DNS_IPV4_ONLY) && (tmp_ip->af==AF_INET6))||
  2618. ((dns_flags&DNS_IPV6_ONLY) && (tmp_ip->af==AF_INET))){
  2619. return 0;
  2620. }
  2621. *port=SIP_PORT;
  2622. return ip_addr2he(name, tmp_ip);
  2623. }
  2624. /* do naptr lookup */
  2625. if ((e=dns_get_entry(name, T_NAPTR))==0)
  2626. goto naptr_not_found;
  2627. naptr_iterate_init(&tried_bmp);
  2628. while(dns_naptr_sip_iterate(e->rr_lst, &tried_bmp,
  2629. &srv_name, &n_proto)){
  2630. if ((he=dns_srv_get_he(&srv_name, port, dns_flags))!=0){
  2631. #ifdef DNS_CACHE_DEBUG
  2632. DBG("dns_naptr_sip_resolvehost(%.*s, %d, %d) srv, ret=%p\n",
  2633. name->len, name->s, (int)*port, (int)*proto, he);
  2634. #endif
  2635. dns_hash_put(e);
  2636. *proto=n_proto;
  2637. return he;
  2638. }
  2639. }
  2640. /* no acceptable naptr record found, fallback to srv */
  2641. dns_hash_put(e);
  2642. }
  2643. naptr_not_found:
  2644. if(proto) *proto = origproto;
  2645. he = no_naptr_srv_sip_resolvehost(name,port,proto);
  2646. /* fallback all the way down to A/AAAA */
  2647. if (he==0) {
  2648. he=dns_get_he(name,dns_flags);
  2649. }
  2650. return he;
  2651. }
  2652. #endif /* USE_NAPTR */
  2653. /* resolves a host name trying NAPTR lookup if *proto==0 and *port==0, SRV
  2654. * lookup if *port==0 or normal A/AAAA lookup
  2655. * if *port!=0.
  2656. * when performing SRV lookup (*port==0) it will use proto to look for
  2657. * tcp or udp hosts; if proto==0 => no SRV lookup
  2658. * returns: hostent struct & *port filled with the port from the SRV record;
  2659. * 0 on error
  2660. */
  2661. struct hostent* dns_sip_resolvehost(str* name, unsigned short* port,
  2662. char* proto)
  2663. {
  2664. #ifdef USE_NAPTR
  2665. if (dns_flags&DNS_TRY_NAPTR)
  2666. return dns_naptr_sip_resolvehost(name, port, proto);
  2667. #endif
  2668. return dns_srv_sip_resolvehost(name, port, proto);
  2669. }
  2670. /* performs an a lookup, fills the dns_entry pointer and the ip addr.
  2671. * (with the first good ip). if *e ==0 does the a lookup, and changes it
  2672. * to the result, if not it uses the current value and tries to use
  2673. * the rr_no record from it.
  2674. * params: e - must contain the "in-use" dns_hash_entry pointer (from
  2675. * a previous call) or *e==0 (for the first call)
  2676. * name - host name for which we do the lookup (required only
  2677. * when *e==0)
  2678. * ip - will be filled with the first good resolved ip started
  2679. * at *rr_no
  2680. * rr_no - record number to start searching for a good ip from
  2681. * (e.g. value from previous call + 1), filled on return
  2682. * with the number of the record corresponding to the
  2683. * returned ip
  2684. * returns 0 on success, <0 on error (see the error codes),
  2685. * fills e, ip and rr_no
  2686. * On end of records (when used to iterate on all the ips) it
  2687. * will return E_DNS_EOR (you should not log an error for this
  2688. * value, is just a signal that the address list end has been reached)
  2689. * Note: either e or name must be different from 0 (name.s !=0 also)
  2690. * WARNING: dns_hash_put(*e) must be called when you don't need
  2691. * the entry anymore and *e!=0 (failling to do so => mem. leak)
  2692. * Example:
  2693. * dns_entry=0;
  2694. * ret=dns_a_get_ip(&dns_entry, name, &ip, &rr_no); -- get the first rr.
  2695. * ...
  2696. * rr_no++;
  2697. * while((ret>=0) && dns_entry)
  2698. * dns_a_get_ip(&dns_entry, name, &ip, &rr_no); -- get the next rr
  2699. * if (ret!=-E_DNS_EOR) ERROR(....);
  2700. * ...
  2701. * dns_hash_put(dns_entry); -- finished with the entry
  2702. */
  2703. inline static int dns_a_resolve( struct dns_hash_entry** e,
  2704. unsigned char* rr_no,
  2705. str* name,
  2706. struct ip_addr* ip)
  2707. {
  2708. struct dns_rr* rr;
  2709. int ret;
  2710. ticks_t now;
  2711. struct ip_addr* tmp;
  2712. rr=0;
  2713. ret=-E_DNS_NO_IP;
  2714. if (*e==0){ /* do lookup */
  2715. /* if ip don't set *e */
  2716. if (str2ip6(name)!=0)
  2717. goto error;
  2718. if ((tmp=str2ip(name))!=0){
  2719. *ip=*tmp;
  2720. *rr_no=0;
  2721. return 0;
  2722. }
  2723. if ((*e=dns_get_entry(name, T_A))==0)
  2724. goto error;
  2725. /* found */
  2726. *rr_no=0;
  2727. ret=-E_DNS_BAD_IP_ENTRY;
  2728. }
  2729. now=get_ticks_raw();
  2730. /* if the entry has already expired use the time at the end of lifetime */
  2731. if (unlikely((s_ticks_t)(now-(*e)->expire)>=0)) now=(*e)->expire-1;
  2732. rr=dns_entry_get_rr(*e, rr_no, now);
  2733. if (rr){
  2734. /* everything is ok now, we can try to "convert" the ip */
  2735. dns_rr2ip((*e)->type, rr, ip);
  2736. ret=0;
  2737. }else{
  2738. ret=-E_DNS_EOR;
  2739. }
  2740. error:
  2741. DBG("dns_a_resolve(%.*s, %d) returning %d\n",
  2742. name->len, name->s, *rr_no, ret);
  2743. return ret;
  2744. }
  2745. /* lookup, fills the dns_entry pointer and the ip addr.
  2746. * (with the first good ip). if *e ==0 does the a lookup, and changes it
  2747. * to the result, if not it uses the current value and tries to use
  2748. * Same as dns_a_resolve but for aaaa records (see above).
  2749. */
  2750. inline static int dns_aaaa_resolve( struct dns_hash_entry** e,
  2751. unsigned char* rr_no,
  2752. str* name,
  2753. struct ip_addr* ip)
  2754. {
  2755. struct dns_rr* rr;
  2756. int ret;
  2757. ticks_t now;
  2758. struct ip_addr* tmp;
  2759. rr=0;
  2760. ret=-E_DNS_NO_IP;
  2761. if (*e==0){ /* do lookup */
  2762. /* if ip don't set *e */
  2763. if (str2ip(name)!=0)
  2764. goto error;
  2765. if ((tmp=str2ip6(name))!=0){
  2766. *ip=*tmp;
  2767. *rr_no=0;
  2768. return 0;
  2769. }
  2770. if ((*e=dns_get_entry(name, T_AAAA))==0)
  2771. goto error;
  2772. /* found */
  2773. *rr_no=0;
  2774. ret=-E_DNS_BAD_IP_ENTRY;
  2775. }
  2776. now=get_ticks_raw();
  2777. /* if the entry has already expired use the time at the end of lifetime */
  2778. if (unlikely((s_ticks_t)(now-(*e)->expire)>=0)) now=(*e)->expire-1;
  2779. rr=dns_entry_get_rr(*e, rr_no, now);
  2780. if (rr){
  2781. /* everything is ok now, we can try to "convert" the ip */
  2782. dns_rr2ip((*e)->type, rr, ip);
  2783. ret=0;
  2784. }else{
  2785. ret=-E_DNS_EOR; /* no more records */
  2786. }
  2787. error:
  2788. return ret;
  2789. }
  2790. /* performs an a or aaaa dns lookup, returns <0 on error (see the
  2791. * dns error codes) and 0 on success
  2792. * flags: - none set: tries first an a_lookup and if it fails an aaaa_lookup
  2793. * - DNS_IPV6_FIRST: tries first an aaaa_lookup and then an a_lookup
  2794. * - DNS_IPV4_ONLY: tries only an a_lookup
  2795. * - DNS_IPV6_ONLY: tries only an aaaa_lookup
  2796. * see dns_a_resolve() for the rest of the params., examples a.s.o
  2797. * WARNING: don't forget dns_hash_put(*e) when e is not needed anymore
  2798. */
  2799. inline static int dns_ip_resolve( struct dns_hash_entry** e,
  2800. unsigned char* rr_no,
  2801. str* name,
  2802. struct ip_addr* ip,
  2803. int flags)
  2804. {
  2805. int ret, orig_ret;
  2806. str host;
  2807. struct dns_hash_entry* orig;
  2808. ret=-E_DNS_NO_IP;
  2809. if (*e==0){ /* first call */
  2810. if ((flags&(DNS_IPV6_FIRST|DNS_IPV6_ONLY))){
  2811. ret=dns_aaaa_resolve(e, rr_no, name, ip);
  2812. if (ret>=0) return ret;
  2813. }else{
  2814. ret=dns_a_resolve(e, rr_no, name, ip);
  2815. if (ret>=0) return ret;
  2816. }
  2817. if (flags&DNS_IPV6_FIRST){
  2818. ret=dns_a_resolve(e, rr_no, name, ip);
  2819. }else if (!(flags&(DNS_IPV6_ONLY|DNS_IPV4_ONLY))){
  2820. ret=dns_aaaa_resolve(e, rr_no, name, ip);
  2821. }
  2822. }else if ((*e)->type==T_A){
  2823. /* continue A resolving */
  2824. /* retrieve host name from the hash entry (ignore name which might
  2825. be null when continuing a srv lookup) */
  2826. host.s=(*e)->name;
  2827. host.len=(*e)->name_len;
  2828. ret=dns_a_resolve(e, rr_no, &host, ip);
  2829. if (ret>=0) return ret;
  2830. if (!(flags&(DNS_IPV6_ONLY|DNS_IPV6_FIRST|DNS_IPV4_ONLY))){
  2831. /* not found, try with AAAA */
  2832. orig_ret=ret;
  2833. orig=*e;
  2834. *e=0;
  2835. *rr_no=0;
  2836. ret=dns_aaaa_resolve(e, rr_no, &host, ip);
  2837. if (ret==-E_DNS_NO_IP && orig_ret==-E_DNS_EOR)
  2838. ret=orig_ret;
  2839. /* delay original record release until we're finished with host*/
  2840. dns_hash_put(orig);
  2841. }
  2842. }else if ((*e)->type==T_AAAA){
  2843. /* retrieve host name from the hash entry (ignore name which might
  2844. be null when continuing a srv lookup) */
  2845. host.s=(*e)->name;
  2846. host.len=(*e)->name_len;
  2847. /* continue AAAA resolving */
  2848. ret=dns_aaaa_resolve(e, rr_no, &host, ip);
  2849. if (ret>=0) return ret;
  2850. if ((flags&DNS_IPV6_FIRST) && !(flags&DNS_IPV6_ONLY)){
  2851. /* not found, try with A */
  2852. orig_ret=ret;
  2853. orig=*e;
  2854. *e=0;
  2855. *rr_no=0;
  2856. ret=dns_a_resolve(e, rr_no, &host, ip);
  2857. if (ret==-E_DNS_NO_IP && orig_ret==-E_DNS_EOR)
  2858. ret=orig_ret;
  2859. /* delay original record release until we're finished with host*/
  2860. dns_hash_put(orig);
  2861. }
  2862. }else{
  2863. LM_CRIT("invalid record type %d\n", (*e)->type);
  2864. }
  2865. return ret;
  2866. }
  2867. /* gets the first srv record starting at rr_no
  2868. * Next call will return the next record a.s.o.
  2869. * (similar to dns_a_resolve but for srv, sets host, port and automatically
  2870. * switches to the next record in the future)
  2871. *
  2872. * if DNS_SRV_LB and tried!=NULL will do random weight based selection
  2873. * for choosing between SRV RRs with the same priority (as described in
  2874. * RFC2782).
  2875. * If tried==NULL or DNS_SRV_LB is not defined => always returns next
  2876. * record in the priority order and for records with the same priority
  2877. * the record with the higher weight (from the remaining ones)
  2878. */
  2879. inline static int dns_srv_resolve_nxt(struct dns_hash_entry** e,
  2880. #ifdef DNS_SRV_LB
  2881. srv_flags_t* tried,
  2882. #endif
  2883. unsigned char* rr_no,
  2884. str* name, str* host, unsigned short* port)
  2885. {
  2886. struct dns_rr* rr;
  2887. int ret;
  2888. ticks_t now;
  2889. rr=0;
  2890. ret=-E_DNS_NO_SRV;
  2891. if (*e==0){
  2892. if ((*e=dns_get_entry(name, T_SRV))==0)
  2893. goto error;
  2894. /* found it */
  2895. *rr_no=0;
  2896. #ifdef DNS_SRV_LB
  2897. if (tried)
  2898. srv_reset_tried(tried);
  2899. #endif
  2900. ret=-E_DNS_BAD_SRV_ENTRY;
  2901. }
  2902. now=get_ticks_raw();
  2903. /* if the entry has already expired use the time at the end of lifetime */
  2904. if (unlikely((s_ticks_t)(now-(*e)->expire)>=0)) now=(*e)->expire-1;
  2905. #ifdef DNS_SRV_LB
  2906. if (tried){
  2907. rr=dns_srv_get_nxt_rr(*e, tried, rr_no, now);
  2908. }else
  2909. #endif
  2910. {
  2911. rr=dns_entry_get_rr(*e, rr_no, now);
  2912. (*rr_no)++; /* try next record next time */
  2913. }
  2914. if (rr){
  2915. host->s=((struct srv_rdata*)rr->rdata)->name;
  2916. host->len=((struct srv_rdata*)rr->rdata)->name_len;
  2917. *port=((struct srv_rdata*)rr->rdata)->port;
  2918. ret=0;
  2919. }else{
  2920. ret=-E_DNS_EOR; /* no more records */
  2921. }
  2922. error:
  2923. return ret;
  2924. }
  2925. /* gets the first srv record starting at h->srv_no, resolve it
  2926. * and get the first ip address (starting at h->ip_no)
  2927. * (similar to dns_a_resolve but for srv, sets host, port)
  2928. * WARNING: don't forget to init h prior to calling this function the first
  2929. * time and dns_srv_handle_put(h), even if error is returned
  2930. */
  2931. inline static int dns_srv_resolve_ip(struct dns_srv_handle* h,
  2932. str* name, struct ip_addr* ip, unsigned short* port,
  2933. int flags)
  2934. {
  2935. int ret;
  2936. str host;
  2937. host.len=0;
  2938. host.s=0;
  2939. do{
  2940. if (h->a==0){
  2941. #ifdef DNS_SRV_LB
  2942. if ((ret=dns_srv_resolve_nxt(&h->srv,
  2943. (flags & DNS_SRV_RR_LB)?&h->srv_tried_rrs:0,
  2944. &h->srv_no,
  2945. name, &host, port))<0)
  2946. goto error;
  2947. #else
  2948. if ((ret=dns_srv_resolve_nxt(&h->srv, &h->srv_no,
  2949. name, &host, port))<0)
  2950. goto error;
  2951. #endif
  2952. h->port=*port; /* store new port */
  2953. }else{
  2954. *port=h->port; /* return the stored port */
  2955. }
  2956. if ((ret=dns_ip_resolve(&h->a, &h->ip_no, &host, ip, flags))<0){
  2957. /* couldn't find any good ip for this record, try the next one */
  2958. if (h->a){
  2959. dns_hash_put(h->a);
  2960. h->a=0;
  2961. }
  2962. }else if (h->a==0){
  2963. /* this was an ip, try the next srv record in the future */
  2964. }
  2965. }while(ret<0);
  2966. error:
  2967. #ifdef DNS_CACHE_DEBUG
  2968. DBG("dns_srv_resolve_ip(\"%.*s\", %d, %d), ret=%d, ip=%s\n",
  2969. name->len, name->s, h->srv_no, h->ip_no, ret,
  2970. ip?ZSW(ip_addr2a(ip)):"");
  2971. #endif
  2972. return ret;
  2973. }
  2974. /* resolves a host name trying SRV lookup if *port==0 or normal A/AAAA lookup
  2975. * if *port!=0.
  2976. * when performing SRV lookup (*port==0) it will use proto to look for
  2977. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  2978. * h must be initialized prior to calling this function and can be used to
  2979. * get the subsequent ips
  2980. * returns: <0 on error
  2981. * 0 on success and it fills *ip, *port, *h
  2982. */
  2983. inline static int dns_srv_sip_resolve(struct dns_srv_handle* h, str* name,
  2984. struct ip_addr* ip, unsigned short* port, char* proto,
  2985. int flags)
  2986. {
  2987. struct dns_srv_proto srv_proto_list[PROTO_LAST];
  2988. static char tmp[MAX_DNS_NAME]; /* tmp. buff. for SRV lookups */
  2989. str srv_name;
  2990. struct ip_addr* tmp_ip;
  2991. int ret;
  2992. struct hostent* he;
  2993. size_t i,list_len;
  2994. char origproto;
  2995. origproto = *proto;
  2996. if (dns_hash==0){ /* not init => use normal, non-cached version */
  2997. LM_WARN("called before dns cache initialization\n");
  2998. h->srv=h->a=0;
  2999. he=_sip_resolvehost(name, port, proto);
  3000. if (he){
  3001. hostent2ip_addr(ip, he, 0);
  3002. return 0;
  3003. }
  3004. return -E_DNS_NO_SRV;
  3005. }
  3006. if ((h->srv==0) && (h->a==0)){ /* first call */
  3007. if (proto && *proto==0){ /* makes sure we have a protocol set*/
  3008. *proto=PROTO_UDP; /* default */
  3009. }
  3010. h->port=(*proto==PROTO_TLS)?SIPS_PORT:SIP_PORT; /* just in case we
  3011. don't find another */
  3012. h->proto=*proto; /* store initial protocol */
  3013. if (port){
  3014. if (*port==0){
  3015. /* try SRV if initial call & no port specified
  3016. * (draft-ietf-sip-srv-06) */
  3017. if ((name->len+SRV_MAX_PREFIX_LEN+1)>MAX_DNS_NAME){
  3018. LM_WARN("domain name too long (%d), unable to perform SRV lookup\n",
  3019. name->len);
  3020. }else{
  3021. /* check if it's an ip address */
  3022. if ( ((tmp_ip=str2ip(name))!=0)
  3023. || ((tmp_ip=str2ip6(name))!=0)
  3024. ){
  3025. /* we are lucky, this is an ip address */
  3026. if (((flags&DNS_IPV4_ONLY) && (tmp_ip->af==AF_INET6))||
  3027. ((flags&DNS_IPV6_ONLY) && (tmp_ip->af==AF_INET))){
  3028. return -E_DNS_AF_MISMATCH;
  3029. }
  3030. *ip=*tmp_ip;
  3031. *port=h->port;
  3032. /* proto already set */
  3033. return 0;
  3034. }
  3035. /* looping on the ordered list until we found a protocol what has srv record */
  3036. list_len = create_srv_pref_list(&origproto, srv_proto_list);
  3037. for (i=0; i<list_len;i++) {
  3038. switch (srv_proto_list[i].proto) {
  3039. case PROTO_UDP:
  3040. case PROTO_TCP:
  3041. case PROTO_TLS:
  3042. case PROTO_SCTP:
  3043. create_srv_name(srv_proto_list[i].proto, name, tmp);
  3044. break;
  3045. default:
  3046. LM_CRIT("unknown proto %d\n", (int)srv_proto_list[i].proto);
  3047. return -E_DNS_CRITICAL;
  3048. }
  3049. srv_name.s=tmp;
  3050. srv_name.len=strlen(tmp);
  3051. if ((ret=dns_srv_resolve_ip(h, &srv_name, ip, port, flags))>=0)
  3052. {
  3053. h->proto = *proto = srv_proto_list[i].proto;
  3054. #ifdef DNS_CACHE_DEBUG
  3055. DBG("dns_srv_sip_resolve(%.*s, %d, %d), srv0, ret=%d\n",
  3056. name->len, name->s, h->srv_no, h->ip_no, ret);
  3057. #endif
  3058. return ret;
  3059. }
  3060. }
  3061. }
  3062. }else{ /* if (*port==0) */
  3063. h->port=*port; /* store initial port */
  3064. /* proto already set */
  3065. }
  3066. } /* if (port) */
  3067. }else if (h->srv){
  3068. srv_name.s=h->srv->name;
  3069. srv_name.len=h->srv->name_len;
  3070. /* continue srv resolving */
  3071. ret=dns_srv_resolve_ip(h, &srv_name, ip, port, flags);
  3072. if (proto)
  3073. *proto=h->proto;
  3074. DBG("dns_srv_sip_resolve(%.*s, %d, %d), srv, ret=%d\n",
  3075. name->len, name->s, h->srv_no, h->ip_no, ret);
  3076. return ret;
  3077. }
  3078. if (name->len >= MAX_DNS_NAME) {
  3079. LM_ERR("domain name too long\n");
  3080. return -E_DNS_NAME_TOO_LONG;
  3081. }
  3082. ret=dns_ip_resolve(&h->a, &h->ip_no, name, ip, flags);
  3083. if (port)
  3084. *port=h->port;
  3085. if (proto)
  3086. *proto=h->proto;
  3087. #ifdef DNS_CACHE_DEBUG
  3088. DBG("dns_srv_sip_resolve(%.*s, %d, %d), ip, ret=%d\n",
  3089. name->len, name->s, h->srv_no, h->ip_no, ret);
  3090. #endif
  3091. return ret;
  3092. }
  3093. #ifdef USE_NAPTR
  3094. /* resolves a host name trying:
  3095. * - NAPTR lookup if the address is not an ip and proto!=0, port!=0
  3096. * *port==0 and *proto=0 and if flags allow NAPTR lookups
  3097. * -SRV lookup if port!=0 and *port==0
  3098. * - normal A/AAAA lookup if *port!=0, or port==0
  3099. * when performing SRV lookup (*port==0) it will use proto to look for
  3100. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  3101. * h must be initialized prior to calling this function and can be used to
  3102. * get the subsequent ips
  3103. * returns: <0 on error
  3104. * 0 on success and it fills *ip, *port, dns_sip_resolve_h
  3105. * WARNING: when finished, dns_sip_resolve_put(h) must be called!
  3106. */
  3107. inline static int dns_naptr_sip_resolve(struct dns_srv_handle* h, str* name,
  3108. struct ip_addr* ip, unsigned short* port, char* proto,
  3109. int flags)
  3110. {
  3111. struct hostent* he;
  3112. struct ip_addr* tmp_ip;
  3113. naptr_bmp_t tried_bmp;
  3114. struct dns_hash_entry* e;
  3115. char n_proto, origproto;
  3116. str srv_name;
  3117. int ret;
  3118. ret=-E_DNS_NO_NAPTR;
  3119. origproto=*proto;
  3120. if (dns_hash==0){ /* not init => use normal, non-cached version */
  3121. LM_WARN("called before dns cache initialization\n");
  3122. h->srv=h->a=0;
  3123. he=_sip_resolvehost(name, port, proto);
  3124. if (he){
  3125. hostent2ip_addr(ip, he, 0);
  3126. return 0;
  3127. }
  3128. return -E_DNS_NO_NAPTR;
  3129. }
  3130. if (((h->srv==0) && (h->a==0)) && /* first call */
  3131. proto && port && (*proto==0) && (*port==0)){
  3132. *proto=PROTO_UDP; /* just in case we don't find another */
  3133. /* check if it's an ip address */
  3134. if ( ((tmp_ip=str2ip(name))!=0)
  3135. || ((tmp_ip=str2ip6(name))!=0)
  3136. ){
  3137. /* we are lucky, this is an ip address */
  3138. if (((flags&DNS_IPV4_ONLY) && (tmp_ip->af==AF_INET6))||
  3139. ((flags&DNS_IPV6_ONLY) && (tmp_ip->af==AF_INET))){
  3140. return -E_DNS_AF_MISMATCH;
  3141. }
  3142. *ip=*tmp_ip;
  3143. h->port=SIP_PORT;
  3144. h->proto=*proto;
  3145. *port=h->port;
  3146. return 0;
  3147. }
  3148. /* do naptr lookup */
  3149. if ((e=dns_get_entry(name, T_NAPTR))==0)
  3150. goto naptr_not_found;
  3151. naptr_iterate_init(&tried_bmp);
  3152. while(dns_naptr_sip_iterate(e->rr_lst, &tried_bmp,
  3153. &srv_name, &n_proto)){
  3154. dns_srv_handle_init(h); /* make sure h does not contain garbage
  3155. from previous dns_srv_sip_resolve calls */
  3156. if ((ret=dns_srv_resolve_ip(h, &srv_name, ip, port, flags))>=0){
  3157. #ifdef DNS_CACHE_DEBUG
  3158. DBG("dns_naptr_sip_resolve(%.*s, %d, %d), srv0, ret=%d\n",
  3159. name->len, name->s, h->srv_no, h->ip_no, ret);
  3160. #endif
  3161. dns_hash_put(e);
  3162. *proto=n_proto;
  3163. h->proto=*proto;
  3164. return ret;
  3165. }
  3166. }
  3167. /* no acceptable naptr record found, fallback to srv */
  3168. dns_hash_put(e);
  3169. dns_srv_handle_init(h); /* make sure h does not contain garbage
  3170. from previous dns_srv_sip_resolve calls */
  3171. }
  3172. naptr_not_found:
  3173. *proto=origproto;
  3174. return dns_srv_sip_resolve(h, name, ip, port, proto, flags);
  3175. }
  3176. #endif /* USE_NAPTR */
  3177. /* resolves a host name trying:
  3178. * - NAPTR lookup if the address is not an ip and proto!=0, port!=0
  3179. * *port==0 and *proto=0 and if flags allow NAPTR lookups
  3180. * -SRV lookup if port!=0 and *port==0
  3181. * - normal A/AAAA lookup if *port!=0, or port==0
  3182. * when performing SRV lookup (*port==0) it will use proto to look for
  3183. * tcp or udp hosts, otherwise proto is unused; if proto==0 => no SRV lookup
  3184. * h must be initialized prior to calling this function and can be used to
  3185. * get the subsequent ips
  3186. * returns: <0 on error
  3187. * 0 on success and it fills *ip, *port, dns_sip_resolve_h
  3188. */
  3189. int dns_sip_resolve(struct dns_srv_handle* h, str* name,
  3190. struct ip_addr* ip, unsigned short* port, char* proto,
  3191. int flags)
  3192. {
  3193. #ifdef USE_NAPTR
  3194. if (flags&DNS_TRY_NAPTR)
  3195. return dns_naptr_sip_resolve(h, name, ip, port, proto, flags);
  3196. #endif
  3197. return dns_srv_sip_resolve(h, name, ip, port, proto, flags);
  3198. }
  3199. /* performs an a lookup and fills ip with the first good ip address
  3200. * returns 0 on success, <0 on error (see the error codes)
  3201. */
  3202. inline static int dns_a_get_ip(str* name, struct ip_addr* ip)
  3203. {
  3204. struct dns_hash_entry* e;
  3205. int ret;
  3206. unsigned char rr_no;
  3207. e=0;
  3208. rr_no=0;
  3209. ret=dns_a_resolve(&e, &rr_no, name, ip);
  3210. if (e) dns_hash_put(e);
  3211. return ret;
  3212. }
  3213. inline static int dns_aaaa_get_ip(str* name, struct ip_addr* ip)
  3214. {
  3215. struct dns_hash_entry* e;
  3216. int ret;
  3217. unsigned char rr_no;
  3218. e=0;
  3219. rr_no=0;
  3220. ret=dns_aaaa_resolve(&e, &rr_no, name, ip);
  3221. if (e) dns_hash_put(e);
  3222. return ret;
  3223. }
  3224. /* performs an a or aaaa dns lookup, returns <0 on error (see the
  3225. * dns error codes) and 0 on success
  3226. * flags: - none set: tries first an a_lookup and if it fails an aaaa_lookup
  3227. * - DNS_IPV6_FIRST: tries first an aaaa_lookup and then an a_lookup
  3228. * - DNS_IPV4_ONLY: tries only an a_lookup
  3229. * - DNS_IPV6_ONLY: tries only an aaaa_lookup
  3230. */
  3231. int dns_get_ip(str* name, struct ip_addr* ip, int flags)
  3232. {
  3233. int ret;
  3234. struct dns_hash_entry* e;
  3235. unsigned char rr_no;
  3236. e=0;
  3237. rr_no=0;
  3238. ret=dns_ip_resolve(&e, &rr_no, name, ip, flags);
  3239. if (e)
  3240. dns_hash_put(e);
  3241. return ret;
  3242. }
  3243. /* fast "inline" version, gets the first good ip:port */
  3244. int dns_srv_get_ip(str* name, struct ip_addr* ip, unsigned short* port,
  3245. int flags)
  3246. {
  3247. int ret;
  3248. struct dns_srv_handle h;
  3249. dns_srv_handle_init(&h);
  3250. ret=dns_srv_resolve_ip(&h, name, ip, port, flags);
  3251. dns_srv_handle_put(&h);
  3252. return ret;
  3253. }
  3254. #ifdef DNS_WATCHDOG_SUPPORT
  3255. /* sets the state of the DNS servers:
  3256. * 1: at least one server is up
  3257. * 0: all the servers are down
  3258. */
  3259. void dns_set_server_state(int state)
  3260. {
  3261. atomic_set(dns_servers_up, state);
  3262. }
  3263. /* returns the state of the DNS servers */
  3264. int dns_get_server_state(void)
  3265. {
  3266. return atomic_get(dns_servers_up);
  3267. }
  3268. #endif /* DNS_WATCHDOG_SUPPORT */
  3269. /* rpc functions */
  3270. void dns_cache_mem_info(rpc_t* rpc, void* ctx)
  3271. {
  3272. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3273. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3274. return;
  3275. }
  3276. rpc->add(ctx, "dd", *dns_cache_mem_used, cfg_get(core, core_cfg, dns_cache_max_mem));
  3277. }
  3278. void dns_cache_debug(rpc_t* rpc, void* ctx)
  3279. {
  3280. int h;
  3281. struct dns_hash_entry* e;
  3282. ticks_t now;
  3283. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3284. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3285. return;
  3286. }
  3287. now=get_ticks_raw();
  3288. LOCK_DNS_HASH();
  3289. for (h=0; h<DNS_HASH_SIZE; h++){
  3290. clist_foreach(&dns_hash[h], e, next){
  3291. rpc->add(ctx, "sdddddd",
  3292. e->name, e->type, e->total_size, e->refcnt.val,
  3293. (s_ticks_t)(e->expire-now)<0?-1:
  3294. TICKS_TO_S(e->expire-now),
  3295. TICKS_TO_S(now-e->last_used),
  3296. e->ent_flags);
  3297. }
  3298. }
  3299. UNLOCK_DNS_HASH();
  3300. }
  3301. #ifdef USE_DNS_CACHE_STATS
  3302. static unsigned long stat_sum(int ivar, int breset)
  3303. {
  3304. unsigned long isum=0;
  3305. int i1=0;
  3306. for (; i1 < get_max_procs(); i1++)
  3307. switch (ivar) {
  3308. case 0:
  3309. isum+=dns_cache_stats[i1].dns_req_cnt;
  3310. if (breset)
  3311. dns_cache_stats[i1].dns_req_cnt=0;
  3312. break;
  3313. case 1:
  3314. isum+=dns_cache_stats[i1].dc_hits_cnt;
  3315. if (breset)
  3316. dns_cache_stats[i1].dc_hits_cnt=0;
  3317. break;
  3318. case 2:
  3319. isum+=dns_cache_stats[i1].dc_neg_hits_cnt;
  3320. if (breset)
  3321. dns_cache_stats[i1].dc_neg_hits_cnt=0;
  3322. break;
  3323. case 3:
  3324. isum+=dns_cache_stats[i1].dc_lru_cnt;
  3325. if (breset)
  3326. dns_cache_stats[i1].dc_lru_cnt=0;
  3327. break;
  3328. }
  3329. return isum;
  3330. }
  3331. void dns_cache_stats_get(rpc_t* rpc, void* c)
  3332. {
  3333. char *name=NULL;
  3334. void *handle;
  3335. int found=0,i=0;
  3336. int reset=0;
  3337. char* dns_cache_stats_names[] = {
  3338. "dns_req_cnt",
  3339. "dc_hits_cnt",
  3340. "dc_neg_hits_cnt",
  3341. "dc_lru_cnt",
  3342. NULL
  3343. };
  3344. if (!cfg_get(core, core_cfg, use_dns_cache)) {
  3345. rpc->fault(c, 500, "dns cache support disabled");
  3346. return;
  3347. }
  3348. if (rpc->scan(c, "s", &name) < 0)
  3349. return;
  3350. if (rpc->scan(c, "d", &reset) < 0)
  3351. return;
  3352. if (!strcasecmp(name, DNS_CACHE_ALL_STATS)) {
  3353. /* dump all the dns cache stat values */
  3354. rpc->add(c, "{", &handle);
  3355. for (i=0; dns_cache_stats_names[i]; i++)
  3356. rpc->struct_add(handle, "d",
  3357. dns_cache_stats_names[i],
  3358. stat_sum(i, reset));
  3359. found=1;
  3360. } else {
  3361. for (i=0; dns_cache_stats_names[i]; i++)
  3362. if (!strcasecmp(dns_cache_stats_names[i], name)) {
  3363. rpc->add(c, "{", &handle);
  3364. rpc->struct_add(handle, "d",
  3365. dns_cache_stats_names[i],
  3366. stat_sum(i, reset));
  3367. found=1;
  3368. break;
  3369. }
  3370. }
  3371. if(!found)
  3372. rpc->fault(c, 500, "unknown dns cache stat parameter");
  3373. return;
  3374. }
  3375. #endif /* USE_DNS_CACHE_STATS */
  3376. /* rpc functions */
  3377. void dns_cache_debug_all(rpc_t* rpc, void* ctx)
  3378. {
  3379. int h;
  3380. struct dns_hash_entry* e;
  3381. struct dns_rr* rr;
  3382. struct ip_addr ip;
  3383. int i;
  3384. ticks_t now;
  3385. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3386. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3387. return;
  3388. }
  3389. now=get_ticks_raw();
  3390. LOCK_DNS_HASH();
  3391. for (h=0; h<DNS_HASH_SIZE; h++){
  3392. clist_foreach(&dns_hash[h], e, next){
  3393. for (i=0, rr=e->rr_lst; rr; i++, rr=rr->next){
  3394. rpc->add(ctx, "sddddddd",
  3395. e->name, (int)e->type, i, (int)e->total_size,
  3396. (int)e->refcnt.val,
  3397. (int)(s_ticks_t)(e->expire-now)<0?-1:
  3398. TICKS_TO_S(e->expire-now),
  3399. (int)TICKS_TO_S(now-e->last_used),
  3400. (int)e->ent_flags);
  3401. switch(e->type){
  3402. case T_A:
  3403. case T_AAAA:
  3404. if (dns_rr2ip(e->type, rr, &ip)==0){
  3405. rpc->add(ctx, "ss", "ip", ip_addr2a(&ip) );
  3406. }else{
  3407. rpc->add(ctx, "ss", "ip", "<error: bad rr>");
  3408. }
  3409. break;
  3410. case T_SRV:
  3411. rpc->add(ctx, "ss", "srv",
  3412. ((struct srv_rdata*)(rr->rdata))->name);
  3413. break;
  3414. case T_NAPTR:
  3415. rpc->add(ctx, "ss", "naptr ",
  3416. ((struct naptr_rdata*)(rr->rdata))->flags);
  3417. break;
  3418. case T_CNAME:
  3419. rpc->add(ctx, "ss", "cname",
  3420. ((struct cname_rdata*)(rr->rdata))->name);
  3421. break;
  3422. case T_TXT:
  3423. rpc->add(ctx, "ss", "txt",
  3424. ((struct txt_rdata*)(rr->rdata))->cstr_no?
  3425. ((struct txt_rdata*)(rr->rdata))->txt[0].cstr:
  3426. "");
  3427. break;
  3428. case T_EBL:
  3429. rpc->add(ctx, "ss", "ebl",
  3430. ((struct ebl_rdata*)(rr->rdata))->apex);
  3431. break;
  3432. case T_PTR:
  3433. rpc->add(ctx, "ss", "ptr",
  3434. ((struct ptr_rdata*)(rr->rdata))->ptrdname);
  3435. break;
  3436. default:
  3437. rpc->add(ctx, "ss", "unknown", "?");
  3438. }
  3439. rpc->add(ctx, "d",
  3440. (int)(s_ticks_t)(rr->expire-now)<0?-1:
  3441. TICKS_TO_S(rr->expire-now));
  3442. }
  3443. }
  3444. }
  3445. UNLOCK_DNS_HASH();
  3446. }
  3447. static char *print_type(unsigned short type)
  3448. {
  3449. switch (type) {
  3450. case T_A:
  3451. return "A";
  3452. case T_AAAA:
  3453. return "AAAA";
  3454. case T_SRV:
  3455. return "SRV";
  3456. case T_NAPTR:
  3457. return "NAPTR";
  3458. case T_CNAME:
  3459. return "CNAME";
  3460. case T_TXT:
  3461. return "TXT";
  3462. case T_EBL:
  3463. return "EBL";
  3464. case T_PTR:
  3465. return "PTR";
  3466. default:
  3467. return "unknown";
  3468. }
  3469. }
  3470. /** convert string type to dns integer T_*.
  3471. * used for rpc type translation.
  3472. * @return T_* on success, -1 on error.
  3473. */
  3474. static int dns_get_type(str* s)
  3475. {
  3476. char *t;
  3477. int len;
  3478. t=s->s;
  3479. len=s->len;
  3480. /* skip over a T_ or t_ prefix */
  3481. if ((len>2) && (t[0]=='T' || t[0]=='t') && (t[1]=='_')){
  3482. t+=2;
  3483. len-=2;
  3484. }
  3485. switch(len){
  3486. case 1:
  3487. if (t[0]=='A' || t[0]=='a')
  3488. return T_A;
  3489. break;
  3490. case 4:
  3491. if (strncasecmp(t, "AAAA", len)==0)
  3492. return T_AAAA;
  3493. break;
  3494. case 3:
  3495. if (strncasecmp(t, "SRV", len)==0)
  3496. return T_SRV;
  3497. else if (strncasecmp(t, "TXT", len)==0)
  3498. return T_TXT;
  3499. else if (strncasecmp(t, "EBL", len)==0)
  3500. return T_EBL;
  3501. else if (strncasecmp(t, "PTR", len)==0)
  3502. return T_PTR;
  3503. break;
  3504. case 5:
  3505. if (strncasecmp(t, "NAPTR", len)==0)
  3506. return T_NAPTR;
  3507. else if (strncasecmp(t, "CNAME", len)==0)
  3508. return T_CNAME;
  3509. break;
  3510. }
  3511. return -1;
  3512. }
  3513. /** rpc-prints a dns cache entry.
  3514. */
  3515. void dns_cache_print_entry(rpc_t* rpc, void* ctx, struct dns_hash_entry* e)
  3516. {
  3517. int expires;
  3518. struct dns_rr* rr;
  3519. struct ip_addr ip;
  3520. ticks_t now;
  3521. str s;
  3522. int i;
  3523. now=get_ticks_raw();
  3524. expires = (s_ticks_t)(e->expire-now)<0?-1: TICKS_TO_S(e->expire-now);
  3525. rpc->rpl_printf(ctx, "%sname: %s", SPACE_FORMAT, e->name);
  3526. rpc->rpl_printf(ctx, "%stype: %s", SPACE_FORMAT, print_type(e->type));
  3527. rpc->rpl_printf(ctx, "%ssize (bytes): %d", SPACE_FORMAT,
  3528. e->total_size);
  3529. rpc->rpl_printf(ctx, "%sreference counter: %d", SPACE_FORMAT,
  3530. e->refcnt.val);
  3531. if (e->ent_flags & DNS_FLAG_PERMANENT) {
  3532. rpc->rpl_printf(ctx, "%spermanent: yes", SPACE_FORMAT);
  3533. } else {
  3534. rpc->rpl_printf(ctx, "%spermanent: no", SPACE_FORMAT);
  3535. rpc->rpl_printf(ctx, "%sexpires in (s): %d", SPACE_FORMAT, expires);
  3536. }
  3537. rpc->rpl_printf(ctx, "%slast used (s): %d", SPACE_FORMAT,
  3538. TICKS_TO_S(now-e->last_used));
  3539. rpc->rpl_printf(ctx, "%snegative entry: %s", SPACE_FORMAT,
  3540. (e->ent_flags & DNS_FLAG_BAD_NAME) ? "yes" : "no");
  3541. for (rr=e->rr_lst; rr; rr=rr->next) {
  3542. switch(e->type) {
  3543. case T_A:
  3544. case T_AAAA:
  3545. if (dns_rr2ip(e->type, rr, &ip)==0){
  3546. rpc->rpl_printf(ctx, "%srr ip: %s", SPACE_FORMAT,
  3547. ip_addr2a(&ip) );
  3548. }else{
  3549. rpc->rpl_printf(ctx, "%srr ip: <error: bad rr>",
  3550. SPACE_FORMAT);
  3551. }
  3552. break;
  3553. case T_SRV:
  3554. rpc->rpl_printf(ctx, "%srr name: %s", SPACE_FORMAT,
  3555. ((struct srv_rdata*)(rr->rdata))->name);
  3556. rpc->rpl_printf(ctx, "%srr port: %d", SPACE_FORMAT,
  3557. ((struct srv_rdata*)(rr->rdata))->port);
  3558. rpc->rpl_printf(ctx, "%srr priority: %d", SPACE_FORMAT,
  3559. ((struct srv_rdata*)(rr->rdata))->priority);
  3560. rpc->rpl_printf(ctx, "%srr weight: %d", SPACE_FORMAT,
  3561. ((struct srv_rdata*)(rr->rdata))->weight);
  3562. break;
  3563. case T_NAPTR:
  3564. rpc->rpl_printf(ctx, "%srr order: %d", SPACE_FORMAT,
  3565. ((struct naptr_rdata*)(rr->rdata))->order);
  3566. rpc->rpl_printf(ctx, "%srr preference: %d", SPACE_FORMAT,
  3567. ((struct naptr_rdata*)(rr->rdata))->pref);
  3568. s.s = ((struct naptr_rdata*)(rr->rdata))->flags;
  3569. s.len = ((struct naptr_rdata*)(rr->rdata))->flags_len;
  3570. rpc->rpl_printf(ctx, "%srr flags: %.*s", SPACE_FORMAT,
  3571. s.len, s.s);
  3572. s.s=((struct naptr_rdata*)(rr->rdata))->services;
  3573. s.len=((struct naptr_rdata*)(rr->rdata))->services_len;
  3574. rpc->rpl_printf(ctx, "%srr service: %.*s", SPACE_FORMAT,
  3575. s.len, s.s);
  3576. s.s = ((struct naptr_rdata*)(rr->rdata))->regexp;
  3577. s.len = ((struct naptr_rdata*)(rr->rdata))->regexp_len;
  3578. rpc->rpl_printf(ctx, "%srr regexp: %.*s", SPACE_FORMAT,
  3579. s.len, s.s);
  3580. s.s = ((struct naptr_rdata*)(rr->rdata))->repl;
  3581. s.len = ((struct naptr_rdata*)(rr->rdata))->repl_len;
  3582. rpc->rpl_printf(ctx, "%srr replacement: %.*s",
  3583. SPACE_FORMAT, s.len, s.s);
  3584. break;
  3585. case T_CNAME:
  3586. rpc->rpl_printf(ctx, "%srr name: %s", SPACE_FORMAT,
  3587. ((struct cname_rdata*)(rr->rdata))->name);
  3588. break;
  3589. case T_TXT:
  3590. for (i=0; i<((struct txt_rdata*)(rr->rdata))->cstr_no;
  3591. i++){
  3592. rpc->rpl_printf(ctx, "%stxt[%d]: %s", SPACE_FORMAT, i,
  3593. ((struct txt_rdata*)(rr->rdata))->txt[i].cstr);
  3594. }
  3595. break;
  3596. case T_EBL:
  3597. rpc->rpl_printf(ctx, "%srr position: %d", SPACE_FORMAT,
  3598. ((struct ebl_rdata*)(rr->rdata))->position);
  3599. rpc->rpl_printf(ctx, "%srr separator: %s", SPACE_FORMAT,
  3600. ((struct ebl_rdata*)(rr->rdata))->separator);
  3601. rpc->rpl_printf(ctx, "%srr apex: %s", SPACE_FORMAT,
  3602. ((struct ebl_rdata*)(rr->rdata))->apex);
  3603. break;
  3604. case T_PTR:
  3605. rpc->rpl_printf(ctx, "%srr name: %s", SPACE_FORMAT,
  3606. ((struct ptr_rdata*)(rr->rdata))->ptrdname);
  3607. break;
  3608. default:
  3609. rpc->rpl_printf(ctx, "%sresource record: unknown",
  3610. SPACE_FORMAT);
  3611. }
  3612. if ((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  3613. rpc->rpl_printf(ctx, "%srr expires in (s): %d", SPACE_FORMAT,
  3614. (s_ticks_t)(rr->expire-now)<0?-1 :
  3615. TICKS_TO_S(rr->expire-now));
  3616. }
  3617. }
  3618. /* dumps the content of the cache in a human-readable format */
  3619. void dns_cache_view(rpc_t* rpc, void* ctx)
  3620. {
  3621. int h;
  3622. struct dns_hash_entry* e;
  3623. ticks_t now;
  3624. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3625. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3626. return;
  3627. }
  3628. now=get_ticks_raw();
  3629. LOCK_DNS_HASH();
  3630. for (h=0; h<DNS_HASH_SIZE; h++){
  3631. clist_foreach(&dns_hash[h], e, next){
  3632. if (((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  3633. && TICKS_LT(e->expire, now)
  3634. ) {
  3635. continue;
  3636. }
  3637. rpc->rpl_printf(ctx, "{\n");
  3638. dns_cache_print_entry(rpc, ctx, e);
  3639. rpc->rpl_printf(ctx, "}");
  3640. }
  3641. }
  3642. UNLOCK_DNS_HASH();
  3643. }
  3644. /* Delete all the entries from the cache.
  3645. * If del_permanent is 0, then only the
  3646. * non-permanent entries are deleted.
  3647. */
  3648. void dns_cache_flush(int del_permanent)
  3649. {
  3650. int h;
  3651. struct dns_hash_entry* e;
  3652. struct dns_hash_entry* tmp;
  3653. DBG("dns_cache_flush(): removing elements from the cache\n");
  3654. LOCK_DNS_HASH();
  3655. for (h=0; h<DNS_HASH_SIZE; h++){
  3656. clist_foreach_safe(&dns_hash[h], e, tmp, next){
  3657. if (del_permanent || ((e->ent_flags & DNS_FLAG_PERMANENT) == 0))
  3658. _dns_hash_remove(e);
  3659. }
  3660. }
  3661. UNLOCK_DNS_HASH();
  3662. }
  3663. /* deletes all the non-permanent entries from the cache */
  3664. void dns_cache_delete_all(rpc_t* rpc, void* ctx)
  3665. {
  3666. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3667. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3668. return;
  3669. }
  3670. dns_cache_flush(0);
  3671. rpc->rpl_printf(ctx, "OK");
  3672. }
  3673. /* deletes all the entries from the cache,
  3674. * even the permanent ones */
  3675. void dns_cache_delete_all_force(rpc_t* rpc, void* ctx)
  3676. {
  3677. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3678. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  3679. return;
  3680. }
  3681. dns_cache_flush(1);
  3682. rpc->rpl_printf(ctx, "OK");
  3683. }
  3684. /* clones an entry and extends its memory area to hold a new rr.
  3685. * if rdata_size>0 the new dns_rr struct is initialized, but the rdata is
  3686. * only filled with 0.
  3687. */
  3688. static struct dns_hash_entry *dns_cache_clone_entry(struct dns_hash_entry *e,
  3689. int rdata_size,
  3690. int ttl,
  3691. struct dns_rr **_new_rr)
  3692. {
  3693. struct dns_hash_entry *new;
  3694. struct dns_rr *rr, *last_rr, *new_rr;
  3695. int size, rounded_size, rr_size;
  3696. ticks_t now;
  3697. int i;
  3698. now=get_ticks_raw();
  3699. size = e->total_size;
  3700. if (rdata_size) {
  3701. /* we have to extend the entry */
  3702. rounded_size = ROUND_POINTER(size); /* size may not have been
  3703. rounded previously */
  3704. switch (e->type) {
  3705. case T_A:
  3706. case T_AAAA:
  3707. case T_CNAME:
  3708. rr_size = sizeof(struct dns_rr);
  3709. break;
  3710. case T_SRV:
  3711. rr_size = ROUND_SHORT(sizeof(struct dns_rr));
  3712. break;
  3713. case T_NAPTR:
  3714. rr_size = ROUND_POINTER(sizeof(struct dns_rr));
  3715. break;
  3716. case T_TXT:
  3717. rr_size = ROUND_POINTER(sizeof(struct dns_rr));
  3718. break;
  3719. case T_EBL:
  3720. rr_size = ROUND_POINTER(sizeof(struct dns_rr));
  3721. break;
  3722. case T_PTR:
  3723. rr_size = sizeof(struct dns_rr);
  3724. break;
  3725. default:
  3726. LM_ERR("type %d not supported\n", e->type);
  3727. return NULL;
  3728. }
  3729. } else {
  3730. rounded_size = size; /* no need to round the size, we just clone
  3731. the entry without extending it */
  3732. rr_size = 0;
  3733. }
  3734. new=shm_malloc(rounded_size+rr_size+rdata_size);
  3735. if (!new) {
  3736. LM_ERR("out of memory\n");
  3737. return NULL;
  3738. }
  3739. memset(new, 0, rounded_size+rr_size+rdata_size);
  3740. /* clone the entry */
  3741. memcpy(new, e, size);
  3742. /* fix the values and pointers */
  3743. new->next = new->prev = NULL;
  3744. #ifdef DNS_LU_LST
  3745. new->last_used_lst.next = new->last_used_lst.prev = NULL;
  3746. #endif
  3747. new->rr_lst = (struct dns_rr*)translate_pointer((char*)new, (char*)e,
  3748. (char*)new->rr_lst);
  3749. atomic_set(&new->refcnt, 0);
  3750. new->last_used = now;
  3751. /* expire and total_size are fixed later if needed */
  3752. /* fix the pointers inside the rr structures */
  3753. last_rr = NULL;
  3754. for (rr=new->rr_lst; rr; rr=rr->next) {
  3755. rr->rdata = (void*)translate_pointer((char*)new, (char*)e,
  3756. (char*)rr->rdata);
  3757. if (rr->next)
  3758. rr->next = (struct dns_rr*)translate_pointer((char*)new, (char*)e,
  3759. (char*)rr->next);
  3760. else
  3761. last_rr = rr;
  3762. switch(e->type){
  3763. case T_NAPTR:
  3764. /* there are pointers inside the NAPTR rdata stucture */
  3765. ((struct naptr_rdata*)rr->rdata)->flags =
  3766. translate_pointer((char*)new, (char*)e,
  3767. ((struct naptr_rdata*)rr->rdata)->flags);
  3768. ((struct naptr_rdata*)rr->rdata)->services =
  3769. translate_pointer((char*)new, (char*)e,
  3770. ((struct naptr_rdata*)rr->rdata)->services);
  3771. ((struct naptr_rdata*)rr->rdata)->regexp =
  3772. translate_pointer((char*)new, (char*)e,
  3773. ((struct naptr_rdata*)rr->rdata)->regexp);
  3774. ((struct naptr_rdata*)rr->rdata)->repl =
  3775. translate_pointer((char*)new, (char*)e,
  3776. ((struct naptr_rdata*)rr->rdata)->repl);
  3777. break;
  3778. case T_TXT:
  3779. /* there are pointers inside the TXT structure */
  3780. for (i=0; i<((struct txt_rdata*)rr->rdata)->cstr_no; i++){
  3781. ((struct txt_rdata*)rr->rdata)->txt[i].cstr=
  3782. translate_pointer((char*) new, (char*) e,
  3783. ((struct txt_rdata*)rr->rdata)->txt[i].cstr);
  3784. }
  3785. break;
  3786. case T_EBL:
  3787. /* there are pointers inside the EBL structure */
  3788. ((struct ebl_rdata*)rr->rdata)->separator =
  3789. translate_pointer((char*)new, (char*)e,
  3790. ((struct ebl_rdata*)rr->rdata)->separator);
  3791. ((struct ebl_rdata*)rr->rdata)->apex =
  3792. translate_pointer((char*)new, (char*)e,
  3793. ((struct ebl_rdata*)rr->rdata)->apex);
  3794. break;
  3795. }
  3796. }
  3797. if (rdata_size) {
  3798. /* set the pointer to the new rr structure */
  3799. new_rr = (void*)((char*)new + rounded_size);
  3800. new_rr->rdata = (void*)((char*)new_rr+rr_size);
  3801. new_rr->expire = now + S_TO_TICKS(ttl);
  3802. /* link the rr to the previous one */
  3803. last_rr->next = new_rr;
  3804. /* fix the total_size and expires values */
  3805. new->total_size=rounded_size+rr_size+rdata_size;
  3806. new->expire = MAX(new->expire, new_rr->expire);
  3807. if (_new_rr)
  3808. *_new_rr = new_rr;
  3809. } else {
  3810. if (_new_rr)
  3811. *_new_rr = NULL;
  3812. }
  3813. return new;
  3814. }
  3815. /* Adds a new record to the cache.
  3816. * If there is an existing record with the same name and value
  3817. * (ip address in case of A/AAAA record, name in case of SRV record)
  3818. * only the remaining fields are updated.
  3819. *
  3820. * Note that permanent records cannot be overwritten unless
  3821. * the new record is also permanent. A permanent record
  3822. * completely replaces a non-permanent one.
  3823. *
  3824. * Currently only A, AAAA, and SRV records are supported.
  3825. */
  3826. int dns_cache_add_record(unsigned short type,
  3827. str *name,
  3828. int ttl,
  3829. str *value,
  3830. int priority,
  3831. int weight,
  3832. int port,
  3833. int flags)
  3834. {
  3835. struct dns_hash_entry *old=NULL, *new=NULL;
  3836. struct dns_rr *rr;
  3837. str rr_name;
  3838. struct ip_addr *ip_addr;
  3839. ticks_t expire;
  3840. int err, h;
  3841. int size;
  3842. struct dns_rr *new_rr, **rr_p, **rr_iter;
  3843. struct srv_rdata *srv_rd;
  3844. /* eliminate gcc warnings */
  3845. ip_addr = 0;
  3846. size = 0;
  3847. rr_name.s = NULL;
  3848. rr_name.len = 0;
  3849. if (!cfg_get(core, core_cfg, use_dns_cache)){
  3850. LM_ERR("dns cache support disabled (see use_dns_cache)\n");
  3851. return -1;
  3852. }
  3853. if ((type != T_A) && (type != T_AAAA) && (type != T_SRV)) {
  3854. LM_ERR("rr type %d is not implemented\n", type);
  3855. return -1;
  3856. }
  3857. if ((flags & DNS_FLAG_BAD_NAME) == 0) {
  3858. /* fix-up the values */
  3859. switch(type) {
  3860. case T_A:
  3861. ip_addr = str2ip(value);
  3862. if (!ip_addr) {
  3863. LM_ERR("Malformed ip address: %.*s\n",
  3864. value->len, value->s);
  3865. return -1;
  3866. }
  3867. break;
  3868. case T_AAAA:
  3869. ip_addr = str2ip6(value);
  3870. if (!ip_addr) {
  3871. LM_ERR("Malformed ip address: %.*s\n",
  3872. value->len, value->s);
  3873. return -1;
  3874. }
  3875. break;
  3876. case T_SRV:
  3877. rr_name = *value;
  3878. break;
  3879. }
  3880. }
  3881. /* check whether there is a matching entry in the cache */
  3882. old = dns_hash_get(name, type, &h, &err);
  3883. if (old && old->type!=type) {
  3884. /* probably we found a CNAME instead of the specified type,
  3885. it is not needed */
  3886. dns_hash_put(old);
  3887. old=NULL;
  3888. }
  3889. if (old
  3890. && (old->ent_flags & DNS_FLAG_PERMANENT)
  3891. && ((flags & DNS_FLAG_PERMANENT) == 0)
  3892. ) {
  3893. LM_ERR("A non-permanent record cannot overwrite "
  3894. "a permanent entry\n");
  3895. goto error;
  3896. }
  3897. /* prepare the entry */
  3898. if (flags & DNS_FLAG_BAD_NAME) {
  3899. /* negative entry */
  3900. new = dns_cache_mk_bad_entry(name, type, ttl, flags);
  3901. if (!new) {
  3902. LM_ERR("Failed to create a negative "
  3903. "DNS cache entry\n");
  3904. goto error;
  3905. }
  3906. } else {
  3907. if (!old
  3908. || (old->ent_flags & DNS_FLAG_BAD_NAME)
  3909. || (((old->ent_flags & DNS_FLAG_PERMANENT) == 0)
  3910. && (flags & DNS_FLAG_PERMANENT))
  3911. ) {
  3912. /* There was no matching entry in the hash table,
  3913. * the entry is a negative record with inefficient space,
  3914. * or a permanent entry overwrites a non-permanent one.
  3915. * Let us create a new one.
  3916. */
  3917. switch(type) {
  3918. case T_A:
  3919. case T_AAAA:
  3920. new = dns_cache_mk_ip_entry(name, ip_addr);
  3921. if (!new) {
  3922. LM_ERR("Failed to create an A/AAAA record\n");
  3923. goto error;
  3924. }
  3925. /* fix the expiration time, dns_cache_mk_ip_entry() sets it
  3926. * to now-1 */
  3927. expire = get_ticks_raw() + S_TO_TICKS(ttl);
  3928. new->expire = expire;
  3929. new->rr_lst->expire = expire;
  3930. break;
  3931. case T_SRV:
  3932. new = dns_cache_mk_srv_entry(name, priority, weight, port,
  3933. &rr_name, ttl);
  3934. if (!new) {
  3935. LM_ERR("Failed to create an SRV record\n");
  3936. goto error;
  3937. }
  3938. }
  3939. new->ent_flags = flags;
  3940. } else {
  3941. /* we must modify the entry, so better to clone it, modify the new
  3942. * one, and replace the old with the new entry in the hash table,
  3943. * because the entry might be in use (even if the dns hash is
  3944. * locked). The old entry will be removed from the hash and
  3945. * automatically destroyed when its refcnt will be 0*/
  3946. /* check whether there is an rr with the same value */
  3947. for (rr=old->rr_lst; rr; rr=rr->next)
  3948. if ((((type == T_A) || (type == T_AAAA)) &&
  3949. (memcmp(ip_addr->u.addr, ((struct a_rdata*)rr->rdata)->ip,
  3950. ip_addr->len)==0))
  3951. || ((type == T_SRV) &&
  3952. (((struct srv_rdata*)rr->rdata)->name_len == rr_name.len)&&
  3953. (memcmp(rr_name.s, ((struct srv_rdata*)rr->rdata)->name,
  3954. rr_name.len)==0)))
  3955. break;
  3956. if (rr) {
  3957. /* the rr was found in the list */
  3958. new = dns_cache_clone_entry(old, 0, 0, 0);
  3959. if (!new) {
  3960. LM_ERR("Failed to clone an existing "
  3961. "DNS cache entry\n");
  3962. goto error;
  3963. }
  3964. /* let the rr point to the new structure */
  3965. rr = (struct dns_rr*)translate_pointer((char*)new, (char*)old,
  3966. (char*)rr);
  3967. new_rr = rr;
  3968. if (type == T_SRV) {
  3969. /* fix the priority, weight, and port */
  3970. ((struct srv_rdata*)rr->rdata)->priority = priority;
  3971. ((struct srv_rdata*)rr->rdata)->weight = weight;
  3972. ((struct srv_rdata*)rr->rdata)->port = port;
  3973. }
  3974. /* fix the expire value */
  3975. rr->expire = get_ticks_raw() + S_TO_TICKS(ttl);
  3976. new->expire = 0;
  3977. for (rr=new->rr_lst; rr; rr=rr->next)
  3978. new->expire = MAX(new->expire, rr->expire);
  3979. } else {
  3980. /* there was no matching rr, extend the structure with a new
  3981. * one */
  3982. switch(type) {
  3983. case T_A:
  3984. size = sizeof(struct a_rdata);
  3985. break;
  3986. case T_AAAA:
  3987. size = sizeof(struct aaaa_rdata);
  3988. break;
  3989. case T_SRV:
  3990. size = sizeof(struct srv_rdata)-1 +
  3991. rr_name.len+1;
  3992. break;
  3993. }
  3994. new = dns_cache_clone_entry(old, size, ttl, &rr);
  3995. if (!new) {
  3996. LM_ERR("Failed to clone an existing "
  3997. "DNS cache entry\n");
  3998. goto error;
  3999. }
  4000. new_rr = rr;
  4001. switch(type) {
  4002. case T_A:
  4003. case T_AAAA:
  4004. memcpy(rr->rdata, ip_addr->u.addr, ip_addr->len);
  4005. break;
  4006. case T_SRV:
  4007. ((struct srv_rdata*)rr->rdata)->priority = priority;
  4008. ((struct srv_rdata*)rr->rdata)->weight = weight;
  4009. ((struct srv_rdata*)rr->rdata)->port = port;
  4010. ((struct srv_rdata*)rr->rdata)->name_len = rr_name.len;
  4011. memcpy(((struct srv_rdata*)rr->rdata)->name, rr_name.s,
  4012. rr_name.len);
  4013. }
  4014. /* maximum expire value has been already fixed by
  4015. * dns_cache_clone_entry() */
  4016. }
  4017. if (type == T_SRV) {
  4018. /* SRV records must be ordered by their priority and weight.
  4019. * With modifying an exising rr, or adding new rr to the DNS entry,
  4020. * the ordered list might got broken which needs to be fixed.
  4021. */
  4022. rr_p = NULL;
  4023. for ( rr_iter = &new->rr_lst;
  4024. *rr_iter;
  4025. rr_iter = &((*rr_iter)->next)
  4026. ) {
  4027. if (*rr_iter == new_rr) {
  4028. rr_p = rr_iter;
  4029. continue;
  4030. }
  4031. srv_rd = (struct srv_rdata*)(*rr_iter)->rdata;
  4032. if ((priority < srv_rd->priority) ||
  4033. ((priority == srv_rd->priority) && (weight > srv_rd->weight))
  4034. )
  4035. break; /* insert here */
  4036. }
  4037. if (!rr_p)
  4038. for ( rr_p = rr_iter;
  4039. *rr_p && (*rr_p != new_rr);
  4040. rr_p = &((*rr_p)->next)
  4041. );
  4042. if (!rr_p) {
  4043. LM_ERR("Failed to correct the orderd list of SRV resource records\n");
  4044. goto error;
  4045. }
  4046. if (*rr_iter != new_rr->next) {
  4047. /* unlink rr from the list */
  4048. *rr_p = (*rr_p)->next;
  4049. /* link it before *rr_iter */
  4050. new_rr->next = *rr_iter;
  4051. *rr_iter = new_rr;
  4052. }
  4053. }
  4054. }
  4055. }
  4056. LOCK_DNS_HASH();
  4057. if (dns_cache_add_unsafe(new)) {
  4058. LM_ERR("Failed to add the entry to the cache\n");
  4059. UNLOCK_DNS_HASH();
  4060. goto error;
  4061. } else {
  4062. /* remove the old entry from the list */
  4063. if (old)
  4064. _dns_hash_remove(old);
  4065. }
  4066. UNLOCK_DNS_HASH();
  4067. if (old)
  4068. dns_hash_put(old);
  4069. return 0;
  4070. error:
  4071. /* leave the old entry in the list, and free the new one */
  4072. if (old)
  4073. dns_hash_put(old);
  4074. if (new)
  4075. dns_destroy_entry(new);
  4076. return -1;
  4077. }
  4078. /* deletes a record from the cache */
  4079. static void dns_cache_delete_record(rpc_t* rpc, void* ctx, unsigned short type)
  4080. {
  4081. struct dns_hash_entry *e;
  4082. str name;
  4083. int err, h, found=0, permanent=0;
  4084. if (!cfg_get(core, core_cfg, use_dns_cache)){
  4085. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  4086. return;
  4087. }
  4088. if (rpc->scan(ctx, "S", &name) < 1)
  4089. return;
  4090. LOCK_DNS_HASH();
  4091. e=_dns_hash_find(&name, type, &h, &err);
  4092. if (e && (e->type==type)) {
  4093. if ((e->ent_flags & DNS_FLAG_PERMANENT) == 0)
  4094. _dns_hash_remove(e);
  4095. else
  4096. permanent = 1;
  4097. found = 1;
  4098. }
  4099. UNLOCK_DNS_HASH();
  4100. if (permanent)
  4101. rpc->fault(ctx, 400, "Permanent entries cannot be deleted");
  4102. else if (!found)
  4103. rpc->fault(ctx, 400, "Not found");
  4104. }
  4105. /* Delete a single record from the cache,
  4106. * i.e. the record with the same name and value
  4107. * (ip address in case of A/AAAA record, name in case of SRV record).
  4108. *
  4109. * Currently only A, AAAA, and SRV records are supported.
  4110. */
  4111. int dns_cache_delete_single_record(unsigned short type,
  4112. str *name,
  4113. str *value,
  4114. int flags)
  4115. {
  4116. struct dns_hash_entry *old=NULL, *new=NULL;
  4117. struct dns_rr *rr, **next_p;
  4118. str rr_name;
  4119. struct ip_addr *ip_addr;
  4120. int err, h;
  4121. /* eliminate gcc warnings */
  4122. rr_name.s = NULL;
  4123. rr_name.len = 0;
  4124. ip_addr = 0;
  4125. if (!cfg_get(core, core_cfg, use_dns_cache)){
  4126. LM_ERR("dns cache support disabled (see use_dns_cache)\n");
  4127. return -1;
  4128. }
  4129. if ((type != T_A) && (type != T_AAAA) && (type != T_SRV)) {
  4130. LM_ERR("rr type %d is not implemented\n", type);
  4131. return -1;
  4132. }
  4133. if ((flags & DNS_FLAG_BAD_NAME) == 0) {
  4134. /* fix-up the values */
  4135. switch(type) {
  4136. case T_A:
  4137. ip_addr = str2ip(value);
  4138. if (!ip_addr) {
  4139. LM_ERR("Malformed ip address: %.*s\n",
  4140. value->len, value->s);
  4141. return -1;
  4142. }
  4143. break;
  4144. case T_AAAA:
  4145. ip_addr = str2ip6(value);
  4146. if (!ip_addr) {
  4147. LM_ERR("Malformed ip address: %.*s\n",
  4148. value->len, value->s);
  4149. return -1;
  4150. }
  4151. break;
  4152. case T_SRV:
  4153. rr_name = *value;
  4154. break;
  4155. }
  4156. }
  4157. /* check whether there is a matching entry in the cache */
  4158. if ((old = dns_hash_get(name, type, &h, &err)) == NULL)
  4159. goto not_found;
  4160. if ((old->type != type) /* may be CNAME */
  4161. || (old->ent_flags != flags)
  4162. )
  4163. goto not_found;
  4164. if (flags & DNS_FLAG_BAD_NAME) /* negative record, there is no value */
  4165. goto delete;
  4166. /* check whether there is an rr with the same value */
  4167. for (rr=old->rr_lst, next_p=&old->rr_lst;
  4168. rr;
  4169. next_p=&rr->next, rr=rr->next
  4170. )
  4171. if ((((type == T_A) || (type == T_AAAA)) &&
  4172. (memcmp(ip_addr->u.addr, ((struct a_rdata*)rr->rdata)->ip,
  4173. ip_addr->len)==0))
  4174. || ((type == T_SRV) &&
  4175. (((struct srv_rdata*)rr->rdata)->name_len == rr_name.len) &&
  4176. (memcmp(rr_name.s, ((struct srv_rdata*)rr->rdata)->name,
  4177. rr_name.len)==0)))
  4178. break;
  4179. if (!rr)
  4180. goto not_found;
  4181. if ((rr == old->rr_lst) && (rr->next == NULL)) {
  4182. /* There is a single rr value, hence the whole
  4183. * hash entry can be deleted */
  4184. goto delete;
  4185. } else {
  4186. /* we must modify the entry, so better to clone it, modify the new
  4187. * one, and replace the old with the new entry in the hash table,
  4188. * because the entry might be in use (even if the dns hash is
  4189. * locked). The old entry will be removed from the hash and
  4190. * automatically destroyed when its refcnt will be 0*/
  4191. new = dns_cache_clone_entry(old, 0, 0, 0);
  4192. if (!new) {
  4193. LM_ERR("Failed to clone an existing DNS cache entry\n");
  4194. dns_hash_put(old);
  4195. return -1;
  4196. }
  4197. /* let rr and next_p point to the new structure */
  4198. rr = (struct dns_rr*)translate_pointer((char*)new,
  4199. (char*)old,
  4200. (char*)rr);
  4201. next_p = (struct dns_rr**)translate_pointer((char*)new,
  4202. (char*)old,
  4203. (char*)next_p);
  4204. /* unlink rr from the list. The memory will be freed
  4205. * when the whole record is freed */
  4206. *next_p = rr->next;
  4207. }
  4208. delete:
  4209. LOCK_DNS_HASH();
  4210. if (new) {
  4211. /* delete the old entry only if the new one can be added */
  4212. if (dns_cache_add_unsafe(new)) {
  4213. LM_ERR("Failed to add the entry to the cache\n");
  4214. UNLOCK_DNS_HASH();
  4215. if (old)
  4216. dns_hash_put(old);
  4217. return -1;
  4218. } else {
  4219. /* remove the old entry from the list */
  4220. if (old)
  4221. _dns_hash_remove(old);
  4222. }
  4223. } else if (old) {
  4224. _dns_hash_remove(old);
  4225. }
  4226. UNLOCK_DNS_HASH();
  4227. if (old)
  4228. dns_hash_put(old);
  4229. return 0;
  4230. not_found:
  4231. LM_ERR("No matching record found\n");
  4232. if (old)
  4233. dns_hash_put(old);
  4234. return -1;
  4235. }
  4236. /* performs a dns lookup over rpc */
  4237. void dns_cache_rpc_lookup(rpc_t* rpc, void* ctx)
  4238. {
  4239. struct dns_hash_entry *e;
  4240. str name;
  4241. str type;
  4242. int t;
  4243. if (!cfg_get(core, core_cfg, use_dns_cache)){
  4244. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  4245. return;
  4246. }
  4247. if (rpc->scan(ctx, "SS", &type, &name) < 1)
  4248. return;
  4249. t=dns_get_type(&type);
  4250. if (t<0){
  4251. rpc->fault(ctx, 400, "Invalid type");
  4252. return;
  4253. }
  4254. e=dns_get_entry(&name, t);
  4255. if (e==0){
  4256. rpc->fault(ctx, 400, "Not found");
  4257. return;
  4258. }
  4259. dns_cache_print_entry(rpc, ctx, e);
  4260. dns_hash_put(e);
  4261. }
  4262. /* wrapper functions for adding and deleting records */
  4263. void dns_cache_add_a(rpc_t* rpc, void* ctx)
  4264. {
  4265. str name;
  4266. int ttl;
  4267. str ip;
  4268. int flags;
  4269. if (rpc->scan(ctx, "SdSd", &name, &ttl, &ip, &flags) < 4)
  4270. return;
  4271. if (dns_cache_add_record(T_A,
  4272. &name,
  4273. ttl,
  4274. &ip,
  4275. 0 /* priority */,
  4276. 0 /* weight */,
  4277. 0 /* port */,
  4278. flags)
  4279. )
  4280. rpc->fault(ctx, 400, "Failed to add the entry to the cache");
  4281. }
  4282. void dns_cache_add_aaaa(rpc_t* rpc, void* ctx)
  4283. {
  4284. str name;
  4285. int ttl;
  4286. str ip;
  4287. int flags;
  4288. if (rpc->scan(ctx, "SdSd", &name, &ttl, &ip, &flags) < 4)
  4289. return;
  4290. if (dns_cache_add_record(T_AAAA,
  4291. &name,
  4292. ttl,
  4293. &ip,
  4294. 0 /* priority */,
  4295. 0 /* weight */,
  4296. 0 /* port */,
  4297. flags)
  4298. )
  4299. rpc->fault(ctx, 400, "Failed to add the entry to the cache");
  4300. }
  4301. void dns_cache_add_srv(rpc_t* rpc, void* ctx)
  4302. {
  4303. str name;
  4304. int ttl, priority, weight, port;
  4305. str rr_name;
  4306. int flags;
  4307. if (rpc->scan(ctx, "SddddSd", &name, &ttl, &priority, &weight, &port,
  4308. &rr_name, &flags) < 7
  4309. )
  4310. return;
  4311. if (dns_cache_add_record(T_SRV,
  4312. &name,
  4313. ttl,
  4314. &rr_name,
  4315. priority,
  4316. weight,
  4317. port,
  4318. flags)
  4319. )
  4320. rpc->fault(ctx, 400, "Failed to add the entry to the cache");
  4321. }
  4322. void dns_cache_delete_a(rpc_t* rpc, void* ctx)
  4323. {
  4324. dns_cache_delete_record(rpc, ctx, T_A);
  4325. }
  4326. void dns_cache_delete_aaaa(rpc_t* rpc, void* ctx)
  4327. {
  4328. dns_cache_delete_record(rpc, ctx, T_AAAA);
  4329. }
  4330. void dns_cache_delete_srv(rpc_t* rpc, void* ctx)
  4331. {
  4332. dns_cache_delete_record(rpc, ctx, T_SRV);
  4333. }
  4334. void dns_cache_delete_naptr(rpc_t* rpc, void* ctx)
  4335. {
  4336. dns_cache_delete_record(rpc, ctx, T_NAPTR);
  4337. }
  4338. void dns_cache_delete_cname(rpc_t* rpc, void* ctx)
  4339. {
  4340. dns_cache_delete_record(rpc, ctx, T_CNAME);
  4341. }
  4342. void dns_cache_delete_txt(rpc_t* rpc, void* ctx)
  4343. {
  4344. dns_cache_delete_record(rpc, ctx, T_TXT);
  4345. }
  4346. void dns_cache_delete_ebl(rpc_t* rpc, void* ctx)
  4347. {
  4348. dns_cache_delete_record(rpc, ctx, T_EBL);
  4349. }
  4350. void dns_cache_delete_ptr(rpc_t* rpc, void* ctx)
  4351. {
  4352. dns_cache_delete_record(rpc, ctx, T_PTR);
  4353. }
  4354. #ifdef DNS_WATCHDOG_SUPPORT
  4355. /* sets the DNS server states */
  4356. void dns_set_server_state_rpc(rpc_t* rpc, void* ctx)
  4357. {
  4358. int state;
  4359. if (!cfg_get(core, core_cfg, use_dns_cache)){
  4360. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  4361. return;
  4362. }
  4363. if (rpc->scan(ctx, "d", &state) < 1)
  4364. return;
  4365. dns_set_server_state(state);
  4366. }
  4367. /* prints the DNS server state */
  4368. void dns_get_server_state_rpc(rpc_t* rpc, void* ctx)
  4369. {
  4370. if (!cfg_get(core, core_cfg, use_dns_cache)){
  4371. rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
  4372. return;
  4373. }
  4374. rpc->add(ctx, "d", dns_get_server_state());
  4375. }
  4376. #endif /* DNS_WATCHDOG_SUPPORT */
  4377. #endif