resolv.c 67 KB

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