resolv.c 72 KB

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