resolv.c 67 KB

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