resolv.c 68 KB

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