resolv.c 78 KB

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