resolv.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129
  1. /* resolv.c: DNS Resolver
  2. *
  3. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
  4. * The Silver Hammer Group, Ltd.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. */
  11. /*
  12. * Portions Copyright (c) 1985, 1993
  13. * The Regents of the University of California. All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 4. Neither the name of the University nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. */
  39. /*
  40. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  41. *
  42. * Permission to use, copy, modify, and distribute this software for any
  43. * purpose with or without fee is hereby granted, provided that the above
  44. * copyright notice and this permission notice appear in all copies, and that
  45. * the name of Digital Equipment Corporation not be used in advertising or
  46. * publicity pertaining to distribution of the document or software without
  47. * specific, written prior permission.
  48. *
  49. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  50. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  51. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  52. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  53. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  54. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  55. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  56. * SOFTWARE.
  57. */
  58. /*
  59. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  60. *
  61. * Permission to use, copy, modify, and distribute this software for any
  62. * purpose with or without fee is hereby granted, provided that the above
  63. * copyright notice and this permission notice appear in all copies.
  64. *
  65. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  66. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  67. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  68. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  69. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  70. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  71. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  72. * SOFTWARE.
  73. */
  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. *
  89. * Add "search" domains from resolv.conf
  90. *
  91. * Some systems will return a security
  92. * signature along with query answer for
  93. * dynamic DNS entries.
  94. * -- skip/ignore this answer
  95. *
  96. * Include arpa/nameser.h for defines.
  97. *
  98. * General cleanup
  99. *
  100. * 20-Jun-2001 Michal Moskal <malekith@pld.org.pl>
  101. * partial IPv6 support (i.e. gethostbyname2() and resolve_address2()
  102. * functions added), IPv6 nameservers are also supported.
  103. *
  104. * 6-Oct-2001 Jari Korva <jari.korva@iki.fi>
  105. * more IPv6 support (IPv6 support for gethostbyaddr();
  106. * address family parameter and improved IPv6 support for get_hosts_byname
  107. * and read_etc_hosts; getnameinfo() port from glibc; defined
  108. * defined ip6addr_any and in6addr_loopback)
  109. *
  110. * 2-Feb-2002 Erik Andersen <andersen@codepoet.org>
  111. * Added gethostent(), sethostent(), and endhostent()
  112. *
  113. * 17-Aug-2002 Manuel Novoa III <mjn3@codepoet.org>
  114. * Fixed __read_etc_hosts_r to return alias list, and modified buffer
  115. * allocation accordingly. See MAX_ALIASES and ALIAS_DIM below.
  116. * This fixes the segfault in the Python 2.2.1 socket test.
  117. *
  118. * 04-Jan-2003 Jay Kulpinski <jskulpin@berkshire.rr.com>
  119. * Fixed __decode_dotted to count the terminating null character
  120. * in a host name.
  121. *
  122. * 02-Oct-2003 Tony J. White <tjw@tjw.org>
  123. * Lifted dn_expand() and dependent ns_name_uncompress(), ns_name_unpack(),
  124. * and ns_name_ntop() from glibc 2.3.2 for compatibility with ipsec-tools
  125. * and openldap.
  126. *
  127. * 7-Sep-2004 Erik Andersen <andersen@codepoet.org>
  128. * Added gethostent_r()
  129. *
  130. */
  131. #define __FORCE_GLIBC
  132. #include <features.h>
  133. #include <string.h>
  134. #include <stdio.h>
  135. #include <signal.h>
  136. #include <errno.h>
  137. #include <sys/poll.h>
  138. #include <sys/socket.h>
  139. #include <sys/types.h>
  140. #include <sys/time.h>
  141. #include <netinet/in.h>
  142. #include <arpa/inet.h>
  143. #include <stdlib.h>
  144. #include <unistd.h>
  145. #include <resolv.h>
  146. #include <netdb.h>
  147. #include <ctype.h>
  148. #include <stdbool.h>
  149. #include <arpa/nameser.h>
  150. #include <sys/utsname.h>
  151. #include <sys/un.h>
  152. #include <bits/uClibc_mutex.h>
  153. /* poll() is not supported in kernel <= 2.0, therefore if __NR_poll is
  154. * not available, we assume an old Linux kernel is in use and we will
  155. * use select() instead. */
  156. #include <sys/syscall.h>
  157. #ifndef __NR_poll
  158. # define USE_SELECT
  159. #endif
  160. /* Experimentally off - libc_hidden_proto(memcpy) */
  161. /* Experimentally off - libc_hidden_proto(memset) */
  162. /* Experimentally off - libc_hidden_proto(memmove) */
  163. /* Experimentally off - libc_hidden_proto(strchr) */
  164. /* Experimentally off - libc_hidden_proto(strcmp) */
  165. /* Experimentally off - libc_hidden_proto(strcpy) */
  166. /* Experimentally off - libc_hidden_proto(strdup) */
  167. /* Experimentally off - libc_hidden_proto(strlen) */
  168. /* Experimentally off - libc_hidden_proto(strncat) */
  169. /* Experimentally off - libc_hidden_proto(strncpy) */
  170. /* libc_hidden_proto(strnlen) */
  171. /* Experimentally off - libc_hidden_proto(strstr) */
  172. /* Experimentally off - libc_hidden_proto(strcasecmp) */
  173. libc_hidden_proto(socket)
  174. libc_hidden_proto(close)
  175. libc_hidden_proto(fopen)
  176. libc_hidden_proto(fclose)
  177. libc_hidden_proto(random)
  178. libc_hidden_proto(getservbyport)
  179. libc_hidden_proto(gethostname)
  180. libc_hidden_proto(uname)
  181. libc_hidden_proto(inet_addr)
  182. libc_hidden_proto(inet_aton)
  183. libc_hidden_proto(inet_pton)
  184. libc_hidden_proto(inet_ntop)
  185. libc_hidden_proto(connect)
  186. libc_hidden_proto(poll)
  187. libc_hidden_proto(select)
  188. libc_hidden_proto(recv)
  189. libc_hidden_proto(send)
  190. libc_hidden_proto(printf)
  191. libc_hidden_proto(sprintf)
  192. libc_hidden_proto(snprintf)
  193. libc_hidden_proto(fgets)
  194. libc_hidden_proto(getnameinfo)
  195. libc_hidden_proto(gethostbyname)
  196. libc_hidden_proto(gethostbyname_r)
  197. libc_hidden_proto(gethostbyname2_r)
  198. libc_hidden_proto(gethostbyaddr)
  199. libc_hidden_proto(gethostbyaddr_r)
  200. libc_hidden_proto(ns_name_uncompress)
  201. libc_hidden_proto(ns_name_unpack)
  202. libc_hidden_proto(ns_name_ntop)
  203. libc_hidden_proto(res_init)
  204. libc_hidden_proto(res_query)
  205. libc_hidden_proto(res_querydomain)
  206. libc_hidden_proto(gethostent_r)
  207. libc_hidden_proto(fprintf)
  208. libc_hidden_proto(__h_errno_location)
  209. #ifdef __UCLIBC_HAS_XLOCALE__
  210. libc_hidden_proto(__ctype_b_loc)
  211. #elif defined __UCLIBC_HAS_CTYPE_TABLES__
  212. libc_hidden_proto(__ctype_b)
  213. #endif
  214. int __libc_getdomainname(char *name, size_t len);
  215. libc_hidden_proto(__libc_getdomainname)
  216. #define MAX_RECURSE 5
  217. #define REPLY_TIMEOUT 10
  218. #define MAX_RETRIES 3
  219. #define MAX_SERVERS 3
  220. #define MAX_SEARCH 4
  221. #define MAX_ALIASES 5
  222. /* 1:ip + 1:full + MAX_ALIASES:aliases + 1:NULL */
  223. #define ALIAS_DIM (2 + MAX_ALIASES + 1)
  224. #undef DEBUG
  225. /* #define DEBUG */
  226. #ifdef DEBUG
  227. #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
  228. #else
  229. #define DPRINTF(X,args...)
  230. #endif
  231. #undef ARRAY_SIZE
  232. #define ARRAY_SIZE(v) (sizeof(v) / sizeof((v)[0]))
  233. /* Make sure the incoming char * buffer is aligned enough to handle our random
  234. * structures. This define is the same as we use for malloc alignment (which
  235. * has same requirements). The offset is the number of bytes we need to adjust
  236. * in order to attain desired alignment.
  237. */
  238. #define ALIGN_ATTR __alignof__(double __attribute_aligned__ (sizeof(size_t)))
  239. #define ALIGN_BUFFER_OFFSET(buf) ((ALIGN_ATTR - ((size_t)buf % ALIGN_ATTR)) % ALIGN_ATTR)
  240. /* Structs */
  241. struct resolv_header {
  242. int id;
  243. int qr,opcode,aa,tc,rd,ra,rcode;
  244. int qdcount;
  245. int ancount;
  246. int nscount;
  247. int arcount;
  248. };
  249. struct resolv_question {
  250. char * dotted;
  251. int qtype;
  252. int qclass;
  253. };
  254. struct resolv_answer {
  255. char * dotted;
  256. int atype;
  257. int aclass;
  258. int ttl;
  259. int rdlength;
  260. const unsigned char * rdata;
  261. int rdoffset;
  262. char* buf;
  263. size_t buflen;
  264. size_t add_count;
  265. };
  266. enum etc_hosts_action {
  267. GET_HOSTS_BYNAME = 0,
  268. GETHOSTENT,
  269. GET_HOSTS_BYADDR,
  270. };
  271. typedef union sockaddr46_t {
  272. struct sockaddr sa;
  273. #ifdef __UCLIBC_HAS_IPV4__
  274. struct sockaddr_in sa4;
  275. #endif
  276. #ifdef __UCLIBC_HAS_IPV6__
  277. struct sockaddr_in6 sa6;
  278. #endif
  279. } sockaddr46_t;
  280. __UCLIBC_MUTEX_EXTERN(__resolv_lock);
  281. /* Protected by __resolv_lock */
  282. extern void (*__res_sync)(void) attribute_hidden;
  283. //extern uint32_t __resolv_opts attribute_hidden;
  284. extern unsigned __nameservers attribute_hidden;
  285. extern unsigned __searchdomains attribute_hidden;
  286. extern sockaddr46_t *__nameserver attribute_hidden;
  287. extern char **__searchdomain attribute_hidden;
  288. #ifdef __UCLIBC_HAS_IPV4__
  289. extern const struct sockaddr_in __local_nameserver attribute_hidden;
  290. #else
  291. extern const struct sockaddr_in6 __local_nameserver attribute_hidden;
  292. #endif
  293. /* Arbitrary */
  294. #define MAXLEN_searchdomain 128
  295. /* function prototypes */
  296. extern int __get_hosts_byname_r(const char * name, int type,
  297. struct hostent * result_buf,
  298. char * buf, size_t buflen,
  299. struct hostent ** result,
  300. int * h_errnop) attribute_hidden;
  301. extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
  302. struct hostent * result_buf,
  303. char * buf, size_t buflen,
  304. struct hostent ** result,
  305. int * h_errnop) attribute_hidden;
  306. extern FILE * __open_etc_hosts(void) attribute_hidden;
  307. extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
  308. enum etc_hosts_action action,
  309. struct hostent * result_buf,
  310. char * buf, size_t buflen,
  311. struct hostent ** result,
  312. int * h_errnop) attribute_hidden;
  313. extern int __dns_lookup(const char * name, int type,
  314. unsigned char ** outpacket,
  315. struct resolv_answer * a) attribute_hidden;
  316. extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen) attribute_hidden;
  317. extern int __decode_dotted(const unsigned char * const message, int offset,
  318. char * dest, int maxlen) attribute_hidden;
  319. extern int __length_dotted(const unsigned char * const message, int offset) attribute_hidden;
  320. extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen) attribute_hidden;
  321. extern void __decode_header(unsigned char * data, struct resolv_header * h) attribute_hidden;
  322. extern int __encode_question(const struct resolv_question * const q,
  323. unsigned char * dest, int maxlen) attribute_hidden;
  324. extern int __decode_question(const unsigned char * const message, int offset,
  325. struct resolv_question * q) attribute_hidden;
  326. extern int __encode_answer(struct resolv_answer * a,
  327. unsigned char * dest, int maxlen) attribute_hidden;
  328. extern int __decode_answer(const unsigned char * message, int offset,
  329. int len, struct resolv_answer * a) attribute_hidden;
  330. extern int __length_question(const unsigned char * const message, int offset) attribute_hidden;
  331. extern void __open_nameservers(void) attribute_hidden;
  332. extern void __close_nameservers(void) attribute_hidden;
  333. extern int __dn_expand(const u_char *, const u_char *, const u_char *,
  334. char *, int);
  335. /*
  336. * Theory of operation.
  337. *
  338. * gethostbyname, getaddrinfo and friends end up here, and they sometimes
  339. * need to talk to DNS servers. In order to do this, we need to read /etc/resolv.conf
  340. * and determine servers' addresses and the like. resolv.conf format:
  341. *
  342. * nameserver <IP[v6]>
  343. * Address of DNS server. Cumulative.
  344. * If not specified, assumed to be on localhost.
  345. * search <domain1>[ <domain2>]...
  346. * Append these domains to unqualified names.
  347. * See ndots:n option.
  348. * $LOCALDOMAIN (space-separated list) overrides this.
  349. * domain <domain>
  350. * Effectively same as "search" with one domain.
  351. * If no "domain" line is present, the domain is determined
  352. * from the local host name returned by gethostname();
  353. * the domain part is taken to be everything after the first dot.
  354. * If there are no dots, there will be no "domain".
  355. * The domain and search keywords are mutually exclusive.
  356. * If more than one instance of these keywords is present,
  357. * the last instance wins.
  358. * sortlist 130.155.160.0[/255.255.240.0] 130.155.0.0
  359. * Allows addresses returned by gethostbyname to be sorted.
  360. * Not supported.
  361. * options option[ option]...
  362. * (so far we support none)
  363. * $RES_OPTIONS (space-separated list) is to be added to "options"
  364. * debug sets RES_DEBUG in _res.options
  365. * ndots:n how many dots there should be so that name will be tried
  366. * first as an absolute name before any search list elements
  367. * are appended to it. Default 1
  368. * timeout:n how long to wait for response. Default 5
  369. * (sun seems to have retrans:n synonym)
  370. * attempts:n number of rounds to do before giving up and returning
  371. * an error. Default 2
  372. * (sun seems to have retry:n synonym)
  373. * rotate sets RES_ROTATE in _res.options, round robin
  374. * selection of nameservers. Otherwise try
  375. * the first listed server first every time
  376. * no-check-names
  377. * sets RES_NOCHECKNAME in _res.options, which disables
  378. * checking of incoming host names for invalid characters
  379. * such as underscore (_), non-ASCII, or control characters
  380. * inet6 sets RES_USE_INET6 in _res.options. Try a AAAA query
  381. * before an A query inside the gethostbyname(), and map
  382. * IPv4 responses in IPv6 "tunnelled form" if no AAAA records
  383. * are found but an A record set exists
  384. * no_tld_query (FreeBSDism?)
  385. * do not attempt to resolve names without dots
  386. *
  387. * We will read and analyze /etc/resolv.conf as needed before
  388. * we do a DNS request. This happens in __dns_lookup.
  389. * (TODO: also re-parse it after a timeout, it might get updated).
  390. *
  391. * BSD has res_init routine which is used to initialize resolver state
  392. * which is held in global structure _res.
  393. * Generally, programs call res_init, then fiddle with _res.XXX
  394. * (_res.options and _res.nscount, _res.nsaddr_list[N]
  395. * are popular targets of fiddling) and expect subsequent calls
  396. * to gethostbyname, getaddrinfo, etc to use modified information.
  397. *
  398. * However, historical _res structure is quite awkward.
  399. * Using it for storing /etc/resolv.conf info is not desirable,
  400. * and __dns_lookup does not use it.
  401. *
  402. * We would like to avoid using it unless absolutely necessary.
  403. * If user doesn't use res_init, we should arrange it so that
  404. * _res structure doesn't even *get linked in* into user's application
  405. * (imagine static uclibc build here).
  406. *
  407. * The solution is a __res_sync function pointer, which is normally NULL.
  408. * But if res_init is called, it gets set and any subsequent gethostbyname
  409. * et al "syncronizes" our internal structures with potentially
  410. * modified _res.XXX stuff by calling __res_sync.
  411. * The trick here is that if res_init is not used and not linked in,
  412. * gethostbyname itself won't reference _res and _res won't be linked in
  413. * either. Other possible methods like
  414. * if (__res_sync_just_an_int_flag)
  415. * __sync_me_with_res()
  416. * would pull in __sync_me_with_res, which pulls in _res. Bad.
  417. */
  418. #ifdef L_encodeh
  419. int attribute_hidden __encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
  420. {
  421. if (maxlen < HFIXEDSZ)
  422. return -1;
  423. dest[0] = (h->id & 0xff00) >> 8;
  424. dest[1] = (h->id & 0x00ff) >> 0;
  425. dest[2] = (h->qr ? 0x80 : 0) |
  426. ((h->opcode & 0x0f) << 3) |
  427. (h->aa ? 0x04 : 0) |
  428. (h->tc ? 0x02 : 0) |
  429. (h->rd ? 0x01 : 0);
  430. dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
  431. dest[4] = (h->qdcount & 0xff00) >> 8;
  432. dest[5] = (h->qdcount & 0x00ff) >> 0;
  433. dest[6] = (h->ancount & 0xff00) >> 8;
  434. dest[7] = (h->ancount & 0x00ff) >> 0;
  435. dest[8] = (h->nscount & 0xff00) >> 8;
  436. dest[9] = (h->nscount & 0x00ff) >> 0;
  437. dest[10] = (h->arcount & 0xff00) >> 8;
  438. dest[11] = (h->arcount & 0x00ff) >> 0;
  439. return HFIXEDSZ;
  440. }
  441. #endif
  442. #ifdef L_decodeh
  443. void attribute_hidden __decode_header(unsigned char *data, struct resolv_header *h)
  444. {
  445. h->id = (data[0] << 8) | data[1];
  446. h->qr = (data[2] & 0x80) ? 1 : 0;
  447. h->opcode = (data[2] >> 3) & 0x0f;
  448. h->aa = (data[2] & 0x04) ? 1 : 0;
  449. h->tc = (data[2] & 0x02) ? 1 : 0;
  450. h->rd = (data[2] & 0x01) ? 1 : 0;
  451. h->ra = (data[3] & 0x80) ? 1 : 0;
  452. h->rcode = data[3] & 0x0f;
  453. h->qdcount = (data[4] << 8) | data[5];
  454. h->ancount = (data[6] << 8) | data[7];
  455. h->nscount = (data[8] << 8) | data[9];
  456. h->arcount = (data[10] << 8) | data[11];
  457. }
  458. #endif
  459. #ifdef L_encoded
  460. /* Encode a dotted string into nameserver transport-level encoding.
  461. This routine is fairly dumb, and doesn't attempt to compress
  462. the data */
  463. int attribute_hidden __encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
  464. {
  465. unsigned used = 0;
  466. while (dotted && *dotted) {
  467. char *c = strchr(dotted, '.');
  468. int l = c ? c - dotted : strlen(dotted);
  469. /* two consecutive dots are not valid */
  470. if (l == 0)
  471. return -1;
  472. if (l >= (maxlen - used - 1))
  473. return -1;
  474. dest[used++] = l;
  475. memcpy(dest + used, dotted, l);
  476. used += l;
  477. if (!c)
  478. break;
  479. dotted = c + 1;
  480. }
  481. if (maxlen < 1)
  482. return -1;
  483. dest[used++] = 0;
  484. return used;
  485. }
  486. #endif
  487. #ifdef L_decoded
  488. /* Decode a dotted string from nameserver transport-level encoding.
  489. This routine understands compressed data. */
  490. int attribute_hidden __decode_dotted(const unsigned char * const data, int offset,
  491. char *dest, int maxlen)
  492. {
  493. int l;
  494. bool measure = 1;
  495. unsigned total = 0;
  496. unsigned used = 0;
  497. if (!data)
  498. return -1;
  499. while ((l = data[offset++])) {
  500. if (measure)
  501. total++;
  502. if ((l & 0xc0) == (0xc0)) {
  503. if (measure)
  504. total++;
  505. /* compressed item, redirect */
  506. offset = ((l & 0x3f) << 8) | data[offset];
  507. measure = 0;
  508. continue;
  509. }
  510. if ((used + l + 1) >= maxlen)
  511. return -1;
  512. memcpy(dest + used, data + offset, l);
  513. offset += l;
  514. used += l;
  515. if (measure)
  516. total += l;
  517. if (data[offset] != 0)
  518. dest[used++] = '.';
  519. else
  520. dest[used++] = '\0';
  521. }
  522. /* The null byte must be counted too */
  523. if (measure)
  524. total++;
  525. DPRINTF("Total decode len = %d\n", total);
  526. return total;
  527. }
  528. #endif
  529. #ifdef L_lengthd
  530. /* Returns -1 only if data == NULL */
  531. int attribute_hidden __length_dotted(const unsigned char * const data, int offset)
  532. {
  533. int orig_offset = offset;
  534. int l;
  535. if (!data)
  536. return -1;
  537. while ((l = data[offset++])) {
  538. if ((l & 0xc0) == (0xc0)) {
  539. offset++;
  540. break;
  541. }
  542. offset += l;
  543. }
  544. return offset - orig_offset;
  545. }
  546. #endif
  547. #ifdef L_encodeq
  548. int attribute_hidden __encode_question(const struct resolv_question * const q,
  549. unsigned char *dest, int maxlen)
  550. {
  551. int i;
  552. i = __encode_dotted(q->dotted, dest, maxlen);
  553. if (i < 0)
  554. return i;
  555. dest += i;
  556. maxlen -= i;
  557. if (maxlen < 4)
  558. return -1;
  559. dest[0] = (q->qtype & 0xff00) >> 8;
  560. dest[1] = (q->qtype & 0x00ff) >> 0;
  561. dest[2] = (q->qclass & 0xff00) >> 8;
  562. dest[3] = (q->qclass & 0x00ff) >> 0;
  563. return i + 4;
  564. }
  565. #endif
  566. #ifdef L_decodeq
  567. int attribute_hidden __decode_question(const unsigned char * const message, int offset,
  568. struct resolv_question *q)
  569. {
  570. char temp[256];
  571. int i;
  572. i = __decode_dotted(message, offset, temp, sizeof(temp));
  573. if (i < 0)
  574. return i;
  575. offset += i;
  576. //TODO: what if strdup fails?
  577. q->dotted = strdup(temp);
  578. q->qtype = (message[offset + 0] << 8) | message[offset + 1];
  579. q->qclass = (message[offset + 2] << 8) | message[offset + 3];
  580. return i + 4;
  581. }
  582. #endif
  583. #ifdef L_lengthq
  584. /* Returns -1 only if message == NULL */
  585. int attribute_hidden __length_question(const unsigned char * const message, int offset)
  586. {
  587. int i;
  588. /* returns -1 only if message == NULL */
  589. i = __length_dotted(message, offset);
  590. if (i < 0)
  591. return i;
  592. return i + 4;
  593. }
  594. #endif
  595. #ifdef L_encodea
  596. int attribute_hidden __encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
  597. {
  598. int i;
  599. i = __encode_dotted(a->dotted, dest, maxlen);
  600. if (i < 0)
  601. return i;
  602. dest += i;
  603. maxlen -= i;
  604. if (maxlen < (RRFIXEDSZ + a->rdlength))
  605. return -1;
  606. *dest++ = (a->atype & 0xff00) >> 8;
  607. *dest++ = (a->atype & 0x00ff) >> 0;
  608. *dest++ = (a->aclass & 0xff00) >> 8;
  609. *dest++ = (a->aclass & 0x00ff) >> 0;
  610. *dest++ = (a->ttl & 0xff000000) >> 24;
  611. *dest++ = (a->ttl & 0x00ff0000) >> 16;
  612. *dest++ = (a->ttl & 0x0000ff00) >> 8;
  613. *dest++ = (a->ttl & 0x000000ff) >> 0;
  614. *dest++ = (a->rdlength & 0xff00) >> 8;
  615. *dest++ = (a->rdlength & 0x00ff) >> 0;
  616. memcpy(dest, a->rdata, a->rdlength);
  617. return i + RRFIXEDSZ + a->rdlength;
  618. }
  619. #endif
  620. #ifdef L_decodea
  621. int attribute_hidden __decode_answer(const unsigned char *message, int offset,
  622. int len, struct resolv_answer *a)
  623. {
  624. char temp[256];
  625. int i;
  626. DPRINTF("decode_answer(start): off %d, len %d\n", offset, len);
  627. i = __decode_dotted(message, offset, temp, sizeof(temp));
  628. if (i < 0)
  629. return i;
  630. message += offset + i;
  631. len -= i + RRFIXEDSZ + offset;
  632. if (len < 0) {
  633. DPRINTF("decode_answer: off %d, len %d, i %d\n", offset, len, i);
  634. return len;
  635. }
  636. // TODO: what if strdup fails?
  637. a->dotted = strdup(temp);
  638. a->atype = (message[0] << 8) | message[1];
  639. message += 2;
  640. a->aclass = (message[0] << 8) | message[1];
  641. message += 2;
  642. a->ttl = (message[0] << 24) |
  643. (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
  644. message += 4;
  645. a->rdlength = (message[0] << 8) | message[1];
  646. message += 2;
  647. a->rdata = message;
  648. a->rdoffset = offset + i + RRFIXEDSZ;
  649. DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
  650. if (len < a->rdlength)
  651. return -1;
  652. return i + RRFIXEDSZ + a->rdlength;
  653. }
  654. #endif
  655. #ifdef CURRENTLY_UNUSED
  656. #ifdef L_encodep
  657. int __encode_packet(struct resolv_header *h,
  658. struct resolv_question **q,
  659. struct resolv_answer **an,
  660. struct resolv_answer **ns,
  661. struct resolv_answer **ar,
  662. unsigned char *dest, int maxlen) attribute_hidden;
  663. int __encode_packet(struct resolv_header *h,
  664. struct resolv_question **q,
  665. struct resolv_answer **an,
  666. struct resolv_answer **ns,
  667. struct resolv_answer **ar,
  668. unsigned char *dest, int maxlen)
  669. {
  670. int i, total = 0;
  671. unsigned j;
  672. i = __encode_header(h, dest, maxlen);
  673. if (i < 0)
  674. return i;
  675. dest += i;
  676. maxlen -= i;
  677. total += i;
  678. for (j = 0; j < h->qdcount; j++) {
  679. i = __encode_question(q[j], dest, maxlen);
  680. if (i < 0)
  681. return i;
  682. dest += i;
  683. maxlen -= i;
  684. total += i;
  685. }
  686. for (j = 0; j < h->ancount; j++) {
  687. i = __encode_answer(an[j], dest, maxlen);
  688. if (i < 0)
  689. return i;
  690. dest += i;
  691. maxlen -= i;
  692. total += i;
  693. }
  694. for (j = 0; j < h->nscount; j++) {
  695. i = __encode_answer(ns[j], dest, maxlen);
  696. if (i < 0)
  697. return i;
  698. dest += i;
  699. maxlen -= i;
  700. total += i;
  701. }
  702. for (j = 0; j < h->arcount; j++) {
  703. i = __encode_answer(ar[j], dest, maxlen);
  704. if (i < 0)
  705. return i;
  706. dest += i;
  707. maxlen -= i;
  708. total += i;
  709. }
  710. return total;
  711. }
  712. #endif
  713. #ifdef L_decodep
  714. int __decode_packet(unsigned char *data, struct resolv_header *h) attribute_hidden;
  715. int __decode_packet(unsigned char *data, struct resolv_header *h)
  716. {
  717. __decode_header(data, h);
  718. return HFIXEDSZ;
  719. }
  720. #endif
  721. #ifdef L_formquery
  722. int __form_query(int id, const char *name, int type, unsigned char *packet, int maxlen);
  723. int __form_query(int id, const char *name, int type, unsigned char *packet,
  724. int maxlen)
  725. {
  726. struct resolv_header h;
  727. struct resolv_question q;
  728. int i, j;
  729. memset(&h, 0, sizeof(h));
  730. h.id = id;
  731. h.qdcount = 1;
  732. q.dotted = (char *) name;
  733. q.qtype = type;
  734. q.qclass = C_IN; /* CLASS_IN */
  735. i = __encode_header(&h, packet, maxlen);
  736. if (i < 0)
  737. return i;
  738. j = __encode_question(&q, packet + i, maxlen - i);
  739. if (j < 0)
  740. return j;
  741. return i + j;
  742. }
  743. #endif
  744. #endif /* CURRENTLY_UNUSED */
  745. #ifdef L_opennameservers
  746. # if __BYTE_ORDER == __LITTLE_ENDIAN
  747. #define NAMESERVER_PORT_N (__bswap_constant_16(NAMESERVER_PORT))
  748. #else
  749. #define NAMESERVER_PORT_N NAMESERVER_PORT
  750. #endif
  751. __UCLIBC_MUTEX_INIT(__resolv_lock, PTHREAD_MUTEX_INITIALIZER);
  752. /* Protected by __resolv_lock */
  753. void (*__res_sync)(void);
  754. //uint32_t __resolv_opts;
  755. unsigned __nameservers;
  756. unsigned __searchdomains;
  757. sockaddr46_t *__nameserver;
  758. char **__searchdomain;
  759. #ifdef __UCLIBC_HAS_IPV4__
  760. const struct sockaddr_in __local_nameserver = {
  761. .sin_family = AF_INET,
  762. .sin_port = NAMESERVER_PORT_N,
  763. };
  764. #else
  765. const struct sockaddr_in6 __local_nameserver = {
  766. .sin6_family = AF_INET6,
  767. .sin6_port = NAMESERVER_PORT_N,
  768. };
  769. #endif
  770. /* Helpers. Both stop on EOL, if it's '\n', it is converted to NUL first */
  771. static char *skip_nospace(char *p)
  772. {
  773. while (*p != '\0' && !isspace(*p)) {
  774. if (*p == '\n') {
  775. *p = '\0';
  776. break;
  777. }
  778. p++;
  779. }
  780. return p;
  781. }
  782. static char *skip_and_NUL_space(char *p)
  783. {
  784. /* NB: '\n' is not isspace! */
  785. while (1) {
  786. char c = *p;
  787. if (c == '\0' || !isspace(c))
  788. break;
  789. *p = '\0';
  790. if (c == '\n' || c == '#')
  791. break;
  792. p++;
  793. }
  794. return p;
  795. }
  796. /* Must be called under __resolv_lock. */
  797. void attribute_hidden __open_nameservers(void)
  798. {
  799. char szBuffer[MAXLEN_searchdomain];
  800. FILE *fp;
  801. int i;
  802. sockaddr46_t sa;
  803. //if (!__res_sync && last_time_was_long_ago)
  804. // __close_nameservers(); /* force config reread */
  805. if (__nameservers)
  806. goto sync;
  807. fp = fopen("/etc/resolv.conf", "r");
  808. if (!fp) {
  809. // TODO: why? google says nothing about this...
  810. fp = fopen("/etc/config/resolv.conf", "r");
  811. }
  812. if (fp) {
  813. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  814. void *ptr;
  815. char *keyword, *p;
  816. keyword = p = skip_and_NUL_space(szBuffer);
  817. /* skip keyword */
  818. p = skip_nospace(p);
  819. /* find next word */
  820. p = skip_and_NUL_space(p);
  821. if (strcmp(keyword, "nameserver") == 0) {
  822. /* terminate IP addr */
  823. *skip_nospace(p) = '\0';
  824. memset(&sa, 0, sizeof(sa));
  825. if (0) /* nothing */;
  826. #ifdef __UCLIBC_HAS_IPV6__
  827. else if (inet_pton(AF_INET6, p, &sa.sa6.sin6_addr) > 0) {
  828. sa.sa6.sin6_family = AF_INET6;
  829. sa.sa6.sin6_port = htons(NAMESERVER_PORT);
  830. }
  831. #endif
  832. #ifdef __UCLIBC_HAS_IPV4__
  833. else if (inet_pton(AF_INET, p, &sa.sa4.sin_addr) > 0) {
  834. sa.sa4.sin_family = AF_INET;
  835. sa.sa4.sin_port = htons(NAMESERVER_PORT);
  836. }
  837. #endif
  838. else
  839. continue; /* garbage on this line */
  840. ptr = realloc(__nameserver, (__nameservers + 1) * sizeof(__nameserver[0]));
  841. if (!ptr)
  842. continue;
  843. __nameserver = ptr;
  844. __nameserver[__nameservers++] = sa; /* struct copy */
  845. continue;
  846. }
  847. if (strcmp(keyword, "domain") == 0 || strcmp(keyword, "search") == 0) {
  848. char *p1;
  849. /* free old domains ("last 'domain' or 'search' wins" rule) */
  850. while (__searchdomains)
  851. free(__searchdomain[--__searchdomains]);
  852. /*free(__searchdomain);*/
  853. /*__searchdomain = NULL; - not necessary */
  854. next_word:
  855. /* terminate current word */
  856. p1 = skip_nospace(p);
  857. /* find next word (maybe) */
  858. p1 = skip_and_NUL_space(p1);
  859. /* add it */
  860. ptr = realloc(__searchdomain, (__searchdomains + 1) * sizeof(__searchdomain[0]));
  861. if (!ptr)
  862. continue;
  863. __searchdomain = ptr;
  864. /* NB: strlen(p) <= MAXLEN_searchdomain) because szBuffer[] is smaller */
  865. ptr = strdup(p);
  866. if (!ptr)
  867. continue;
  868. DPRINTF("adding search %s\n", (char*)ptr);
  869. __searchdomain[__searchdomains++] = (char*)ptr;
  870. p = p1;
  871. if (*p)
  872. goto next_word;
  873. continue;
  874. }
  875. /* if (strcmp(keyword, "sortlist") == 0)... */
  876. /* if (strcmp(keyword, "options") == 0)... */
  877. }
  878. fclose(fp);
  879. }
  880. if (__nameservers == 0) {
  881. /* Have to handle malloc failure! What a mess...
  882. * And it's not only here, we need to be careful
  883. * to never write into __nameserver[0] if it points
  884. * to constant __local_nameserver, or free it. */
  885. __nameserver = malloc(sizeof(__nameserver[0]));
  886. if (__nameserver)
  887. memcpy(__nameserver, &__local_nameserver, sizeof(__local_nameserver));
  888. else
  889. __nameserver = (void*) &__local_nameserver;
  890. __nameservers++;
  891. }
  892. if (__searchdomains == 0) {
  893. char buf[256];
  894. char *p;
  895. i = gethostname(buf, sizeof(buf) - 1);
  896. buf[sizeof(buf) - 1] = '\0';
  897. if (i == 0 && (p = strchr(buf, '.')) != NULL && p[1]) {
  898. p = strdup(p + 1);
  899. if (!p)
  900. goto err;
  901. __searchdomain = malloc(sizeof(__searchdomain[0]));
  902. if (!__searchdomain) {
  903. free(p);
  904. goto err;
  905. }
  906. __searchdomain[0] = p;
  907. __searchdomains++;
  908. err: ;
  909. }
  910. }
  911. DPRINTF("nameservers = %d\n", __nameservers);
  912. sync:
  913. if (__res_sync)
  914. __res_sync();
  915. }
  916. #endif
  917. #ifdef L_closenameservers
  918. /* Must be called under __resolv_lock. */
  919. void attribute_hidden __close_nameservers(void)
  920. {
  921. if (__nameserver != (void*) &__local_nameserver)
  922. free(__nameserver);
  923. __nameserver = NULL;
  924. __nameservers = 0;
  925. while (__searchdomains)
  926. free(__searchdomain[--__searchdomains]);
  927. free(__searchdomain);
  928. __searchdomain = NULL;
  929. /*__searchdomains = 0; - already is */
  930. }
  931. #endif
  932. #ifdef L_dnslookup
  933. /* On entry:
  934. * a.buf(len) = auxiliary buffer for IP addresses after first one
  935. * a.add_count = how many additional addresses are there already
  936. * outpacket = where to save ptr to raw packet? can be NULL
  937. * On exit:
  938. * ret < 0: error, all other data is not valid
  939. * a.add_count & a.buf: updated
  940. * a.rdlength: length of addresses (4 bytes for IPv4)
  941. * *outpacket: updated (packet is malloced, you need to free it)
  942. * a.rdata: points into *outpacket to 1st IP addr
  943. * NB: don't pass outpacket == NULL if you need to use a.rdata!
  944. * a.atype: type of query?
  945. * a.dotted: which name we _actually_ used. May contain search domains
  946. * appended. (why the filed is called "dotted" I have no idea)
  947. * This is a malloced string. May be NULL because strdup failed.
  948. */
  949. int attribute_hidden __dns_lookup(const char *name, int type,
  950. unsigned char **outpacket,
  951. struct resolv_answer *a)
  952. {
  953. /* Protected by __resolv_lock: */
  954. static int last_ns_num = 0;
  955. static uint16_t last_id = 1;
  956. int i, j, len, fd, pos, rc;
  957. int name_len;
  958. #ifdef USE_SELECT
  959. struct timeval tv;
  960. fd_set fds;
  961. #else
  962. struct pollfd fds;
  963. #endif
  964. struct resolv_header h;
  965. struct resolv_question q;
  966. struct resolv_answer ma;
  967. bool first_answer = 1;
  968. int retries_left;
  969. unsigned char *packet = malloc(PACKETSZ);
  970. char *lookup;
  971. int variant = -1; /* search domain to append, -1: none */
  972. int local_ns_num = -1; /* Nth server to use */
  973. int local_id = local_id; /* for compiler */
  974. int sdomains;
  975. bool ends_with_dot;
  976. sockaddr46_t sa;
  977. fd = -1;
  978. lookup = NULL;
  979. name_len = strlen(name);
  980. if ((unsigned)name_len >= MAXDNAME - MAXLEN_searchdomain - 2)
  981. goto fail; /* paranoia */
  982. lookup = malloc(name_len + 1/*for '.'*/ + MAXLEN_searchdomain + 1);
  983. if (!packet || !lookup || !name[0])
  984. goto fail;
  985. ends_with_dot = (name[name_len - 1] == '.');
  986. /* no strcpy! paranoia, user might change name[] under us */
  987. memcpy(lookup, name, name_len);
  988. DPRINTF("Looking up type %d answer for '%s'\n", type, name);
  989. retries_left = 0; /* for compiler */
  990. do {
  991. unsigned reply_timeout;
  992. if (fd != -1) {
  993. close(fd);
  994. fd = -1;
  995. }
  996. /* Mess with globals while under lock */
  997. /* NB: even data *pointed to* by globals may vanish
  998. * outside the locks. We should assume any and all
  999. * globals can completely change between locked
  1000. * code regions. OTOH, this is rare, so we don't need
  1001. * to handle it "nicely" (do not skip servers,
  1002. * search domains, etc), we only need to ensure
  1003. * we do not SEGV, use freed+overwritten data
  1004. * or do other Really Bad Things. */
  1005. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1006. __open_nameservers();
  1007. sdomains = __searchdomains;
  1008. lookup[name_len] = '\0';
  1009. if ((unsigned)variant < sdomains) {
  1010. /* lookup is name_len + 1 + MAXLEN_searchdomain + 1 long */
  1011. /* __searchdomain[] is not bigger than MAXLEN_searchdomain */
  1012. lookup[name_len] = '.';
  1013. strcpy(&lookup[name_len + 1], __searchdomain[variant]);
  1014. }
  1015. /* first time? pick starting server etc */
  1016. if (local_ns_num < 0) {
  1017. local_id = last_id;
  1018. //TODO: implement /etc/resolv.conf's "options rotate"
  1019. // (a.k.a. RES_ROTATE bit in _res.options)
  1020. // local_ns_num = 0;
  1021. // if (_res.options & RES_ROTATE)
  1022. local_ns_num = last_ns_num;
  1023. retries_left = __nameservers * MAX_RETRIES;
  1024. }
  1025. retries_left--;
  1026. if (local_ns_num >= __nameservers)
  1027. local_ns_num = 0;
  1028. local_id++;
  1029. local_id &= 0xffff;
  1030. /* write new values back while still under lock */
  1031. last_id = local_id;
  1032. last_ns_num = local_ns_num;
  1033. /* struct copy */
  1034. /* can't just take a pointer, __nameserver[x]
  1035. * is not safe to use outside of locks */
  1036. sa = __nameserver[local_ns_num];
  1037. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1038. memset(packet, 0, PACKETSZ);
  1039. memset(&h, 0, sizeof(h));
  1040. /* encode header */
  1041. h.id = local_id;
  1042. h.qdcount = 1;
  1043. h.rd = 1;
  1044. DPRINTF("encoding header\n", h.rd);
  1045. i = __encode_header(&h, packet, PACKETSZ);
  1046. if (i < 0)
  1047. goto fail;
  1048. /* encode question */
  1049. DPRINTF("lookup name: %s\n", lookup);
  1050. q.dotted = lookup;
  1051. q.qtype = type;
  1052. q.qclass = C_IN; /* CLASS_IN */
  1053. j = __encode_question(&q, packet+i, PACKETSZ-i);
  1054. if (j < 0)
  1055. goto fail;
  1056. len = i + j;
  1057. /* send packet */
  1058. DPRINTF("On try %d, sending query to port %d\n",
  1059. retries_left, NAMESERVER_PORT);
  1060. fd = socket(sa.sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
  1061. if (fd < 0) /* paranoia */
  1062. goto try_next_server;
  1063. rc = connect(fd, &sa.sa, sizeof(sa));
  1064. if (rc < 0) {
  1065. //if (errno == ENETUNREACH) {
  1066. /* routing error, presume not transient */
  1067. goto try_next_server;
  1068. //}
  1069. //For example, what transient error this can be? Can't think of any
  1070. ///* retry */
  1071. //continue;
  1072. }
  1073. DPRINTF("Xmit packet len:%d id:%d qr:%d\n", len, h.id, h.qr);
  1074. /* no error check - if it fails, we time out on recv */
  1075. send(fd, packet, len, 0);
  1076. #ifdef USE_SELECT
  1077. reply_timeout = REPLY_TIMEOUT;
  1078. wait_again:
  1079. FD_ZERO(&fds);
  1080. FD_SET(fd, &fds);
  1081. tv.tv_sec = reply_timeout;
  1082. tv.tv_usec = 0;
  1083. if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
  1084. DPRINTF("Timeout\n");
  1085. /* timed out, so retry send and receive
  1086. * to next nameserver */
  1087. goto try_next_server;
  1088. }
  1089. reply_timeout--;
  1090. #else
  1091. reply_timeout = REPLY_TIMEOUT * 1000;
  1092. wait_again:
  1093. fds.fd = fd;
  1094. fds.events = POLLIN;
  1095. if (poll(&fds, 1, reply_timeout) <= 0) {
  1096. DPRINTF("Timeout\n");
  1097. /* timed out, so retry send and receive
  1098. * to next nameserver */
  1099. goto try_next_server;
  1100. }
  1101. //TODO: better timeout accounting?
  1102. reply_timeout -= 1000;
  1103. #endif
  1104. len = recv(fd, packet, PACKETSZ, MSG_DONTWAIT);
  1105. if (len < HFIXEDSZ) {
  1106. /* too short!
  1107. * it's just a bogus packet from somewhere */
  1108. bogus_packet:
  1109. if (reply_timeout)
  1110. goto wait_again;
  1111. goto try_next_server;
  1112. }
  1113. __decode_header(packet, &h);
  1114. DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
  1115. if (h.id != local_id || !h.qr) {
  1116. /* unsolicited */
  1117. goto bogus_packet;
  1118. }
  1119. DPRINTF("Got response (i think)!\n");
  1120. DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
  1121. h.qdcount, h.ancount, h.nscount, h.arcount);
  1122. DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
  1123. h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
  1124. /* bug 660 says we treat negative response as an error
  1125. * and retry, which is, eh, an error. :)
  1126. * We were incurring long delays because of this. */
  1127. if (h.rcode == NXDOMAIN) {
  1128. /* if possible, try next search domain */
  1129. if (!ends_with_dot) {
  1130. DPRINTF("variant:%d sdomains:%d\n", variant, sdomains);
  1131. if (variant < sdomains - 1) {
  1132. /* next search domain */
  1133. variant++;
  1134. continue;
  1135. }
  1136. /* no more search domains to try */
  1137. }
  1138. /* dont loop, this is "no such host" situation */
  1139. h_errno = HOST_NOT_FOUND;
  1140. goto fail1;
  1141. }
  1142. if (h.rcode != 0) /* error */
  1143. goto try_next_sdomain;
  1144. /* code below won't work correctly with h.ancount == 0, so... */
  1145. if (h.ancount <= 0) {
  1146. h_errno = NO_DATA; /* is this correct code? */
  1147. goto fail1;
  1148. }
  1149. pos = HFIXEDSZ;
  1150. for (j = 0; j < h.qdcount; j++) {
  1151. DPRINTF("Skipping question %d at %d\n", j, pos);
  1152. /* returns -1 only if packet == NULL (can't happen) */
  1153. i = __length_question(packet, pos);
  1154. DPRINTF("Length of question %d is %d\n", j, i);
  1155. pos += i;
  1156. }
  1157. DPRINTF("Decoding answer at pos %d\n", pos);
  1158. first_answer = 1;
  1159. for (j = 0; j < h.ancount && pos < len; j++) {
  1160. i = __decode_answer(packet, pos, len, &ma);
  1161. if (i < 0) {
  1162. DPRINTF("failed decode %d\n", i);
  1163. /* if the message was truncated and we have
  1164. * decoded some answers, pretend it's OK */
  1165. if (j && h.tc)
  1166. break;
  1167. goto try_next_sdomain;
  1168. }
  1169. pos += i;
  1170. if (first_answer) {
  1171. ma.buf = a->buf;
  1172. ma.buflen = a->buflen;
  1173. ma.add_count = a->add_count;
  1174. memcpy(a, &ma, sizeof(ma));
  1175. if (a->atype != T_SIG && (NULL == a->buf || (type != T_A && type != T_AAAA)))
  1176. break;
  1177. if (a->atype != type) {
  1178. free(a->dotted);
  1179. continue;
  1180. }
  1181. a->add_count = h.ancount - j - 1;
  1182. if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
  1183. break;
  1184. a->add_count = 0;
  1185. first_answer = 0;
  1186. } else {
  1187. free(ma.dotted);
  1188. if (ma.atype != type)
  1189. continue;
  1190. if (a->rdlength != ma.rdlength) {
  1191. free(a->dotted);
  1192. DPRINTF("Answer address len(%u) differs from original(%u)\n",
  1193. ma.rdlength, a->rdlength);
  1194. goto try_next_sdomain;
  1195. }
  1196. memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
  1197. ++a->add_count;
  1198. }
  1199. }
  1200. /* success! */
  1201. DPRINTF("Answer name = |%s|\n", a->dotted);
  1202. DPRINTF("Answer type = |%d|\n", a->atype);
  1203. if (fd != -1)
  1204. close(fd);
  1205. if (outpacket)
  1206. *outpacket = packet;
  1207. else
  1208. free(packet);
  1209. free(lookup);
  1210. return len;
  1211. //TODO: audit whether *any* "goto try_next_sdomain" is actually correct:
  1212. //trying next search domain makes sense only if we got NXDOMAIN!
  1213. //why we do it in other cases too?
  1214. try_next_sdomain:
  1215. /* if there are searchdomains, try them */
  1216. if (!ends_with_dot) {
  1217. if (variant < sdomains - 1) {
  1218. /* next search */
  1219. variant++;
  1220. continue;
  1221. }
  1222. }
  1223. try_next_server:
  1224. /* if there are other nameservers, try them */
  1225. local_ns_num++;
  1226. variant = -1;
  1227. } while (retries_left > 0);
  1228. fail:
  1229. h_errno = NETDB_INTERNAL;
  1230. fail1:
  1231. if (fd != -1)
  1232. close(fd);
  1233. free(lookup);
  1234. free(packet);
  1235. return -1;
  1236. }
  1237. #endif
  1238. #ifdef L_read_etc_hosts_r
  1239. FILE * __open_etc_hosts(void)
  1240. {
  1241. FILE * fp;
  1242. if ((fp = fopen("/etc/hosts", "r")) == NULL) {
  1243. fp = fopen("/etc/config/hosts", "r");
  1244. }
  1245. return fp;
  1246. }
  1247. int attribute_hidden __read_etc_hosts_r(
  1248. FILE * fp,
  1249. const char * name,
  1250. int type,
  1251. enum etc_hosts_action action,
  1252. struct hostent * result_buf,
  1253. char * buf, size_t buflen,
  1254. struct hostent ** result,
  1255. int * h_errnop)
  1256. {
  1257. struct in_addr *in = NULL;
  1258. struct in_addr **addr_list = NULL;
  1259. #ifdef __UCLIBC_HAS_IPV6__
  1260. struct in6_addr *in6 = NULL;
  1261. struct in6_addr **addr_list6 = NULL;
  1262. #endif /* __UCLIBC_HAS_IPV6__ */
  1263. char *cp, **alias;
  1264. int aliases, i, ret = HOST_NOT_FOUND;
  1265. /* make sure user char * is aligned */
  1266. i = ALIGN_BUFFER_OFFSET(buf);
  1267. buf += i;
  1268. buflen -= i;
  1269. alias = (char **)buf;
  1270. buf += sizeof(char **) * ALIAS_DIM;
  1271. buflen -= sizeof(char **) * ALIAS_DIM;
  1272. if ((ssize_t)buflen < 0)
  1273. return ERANGE;
  1274. if (action != GETHOSTENT) {
  1275. #ifdef __UCLIBC_HAS_IPV6__
  1276. char *p = buf;
  1277. size_t len = buflen;
  1278. #endif
  1279. *h_errnop = NETDB_INTERNAL;
  1280. in = (struct in_addr*)buf;
  1281. buf += sizeof(*in);
  1282. buflen -= sizeof(*in);
  1283. addr_list = (struct in_addr **)buf;
  1284. buf += sizeof(*addr_list) * 2;
  1285. buflen -= sizeof(*addr_list) * 2;
  1286. if ((ssize_t)buflen < 0)
  1287. return ERANGE;
  1288. #ifdef __UCLIBC_HAS_IPV6__
  1289. in6 = (struct in6_addr*)p;
  1290. p += sizeof(*in6);
  1291. len -= sizeof(*in6);
  1292. addr_list6 = (struct in6_addr**)p;
  1293. p += sizeof(*addr_list6) * 2;
  1294. len -= sizeof(*addr_list6) * 2;
  1295. if ((ssize_t)len < 0)
  1296. return ERANGE;
  1297. if (len < buflen) {
  1298. buflen = len;
  1299. buf = p;
  1300. }
  1301. #endif
  1302. if ((ssize_t)buflen < 80)
  1303. return ERANGE;
  1304. fp = __open_etc_hosts();
  1305. if (fp == NULL) {
  1306. *result = NULL;
  1307. return errno;
  1308. }
  1309. }
  1310. *h_errnop = HOST_NOT_FOUND;
  1311. while (fgets(buf, buflen, fp)) {
  1312. cp = strchr(buf, '#');
  1313. if (cp)
  1314. *cp = '\0';
  1315. DPRINTF("Looking at: %s\n", buf);
  1316. aliases = 0;
  1317. cp = buf;
  1318. while (*cp) {
  1319. while (*cp && isspace(*cp))
  1320. *cp++ = '\0';
  1321. if (!*cp)
  1322. break;
  1323. if (aliases < (2+MAX_ALIASES))
  1324. alias[aliases++] = cp;
  1325. while (*cp && !isspace(*cp))
  1326. cp++;
  1327. }
  1328. alias[aliases] = NULL;
  1329. if (aliases < 2)
  1330. continue; /* syntax error really */
  1331. if (action == GETHOSTENT) {
  1332. /* Return whatever the next entry happens to be. */
  1333. break;
  1334. }
  1335. if (action == GET_HOSTS_BYADDR) {
  1336. if (strcmp(name, alias[0]) != 0)
  1337. continue;
  1338. } else {
  1339. /* GET_HOSTS_BYNAME */
  1340. for (i = 1; i < aliases; i++)
  1341. if (strcasecmp(name, alias[i]) == 0)
  1342. break;
  1343. if (i >= aliases)
  1344. continue;
  1345. }
  1346. if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
  1347. DPRINTF("Found INET\n");
  1348. addr_list[0] = in;
  1349. addr_list[1] = NULL;
  1350. result_buf->h_name = alias[1];
  1351. result_buf->h_addrtype = AF_INET;
  1352. result_buf->h_length = sizeof(*in);
  1353. result_buf->h_addr_list = (char**) addr_list;
  1354. result_buf->h_aliases = alias + 2;
  1355. *result = result_buf;
  1356. ret = NETDB_SUCCESS;
  1357. }
  1358. #ifdef __UCLIBC_HAS_IPV6__
  1359. else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
  1360. DPRINTF("Found INET6\n");
  1361. addr_list6[0] = in6;
  1362. addr_list6[1] = NULL;
  1363. result_buf->h_name = alias[1];
  1364. result_buf->h_addrtype = AF_INET6;
  1365. result_buf->h_length = sizeof(*in6);
  1366. result_buf->h_addr_list = (char**) addr_list6;
  1367. result_buf->h_aliases = alias + 2;
  1368. *result = result_buf;
  1369. ret = NETDB_SUCCESS;
  1370. }
  1371. #endif
  1372. else {
  1373. /* continue parsing in the hope the user has multiple
  1374. * host types listed in the database like so:
  1375. * <ipv4 addr> host
  1376. * <ipv6 addr> host
  1377. * If looking for an IPv6 addr, don't bail when we got the IPv4
  1378. */
  1379. DPRINTF("Error: Found host but diff network type\n");
  1380. /* NB: gethostbyname2_r depends on this feature
  1381. * to avoid looking for IPv6 addr of "localhost" etc */
  1382. ret = TRY_AGAIN;
  1383. continue;
  1384. }
  1385. break;
  1386. }
  1387. if (action != GETHOSTENT)
  1388. fclose(fp);
  1389. return ret;
  1390. }
  1391. #endif
  1392. #ifdef L_get_hosts_byname_r
  1393. int attribute_hidden __get_hosts_byname_r(const char * name, int type,
  1394. struct hostent * result_buf,
  1395. char * buf, size_t buflen,
  1396. struct hostent ** result,
  1397. int * h_errnop)
  1398. {
  1399. return __read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME,
  1400. result_buf, buf, buflen, result, h_errnop);
  1401. }
  1402. #endif
  1403. #ifdef L_get_hosts_byaddr_r
  1404. int attribute_hidden __get_hosts_byaddr_r(const char * addr, int len, int type,
  1405. struct hostent * result_buf,
  1406. char * buf, size_t buflen,
  1407. struct hostent ** result,
  1408. int * h_errnop)
  1409. {
  1410. #ifndef __UCLIBC_HAS_IPV6__
  1411. char ipaddr[INET_ADDRSTRLEN];
  1412. #else
  1413. char ipaddr[INET6_ADDRSTRLEN];
  1414. #endif /* __UCLIBC_HAS_IPV6__ */
  1415. switch (type) {
  1416. case AF_INET:
  1417. if (len != sizeof(struct in_addr))
  1418. return 0;
  1419. break;
  1420. #ifdef __UCLIBC_HAS_IPV6__
  1421. case AF_INET6:
  1422. if (len != sizeof(struct in6_addr))
  1423. return 0;
  1424. break;
  1425. #endif /* __UCLIBC_HAS_IPV6__ */
  1426. default:
  1427. return 0;
  1428. }
  1429. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1430. return __read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1431. result_buf, buf, buflen, result, h_errnop);
  1432. }
  1433. #endif
  1434. #ifdef L_getnameinfo
  1435. int getnameinfo(const struct sockaddr *sa, socklen_t addrlen, char *host,
  1436. socklen_t hostlen, char *serv, socklen_t servlen,
  1437. unsigned int flags)
  1438. {
  1439. int serrno = errno;
  1440. unsigned ok;
  1441. struct hostent *h = NULL;
  1442. char domain[256];
  1443. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1444. return EAI_BADFLAGS;
  1445. if (sa == NULL || addrlen < sizeof(sa_family_t))
  1446. goto BAD_FAM;
  1447. ok = sa->sa_family;
  1448. if (ok == AF_LOCAL) /* valid */;
  1449. #ifdef __UCLIBC_HAS_IPV4__
  1450. else if (ok == AF_INET) {
  1451. if (addrlen < sizeof(struct sockaddr_in))
  1452. goto BAD_FAM;
  1453. }
  1454. #endif
  1455. #ifdef __UCLIBC_HAS_IPV6__
  1456. else if (ok == AF_INET6) {
  1457. if (addrlen < sizeof(struct sockaddr_in6))
  1458. goto BAD_FAM;
  1459. }
  1460. #endif /* __UCLIBC_HAS_IPV6__ */
  1461. else
  1462. BAD_FAM:
  1463. return EAI_FAMILY;
  1464. ok = 0;
  1465. if (host != NULL && hostlen > 0)
  1466. switch (sa->sa_family) {
  1467. case AF_INET:
  1468. #ifdef __UCLIBC_HAS_IPV6__
  1469. case AF_INET6:
  1470. #endif
  1471. if (!(flags & NI_NUMERICHOST)) {
  1472. if (0) /* nothing */;
  1473. #ifdef __UCLIBC_HAS_IPV6__
  1474. else if (sa->sa_family == AF_INET6)
  1475. h = gethostbyaddr((const void *)
  1476. &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1477. sizeof(struct in6_addr), AF_INET6);
  1478. #endif
  1479. #ifdef __UCLIBC_HAS_IPV4__
  1480. else
  1481. h = gethostbyaddr((const void *)
  1482. &(((const struct sockaddr_in *)sa)->sin_addr),
  1483. sizeof(struct in_addr), AF_INET);
  1484. #endif
  1485. if (h) {
  1486. char *c;
  1487. #undef min
  1488. #define min(x,y) (((x) > (y)) ? (y) : (x))
  1489. if ((flags & NI_NOFQDN)
  1490. && (__libc_getdomainname(domain, sizeof(domain)) == 0)
  1491. && (c = strstr(h->h_name, domain)) != NULL
  1492. && (c != h->h_name) && (*(--c) == '.')
  1493. ) {
  1494. strncpy(host, h->h_name,
  1495. min(hostlen, (size_t) (c - h->h_name)));
  1496. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1497. } else {
  1498. strncpy(host, h->h_name, hostlen);
  1499. }
  1500. ok = 1;
  1501. #undef min
  1502. }
  1503. }
  1504. if (!ok) {
  1505. const char *c = NULL;
  1506. if (flags & NI_NAMEREQD) {
  1507. errno = serrno;
  1508. return EAI_NONAME;
  1509. }
  1510. if (0) /* nothing */;
  1511. #ifdef __UCLIBC_HAS_IPV6__
  1512. else if (sa->sa_family == AF_INET6) {
  1513. const struct sockaddr_in6 *sin6p;
  1514. sin6p = (const struct sockaddr_in6 *) sa;
  1515. c = inet_ntop(AF_INET6,
  1516. (const void *) &sin6p->sin6_addr,
  1517. host, hostlen);
  1518. #if 0
  1519. /* Does scope id need to be supported? */
  1520. uint32_t scopeid;
  1521. scopeid = sin6p->sin6_scope_id;
  1522. if (scopeid != 0) {
  1523. /* Buffer is >= IFNAMSIZ+1. */
  1524. char scopebuf[IFNAMSIZ + 1];
  1525. char *scopeptr;
  1526. int ni_numericscope = 0;
  1527. size_t real_hostlen = strnlen(host, hostlen);
  1528. size_t scopelen = 0;
  1529. scopebuf[0] = SCOPE_DELIMITER;
  1530. scopebuf[1] = '\0';
  1531. scopeptr = &scopebuf[1];
  1532. if (IN6_IS_ADDR_LINKLOCAL(&sin6p->sin6_addr)
  1533. || IN6_IS_ADDR_MC_LINKLOCAL(&sin6p->sin6_addr)) {
  1534. if (if_indextoname(scopeid, scopeptr) == NULL)
  1535. ++ni_numericscope;
  1536. else
  1537. scopelen = strlen(scopebuf);
  1538. } else {
  1539. ++ni_numericscope;
  1540. }
  1541. if (ni_numericscope)
  1542. scopelen = 1 + snprintf(scopeptr,
  1543. (scopebuf
  1544. + sizeof scopebuf
  1545. - scopeptr),
  1546. "%u", scopeid);
  1547. if (real_hostlen + scopelen + 1 > hostlen)
  1548. return EAI_SYSTEM;
  1549. memcpy(host + real_hostlen, scopebuf, scopelen + 1);
  1550. }
  1551. #endif
  1552. }
  1553. #endif /* __UCLIBC_HAS_IPV6__ */
  1554. #if defined __UCLIBC_HAS_IPV4__
  1555. else {
  1556. c = inet_ntop(AF_INET, (const void *)
  1557. &(((const struct sockaddr_in *) sa)->sin_addr),
  1558. host, hostlen);
  1559. }
  1560. #endif
  1561. if (c == NULL) {
  1562. errno = serrno;
  1563. return EAI_SYSTEM;
  1564. }
  1565. ok = 1;
  1566. }
  1567. break;
  1568. case AF_LOCAL:
  1569. if (!(flags & NI_NUMERICHOST)) {
  1570. struct utsname utsname;
  1571. if (!uname(&utsname)) {
  1572. strncpy(host, utsname.nodename, hostlen);
  1573. break;
  1574. };
  1575. };
  1576. if (flags & NI_NAMEREQD) {
  1577. errno = serrno;
  1578. return EAI_NONAME;
  1579. }
  1580. strncpy(host, "localhost", hostlen);
  1581. break;
  1582. /* Already checked above
  1583. default:
  1584. return EAI_FAMILY;
  1585. */
  1586. }
  1587. if (serv && (servlen > 0)) {
  1588. if (sa->sa_family == AF_LOCAL) {
  1589. strncpy(serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1590. } else { /* AF_INET || AF_INET6 */
  1591. if (!(flags & NI_NUMERICSERV)) {
  1592. struct servent *s;
  1593. s = getservbyport(((const struct sockaddr_in *) sa)->sin_port,
  1594. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1595. if (s) {
  1596. strncpy(serv, s->s_name, servlen);
  1597. goto DONE;
  1598. }
  1599. }
  1600. snprintf(serv, servlen, "%d",
  1601. ntohs(((const struct sockaddr_in *) sa)->sin_port));
  1602. }
  1603. }
  1604. DONE:
  1605. if (host && (hostlen > 0))
  1606. host[hostlen-1] = 0;
  1607. if (serv && (servlen > 0))
  1608. serv[servlen-1] = 0;
  1609. errno = serrno;
  1610. return 0;
  1611. }
  1612. libc_hidden_def(getnameinfo)
  1613. #endif
  1614. #ifdef L_gethostbyname_r
  1615. /* Bug 671 says:
  1616. * "uClibc resolver's gethostbyname does not return the requested name
  1617. * as an alias, but instead returns the canonical name. glibc's
  1618. * gethostbyname has a similar bug where it returns the requested name
  1619. * with the search domain name appended (to make a FQDN) as an alias,
  1620. * but not the original name itself. Both contradict POSIX, which says
  1621. * that the name argument passed to gethostbyname must be in the alias list"
  1622. * This is fixed now, and we differ from glibc:
  1623. *
  1624. * $ ./gethostbyname_uclibc wer.google.com
  1625. * h_name:'c13-ss-2-lb.cnet.com'
  1626. * h_length:4
  1627. * h_addrtype:2 AF_INET
  1628. * alias:'wer.google.com' <===
  1629. * addr: 0x4174efd8 '216.239.116.65'
  1630. *
  1631. * $ ./gethostbyname_glibc wer.google.com
  1632. * h_name:'c13-ss-2-lb.cnet.com'
  1633. * h_length:4
  1634. * h_addrtype:2 AF_INET
  1635. * alias:'wer.google.com.com' <===
  1636. * addr:'216.239.116.65'
  1637. *
  1638. * When examples were run, /etc/resolv.conf contained "search com" line.
  1639. */
  1640. int gethostbyname_r(const char * name,
  1641. struct hostent * result_buf,
  1642. char * buf,
  1643. size_t buflen,
  1644. struct hostent ** result,
  1645. int * h_errnop)
  1646. {
  1647. struct in_addr **addr_list;
  1648. char **alias;
  1649. char *alias0;
  1650. unsigned char *packet;
  1651. struct resolv_answer a;
  1652. int i;
  1653. int wrong_af = 0;
  1654. *result = NULL;
  1655. if (!name)
  1656. return EINVAL;
  1657. /* do /etc/hosts first */
  1658. {
  1659. int old_errno = errno; /* save the old errno and reset errno */
  1660. __set_errno(0); /* to check for missing /etc/hosts. */
  1661. i = __get_hosts_byname_r(name, AF_INET, result_buf,
  1662. buf, buflen, result, h_errnop);
  1663. if (i == NETDB_SUCCESS) {
  1664. __set_errno(old_errno);
  1665. return i;
  1666. }
  1667. switch (*h_errnop) {
  1668. case HOST_NOT_FOUND:
  1669. wrong_af = (i == TRY_AGAIN);
  1670. case NO_ADDRESS:
  1671. break;
  1672. case NETDB_INTERNAL:
  1673. if (errno == ENOENT) {
  1674. break;
  1675. }
  1676. /* else fall through */
  1677. default:
  1678. return i;
  1679. }
  1680. __set_errno(old_errno);
  1681. }
  1682. DPRINTF("Nothing found in /etc/hosts\n");
  1683. *h_errnop = NETDB_INTERNAL;
  1684. /* prepare future h_aliases[0] */
  1685. i = strlen(name) + 1;
  1686. if ((ssize_t)buflen <= i)
  1687. return ERANGE;
  1688. strcpy(buf, name);
  1689. alias0 = buf;
  1690. buf += i;
  1691. buflen -= i;
  1692. /* make sure pointer is aligned */
  1693. i = ALIGN_BUFFER_OFFSET(buf);
  1694. buf += i;
  1695. buflen -= i;
  1696. alias = (char **)buf;
  1697. buf += sizeof(alias[0]) * 2;
  1698. buflen -= sizeof(alias[0]) * 2;
  1699. addr_list = (struct in_addr **)buf;
  1700. /* do not use *buf or buflen before this verification! */
  1701. /* buflen may be < 0, must do signed compare */
  1702. if ((ssize_t)buflen < 256)
  1703. return ERANGE;
  1704. /* we store only one "alias" - the name itself */
  1705. #ifdef __UCLIBC_MJN3_ONLY__
  1706. #warning TODO -- generate the full list
  1707. #endif
  1708. alias[0] = alias0;
  1709. alias[1] = NULL;
  1710. /* maybe it is already an address? */
  1711. {
  1712. struct in_addr *in = (struct in_addr *)(buf + sizeof(addr_list[0]) * 2);
  1713. if (inet_aton(name, in)) {
  1714. addr_list[0] = in;
  1715. addr_list[1] = NULL;
  1716. result_buf->h_name = alias0;
  1717. result_buf->h_aliases = alias;
  1718. result_buf->h_addrtype = AF_INET;
  1719. result_buf->h_length = sizeof(struct in_addr);
  1720. result_buf->h_addr_list = (char **) addr_list;
  1721. *result = result_buf;
  1722. *h_errnop = NETDB_SUCCESS;
  1723. return NETDB_SUCCESS;
  1724. }
  1725. }
  1726. /* what if /etc/hosts has it but it's not IPv4?
  1727. * F.e. "::1 localhost6". We don't do DNS query for such hosts -
  1728. * "ping localhost6" should be fast even if DNS server is down! */
  1729. if (wrong_af) {
  1730. *h_errnop = HOST_NOT_FOUND;
  1731. return TRY_AGAIN;
  1732. }
  1733. /* talk to DNS servers */
  1734. {
  1735. a.buf = buf;
  1736. /* take into account that at least one address will be there,
  1737. * we'll need space of one in_addr + two addr_list[] elems */
  1738. a.buflen = buflen - ((sizeof(addr_list[0]) * 2 + sizeof(struct in_addr)));
  1739. a.add_count = 0;
  1740. i = __dns_lookup(name, T_A, &packet, &a);
  1741. if (i < 0) {
  1742. *h_errnop = HOST_NOT_FOUND;
  1743. DPRINTF("__dns_lookup returned < 0\n");
  1744. return TRY_AGAIN;
  1745. }
  1746. }
  1747. if (a.atype == T_A) { /* ADDRESS */
  1748. /* we need space for addr_list[] and one IPv4 address */
  1749. /* + 1 accounting for 1st addr (it's in a.rdata),
  1750. * another + 1 for NULL in last addr_list[]: */
  1751. int need_bytes = sizeof(addr_list[0]) * (a.add_count + 1 + 1)
  1752. /* for 1st addr (it's in a.rdata): */
  1753. + sizeof(struct in_addr);
  1754. /* how many bytes will 2nd and following addresses take? */
  1755. int ips_len = a.add_count * a.rdlength;
  1756. buflen -= (need_bytes + ips_len);
  1757. if ((ssize_t)buflen < 0) {
  1758. DPRINTF("buffer too small for all addresses\n");
  1759. /* *h_errnop = NETDB_INTERNAL; - already is */
  1760. i = ERANGE;
  1761. goto free_and_ret;
  1762. }
  1763. /* if there are additional addresses in buf,
  1764. * move them forward so that they are not destroyed */
  1765. DPRINTF("a.add_count:%d a.rdlength:%d a.rdata:%p\n", a.add_count, a.rdlength, a.rdata);
  1766. memmove(buf + need_bytes, buf, ips_len);
  1767. /* 1st address is in a.rdata, insert it */
  1768. buf += need_bytes - sizeof(struct in_addr);
  1769. memcpy(buf, a.rdata, sizeof(struct in_addr));
  1770. /* fill addr_list[] */
  1771. for (i = 0; i <= a.add_count; i++) {
  1772. addr_list[i] = (struct in_addr*)buf;
  1773. buf += sizeof(struct in_addr);
  1774. }
  1775. addr_list[i] = NULL;
  1776. /* if we have enough space, we can report "better" name
  1777. * (it may contain search domains attached by __dns_lookup,
  1778. * or CNAME of the host if it is different from the name
  1779. * we used to find it) */
  1780. if (a.dotted && buflen > strlen(a.dotted)) {
  1781. strcpy(buf, a.dotted);
  1782. alias0 = buf;
  1783. }
  1784. result_buf->h_name = alias0;
  1785. result_buf->h_aliases = alias;
  1786. result_buf->h_addrtype = AF_INET;
  1787. result_buf->h_length = sizeof(struct in_addr);
  1788. result_buf->h_addr_list = (char **) addr_list;
  1789. *result = result_buf;
  1790. *h_errnop = NETDB_SUCCESS;
  1791. i = NETDB_SUCCESS;
  1792. goto free_and_ret;
  1793. }
  1794. *h_errnop = HOST_NOT_FOUND;
  1795. i = TRY_AGAIN;
  1796. free_and_ret:
  1797. free(a.dotted);
  1798. free(packet);
  1799. return i;
  1800. }
  1801. libc_hidden_def(gethostbyname_r)
  1802. #endif
  1803. #ifdef L_gethostbyname2_r
  1804. int gethostbyname2_r(const char *name,
  1805. int family,
  1806. struct hostent * result_buf,
  1807. char * buf,
  1808. size_t buflen,
  1809. struct hostent ** result,
  1810. int * h_errnop)
  1811. {
  1812. #ifndef __UCLIBC_HAS_IPV6__
  1813. return family == (AF_INET)
  1814. ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop)
  1815. : HOST_NOT_FOUND;
  1816. #else
  1817. struct in6_addr *in;
  1818. struct in6_addr **addr_list;
  1819. unsigned char *packet;
  1820. struct resolv_answer a;
  1821. int i;
  1822. int nest = 0;
  1823. int wrong_af = 0;
  1824. if (family == AF_INET)
  1825. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1826. if (family != AF_INET6)
  1827. return EINVAL;
  1828. *result = NULL;
  1829. if (!name)
  1830. return EINVAL;
  1831. /* do /etc/hosts first */
  1832. {
  1833. int old_errno = errno; /* Save the old errno and reset errno */
  1834. __set_errno(0); /* to check for missing /etc/hosts. */
  1835. i = __get_hosts_byname_r(name, family, result_buf,
  1836. buf, buflen, result, h_errnop);
  1837. if (i == NETDB_SUCCESS) {
  1838. __set_errno(old_errno);
  1839. return i;
  1840. }
  1841. switch (*h_errnop) {
  1842. case HOST_NOT_FOUND:
  1843. wrong_af = (i == TRY_AGAIN);
  1844. case NO_ADDRESS:
  1845. break;
  1846. case NETDB_INTERNAL:
  1847. if (errno == ENOENT) {
  1848. break;
  1849. }
  1850. /* else fall through */
  1851. default:
  1852. return i;
  1853. }
  1854. __set_errno(old_errno);
  1855. }
  1856. DPRINTF("Nothing found in /etc/hosts\n");
  1857. *h_errnop = NETDB_INTERNAL;
  1858. /* make sure pointer is aligned */
  1859. i = ALIGN_BUFFER_OFFSET(buf);
  1860. buf += i;
  1861. buflen -= i;
  1862. in = (struct in6_addr*)buf;
  1863. buf += sizeof(*in);
  1864. buflen -= sizeof(*in);
  1865. addr_list = (struct in6_addr**)buf;
  1866. buf += sizeof(*addr_list) * 2;
  1867. buflen -= sizeof(*addr_list) * 2;
  1868. if ((ssize_t)buflen < 256)
  1869. return ERANGE;
  1870. addr_list[0] = in;
  1871. addr_list[1] = NULL;
  1872. strncpy(buf, name, buflen);
  1873. /* maybe it is already an address? */
  1874. if (inet_pton(AF_INET6, name, in)) {
  1875. result_buf->h_name = buf;
  1876. result_buf->h_addrtype = AF_INET6;
  1877. result_buf->h_length = sizeof(*in);
  1878. result_buf->h_addr_list = (char **) addr_list;
  1879. *result = result_buf;
  1880. *h_errnop = NETDB_SUCCESS;
  1881. return NETDB_SUCCESS;
  1882. }
  1883. /* what if /etc/hosts has it but it's not IPv6?
  1884. * F.e. "127.0.0.1 localhost". We don't do DNS query for such hosts -
  1885. * "ping localhost" should be fast even if DNS server is down! */
  1886. if (wrong_af) {
  1887. *h_errnop = HOST_NOT_FOUND;
  1888. return TRY_AGAIN;
  1889. }
  1890. /* talk to DNS servers */
  1891. // TODO: why it's so different from gethostbyname_r (IPv4 case)?
  1892. memset(&a, '\0', sizeof(a));
  1893. for (;;) {
  1894. i = __dns_lookup(buf, T_AAAA, &packet, &a);
  1895. if (i < 0) {
  1896. *h_errnop = HOST_NOT_FOUND;
  1897. return TRY_AGAIN;
  1898. }
  1899. strncpy(buf, a.dotted, buflen);
  1900. free(a.dotted);
  1901. if (a.atype == T_CNAME) { /* CNAME */
  1902. DPRINTF("Got a CNAME in gethostbyname()\n");
  1903. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1904. free(packet);
  1905. if (i < 0) {
  1906. *h_errnop = NO_RECOVERY;
  1907. return -1;
  1908. }
  1909. if (++nest > MAX_RECURSE) {
  1910. *h_errnop = NO_RECOVERY;
  1911. return -1;
  1912. }
  1913. continue;
  1914. }
  1915. if (a.atype == T_AAAA) { /* ADDRESS */
  1916. memcpy(in, a.rdata, sizeof(*in));
  1917. result_buf->h_name = buf;
  1918. result_buf->h_addrtype = AF_INET6;
  1919. result_buf->h_length = sizeof(*in);
  1920. result_buf->h_addr_list = (char **) addr_list;
  1921. free(packet);
  1922. break;
  1923. }
  1924. free(packet);
  1925. *h_errnop = HOST_NOT_FOUND;
  1926. return TRY_AGAIN;
  1927. }
  1928. *result = result_buf;
  1929. *h_errnop = NETDB_SUCCESS;
  1930. return NETDB_SUCCESS;
  1931. #endif /* __UCLIBC_HAS_IPV6__ */
  1932. }
  1933. libc_hidden_def(gethostbyname2_r)
  1934. #endif
  1935. #ifdef L_gethostbyaddr_r
  1936. int gethostbyaddr_r(const void *addr, socklen_t len, int type,
  1937. struct hostent * result_buf,
  1938. char * buf, size_t buflen,
  1939. struct hostent ** result,
  1940. int * h_errnop)
  1941. {
  1942. struct in_addr *in;
  1943. struct in_addr **addr_list;
  1944. #ifdef __UCLIBC_HAS_IPV6__
  1945. char *qp;
  1946. size_t plen;
  1947. struct in6_addr *in6;
  1948. struct in6_addr **addr_list6;
  1949. #endif /* __UCLIBC_HAS_IPV6__ */
  1950. char **alias;
  1951. unsigned char *packet;
  1952. struct resolv_answer a;
  1953. int i;
  1954. int nest = 0;
  1955. *result = NULL;
  1956. if (!addr)
  1957. return EINVAL;
  1958. memset(&a, '\0', sizeof(a));
  1959. switch (type) {
  1960. case AF_INET:
  1961. if (len != sizeof(struct in_addr))
  1962. return EINVAL;
  1963. break;
  1964. #ifdef __UCLIBC_HAS_IPV6__
  1965. case AF_INET6:
  1966. if (len != sizeof(struct in6_addr))
  1967. return EINVAL;
  1968. break;
  1969. #endif /* __UCLIBC_HAS_IPV6__ */
  1970. default:
  1971. return EINVAL;
  1972. }
  1973. /* do /etc/hosts first */
  1974. i = __get_hosts_byaddr_r(addr, len, type, result_buf,
  1975. buf, buflen, result, h_errnop);
  1976. if (i == 0)
  1977. return i;
  1978. switch (*h_errnop) {
  1979. case HOST_NOT_FOUND:
  1980. case NO_ADDRESS:
  1981. break;
  1982. default:
  1983. return i;
  1984. }
  1985. *h_errnop = NETDB_INTERNAL;
  1986. /* make sure pointer is aligned */
  1987. i = ALIGN_BUFFER_OFFSET(buf);
  1988. buf += i;
  1989. buflen -= i;
  1990. #ifdef __UCLIBC_HAS_IPV6__
  1991. qp = buf;
  1992. plen = buflen;
  1993. #endif
  1994. in = (struct in_addr*)buf;
  1995. buf += sizeof(*in);
  1996. buflen -= sizeof(*in);
  1997. addr_list = (struct in_addr**)buf;
  1998. buf += sizeof(*addr_list) * 2;
  1999. buflen -= sizeof(*addr_list) * 2;
  2000. alias = (char **)buf;
  2001. buf += sizeof(*alias) * ALIAS_DIM;
  2002. buflen -= sizeof(*alias) * ALIAS_DIM;
  2003. if ((ssize_t)buflen < 0)
  2004. return ERANGE;
  2005. #ifdef __UCLIBC_HAS_IPV6__
  2006. in6 = (struct in6_addr*)qp;
  2007. qp += sizeof(*in6);
  2008. plen -= sizeof(*in6);
  2009. addr_list6 = (struct in6_addr**)qp;
  2010. qp += sizeof(*addr_list6) * 2;
  2011. plen -= sizeof(*addr_list6) * 2;
  2012. if ((ssize_t)plen < 0)
  2013. return ERANGE;
  2014. if (plen < buflen) {
  2015. buflen = plen;
  2016. buf = qp;
  2017. }
  2018. #endif
  2019. if (buflen < 256)
  2020. return ERANGE;
  2021. if (type == AF_INET) {
  2022. unsigned char *tmp_addr = (unsigned char *)addr;
  2023. memcpy(&in->s_addr, addr, len);
  2024. addr_list[0] = in;
  2025. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  2026. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  2027. }
  2028. #ifdef __UCLIBC_HAS_IPV6__
  2029. else {
  2030. memcpy(in6->s6_addr, addr, len);
  2031. addr_list6[0] = in6;
  2032. qp = buf;
  2033. for (i = len - 1; i >= 0; i--) {
  2034. qp += sprintf(qp, "%x.%x.",
  2035. in6->s6_addr[i] & 0xf,
  2036. (in6->s6_addr[i] >> 4) & 0xf);
  2037. }
  2038. strcpy(qp, "ip6.arpa");
  2039. }
  2040. #endif
  2041. addr_list[1] = NULL;
  2042. alias[0] = buf;
  2043. alias[1] = NULL;
  2044. for (;;) {
  2045. i = __dns_lookup(buf, T_PTR, &packet, &a);
  2046. if (i < 0) {
  2047. *h_errnop = HOST_NOT_FOUND;
  2048. return TRY_AGAIN;
  2049. }
  2050. strncpy(buf, a.dotted, buflen);
  2051. free(a.dotted);
  2052. if (a.atype == T_CNAME) { /* CNAME */
  2053. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  2054. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  2055. free(packet);
  2056. if (i < 0) {
  2057. *h_errnop = NO_RECOVERY;
  2058. return -1;
  2059. }
  2060. if (++nest > MAX_RECURSE) {
  2061. *h_errnop = NO_RECOVERY;
  2062. return -1;
  2063. }
  2064. continue;
  2065. }
  2066. if (a.atype == T_PTR) { /* ADDRESS */
  2067. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  2068. free(packet);
  2069. result_buf->h_name = buf;
  2070. result_buf->h_addrtype = type;
  2071. if (type == AF_INET)
  2072. result_buf->h_length = sizeof(*in);
  2073. #ifdef __UCLIBC_HAS_IPV6__
  2074. else
  2075. result_buf->h_length = sizeof(*in6);
  2076. #endif
  2077. result_buf->h_addr_list = (char **) addr_list;
  2078. result_buf->h_aliases = alias;
  2079. break;
  2080. }
  2081. free(packet);
  2082. *h_errnop = NO_ADDRESS;
  2083. return TRY_AGAIN;
  2084. }
  2085. *result = result_buf;
  2086. *h_errnop = NETDB_SUCCESS;
  2087. return NETDB_SUCCESS;
  2088. }
  2089. libc_hidden_def(gethostbyaddr_r)
  2090. #endif
  2091. #ifdef L_gethostent_r
  2092. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  2093. static smallint __stay_open;
  2094. static FILE * __gethostent_fp;
  2095. void endhostent(void)
  2096. {
  2097. __UCLIBC_MUTEX_LOCK(mylock);
  2098. __stay_open = 0;
  2099. if (__gethostent_fp) {
  2100. fclose(__gethostent_fp);
  2101. __gethostent_fp = NULL;
  2102. }
  2103. __UCLIBC_MUTEX_UNLOCK(mylock);
  2104. }
  2105. void sethostent(int stay_open)
  2106. {
  2107. __UCLIBC_MUTEX_LOCK(mylock);
  2108. __stay_open = (stay_open != 0);
  2109. __UCLIBC_MUTEX_UNLOCK(mylock);
  2110. }
  2111. int gethostent_r(struct hostent *result_buf, char *buf, size_t buflen,
  2112. struct hostent **result, int *h_errnop)
  2113. {
  2114. int ret;
  2115. __UCLIBC_MUTEX_LOCK(mylock);
  2116. if (__gethostent_fp == NULL) {
  2117. __gethostent_fp = __open_etc_hosts();
  2118. if (__gethostent_fp == NULL) {
  2119. *result = NULL;
  2120. ret = TRY_AGAIN;
  2121. goto DONE;
  2122. }
  2123. }
  2124. ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  2125. result_buf, buf, buflen, result, h_errnop);
  2126. if (__stay_open == 0) {
  2127. fclose(__gethostent_fp);
  2128. __gethostent_fp = NULL;
  2129. }
  2130. DONE:
  2131. __UCLIBC_MUTEX_UNLOCK(mylock);
  2132. return ret;
  2133. }
  2134. libc_hidden_def(gethostent_r)
  2135. #endif
  2136. #ifdef L_gethostent
  2137. struct hostent *gethostent(void)
  2138. {
  2139. static struct hostent h;
  2140. static char buf[
  2141. #ifndef __UCLIBC_HAS_IPV6__
  2142. sizeof(struct in_addr) + sizeof(struct in_addr *) * 2 +
  2143. #else
  2144. sizeof(struct in6_addr) + sizeof(struct in6_addr *) * 2 +
  2145. #endif /* __UCLIBC_HAS_IPV6__ */
  2146. sizeof(char *) * ALIAS_DIM +
  2147. 80 /*namebuffer*/ + 2 /* margin */];
  2148. struct hostent *host;
  2149. gethostent_r(&h, buf, sizeof(buf), &host, &h_errno);
  2150. return host;
  2151. }
  2152. #endif
  2153. #ifdef L_gethostbyname
  2154. struct hostent *gethostbyname(const char *name)
  2155. {
  2156. static struct hostent h;
  2157. static char buf[sizeof(struct in_addr) +
  2158. sizeof(struct in_addr *) * 2 +
  2159. sizeof(char *)*ALIAS_DIM + 384/*namebuffer*/ + 32/* margin */];
  2160. struct hostent *hp;
  2161. gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
  2162. return hp;
  2163. }
  2164. libc_hidden_def(gethostbyname)
  2165. #endif
  2166. #ifdef L_gethostbyname2
  2167. struct hostent *gethostbyname2(const char *name, int family)
  2168. {
  2169. #ifndef __UCLIBC_HAS_IPV6__
  2170. return family == AF_INET ? gethostbyname(name) : (struct hostent*)NULL;
  2171. #else
  2172. static struct hostent h;
  2173. static char buf[sizeof(struct in6_addr) +
  2174. sizeof(struct in6_addr *) * 2 +
  2175. sizeof(char *)*ALIAS_DIM + 384/*namebuffer*/ + 32/* margin */];
  2176. struct hostent *hp;
  2177. gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
  2178. return hp;
  2179. #endif
  2180. }
  2181. #endif
  2182. #ifdef L_gethostbyaddr
  2183. struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type)
  2184. {
  2185. static struct hostent h;
  2186. static char buf[
  2187. #ifndef __UCLIBC_HAS_IPV6__
  2188. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  2189. #else
  2190. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  2191. #endif /* __UCLIBC_HAS_IPV6__ */
  2192. sizeof(char *)*ALIAS_DIM + 384 /*namebuffer*/ + 32 /* margin */];
  2193. struct hostent *hp;
  2194. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  2195. return hp;
  2196. }
  2197. libc_hidden_def(gethostbyaddr)
  2198. #endif
  2199. #ifdef L_res_comp
  2200. /*
  2201. * Expand compressed domain name 'comp_dn' to full domain name.
  2202. * 'msg' is a pointer to the begining of the message,
  2203. * 'eomorig' points to the first location after the message,
  2204. * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  2205. * Return size of compressed name or -1 if there was an error.
  2206. */
  2207. int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  2208. char *dst, int dstsiz)
  2209. {
  2210. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  2211. if (n > 0 && dst[0] == '.')
  2212. dst[0] = '\0';
  2213. return n;
  2214. }
  2215. #endif /* L_res_comp */
  2216. #ifdef L_ns_name
  2217. /* Thinking in noninternationalized USASCII (per the DNS spec),
  2218. * is this character visible and not a space when printed ?
  2219. */
  2220. static int printable(int ch)
  2221. {
  2222. return (ch > 0x20 && ch < 0x7f);
  2223. }
  2224. /* Thinking in noninternationalized USASCII (per the DNS spec),
  2225. * is this characted special ("in need of quoting") ?
  2226. */
  2227. static int special(int ch)
  2228. {
  2229. switch (ch) {
  2230. case 0x22: /* '"' */
  2231. case 0x2E: /* '.' */
  2232. case 0x3B: /* ';' */
  2233. case 0x5C: /* '\\' */
  2234. /* Special modifiers in zone files. */
  2235. case 0x40: /* '@' */
  2236. case 0x24: /* '$' */
  2237. return 1;
  2238. default:
  2239. return 0;
  2240. }
  2241. }
  2242. /*
  2243. * ns_name_uncompress(msg, eom, src, dst, dstsiz)
  2244. * Expand compressed domain name to presentation format.
  2245. * return:
  2246. * Number of bytes read out of `src', or -1 (with errno set).
  2247. * note:
  2248. * Root domain returns as "." not "".
  2249. */
  2250. int ns_name_uncompress(const u_char *msg, const u_char *eom,
  2251. const u_char *src, char *dst, size_t dstsiz)
  2252. {
  2253. u_char tmp[NS_MAXCDNAME];
  2254. int n;
  2255. n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp);
  2256. if (n == -1)
  2257. return -1;
  2258. if (ns_name_ntop(tmp, dst, dstsiz) == -1)
  2259. return -1;
  2260. return n;
  2261. }
  2262. libc_hidden_def(ns_name_uncompress)
  2263. /*
  2264. * ns_name_ntop(src, dst, dstsiz)
  2265. * Convert an encoded domain name to printable ascii as per RFC1035.
  2266. * return:
  2267. * Number of bytes written to buffer, or -1 (with errno set)
  2268. * notes:
  2269. * The root is returned as "."
  2270. * All other domains are returned in non absolute form
  2271. */
  2272. int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
  2273. {
  2274. static const char digits[] = "0123456789";
  2275. const u_char *cp;
  2276. char *dn, *eom;
  2277. u_char c;
  2278. u_int n;
  2279. cp = src;
  2280. dn = dst;
  2281. eom = dst + dstsiz;
  2282. while ((n = *cp++) != 0) {
  2283. if ((n & NS_CMPRSFLGS) != 0) {
  2284. /* Some kind of compression pointer. */
  2285. __set_errno(EMSGSIZE);
  2286. return -1;
  2287. }
  2288. if (dn != dst) {
  2289. if (dn >= eom) {
  2290. __set_errno(EMSGSIZE);
  2291. return -1;
  2292. }
  2293. *dn++ = '.';
  2294. }
  2295. if (dn + n >= eom) {
  2296. __set_errno(EMSGSIZE);
  2297. return -1;
  2298. }
  2299. for ((void)NULL; n > 0; n--) {
  2300. c = *cp++;
  2301. if (special(c)) {
  2302. if (dn + 1 >= eom) {
  2303. __set_errno(EMSGSIZE);
  2304. return -1;
  2305. }
  2306. *dn++ = '\\';
  2307. *dn++ = (char)c;
  2308. } else if (!printable(c)) {
  2309. if (dn + 3 >= eom) {
  2310. __set_errno(EMSGSIZE);
  2311. return -1;
  2312. }
  2313. *dn++ = '\\';
  2314. *dn++ = digits[c / 100];
  2315. *dn++ = digits[(c % 100) / 10];
  2316. *dn++ = digits[c % 10];
  2317. } else {
  2318. if (dn >= eom) {
  2319. __set_errno(EMSGSIZE);
  2320. return -1;
  2321. }
  2322. *dn++ = (char)c;
  2323. }
  2324. }
  2325. }
  2326. if (dn == dst) {
  2327. if (dn >= eom) {
  2328. __set_errno(EMSGSIZE);
  2329. return -1;
  2330. }
  2331. *dn++ = '.';
  2332. }
  2333. if (dn >= eom) {
  2334. __set_errno(EMSGSIZE);
  2335. return -1;
  2336. }
  2337. *dn++ = '\0';
  2338. return (dn - dst);
  2339. }
  2340. libc_hidden_def(ns_name_ntop)
  2341. /*
  2342. * ns_name_unpack(msg, eom, src, dst, dstsiz)
  2343. * Unpack a domain name from a message, source may be compressed.
  2344. * return:
  2345. * -1 if it fails, or consumed octets if it succeeds.
  2346. */
  2347. int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
  2348. u_char *dst, size_t dstsiz)
  2349. {
  2350. const u_char *srcp, *dstlim;
  2351. u_char *dstp;
  2352. int n, len, checked;
  2353. len = -1;
  2354. checked = 0;
  2355. dstp = dst;
  2356. srcp = src;
  2357. dstlim = dst + dstsiz;
  2358. if (srcp < msg || srcp >= eom) {
  2359. __set_errno(EMSGSIZE);
  2360. return -1;
  2361. }
  2362. /* Fetch next label in domain name. */
  2363. while ((n = *srcp++) != 0) {
  2364. /* Check for indirection. */
  2365. switch (n & NS_CMPRSFLGS) {
  2366. case 0:
  2367. /* Limit checks. */
  2368. if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  2369. __set_errno(EMSGSIZE);
  2370. return -1;
  2371. }
  2372. checked += n + 1;
  2373. *dstp++ = n;
  2374. memcpy(dstp, srcp, n);
  2375. dstp += n;
  2376. srcp += n;
  2377. break;
  2378. case NS_CMPRSFLGS:
  2379. if (srcp >= eom) {
  2380. __set_errno(EMSGSIZE);
  2381. return -1;
  2382. }
  2383. if (len < 0)
  2384. len = srcp - src + 1;
  2385. srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  2386. if (srcp < msg || srcp >= eom) { /* Out of range. */
  2387. __set_errno(EMSGSIZE);
  2388. return -1;
  2389. }
  2390. checked += 2;
  2391. /*
  2392. * Check for loops in the compressed name;
  2393. * if we've looked at the whole message,
  2394. * there must be a loop.
  2395. */
  2396. if (checked >= eom - msg) {
  2397. __set_errno(EMSGSIZE);
  2398. return -1;
  2399. }
  2400. break;
  2401. default:
  2402. __set_errno(EMSGSIZE);
  2403. return -1; /* flag error */
  2404. }
  2405. }
  2406. *dstp = '\0';
  2407. if (len < 0)
  2408. len = srcp - src;
  2409. return len;
  2410. }
  2411. libc_hidden_def(ns_name_unpack)
  2412. #endif /* L_ns_name */
  2413. #ifdef L_res_init
  2414. /* Protected by __resolv_lock */
  2415. struct __res_state _res;
  2416. /* Will be called under __resolv_lock. */
  2417. static void res_sync_func(void)
  2418. {
  2419. struct __res_state *rp = &(_res);
  2420. int n;
  2421. /* If we didn't get malloc failure earlier... */
  2422. if (__nameserver != (void*) &__local_nameserver) {
  2423. /* TODO:
  2424. * if (__nameservers < rp->nscount) - try to grow __nameserver[]?
  2425. */
  2426. #ifdef __UCLIBC_HAS_IPV6__
  2427. if (__nameservers > rp->_u._ext.nscount)
  2428. __nameservers = rp->_u._ext.nscount;
  2429. n = __nameservers;
  2430. while (--n >= 0)
  2431. __nameserver[n].sa6 = *rp->_u._ext.nsaddrs[n]; /* struct copy */
  2432. #else /* IPv4 only */
  2433. if (__nameservers > rp->nscount)
  2434. __nameservers = rp->nscount;
  2435. n = __nameservers;
  2436. while (--n >= 0)
  2437. __nameserver[n].sa4 = rp->nsaddr_list[n]; /* struct copy */
  2438. #endif
  2439. }
  2440. /* Extend and comment what program is known
  2441. * to use which _res.XXX member(s).
  2442. */
  2443. // __resolv_opts = rp->options;
  2444. // ...
  2445. }
  2446. /* Our res_init never fails (always returns 0) */
  2447. int res_init(void)
  2448. {
  2449. struct __res_state *rp = &(_res);
  2450. int i;
  2451. int n;
  2452. #ifdef __UCLIBC_HAS_IPV6__
  2453. int m = 0;
  2454. #endif
  2455. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  2456. __close_nameservers();
  2457. __open_nameservers();
  2458. __res_sync = res_sync_func;
  2459. memset(rp, 0, sizeof(*rp));
  2460. rp->options = RES_INIT;
  2461. #ifdef __UCLIBC_HAS_COMPAT_RES_STATE__
  2462. rp->retrans = RES_TIMEOUT;
  2463. rp->retry = 4;
  2464. rp->id = random();
  2465. #endif
  2466. rp->ndots = 1;
  2467. #ifdef __UCLIBC_HAS_EXTRA_COMPAT_RES_STATE__
  2468. rp->_vcsock = -1;
  2469. #endif
  2470. n = __searchdomains;
  2471. if (n > ARRAY_SIZE(rp->dnsrch))
  2472. n = ARRAY_SIZE(rp->dnsrch);
  2473. for (i = 0; i < n; i++)
  2474. rp->dnsrch[i] = __searchdomain[i];
  2475. /* copy nameservers' addresses */
  2476. i = 0;
  2477. #ifdef __UCLIBC_HAS_IPV4__
  2478. n = 0;
  2479. while (n < ARRAY_SIZE(rp->nsaddr_list) && i < __nameservers) {
  2480. if (__nameserver[i].sa.sa_family == AF_INET) {
  2481. rp->nsaddr_list[n] = __nameserver[i].sa4; /* struct copy */
  2482. #ifdef __UCLIBC_HAS_IPV6__
  2483. if (m < ARRAY_SIZE(rp->_u._ext.nsaddrs)) {
  2484. rp->_u._ext.nsaddrs[m] = (void*) &rp->nsaddr_list[n];
  2485. m++;
  2486. }
  2487. #endif
  2488. n++;
  2489. }
  2490. #ifdef __UCLIBC_HAS_IPV6__
  2491. if (__nameserver[i].sa.sa_family == AF_INET6
  2492. && m < ARRAY_SIZE(rp->_u._ext.nsaddrs)
  2493. ) {
  2494. struct sockaddr_in6 *sa6 = malloc(sizeof(sa6));
  2495. if (sa6) {
  2496. *sa6 = __nameserver[i].sa6; /* struct copy */
  2497. rp->_u._ext.nsaddrs[m] = sa6;
  2498. m++;
  2499. }
  2500. }
  2501. #endif
  2502. i++;
  2503. }
  2504. rp->nscount = n;
  2505. #ifdef __UCLIBC_HAS_IPV6__
  2506. rp->_u._ext.nscount = m;
  2507. #endif
  2508. #else /* IPv6 only */
  2509. while (m < ARRAY_SIZE(rp->_u._ext.nsaddrs) && i < __nameservers) {
  2510. struct sockaddr_in6 *sa6 = malloc(sizeof(sa6));
  2511. if (sa6) {
  2512. *sa6 = __nameserver[i].sa6; /* struct copy */
  2513. rp->_u._ext.nsaddrs[m] = sa6;
  2514. m++;
  2515. }
  2516. i++;
  2517. }
  2518. rp->_u._ext.nscount = m;
  2519. #endif
  2520. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  2521. return 0;
  2522. }
  2523. libc_hidden_def(res_init)
  2524. #ifdef __UCLIBC_HAS_BSD_RES_CLOSE__
  2525. void res_close(void)
  2526. {
  2527. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  2528. __close_nameservers();
  2529. __res_sync = NULL;
  2530. #ifdef __UCLIBC_HAS_IPV6__
  2531. {
  2532. char *p1 = (char*) &(_res.nsaddr_list[0]);
  2533. int m = 0;
  2534. /* free nsaddrs[m] if they do not point to nsaddr_list[x] */
  2535. while (m < ARRAY_SIZE(_res._u._ext.nsaddrs)) {
  2536. char *p2 = (char*)(_res._u._ext.nsaddrs[m]);
  2537. if (p2 < p1 || (p2 - p1) > sizeof(_res.nsaddr_list))
  2538. free(p2);
  2539. }
  2540. }
  2541. #endif
  2542. memset(&_res, 0, sizeof(_res));
  2543. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  2544. }
  2545. #endif
  2546. #endif /* L_res_init */
  2547. #ifdef L_res_query
  2548. int res_query(const char *dname, int class, int type,
  2549. unsigned char *answer, int anslen)
  2550. {
  2551. int i;
  2552. unsigned char * packet = NULL;
  2553. struct resolv_answer a;
  2554. if (!dname || class != 1 /* CLASS_IN */) {
  2555. h_errno = NO_RECOVERY;
  2556. return -1;
  2557. }
  2558. memset(&a, '\0', sizeof(a));
  2559. i = __dns_lookup(dname, type, &packet, &a);
  2560. if (i < 0) {
  2561. h_errno = TRY_AGAIN;
  2562. return -1;
  2563. }
  2564. free(a.dotted);
  2565. if (a.atype == type) { /* CNAME */
  2566. if (i > anslen)
  2567. i = anslen;
  2568. memcpy(answer, packet, i);
  2569. }
  2570. free(packet);
  2571. return i;
  2572. }
  2573. libc_hidden_def(res_query)
  2574. /*
  2575. * Formulate a normal query, send, and retrieve answer in supplied buffer.
  2576. * Return the size of the response on success, -1 on error.
  2577. * If enabled, implement search rules until answer or unrecoverable failure
  2578. * is detected. Error code, if any, is left in h_errno.
  2579. */
  2580. #define __TRAILING_DOT (1<<0)
  2581. #define __GOT_NODATA (1<<1)
  2582. #define __GOT_SERVFAIL (1<<2)
  2583. #define __TRIED_AS_IS (1<<3)
  2584. int res_search(const char *name, int class, int type, u_char *answer,
  2585. int anslen)
  2586. {
  2587. const char *cp, * const *domain;
  2588. HEADER *hp = (HEADER *)(void *)answer;
  2589. unsigned dots;
  2590. unsigned state;
  2591. int ret, saved_herrno;
  2592. uint32_t _res_options;
  2593. unsigned _res_ndots;
  2594. char **_res_dnsrch;
  2595. if (!name || !answer) {
  2596. h_errno = NETDB_INTERNAL;
  2597. return -1;
  2598. }
  2599. again:
  2600. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  2601. _res_options = _res.options;
  2602. _res_ndots = _res.ndots;
  2603. _res_dnsrch = _res.dnsrch;
  2604. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  2605. if (!(_res_options & RES_INIT)) {
  2606. res_init(); /* our res_init never fails */
  2607. goto again;
  2608. }
  2609. state = 0;
  2610. errno = 0;
  2611. h_errno = HOST_NOT_FOUND; /* default, if we never query */
  2612. dots = 0;
  2613. for (cp = name; *cp; cp++)
  2614. dots += (*cp == '.');
  2615. if (cp > name && *--cp == '.')
  2616. state |= __TRAILING_DOT;
  2617. /*
  2618. * If there are dots in the name already, let's just give it a try
  2619. * 'as is'. The threshold can be set with the "ndots" option.
  2620. */
  2621. saved_herrno = -1;
  2622. if (dots >= _res_ndots) {
  2623. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  2624. if (ret > 0)
  2625. return ret;
  2626. saved_herrno = h_errno;
  2627. state |= __TRIED_AS_IS;
  2628. }
  2629. /*
  2630. * We do at least one level of search if
  2631. * - there is no dot and RES_DEFNAME is set, or
  2632. * - there is at least one dot, there is no trailing dot,
  2633. * and RES_DNSRCH is set.
  2634. */
  2635. if ((!dots && (_res_options & RES_DEFNAMES))
  2636. || (dots && !(state & __TRAILING_DOT) && (_res_options & RES_DNSRCH))
  2637. ) {
  2638. bool done = 0;
  2639. for (domain = (const char * const *)_res_dnsrch;
  2640. *domain && !done;
  2641. domain++) {
  2642. ret = res_querydomain(name, *domain, class, type,
  2643. answer, anslen);
  2644. if (ret > 0)
  2645. return ret;
  2646. /*
  2647. * If no server present, give up.
  2648. * If name isn't found in this domain,
  2649. * keep trying higher domains in the search list
  2650. * (if that's enabled).
  2651. * On a NO_DATA error, keep trying, otherwise
  2652. * a wildcard entry of another type could keep us
  2653. * from finding this entry higher in the domain.
  2654. * If we get some other error (negative answer or
  2655. * server failure), then stop searching up,
  2656. * but try the input name below in case it's
  2657. * fully-qualified.
  2658. */
  2659. if (errno == ECONNREFUSED) {
  2660. h_errno = TRY_AGAIN;
  2661. return -1;
  2662. }
  2663. switch (h_errno) {
  2664. case NO_DATA:
  2665. state |= __GOT_NODATA;
  2666. /* FALLTHROUGH */
  2667. case HOST_NOT_FOUND:
  2668. /* keep trying */
  2669. break;
  2670. case TRY_AGAIN:
  2671. if (hp->rcode == SERVFAIL) {
  2672. /* try next search element, if any */
  2673. state |= __GOT_SERVFAIL;
  2674. break;
  2675. }
  2676. /* FALLTHROUGH */
  2677. default:
  2678. /* anything else implies that we're done */
  2679. done = 1;
  2680. }
  2681. /*
  2682. * if we got here for some reason other than DNSRCH,
  2683. * we only wanted one iteration of the loop, so stop.
  2684. */
  2685. if (!(_res_options & RES_DNSRCH))
  2686. done = 1;
  2687. }
  2688. }
  2689. /*
  2690. * if we have not already tried the name "as is", do that now.
  2691. * note that we do this regardless of how many dots were in the
  2692. * name or whether it ends with a dot.
  2693. */
  2694. if (!(state & __TRIED_AS_IS)) {
  2695. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  2696. if (ret > 0)
  2697. return ret;
  2698. }
  2699. /*
  2700. * if we got here, we didn't satisfy the search.
  2701. * if we did an initial full query, return that query's h_errno
  2702. * (note that we wouldn't be here if that query had succeeded).
  2703. * else if we ever got a nodata, send that back as the reason.
  2704. * else send back meaningless h_errno, that being the one from
  2705. * the last DNSRCH we did.
  2706. */
  2707. if (saved_herrno != -1)
  2708. h_errno = saved_herrno;
  2709. else if (state & __GOT_NODATA)
  2710. h_errno = NO_DATA;
  2711. else if (state & __GOT_SERVFAIL)
  2712. h_errno = TRY_AGAIN;
  2713. return -1;
  2714. }
  2715. #undef __TRAILING_DOT
  2716. #undef __GOT_NODATA
  2717. #undef __GOT_SERVFAIL
  2718. #undef __TRIED_AS_IS
  2719. /*
  2720. * Perform a call on res_query on the concatenation of name and domain,
  2721. * removing a trailing dot from name if domain is NULL.
  2722. */
  2723. int res_querydomain(const char *name, const char *domain, int class, int type,
  2724. u_char * answer, int anslen)
  2725. {
  2726. char nbuf[MAXDNAME];
  2727. const char *longname = nbuf;
  2728. size_t n, d;
  2729. #ifdef DEBUG
  2730. uint32_t _res_options;
  2731. #endif
  2732. if (!name || !answer) {
  2733. h_errno = NETDB_INTERNAL;
  2734. return -1;
  2735. }
  2736. #ifdef DEBUG
  2737. again:
  2738. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  2739. _res_options = _res.options;
  2740. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  2741. if (!(_res_options & RES_INIT)) {
  2742. res_init(); /* our res_init never fails */
  2743. goto again:
  2744. }
  2745. if (_res_options & RES_DEBUG)
  2746. printf(";; res_querydomain(%s, %s, %d, %d)\n",
  2747. name, (domain ? domain : "<Nil>"), class, type);
  2748. #endif
  2749. if (domain == NULL) {
  2750. /*
  2751. * Check for trailing '.';
  2752. * copy without '.' if present.
  2753. */
  2754. n = strlen(name);
  2755. if (n + 1 > sizeof(nbuf)) {
  2756. h_errno = NO_RECOVERY;
  2757. return -1;
  2758. }
  2759. if (n > 0 && name[--n] == '.') {
  2760. strncpy(nbuf, name, n);
  2761. nbuf[n] = '\0';
  2762. } else
  2763. longname = name;
  2764. } else {
  2765. n = strlen(name);
  2766. d = strlen(domain);
  2767. if (n + 1 + d + 1 > sizeof(nbuf)) {
  2768. h_errno = NO_RECOVERY;
  2769. return -1;
  2770. }
  2771. snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
  2772. }
  2773. return res_query(longname, class, type, answer, anslen);
  2774. }
  2775. libc_hidden_def(res_querydomain)
  2776. /* res_mkquery */
  2777. /* res_send */
  2778. /* dn_comp */
  2779. /* dn_expand */
  2780. #endif
  2781. /* vi: set sw=4 ts=4: */