resolv.c 68 KB

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