resolv.c 107 KB

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