resolv.c 76 KB

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