resolv.c 108 KB

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