resolv.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  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. int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char **nsip,
  649. unsigned char **outpacket, struct resolv_answer *a)
  650. {
  651. int i, j, len, fd, pos, rc;
  652. #ifdef USE_SELECT
  653. struct timeval tv;
  654. fd_set fds;
  655. #else
  656. struct pollfd fds;
  657. #endif
  658. struct resolv_header h;
  659. struct resolv_question q;
  660. struct resolv_answer ma;
  661. bool first_answer = 1;
  662. unsigned retries = 0;
  663. unsigned char *packet = malloc(PACKETSZ);
  664. char *dns, *lookup = malloc(MAXDNAME);
  665. int variant = -1; /* search domain to append, -1 - none */
  666. int local_ns = -1, local_id = -1;
  667. bool ends_with_dot;
  668. #ifdef __UCLIBC_HAS_IPV6__
  669. bool v6;
  670. struct sockaddr_in6 sa6;
  671. #endif
  672. #ifdef __UCLIBC_HAS_IPV4__
  673. struct sockaddr_in sa;
  674. #endif
  675. fd = -1;
  676. if (!packet || !lookup || !nscount || !name[0])
  677. goto fail;
  678. DPRINTF("Looking up type %d answer for '%s'\n", type, name);
  679. ends_with_dot = (name[strlen(name) - 1] == '.');
  680. /* Mess with globals while under lock */
  681. __UCLIBC_MUTEX_LOCK(mylock);
  682. local_ns = static_ns % nscount;
  683. local_id = static_id;
  684. __UCLIBC_MUTEX_UNLOCK(mylock);
  685. while (retries < MAX_RETRIES) {
  686. if (fd != -1)
  687. close(fd);
  688. memset(packet, 0, PACKETSZ);
  689. memset(&h, 0, sizeof(h));
  690. ++local_id;
  691. local_id &= 0xffff;
  692. h.id = local_id;
  693. h.qdcount = 1;
  694. h.rd = 1;
  695. DPRINTF("encoding header\n", h.rd);
  696. i = __encode_header(&h, packet, PACKETSZ);
  697. if (i < 0)
  698. goto fail;
  699. strncpy(lookup, name, MAXDNAME);
  700. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  701. /* nsip is really __nameserver[] which is a global that
  702. needs to hold __resolv_lock before access!! */
  703. dns = nsip[local_ns];
  704. /* TODO: all future accesses to 'dns' were guarded by __resolv_lock too.
  705. * Why? We already fetched nsip[local_ns] here,
  706. * future changes to nsip[] by other threads cannot affect us.
  707. * We can use 'dns' without locking. If I'm wrong,
  708. * please explain in comments why locking is needed.
  709. * One thing that worries me is - what if __close_nameservers() free()s
  710. * dns under us? __resolv_lock'ing around accesses to dns won't help either,
  711. * as free() might occur between accesses!
  712. */
  713. if (variant >= 0) {
  714. if (variant < __searchdomains) {
  715. strncat(lookup, ".", MAXDNAME);
  716. strncat(lookup, __searchdomain[variant], MAXDNAME);
  717. }
  718. }
  719. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  720. DPRINTF("lookup name: %s\n", lookup);
  721. q.dotted = lookup;
  722. q.qtype = type;
  723. q.qclass = C_IN; /* CLASS_IN */
  724. j = __encode_question(&q, packet+i, PACKETSZ-i);
  725. if (j < 0)
  726. goto fail;
  727. len = i + j;
  728. DPRINTF("On try %d, sending query to port %d of machine %s\n",
  729. retries+1, NAMESERVER_PORT, dns);
  730. #ifdef __UCLIBC_HAS_IPV6__
  731. //__UCLIBC_MUTEX_LOCK(__resolv_lock);
  732. ///* 'dns' is really __nameserver[] which is a global that
  733. // needs to hold __resolv_lock before access!! */
  734. v6 = inet_pton(AF_INET6, dns, &sa6.sin6_addr) > 0;
  735. //__UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  736. fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  737. #else
  738. fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  739. #endif
  740. if (fd < 0) {
  741. retries++;
  742. continue;
  743. }
  744. /* Connect to the UDP socket so that asyncronous errors are returned */
  745. #ifdef __UCLIBC_HAS_IPV6__
  746. if (v6) {
  747. sa6.sin6_family = AF_INET6;
  748. sa6.sin6_port = htons(NAMESERVER_PORT);
  749. /* sa6.sin6_addr is already here */
  750. rc = connect(fd, (struct sockaddr *) &sa6, sizeof(sa6));
  751. } else {
  752. #endif
  753. #ifdef __UCLIBC_HAS_IPV4__
  754. sa.sin_family = AF_INET;
  755. sa.sin_port = htons(NAMESERVER_PORT);
  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. sa.sin_addr.s_addr = inet_addr(dns);
  760. //__UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  761. rc = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
  762. #endif
  763. #ifdef __UCLIBC_HAS_IPV6__
  764. }
  765. #endif
  766. if (rc < 0) {
  767. if (errno == ENETUNREACH) {
  768. /* routing error, presume not transient */
  769. goto tryall;
  770. }
  771. /* retry */
  772. retries++;
  773. continue;
  774. }
  775. DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
  776. len, h.id, h.qr);
  777. send(fd, packet, len, 0);
  778. #ifdef USE_SELECT
  779. FD_ZERO(&fds);
  780. FD_SET(fd, &fds);
  781. tv.tv_sec = REPLY_TIMEOUT;
  782. tv.tv_usec = 0;
  783. if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
  784. DPRINTF("Timeout\n");
  785. /* timed out, so retry send and receive,
  786. * to next nameserver on queue */
  787. goto tryall;
  788. }
  789. #else
  790. fds.fd = fd;
  791. fds.events = POLLIN;
  792. if (poll(&fds, 1, REPLY_TIMEOUT * 1000) <= 0) {
  793. DPRINTF("Timeout\n");
  794. /* timed out, so retry send and receive,
  795. * to next nameserver on queue */
  796. goto tryall;
  797. }
  798. #endif
  799. len = recv(fd, packet, PACKETSZ, 0);
  800. if (len < HFIXEDSZ) {
  801. /* too short! */
  802. goto again;
  803. }
  804. __decode_header(packet, &h);
  805. DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
  806. if ((h.id != local_id) || (!h.qr)) {
  807. /* unsolicited */
  808. goto again;
  809. }
  810. DPRINTF("Got response (i think)!\n");
  811. DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
  812. h.qdcount, h.ancount, h.nscount, h.arcount);
  813. DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
  814. h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
  815. if (h.rcode == NXDOMAIN) {
  816. // bug 660 says we treat negative response as an error and retry
  817. // which is, eh, an error. :) We were incurring long delays because of this.
  818. /* this is not an error - don't goto again! */
  819. h_errno = HOST_NOT_FOUND;
  820. goto fail1;
  821. }
  822. if (h.rcode != 0) /* error */
  823. goto again;
  824. /* code below won't work correctly with h.ancount == 0, so... */
  825. if (h.ancount < 1) {
  826. h_errno = NO_DATA; /* is this correct code? */
  827. goto fail1;
  828. }
  829. pos = HFIXEDSZ;
  830. for (j = 0; j < h.qdcount; j++) {
  831. DPRINTF("Skipping question %d at %d\n", j, pos);
  832. /* returns -1 only if packet == NULL (can't happen) */
  833. i = __length_question(packet, pos);
  834. //if (i < 0)
  835. // goto again;
  836. DPRINTF("Length of question %d is %d\n", j, i);
  837. pos += i;
  838. }
  839. DPRINTF("Decoding answer at pos %d\n", pos);
  840. first_answer = 1;
  841. for (j = 0; j < h.ancount && pos < len; j++, pos += i) {
  842. i = __decode_answer(packet, pos, len, &ma);
  843. if (i < 0) {
  844. DPRINTF("failed decode %d\n", i);
  845. /* if the message was truncated and we have
  846. decoded some answers, pretend it's OK */
  847. if (j && h.tc)
  848. break;
  849. goto again;
  850. }
  851. if (first_answer) {
  852. ma.buf = a->buf;
  853. ma.buflen = a->buflen;
  854. ma.add_count = a->add_count;
  855. memcpy(a, &ma, sizeof(ma));
  856. if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA)))
  857. break;
  858. if (a->atype != type) {
  859. free(a->dotted);
  860. continue;
  861. }
  862. a->add_count = h.ancount - j - 1;
  863. if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
  864. break;
  865. a->add_count = 0;
  866. first_answer = 0;
  867. } else {
  868. free(ma.dotted);
  869. if (ma.atype != type)
  870. continue;
  871. if (a->rdlength != ma.rdlength) {
  872. free(a->dotted);
  873. DPRINTF("Answer address len(%u) differs from original(%u)\n",
  874. ma.rdlength, a->rdlength);
  875. goto again;
  876. }
  877. memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
  878. ++a->add_count;
  879. }
  880. }
  881. DPRINTF("Answer name = |%s|\n", a->dotted);
  882. DPRINTF("Answer type = |%d|\n", a->atype);
  883. close(fd);
  884. if (outpacket)
  885. *outpacket = packet;
  886. else
  887. free(packet);
  888. free(lookup);
  889. /* Mess with globals while under lock */
  890. __UCLIBC_MUTEX_LOCK(mylock);
  891. static_ns = local_ns;
  892. static_id = local_id;
  893. __UCLIBC_MUTEX_UNLOCK(mylock);
  894. return len; /* success! */
  895. tryall:
  896. /* if there are other nameservers, give them a go,
  897. otherwise return with error */
  898. variant = -1;
  899. local_ns = (local_ns + 1) % nscount;
  900. if (local_ns == 0)
  901. retries++;
  902. continue;
  903. again:
  904. /* if there are searchdomains, try them or fallback as passed */
  905. if (!ends_with_dot) {
  906. int sdomains;
  907. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  908. sdomains = __searchdomains;
  909. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  910. if (variant < sdomains - 1) {
  911. /* next search */
  912. variant++;
  913. continue;
  914. }
  915. }
  916. /* next server, first search */
  917. local_ns = (local_ns + 1) % nscount;
  918. if (local_ns == 0)
  919. retries++;
  920. variant = -1;
  921. }
  922. fail:
  923. h_errno = NETDB_INTERNAL;
  924. fail1:
  925. if (fd != -1)
  926. close(fd);
  927. free(lookup);
  928. free(packet);
  929. /* Mess with globals while under lock */
  930. if (local_ns != -1) {
  931. __UCLIBC_MUTEX_LOCK(mylock);
  932. static_ns = local_ns;
  933. static_id = local_id;
  934. __UCLIBC_MUTEX_UNLOCK(mylock);
  935. }
  936. return -1;
  937. }
  938. #endif
  939. #ifdef L_opennameservers
  940. /* We use __resolv_lock to guard access to the
  941. * '__nameservers' and __searchdomains globals */
  942. int __nameservers;
  943. char * __nameserver[MAX_SERVERS];
  944. int __searchdomains;
  945. char * __searchdomain[MAX_SEARCH];
  946. __UCLIBC_MUTEX_INIT(__resolv_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  947. /*
  948. * we currently read formats not quite the same as that on normal
  949. * unix systems, we can have a list of nameservers after the keyword.
  950. */
  951. void attribute_hidden __open_nameservers(void)
  952. {
  953. FILE *fp;
  954. int i;
  955. #define RESOLV_ARGS 5
  956. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  957. int argc;
  958. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  959. if (__nameservers > 0)
  960. goto DONE;
  961. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  962. (fp = fopen("/etc/config/resolv.conf", "r")))
  963. {
  964. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  965. for (p = szBuffer; *p && isspace(*p); p++)
  966. /* skip white space */;
  967. if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  968. continue;
  969. argc = 0;
  970. while (*p && argc < RESOLV_ARGS) {
  971. argv[argc++] = p;
  972. while (*p && !isspace(*p) && *p != '\n')
  973. p++;
  974. while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  975. *p++ = '\0';
  976. }
  977. if (strcmp(argv[0], "nameserver") == 0) {
  978. for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
  979. // TODO: what if strdup fails?
  980. __nameserver[__nameservers++] = strdup(argv[i]);
  981. DPRINTF("adding nameserver %s\n", argv[i]);
  982. }
  983. }
  984. /* domain and search are mutually exclusive, the last one wins */
  985. if (strcmp(argv[0],"domain") == 0 || strcmp(argv[0],"search") == 0) {
  986. while (__searchdomains > 0) {
  987. free(__searchdomain[--__searchdomains]);
  988. __searchdomain[__searchdomains] = NULL;
  989. }
  990. for (i = 1; i < argc && __searchdomains < MAX_SEARCH; i++) {
  991. // TODO: what if strdup fails?
  992. __searchdomain[__searchdomains++] = strdup(argv[i]);
  993. DPRINTF("adding search %s\n", argv[i]);
  994. }
  995. }
  996. }
  997. fclose(fp);
  998. DPRINTF("nameservers = %d\n", __nameservers);
  999. goto DONE;
  1000. }
  1001. DPRINTF("failed to open %s\n", "resolv.conf");
  1002. h_errno = NO_RECOVERY;
  1003. /* rv = -1; */
  1004. DONE:
  1005. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1006. /* return rv; */
  1007. }
  1008. #endif
  1009. #ifdef L_closenameservers
  1010. void attribute_hidden __close_nameservers(void)
  1011. {
  1012. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1013. while (__nameservers > 0) {
  1014. free(__nameserver[--__nameservers]);
  1015. __nameserver[__nameservers] = NULL;
  1016. }
  1017. while (__searchdomains > 0) {
  1018. free(__searchdomain[--__searchdomains]);
  1019. __searchdomain[__searchdomains] = NULL;
  1020. }
  1021. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1022. }
  1023. #endif
  1024. #ifdef L_gethostbyname
  1025. struct hostent *gethostbyname(const char *name)
  1026. {
  1027. static struct hostent h;
  1028. static char buf[sizeof(struct in_addr) +
  1029. sizeof(struct in_addr *)*2 +
  1030. sizeof(char *)*ALIAS_DIM + 384/*namebuffer*/ + 32/* margin */];
  1031. struct hostent *hp;
  1032. gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
  1033. return hp;
  1034. }
  1035. libc_hidden_def(gethostbyname)
  1036. #endif
  1037. #ifdef L_gethostbyname2
  1038. struct hostent *gethostbyname2(const char *name, int family)
  1039. {
  1040. #ifndef __UCLIBC_HAS_IPV6__
  1041. return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
  1042. #else /* __UCLIBC_HAS_IPV6__ */
  1043. static struct hostent h;
  1044. static char buf[sizeof(struct in6_addr) +
  1045. sizeof(struct in6_addr *)*2 +
  1046. sizeof(char *)*ALIAS_DIM + 384/*namebuffer*/ + 32/* margin */];
  1047. struct hostent *hp;
  1048. gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
  1049. return hp;
  1050. #endif /* __UCLIBC_HAS_IPV6__ */
  1051. }
  1052. #endif
  1053. #ifdef L_res_init
  1054. /* We use __resolv_lock to guard access to global '_res' */
  1055. struct __res_state _res;
  1056. int res_init(void)
  1057. {
  1058. struct __res_state *rp = &(_res);
  1059. __UCLIBC_MUTEX_LOCK(__resolv_lock); /* must be a recursive lock! */
  1060. __close_nameservers();
  1061. __open_nameservers();
  1062. rp->retrans = RES_TIMEOUT;
  1063. rp->retry = 4;
  1064. rp->options = RES_INIT;
  1065. rp->id = (u_int) random();
  1066. rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
  1067. rp->nsaddr.sin_family = AF_INET;
  1068. rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
  1069. rp->ndots = 1;
  1070. /** rp->pfcode = 0; **/
  1071. rp->_vcsock = -1;
  1072. /** rp->_flags = 0; **/
  1073. /** rp->qhook = NULL; **/
  1074. /** rp->rhook = NULL; **/
  1075. /** rp->_u._ext.nsinit = 0; **/
  1076. if (__searchdomains) {
  1077. int i;
  1078. for (i = 0; i < __searchdomains; i++)
  1079. rp->dnsrch[i] = __searchdomain[i];
  1080. }
  1081. if (__nameservers) {
  1082. int i;
  1083. struct in_addr a;
  1084. for (i = 0; i < __nameservers; i++) {
  1085. if (inet_aton(__nameserver[i], &a)) {
  1086. rp->nsaddr_list[i].sin_addr = a;
  1087. rp->nsaddr_list[i].sin_family = AF_INET;
  1088. rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
  1089. }
  1090. }
  1091. }
  1092. rp->nscount = __nameservers;
  1093. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1094. return 0;
  1095. }
  1096. libc_hidden_def(res_init)
  1097. #ifdef __UCLIBC_HAS_BSD_RES_CLOSE__
  1098. void res_close(void)
  1099. {
  1100. __close_nameservers();
  1101. memset(&_res, 0, sizeof(_res));
  1102. }
  1103. #endif
  1104. #endif
  1105. #ifdef L_res_query
  1106. #ifndef MIN
  1107. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  1108. #endif
  1109. int res_query(const char *dname, int class, int type,
  1110. unsigned char *answer, int anslen)
  1111. {
  1112. int i;
  1113. unsigned char * packet = 0;
  1114. struct resolv_answer a;
  1115. int __nameserversXX;
  1116. char ** __nameserverXX;
  1117. if (!dname || class != 1 /* CLASS_IN */) {
  1118. h_errno = NO_RECOVERY;
  1119. return -1;
  1120. }
  1121. memset(&a, '\0', sizeof(a));
  1122. __open_nameservers();
  1123. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1124. __nameserversXX = __nameservers;
  1125. __nameserverXX = __nameserver;
  1126. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1127. i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
  1128. if (i < 0) {
  1129. h_errno = TRY_AGAIN;
  1130. return -1;
  1131. }
  1132. free(a.dotted);
  1133. if (a.atype == type) { /* CNAME */
  1134. i = MIN(anslen, i);
  1135. memcpy(answer, packet, i);
  1136. }
  1137. free(packet);
  1138. return i;
  1139. }
  1140. libc_hidden_def(res_query)
  1141. /*
  1142. * Formulate a normal query, send, and retrieve answer in supplied buffer.
  1143. * Return the size of the response on success, -1 on error.
  1144. * If enabled, implement search rules until answer or unrecoverable failure
  1145. * is detected. Error code, if any, is left in h_errno.
  1146. */
  1147. #define __TRAILING_DOT (1<<0)
  1148. #define __GOT_NODATA (1<<1)
  1149. #define __GOT_SERVFAIL (1<<2)
  1150. #define __TRIED_AS_IS (1<<3)
  1151. int res_search(const char *name, int class, int type, u_char *answer,
  1152. int anslen)
  1153. {
  1154. const char *cp, * const *domain;
  1155. HEADER *hp = (HEADER *)(void *)answer;
  1156. u_int dots;
  1157. unsigned _state = 0;
  1158. int ret, saved_herrno;
  1159. u_long _res_options;
  1160. unsigned _res_ndots;
  1161. char **_res_dnsrch;
  1162. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1163. _res_options = _res.options;
  1164. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1165. if ((!name || !answer) || ((_res_options & RES_INIT) == 0 && res_init() == -1)) {
  1166. h_errno = NETDB_INTERNAL;
  1167. return -1;
  1168. }
  1169. errno = 0;
  1170. h_errno = HOST_NOT_FOUND; /* default, if we never query */
  1171. dots = 0;
  1172. for (cp = name; *cp; cp++)
  1173. dots += (*cp == '.');
  1174. if (cp > name && *--cp == '.')
  1175. _state |= __TRAILING_DOT;
  1176. /*
  1177. * If there are dots in the name already, let's just give it a try
  1178. * 'as is'. The threshold can be set with the "ndots" option.
  1179. */
  1180. saved_herrno = -1;
  1181. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1182. _res_ndots = _res.ndots;
  1183. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1184. if (dots >= _res_ndots) {
  1185. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1186. if (ret > 0)
  1187. return ret;
  1188. saved_herrno = h_errno;
  1189. _state |= __TRIED_AS_IS;
  1190. }
  1191. /*
  1192. * We do at least one level of search if
  1193. * - there is no dot and RES_DEFNAME is set, or
  1194. * - there is at least one dot, there is no trailing dot,
  1195. * and RES_DNSRCH is set.
  1196. */
  1197. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1198. _res_options = _res.options;
  1199. _res_dnsrch = _res.dnsrch;
  1200. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1201. if ((!dots && (_res_options & RES_DEFNAMES)) ||
  1202. (dots && !(_state & __TRAILING_DOT) && (_res_options & RES_DNSRCH))) {
  1203. bool done = 0;
  1204. for (domain = (const char * const *)_res_dnsrch;
  1205. *domain && !done;
  1206. domain++) {
  1207. ret = res_querydomain(name, *domain, class, type,
  1208. answer, anslen);
  1209. if (ret > 0)
  1210. return ret;
  1211. /*
  1212. * If no server present, give up.
  1213. * If name isn't found in this domain,
  1214. * keep trying higher domains in the search list
  1215. * (if that's enabled).
  1216. * On a NO_DATA error, keep trying, otherwise
  1217. * a wildcard entry of another type could keep us
  1218. * from finding this entry higher in the domain.
  1219. * If we get some other error (negative answer or
  1220. * server failure), then stop searching up,
  1221. * but try the input name below in case it's
  1222. * fully-qualified.
  1223. */
  1224. if (errno == ECONNREFUSED) {
  1225. h_errno = TRY_AGAIN;
  1226. return -1;
  1227. }
  1228. switch (h_errno) {
  1229. case NO_DATA:
  1230. _state |= __GOT_NODATA;
  1231. /* FALLTHROUGH */
  1232. case HOST_NOT_FOUND:
  1233. /* keep trying */
  1234. break;
  1235. case TRY_AGAIN:
  1236. if (hp->rcode == SERVFAIL) {
  1237. /* try next search element, if any */
  1238. _state |= __GOT_SERVFAIL;
  1239. break;
  1240. }
  1241. /* FALLTHROUGH */
  1242. default:
  1243. /* anything else implies that we're done */
  1244. done = 1;
  1245. }
  1246. /*
  1247. * if we got here for some reason other than DNSRCH,
  1248. * we only wanted one iteration of the loop, so stop.
  1249. */
  1250. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1251. _res_options = _res.options;
  1252. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1253. if (!(_res_options & RES_DNSRCH))
  1254. done = 1;
  1255. }
  1256. }
  1257. /*
  1258. * if we have not already tried the name "as is", do that now.
  1259. * note that we do this regardless of how many dots were in the
  1260. * name or whether it ends with a dot.
  1261. */
  1262. if (!(_state & __TRIED_AS_IS)) {
  1263. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1264. if (ret > 0)
  1265. return ret;
  1266. }
  1267. /*
  1268. * if we got here, we didn't satisfy the search.
  1269. * if we did an initial full query, return that query's h_errno
  1270. * (note that we wouldn't be here if that query had succeeded).
  1271. * else if we ever got a nodata, send that back as the reason.
  1272. * else send back meaningless h_errno, that being the one from
  1273. * the last DNSRCH we did.
  1274. */
  1275. if (saved_herrno != -1)
  1276. h_errno = saved_herrno;
  1277. else if (_state & __GOT_NODATA)
  1278. h_errno = NO_DATA;
  1279. else if (_state & __GOT_SERVFAIL)
  1280. h_errno = TRY_AGAIN;
  1281. return -1;
  1282. }
  1283. #undef __TRAILING_DOT
  1284. #undef __GOT_NODATA
  1285. #undef __GOT_SERVFAIL
  1286. #undef __TRIED_AS_IS
  1287. /*
  1288. * Perform a call on res_query on the concatenation of name and domain,
  1289. * removing a trailing dot from name if domain is NULL.
  1290. */
  1291. int res_querydomain(const char *name, const char *domain, int class, int type,
  1292. u_char * answer, int anslen)
  1293. {
  1294. char nbuf[MAXDNAME];
  1295. const char *longname = nbuf;
  1296. size_t n, d;
  1297. u_long _res_options;
  1298. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1299. _res_options = _res.options;
  1300. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1301. if ((!name || !answer) || ((_res_options & RES_INIT) == 0 && res_init() == -1)) {
  1302. h_errno = NETDB_INTERNAL;
  1303. return -1;
  1304. }
  1305. #ifdef DEBUG
  1306. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1307. _res_options = _res.options;
  1308. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1309. if (_res_options & RES_DEBUG)
  1310. printf(";; res_querydomain(%s, %s, %d, %d)\n",
  1311. name, (domain ? domain : "<Nil>"), class, type);
  1312. #endif
  1313. if (domain == NULL) {
  1314. /*
  1315. * Check for trailing '.';
  1316. * copy without '.' if present.
  1317. */
  1318. n = strlen(name);
  1319. if (n + 1 > sizeof(nbuf)) {
  1320. h_errno = NO_RECOVERY;
  1321. return -1;
  1322. }
  1323. if (n > 0 && name[--n] == '.') {
  1324. strncpy(nbuf, name, n);
  1325. nbuf[n] = '\0';
  1326. } else
  1327. longname = name;
  1328. } else {
  1329. n = strlen(name);
  1330. d = strlen(domain);
  1331. if (n + 1 + d + 1 > sizeof(nbuf)) {
  1332. h_errno = NO_RECOVERY;
  1333. return -1;
  1334. }
  1335. snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
  1336. }
  1337. return res_query(longname, class, type, answer, anslen);
  1338. }
  1339. libc_hidden_def(res_querydomain)
  1340. /* res_mkquery */
  1341. /* res_send */
  1342. /* dn_comp */
  1343. /* dn_expand */
  1344. #endif
  1345. #ifdef L_gethostbyaddr
  1346. struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
  1347. {
  1348. static struct hostent h;
  1349. static char buf[
  1350. #ifndef __UCLIBC_HAS_IPV6__
  1351. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1352. #else
  1353. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1354. #endif /* __UCLIBC_HAS_IPV6__ */
  1355. sizeof(char *)*ALIAS_DIM + 384 /*namebuffer*/ + 32 /* margin */];
  1356. struct hostent *hp;
  1357. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  1358. return hp;
  1359. }
  1360. libc_hidden_def(gethostbyaddr)
  1361. #endif
  1362. #ifdef L_read_etc_hosts_r
  1363. FILE * __open_etc_hosts(void)
  1364. {
  1365. FILE * fp;
  1366. if ((fp = fopen("/etc/hosts", "r")) == NULL) {
  1367. fp = fopen("/etc/config/hosts", "r");
  1368. }
  1369. return fp;
  1370. }
  1371. int attribute_hidden __read_etc_hosts_r(FILE * fp, const char * name, int type,
  1372. enum etc_hosts_action action,
  1373. struct hostent * result_buf,
  1374. char * buf, size_t buflen,
  1375. struct hostent ** result,
  1376. int * h_errnop)
  1377. {
  1378. struct in_addr *in = NULL;
  1379. struct in_addr **addr_list = NULL;
  1380. #ifdef __UCLIBC_HAS_IPV6__
  1381. struct in6_addr *in6 = NULL;
  1382. struct in6_addr **addr_list6 = NULL;
  1383. #endif /* __UCLIBC_HAS_IPV6__ */
  1384. char *cp, **alias;
  1385. int aliases, i, ret = HOST_NOT_FOUND;
  1386. /* make sure user char * is aligned */
  1387. i = ALIGN_BUFFER_OFFSET(buf);
  1388. if (unlikely(i)) {
  1389. if (buflen < i)
  1390. return ERANGE;
  1391. buf += i;
  1392. buflen -= i;
  1393. }
  1394. if (buflen < sizeof(char *)*ALIAS_DIM)
  1395. return ERANGE;
  1396. alias = (char **)buf;
  1397. buf += sizeof(char **)*ALIAS_DIM;
  1398. buflen -= sizeof(char **)*ALIAS_DIM;
  1399. if (action != GETHOSTENT) {
  1400. #ifdef __UCLIBC_HAS_IPV6__
  1401. char *p = buf;
  1402. size_t len = buflen;
  1403. #endif /* __UCLIBC_HAS_IPV6__ */
  1404. *h_errnop = NETDB_INTERNAL;
  1405. if (buflen < sizeof(*in))
  1406. return ERANGE;
  1407. in = (struct in_addr*)buf;
  1408. buf += sizeof(*in);
  1409. buflen -= sizeof(*in);
  1410. if (buflen < sizeof(*addr_list)*2)
  1411. return ERANGE;
  1412. addr_list = (struct in_addr **)buf;
  1413. buf += sizeof(*addr_list)*2;
  1414. buflen -= sizeof(*addr_list)*2;
  1415. #ifdef __UCLIBC_HAS_IPV6__
  1416. if (len < sizeof(*in6))
  1417. return ERANGE;
  1418. in6 = (struct in6_addr*)p;
  1419. p += sizeof(*in6);
  1420. len -= sizeof(*in6);
  1421. if (len < sizeof(*addr_list6)*2)
  1422. return ERANGE;
  1423. addr_list6 = (struct in6_addr**)p;
  1424. p += sizeof(*addr_list6)*2;
  1425. len -= sizeof(*addr_list6)*2;
  1426. if (len < buflen) {
  1427. buflen = len;
  1428. buf = p;
  1429. }
  1430. #endif /* __UCLIBC_HAS_IPV6__ */
  1431. if (buflen < 80)
  1432. return ERANGE;
  1433. fp = __open_etc_hosts();
  1434. if (fp == NULL) {
  1435. *result = NULL;
  1436. return errno;
  1437. }
  1438. }
  1439. *h_errnop = HOST_NOT_FOUND;
  1440. while (fgets(buf, buflen, fp)) {
  1441. cp = strchr(buf, '#');
  1442. if (cp)
  1443. *cp = '\0';
  1444. DPRINTF("Looking at: %s\n", buf);
  1445. aliases = 0;
  1446. cp = buf;
  1447. while (*cp) {
  1448. while (*cp && isspace(*cp))
  1449. *cp++ = '\0';
  1450. if (!*cp)
  1451. break;
  1452. if (aliases < (2+MAX_ALIASES))
  1453. alias[aliases++] = cp;
  1454. while (*cp && !isspace(*cp))
  1455. cp++;
  1456. }
  1457. alias[aliases] = 0;
  1458. if (aliases < 2)
  1459. continue; /* syntax error really */
  1460. if (action == GETHOSTENT) {
  1461. /* Return whatever the next entry happens to be. */
  1462. break;
  1463. }
  1464. if (action == GET_HOSTS_BYADDR) {
  1465. if (strcmp(name, alias[0]) != 0)
  1466. continue;
  1467. } else {
  1468. /* GET_HOSTS_BYNAME */
  1469. for (i = 1; i < aliases; i++)
  1470. if (strcasecmp(name, alias[i]) == 0)
  1471. break;
  1472. if (i >= aliases)
  1473. continue;
  1474. }
  1475. if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
  1476. DPRINTF("Found INET\n");
  1477. addr_list[0] = in;
  1478. addr_list[1] = 0;
  1479. result_buf->h_name = alias[1];
  1480. result_buf->h_addrtype = AF_INET;
  1481. result_buf->h_length = sizeof(*in);
  1482. result_buf->h_addr_list = (char**) addr_list;
  1483. result_buf->h_aliases = alias + 2;
  1484. *result = result_buf;
  1485. ret = NETDB_SUCCESS;
  1486. #ifdef __UCLIBC_HAS_IPV6__
  1487. } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
  1488. DPRINTF("Found INET6\n");
  1489. addr_list6[0] = in6;
  1490. addr_list6[1] = 0;
  1491. result_buf->h_name = alias[1];
  1492. result_buf->h_addrtype = AF_INET6;
  1493. result_buf->h_length = sizeof(*in6);
  1494. result_buf->h_addr_list = (char**) addr_list6;
  1495. result_buf->h_aliases = alias + 2;
  1496. *result = result_buf;
  1497. ret = NETDB_SUCCESS;
  1498. #endif /* __UCLIBC_HAS_IPV6__ */
  1499. } else {
  1500. /* continue parsing in the hope the user has multiple
  1501. * host types listed in the database like so:
  1502. * <ipv4 addr> host
  1503. * <ipv6 addr> host
  1504. * If looking for an IPv6 addr, don't bail when we got the IPv4
  1505. */
  1506. DPRINTF("Error: Found host but diff network type\n");
  1507. /* NB: gethostbyname2_r depends on this feature
  1508. * to avoid looking for IPv6 addr of "localhost" etc */
  1509. ret = TRY_AGAIN;
  1510. continue;
  1511. }
  1512. break;
  1513. }
  1514. if (action != GETHOSTENT)
  1515. fclose(fp);
  1516. return ret;
  1517. }
  1518. #endif
  1519. #ifdef L_gethostent
  1520. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  1521. static smallint __stay_open;
  1522. static FILE * __gethostent_fp;
  1523. void endhostent(void)
  1524. {
  1525. __UCLIBC_MUTEX_LOCK(mylock);
  1526. __stay_open = 0;
  1527. if (__gethostent_fp) {
  1528. fclose(__gethostent_fp);
  1529. __gethostent_fp = NULL;
  1530. }
  1531. __UCLIBC_MUTEX_UNLOCK(mylock);
  1532. }
  1533. void sethostent(int stay_open)
  1534. {
  1535. __UCLIBC_MUTEX_LOCK(mylock);
  1536. __stay_open = (stay_open != 0);
  1537. __UCLIBC_MUTEX_UNLOCK(mylock);
  1538. }
  1539. int gethostent_r(struct hostent *result_buf, char *buf, size_t buflen,
  1540. struct hostent **result, int *h_errnop)
  1541. {
  1542. int ret;
  1543. __UCLIBC_MUTEX_LOCK(mylock);
  1544. if (__gethostent_fp == NULL) {
  1545. __gethostent_fp = __open_etc_hosts();
  1546. if (__gethostent_fp == NULL) {
  1547. *result = NULL;
  1548. ret = TRY_AGAIN;
  1549. goto DONE;
  1550. }
  1551. }
  1552. ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  1553. result_buf, buf, buflen, result, h_errnop);
  1554. if (__stay_open == 0) {
  1555. fclose(__gethostent_fp);
  1556. __gethostent_fp = NULL;
  1557. }
  1558. DONE:
  1559. __UCLIBC_MUTEX_UNLOCK(mylock);
  1560. return ret;
  1561. }
  1562. libc_hidden_def(gethostent_r)
  1563. struct hostent *gethostent(void)
  1564. {
  1565. static struct hostent h;
  1566. static char buf[
  1567. #ifndef __UCLIBC_HAS_IPV6__
  1568. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1569. #else
  1570. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1571. #endif /* __UCLIBC_HAS_IPV6__ */
  1572. sizeof(char *)*ALIAS_DIM +
  1573. 80 /*namebuffer*/ + 2 /* margin */];
  1574. struct hostent *host;
  1575. __UCLIBC_MUTEX_LOCK(mylock);
  1576. gethostent_r(&h, buf, sizeof(buf), &host, &h_errno);
  1577. __UCLIBC_MUTEX_UNLOCK(mylock);
  1578. return host;
  1579. }
  1580. #endif
  1581. #ifdef L_get_hosts_byname_r
  1582. int attribute_hidden __get_hosts_byname_r(const char * name, int type,
  1583. struct hostent * result_buf,
  1584. char * buf, size_t buflen,
  1585. struct hostent ** result,
  1586. int * h_errnop)
  1587. {
  1588. return __read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME,
  1589. result_buf, buf, buflen, result, h_errnop);
  1590. }
  1591. #endif
  1592. #ifdef L_get_hosts_byaddr_r
  1593. int attribute_hidden __get_hosts_byaddr_r(const char * addr, int len, int type,
  1594. struct hostent * result_buf,
  1595. char * buf, size_t buflen,
  1596. struct hostent ** result,
  1597. int * h_errnop)
  1598. {
  1599. #ifndef __UCLIBC_HAS_IPV6__
  1600. char ipaddr[INET_ADDRSTRLEN];
  1601. #else
  1602. char ipaddr[INET6_ADDRSTRLEN];
  1603. #endif /* __UCLIBC_HAS_IPV6__ */
  1604. switch (type) {
  1605. case AF_INET:
  1606. if (len != sizeof(struct in_addr))
  1607. return 0;
  1608. break;
  1609. #ifdef __UCLIBC_HAS_IPV6__
  1610. case AF_INET6:
  1611. if (len != sizeof(struct in6_addr))
  1612. return 0;
  1613. break;
  1614. #endif /* __UCLIBC_HAS_IPV6__ */
  1615. default:
  1616. return 0;
  1617. }
  1618. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1619. return __read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1620. result_buf, buf, buflen, result, h_errnop);
  1621. }
  1622. #endif
  1623. #ifdef L_getnameinfo
  1624. #ifndef min
  1625. # define min(x,y) (((x) > (y)) ? (y) : (x))
  1626. #endif /* min */
  1627. libc_hidden_proto(getnameinfo)
  1628. int getnameinfo(const struct sockaddr *sa, socklen_t addrlen, char *host,
  1629. socklen_t hostlen, char *serv, socklen_t servlen,
  1630. unsigned int flags)
  1631. {
  1632. int serrno = errno;
  1633. unsigned ok;
  1634. struct hostent *h = NULL;
  1635. char domain[256];
  1636. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1637. return EAI_BADFLAGS;
  1638. if (sa == NULL || addrlen < sizeof (sa_family_t))
  1639. goto BAD_FAM;
  1640. ok = sa->sa_family;
  1641. if (ok == AF_LOCAL) /* valid */;
  1642. #ifdef __UCLIBC_HAS_IPV4__
  1643. else if (ok == AF_INET) {
  1644. if (addrlen < sizeof (struct sockaddr_in))
  1645. goto BAD_FAM;
  1646. }
  1647. #endif
  1648. #ifdef __UCLIBC_HAS_IPV6__
  1649. else if (ok == AF_INET6) {
  1650. if (addrlen < sizeof (struct sockaddr_in6))
  1651. goto BAD_FAM;
  1652. }
  1653. #endif /* __UCLIBC_HAS_IPV6__ */
  1654. else
  1655. BAD_FAM:
  1656. return EAI_FAMILY;
  1657. ok = 0;
  1658. if (host != NULL && hostlen > 0)
  1659. switch (sa->sa_family) {
  1660. case AF_INET:
  1661. #ifdef __UCLIBC_HAS_IPV6__
  1662. case AF_INET6:
  1663. #endif /* __UCLIBC_HAS_IPV6__ */
  1664. if (!(flags & NI_NUMERICHOST)) {
  1665. #ifdef __UCLIBC_HAS_IPV6__
  1666. if (sa->sa_family == AF_INET6)
  1667. h = gethostbyaddr ((const void *)
  1668. &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1669. sizeof(struct in6_addr), AF_INET6);
  1670. #endif /* __UCLIBC_HAS_IPV6__ */
  1671. #if defined __UCLIBC_HAS_IPV6__ && defined __UCLIBC_HAS_IPV4__
  1672. else
  1673. #endif
  1674. #ifdef __UCLIBC_HAS_IPV4__
  1675. h = gethostbyaddr ((const void *)
  1676. &(((const struct sockaddr_in *)sa)->sin_addr),
  1677. sizeof(struct in_addr), AF_INET);
  1678. #endif /* __UCLIBC_HAS_IPV4__ */
  1679. if (h) {
  1680. char *c;
  1681. if ((flags & NI_NOFQDN)
  1682. && (__libc_getdomainname (domain, sizeof(domain)) == 0)
  1683. && (c = strstr (h->h_name, domain))
  1684. && (c != h->h_name) && (*(--c) == '.')) {
  1685. strncpy (host, h->h_name,
  1686. min(hostlen, (size_t) (c - h->h_name)));
  1687. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1688. } else {
  1689. strncpy (host, h->h_name, hostlen);
  1690. }
  1691. ok = 1;
  1692. }
  1693. }
  1694. if (!ok) {
  1695. const char *c;
  1696. if (flags & NI_NAMEREQD) {
  1697. errno = serrno;
  1698. return EAI_NONAME;
  1699. }
  1700. #ifdef __UCLIBC_HAS_IPV6__
  1701. if (sa->sa_family == AF_INET6) {
  1702. const struct sockaddr_in6 *sin6p;
  1703. sin6p = (const struct sockaddr_in6 *) sa;
  1704. c = inet_ntop (AF_INET6,
  1705. (const void *) &sin6p->sin6_addr, host, hostlen);
  1706. #if 0
  1707. /* Does scope id need to be supported? */
  1708. uint32_t scopeid;
  1709. scopeid = sin6p->sin6_scope_id;
  1710. if (scopeid != 0) {
  1711. /* Buffer is >= IFNAMSIZ+1. */
  1712. char scopebuf[IFNAMSIZ + 1];
  1713. char *scopeptr;
  1714. int ni_numericscope = 0;
  1715. size_t real_hostlen = strnlen (host, hostlen);
  1716. size_t scopelen = 0;
  1717. scopebuf[0] = SCOPE_DELIMITER;
  1718. scopebuf[1] = '\0';
  1719. scopeptr = &scopebuf[1];
  1720. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1721. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1722. if (if_indextoname (scopeid, scopeptr) == NULL)
  1723. ++ni_numericscope;
  1724. else
  1725. scopelen = strlen (scopebuf);
  1726. } else {
  1727. ++ni_numericscope;
  1728. }
  1729. if (ni_numericscope)
  1730. scopelen = 1 + snprintf (scopeptr,
  1731. (scopebuf
  1732. + sizeof scopebuf
  1733. - scopeptr),
  1734. "%u", scopeid);
  1735. if (real_hostlen + scopelen + 1 > hostlen)
  1736. return EAI_SYSTEM;
  1737. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1738. }
  1739. #endif
  1740. }
  1741. #endif /* __UCLIBC_HAS_IPV6__ */
  1742. #if defined __UCLIBC_HAS_IPV6__ && defined __UCLIBC_HAS_IPV4__
  1743. else
  1744. #endif /* __UCLIBC_HAS_IPV6__ && defined __UCLIBC_HAS_IPV4__ */
  1745. #if defined __UCLIBC_HAS_IPV4__
  1746. c = inet_ntop (AF_INET, (const void *)
  1747. &(((const struct sockaddr_in *) sa)->sin_addr),
  1748. host, hostlen);
  1749. #endif /* __UCLIBC_HAS_IPV4__ */
  1750. if (c == NULL) {
  1751. errno = serrno;
  1752. return EAI_SYSTEM;
  1753. }
  1754. ok = 1;
  1755. }
  1756. break;
  1757. case AF_LOCAL:
  1758. if (!(flags & NI_NUMERICHOST)) {
  1759. struct utsname utsname;
  1760. if (!uname (&utsname)) {
  1761. strncpy (host, utsname.nodename, hostlen);
  1762. break;
  1763. };
  1764. };
  1765. if (flags & NI_NAMEREQD) {
  1766. errno = serrno;
  1767. return EAI_NONAME;
  1768. }
  1769. strncpy (host, "localhost", hostlen);
  1770. break;
  1771. /* Already checked above
  1772. default:
  1773. return EAI_FAMILY;
  1774. */
  1775. }
  1776. if (serv && (servlen > 0)) {
  1777. if (sa->sa_family == AF_LOCAL) {
  1778. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1779. } else { /* AF_INET || AF_INET6 */
  1780. if (!(flags & NI_NUMERICSERV)) {
  1781. struct servent *s;
  1782. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1783. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1784. if (s) {
  1785. strncpy (serv, s->s_name, servlen);
  1786. goto DONE;
  1787. }
  1788. }
  1789. snprintf (serv, servlen, "%d",
  1790. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1791. }
  1792. }
  1793. DONE:
  1794. if (host && (hostlen > 0))
  1795. host[hostlen-1] = 0;
  1796. if (serv && (servlen > 0))
  1797. serv[servlen-1] = 0;
  1798. errno = serrno;
  1799. return 0;
  1800. }
  1801. libc_hidden_def(getnameinfo)
  1802. #endif
  1803. #ifdef L_gethostbyname_r
  1804. int gethostbyname_r(const char * name,
  1805. struct hostent * result_buf,
  1806. char * buf, size_t buflen,
  1807. struct hostent ** result,
  1808. int * h_errnop)
  1809. {
  1810. struct in_addr *in;
  1811. struct in_addr **addr_list;
  1812. char **alias;
  1813. unsigned char *packet;
  1814. struct resolv_answer a;
  1815. int i;
  1816. int __nameserversXX;
  1817. char ** __nameserverXX;
  1818. *result = NULL;
  1819. if (!name)
  1820. return EINVAL;
  1821. /* do /etc/hosts first */
  1822. {
  1823. int old_errno = errno; /* save the old errno and reset errno */
  1824. __set_errno(0); /* to check for missing /etc/hosts. */
  1825. i = __get_hosts_byname_r(name, AF_INET, result_buf,
  1826. buf, buflen, result, h_errnop);
  1827. if (i == 0)
  1828. return i;
  1829. switch (*h_errnop) {
  1830. case HOST_NOT_FOUND:
  1831. case NO_ADDRESS:
  1832. break;
  1833. case NETDB_INTERNAL:
  1834. if (errno == ENOENT) {
  1835. break;
  1836. }
  1837. /* else fall through */
  1838. default:
  1839. return i;
  1840. }
  1841. __set_errno(old_errno);
  1842. }
  1843. DPRINTF("Nothing found in /etc/hosts\n");
  1844. /* make sure user char * is aligned */
  1845. i = ALIGN_BUFFER_OFFSET(buf);
  1846. if (unlikely(i)) {
  1847. if (buflen < i)
  1848. return ERANGE;
  1849. buf += i;
  1850. buflen -= i;
  1851. }
  1852. *h_errnop = NETDB_INTERNAL;
  1853. if (buflen < sizeof(*in))
  1854. return ERANGE;
  1855. in = (struct in_addr*)buf;
  1856. buf += sizeof(*in);
  1857. buflen -= sizeof(*in);
  1858. if (buflen < sizeof(*addr_list)*2)
  1859. return ERANGE;
  1860. addr_list = (struct in_addr**)buf;
  1861. buf += sizeof(*addr_list)*2;
  1862. buflen -= sizeof(*addr_list)*2;
  1863. addr_list[0] = in;
  1864. addr_list[1] = 0;
  1865. if (buflen < sizeof(char *)*ALIAS_DIM)
  1866. return ERANGE;
  1867. alias = (char **)buf;
  1868. buf += sizeof(char **)*ALIAS_DIM;
  1869. buflen -= sizeof(char **)*ALIAS_DIM;
  1870. if (buflen < 256)
  1871. return ERANGE;
  1872. strncpy(buf, name, buflen);
  1873. alias[0] = buf;
  1874. alias[1] = NULL;
  1875. /* First check if this is already an address */
  1876. if (inet_aton(name, in)) {
  1877. result_buf->h_name = buf;
  1878. result_buf->h_addrtype = AF_INET;
  1879. result_buf->h_length = sizeof(*in);
  1880. result_buf->h_addr_list = (char **) addr_list;
  1881. result_buf->h_aliases = alias;
  1882. *result = result_buf;
  1883. *h_errnop = NETDB_SUCCESS;
  1884. return NETDB_SUCCESS;
  1885. }
  1886. __open_nameservers();
  1887. /*for (;;)*/ {
  1888. //FIXME: why was it a loop? It never loops...
  1889. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1890. __nameserversXX = __nameservers;
  1891. __nameserverXX = __nameserver;
  1892. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1893. a.buf = buf;
  1894. a.buflen = buflen;
  1895. a.add_count = 0;
  1896. i = __dns_lookup(name, T_A, __nameserversXX, __nameserverXX, &packet, &a);
  1897. if (i < 0) {
  1898. *h_errnop = HOST_NOT_FOUND;
  1899. DPRINTF("__dns_lookup\n");
  1900. return TRY_AGAIN;
  1901. }
  1902. if ((a.rdlength + sizeof(struct in_addr*)) * a.add_count + 256 > buflen) {
  1903. free(a.dotted);
  1904. free(packet);
  1905. *h_errnop = NETDB_INTERNAL;
  1906. DPRINTF("buffer too small for all addresses\n");
  1907. return ERANGE;
  1908. }
  1909. if (a.add_count > 0) {
  1910. memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
  1911. addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
  1912. addr_list[0] = in;
  1913. for (i = a.add_count - 1; i >= 0; --i)
  1914. addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
  1915. addr_list[a.add_count + 1] = 0;
  1916. buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
  1917. buf = (char*)&addr_list[a.add_count + 2];
  1918. }
  1919. strncpy(buf, a.dotted, buflen);
  1920. free(a.dotted);
  1921. if (a.atype == T_A) { /* ADDRESS */
  1922. memcpy(in, a.rdata, sizeof(*in));
  1923. result_buf->h_name = buf;
  1924. result_buf->h_addrtype = AF_INET;
  1925. result_buf->h_length = sizeof(*in);
  1926. result_buf->h_addr_list = (char **) addr_list;
  1927. #ifdef __UCLIBC_MJN3_ONLY__
  1928. #warning TODO -- generate the full list
  1929. #endif
  1930. result_buf->h_aliases = alias; /* TODO: generate the full list */
  1931. free(packet);
  1932. /*was: break;*/
  1933. *result = result_buf;
  1934. *h_errnop = NETDB_SUCCESS;
  1935. return NETDB_SUCCESS;
  1936. }
  1937. free(packet);
  1938. *h_errnop = HOST_NOT_FOUND;
  1939. return TRY_AGAIN;
  1940. }
  1941. /*
  1942. *result = result_buf;
  1943. *h_errnop = NETDB_SUCCESS;
  1944. return NETDB_SUCCESS;
  1945. */
  1946. }
  1947. libc_hidden_def(gethostbyname_r)
  1948. #endif
  1949. #ifdef L_gethostbyname2_r
  1950. int gethostbyname2_r(const char *name, int family,
  1951. struct hostent * result_buf,
  1952. char * buf, size_t buflen,
  1953. struct hostent ** result,
  1954. int * h_errnop)
  1955. {
  1956. #ifndef __UCLIBC_HAS_IPV6__
  1957. return family == (AF_INET)
  1958. ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop)
  1959. : HOST_NOT_FOUND;
  1960. #else /* __UCLIBC_HAS_IPV6__ */
  1961. struct in6_addr *in;
  1962. struct in6_addr **addr_list;
  1963. unsigned char *packet;
  1964. struct resolv_answer a;
  1965. int i;
  1966. int nest = 0;
  1967. int __nameserversXX;
  1968. char ** __nameserverXX;
  1969. int wrong_af = 0;
  1970. if (family == AF_INET)
  1971. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1972. if (family != AF_INET6)
  1973. return EINVAL;
  1974. __open_nameservers();
  1975. *result = NULL;
  1976. if (!name)
  1977. return EINVAL;
  1978. /* do /etc/hosts first */
  1979. {
  1980. int old_errno = errno; /* Save the old errno and reset errno */
  1981. __set_errno(0); /* to check for missing /etc/hosts. */
  1982. i = __get_hosts_byname_r(name, family, result_buf,
  1983. buf, buflen, result, h_errnop);
  1984. if (i == NETDB_SUCCESS) {
  1985. //FIXME: restore errno?
  1986. return i;
  1987. }
  1988. switch (*h_errnop) {
  1989. case HOST_NOT_FOUND:
  1990. wrong_af = (i == TRY_AGAIN);
  1991. case NO_ADDRESS:
  1992. break;
  1993. case NETDB_INTERNAL:
  1994. if (errno == ENOENT) {
  1995. break;
  1996. }
  1997. /* else fall through */
  1998. default:
  1999. return i;
  2000. }
  2001. __set_errno(old_errno);
  2002. }
  2003. DPRINTF("Nothing found in /etc/hosts\n");
  2004. *h_errnop = NETDB_INTERNAL;
  2005. if (buflen < sizeof(*in))
  2006. return ERANGE;
  2007. in = (struct in6_addr*)buf;
  2008. buf += sizeof(*in);
  2009. buflen -= sizeof(*in);
  2010. if (buflen < sizeof(*addr_list)*2)
  2011. return ERANGE;
  2012. addr_list = (struct in6_addr**)buf;
  2013. buf += sizeof(*addr_list)*2;
  2014. buflen -= sizeof(*addr_list)*2;
  2015. addr_list[0] = in;
  2016. addr_list[1] = 0;
  2017. if (buflen < 256)
  2018. return ERANGE;
  2019. strncpy(buf, name, buflen);
  2020. /* First check if this is already an address */
  2021. if (inet_pton(AF_INET6, name, in)) {
  2022. result_buf->h_name = buf;
  2023. result_buf->h_addrtype = AF_INET6;
  2024. result_buf->h_length = sizeof(*in);
  2025. result_buf->h_addr_list = (char **) addr_list;
  2026. *result = result_buf;
  2027. *h_errnop = NETDB_SUCCESS;
  2028. return NETDB_SUCCESS;
  2029. }
  2030. /* What if /etc/hosts has it but it's not IPv6?
  2031. * F.e. "127.0.0.1 localhost". We don't do DNS query for such hosts -
  2032. * "ping localhost" should be fast even if DNS server is down! */
  2033. if (wrong_af) {
  2034. *h_errnop = HOST_NOT_FOUND;
  2035. return TRY_AGAIN;
  2036. }
  2037. memset(&a, '\0', sizeof(a));
  2038. for (;;) {
  2039. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  2040. __nameserversXX = __nameservers;
  2041. __nameserverXX = __nameserver;
  2042. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  2043. i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
  2044. if (i < 0) {
  2045. *h_errnop = HOST_NOT_FOUND;
  2046. return TRY_AGAIN;
  2047. }
  2048. strncpy(buf, a.dotted, buflen);
  2049. free(a.dotted);
  2050. if (a.atype == T_CNAME) { /* CNAME */
  2051. DPRINTF("Got a CNAME in gethostbyname()\n");
  2052. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  2053. free(packet);
  2054. if (i < 0) {
  2055. *h_errnop = NO_RECOVERY;
  2056. return -1;
  2057. }
  2058. if (++nest > MAX_RECURSE) {
  2059. *h_errnop = NO_RECOVERY;
  2060. return -1;
  2061. }
  2062. continue;
  2063. }
  2064. if (a.atype == T_AAAA) { /* ADDRESS */
  2065. memcpy(in, a.rdata, sizeof(*in));
  2066. result_buf->h_name = buf;
  2067. result_buf->h_addrtype = AF_INET6;
  2068. result_buf->h_length = sizeof(*in);
  2069. result_buf->h_addr_list = (char **) addr_list;
  2070. free(packet);
  2071. break;
  2072. }
  2073. free(packet);
  2074. *h_errnop = HOST_NOT_FOUND;
  2075. return TRY_AGAIN;
  2076. }
  2077. *result = result_buf;
  2078. *h_errnop = NETDB_SUCCESS;
  2079. return NETDB_SUCCESS;
  2080. #endif /* __UCLIBC_HAS_IPV6__ */
  2081. }
  2082. libc_hidden_def(gethostbyname2_r)
  2083. #endif
  2084. #ifdef L_gethostbyaddr_r
  2085. int gethostbyaddr_r(const void *addr, socklen_t len, int type,
  2086. struct hostent * result_buf,
  2087. char * buf, size_t buflen,
  2088. struct hostent ** result,
  2089. int * h_errnop)
  2090. {
  2091. struct in_addr *in;
  2092. struct in_addr **addr_list;
  2093. #ifdef __UCLIBC_HAS_IPV6__
  2094. char *qp;
  2095. size_t plen;
  2096. struct in6_addr *in6;
  2097. struct in6_addr **addr_list6;
  2098. #endif /* __UCLIBC_HAS_IPV6__ */
  2099. char **alias;
  2100. unsigned char *packet;
  2101. struct resolv_answer a;
  2102. int i;
  2103. int nest = 0;
  2104. int __nameserversXX;
  2105. char ** __nameserverXX;
  2106. *result = NULL;
  2107. if (!addr)
  2108. return EINVAL;
  2109. memset((char *) &a, '\0', sizeof(a));
  2110. switch (type) {
  2111. case AF_INET:
  2112. if (len != sizeof(struct in_addr))
  2113. return EINVAL;
  2114. break;
  2115. #ifdef __UCLIBC_HAS_IPV6__
  2116. case AF_INET6:
  2117. if (len != sizeof(struct in6_addr))
  2118. return EINVAL;
  2119. break;
  2120. #endif /* __UCLIBC_HAS_IPV6__ */
  2121. default:
  2122. return EINVAL;
  2123. }
  2124. /* do /etc/hosts first */
  2125. if ((i = __get_hosts_byaddr_r(addr, len, type, result_buf,
  2126. buf, buflen, result, h_errnop)) == 0)
  2127. return i;
  2128. switch (*h_errnop) {
  2129. case HOST_NOT_FOUND:
  2130. case NO_ADDRESS:
  2131. break;
  2132. default:
  2133. return i;
  2134. }
  2135. __open_nameservers();
  2136. #ifdef __UCLIBC_HAS_IPV6__
  2137. qp = buf;
  2138. plen = buflen;
  2139. #endif /* __UCLIBC_HAS_IPV6__ */
  2140. *h_errnop = NETDB_INTERNAL;
  2141. if (buflen < sizeof(*in))
  2142. return ERANGE;
  2143. in = (struct in_addr*)buf;
  2144. buf += sizeof(*in);
  2145. buflen -= sizeof(*in);
  2146. if (buflen < sizeof(*addr_list)*2)
  2147. return ERANGE;
  2148. addr_list = (struct in_addr**)buf;
  2149. buf += sizeof(*addr_list)*2;
  2150. buflen -= sizeof(*addr_list)*2;
  2151. if (buflen < sizeof(char *)*ALIAS_DIM)
  2152. return ERANGE;
  2153. alias = (char **)buf;
  2154. buf += sizeof(*alias)*ALIAS_DIM;
  2155. buflen -= sizeof(*alias)*ALIAS_DIM;
  2156. #ifdef __UCLIBC_HAS_IPV6__
  2157. if (plen < sizeof(*in6))
  2158. return ERANGE;
  2159. in6 = (struct in6_addr*)qp;
  2160. qp += sizeof(*in6);
  2161. plen -= sizeof(*in6);
  2162. if (plen < sizeof(*addr_list6)*2)
  2163. return ERANGE;
  2164. addr_list6 = (struct in6_addr**)qp;
  2165. qp += sizeof(*addr_list6)*2;
  2166. plen -= sizeof(*addr_list6)*2;
  2167. if (plen < buflen) {
  2168. buflen = plen;
  2169. buf = qp;
  2170. }
  2171. #endif /* __UCLIBC_HAS_IPV6__ */
  2172. if (buflen < 256)
  2173. return ERANGE;
  2174. if (type == AF_INET) {
  2175. unsigned char *tmp_addr = (unsigned char *)addr;
  2176. memcpy(&in->s_addr, addr, len);
  2177. addr_list[0] = in;
  2178. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  2179. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  2180. #ifdef __UCLIBC_HAS_IPV6__
  2181. } else {
  2182. memcpy(in6->s6_addr, addr, len);
  2183. addr_list6[0] = in6;
  2184. qp = buf;
  2185. for (i = len - 1; i >= 0; i--) {
  2186. qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
  2187. (in6->s6_addr[i] >> 4) & 0xf);
  2188. }
  2189. strcpy(qp, "ip6.arpa");
  2190. #endif /* __UCLIBC_HAS_IPV6__ */
  2191. }
  2192. addr_list[1] = 0;
  2193. alias[0] = buf;
  2194. alias[1] = 0;
  2195. for (;;) {
  2196. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  2197. __nameserversXX = __nameservers;
  2198. __nameserverXX = __nameserver;
  2199. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  2200. i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
  2201. if (i < 0) {
  2202. *h_errnop = HOST_NOT_FOUND;
  2203. return TRY_AGAIN;
  2204. }
  2205. strncpy(buf, a.dotted, buflen);
  2206. free(a.dotted);
  2207. if (a.atype == T_CNAME) { /* CNAME */
  2208. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  2209. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  2210. free(packet);
  2211. if (i < 0) {
  2212. *h_errnop = NO_RECOVERY;
  2213. return -1;
  2214. }
  2215. if (++nest > MAX_RECURSE) {
  2216. *h_errnop = NO_RECOVERY;
  2217. return -1;
  2218. }
  2219. continue;
  2220. }
  2221. if (a.atype == T_PTR) { /* ADDRESS */
  2222. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  2223. free(packet);
  2224. result_buf->h_name = buf;
  2225. result_buf->h_addrtype = type;
  2226. if (type == AF_INET) {
  2227. result_buf->h_length = sizeof(*in);
  2228. #ifdef __UCLIBC_HAS_IPV6__
  2229. } else {
  2230. result_buf->h_length = sizeof(*in6);
  2231. #endif /* __UCLIBC_HAS_IPV6__ */
  2232. }
  2233. result_buf->h_addr_list = (char **) addr_list;
  2234. result_buf->h_aliases = alias;
  2235. break;
  2236. }
  2237. free(packet);
  2238. *h_errnop = NO_ADDRESS;
  2239. return TRY_AGAIN;
  2240. }
  2241. *result = result_buf;
  2242. *h_errnop = NETDB_SUCCESS;
  2243. return NETDB_SUCCESS;
  2244. }
  2245. libc_hidden_def(gethostbyaddr_r)
  2246. #endif
  2247. #ifdef L_res_comp
  2248. /*
  2249. * Expand compressed domain name 'comp_dn' to full domain name.
  2250. * 'msg' is a pointer to the begining of the message,
  2251. * 'eomorig' points to the first location after the message,
  2252. * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  2253. * Return size of compressed name or -1 if there was an error.
  2254. */
  2255. int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  2256. char *dst, int dstsiz)
  2257. {
  2258. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  2259. if (n > 0 && dst[0] == '.')
  2260. dst[0] = '\0';
  2261. return n;
  2262. }
  2263. #endif /* L_res_comp */
  2264. #ifdef L_ns_name
  2265. /*
  2266. * printable(ch)
  2267. * Thinking in noninternationalized USASCII (per the DNS spec),
  2268. * is this character visible and not a space when printed ?
  2269. * return:
  2270. * boolean.
  2271. */
  2272. static int printable(int ch)
  2273. {
  2274. return (ch > 0x20 && ch < 0x7f);
  2275. }
  2276. /*
  2277. * special(ch)
  2278. * Thinking in noninternationalized USASCII (per the DNS spec),
  2279. * is this characted special ("in need of quoting") ?
  2280. * return:
  2281. * boolean.
  2282. */
  2283. static int special(int ch)
  2284. {
  2285. switch (ch) {
  2286. case 0x22: /* '"' */
  2287. case 0x2E: /* '.' */
  2288. case 0x3B: /* ';' */
  2289. case 0x5C: /* '\\' */
  2290. /* Special modifiers in zone files. */
  2291. case 0x40: /* '@' */
  2292. case 0x24: /* '$' */
  2293. return 1;
  2294. default:
  2295. return 0;
  2296. }
  2297. }
  2298. /*
  2299. * ns_name_uncompress(msg, eom, src, dst, dstsiz)
  2300. * Expand compressed domain name to presentation format.
  2301. * return:
  2302. * Number of bytes read out of `src', or -1 (with errno set).
  2303. * note:
  2304. * Root domain returns as "." not "".
  2305. */
  2306. int ns_name_uncompress(const u_char *msg, const u_char *eom,
  2307. const u_char *src, char *dst, size_t dstsiz)
  2308. {
  2309. u_char tmp[NS_MAXCDNAME];
  2310. int n;
  2311. if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
  2312. return -1;
  2313. if (ns_name_ntop(tmp, dst, dstsiz) == -1)
  2314. return -1;
  2315. return n;
  2316. }
  2317. libc_hidden_def(ns_name_uncompress)
  2318. /*
  2319. * ns_name_ntop(src, dst, dstsiz)
  2320. * Convert an encoded domain name to printable ascii as per RFC1035.
  2321. * return:
  2322. * Number of bytes written to buffer, or -1 (with errno set)
  2323. * notes:
  2324. * The root is returned as "."
  2325. * All other domains are returned in non absolute form
  2326. */
  2327. int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
  2328. {
  2329. static const char digits[] = "0123456789";
  2330. const u_char *cp;
  2331. char *dn, *eom;
  2332. u_char c;
  2333. u_int n;
  2334. cp = src;
  2335. dn = dst;
  2336. eom = dst + dstsiz;
  2337. while ((n = *cp++) != 0) {
  2338. if ((n & NS_CMPRSFLGS) != 0) {
  2339. /* Some kind of compression pointer. */
  2340. __set_errno(EMSGSIZE);
  2341. return -1;
  2342. }
  2343. if (dn != dst) {
  2344. if (dn >= eom) {
  2345. __set_errno(EMSGSIZE);
  2346. return -1;
  2347. }
  2348. *dn++ = '.';
  2349. }
  2350. if (dn + n >= eom) {
  2351. __set_errno(EMSGSIZE);
  2352. return -1;
  2353. }
  2354. for ((void)NULL; n > 0; n--) {
  2355. c = *cp++;
  2356. if (special(c)) {
  2357. if (dn + 1 >= eom) {
  2358. __set_errno(EMSGSIZE);
  2359. return -1;
  2360. }
  2361. *dn++ = '\\';
  2362. *dn++ = (char)c;
  2363. } else if (!printable(c)) {
  2364. if (dn + 3 >= eom) {
  2365. __set_errno(EMSGSIZE);
  2366. return -1;
  2367. }
  2368. *dn++ = '\\';
  2369. *dn++ = digits[c / 100];
  2370. *dn++ = digits[(c % 100) / 10];
  2371. *dn++ = digits[c % 10];
  2372. } else {
  2373. if (dn >= eom) {
  2374. __set_errno(EMSGSIZE);
  2375. return -1;
  2376. }
  2377. *dn++ = (char)c;
  2378. }
  2379. }
  2380. }
  2381. if (dn == dst) {
  2382. if (dn >= eom) {
  2383. __set_errno(EMSGSIZE);
  2384. return -1;
  2385. }
  2386. *dn++ = '.';
  2387. }
  2388. if (dn >= eom) {
  2389. __set_errno(EMSGSIZE);
  2390. return -1;
  2391. }
  2392. *dn++ = '\0';
  2393. return (dn - dst);
  2394. }
  2395. libc_hidden_def(ns_name_ntop)
  2396. /*
  2397. * ns_name_unpack(msg, eom, src, dst, dstsiz)
  2398. * Unpack a domain name from a message, source may be compressed.
  2399. * return:
  2400. * -1 if it fails, or consumed octets if it succeeds.
  2401. */
  2402. int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
  2403. u_char *dst, size_t dstsiz)
  2404. {
  2405. const u_char *srcp, *dstlim;
  2406. u_char *dstp;
  2407. int n, len, checked;
  2408. len = -1;
  2409. checked = 0;
  2410. dstp = dst;
  2411. srcp = src;
  2412. dstlim = dst + dstsiz;
  2413. if (srcp < msg || srcp >= eom) {
  2414. __set_errno(EMSGSIZE);
  2415. return -1;
  2416. }
  2417. /* Fetch next label in domain name. */
  2418. while ((n = *srcp++) != 0) {
  2419. /* Check for indirection. */
  2420. switch (n & NS_CMPRSFLGS) {
  2421. case 0:
  2422. /* Limit checks. */
  2423. if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  2424. __set_errno(EMSGSIZE);
  2425. return -1;
  2426. }
  2427. checked += n + 1;
  2428. *dstp++ = n;
  2429. memcpy(dstp, srcp, n);
  2430. dstp += n;
  2431. srcp += n;
  2432. break;
  2433. case NS_CMPRSFLGS:
  2434. if (srcp >= eom) {
  2435. __set_errno(EMSGSIZE);
  2436. return -1;
  2437. }
  2438. if (len < 0)
  2439. len = srcp - src + 1;
  2440. srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  2441. if (srcp < msg || srcp >= eom) { /* Out of range. */
  2442. __set_errno(EMSGSIZE);
  2443. return -1;
  2444. }
  2445. checked += 2;
  2446. /*
  2447. * Check for loops in the compressed name;
  2448. * if we've looked at the whole message,
  2449. * there must be a loop.
  2450. */
  2451. if (checked >= eom - msg) {
  2452. __set_errno(EMSGSIZE);
  2453. return -1;
  2454. }
  2455. break;
  2456. default:
  2457. __set_errno(EMSGSIZE);
  2458. return -1; /* flag error */
  2459. }
  2460. }
  2461. *dstp = '\0';
  2462. if (len < 0)
  2463. len = srcp - src;
  2464. return len;
  2465. }
  2466. libc_hidden_def(ns_name_unpack)
  2467. #endif /* L_ns_name */
  2468. /* vi: set sw=4 ts=4: */