resolv.c 68 KB

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