resolv.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309
  1. /* vi: set sw=4 ts=4: */
  2. /* resolv.c: DNS Resolver
  3. *
  4. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
  5. * The Silver Hammer Group, Ltd.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. */
  12. /*
  13. * Portions Copyright (c) 1985, 1993
  14. * The Regents of the University of California. All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. * 4. Neither the name of the University nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. * SUCH DAMAGE.
  39. */
  40. /*
  41. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  42. *
  43. * Permission to use, copy, modify, and distribute this software for any
  44. * purpose with or without fee is hereby granted, provided that the above
  45. * copyright notice and this permission notice appear in all copies, and that
  46. * the name of Digital Equipment Corporation not be used in advertising or
  47. * publicity pertaining to distribution of the document or software without
  48. * specific, written prior permission.
  49. *
  50. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  51. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  52. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  53. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  54. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  55. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  56. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  57. * SOFTWARE.
  58. */
  59. /*
  60. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  61. *
  62. * Permission to use, copy, modify, and distribute this software for any
  63. * purpose with or without fee is hereby granted, provided that the above
  64. * copyright notice and this permission notice appear in all copies.
  65. *
  66. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  67. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  68. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  69. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  70. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  71. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  72. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  73. * SOFTWARE.
  74. */
  75. /*
  76. * 5-Oct-2000 W. Greathouse wgreathouse@smva.com
  77. * Fix memory leak and memory corruption.
  78. * -- Every name resolution resulted in
  79. * a new parse of resolv.conf and new
  80. * copy of nameservers allocated by
  81. * strdup.
  82. * -- Every name resolution resulted in
  83. * a new read of resolv.conf without
  84. * resetting index from prior read...
  85. * resulting in exceeding array bounds.
  86. *
  87. * Limit nameservers read from resolv.conf.
  88. * Add "search" domains from resolv.conf.
  89. * Some systems will return a security
  90. * signature along with query answer for
  91. * dynamic DNS entries -- skip/ignore this answer.
  92. * Include arpa/nameser.h for defines.
  93. * General cleanup.
  94. *
  95. * 20-Jun-2001 Michal Moskal <malekith@pld.org.pl>
  96. * partial IPv6 support (i.e. gethostbyname2() and resolve_address2()
  97. * functions added), IPv6 nameservers are also supported.
  98. *
  99. * 6-Oct-2001 Jari Korva <jari.korva@iki.fi>
  100. * more IPv6 support (IPv6 support for gethostbyaddr();
  101. * address family parameter and improved IPv6 support for get_hosts_byname
  102. * and read_etc_hosts; getnameinfo() port from glibc; defined
  103. * defined ip6addr_any and in6addr_loopback)
  104. *
  105. * 2-Feb-2002 Erik Andersen <andersen@codepoet.org>
  106. * Added gethostent(), sethostent(), and endhostent()
  107. *
  108. * 17-Aug-2002 Manuel Novoa III <mjn3@codepoet.org>
  109. * Fixed __read_etc_hosts_r to return alias list, and modified buffer
  110. * allocation accordingly. See MAX_ALIASES and ALIAS_DIM below.
  111. * This fixes the segfault in the Python 2.2.1 socket test.
  112. *
  113. * 04-Jan-2003 Jay Kulpinski <jskulpin@berkshire.rr.com>
  114. * Fixed __decode_dotted to count the terminating null character
  115. * in a host name.
  116. *
  117. * 02-Oct-2003 Tony J. White <tjw@tjw.org>
  118. * Lifted dn_expand() and dependent ns_name_uncompress(), ns_name_unpack(),
  119. * and ns_name_ntop() from glibc 2.3.2 for compatibility with ipsec-tools
  120. * and openldap.
  121. *
  122. * 7-Sep-2004 Erik Andersen <andersen@codepoet.org>
  123. * Added gethostent_r()
  124. *
  125. * 2008, 2009 Denys Vlasenko <vda.linux@googlemail.com>
  126. * Cleanups, fixes, readability, more cleanups and more fixes.
  127. *
  128. * March 2010 Bernhard Reutner-Fischer
  129. * Switch to common config parser
  130. */
  131. /* Nota bene:
  132. * The whole resolver code has several (severe) problems:
  133. * - it doesn't even build without IPv4, i.e. !UCLIBC_HAS_IPV4 but only IPv6
  134. * - it is way too big
  135. *
  136. * Both points above are considered bugs, patches/reimplementations welcome.
  137. */
  138. /* RFC 1035
  139. ...
  140. Whenever an octet represents a numeric quantity, the left most bit
  141. in the diagram is the high order or most significant bit.
  142. That is, the bit labeled 0 is the most significant bit.
  143. ...
  144. 4.1.1. Header section format
  145. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  146. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  147. | ID |
  148. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  149. |QR| OPCODE |AA|TC|RD|RA| 0 0 0| RCODE |
  150. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  151. | QDCOUNT |
  152. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  153. | ANCOUNT |
  154. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  155. | NSCOUNT |
  156. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  157. | ARCOUNT |
  158. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  159. ID 16 bit random identifier assigned by querying peer.
  160. Used to match query/response.
  161. QR message is a query (0), or a response (1).
  162. OPCODE 0 standard query (QUERY)
  163. 1 inverse query (IQUERY)
  164. 2 server status request (STATUS)
  165. AA Authoritative Answer - this bit is valid in responses.
  166. Responding name server is an authority for the domain name
  167. in question section. Answer section may have multiple owner names
  168. because of aliases. The AA bit corresponds to the name which matches
  169. the query name, or the first owner name in the answer section.
  170. TC TrunCation - this message was truncated.
  171. RD Recursion Desired - this bit may be set in a query and
  172. is copied into the response. If RD is set, it directs
  173. the name server to pursue the query recursively.
  174. Recursive query support is optional.
  175. RA Recursion Available - this be is set or cleared in a
  176. response, and denotes whether recursive query support is
  177. available in the name server.
  178. RCODE Response code.
  179. 0 No error condition
  180. 1 Format error
  181. 2 Server failure - server was unable to process the query
  182. due to a problem with the name server.
  183. 3 Name Error - meaningful only for responses from
  184. an authoritative name server. The referenced domain name
  185. does not exist.
  186. 4 Not Implemented.
  187. 5 Refused.
  188. QDCOUNT number of entries in the question section.
  189. ANCOUNT number of records in the answer section.
  190. NSCOUNT number of records in the authority records section.
  191. ARCOUNT number of records in the additional records section.
  192. 4.1.2. Question section format
  193. The section contains QDCOUNT (usually 1) entries, each of this format:
  194. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  195. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  196. / QNAME /
  197. / /
  198. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  199. | QTYPE |
  200. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  201. | QCLASS |
  202. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  203. QNAME a domain name represented as a sequence of labels, where
  204. each label consists of a length octet followed by that
  205. number of octets. The domain name terminates with the
  206. zero length octet for the null label of the root. Note
  207. that this field may be an odd number of octets; no
  208. padding is used.
  209. QTYPE a two octet type of the query.
  210. 1 a host address [REQ_A const]
  211. 2 an authoritative name server
  212. 3 a mail destination (Obsolete - use MX)
  213. 4 a mail forwarder (Obsolete - use MX)
  214. 5 the canonical name for an alias
  215. 6 marks the start of a zone of authority
  216. 7 a mailbox domain name (EXPERIMENTAL)
  217. 8 a mail group member (EXPERIMENTAL)
  218. 9 a mail rename domain name (EXPERIMENTAL)
  219. 10 a null RR (EXPERIMENTAL)
  220. 11 a well known service description
  221. 12 a domain name pointer [REQ_PTR const]
  222. 13 host information
  223. 14 mailbox or mail list information
  224. 15 mail exchange
  225. 16 text strings
  226. 0x1c IPv6?
  227. 252 a request for a transfer of an entire zone
  228. 253 a request for mailbox-related records (MB, MG or MR)
  229. 254 a request for mail agent RRs (Obsolete - see MX)
  230. 255 a request for all records
  231. QCLASS a two octet code that specifies the class of the query.
  232. 1 the Internet
  233. (others are historic only)
  234. 255 any class
  235. 4.1.3. Resource record format
  236. The answer, authority, and additional sections all share the same format:
  237. a variable number of resource records, where the number of records
  238. is specified in the corresponding count field in the header.
  239. Each resource record has this format:
  240. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  241. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  242. / /
  243. / NAME /
  244. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  245. | TYPE |
  246. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  247. | CLASS |
  248. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  249. | TTL |
  250. | |
  251. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  252. | RDLENGTH |
  253. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
  254. / RDATA /
  255. / /
  256. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  257. NAME a domain name to which this resource record pertains.
  258. TYPE two octets containing one of the RR type codes. This
  259. field specifies the meaning of the data in the RDATA field.
  260. CLASS two octets which specify the class of the data in the RDATA field.
  261. TTL a 32 bit unsigned integer that specifies the time interval
  262. (in seconds) that the record may be cached.
  263. RDLENGTH a 16 bit integer, length in octets of the RDATA field.
  264. RDATA a variable length string of octets that describes the resource.
  265. The format of this information varies according to the TYPE
  266. and CLASS of the resource record.
  267. If the TYPE is A and the CLASS is IN, it's a 4 octet IP address.
  268. 4.1.4. Message compression
  269. In order to reduce the size of messages, domain names can be compressed.
  270. An entire domain name or a list of labels at the end of a domain name
  271. is replaced with a pointer to a prior occurance of the same name.
  272. The pointer takes the form of a two octet sequence:
  273. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  274. | 1 1| OFFSET |
  275. +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  276. The first two bits are ones. This allows a pointer to be distinguished
  277. from a label, since the label must begin with two zero bits because
  278. labels are restricted to 63 octets or less. The OFFSET field specifies
  279. an offset from the start of the message (i.e., the first octet
  280. of the ID field in the domain header).
  281. A zero offset specifies the first byte of the ID field, etc.
  282. Domain name in a message can be represented as either:
  283. - a sequence of labels ending in a zero octet
  284. - a pointer
  285. - a sequence of labels ending with a pointer
  286. */
  287. #include <string.h>
  288. #include <stdio.h>
  289. #include <stdio_ext.h>
  290. #include <signal.h>
  291. #include <errno.h>
  292. #include <sys/poll.h>
  293. #include <sys/socket.h>
  294. #include <sys/types.h>
  295. #include <sys/time.h>
  296. #include <netinet/in.h>
  297. #include <arpa/inet.h>
  298. #include <stdlib.h>
  299. #include <unistd.h>
  300. #include <resolv.h>
  301. #include <netdb.h>
  302. #include <ctype.h>
  303. #include <stdbool.h>
  304. #include <time.h>
  305. #include <arpa/nameser.h>
  306. #include <sys/utsname.h>
  307. #include <sys/un.h>
  308. #include <sys/stat.h>
  309. #include <sys/param.h>
  310. #include <bits/uClibc_mutex.h>
  311. #include "internal/parse_config.h"
  312. /* poll() is not supported in kernel <= 2.0, therefore if __NR_poll is
  313. * not available, we assume an old Linux kernel is in use and we will
  314. * use select() instead. */
  315. #include <sys/syscall.h>
  316. #ifndef __NR_poll
  317. # define USE_SELECT
  318. #endif
  319. #if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__
  320. #define IF_HAS_BOTH(...) __VA_ARGS__
  321. #else
  322. #define IF_HAS_BOTH(...)
  323. #endif
  324. #define MAX_RECURSE 5
  325. #define MAXALIASES (4)
  326. #define BUFSZ (80) /* one line */
  327. #define NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */
  328. #define DNS_LABELTYPE_BITSTRING 0x41
  329. #undef DEBUG
  330. /* #define DEBUG */
  331. #ifdef DEBUG
  332. #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
  333. #else
  334. #define DPRINTF(X,args...)
  335. #endif
  336. /* Make sure the incoming char * buffer is aligned enough to handle our random
  337. * structures. This define is the same as we use for malloc alignment (which
  338. * has same requirements). The offset is the number of bytes we need to adjust
  339. * in order to attain desired alignment.
  340. */
  341. #define ALIGN_ATTR __alignof__(double __attribute_aligned__ (sizeof(size_t)))
  342. #define ALIGN_BUFFER_OFFSET(buf) ((ALIGN_ATTR - ((size_t)buf % ALIGN_ATTR)) % ALIGN_ATTR)
  343. /* Structs */
  344. struct resolv_header {
  345. int id;
  346. int qr, opcode, aa, tc, rd, ra, rcode;
  347. int qdcount;
  348. int ancount;
  349. int nscount;
  350. int arcount;
  351. };
  352. struct resolv_question {
  353. char *dotted;
  354. int qtype;
  355. int qclass;
  356. };
  357. struct resolv_answer {
  358. char *dotted;
  359. int atype;
  360. int aclass;
  361. int ttl;
  362. int rdlength;
  363. const unsigned char *rdata;
  364. int rdoffset;
  365. char* buf;
  366. size_t buflen;
  367. size_t add_count;
  368. };
  369. enum etc_hosts_action {
  370. GET_HOSTS_BYNAME = 0,
  371. GETHOSTENT,
  372. GET_HOSTS_BYADDR,
  373. };
  374. typedef union sockaddr46_t {
  375. struct sockaddr sa;
  376. #ifdef __UCLIBC_HAS_IPV4__
  377. struct sockaddr_in sa4;
  378. #endif
  379. #ifdef __UCLIBC_HAS_IPV6__
  380. struct sockaddr_in6 sa6;
  381. #endif
  382. } sockaddr46_t;
  383. __UCLIBC_MUTEX_EXTERN(__resolv_lock) attribute_hidden;
  384. /* Protected by __resolv_lock */
  385. extern void (*__res_sync)(void) attribute_hidden;
  386. /*extern uint32_t __resolv_opts attribute_hidden; */
  387. extern uint8_t __resolv_timeout attribute_hidden;
  388. extern uint8_t __resolv_attempts attribute_hidden;
  389. extern unsigned __nameservers attribute_hidden;
  390. extern unsigned __searchdomains attribute_hidden;
  391. extern sockaddr46_t *__nameserver attribute_hidden;
  392. extern char **__searchdomain attribute_hidden;
  393. #ifdef __UCLIBC_HAS_IPV4__
  394. extern const struct sockaddr_in __local_nameserver attribute_hidden;
  395. #else
  396. extern const struct sockaddr_in6 __local_nameserver attribute_hidden;
  397. #endif
  398. /* Arbitrary */
  399. #define MAXLEN_searchdomain 128
  400. /* prototypes for internal functions */
  401. extern void endhostent_unlocked(void) attribute_hidden;
  402. extern int __get_hosts_byname_r(const char *name,
  403. int type,
  404. struct hostent *result_buf,
  405. char *buf,
  406. size_t buflen,
  407. struct hostent **result,
  408. int *h_errnop) attribute_hidden;
  409. extern int __get_hosts_byaddr_r(const char *addr,
  410. int len,
  411. int type,
  412. struct hostent *result_buf,
  413. char *buf,
  414. size_t buflen,
  415. struct hostent **result,
  416. int *h_errnop) attribute_hidden;
  417. extern parser_t *__open_etc_hosts(void) attribute_hidden;
  418. extern int __read_etc_hosts_r(parser_t *parser,
  419. const char *name,
  420. int type,
  421. enum etc_hosts_action action,
  422. struct hostent *result_buf,
  423. char *buf,
  424. size_t buflen,
  425. struct hostent **result,
  426. int *h_errnop) attribute_hidden;
  427. extern int __dns_lookup(const char *name,
  428. int type,
  429. unsigned char **outpacket,
  430. struct resolv_answer *a) attribute_hidden;
  431. extern int __encode_dotted(const char *dotted,
  432. unsigned char *dest,
  433. int maxlen) attribute_hidden;
  434. extern int __decode_dotted(const unsigned char *packet,
  435. int offset,
  436. int packet_len,
  437. char *dest,
  438. int dest_len) attribute_hidden;
  439. extern int __encode_header(struct resolv_header *h,
  440. unsigned char *dest,
  441. int maxlen) attribute_hidden;
  442. extern void __decode_header(unsigned char *data,
  443. struct resolv_header *h) attribute_hidden;
  444. extern int __encode_question(const struct resolv_question *q,
  445. unsigned char *dest,
  446. int maxlen) attribute_hidden;
  447. extern int __encode_answer(struct resolv_answer *a,
  448. unsigned char *dest,
  449. int maxlen) attribute_hidden;
  450. extern void __open_nameservers(void) attribute_hidden;
  451. extern void __close_nameservers(void) attribute_hidden;
  452. /*
  453. * Theory of operation.
  454. *
  455. * gethostbyname, getaddrinfo and friends end up here, and they sometimes
  456. * need to talk to DNS servers. In order to do this, we need to read /etc/resolv.conf
  457. * and determine servers' addresses and the like. resolv.conf format:
  458. *
  459. * nameserver <IP[v6]>
  460. * Address of DNS server. Cumulative.
  461. * If not specified, assumed to be on localhost.
  462. * search <domain1>[ <domain2>]...
  463. * Append these domains to unqualified names.
  464. * See ndots:n option.
  465. * $LOCALDOMAIN (space-separated list) overrides this.
  466. * domain <domain>
  467. * Effectively same as "search" with one domain.
  468. * If no "domain" line is present, the domain is determined
  469. * from the local host name returned by gethostname();
  470. * the domain part is taken to be everything after the first dot.
  471. * If there are no dots, there will be no "domain".
  472. * The domain and search keywords are mutually exclusive.
  473. * If more than one instance of these keywords is present,
  474. * the last instance wins.
  475. * sortlist 130.155.160.0[/255.255.240.0] 130.155.0.0
  476. * Allows addresses returned by gethostbyname to be sorted.
  477. * Not supported.
  478. * options option[ option]...
  479. * (so far we support timeout:n and attempts:n)
  480. * $RES_OPTIONS (space-separated list) is to be added to "options"
  481. * debug sets RES_DEBUG in _res.options
  482. * ndots:n how many dots there should be so that name will be tried
  483. * first as an absolute name before any search list elements
  484. * are appended to it. Default 1
  485. * timeout:n how long to wait for response. Default 5
  486. * (sun seems to have retrans:n synonym)
  487. * attempts:n number of rounds to do before giving up and returning
  488. * an error. Default 2
  489. * (sun seems to have retry:n synonym)
  490. * rotate sets RES_ROTATE in _res.options, round robin
  491. * selection of nameservers. Otherwise try
  492. * the first listed server first every time
  493. * no-check-names
  494. * sets RES_NOCHECKNAME in _res.options, which disables
  495. * checking of incoming host names for invalid characters
  496. * such as underscore (_), non-ASCII, or control characters
  497. * inet6 sets RES_USE_INET6 in _res.options. Try a AAAA query
  498. * before an A query inside the gethostbyname(), and map
  499. * IPv4 responses in IPv6 "tunnelled form" if no AAAA records
  500. * are found but an A record set exists
  501. * no_tld_query (FreeBSDism?)
  502. * do not attempt to resolve names without dots
  503. *
  504. * We will read and analyze /etc/resolv.conf as needed before
  505. * we do a DNS request. This happens in __dns_lookup.
  506. * It is reread if its mtime is changed.
  507. *
  508. * BSD has res_init routine which is used to initialize resolver state
  509. * which is held in global structure _res.
  510. * Generally, programs call res_init, then fiddle with _res.XXX
  511. * (_res.options and _res.nscount, _res.nsaddr_list[N]
  512. * are popular targets of fiddling) and expect subsequent calls
  513. * to gethostbyname, getaddrinfo, etc to use modified information.
  514. *
  515. * However, historical _res structure is quite awkward.
  516. * Using it for storing /etc/resolv.conf info is not desirable,
  517. * and __dns_lookup does not use it.
  518. *
  519. * We would like to avoid using it unless absolutely necessary.
  520. * If user doesn't use res_init, we should arrange it so that
  521. * _res structure doesn't even *get linked in* into user's application
  522. * (imagine static uclibc build here).
  523. *
  524. * The solution is a __res_sync function pointer, which is normally NULL.
  525. * But if res_init is called, it gets set and any subsequent gethostbyname
  526. * et al "syncronizes" our internal structures with potentially
  527. * modified _res.XXX stuff by calling __res_sync.
  528. * The trick here is that if res_init is not used and not linked in,
  529. * gethostbyname itself won't reference _res and _res won't be linked in
  530. * either. Other possible methods like
  531. * if (__res_sync_just_an_int_flag)
  532. * __sync_me_with_res()
  533. * would pull in __sync_me_with_res, which pulls in _res. Bad.
  534. */
  535. #ifdef L_encodeh
  536. int __encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
  537. {
  538. if (maxlen < HFIXEDSZ)
  539. return -1;
  540. dest[0] = (h->id & 0xff00) >> 8;
  541. dest[1] = (h->id & 0x00ff) >> 0;
  542. dest[2] = (h->qr ? 0x80 : 0) |
  543. ((h->opcode & 0x0f) << 3) |
  544. (h->aa ? 0x04 : 0) |
  545. (h->tc ? 0x02 : 0) |
  546. (h->rd ? 0x01 : 0);
  547. dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
  548. dest[4] = (h->qdcount & 0xff00) >> 8;
  549. dest[5] = (h->qdcount & 0x00ff) >> 0;
  550. dest[6] = (h->ancount & 0xff00) >> 8;
  551. dest[7] = (h->ancount & 0x00ff) >> 0;
  552. dest[8] = (h->nscount & 0xff00) >> 8;
  553. dest[9] = (h->nscount & 0x00ff) >> 0;
  554. dest[10] = (h->arcount & 0xff00) >> 8;
  555. dest[11] = (h->arcount & 0x00ff) >> 0;
  556. return HFIXEDSZ;
  557. }
  558. #endif /* L_encodeh */
  559. #ifdef L_decodeh
  560. void __decode_header(unsigned char *data,
  561. struct resolv_header *h)
  562. {
  563. h->id = (data[0] << 8) | data[1];
  564. h->qr = (data[2] & 0x80) ? 1 : 0;
  565. h->opcode = (data[2] >> 3) & 0x0f;
  566. h->aa = (data[2] & 0x04) ? 1 : 0;
  567. h->tc = (data[2] & 0x02) ? 1 : 0;
  568. h->rd = (data[2] & 0x01) ? 1 : 0;
  569. h->ra = (data[3] & 0x80) ? 1 : 0;
  570. h->rcode = data[3] & 0x0f;
  571. h->qdcount = (data[4] << 8) | data[5];
  572. h->ancount = (data[6] << 8) | data[7];
  573. h->nscount = (data[8] << 8) | data[9];
  574. h->arcount = (data[10] << 8) | data[11];
  575. }
  576. #endif /* L_decodeh */
  577. #ifdef L_encoded
  578. /* Encode a dotted string into nameserver transport-level encoding.
  579. This routine is fairly dumb, and doesn't attempt to compress
  580. the data */
  581. int __encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
  582. {
  583. unsigned used = 0;
  584. while (dotted && *dotted) {
  585. char *c = strchr(dotted, '.');
  586. int l = c ? c - dotted : strlen(dotted);
  587. /* two consecutive dots are not valid */
  588. if (l == 0)
  589. return -1;
  590. if (l >= (maxlen - used - 1))
  591. return -1;
  592. dest[used++] = l;
  593. memcpy(dest + used, dotted, l);
  594. used += l;
  595. if (!c)
  596. break;
  597. dotted = c + 1;
  598. }
  599. if (maxlen < 1)
  600. return -1;
  601. dest[used++] = 0;
  602. return used;
  603. }
  604. #endif /* L_encoded */
  605. #ifdef L_decoded
  606. /* Decode a dotted string from nameserver transport-level encoding.
  607. This routine understands compressed data. */
  608. int __decode_dotted(const unsigned char *packet,
  609. int offset,
  610. int packet_len,
  611. char *dest,
  612. int dest_len)
  613. {
  614. unsigned b;
  615. bool measure = 1;
  616. unsigned total = 0;
  617. unsigned used = 0;
  618. if (!packet)
  619. return -1;
  620. while (1) {
  621. if (offset >= packet_len)
  622. return -1;
  623. b = packet[offset++];
  624. if (b == 0)
  625. break;
  626. if (measure)
  627. total++;
  628. if ((b & 0xc0) == 0xc0) {
  629. if (offset >= packet_len)
  630. return -1;
  631. if (measure)
  632. total++;
  633. /* compressed item, redirect */
  634. offset = ((b & 0x3f) << 8) | packet[offset];
  635. measure = 0;
  636. continue;
  637. }
  638. if (used + b + 1 >= dest_len)
  639. return -1;
  640. if (offset + b >= packet_len)
  641. return -1;
  642. memcpy(dest + used, packet + offset, b);
  643. offset += b;
  644. used += b;
  645. if (measure)
  646. total += b;
  647. if (packet[offset] != 0)
  648. dest[used++] = '.';
  649. else
  650. dest[used++] = '\0';
  651. }
  652. /* The null byte must be counted too */
  653. if (measure)
  654. total++;
  655. DPRINTF("Total decode len = %d\n", total);
  656. return total;
  657. }
  658. #endif /* L_decoded */
  659. #ifdef L_encodeq
  660. int __encode_question(const struct resolv_question *q,
  661. unsigned char *dest,
  662. int maxlen)
  663. {
  664. int i;
  665. i = __encode_dotted(q->dotted, dest, maxlen);
  666. if (i < 0)
  667. return i;
  668. dest += i;
  669. maxlen -= i;
  670. if (maxlen < 4)
  671. return -1;
  672. dest[0] = (q->qtype & 0xff00) >> 8;
  673. dest[1] = (q->qtype & 0x00ff) >> 0;
  674. dest[2] = (q->qclass & 0xff00) >> 8;
  675. dest[3] = (q->qclass & 0x00ff) >> 0;
  676. return i + 4;
  677. }
  678. #endif /* L_encodeq */
  679. #ifdef L_encodea
  680. int __encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
  681. {
  682. int i;
  683. i = __encode_dotted(a->dotted, dest, maxlen);
  684. if (i < 0)
  685. return i;
  686. dest += i;
  687. maxlen -= i;
  688. if (maxlen < (RRFIXEDSZ + a->rdlength))
  689. return -1;
  690. *dest++ = (a->atype & 0xff00) >> 8;
  691. *dest++ = (a->atype & 0x00ff) >> 0;
  692. *dest++ = (a->aclass & 0xff00) >> 8;
  693. *dest++ = (a->aclass & 0x00ff) >> 0;
  694. *dest++ = (a->ttl & 0xff000000) >> 24;
  695. *dest++ = (a->ttl & 0x00ff0000) >> 16;
  696. *dest++ = (a->ttl & 0x0000ff00) >> 8;
  697. *dest++ = (a->ttl & 0x000000ff) >> 0;
  698. *dest++ = (a->rdlength & 0xff00) >> 8;
  699. *dest++ = (a->rdlength & 0x00ff) >> 0;
  700. memcpy(dest, a->rdata, a->rdlength);
  701. return i + RRFIXEDSZ + a->rdlength;
  702. }
  703. #endif /* L_encodea */
  704. #ifdef CURRENTLY_UNUSED
  705. #ifdef L_encodep
  706. int __encode_packet(struct resolv_header *h,
  707. struct resolv_question **q,
  708. struct resolv_answer **an,
  709. struct resolv_answer **ns,
  710. struct resolv_answer **ar,
  711. unsigned char *dest, int maxlen) attribute_hidden;
  712. int __encode_packet(struct resolv_header *h,
  713. struct resolv_question **q,
  714. struct resolv_answer **an,
  715. struct resolv_answer **ns,
  716. struct resolv_answer **ar,
  717. unsigned char *dest, int maxlen)
  718. {
  719. int i, total = 0;
  720. unsigned j;
  721. i = __encode_header(h, dest, maxlen);
  722. if (i < 0)
  723. return i;
  724. dest += i;
  725. maxlen -= i;
  726. total += i;
  727. for (j = 0; j < h->qdcount; j++) {
  728. i = __encode_question(q[j], dest, maxlen);
  729. if (i < 0)
  730. return i;
  731. dest += i;
  732. maxlen -= i;
  733. total += i;
  734. }
  735. for (j = 0; j < h->ancount; j++) {
  736. i = __encode_answer(an[j], dest, maxlen);
  737. if (i < 0)
  738. return i;
  739. dest += i;
  740. maxlen -= i;
  741. total += i;
  742. }
  743. for (j = 0; j < h->nscount; j++) {
  744. i = __encode_answer(ns[j], dest, maxlen);
  745. if (i < 0)
  746. return i;
  747. dest += i;
  748. maxlen -= i;
  749. total += i;
  750. }
  751. for (j = 0; j < h->arcount; j++) {
  752. i = __encode_answer(ar[j], dest, maxlen);
  753. if (i < 0)
  754. return i;
  755. dest += i;
  756. maxlen -= i;
  757. total += i;
  758. }
  759. return total;
  760. }
  761. #endif /* L_encodep */
  762. #ifdef L_decodep
  763. int __decode_packet(unsigned char *data, struct resolv_header *h) attribute_hidden;
  764. int __decode_packet(unsigned char *data, struct resolv_header *h)
  765. {
  766. __decode_header(data, h);
  767. return HFIXEDSZ;
  768. }
  769. #endif /* L_decodep */
  770. #ifdef L_formquery
  771. int __form_query(int id,
  772. const char *name,
  773. int type,
  774. unsigned char *packet,
  775. int maxlen) attribute_hidden;
  776. int __form_query(int id,
  777. const char *name,
  778. int type,
  779. unsigned char *packet,
  780. int maxlen)
  781. {
  782. struct resolv_header h;
  783. struct resolv_question q;
  784. int i, j;
  785. memset(&h, 0, sizeof(h));
  786. h.id = id;
  787. h.qdcount = 1;
  788. q.dotted = (char *) name;
  789. q.qtype = type;
  790. q.qclass = C_IN; /* CLASS_IN */
  791. i = __encode_header(&h, packet, maxlen);
  792. if (i < 0)
  793. return i;
  794. j = __encode_question(&q, packet + i, maxlen - i);
  795. if (j < 0)
  796. return j;
  797. return i + j;
  798. }
  799. #endif /* L_formquery */
  800. #endif /* CURRENTLY_UNUSED */
  801. #ifdef L_opennameservers
  802. # if __BYTE_ORDER == __LITTLE_ENDIAN
  803. #define NAMESERVER_PORT_N (__bswap_constant_16(NAMESERVER_PORT))
  804. #else
  805. #define NAMESERVER_PORT_N NAMESERVER_PORT
  806. #endif
  807. __UCLIBC_MUTEX_INIT(__resolv_lock, PTHREAD_MUTEX_INITIALIZER);
  808. /* Protected by __resolv_lock */
  809. void (*__res_sync)(void);
  810. /*uint32_t __resolv_opts; */
  811. uint8_t __resolv_timeout = RES_TIMEOUT;
  812. uint8_t __resolv_attempts = RES_DFLRETRY;
  813. unsigned __nameservers;
  814. unsigned __searchdomains;
  815. sockaddr46_t *__nameserver;
  816. char **__searchdomain;
  817. #ifdef __UCLIBC_HAS_IPV4__
  818. const struct sockaddr_in __local_nameserver = {
  819. .sin_family = AF_INET,
  820. .sin_port = NAMESERVER_PORT_N,
  821. };
  822. #else
  823. const struct sockaddr_in6 __local_nameserver = {
  824. .sin6_family = AF_INET6,
  825. .sin6_port = NAMESERVER_PORT_N,
  826. };
  827. #endif
  828. /* Helpers. Both stop on EOL, if it's '\n', it is converted to NUL first */
  829. static char *skip_nospace(char *p)
  830. {
  831. while (*p != '\0' && !isspace(*p)) {
  832. if (*p == '\n') {
  833. *p = '\0';
  834. break;
  835. }
  836. p++;
  837. }
  838. return p;
  839. }
  840. static char *skip_and_NUL_space(char *p)
  841. {
  842. /* NB: '\n' is not isspace! */
  843. while (1) {
  844. char c = *p;
  845. if (c == '\0' || !isspace(c))
  846. break;
  847. *p = '\0';
  848. if (c == '\n' || c == '#')
  849. break;
  850. p++;
  851. }
  852. return p;
  853. }
  854. /* Must be called under __resolv_lock. */
  855. void __open_nameservers(void)
  856. {
  857. static uint32_t resolv_conf_mtime;
  858. char szBuffer[MAXLEN_searchdomain];
  859. FILE *fp;
  860. int i;
  861. sockaddr46_t sa;
  862. if (!__res_sync) {
  863. /* Reread /etc/resolv.conf if it was modified. */
  864. struct stat sb;
  865. if (stat("/etc/resolv.conf", &sb) != 0)
  866. sb.st_mtime = 0;
  867. if (resolv_conf_mtime != (uint32_t)sb.st_mtime) {
  868. resolv_conf_mtime = sb.st_mtime;
  869. __close_nameservers(); /* force config reread */
  870. }
  871. }
  872. if (__nameservers)
  873. goto sync;
  874. __resolv_timeout = RES_TIMEOUT;
  875. __resolv_attempts = RES_DFLRETRY;
  876. fp = fopen("/etc/resolv.conf", "r");
  877. #ifdef FALLBACK_TO_CONFIG_RESOLVCONF
  878. if (!fp) {
  879. /* If we do not have a pre-populated /etc/resolv.conf then
  880. try to use the one from /etc/config which exists on numerous
  881. systems ranging from some uClinux to IRIX installations and
  882. may be the only /etc dir that was mounted rw. */
  883. fp = fopen("/etc/config/resolv.conf", "r");
  884. }
  885. #endif
  886. if (fp) {
  887. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  888. void *ptr;
  889. char *keyword, *p;
  890. keyword = p = skip_and_NUL_space(szBuffer);
  891. /* skip keyword */
  892. p = skip_nospace(p);
  893. /* find next word */
  894. p = skip_and_NUL_space(p);
  895. if (strcmp(keyword, "nameserver") == 0) {
  896. /* terminate IP addr */
  897. *skip_nospace(p) = '\0';
  898. memset(&sa, 0, sizeof(sa));
  899. if (0) /* nothing */;
  900. #ifdef __UCLIBC_HAS_IPV6__
  901. else if (inet_pton(AF_INET6, p, &sa.sa6.sin6_addr) > 0) {
  902. sa.sa6.sin6_family = AF_INET6;
  903. sa.sa6.sin6_port = htons(NAMESERVER_PORT);
  904. }
  905. #endif
  906. #ifdef __UCLIBC_HAS_IPV4__
  907. else if (inet_pton(AF_INET, p, &sa.sa4.sin_addr) > 0) {
  908. sa.sa4.sin_family = AF_INET;
  909. sa.sa4.sin_port = htons(NAMESERVER_PORT);
  910. }
  911. #endif
  912. else
  913. continue; /* garbage on this line */
  914. ptr = realloc(__nameserver, (__nameservers + 1) * sizeof(__nameserver[0]));
  915. if (!ptr)
  916. continue;
  917. __nameserver = ptr;
  918. __nameserver[__nameservers++] = sa; /* struct copy */
  919. continue;
  920. }
  921. if (strcmp(keyword, "domain") == 0 || strcmp(keyword, "search") == 0) {
  922. char *p1;
  923. /* free old domains ("last 'domain' or 'search' wins" rule) */
  924. while (__searchdomains)
  925. free(__searchdomain[--__searchdomains]);
  926. /*free(__searchdomain);*/
  927. /*__searchdomain = NULL; - not necessary */
  928. next_word:
  929. /* terminate current word */
  930. p1 = skip_nospace(p);
  931. /* find next word (maybe) */
  932. p1 = skip_and_NUL_space(p1);
  933. /* add it */
  934. ptr = realloc(__searchdomain, (__searchdomains + 1) * sizeof(__searchdomain[0]));
  935. if (!ptr)
  936. continue;
  937. __searchdomain = ptr;
  938. /* NB: strlen(p) <= MAXLEN_searchdomain) because szBuffer[] is smaller */
  939. ptr = strdup(p);
  940. if (!ptr)
  941. continue;
  942. DPRINTF("adding search %s\n", (char*)ptr);
  943. __searchdomain[__searchdomains++] = (char*)ptr;
  944. p = p1;
  945. if (*p)
  946. goto next_word;
  947. continue;
  948. }
  949. /* if (strcmp(keyword, "sortlist") == 0)... */
  950. if (strcmp(keyword, "options") == 0) {
  951. char *p1;
  952. uint8_t *what;
  953. if (p == NULL || (p1 = strchr(p, ':')) == NULL)
  954. continue;
  955. *p1++ = '\0';
  956. if (strcmp(p, "timeout") == 0)
  957. what = &__resolv_timeout;
  958. else if (strcmp(p, "attempts") == 0)
  959. what = &__resolv_attempts;
  960. else
  961. continue;
  962. *what = atoi(p1);
  963. DPRINTF("option %s:%d\n", p, *what);
  964. }
  965. }
  966. fclose(fp);
  967. }
  968. if (__nameservers == 0) {
  969. /* Have to handle malloc failure! What a mess...
  970. * And it's not only here, we need to be careful
  971. * to never write into __nameserver[0] if it points
  972. * to constant __local_nameserver, or free it. */
  973. __nameserver = malloc(sizeof(__nameserver[0]));
  974. if (__nameserver)
  975. memcpy(__nameserver, &__local_nameserver, sizeof(__local_nameserver));
  976. else
  977. __nameserver = (void*) &__local_nameserver;
  978. __nameservers++;
  979. }
  980. if (__searchdomains == 0) {
  981. char buf[256];
  982. char *p;
  983. i = gethostname(buf, sizeof(buf) - 1);
  984. buf[sizeof(buf) - 1] = '\0';
  985. if (i == 0 && (p = strchr(buf, '.')) != NULL && p[1]) {
  986. p = strdup(p + 1);
  987. if (!p)
  988. goto err;
  989. __searchdomain = malloc(sizeof(__searchdomain[0]));
  990. if (!__searchdomain) {
  991. free(p);
  992. goto err;
  993. }
  994. __searchdomain[0] = p;
  995. __searchdomains++;
  996. err: ;
  997. }
  998. }
  999. DPRINTF("nameservers = %d\n", __nameservers);
  1000. sync:
  1001. if (__res_sync)
  1002. __res_sync();
  1003. }
  1004. #endif /* L_opennameservers */
  1005. #ifdef L_closenameservers
  1006. /* Must be called under __resolv_lock. */
  1007. void __close_nameservers(void)
  1008. {
  1009. if (__nameserver != (void*) &__local_nameserver)
  1010. free(__nameserver);
  1011. __nameserver = NULL;
  1012. __nameservers = 0;
  1013. while (__searchdomains)
  1014. free(__searchdomain[--__searchdomains]);
  1015. free(__searchdomain);
  1016. __searchdomain = NULL;
  1017. /*__searchdomains = 0; - already is */
  1018. }
  1019. #endif /* L_closenameservers */
  1020. #ifdef L_dnslookup
  1021. /* Helpers */
  1022. static int __length_question(const unsigned char *data, int maxlen)
  1023. {
  1024. const unsigned char *start;
  1025. unsigned b;
  1026. if (!data)
  1027. return -1;
  1028. start = data;
  1029. while (1) {
  1030. if (maxlen <= 0)
  1031. return -1;
  1032. b = *data++;
  1033. if (b == 0)
  1034. break;
  1035. if ((b & 0xc0) == 0xc0) {
  1036. /* It's a "compressed" name. */
  1037. data++; /* skip lsb of redirected offset */
  1038. maxlen -= 2;
  1039. break;
  1040. }
  1041. data += b;
  1042. maxlen -= (b + 1); /* account for data++ above */
  1043. }
  1044. /* Up to here we were skipping encoded name */
  1045. /* Account for QTYPE and QCLASS fields */
  1046. if (maxlen < 4)
  1047. return -1;
  1048. return data - start + 2 + 2;
  1049. }
  1050. static int __decode_answer(const unsigned char *message, /* packet */
  1051. int offset,
  1052. int len, /* total packet len */
  1053. struct resolv_answer *a)
  1054. {
  1055. char temp[256];
  1056. int i;
  1057. DPRINTF("decode_answer(start): off %d, len %d\n", offset, len);
  1058. i = __decode_dotted(message, offset, len, temp, sizeof(temp));
  1059. if (i < 0)
  1060. return i;
  1061. message += offset + i;
  1062. len -= i + RRFIXEDSZ + offset;
  1063. if (len < 0) {
  1064. DPRINTF("decode_answer: off %d, len %d, i %d\n", offset, len, i);
  1065. return len;
  1066. }
  1067. /* TODO: what if strdup fails? */
  1068. a->dotted = strdup(temp);
  1069. a->atype = (message[0] << 8) | message[1];
  1070. message += 2;
  1071. a->aclass = (message[0] << 8) | message[1];
  1072. message += 2;
  1073. a->ttl = (message[0] << 24) |
  1074. (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
  1075. message += 4;
  1076. a->rdlength = (message[0] << 8) | message[1];
  1077. message += 2;
  1078. a->rdata = message;
  1079. a->rdoffset = offset + i + RRFIXEDSZ;
  1080. DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
  1081. if (len < a->rdlength)
  1082. return -1;
  1083. return i + RRFIXEDSZ + a->rdlength;
  1084. }
  1085. /* On entry:
  1086. * a.buf(len) = auxiliary buffer for IP addresses after first one
  1087. * a.add_count = how many additional addresses are there already
  1088. * outpacket = where to save ptr to raw packet? can be NULL
  1089. * On exit:
  1090. * ret < 0: error, all other data is not valid
  1091. * ret >= 0: length of reply packet
  1092. * a.add_count & a.buf: updated
  1093. * a.rdlength: length of addresses (4 bytes for IPv4)
  1094. * *outpacket: updated (packet is malloced, you need to free it)
  1095. * a.rdata: points into *outpacket to 1st IP addr
  1096. * NB: don't pass outpacket == NULL if you need to use a.rdata!
  1097. * a.atype: type of query?
  1098. * a.dotted: which name we _actually_ used. May contain search domains
  1099. * appended. (why the filed is called "dotted" I have no idea)
  1100. * This is a malloced string. May be NULL because strdup failed.
  1101. */
  1102. int __dns_lookup(const char *name,
  1103. int type,
  1104. unsigned char **outpacket,
  1105. struct resolv_answer *a)
  1106. {
  1107. /* Protected by __resolv_lock: */
  1108. static int last_ns_num = 0;
  1109. static uint16_t last_id = 1;
  1110. int i, j, fd, rc;
  1111. int packet_len;
  1112. int name_len;
  1113. #ifdef USE_SELECT
  1114. struct timeval tv;
  1115. fd_set fds;
  1116. #else
  1117. struct pollfd fds;
  1118. #endif
  1119. struct resolv_header h;
  1120. struct resolv_question q;
  1121. struct resolv_answer ma;
  1122. bool first_answer = 1;
  1123. int retries_left;
  1124. unsigned char *packet = malloc(PACKETSZ);
  1125. char *lookup;
  1126. int variant = -1; /* search domain to append, -1: none */
  1127. int local_ns_num = -1; /* Nth server to use */
  1128. int local_id = local_id; /* for compiler */
  1129. int sdomains;
  1130. bool ends_with_dot;
  1131. sockaddr46_t sa;
  1132. fd = -1;
  1133. lookup = NULL;
  1134. name_len = strlen(name);
  1135. if ((unsigned)name_len >= MAXDNAME - MAXLEN_searchdomain - 2)
  1136. goto fail; /* paranoia */
  1137. lookup = malloc(name_len + 1/*for '.'*/ + MAXLEN_searchdomain + 1);
  1138. if (!packet || !lookup || !name[0])
  1139. goto fail;
  1140. ends_with_dot = (name[name_len - 1] == '.');
  1141. /* no strcpy! paranoia, user might change name[] under us */
  1142. memcpy(lookup, name, name_len);
  1143. DPRINTF("Looking up type %d answer for '%s'\n", type, name);
  1144. retries_left = 0; /* for compiler */
  1145. do {
  1146. int pos;
  1147. unsigned reply_timeout;
  1148. if (fd != -1) {
  1149. close(fd);
  1150. fd = -1;
  1151. }
  1152. /* Mess with globals while under lock */
  1153. /* NB: even data *pointed to* by globals may vanish
  1154. * outside the locks. We should assume any and all
  1155. * globals can completely change between locked
  1156. * code regions. OTOH, this is rare, so we don't need
  1157. * to handle it "nicely" (do not skip servers,
  1158. * search domains, etc), we only need to ensure
  1159. * we do not SEGV, use freed+overwritten data
  1160. * or do other Really Bad Things. */
  1161. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1162. __open_nameservers();
  1163. sdomains = __searchdomains;
  1164. lookup[name_len] = '\0';
  1165. if ((unsigned)variant < sdomains) {
  1166. /* lookup is name_len + 1 + MAXLEN_searchdomain + 1 long */
  1167. /* __searchdomain[] is not bigger than MAXLEN_searchdomain */
  1168. lookup[name_len] = '.';
  1169. strcpy(&lookup[name_len + 1], __searchdomain[variant]);
  1170. }
  1171. /* first time? pick starting server etc */
  1172. if (local_ns_num < 0) {
  1173. local_id = last_id;
  1174. /*TODO: implement /etc/resolv.conf's "options rotate"
  1175. (a.k.a. RES_ROTATE bit in _res.options)
  1176. local_ns_num = 0;
  1177. if (_res.options & RES_ROTATE) */
  1178. local_ns_num = last_ns_num;
  1179. retries_left = __nameservers * __resolv_attempts;
  1180. }
  1181. retries_left--;
  1182. if (local_ns_num >= __nameservers)
  1183. local_ns_num = 0;
  1184. local_id++;
  1185. local_id &= 0xffff;
  1186. /* write new values back while still under lock */
  1187. last_id = local_id;
  1188. last_ns_num = local_ns_num;
  1189. /* struct copy */
  1190. /* can't just take a pointer, __nameserver[x]
  1191. * is not safe to use outside of locks */
  1192. sa = __nameserver[local_ns_num];
  1193. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1194. memset(packet, 0, PACKETSZ);
  1195. memset(&h, 0, sizeof(h));
  1196. /* encode header */
  1197. h.id = local_id;
  1198. h.qdcount = 1;
  1199. h.rd = 1;
  1200. DPRINTF("encoding header\n", h.rd);
  1201. i = __encode_header(&h, packet, PACKETSZ);
  1202. if (i < 0)
  1203. goto fail;
  1204. /* encode question */
  1205. DPRINTF("lookup name: %s\n", lookup);
  1206. q.dotted = lookup;
  1207. q.qtype = type;
  1208. q.qclass = C_IN; /* CLASS_IN */
  1209. j = __encode_question(&q, packet+i, PACKETSZ-i);
  1210. if (j < 0)
  1211. goto fail;
  1212. packet_len = i + j;
  1213. /* send packet */
  1214. #ifdef DEBUG
  1215. {
  1216. const socklen_t plen = sa.sa.sa_family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
  1217. char *pbuf = malloc(plen);
  1218. if (pbuf == NULL) ;/* nothing */
  1219. #ifdef __UCLIBC_HAS_IPV6__
  1220. else if (sa.sa.sa_family == AF_INET6)
  1221. pbuf = (char*)inet_ntop(AF_INET6, &sa.sa6.sin6_addr, pbuf, plen);
  1222. #endif
  1223. #ifdef __UCLIBC_HAS_IPV4__
  1224. else if (sa.sa.sa_family == AF_INET)
  1225. pbuf = (char*)inet_ntop(AF_INET, &sa.sa4.sin_addr, pbuf, plen);
  1226. #endif
  1227. DPRINTF("On try %d, sending query to %s, port %d\n",
  1228. retries_left, pbuf, NAMESERVER_PORT);
  1229. free(pbuf);
  1230. }
  1231. #endif
  1232. fd = socket(sa.sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
  1233. if (fd < 0) /* paranoia */
  1234. goto try_next_server;
  1235. rc = connect(fd, &sa.sa, sizeof(sa));
  1236. if (rc < 0) {
  1237. /*if (errno == ENETUNREACH) { */
  1238. /* routing error, presume not transient */
  1239. goto try_next_server;
  1240. /*} */
  1241. /*For example, what transient error this can be? Can't think of any */
  1242. /* retry */
  1243. /*continue; */
  1244. }
  1245. DPRINTF("Xmit packet len:%d id:%d qr:%d\n", packet_len, h.id, h.qr);
  1246. /* no error check - if it fails, we time out on recv */
  1247. send(fd, packet, packet_len, 0);
  1248. #ifdef USE_SELECT
  1249. reply_timeout = __resolv_timeout;
  1250. wait_again:
  1251. FD_ZERO(&fds);
  1252. FD_SET(fd, &fds);
  1253. tv.tv_sec = reply_timeout;
  1254. tv.tv_usec = 0;
  1255. if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
  1256. DPRINTF("Timeout\n");
  1257. /* timed out, so retry send and receive
  1258. * to next nameserver */
  1259. goto try_next_server;
  1260. }
  1261. reply_timeout--;
  1262. #else /* !USE_SELECT */
  1263. reply_timeout = __resolv_timeout * 1000;
  1264. wait_again:
  1265. fds.fd = fd;
  1266. fds.events = POLLIN;
  1267. if (poll(&fds, 1, reply_timeout) <= 0) {
  1268. DPRINTF("Timeout\n");
  1269. /* timed out, so retry send and receive
  1270. * to next nameserver */
  1271. goto try_next_server;
  1272. }
  1273. /*TODO: better timeout accounting?*/
  1274. reply_timeout -= 1000;
  1275. #endif /* USE_SELECT */
  1276. /* vda: a bogus response seen in real world (caused SEGV in uclibc):
  1277. * "ping www.google.com" sending AAAA query and getting
  1278. * response with one answer... with answer part missing!
  1279. * Fixed by thorough checks for not going past the packet's end.
  1280. */
  1281. #ifdef DEBUG
  1282. {
  1283. static const char test_query[32] = "\0\2\1\0\0\1\0\0\0\0\0\0\3www\6google\3com\0\0\34\0\1";
  1284. static const char test_respn[32] = "\0\2\201\200\0\1\0\1\0\0\0\0\3www\6google\3com\0\0\34\0\1";
  1285. pos = memcmp(packet + 2, test_query + 2, 30);
  1286. packet_len = recv(fd, packet, PACKETSZ, MSG_DONTWAIT);
  1287. if (pos == 0) {
  1288. packet_len = 32;
  1289. memcpy(packet + 2, test_respn + 2, 30);
  1290. }
  1291. }
  1292. #else
  1293. packet_len = recv(fd, packet, PACKETSZ, MSG_DONTWAIT);
  1294. #endif
  1295. if (packet_len < HFIXEDSZ) {
  1296. /* too short!
  1297. * If the peer did shutdown then retry later,
  1298. * try next peer on error.
  1299. * it's just a bogus packet from somewhere */
  1300. bogus_packet:
  1301. if (packet_len >= 0 && reply_timeout)
  1302. goto wait_again;
  1303. goto try_next_server;
  1304. }
  1305. __decode_header(packet, &h);
  1306. DPRINTF("len:%d id:%d qr:%d\n", packet_len, h.id, h.qr);
  1307. if (h.id != local_id || !h.qr) {
  1308. /* unsolicited */
  1309. goto bogus_packet;
  1310. }
  1311. DPRINTF("Got response (i think)!\n");
  1312. DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
  1313. h.qdcount, h.ancount, h.nscount, h.arcount);
  1314. DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
  1315. h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
  1316. /* bug 660 says we treat negative response as an error
  1317. * and retry, which is, eh, an error. :)
  1318. * We were incurring long delays because of this. */
  1319. if (h.rcode == NXDOMAIN || h.rcode == SERVFAIL) {
  1320. /* if possible, try next search domain */
  1321. if (!ends_with_dot) {
  1322. DPRINTF("variant:%d sdomains:%d\n", variant, sdomains);
  1323. if (variant < sdomains - 1) {
  1324. /* next search domain */
  1325. variant++;
  1326. continue;
  1327. }
  1328. /* no more search domains to try */
  1329. }
  1330. /* dont loop, this is "no such host" situation */
  1331. h_errno = HOST_NOT_FOUND;
  1332. goto fail1;
  1333. }
  1334. /* Insert other non-fatal errors here, which do not warrant
  1335. * switching to next nameserver */
  1336. /* Strange error, assuming this nameserver is feeling bad */
  1337. if (h.rcode != 0)
  1338. goto try_next_server;
  1339. /* Code below won't work correctly with h.ancount == 0, so... */
  1340. if (h.ancount <= 0) {
  1341. h_errno = NO_DATA; /* [is this correct code to check for?] */
  1342. goto fail1;
  1343. }
  1344. pos = HFIXEDSZ;
  1345. for (j = 0; j < h.qdcount; j++) {
  1346. DPRINTF("Skipping question %d at %d\n", j, pos);
  1347. i = __length_question(packet + pos, packet_len - pos);
  1348. if (i < 0) {
  1349. DPRINTF("Packet'question section "
  1350. "is truncated, trying next server\n");
  1351. goto try_next_server;
  1352. }
  1353. pos += i;
  1354. DPRINTF("Length of question %d is %d\n", j, i);
  1355. }
  1356. DPRINTF("Decoding answer at pos %d\n", pos);
  1357. first_answer = 1;
  1358. a->dotted = NULL;
  1359. for (j = 0; j < h.ancount; j++) {
  1360. i = __decode_answer(packet, pos, packet_len, &ma);
  1361. if (i < 0) {
  1362. DPRINTF("failed decode %d\n", i);
  1363. /* If the message was truncated but we have
  1364. * decoded some answers, pretend it's OK */
  1365. if (j && h.tc)
  1366. break;
  1367. goto try_next_server;
  1368. }
  1369. pos += i;
  1370. if (first_answer) {
  1371. ma.buf = a->buf;
  1372. ma.buflen = a->buflen;
  1373. ma.add_count = a->add_count;
  1374. free(a->dotted);
  1375. memcpy(a, &ma, sizeof(ma));
  1376. if (a->atype != T_SIG && (NULL == a->buf || (type != T_A && type != T_AAAA)))
  1377. break;
  1378. if (a->atype != type)
  1379. continue;
  1380. a->add_count = h.ancount - j - 1;
  1381. if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
  1382. break;
  1383. a->add_count = 0;
  1384. first_answer = 0;
  1385. } else {
  1386. free(ma.dotted);
  1387. if (ma.atype != type)
  1388. continue;
  1389. if (a->rdlength != ma.rdlength) {
  1390. free(a->dotted);
  1391. DPRINTF("Answer address len(%u) differs from original(%u)\n",
  1392. ma.rdlength, a->rdlength);
  1393. goto try_next_server;
  1394. }
  1395. memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
  1396. ++a->add_count;
  1397. }
  1398. }
  1399. /* Success! */
  1400. DPRINTF("Answer name = |%s|\n", a->dotted);
  1401. DPRINTF("Answer type = |%d|\n", a->atype);
  1402. if (fd != -1)
  1403. close(fd);
  1404. if (outpacket)
  1405. *outpacket = packet;
  1406. else
  1407. free(packet);
  1408. free(lookup);
  1409. return packet_len;
  1410. try_next_server:
  1411. /* Try next nameserver */
  1412. local_ns_num++;
  1413. variant = -1;
  1414. } while (retries_left > 0);
  1415. fail:
  1416. h_errno = NETDB_INTERNAL;
  1417. fail1:
  1418. if (fd != -1)
  1419. close(fd);
  1420. free(lookup);
  1421. free(packet);
  1422. return -1;
  1423. }
  1424. #endif /* L_dnslookup */
  1425. #ifdef L_read_etc_hosts_r
  1426. parser_t * __open_etc_hosts(void)
  1427. {
  1428. parser_t *parser;
  1429. parser = config_open("/etc/hosts");
  1430. #ifdef FALLBACK_TO_CONFIG_RESOLVCONF
  1431. if (parser == NULL)
  1432. parser = config_open("/etc/config/hosts");
  1433. #endif
  1434. return parser;
  1435. }
  1436. #define MINTOKENS 2 /* ip address + canonical name */
  1437. #define MAXTOKENS (MINTOKENS + MAXALIASES)
  1438. #define HALISTOFF (sizeof(char*) * MAXTOKENS)
  1439. #define INADDROFF (HALISTOFF + 2 * sizeof(char*))
  1440. int __read_etc_hosts_r(
  1441. parser_t * parser,
  1442. const char *name,
  1443. int type,
  1444. enum etc_hosts_action action,
  1445. struct hostent *result_buf,
  1446. char *buf, size_t buflen,
  1447. struct hostent **result,
  1448. int *h_errnop)
  1449. {
  1450. char **tok = NULL;
  1451. struct in_addr *h_addr0 = NULL;
  1452. const size_t aliaslen = INADDROFF +
  1453. #ifdef __UCLIBC_HAS_IPV6__
  1454. sizeof(struct in6_addr)
  1455. #else
  1456. sizeof(struct in_addr)
  1457. #endif
  1458. ;
  1459. int ret = HOST_NOT_FOUND;
  1460. *h_errnop = NETDB_INTERNAL;
  1461. if (buflen < aliaslen
  1462. || (buflen - aliaslen) < BUFSZ + 1)
  1463. return ERANGE;
  1464. if (parser == NULL)
  1465. parser = __open_etc_hosts();
  1466. if (parser == NULL) {
  1467. *result = NULL;
  1468. return errno;
  1469. }
  1470. /* Layout in buf:
  1471. * char *alias[MAXTOKENS] = {address, name, aliases...}
  1472. * char **h_addr_list[1] = {*in[6]_addr, NULL}
  1473. * struct in[6]_addr
  1474. * char line_buffer[BUFSZ+];
  1475. */
  1476. parser->data = buf;
  1477. parser->data_len = aliaslen;
  1478. parser->line_len = buflen - aliaslen;
  1479. *h_errnop = HOST_NOT_FOUND;
  1480. /* <ip>[[:space:]][<aliases>] */
  1481. while (config_read(parser, &tok, MAXTOKENS, MINTOKENS, "# \t", PARSE_NORMAL)) {
  1482. result_buf->h_aliases = tok+1;
  1483. if (action == GETHOSTENT) {
  1484. /* Return whatever the next entry happens to be. */
  1485. break;
  1486. }
  1487. if (action == GET_HOSTS_BYADDR) {
  1488. if (strcmp(name, *tok) != 0)
  1489. continue;
  1490. } else { /* GET_HOSTS_BYNAME */
  1491. int aliases = 0;
  1492. char **alias = tok + 1;
  1493. while (aliases < MAXALIASES) {
  1494. char *tmp = *(alias+aliases++);
  1495. if (tmp && strcasecmp(name, tmp) == 0)
  1496. goto found;
  1497. }
  1498. continue;
  1499. }
  1500. found:
  1501. result_buf->h_name = *(result_buf->h_aliases++);
  1502. result_buf->h_addr_list = (char**)(buf + HALISTOFF);
  1503. *(result_buf->h_addr_list + 1) = '\0';
  1504. h_addr0 = (struct in_addr*)(buf + INADDROFF);
  1505. result_buf->h_addr = (char*)h_addr0;
  1506. if (0) /* nothing */;
  1507. #ifdef __UCLIBC_HAS_IPV4__
  1508. else if (type == AF_INET
  1509. && inet_pton(AF_INET, *tok, h_addr0) > 0) {
  1510. DPRINTF("Found INET\n");
  1511. result_buf->h_addrtype = AF_INET;
  1512. result_buf->h_length = sizeof(struct in_addr);
  1513. *result = result_buf;
  1514. ret = NETDB_SUCCESS;
  1515. }
  1516. #endif
  1517. #ifdef __UCLIBC_HAS_IPV6__
  1518. #define in6 ((struct in6_addr *)buf)
  1519. else if (type == AF_INET6
  1520. && inet_pton(AF_INET6, *tok, h_addr0) > 0) {
  1521. DPRINTF("Found INET6\n");
  1522. result_buf->h_addrtype = AF_INET6;
  1523. result_buf->h_length = sizeof(struct in6_addr);
  1524. *result = result_buf;
  1525. ret = NETDB_SUCCESS;
  1526. }
  1527. #endif
  1528. else {
  1529. /* continue parsing in the hope the user has multiple
  1530. * host types listed in the database like so:
  1531. * <ipv4 addr> host
  1532. * <ipv6 addr> host
  1533. * If looking for an IPv6 addr, don't bail when we got the IPv4
  1534. */
  1535. DPRINTF("Error: Found host but different address family\n");
  1536. /* NB: gethostbyname2_r depends on this feature
  1537. * to avoid looking for IPv6 addr of "localhost" etc */
  1538. ret = TRY_AGAIN;
  1539. continue;
  1540. }
  1541. break;
  1542. }
  1543. if (action != GETHOSTENT)
  1544. config_close(parser);
  1545. return ret;
  1546. #undef in6
  1547. }
  1548. #endif /* L_read_etc_hosts_r */
  1549. #ifdef L_get_hosts_byname_r
  1550. int __get_hosts_byname_r(const char *name,
  1551. int type,
  1552. struct hostent *result_buf,
  1553. char *buf,
  1554. size_t buflen,
  1555. struct hostent **result,
  1556. int *h_errnop)
  1557. {
  1558. return __read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME,
  1559. result_buf, buf, buflen, result, h_errnop);
  1560. }
  1561. #endif /* L_get_hosts_byname_r */
  1562. #ifdef L_get_hosts_byaddr_r
  1563. int __get_hosts_byaddr_r(const char *addr,
  1564. int len,
  1565. int type,
  1566. struct hostent *result_buf,
  1567. char *buf,
  1568. size_t buflen,
  1569. struct hostent **result,
  1570. int *h_errnop)
  1571. {
  1572. #ifndef __UCLIBC_HAS_IPV6__
  1573. char ipaddr[INET_ADDRSTRLEN];
  1574. #else
  1575. char ipaddr[INET6_ADDRSTRLEN];
  1576. #endif
  1577. switch (type) {
  1578. #ifdef __UCLIBC_HAS_IPV4__
  1579. case AF_INET:
  1580. if (len != sizeof(struct in_addr))
  1581. return 0;
  1582. break;
  1583. #endif
  1584. #ifdef __UCLIBC_HAS_IPV6__
  1585. case AF_INET6:
  1586. if (len != sizeof(struct in6_addr))
  1587. return 0;
  1588. break;
  1589. #endif
  1590. default:
  1591. return 0;
  1592. }
  1593. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1594. return __read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1595. result_buf, buf, buflen, result, h_errnop);
  1596. }
  1597. #endif /* L_get_hosts_byaddr_r */
  1598. #ifdef L_getnameinfo
  1599. int getnameinfo(const struct sockaddr *sa,
  1600. socklen_t addrlen,
  1601. char *host,
  1602. socklen_t hostlen,
  1603. char *serv,
  1604. socklen_t servlen,
  1605. unsigned flags)
  1606. {
  1607. int serrno = errno;
  1608. unsigned ok;
  1609. struct hostent *hoste = NULL;
  1610. char domain[256];
  1611. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1612. return EAI_BADFLAGS;
  1613. if (sa == NULL || addrlen < sizeof(sa_family_t))
  1614. return EAI_FAMILY;
  1615. ok = sa->sa_family;
  1616. if (ok == AF_LOCAL) /* valid */;
  1617. #ifdef __UCLIBC_HAS_IPV4__
  1618. else if (ok == AF_INET) {
  1619. if (addrlen < sizeof(struct sockaddr_in))
  1620. return EAI_FAMILY;
  1621. }
  1622. #endif
  1623. #ifdef __UCLIBC_HAS_IPV6__
  1624. else if (ok == AF_INET6) {
  1625. if (addrlen < sizeof(struct sockaddr_in6))
  1626. return EAI_FAMILY;
  1627. }
  1628. #endif
  1629. else
  1630. return EAI_FAMILY;
  1631. ok = 0;
  1632. if (host != NULL && hostlen > 0)
  1633. switch (sa->sa_family) {
  1634. case AF_INET:
  1635. #ifdef __UCLIBC_HAS_IPV6__
  1636. case AF_INET6:
  1637. #endif
  1638. if (!(flags & NI_NUMERICHOST)) {
  1639. if (0) /* nothing */;
  1640. #ifdef __UCLIBC_HAS_IPV6__
  1641. else if (sa->sa_family == AF_INET6)
  1642. hoste = gethostbyaddr((const void *)
  1643. &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1644. sizeof(struct in6_addr), AF_INET6);
  1645. #endif
  1646. #ifdef __UCLIBC_HAS_IPV4__
  1647. else
  1648. hoste = gethostbyaddr((const void *)
  1649. &(((const struct sockaddr_in *)sa)->sin_addr),
  1650. sizeof(struct in_addr), AF_INET);
  1651. #endif
  1652. if (hoste) {
  1653. char *c;
  1654. if ((flags & NI_NOFQDN)
  1655. && (getdomainname(domain, sizeof(domain)) == 0)
  1656. && (c = strstr(hoste->h_name, domain)) != NULL
  1657. && (c != hoste->h_name) && (*(--c) == '.')
  1658. ) {
  1659. strncpy(host, hoste->h_name,
  1660. MIN(hostlen, (size_t) (c - hoste->h_name)));
  1661. host[MIN(hostlen - 1, (size_t) (c - hoste->h_name))] = '\0';
  1662. } else {
  1663. strncpy(host, hoste->h_name, hostlen);
  1664. }
  1665. ok = 1;
  1666. }
  1667. }
  1668. if (!ok) {
  1669. const char *c = NULL;
  1670. if (flags & NI_NAMEREQD) {
  1671. errno = serrno;
  1672. return EAI_NONAME;
  1673. }
  1674. if (0) /* nothing */;
  1675. #ifdef __UCLIBC_HAS_IPV6__
  1676. else if (sa->sa_family == AF_INET6) {
  1677. const struct sockaddr_in6 *sin6p;
  1678. sin6p = (const struct sockaddr_in6 *) sa;
  1679. c = inet_ntop(AF_INET6,
  1680. (const void *) &sin6p->sin6_addr,
  1681. host, hostlen);
  1682. #if 0
  1683. /* Does scope id need to be supported? */
  1684. uint32_t scopeid;
  1685. scopeid = sin6p->sin6_scope_id;
  1686. if (scopeid != 0) {
  1687. /* Buffer is >= IFNAMSIZ+1. */
  1688. char scopebuf[IFNAMSIZ + 1];
  1689. char *scopeptr;
  1690. int ni_numericscope = 0;
  1691. size_t real_hostlen = strnlen(host, hostlen);
  1692. size_t scopelen = 0;
  1693. scopebuf[0] = SCOPE_DELIMITER;
  1694. scopebuf[1] = '\0';
  1695. scopeptr = &scopebuf[1];
  1696. if (IN6_IS_ADDR_LINKLOCAL(&sin6p->sin6_addr)
  1697. || IN6_IS_ADDR_MC_LINKLOCAL(&sin6p->sin6_addr)) {
  1698. if (if_indextoname(scopeid, scopeptr) == NULL)
  1699. ++ni_numericscope;
  1700. else
  1701. scopelen = strlen(scopebuf);
  1702. } else {
  1703. ++ni_numericscope;
  1704. }
  1705. if (ni_numericscope)
  1706. scopelen = 1 + snprintf(scopeptr,
  1707. (scopebuf
  1708. + sizeof scopebuf
  1709. - scopeptr),
  1710. "%u", scopeid);
  1711. if (real_hostlen + scopelen + 1 > hostlen)
  1712. return EAI_SYSTEM;
  1713. memcpy(host + real_hostlen, scopebuf, scopelen + 1);
  1714. }
  1715. #endif
  1716. }
  1717. #endif /* __UCLIBC_HAS_IPV6__ */
  1718. #if defined __UCLIBC_HAS_IPV4__
  1719. else {
  1720. c = inet_ntop(AF_INET, (const void *)
  1721. &(((const struct sockaddr_in *) sa)->sin_addr),
  1722. host, hostlen);
  1723. }
  1724. #endif
  1725. if (c == NULL) {
  1726. errno = serrno;
  1727. return EAI_SYSTEM;
  1728. }
  1729. ok = 1;
  1730. }
  1731. break;
  1732. case AF_LOCAL:
  1733. if (!(flags & NI_NUMERICHOST)) {
  1734. struct utsname utsname;
  1735. if (!uname(&utsname)) {
  1736. strncpy(host, utsname.nodename, hostlen);
  1737. break;
  1738. };
  1739. };
  1740. if (flags & NI_NAMEREQD) {
  1741. errno = serrno;
  1742. return EAI_NONAME;
  1743. }
  1744. strncpy(host, "localhost", hostlen);
  1745. break;
  1746. /* Already checked above
  1747. default:
  1748. return EAI_FAMILY;
  1749. */
  1750. }
  1751. if (serv && (servlen > 0)) {
  1752. if (sa->sa_family == AF_LOCAL) {
  1753. strncpy(serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1754. } else { /* AF_INET || AF_INET6 */
  1755. if (!(flags & NI_NUMERICSERV)) {
  1756. struct servent *s;
  1757. s = getservbyport(((const struct sockaddr_in *) sa)->sin_port,
  1758. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1759. if (s) {
  1760. strncpy(serv, s->s_name, servlen);
  1761. goto DONE;
  1762. }
  1763. }
  1764. snprintf(serv, servlen, "%d",
  1765. ntohs(((const struct sockaddr_in *) sa)->sin_port));
  1766. }
  1767. }
  1768. DONE:
  1769. if (host && (hostlen > 0))
  1770. host[hostlen-1] = 0;
  1771. if (serv && (servlen > 0))
  1772. serv[servlen-1] = 0;
  1773. errno = serrno;
  1774. return 0;
  1775. }
  1776. libc_hidden_def(getnameinfo)
  1777. #endif /* L_getnameinfo */
  1778. #ifdef L_gethostbyname_r
  1779. /* Bug 671 says:
  1780. * "uClibc resolver's gethostbyname does not return the requested name
  1781. * as an alias, but instead returns the canonical name. glibc's
  1782. * gethostbyname has a similar bug where it returns the requested name
  1783. * with the search domain name appended (to make a FQDN) as an alias,
  1784. * but not the original name itself. Both contradict POSIX, which says
  1785. * that the name argument passed to gethostbyname must be in the alias list"
  1786. * This is fixed now, and we differ from glibc:
  1787. *
  1788. * $ ./gethostbyname_uclibc wer.google.com
  1789. * h_name:'c13-ss-2-lb.cnet.com'
  1790. * h_length:4
  1791. * h_addrtype:2 AF_INET
  1792. * alias:'wer.google.com' <===
  1793. * addr: 0x4174efd8 '216.239.116.65'
  1794. *
  1795. * $ ./gethostbyname_glibc wer.google.com
  1796. * h_name:'c13-ss-2-lb.cnet.com'
  1797. * h_length:4
  1798. * h_addrtype:2 AF_INET
  1799. * alias:'wer.google.com.com' <===
  1800. * addr:'216.239.116.65'
  1801. *
  1802. * When examples were run, /etc/resolv.conf contained "search com" line.
  1803. */
  1804. int gethostbyname_r(const char *name,
  1805. struct hostent *result_buf,
  1806. char *buf,
  1807. size_t buflen,
  1808. struct hostent **result,
  1809. int *h_errnop)
  1810. {
  1811. struct in_addr **addr_list;
  1812. char **alias;
  1813. char *alias0;
  1814. unsigned char *packet;
  1815. struct resolv_answer a;
  1816. int i;
  1817. int packet_len;
  1818. int wrong_af = 0;
  1819. *result = NULL;
  1820. if (!name)
  1821. return EINVAL;
  1822. /* do /etc/hosts first */
  1823. {
  1824. int old_errno = errno; /* save the old errno and reset errno */
  1825. __set_errno(0); /* to check for missing /etc/hosts. */
  1826. i = __get_hosts_byname_r(name, AF_INET, result_buf,
  1827. buf, buflen, result, h_errnop);
  1828. if (i == NETDB_SUCCESS) {
  1829. __set_errno(old_errno);
  1830. return i;
  1831. }
  1832. switch (*h_errnop) {
  1833. case HOST_NOT_FOUND:
  1834. wrong_af = (i == TRY_AGAIN);
  1835. case NO_ADDRESS:
  1836. break;
  1837. case NETDB_INTERNAL:
  1838. if (errno == ENOENT) {
  1839. break;
  1840. }
  1841. /* else fall through */
  1842. default:
  1843. return i;
  1844. }
  1845. __set_errno(old_errno);
  1846. }
  1847. DPRINTF("Nothing found in /etc/hosts\n");
  1848. *h_errnop = NETDB_INTERNAL;
  1849. /* prepare future h_aliases[0] */
  1850. i = strlen(name) + 1;
  1851. if ((ssize_t)buflen <= i)
  1852. return ERANGE;
  1853. memcpy(buf, name, i); /* paranoia: name might change */
  1854. alias0 = buf;
  1855. buf += i;
  1856. buflen -= i;
  1857. /* make sure pointer is aligned */
  1858. i = ALIGN_BUFFER_OFFSET(buf);
  1859. buf += i;
  1860. buflen -= i;
  1861. /* Layout in buf:
  1862. * char *alias[2];
  1863. * struct in_addr* addr_list[NN+1];
  1864. * struct in_addr* in[NN];
  1865. */
  1866. alias = (char **)buf;
  1867. buf += sizeof(alias[0]) * 2;
  1868. buflen -= sizeof(alias[0]) * 2;
  1869. addr_list = (struct in_addr **)buf;
  1870. /* buflen may be < 0, must do signed compare */
  1871. if ((ssize_t)buflen < 256)
  1872. return ERANGE;
  1873. /* we store only one "alias" - the name itself */
  1874. #ifdef __UCLIBC_MJN3_ONLY__
  1875. #warning TODO -- generate the full list
  1876. #endif
  1877. alias[0] = alias0;
  1878. alias[1] = NULL;
  1879. /* maybe it is already an address? */
  1880. {
  1881. struct in_addr *in = (struct in_addr *)(buf + sizeof(addr_list[0]) * 2);
  1882. if (inet_aton(name, in)) {
  1883. addr_list[0] = in;
  1884. addr_list[1] = NULL;
  1885. result_buf->h_name = alias0;
  1886. result_buf->h_aliases = alias;
  1887. result_buf->h_addrtype = AF_INET;
  1888. result_buf->h_length = sizeof(struct in_addr);
  1889. result_buf->h_addr_list = (char **) addr_list;
  1890. *result = result_buf;
  1891. *h_errnop = NETDB_SUCCESS;
  1892. return NETDB_SUCCESS;
  1893. }
  1894. }
  1895. /* what if /etc/hosts has it but it's not IPv4?
  1896. * F.e. "::1 localhost6". We don't do DNS query for such hosts -
  1897. * "ping localhost6" should be fast even if DNS server is down! */
  1898. if (wrong_af) {
  1899. *h_errnop = HOST_NOT_FOUND;
  1900. return TRY_AGAIN;
  1901. }
  1902. /* talk to DNS servers */
  1903. a.buf = buf;
  1904. /* take into account that at least one address will be there,
  1905. * we'll need space for one in_addr + two addr_list[] elems */
  1906. a.buflen = buflen - ((sizeof(addr_list[0]) * 2 + sizeof(struct in_addr)));
  1907. a.add_count = 0;
  1908. packet_len = __dns_lookup(name, T_A, &packet, &a);
  1909. if (packet_len < 0) {
  1910. *h_errnop = HOST_NOT_FOUND;
  1911. DPRINTF("__dns_lookup returned < 0\n");
  1912. return TRY_AGAIN;
  1913. }
  1914. if (a.atype == T_A) { /* ADDRESS */
  1915. /* we need space for addr_list[] and one IPv4 address */
  1916. /* + 1 accounting for 1st addr (it's in a.rdata),
  1917. * another + 1 for NULL in last addr_list[]: */
  1918. int need_bytes = sizeof(addr_list[0]) * (a.add_count + 1 + 1)
  1919. /* for 1st addr (it's in a.rdata): */
  1920. + sizeof(struct in_addr);
  1921. /* how many bytes will 2nd and following addresses take? */
  1922. int ips_len = a.add_count * a.rdlength;
  1923. buflen -= (need_bytes + ips_len);
  1924. if ((ssize_t)buflen < 0) {
  1925. DPRINTF("buffer too small for all addresses\n");
  1926. /* *h_errnop = NETDB_INTERNAL; - already is */
  1927. i = ERANGE;
  1928. goto free_and_ret;
  1929. }
  1930. /* if there are additional addresses in buf,
  1931. * move them forward so that they are not destroyed */
  1932. DPRINTF("a.add_count:%d a.rdlength:%d a.rdata:%p\n", a.add_count, a.rdlength, a.rdata);
  1933. memmove(buf + need_bytes, buf, ips_len);
  1934. /* 1st address is in a.rdata, insert it */
  1935. buf += need_bytes - sizeof(struct in_addr);
  1936. memcpy(buf, a.rdata, sizeof(struct in_addr));
  1937. /* fill addr_list[] */
  1938. for (i = 0; i <= a.add_count; i++) {
  1939. addr_list[i] = (struct in_addr*)buf;
  1940. buf += sizeof(struct in_addr);
  1941. }
  1942. addr_list[i] = NULL;
  1943. /* if we have enough space, we can report "better" name
  1944. * (it may contain search domains attached by __dns_lookup,
  1945. * or CNAME of the host if it is different from the name
  1946. * we used to find it) */
  1947. if (a.dotted && buflen > strlen(a.dotted)) {
  1948. strcpy(buf, a.dotted);
  1949. alias0 = buf;
  1950. }
  1951. result_buf->h_name = alias0;
  1952. result_buf->h_aliases = alias;
  1953. result_buf->h_addrtype = AF_INET;
  1954. result_buf->h_length = sizeof(struct in_addr);
  1955. result_buf->h_addr_list = (char **) addr_list;
  1956. *result = result_buf;
  1957. *h_errnop = NETDB_SUCCESS;
  1958. i = NETDB_SUCCESS;
  1959. goto free_and_ret;
  1960. }
  1961. *h_errnop = HOST_NOT_FOUND;
  1962. __set_h_errno(HOST_NOT_FOUND);
  1963. i = TRY_AGAIN;
  1964. free_and_ret:
  1965. free(a.dotted);
  1966. free(packet);
  1967. return i;
  1968. }
  1969. libc_hidden_def(gethostbyname_r)
  1970. link_warning(gethostbyname_r, "gethostbyname_r is obsolescent, use getnameinfo() instead.");
  1971. #endif /* L_gethostbyname_r */
  1972. #ifdef L_gethostbyname2_r
  1973. int gethostbyname2_r(const char *name,
  1974. int family,
  1975. struct hostent *result_buf,
  1976. char *buf,
  1977. size_t buflen,
  1978. struct hostent **result,
  1979. int *h_errnop)
  1980. {
  1981. #ifndef __UCLIBC_HAS_IPV6__
  1982. return family == (AF_INET)
  1983. ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop)
  1984. : HOST_NOT_FOUND;
  1985. #else
  1986. struct in6_addr *in;
  1987. struct in6_addr **addr_list;
  1988. unsigned char *packet;
  1989. struct resolv_answer a;
  1990. int i;
  1991. int nest = 0;
  1992. int wrong_af = 0;
  1993. if (family == AF_INET)
  1994. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1995. *result = NULL;
  1996. if (family != AF_INET6)
  1997. return EINVAL;
  1998. if (!name)
  1999. return EINVAL;
  2000. /* do /etc/hosts first */
  2001. {
  2002. int old_errno = errno; /* Save the old errno and reset errno */
  2003. __set_errno(0); /* to check for missing /etc/hosts. */
  2004. i = __get_hosts_byname_r(name, AF_INET6 /*family*/, result_buf,
  2005. buf, buflen, result, h_errnop);
  2006. if (i == NETDB_SUCCESS) {
  2007. __set_errno(old_errno);
  2008. return i;
  2009. }
  2010. switch (*h_errnop) {
  2011. case HOST_NOT_FOUND:
  2012. wrong_af = (i == TRY_AGAIN);
  2013. case NO_ADDRESS:
  2014. break;
  2015. case NETDB_INTERNAL:
  2016. if (errno == ENOENT) {
  2017. break;
  2018. }
  2019. /* else fall through */
  2020. default:
  2021. return i;
  2022. }
  2023. __set_errno(old_errno);
  2024. }
  2025. DPRINTF("Nothing found in /etc/hosts\n");
  2026. *h_errnop = NETDB_INTERNAL;
  2027. /* make sure pointer is aligned */
  2028. i = ALIGN_BUFFER_OFFSET(buf);
  2029. buf += i;
  2030. buflen -= i;
  2031. /* Layout in buf:
  2032. * struct in6_addr* in;
  2033. * struct in6_addr* addr_list[2];
  2034. * char scratch_buf[256];
  2035. */
  2036. in = (struct in6_addr*)buf;
  2037. buf += sizeof(*in);
  2038. buflen -= sizeof(*in);
  2039. addr_list = (struct in6_addr**)buf;
  2040. buf += sizeof(*addr_list) * 2;
  2041. buflen -= sizeof(*addr_list) * 2;
  2042. if ((ssize_t)buflen < 256)
  2043. return ERANGE;
  2044. addr_list[0] = in;
  2045. addr_list[1] = NULL;
  2046. strncpy(buf, name, buflen);
  2047. buf[buflen] = '\0';
  2048. /* maybe it is already an address? */
  2049. if (inet_pton(AF_INET6, name, in)) {
  2050. result_buf->h_name = buf;
  2051. result_buf->h_addrtype = AF_INET6;
  2052. result_buf->h_length = sizeof(*in);
  2053. result_buf->h_addr_list = (char **) addr_list;
  2054. /* result_buf->h_aliases = ??? */
  2055. *result = result_buf;
  2056. *h_errnop = NETDB_SUCCESS;
  2057. return NETDB_SUCCESS;
  2058. }
  2059. /* what if /etc/hosts has it but it's not IPv6?
  2060. * F.e. "127.0.0.1 localhost". We don't do DNS query for such hosts -
  2061. * "ping localhost" should be fast even if DNS server is down! */
  2062. if (wrong_af) {
  2063. *h_errnop = HOST_NOT_FOUND;
  2064. return TRY_AGAIN;
  2065. }
  2066. /* talk to DNS servers */
  2067. /* TODO: why it's so different from gethostbyname_r (IPv4 case)? */
  2068. memset(&a, '\0', sizeof(a));
  2069. for (;;) {
  2070. int packet_len;
  2071. /* Hmm why we memset(a) to zeros only once? */
  2072. packet_len = __dns_lookup(buf, T_AAAA, &packet, &a);
  2073. if (packet_len < 0) {
  2074. *h_errnop = HOST_NOT_FOUND;
  2075. return TRY_AGAIN;
  2076. }
  2077. strncpy(buf, a.dotted, buflen);
  2078. free(a.dotted);
  2079. if (a.atype != T_CNAME)
  2080. break;
  2081. DPRINTF("Got a CNAME in gethostbyname()\n");
  2082. if (++nest > MAX_RECURSE) {
  2083. *h_errnop = NO_RECOVERY;
  2084. return -1;
  2085. }
  2086. i = __decode_dotted(packet, a.rdoffset, packet_len, buf, buflen);
  2087. free(packet);
  2088. if (i < 0) {
  2089. *h_errnop = NO_RECOVERY;
  2090. return -1;
  2091. }
  2092. }
  2093. if (a.atype == T_AAAA) { /* ADDRESS */
  2094. memcpy(in, a.rdata, sizeof(*in));
  2095. result_buf->h_name = buf;
  2096. result_buf->h_addrtype = AF_INET6;
  2097. result_buf->h_length = sizeof(*in);
  2098. result_buf->h_addr_list = (char **) addr_list;
  2099. /* result_buf->h_aliases = ??? */
  2100. free(packet);
  2101. *result = result_buf;
  2102. *h_errnop = NETDB_SUCCESS;
  2103. return NETDB_SUCCESS;
  2104. }
  2105. free(packet);
  2106. *h_errnop = HOST_NOT_FOUND;
  2107. return TRY_AGAIN;
  2108. #endif /* __UCLIBC_HAS_IPV6__ */
  2109. }
  2110. libc_hidden_def(gethostbyname2_r)
  2111. #endif /* L_gethostbyname2_r */
  2112. #ifdef L_gethostbyaddr_r
  2113. int gethostbyaddr_r(const void *addr, socklen_t addrlen,
  2114. int type,
  2115. struct hostent *result_buf,
  2116. char *buf, size_t buflen,
  2117. struct hostent **result,
  2118. int *h_errnop)
  2119. {
  2120. struct in_addr *in;
  2121. struct in_addr **addr_list;
  2122. char **alias;
  2123. unsigned char *packet;
  2124. struct resolv_answer a;
  2125. int i;
  2126. int packet_len;
  2127. int nest = 0;
  2128. *result = NULL;
  2129. if (!addr)
  2130. return EINVAL;
  2131. switch (type) {
  2132. #ifdef __UCLIBC_HAS_IPV4__
  2133. case AF_INET:
  2134. if (addrlen != sizeof(struct in_addr))
  2135. return EINVAL;
  2136. break;
  2137. #endif
  2138. #ifdef __UCLIBC_HAS_IPV6__
  2139. case AF_INET6:
  2140. if (addrlen != sizeof(struct in6_addr))
  2141. return EINVAL;
  2142. break;
  2143. #endif
  2144. default:
  2145. return EINVAL;
  2146. }
  2147. /* do /etc/hosts first */
  2148. i = __get_hosts_byaddr_r(addr, addrlen, type, result_buf,
  2149. buf, buflen, result, h_errnop);
  2150. if (i == 0)
  2151. return i;
  2152. switch (*h_errnop) {
  2153. case HOST_NOT_FOUND:
  2154. case NO_ADDRESS:
  2155. break;
  2156. default:
  2157. return i;
  2158. }
  2159. *h_errnop = NETDB_INTERNAL;
  2160. /* make sure pointer is aligned */
  2161. i = ALIGN_BUFFER_OFFSET(buf);
  2162. buf += i;
  2163. buflen -= i;
  2164. /* Layout in buf:
  2165. * char *alias[ALIAS_DIM];
  2166. * struct in[6]_addr* addr_list[2];
  2167. * struct in[6]_addr in;
  2168. * char scratch_buffer[256+];
  2169. */
  2170. #define in6 ((struct in6_addr *)in)
  2171. alias = (char **)buf;
  2172. addr_list = (struct in_addr**)buf;
  2173. buf += sizeof(*addr_list) * 2;
  2174. buflen -= sizeof(*addr_list) * 2;
  2175. in = (struct in_addr*)buf;
  2176. #ifndef __UCLIBC_HAS_IPV6__
  2177. buf += sizeof(*in);
  2178. buflen -= sizeof(*in);
  2179. if (addrlen > sizeof(*in))
  2180. return ERANGE;
  2181. #else
  2182. buf += sizeof(*in6);
  2183. buflen -= sizeof(*in6);
  2184. if (addrlen > sizeof(*in6))
  2185. return ERANGE;
  2186. #endif
  2187. if ((ssize_t)buflen < 256)
  2188. return ERANGE;
  2189. alias[0] = buf;
  2190. alias[1] = NULL;
  2191. addr_list[0] = in;
  2192. addr_list[1] = NULL;
  2193. memcpy(in, addr, addrlen);
  2194. if (0) /* nothing */;
  2195. #ifdef __UCLIBC_HAS_IPV4__
  2196. else IF_HAS_BOTH(if (type == AF_INET)) {
  2197. unsigned char *tp = (unsigned char *)addr;
  2198. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  2199. tp[3], tp[2], tp[1], tp[0]);
  2200. }
  2201. #endif
  2202. #ifdef __UCLIBC_HAS_IPV6__
  2203. else {
  2204. char *dst = buf;
  2205. unsigned char *tp = (unsigned char *)addr + addrlen - 1;
  2206. do {
  2207. dst += sprintf(dst, "%x.%x.", tp[0] & 0xf, tp[0] >> 4);
  2208. tp--;
  2209. } while (tp >= (unsigned char *)addr);
  2210. strcpy(dst, "ip6.arpa");
  2211. }
  2212. #endif
  2213. memset(&a, '\0', sizeof(a));
  2214. for (;;) {
  2215. /* Hmm why we memset(a) to zeros only once? */
  2216. packet_len = __dns_lookup(buf, T_PTR, &packet, &a);
  2217. if (packet_len < 0) {
  2218. *h_errnop = HOST_NOT_FOUND;
  2219. return TRY_AGAIN;
  2220. }
  2221. strncpy(buf, a.dotted, buflen);
  2222. free(a.dotted);
  2223. if (a.atype != T_CNAME)
  2224. break;
  2225. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  2226. if (++nest > MAX_RECURSE) {
  2227. *h_errnop = NO_RECOVERY;
  2228. return -1;
  2229. }
  2230. /* Decode CNAME into buf, feed it to __dns_lookup() again */
  2231. i = __decode_dotted(packet, a.rdoffset, packet_len, buf, buflen);
  2232. free(packet);
  2233. if (i < 0) {
  2234. *h_errnop = NO_RECOVERY;
  2235. return -1;
  2236. }
  2237. }
  2238. if (a.atype == T_PTR) { /* ADDRESS */
  2239. i = __decode_dotted(packet, a.rdoffset, packet_len, buf, buflen);
  2240. free(packet);
  2241. result_buf->h_name = buf;
  2242. result_buf->h_addrtype = type;
  2243. result_buf->h_length = addrlen;
  2244. result_buf->h_addr_list = (char **) addr_list;
  2245. result_buf->h_aliases = alias;
  2246. *result = result_buf;
  2247. *h_errnop = NETDB_SUCCESS;
  2248. return NETDB_SUCCESS;
  2249. }
  2250. free(packet);
  2251. *h_errnop = NO_ADDRESS;
  2252. return TRY_AGAIN;
  2253. #undef in6
  2254. }
  2255. libc_hidden_def(gethostbyaddr_r)
  2256. link_warning(gethostbyaddr_r, "gethostbyaddr_r is obsolescent, use getaddrinfo() instead.");
  2257. #endif /* L_gethostbyaddr_r */
  2258. #ifdef L_gethostent_r
  2259. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  2260. static parser_t *hostp = NULL;
  2261. static smallint host_stayopen;
  2262. void endhostent_unlocked(void)
  2263. {
  2264. if (hostp) {
  2265. config_close(hostp);
  2266. hostp = NULL;
  2267. }
  2268. host_stayopen = 0;
  2269. }
  2270. void endhostent(void)
  2271. {
  2272. __UCLIBC_MUTEX_LOCK(mylock);
  2273. endhostent_unlocked();
  2274. __UCLIBC_MUTEX_UNLOCK(mylock);
  2275. }
  2276. void sethostent(int stay_open)
  2277. {
  2278. __UCLIBC_MUTEX_LOCK(mylock);
  2279. if (stay_open)
  2280. host_stayopen = 1;
  2281. __UCLIBC_MUTEX_UNLOCK(mylock);
  2282. }
  2283. int gethostent_r(struct hostent *result_buf, char *buf, size_t buflen,
  2284. struct hostent **result, int *h_errnop)
  2285. {
  2286. int ret;
  2287. __UCLIBC_MUTEX_LOCK(mylock);
  2288. if (hostp == NULL) {
  2289. hostp = __open_etc_hosts();
  2290. if (hostp == NULL) {
  2291. *result = NULL;
  2292. ret = TRY_AGAIN;
  2293. goto DONE;
  2294. }
  2295. }
  2296. ret = __read_etc_hosts_r(hostp, NULL, AF_INET, GETHOSTENT,
  2297. result_buf, buf, buflen, result, h_errnop);
  2298. if (!host_stayopen)
  2299. endhostent_unlocked();
  2300. DONE:
  2301. __UCLIBC_MUTEX_UNLOCK(mylock);
  2302. return ret;
  2303. }
  2304. libc_hidden_def(gethostent_r)
  2305. #endif /* L_gethostent_r */
  2306. #ifdef L_gethostent
  2307. struct hostent *gethostent(void)
  2308. {
  2309. static struct hostent hoste;
  2310. static char buf[
  2311. #ifndef __UCLIBC_HAS_IPV6__
  2312. sizeof(struct in_addr) + sizeof(struct in_addr *) * 2 +
  2313. #else
  2314. sizeof(struct in6_addr) + sizeof(struct in6_addr *) * 2 +
  2315. #endif /* __UCLIBC_HAS_IPV6__ */
  2316. BUFSZ /*namebuffer*/ + 2 /* margin */];
  2317. struct hostent *host;
  2318. gethostent_r(&hoste, buf, sizeof(buf), &host, &h_errno);
  2319. return host;
  2320. }
  2321. #endif /* L_gethostent */
  2322. #ifdef L_gethostbyname2
  2323. struct hostent *gethostbyname2(const char *name, int family)
  2324. {
  2325. #ifndef __UCLIBC_HAS_IPV6__
  2326. return family == AF_INET ? gethostbyname(name) : (struct hostent*)NULL;
  2327. #else
  2328. static struct hostent hoste;
  2329. static char buf[sizeof(struct in6_addr) +
  2330. sizeof(struct in6_addr *) * 2 +
  2331. /*sizeof(char *)*ALIAS_DIM +*/ 384/*namebuffer*/ + 32/* margin */];
  2332. struct hostent *hp;
  2333. gethostbyname2_r(name, family, &hoste, buf, sizeof(buf), &hp, &h_errno);
  2334. return hp;
  2335. #endif
  2336. }
  2337. libc_hidden_def(gethostbyname2)
  2338. #endif /* L_gethostbyname2 */
  2339. #ifdef L_gethostbyname
  2340. struct hostent *gethostbyname(const char *name)
  2341. {
  2342. #ifndef __UCLIBC_HAS_IPV6__
  2343. static struct hostent hoste;
  2344. static char buf[sizeof(struct in_addr) +
  2345. sizeof(struct in_addr *) * 2 +
  2346. /*sizeof(char *)*ALIAS_DIM +*/ 384/*namebuffer*/ + 32/* margin */];
  2347. struct hostent *hp;
  2348. gethostbyname_r(name, &hoste, buf, sizeof(buf), &hp, &h_errno);
  2349. return hp;
  2350. #else
  2351. return gethostbyname2(name, AF_INET);
  2352. #endif
  2353. }
  2354. libc_hidden_def(gethostbyname)
  2355. link_warning(gethostbyname, "gethostbyname is obsolescent, use getnameinfo() instead.");
  2356. #endif /* L_gethostbyname */
  2357. #ifdef L_gethostbyaddr
  2358. struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type)
  2359. {
  2360. static struct hostent hoste;
  2361. static char buf[
  2362. #ifndef __UCLIBC_HAS_IPV6__
  2363. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  2364. #else
  2365. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  2366. #endif /* __UCLIBC_HAS_IPV6__ */
  2367. /*sizeof(char *)*ALIAS_DIM +*/ 384 /*namebuffer*/ + 32 /* margin */];
  2368. struct hostent *hp;
  2369. gethostbyaddr_r(addr, len, type, &hoste, buf, sizeof(buf), &hp, &h_errno);
  2370. return hp;
  2371. }
  2372. libc_hidden_def(gethostbyaddr)
  2373. link_warning(gethostbyaddr, "gethostbyaddr is obsolescent, use getaddrinfo() instead.");
  2374. #endif /* L_gethostbyaddr */
  2375. #ifdef L_res_comp
  2376. /*
  2377. * Expand compressed domain name 'comp_dn' to full domain name.
  2378. * 'msg' is a pointer to the begining of the message,
  2379. * 'eomorig' points to the first location after the message,
  2380. * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  2381. * Return size of compressed name or -1 if there was an error.
  2382. */
  2383. int dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  2384. char *dst, int dstsiz)
  2385. {
  2386. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  2387. if (n > 0 && dst[0] == '.')
  2388. dst[0] = '\0';
  2389. return n;
  2390. }
  2391. libc_hidden_def(dn_expand)
  2392. /*
  2393. * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
  2394. * Return the size of the compressed name or -1.
  2395. * 'length' is the size of the array pointed to by 'comp_dn'.
  2396. */
  2397. int
  2398. dn_comp(const char *src, u_char *dst, int dstsiz,
  2399. u_char **dnptrs, u_char **lastdnptr)
  2400. {
  2401. return ns_name_compress(src, dst, (size_t) dstsiz,
  2402. (const u_char **) dnptrs,
  2403. (const u_char **) lastdnptr);
  2404. }
  2405. libc_hidden_def(dn_comp)
  2406. #endif /* L_res_comp */
  2407. #ifdef L_ns_name
  2408. /* Thinking in noninternationalized USASCII (per the DNS spec),
  2409. * is this character visible and not a space when printed ?
  2410. */
  2411. static int printable(int ch)
  2412. {
  2413. return (ch > 0x20 && ch < 0x7f);
  2414. }
  2415. /* Thinking in noninternationalized USASCII (per the DNS spec),
  2416. * is this characted special ("in need of quoting") ?
  2417. */
  2418. static int special(int ch)
  2419. {
  2420. switch (ch) {
  2421. case 0x22: /* '"' */
  2422. case 0x2E: /* '.' */
  2423. case 0x3B: /* ';' */
  2424. case 0x5C: /* '\\' */
  2425. /* Special modifiers in zone files. */
  2426. case 0x40: /* '@' */
  2427. case 0x24: /* '$' */
  2428. return 1;
  2429. default:
  2430. return 0;
  2431. }
  2432. }
  2433. /*
  2434. * ns_name_uncompress(msg, eom, src, dst, dstsiz)
  2435. * Expand compressed domain name to presentation format.
  2436. * return:
  2437. * Number of bytes read out of `src', or -1 (with errno set).
  2438. * note:
  2439. * Root domain returns as "." not "".
  2440. */
  2441. int ns_name_uncompress(const u_char *msg, const u_char *eom,
  2442. const u_char *src, char *dst, size_t dstsiz)
  2443. {
  2444. u_char tmp[NS_MAXCDNAME];
  2445. int n;
  2446. n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp);
  2447. if (n == -1)
  2448. return -1;
  2449. if (ns_name_ntop(tmp, dst, dstsiz) == -1)
  2450. return -1;
  2451. return n;
  2452. }
  2453. libc_hidden_def(ns_name_uncompress)
  2454. /*
  2455. * ns_name_ntop(src, dst, dstsiz)
  2456. * Convert an encoded domain name to printable ascii as per RFC1035.
  2457. * return:
  2458. * Number of bytes written to buffer, or -1 (with errno set)
  2459. * notes:
  2460. * The root is returned as "."
  2461. * All other domains are returned in non absolute form
  2462. */
  2463. int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
  2464. {
  2465. const u_char *cp;
  2466. char *dn, *eom;
  2467. u_char c;
  2468. u_int n;
  2469. cp = src;
  2470. dn = dst;
  2471. eom = dst + dstsiz;
  2472. while ((n = *cp++) != 0) {
  2473. if ((n & NS_CMPRSFLGS) != 0) {
  2474. /* Some kind of compression pointer. */
  2475. __set_errno(EMSGSIZE);
  2476. return -1;
  2477. }
  2478. if (dn != dst) {
  2479. if (dn >= eom) {
  2480. __set_errno(EMSGSIZE);
  2481. return -1;
  2482. }
  2483. *dn++ = '.';
  2484. }
  2485. if (dn + n >= eom) {
  2486. __set_errno(EMSGSIZE);
  2487. return -1;
  2488. }
  2489. for (; n > 0; n--) {
  2490. c = *cp++;
  2491. if (special(c)) {
  2492. if (dn + 1 >= eom) {
  2493. __set_errno(EMSGSIZE);
  2494. return -1;
  2495. }
  2496. *dn++ = '\\';
  2497. *dn++ = (char)c;
  2498. } else if (!printable(c)) {
  2499. if (dn + 3 >= eom) {
  2500. __set_errno(EMSGSIZE);
  2501. return -1;
  2502. }
  2503. *dn++ = '\\';
  2504. *dn++ = "0123456789"[c / 100];
  2505. c = c % 100;
  2506. *dn++ = "0123456789"[c / 10];
  2507. *dn++ = "0123456789"[c % 10];
  2508. } else {
  2509. if (dn >= eom) {
  2510. __set_errno(EMSGSIZE);
  2511. return -1;
  2512. }
  2513. *dn++ = (char)c;
  2514. }
  2515. }
  2516. }
  2517. if (dn == dst) {
  2518. if (dn >= eom) {
  2519. __set_errno(EMSGSIZE);
  2520. return -1;
  2521. }
  2522. *dn++ = '.';
  2523. }
  2524. if (dn >= eom) {
  2525. __set_errno(EMSGSIZE);
  2526. return -1;
  2527. }
  2528. *dn++ = '\0';
  2529. return (dn - dst);
  2530. }
  2531. libc_hidden_def(ns_name_ntop)
  2532. static int encode_bitstring(const char **bp, const char *end,
  2533. unsigned char **labelp,
  2534. unsigned char ** dst,
  2535. unsigned const char *eom)
  2536. {
  2537. int afterslash = 0;
  2538. const char *cp = *bp;
  2539. unsigned char *tp;
  2540. const char *beg_blen;
  2541. int value = 0, count = 0, tbcount = 0, blen = 0;
  2542. beg_blen = NULL;
  2543. /* a bitstring must contain at least 2 characters */
  2544. if (end - cp < 2)
  2545. return EINVAL;
  2546. /* XXX: currently, only hex strings are supported */
  2547. if (*cp++ != 'x')
  2548. return EINVAL;
  2549. if (!isxdigit((unsigned char) *cp)) /*%< reject '\[x/BLEN]' */
  2550. return EINVAL;
  2551. for (tp = *dst + 1; cp < end && tp < eom; cp++) {
  2552. unsigned char c = *cp;
  2553. switch (c) {
  2554. case ']': /*%< end of the bitstring */
  2555. if (afterslash) {
  2556. char *end_blen;
  2557. if (beg_blen == NULL)
  2558. return EINVAL;
  2559. blen = (int)strtol(beg_blen, &end_blen, 10);
  2560. if (*end_blen != ']')
  2561. return EINVAL;
  2562. }
  2563. if (count)
  2564. *tp++ = ((value << 4) & 0xff);
  2565. cp++; /*%< skip ']' */
  2566. goto done;
  2567. case '/':
  2568. afterslash = 1;
  2569. break;
  2570. default:
  2571. if (afterslash) {
  2572. if (!__isdigit_char(c))
  2573. return EINVAL;
  2574. if (beg_blen == NULL) {
  2575. if (c == '0') {
  2576. /* blen never begings with 0 */
  2577. return EINVAL;
  2578. }
  2579. beg_blen = cp;
  2580. }
  2581. } else {
  2582. if (!__isdigit_char(c)) {
  2583. c = c | 0x20; /* lowercase */
  2584. c = c - 'a';
  2585. if (c > 5) /* not a-f? */
  2586. return EINVAL;
  2587. c += 10 + '0';
  2588. }
  2589. value <<= 4;
  2590. value += (c - '0');
  2591. count += 4;
  2592. tbcount += 4;
  2593. if (tbcount > 256)
  2594. return EINVAL;
  2595. if (count == 8) {
  2596. *tp++ = value;
  2597. count = 0;
  2598. }
  2599. }
  2600. break;
  2601. }
  2602. }
  2603. done:
  2604. if (cp >= end || tp >= eom)
  2605. return EMSGSIZE;
  2606. /*
  2607. * bit length validation:
  2608. * If a <length> is present, the number of digits in the <bit-data>
  2609. * MUST be just sufficient to contain the number of bits specified
  2610. * by the <length>. If there are insignificant bits in a final
  2611. * hexadecimal or octal digit, they MUST be zero.
  2612. * RFC2673, Section 3.2.
  2613. */
  2614. if (blen > 0) {
  2615. int traillen;
  2616. if (((blen + 3) & ~3) != tbcount)
  2617. return EINVAL;
  2618. traillen = tbcount - blen; /*%< between 0 and 3 */
  2619. if (((value << (8 - traillen)) & 0xff) != 0)
  2620. return EINVAL;
  2621. }
  2622. else
  2623. blen = tbcount;
  2624. if (blen == 256)
  2625. blen = 0;
  2626. /* encode the type and the significant bit fields */
  2627. **labelp = DNS_LABELTYPE_BITSTRING;
  2628. **dst = blen;
  2629. *bp = cp;
  2630. *dst = tp;
  2631. return 0;
  2632. }
  2633. int ns_name_pton(const char *src, u_char *dst, size_t dstsiz)
  2634. {
  2635. static const char digits[] = "0123456789";
  2636. u_char *label, *bp, *eom;
  2637. int c, n, escaped, e = 0;
  2638. char *cp;
  2639. escaped = 0;
  2640. bp = dst;
  2641. eom = dst + dstsiz;
  2642. label = bp++;
  2643. while ((c = *src++) != 0) {
  2644. if (escaped) {
  2645. if (c == '[') { /*%< start a bit string label */
  2646. cp = strchr(src, ']');
  2647. if (cp == NULL) {
  2648. errno = EINVAL; /*%< ??? */
  2649. return -1;
  2650. }
  2651. e = encode_bitstring(&src, cp + 2,
  2652. &label, &bp, eom);
  2653. if (e != 0) {
  2654. errno = e;
  2655. return -1;
  2656. }
  2657. escaped = 0;
  2658. label = bp++;
  2659. c = *src++;
  2660. if (c == '\0')
  2661. goto done;
  2662. if (c != '.') {
  2663. errno = EINVAL;
  2664. return -1;
  2665. }
  2666. continue;
  2667. }
  2668. cp = strchr(digits, c);
  2669. if (cp != NULL) {
  2670. n = (cp - digits) * 100;
  2671. c = *src++;
  2672. if (c == '\0')
  2673. goto ret_EMSGSIZE;
  2674. cp = strchr(digits, c);
  2675. if (cp == NULL)
  2676. goto ret_EMSGSIZE;
  2677. n += (cp - digits) * 10;
  2678. c = *src++;
  2679. if (c == '\0')
  2680. goto ret_EMSGSIZE;
  2681. cp = strchr(digits, c);
  2682. if (cp == NULL)
  2683. goto ret_EMSGSIZE;
  2684. n += (cp - digits);
  2685. if (n > 255)
  2686. goto ret_EMSGSIZE;
  2687. c = n;
  2688. }
  2689. escaped = 0;
  2690. } else if (c == '\\') {
  2691. escaped = 1;
  2692. continue;
  2693. } else if (c == '.') {
  2694. c = (bp - label - 1);
  2695. if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */
  2696. goto ret_EMSGSIZE;
  2697. }
  2698. if (label >= eom) {
  2699. goto ret_EMSGSIZE;
  2700. }
  2701. *label = c;
  2702. /* Fully qualified ? */
  2703. if (*src == '\0') {
  2704. if (c != 0) {
  2705. if (bp >= eom) {
  2706. goto ret_EMSGSIZE;
  2707. }
  2708. *bp++ = '\0';
  2709. }
  2710. if ((bp - dst) > MAXCDNAME) {
  2711. goto ret_EMSGSIZE;
  2712. }
  2713. return 1;
  2714. }
  2715. if (c == 0 || *src == '.') {
  2716. goto ret_EMSGSIZE;
  2717. }
  2718. label = bp++;
  2719. continue;
  2720. }
  2721. if (bp >= eom) {
  2722. goto ret_EMSGSIZE;
  2723. }
  2724. *bp++ = (u_char)c;
  2725. }
  2726. c = (bp - label - 1);
  2727. if ((c & NS_CMPRSFLGS) != 0) { /*%< Label too big. */
  2728. goto ret_EMSGSIZE;
  2729. }
  2730. done:
  2731. if (label >= eom) {
  2732. goto ret_EMSGSIZE;
  2733. }
  2734. *label = c;
  2735. if (c != 0) {
  2736. if (bp >= eom) {
  2737. goto ret_EMSGSIZE;
  2738. }
  2739. *bp++ = 0;
  2740. }
  2741. if ((bp - dst) > MAXCDNAME) { /*%< src too big */
  2742. goto ret_EMSGSIZE;
  2743. }
  2744. return 0;
  2745. ret_EMSGSIZE:
  2746. errno = EMSGSIZE;
  2747. return -1;
  2748. }
  2749. libc_hidden_def(ns_name_pton)
  2750. /*
  2751. * ns_name_unpack(msg, eom, src, dst, dstsiz)
  2752. * Unpack a domain name from a message, source may be compressed.
  2753. * return:
  2754. * -1 if it fails, or consumed octets if it succeeds.
  2755. */
  2756. int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
  2757. u_char *dst, size_t dstsiz)
  2758. {
  2759. const u_char *srcp, *dstlim;
  2760. u_char *dstp;
  2761. int n, len, checked;
  2762. len = -1;
  2763. checked = 0;
  2764. dstp = dst;
  2765. srcp = src;
  2766. dstlim = dst + dstsiz;
  2767. if (srcp < msg || srcp >= eom) {
  2768. __set_errno(EMSGSIZE);
  2769. return -1;
  2770. }
  2771. /* Fetch next label in domain name. */
  2772. while ((n = *srcp++) != 0) {
  2773. /* Check for indirection. */
  2774. switch (n & NS_CMPRSFLGS) {
  2775. case 0:
  2776. /* Limit checks. */
  2777. if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  2778. __set_errno(EMSGSIZE);
  2779. return -1;
  2780. }
  2781. checked += n + 1;
  2782. *dstp++ = n;
  2783. memcpy(dstp, srcp, n);
  2784. dstp += n;
  2785. srcp += n;
  2786. break;
  2787. case NS_CMPRSFLGS:
  2788. if (srcp >= eom) {
  2789. __set_errno(EMSGSIZE);
  2790. return -1;
  2791. }
  2792. if (len < 0)
  2793. len = srcp - src + 1;
  2794. srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  2795. if (srcp < msg || srcp >= eom) { /* Out of range. */
  2796. __set_errno(EMSGSIZE);
  2797. return -1;
  2798. }
  2799. checked += 2;
  2800. /*
  2801. * Check for loops in the compressed name;
  2802. * if we've looked at the whole message,
  2803. * there must be a loop.
  2804. */
  2805. if (checked >= eom - msg) {
  2806. __set_errno(EMSGSIZE);
  2807. return -1;
  2808. }
  2809. break;
  2810. default:
  2811. __set_errno(EMSGSIZE);
  2812. return -1; /* flag error */
  2813. }
  2814. }
  2815. *dstp = '\0';
  2816. if (len < 0)
  2817. len = srcp - src;
  2818. return len;
  2819. }
  2820. libc_hidden_def(ns_name_unpack)
  2821. static int labellen(const unsigned char *lp)
  2822. {
  2823. unsigned bitlen;
  2824. unsigned char l = *lp;
  2825. if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
  2826. /* should be avoided by the caller */
  2827. return -1;
  2828. }
  2829. if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
  2830. if (l == DNS_LABELTYPE_BITSTRING) {
  2831. bitlen = lp[1];
  2832. if (bitlen == 0)
  2833. bitlen = 256;
  2834. return ((bitlen + 7 ) / 8 + 1);
  2835. }
  2836. return -1; /*%< unknwon ELT */
  2837. }
  2838. return l;
  2839. }
  2840. static int mklower(int ch)
  2841. {
  2842. if (ch >= 0x41 && ch <= 0x5A)
  2843. return (ch + 0x20);
  2844. return ch;
  2845. }
  2846. static int dn_find(const unsigned char *domain,
  2847. const unsigned char *msg,
  2848. const unsigned char * const *dnptrs,
  2849. const unsigned char * const *lastdnptr)
  2850. {
  2851. const unsigned char *dn, *cp, *sp;
  2852. const unsigned char * const *cpp;
  2853. u_int n;
  2854. for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
  2855. sp = *cpp;
  2856. /*
  2857. * terminate search on:
  2858. * root label
  2859. * compression pointer
  2860. * unusable offset
  2861. */
  2862. while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
  2863. (sp - msg) < 0x4000) {
  2864. dn = domain;
  2865. cp = sp;
  2866. while ((n = *cp++) != 0) {
  2867. /*
  2868. * check for indirection
  2869. */
  2870. switch (n & NS_CMPRSFLGS) {
  2871. case 0: /*%< normal case, n == len */
  2872. n = labellen(cp - 1); /*%< XXX */
  2873. if (n != *dn++)
  2874. goto next;
  2875. for (; n > 0; n--)
  2876. if (mklower(*dn++) !=
  2877. mklower(*cp++))
  2878. goto next;
  2879. /* Is next root for both ? */
  2880. if (*dn == '\0' && *cp == '\0')
  2881. return (sp - msg);
  2882. if (*dn)
  2883. continue;
  2884. goto next;
  2885. case NS_CMPRSFLGS: /*%< indirection */
  2886. cp = msg + (((n & 0x3f) << 8) | *cp);
  2887. break;
  2888. default: /*%< illegal type */
  2889. errno = EMSGSIZE;
  2890. return -1;
  2891. }
  2892. }
  2893. next:
  2894. sp += *sp + 1;
  2895. }
  2896. }
  2897. errno = ENOENT;
  2898. return -1;
  2899. }
  2900. int ns_name_pack(const unsigned char *src,
  2901. unsigned char *dst, int dstsiz,
  2902. const unsigned char **dnptrs,
  2903. const unsigned char **lastdnptr)
  2904. {
  2905. unsigned char *dstp;
  2906. const unsigned char **cpp, **lpp, *eob, *msg;
  2907. const unsigned char *srcp;
  2908. int n, l, first = 1;
  2909. srcp = src;
  2910. dstp = dst;
  2911. eob = dstp + dstsiz;
  2912. lpp = cpp = NULL;
  2913. if (dnptrs != NULL) {
  2914. msg = *dnptrs++;
  2915. if (msg != NULL) {
  2916. for (cpp = dnptrs; *cpp != NULL; cpp++)
  2917. continue;
  2918. lpp = cpp; /*%< end of list to search */
  2919. }
  2920. } else {
  2921. msg = NULL;
  2922. }
  2923. /* make sure the domain we are about to add is legal */
  2924. l = 0;
  2925. do {
  2926. int l0;
  2927. n = *srcp;
  2928. if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
  2929. errno = EMSGSIZE;
  2930. return -1;
  2931. }
  2932. l0 = labellen(srcp);
  2933. if (l0 < 0) {
  2934. errno = EINVAL;
  2935. return -1;
  2936. }
  2937. l += l0 + 1;
  2938. if (l > MAXCDNAME) {
  2939. errno = EMSGSIZE;
  2940. return -1;
  2941. }
  2942. srcp += l0 + 1;
  2943. } while (n != 0);
  2944. /* from here on we need to reset compression pointer array on error */
  2945. srcp = src;
  2946. do {
  2947. /* Look to see if we can use pointers. */
  2948. n = *srcp;
  2949. if (n != 0 && msg != NULL) {
  2950. l = dn_find(srcp, msg, (const unsigned char * const *) dnptrs,
  2951. (const unsigned char * const *) lpp);
  2952. if (l >= 0) {
  2953. if (dstp + 1 >= eob) {
  2954. goto cleanup;
  2955. }
  2956. *dstp++ = ((u_int32_t)l >> 8) | NS_CMPRSFLGS;
  2957. *dstp++ = l % 256;
  2958. return (dstp - dst);
  2959. }
  2960. /* Not found, save it. */
  2961. if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
  2962. (dstp - msg) < 0x4000 && first) {
  2963. *cpp++ = dstp;
  2964. *cpp = NULL;
  2965. first = 0;
  2966. }
  2967. }
  2968. /* copy label to buffer */
  2969. if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
  2970. /* Should not happen. */
  2971. goto cleanup;
  2972. }
  2973. n = labellen(srcp);
  2974. if (dstp + 1 + n >= eob) {
  2975. goto cleanup;
  2976. }
  2977. memcpy(dstp, srcp, (size_t)(n + 1));
  2978. srcp += n + 1;
  2979. dstp += n + 1;
  2980. } while (n != 0);
  2981. if (dstp > eob) {
  2982. cleanup:
  2983. if (msg != NULL)
  2984. *lpp = NULL;
  2985. errno = EMSGSIZE;
  2986. return -1;
  2987. }
  2988. return dstp - dst;
  2989. }
  2990. libc_hidden_def(ns_name_pack)
  2991. int ns_name_compress(const char *src,
  2992. unsigned char *dst, size_t dstsiz,
  2993. const unsigned char **dnptrs,
  2994. const unsigned char **lastdnptr)
  2995. {
  2996. unsigned char tmp[NS_MAXCDNAME];
  2997. if (ns_name_pton(src, tmp, sizeof(tmp)) == -1)
  2998. return -1;
  2999. return ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr);
  3000. }
  3001. libc_hidden_def(ns_name_compress)
  3002. int ns_name_skip(const unsigned char **ptrptr,
  3003. const unsigned char *eom)
  3004. {
  3005. const unsigned char *cp;
  3006. u_int n;
  3007. int l;
  3008. cp = *ptrptr;
  3009. while (cp < eom && (n = *cp++) != 0) {
  3010. /* Check for indirection. */
  3011. switch (n & NS_CMPRSFLGS) {
  3012. case 0: /*%< normal case, n == len */
  3013. cp += n;
  3014. continue;
  3015. case NS_TYPE_ELT: /*%< EDNS0 extended label */
  3016. l = labellen(cp - 1);
  3017. if (l < 0) {
  3018. errno = EMSGSIZE; /*%< XXX */
  3019. return -1;
  3020. }
  3021. cp += l;
  3022. continue;
  3023. case NS_CMPRSFLGS: /*%< indirection */
  3024. cp++;
  3025. break;
  3026. default: /*%< illegal type */
  3027. errno = EMSGSIZE;
  3028. return -1;
  3029. }
  3030. break;
  3031. }
  3032. if (cp > eom) {
  3033. errno = EMSGSIZE;
  3034. return -1;
  3035. }
  3036. *ptrptr = cp;
  3037. return 0;
  3038. }
  3039. libc_hidden_def(ns_name_skip)
  3040. int dn_skipname(const unsigned char *ptr, const unsigned char *eom)
  3041. {
  3042. const unsigned char *saveptr = ptr;
  3043. if (ns_name_skip(&ptr, eom) == -1)
  3044. return -1;
  3045. return ptr - saveptr;
  3046. }
  3047. libc_hidden_def(dn_skipname)
  3048. #endif /* L_ns_name */
  3049. #ifdef L_res_init
  3050. /* Will be called under __resolv_lock. */
  3051. static void res_sync_func(void)
  3052. {
  3053. struct __res_state *rp = &(_res);
  3054. int n;
  3055. /* If we didn't get malloc failure earlier... */
  3056. if (__nameserver != (void*) &__local_nameserver) {
  3057. /* TODO:
  3058. * if (__nameservers < rp->nscount) - try to grow __nameserver[]?
  3059. */
  3060. #ifdef __UCLIBC_HAS_IPV6__
  3061. if (__nameservers > rp->_u._ext.nscount)
  3062. __nameservers = rp->_u._ext.nscount;
  3063. n = __nameservers;
  3064. while (--n >= 0)
  3065. __nameserver[n].sa6 = *rp->_u._ext.nsaddrs[n]; /* struct copy */
  3066. #else /* IPv4 only */
  3067. if (__nameservers > rp->nscount)
  3068. __nameservers = rp->nscount;
  3069. n = __nameservers;
  3070. while (--n >= 0)
  3071. __nameserver[n].sa4 = rp->nsaddr_list[n]; /* struct copy */
  3072. #endif
  3073. }
  3074. __resolv_timeout = rp->retrans ? : RES_TIMEOUT;
  3075. __resolv_attempts = rp->retry ? : RES_DFLRETRY;
  3076. /* Extend and comment what program is known
  3077. * to use which _res.XXX member(s).
  3078. __resolv_opts = rp->options;
  3079. ...
  3080. */
  3081. }
  3082. static int
  3083. __res_vinit(res_state rp, int preinit)
  3084. {
  3085. int i, n, options, retrans, retry, ndots;
  3086. #ifdef __UCLIBC_HAS_IPV6__
  3087. int m = 0;
  3088. #endif
  3089. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  3090. __close_nameservers();
  3091. __open_nameservers();
  3092. if (preinit) {
  3093. options = rp->options;
  3094. retrans = rp->retrans;
  3095. retry = rp->retry;
  3096. ndots = rp->ndots;
  3097. }
  3098. memset(rp, 0, sizeof(*rp));
  3099. if (!preinit) {
  3100. rp->options = RES_DEFAULT;
  3101. rp->retrans = RES_TIMEOUT;
  3102. rp->retry = RES_DFLRETRY;
  3103. rp->ndots = 1;
  3104. } else {
  3105. rp->options = options;
  3106. rp->retrans = retrans;
  3107. rp->retry = retry;
  3108. rp->ndots = ndots;
  3109. }
  3110. #ifdef __UCLIBC_HAS_COMPAT_RES_STATE__
  3111. /* Was: "rp->id = random();" but:
  3112. * - random() pulls in largish static buffers
  3113. * - isn't actually random unless, say, srandom(time(NULL)) was called
  3114. * - is not used by uclibc anyway :)
  3115. */
  3116. /* rp->id = 0; - memset did it */
  3117. #endif
  3118. #ifdef __UCLIBC_HAS_EXTRA_COMPAT_RES_STATE__
  3119. rp->_vcsock = -1;
  3120. #endif
  3121. n = __searchdomains;
  3122. if (n > ARRAY_SIZE(rp->dnsrch))
  3123. n = ARRAY_SIZE(rp->dnsrch);
  3124. for (i = 0; i < n; i++)
  3125. rp->dnsrch[i] = __searchdomain[i];
  3126. /* copy nameservers' addresses */
  3127. i = 0;
  3128. #ifdef __UCLIBC_HAS_IPV4__
  3129. n = 0;
  3130. while (n < ARRAY_SIZE(rp->nsaddr_list) && i < __nameservers) {
  3131. if (__nameserver[i].sa.sa_family == AF_INET) {
  3132. rp->nsaddr_list[n] = __nameserver[i].sa4; /* struct copy */
  3133. #ifdef __UCLIBC_HAS_IPV6__
  3134. if (m < ARRAY_SIZE(rp->_u._ext.nsaddrs)) {
  3135. rp->_u._ext.nsaddrs[m] = (void*) &rp->nsaddr_list[n];
  3136. m++;
  3137. }
  3138. #endif
  3139. n++;
  3140. }
  3141. #ifdef __UCLIBC_HAS_IPV6__
  3142. if (__nameserver[i].sa.sa_family == AF_INET6
  3143. && m < ARRAY_SIZE(rp->_u._ext.nsaddrs)
  3144. ) {
  3145. struct sockaddr_in6 *sa6 = malloc(sizeof(*sa6));
  3146. if (sa6) {
  3147. *sa6 = __nameserver[i].sa6; /* struct copy */
  3148. rp->_u._ext.nsaddrs[m] = sa6;
  3149. m++;
  3150. }
  3151. }
  3152. #endif
  3153. i++;
  3154. }
  3155. rp->nscount = n;
  3156. #ifdef __UCLIBC_HAS_IPV6__
  3157. rp->_u._ext.nscount = m;
  3158. #endif
  3159. #else /* IPv6 only */
  3160. while (m < ARRAY_SIZE(rp->_u._ext.nsaddrs) && i < __nameservers) {
  3161. struct sockaddr_in6 *sa6 = malloc(sizeof(*sa6));
  3162. if (sa6) {
  3163. *sa6 = __nameserver[i].sa6; /* struct copy */
  3164. rp->_u._ext.nsaddrs[m] = sa6;
  3165. m++;
  3166. }
  3167. i++;
  3168. }
  3169. rp->_u._ext.nscount = m;
  3170. #endif
  3171. rp->options |= RES_INIT;
  3172. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  3173. return 0;
  3174. }
  3175. static void attribute_noreturn
  3176. __res_iclose(void)
  3177. {
  3178. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  3179. __close_nameservers();
  3180. __res_sync = NULL;
  3181. #ifdef __UCLIBC_HAS_IPV6__
  3182. {
  3183. char *p1 = (char*) &(_res.nsaddr_list[0]);
  3184. int m = 0;
  3185. /* free nsaddrs[m] if they do not point to nsaddr_list[x] */
  3186. while (m < ARRAY_SIZE(_res._u._ext.nsaddrs)) {
  3187. char *p2 = (char*)(_res._u._ext.nsaddrs[m++]);
  3188. if (p2 < p1 || (p2 - p1) > sizeof(_res.nsaddr_list))
  3189. free(p2);
  3190. }
  3191. }
  3192. #endif
  3193. memset(&_res, 0, sizeof(_res));
  3194. /* the loop is only to make gcc happy */
  3195. while(1)
  3196. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  3197. }
  3198. /*
  3199. * This routine is for closing the socket if a virtual circuit is used and
  3200. * the program wants to close it. This provides support for endhostent()
  3201. * which expects to close the socket.
  3202. *
  3203. * This routine is not expected to be user visible.
  3204. */
  3205. void
  3206. res_nclose(res_state statp)
  3207. {
  3208. __res_iclose();
  3209. }
  3210. #ifdef __UCLIBC_HAS_BSD_RES_CLOSE__
  3211. void res_close(void)
  3212. {
  3213. __res_iclose();
  3214. }
  3215. #endif
  3216. /* This needs to be after the use of _res in res_init, above. */
  3217. #undef _res
  3218. #ifndef __UCLIBC_HAS_THREADS__
  3219. /* The resolver state for use by single-threaded programs.
  3220. This differs from plain `struct __res_state _res;' in that it doesn't
  3221. create a common definition, but a plain symbol that resides in .bss,
  3222. which can have an alias. */
  3223. struct __res_state _res __attribute__((section (".bss")));
  3224. struct __res_state *__resp = &_res;
  3225. #else /* __UCLIBC_HAS_THREADS__ */
  3226. struct __res_state _res __attribute__((section (".bss"))) attribute_hidden;
  3227. # if defined __UCLIBC_HAS_TLS__
  3228. # undef __resp
  3229. __thread struct __res_state *__resp = &_res;
  3230. /*
  3231. * FIXME: Add usage of hidden attribute for this when used in the shared
  3232. * library. It currently crashes the linker when doing section
  3233. * relocations.
  3234. */
  3235. extern __thread struct __res_state *__libc_resp
  3236. __attribute__ ((alias ("__resp"))) attribute_hidden;
  3237. # else
  3238. # undef __resp
  3239. struct __res_state *__resp = &_res;
  3240. # endif
  3241. #endif /* !__UCLIBC_HAS_THREADS__ */
  3242. static unsigned int
  3243. res_randomid(void)
  3244. {
  3245. return 0xffff & getpid();
  3246. }
  3247. /* Our res_init never fails (always returns 0) */
  3248. int
  3249. res_init(void)
  3250. {
  3251. /*
  3252. * These three fields used to be statically initialized. This made
  3253. * it hard to use this code in a shared library. It is necessary,
  3254. * now that we're doing dynamic initialization here, that we preserve
  3255. * the old semantics: if an application modifies one of these three
  3256. * fields of _res before res_init() is called, res_init() will not
  3257. * alter them. Of course, if an application is setting them to
  3258. * _zero_ before calling res_init(), hoping to override what used
  3259. * to be the static default, we can't detect it and unexpected results
  3260. * will follow. Zero for any of these fields would make no sense,
  3261. * so one can safely assume that the applications were already getting
  3262. * unexpected results.
  3263. *
  3264. * _res.options is tricky since some apps were known to diddle the bits
  3265. * before res_init() was first called. We can't replicate that semantic
  3266. * with dynamic initialization (they may have turned bits off that are
  3267. * set in RES_DEFAULT). Our solution is to declare such applications
  3268. * "broken". They could fool us by setting RES_INIT but none do (yet).
  3269. */
  3270. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  3271. if (!_res.retrans)
  3272. _res.retrans = RES_TIMEOUT;
  3273. if (!_res.retry)
  3274. _res.retry = 4;
  3275. if (!(_res.options & RES_INIT))
  3276. _res.options = RES_DEFAULT;
  3277. /*
  3278. * This one used to initialize implicitly to zero, so unless the app
  3279. * has set it to something in particular, we can randomize it now.
  3280. */
  3281. if (!_res.id)
  3282. _res.id = res_randomid();
  3283. __res_sync = res_sync_func;
  3284. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  3285. __res_vinit(&_res, 1);
  3286. return 0;
  3287. }
  3288. libc_hidden_def(res_init)
  3289. /*
  3290. * Set up default settings. If the configuration file exist, the values
  3291. * there will have precedence. Otherwise, the server address is set to
  3292. * INADDR_ANY and the default domain name comes from the gethostname().
  3293. *
  3294. * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
  3295. * rather than INADDR_ANY ("0.0.0.0") as the default name server address
  3296. * since it was noted that INADDR_ANY actually meant ``the first interface
  3297. * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
  3298. * it had to be "up" in order for you to reach your own name server. It
  3299. * was later decided that since the recommended practice is to always
  3300. * install local static routes through 127.0.0.1 for all your network
  3301. * interfaces, that we could solve this problem without a code change.
  3302. *
  3303. * The configuration file should always be used, since it is the only way
  3304. * to specify a default domain. If you are running a server on your local
  3305. * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
  3306. * in the configuration file.
  3307. *
  3308. * Return 0 if completes successfully, -1 on error
  3309. */
  3310. int
  3311. res_ninit(res_state statp)
  3312. {
  3313. return __res_vinit(statp, 0);
  3314. }
  3315. #endif /* L_res_init */
  3316. #ifdef L_res_state
  3317. # if defined __UCLIBC_HAS_TLS__
  3318. struct __res_state *
  3319. __res_state (void)
  3320. {
  3321. return __resp;
  3322. }
  3323. # else
  3324. # undef _res
  3325. extern struct __res_state _res;
  3326. /* When threaded, _res may be a per-thread variable. */
  3327. struct __res_state *
  3328. weak_const_function
  3329. __res_state (void)
  3330. {
  3331. return &_res;
  3332. }
  3333. # endif
  3334. #endif /* L_res_state */
  3335. #ifdef L_res_query
  3336. int res_query(const char *dname, int class, int type,
  3337. unsigned char *answer, int anslen)
  3338. {
  3339. int i;
  3340. unsigned char *packet = NULL;
  3341. struct resolv_answer a;
  3342. if (!dname || class != 1 /* CLASS_IN */) {
  3343. h_errno = NO_RECOVERY;
  3344. return -1;
  3345. }
  3346. memset(&a, '\0', sizeof(a));
  3347. i = __dns_lookup(dname, type, &packet, &a);
  3348. if (i < 0) {
  3349. if (!h_errno) /* TODO: can this ever happen? */
  3350. h_errno = TRY_AGAIN;
  3351. return -1;
  3352. }
  3353. free(a.dotted);
  3354. if (a.atype == type) { /* CNAME */
  3355. if (i > anslen)
  3356. i = anslen;
  3357. memcpy(answer, packet, i);
  3358. }
  3359. free(packet);
  3360. return i;
  3361. }
  3362. libc_hidden_def(res_query)
  3363. /*
  3364. * Formulate a normal query, send, and retrieve answer in supplied buffer.
  3365. * Return the size of the response on success, -1 on error.
  3366. * If enabled, implement search rules until answer or unrecoverable failure
  3367. * is detected. Error code, if any, is left in h_errno.
  3368. */
  3369. #define __TRAILING_DOT (1<<0)
  3370. #define __GOT_NODATA (1<<1)
  3371. #define __GOT_SERVFAIL (1<<2)
  3372. #define __TRIED_AS_IS (1<<3)
  3373. int res_search(const char *name, int class, int type, u_char *answer,
  3374. int anslen)
  3375. {
  3376. const char *cp;
  3377. char **domain;
  3378. HEADER *hp = (HEADER *)(void *)answer;
  3379. unsigned dots;
  3380. unsigned state;
  3381. int ret, saved_herrno;
  3382. uint32_t _res_options;
  3383. unsigned _res_ndots;
  3384. char **_res_dnsrch;
  3385. if (!name || !answer) {
  3386. h_errno = NETDB_INTERNAL;
  3387. return -1;
  3388. }
  3389. again:
  3390. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  3391. _res_options = _res.options;
  3392. _res_ndots = _res.ndots;
  3393. _res_dnsrch = _res.dnsrch;
  3394. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  3395. if (!(_res_options & RES_INIT)) {
  3396. res_init(); /* our res_init never fails */
  3397. goto again;
  3398. }
  3399. state = 0;
  3400. errno = 0;
  3401. h_errno = HOST_NOT_FOUND; /* default, if we never query */
  3402. dots = 0;
  3403. for (cp = name; *cp; cp++)
  3404. dots += (*cp == '.');
  3405. if (cp > name && *--cp == '.')
  3406. state |= __TRAILING_DOT;
  3407. /*
  3408. * If there are dots in the name already, let's just give it a try
  3409. * 'as is'. The threshold can be set with the "ndots" option.
  3410. */
  3411. saved_herrno = -1;
  3412. if (dots >= _res_ndots) {
  3413. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  3414. if (ret > 0)
  3415. return ret;
  3416. saved_herrno = h_errno;
  3417. state |= __TRIED_AS_IS;
  3418. }
  3419. /*
  3420. * We do at least one level of search if
  3421. * - there is no dot and RES_DEFNAME is set, or
  3422. * - there is at least one dot, there is no trailing dot,
  3423. * and RES_DNSRCH is set.
  3424. */
  3425. if ((!dots && (_res_options & RES_DEFNAMES))
  3426. || (dots && !(state & __TRAILING_DOT) && (_res_options & RES_DNSRCH))
  3427. ) {
  3428. bool done = 0;
  3429. for (domain = _res_dnsrch; *domain && !done; domain++) {
  3430. ret = res_querydomain(name, *domain, class, type,
  3431. answer, anslen);
  3432. if (ret > 0)
  3433. return ret;
  3434. /*
  3435. * If no server present, give up.
  3436. * If name isn't found in this domain,
  3437. * keep trying higher domains in the search list
  3438. * (if that's enabled).
  3439. * On a NO_DATA error, keep trying, otherwise
  3440. * a wildcard entry of another type could keep us
  3441. * from finding this entry higher in the domain.
  3442. * If we get some other error (negative answer or
  3443. * server failure), then stop searching up,
  3444. * but try the input name below in case it's
  3445. * fully-qualified.
  3446. */
  3447. if (errno == ECONNREFUSED) {
  3448. h_errno = TRY_AGAIN;
  3449. return -1;
  3450. }
  3451. switch (h_errno) {
  3452. case NO_DATA:
  3453. state |= __GOT_NODATA;
  3454. /* FALLTHROUGH */
  3455. case HOST_NOT_FOUND:
  3456. /* keep trying */
  3457. break;
  3458. case TRY_AGAIN:
  3459. if (hp->rcode == SERVFAIL) {
  3460. /* try next search element, if any */
  3461. state |= __GOT_SERVFAIL;
  3462. break;
  3463. }
  3464. /* FALLTHROUGH */
  3465. default:
  3466. /* anything else implies that we're done */
  3467. done = 1;
  3468. }
  3469. /*
  3470. * if we got here for some reason other than DNSRCH,
  3471. * we only wanted one iteration of the loop, so stop.
  3472. */
  3473. if (!(_res_options & RES_DNSRCH))
  3474. done = 1;
  3475. }
  3476. }
  3477. /*
  3478. * if we have not already tried the name "as is", do that now.
  3479. * note that we do this regardless of how many dots were in the
  3480. * name or whether it ends with a dot.
  3481. */
  3482. if (!(state & __TRIED_AS_IS)) {
  3483. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  3484. if (ret > 0)
  3485. return ret;
  3486. }
  3487. /*
  3488. * if we got here, we didn't satisfy the search.
  3489. * if we did an initial full query, return that query's h_errno
  3490. * (note that we wouldn't be here if that query had succeeded).
  3491. * else if we ever got a nodata, send that back as the reason.
  3492. * else send back meaningless h_errno, that being the one from
  3493. * the last DNSRCH we did.
  3494. */
  3495. if (saved_herrno != -1)
  3496. h_errno = saved_herrno;
  3497. else if (state & __GOT_NODATA)
  3498. h_errno = NO_DATA;
  3499. else if (state & __GOT_SERVFAIL)
  3500. h_errno = TRY_AGAIN;
  3501. return -1;
  3502. }
  3503. #undef __TRAILING_DOT
  3504. #undef __GOT_NODATA
  3505. #undef __GOT_SERVFAIL
  3506. #undef __TRIED_AS_IS
  3507. /*
  3508. * Perform a call on res_query on the concatenation of name and domain,
  3509. * removing a trailing dot from name if domain is NULL.
  3510. */
  3511. int res_querydomain(const char *name, const char *domain, int class, int type,
  3512. u_char *answer, int anslen)
  3513. {
  3514. char nbuf[MAXDNAME];
  3515. const char *longname = nbuf;
  3516. size_t n, d;
  3517. #ifdef DEBUG
  3518. uint32_t _res_options;
  3519. #endif
  3520. if (!name || !answer) {
  3521. h_errno = NETDB_INTERNAL;
  3522. return -1;
  3523. }
  3524. #ifdef DEBUG
  3525. again:
  3526. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  3527. _res_options = _res.options;
  3528. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  3529. if (!(_res_options & RES_INIT)) {
  3530. res_init(); /* our res_init never fails */
  3531. goto again;
  3532. }
  3533. if (_res_options & RES_DEBUG)
  3534. printf(";; res_querydomain(%s, %s, %d, %d)\n",
  3535. name, (domain ? domain : "<Nil>"), class, type);
  3536. #endif
  3537. if (domain == NULL) {
  3538. /*
  3539. * Check for trailing '.';
  3540. * copy without '.' if present.
  3541. */
  3542. n = strlen(name);
  3543. if (n + 1 > sizeof(nbuf)) {
  3544. h_errno = NO_RECOVERY;
  3545. return -1;
  3546. }
  3547. if (n > 0 && name[--n] == '.') {
  3548. strncpy(nbuf, name, n);
  3549. nbuf[n] = '\0';
  3550. } else
  3551. longname = name;
  3552. } else {
  3553. n = strlen(name);
  3554. d = strlen(domain);
  3555. if (n + 1 + d + 1 > sizeof(nbuf)) {
  3556. h_errno = NO_RECOVERY;
  3557. return -1;
  3558. }
  3559. snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
  3560. }
  3561. return res_query(longname, class, type, answer, anslen);
  3562. }
  3563. libc_hidden_def(res_querydomain)
  3564. #endif /* L_res_query */
  3565. #ifdef L_ns_netint
  3566. unsigned int ns_get16(const unsigned char *src)
  3567. {
  3568. unsigned int dst;
  3569. NS_GET16(dst, src);
  3570. return dst;
  3571. }
  3572. unsigned long ns_get32(const unsigned char *src)
  3573. {
  3574. unsigned long dst;
  3575. NS_GET32(dst, src);
  3576. return dst;
  3577. }
  3578. void ns_put16(unsigned int src, unsigned char *dst)
  3579. {
  3580. NS_PUT16(src, dst);
  3581. }
  3582. void ns_put32(unsigned long src, unsigned char *dst)
  3583. {
  3584. NS_PUT32(src, dst);
  3585. }
  3586. #endif /* L_ns_netint */
  3587. #ifdef L_ns_parse
  3588. /* These need to be in the same order as the nres.h:ns_flag enum. */
  3589. struct _ns_flagdata { unsigned short mask, shift; };
  3590. static const struct _ns_flagdata _ns_flagdata[16] = {
  3591. { 0x8000, 15 }, /*%< qr. */
  3592. { 0x7800, 11 }, /*%< opcode. */
  3593. { 0x0400, 10 }, /*%< aa. */
  3594. { 0x0200, 9 }, /*%< tc. */
  3595. { 0x0100, 8 }, /*%< rd. */
  3596. { 0x0080, 7 }, /*%< ra. */
  3597. { 0x0040, 6 }, /*%< z. */
  3598. { 0x0020, 5 }, /*%< ad. */
  3599. { 0x0010, 4 }, /*%< cd. */
  3600. { 0x000f, 0 }, /*%< rcode. */
  3601. { 0x0000, 0 }, /*%< expansion (1/6). */
  3602. { 0x0000, 0 }, /*%< expansion (2/6). */
  3603. { 0x0000, 0 }, /*%< expansion (3/6). */
  3604. { 0x0000, 0 }, /*%< expansion (4/6). */
  3605. { 0x0000, 0 }, /*%< expansion (5/6). */
  3606. { 0x0000, 0 }, /*%< expansion (6/6). */
  3607. };
  3608. static void setsection(ns_msg *msg, ns_sect sect)
  3609. {
  3610. msg->_sect = sect;
  3611. if (sect == ns_s_max) {
  3612. msg->_rrnum = -1;
  3613. msg->_ptr = NULL;
  3614. } else {
  3615. msg->_rrnum = 0;
  3616. msg->_ptr = msg->_sections[(int)sect];
  3617. }
  3618. }
  3619. int ns_skiprr(const unsigned char *ptr,
  3620. const unsigned char *eom,
  3621. ns_sect section, int count)
  3622. {
  3623. const u_char *optr = ptr;
  3624. for (; count > 0; count--) {
  3625. int b, rdlength;
  3626. b = dn_skipname(ptr, eom);
  3627. if (b < 0) {
  3628. errno = EMSGSIZE;
  3629. return -1;
  3630. }
  3631. ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
  3632. if (section != ns_s_qd) {
  3633. if (ptr + NS_INT32SZ + NS_INT16SZ > eom) {
  3634. errno = EMSGSIZE;
  3635. return -1;
  3636. }
  3637. ptr += NS_INT32SZ/*TTL*/;
  3638. NS_GET16(rdlength, ptr);
  3639. ptr += rdlength/*RData*/;
  3640. }
  3641. }
  3642. if (ptr > eom) {
  3643. errno = EMSGSIZE;
  3644. return -1;
  3645. }
  3646. return ptr - optr;
  3647. }
  3648. libc_hidden_def(ns_skiprr)
  3649. int
  3650. ns_initparse(const unsigned char *msg, int msglen, ns_msg *handle)
  3651. {
  3652. const u_char *eom = msg + msglen;
  3653. int i;
  3654. handle->_msg = msg;
  3655. handle->_eom = eom;
  3656. if (msg + NS_INT16SZ > eom) {
  3657. errno = EMSGSIZE;
  3658. return -1;
  3659. }
  3660. NS_GET16(handle->_id, msg);
  3661. if (msg + NS_INT16SZ > eom) {
  3662. errno = EMSGSIZE;
  3663. return -1;
  3664. }
  3665. NS_GET16(handle->_flags, msg);
  3666. for (i = 0; i < ns_s_max; i++) {
  3667. if (msg + NS_INT16SZ > eom) {
  3668. errno = EMSGSIZE;
  3669. return -1;
  3670. }
  3671. NS_GET16(handle->_counts[i], msg);
  3672. }
  3673. for (i = 0; i < ns_s_max; i++)
  3674. if (handle->_counts[i] == 0)
  3675. handle->_sections[i] = NULL;
  3676. else {
  3677. int b = ns_skiprr(msg, eom, (ns_sect)i,
  3678. handle->_counts[i]);
  3679. if (b < 0)
  3680. return -1;
  3681. handle->_sections[i] = msg;
  3682. msg += b;
  3683. }
  3684. if (msg != eom) {
  3685. errno = EMSGSIZE;
  3686. return -1;
  3687. }
  3688. setsection(handle, ns_s_max);
  3689. return 0;
  3690. }
  3691. int
  3692. ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr)
  3693. {
  3694. int b;
  3695. int tmp;
  3696. /* Make section right. */
  3697. tmp = section;
  3698. if (tmp < 0 || section >= ns_s_max) {
  3699. errno = ENODEV;
  3700. return -1;
  3701. }
  3702. if (section != handle->_sect)
  3703. setsection(handle, section);
  3704. /* Make rrnum right. */
  3705. if (rrnum == -1)
  3706. rrnum = handle->_rrnum;
  3707. if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) {
  3708. errno = ENODEV;
  3709. return -1;
  3710. }
  3711. if (rrnum < handle->_rrnum)
  3712. setsection(handle, section);
  3713. if (rrnum > handle->_rrnum) {
  3714. b = ns_skiprr(handle->_ptr, handle->_eom, section,
  3715. rrnum - handle->_rrnum);
  3716. if (b < 0)
  3717. return -1;
  3718. handle->_ptr += b;
  3719. handle->_rrnum = rrnum;
  3720. }
  3721. /* Do the parse. */
  3722. b = dn_expand(handle->_msg, handle->_eom,
  3723. handle->_ptr, rr->name, NS_MAXDNAME);
  3724. if (b < 0)
  3725. return -1;
  3726. handle->_ptr += b;
  3727. if (handle->_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) {
  3728. errno = EMSGSIZE;
  3729. return -1;
  3730. }
  3731. NS_GET16(rr->type, handle->_ptr);
  3732. NS_GET16(rr->rr_class, handle->_ptr);
  3733. if (section == ns_s_qd) {
  3734. rr->ttl = 0;
  3735. rr->rdlength = 0;
  3736. rr->rdata = NULL;
  3737. } else {
  3738. if (handle->_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) {
  3739. errno = EMSGSIZE;
  3740. return -1;
  3741. }
  3742. NS_GET32(rr->ttl, handle->_ptr);
  3743. NS_GET16(rr->rdlength, handle->_ptr);
  3744. if (handle->_ptr + rr->rdlength > handle->_eom) {
  3745. errno = EMSGSIZE;
  3746. return -1;
  3747. }
  3748. rr->rdata = handle->_ptr;
  3749. handle->_ptr += rr->rdlength;
  3750. }
  3751. if (++handle->_rrnum > handle->_counts[(int)section])
  3752. setsection(handle, (ns_sect)((int)section + 1));
  3753. return 0;
  3754. }
  3755. int ns_msg_getflag(ns_msg handle, int flag)
  3756. {
  3757. return ((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift;
  3758. }
  3759. #endif /* L_ns_parse */
  3760. #ifdef L_res_data
  3761. int res_mkquery(int op, const char *dname, int class, int type,
  3762. const unsigned char *data, int datalen,
  3763. const unsigned char *newrr_in,
  3764. unsigned char *buf, int buflen)
  3765. {
  3766. HEADER *hp;
  3767. unsigned char *cp, *ep;
  3768. unsigned char *dnptrs[20], **dpp, **lastdnptr;
  3769. uint32_t _res_options;
  3770. int n;
  3771. if (!buf || buflen < HFIXEDSZ) {
  3772. h_errno = NETDB_INTERNAL;
  3773. return -1;
  3774. }
  3775. again:
  3776. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  3777. _res_options = _res.options;
  3778. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  3779. if (!(_res_options & RES_INIT)) {
  3780. res_init(); /* our res_init never fails */
  3781. goto again;
  3782. }
  3783. #ifdef DEBUG
  3784. if (_res_options & RES_DEBUG)
  3785. printf(";; res_mkquery(%d, %s, %d, %d)\n",
  3786. op, dname && *dname ? dname : "<null>", class, type);
  3787. #endif
  3788. memset(buf, 0, HFIXEDSZ);
  3789. hp = (HEADER *) buf;
  3790. hp->id = getpid() & 0xffff;
  3791. hp->opcode = op;
  3792. hp->rd = (_res.options & RES_RECURSE) != 0U;
  3793. hp->rcode = NOERROR;
  3794. cp = buf + HFIXEDSZ;
  3795. ep = buf + buflen;
  3796. dpp = dnptrs;
  3797. *dpp++ = buf;
  3798. *dpp++ = NULL;
  3799. lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
  3800. /*
  3801. * perform opcode specific processing
  3802. */
  3803. switch (op) {
  3804. case QUERY:
  3805. case NS_NOTIFY_OP:
  3806. if (ep - cp < QFIXEDSZ)
  3807. return -1;
  3808. n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, lastdnptr);
  3809. if (n < 0)
  3810. return -1;
  3811. cp += n;
  3812. NS_PUT16(type, cp);
  3813. NS_PUT16(class, cp);
  3814. hp->qdcount = htons(1);
  3815. if (op == QUERY || data == NULL)
  3816. break;
  3817. /*
  3818. * Make an additional record for completion domain.
  3819. */
  3820. if ((ep - cp) < RRFIXEDSZ)
  3821. return -1;
  3822. n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
  3823. dnptrs, lastdnptr);
  3824. if (n < 0)
  3825. return -1;
  3826. cp += n;
  3827. NS_PUT16(T_NULL, cp);
  3828. NS_PUT16(class, cp);
  3829. NS_PUT32(0, cp);
  3830. NS_PUT16(0, cp);
  3831. hp->arcount = htons(1);
  3832. break;
  3833. case IQUERY:
  3834. /*
  3835. * Initialize answer section
  3836. */
  3837. if (ep - cp < 1 + RRFIXEDSZ + datalen)
  3838. return -1;
  3839. *cp++ = '\0'; /*%< no domain name */
  3840. NS_PUT16(type, cp);
  3841. NS_PUT16(class, cp);
  3842. NS_PUT32(0, cp);
  3843. NS_PUT16(datalen, cp);
  3844. if (datalen) {
  3845. memcpy(cp, data, (size_t)datalen);
  3846. cp += datalen;
  3847. }
  3848. hp->ancount = htons(1);
  3849. break;
  3850. default:
  3851. return -1;
  3852. }
  3853. return cp - buf;
  3854. }
  3855. #endif /* L_res_data */
  3856. /* Unimplemented: */
  3857. /* res_send */