dns_cache.c 134 KB

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