resolv.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760
  1. /* resolv.c: DNS Resolver
  2. *
  3. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
  4. * The Silver Hammer Group, Ltd.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. */
  11. /*
  12. * Portions Copyright (c) 1985, 1993
  13. * The Regents of the University of California. All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 4. Neither the name of the University nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. */
  39. /*
  40. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  41. *
  42. * Permission to use, copy, modify, and distribute this software for any
  43. * purpose with or without fee is hereby granted, provided that the above
  44. * copyright notice and this permission notice appear in all copies, and that
  45. * the name of Digital Equipment Corporation not be used in advertising or
  46. * publicity pertaining to distribution of the document or software without
  47. * specific, written prior permission.
  48. *
  49. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  50. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  51. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  52. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  53. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  54. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  55. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  56. * SOFTWARE.
  57. */
  58. /*
  59. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  60. *
  61. * Permission to use, copy, modify, and distribute this software for any
  62. * purpose with or without fee is hereby granted, provided that the above
  63. * copyright notice and this permission notice appear in all copies.
  64. *
  65. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  66. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  67. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  68. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  69. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  70. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  71. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  72. * SOFTWARE.
  73. */
  74. /*
  75. *
  76. * 5-Oct-2000 W. Greathouse wgreathouse@smva.com
  77. * Fix memory leak and memory corruption.
  78. * -- Every name resolution resulted in
  79. * a new parse of resolv.conf and new
  80. * copy of nameservers allocated by
  81. * strdup.
  82. * -- Every name resolution resulted in
  83. * a new read of resolv.conf without
  84. * resetting index from prior read...
  85. * resulting in exceeding array bounds.
  86. *
  87. * Limit nameservers read from resolv.conf
  88. *
  89. * Add "search" domains from resolv.conf
  90. *
  91. * Some systems will return a security
  92. * signature along with query answer for
  93. * dynamic DNS entries.
  94. * -- skip/ignore this answer
  95. *
  96. * Include arpa/nameser.h for defines.
  97. *
  98. * General cleanup
  99. *
  100. * 20-Jun-2001 Michal Moskal <malekith@pld.org.pl>
  101. * partial IPv6 support (i.e. gethostbyname2() and resolve_address2()
  102. * functions added), IPv6 nameservers are also supported.
  103. *
  104. * 6-Oct-2001 Jari Korva <jari.korva@iki.fi>
  105. * more IPv6 support (IPv6 support for gethostbyaddr();
  106. * address family parameter and improved IPv6 support for get_hosts_byname
  107. * and read_etc_hosts; getnameinfo() port from glibc; defined
  108. * defined ip6addr_any and in6addr_loopback)
  109. *
  110. * 2-Feb-2002 Erik Andersen <andersen@codepoet.org>
  111. * Added gethostent(), sethostent(), and endhostent()
  112. *
  113. * 17-Aug-2002 Manuel Novoa III <mjn3@codepoet.org>
  114. * Fixed __read_etc_hosts_r to return alias list, and modified buffer
  115. * allocation accordingly. See MAX_ALIASES and ALIAS_DIM below.
  116. * This fixes the segfault in the Python 2.2.1 socket test.
  117. *
  118. * 04-Jan-2003 Jay Kulpinski <jskulpin@berkshire.rr.com>
  119. * Fixed __decode_dotted to count the terminating null character
  120. * in a host name.
  121. *
  122. * 02-Oct-2003 Tony J. White <tjw@tjw.org>
  123. * Lifted dn_expand() and dependent ns_name_uncompress(), ns_name_unpack(),
  124. * and ns_name_ntop() from glibc 2.3.2 for compatibility with ipsec-tools
  125. * and openldap.
  126. *
  127. * 7-Sep-2004 Erik Andersen <andersen@codepoet.org>
  128. * Added gethostent_r()
  129. *
  130. */
  131. #define __FORCE_GLIBC
  132. #include <features.h>
  133. #include <string.h>
  134. #include <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. libc_hidden_proto(memcpy)
  164. libc_hidden_proto(memset)
  165. libc_hidden_proto(memmove)
  166. libc_hidden_proto(strchr)
  167. libc_hidden_proto(strcmp)
  168. libc_hidden_proto(strcpy)
  169. libc_hidden_proto(strdup)
  170. libc_hidden_proto(strlen)
  171. libc_hidden_proto(strncat)
  172. libc_hidden_proto(strncpy)
  173. /* libc_hidden_proto(strnlen) */
  174. libc_hidden_proto(strstr)
  175. 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()
  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(name, class, type, answer, anslen)
  1118. const char *name; /* domain name */
  1119. int class, type; /* class and type of query */
  1120. u_char *answer; /* buffer to put answer */
  1121. int anslen; /* size of answer */
  1122. {
  1123. const char *cp, * const *domain;
  1124. HEADER *hp = (HEADER *)(void *)answer;
  1125. u_int dots;
  1126. unsigned _state = 0;
  1127. int ret, saved_herrno;
  1128. u_long _res_options;
  1129. unsigned _res_ndots;
  1130. char **_res_dnsrch;
  1131. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1132. _res_options = _res.options;
  1133. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1134. if ((!name || !answer) || ((_res_options & RES_INIT) == 0 && res_init() == -1)) {
  1135. h_errno = NETDB_INTERNAL;
  1136. return (-1);
  1137. }
  1138. errno = 0;
  1139. h_errno = HOST_NOT_FOUND; /* default, if we never query */
  1140. dots = 0;
  1141. for (cp = name; *cp; cp++)
  1142. dots += (*cp == '.');
  1143. if (cp > name && *--cp == '.')
  1144. _state |= __TRAILING_DOT;
  1145. /*
  1146. * If there are dots in the name already, let's just give it a try
  1147. * 'as is'. The threshold can be set with the "ndots" option.
  1148. */
  1149. saved_herrno = -1;
  1150. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1151. _res_ndots = _res.ndots;
  1152. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1153. if (dots >= _res_ndots) {
  1154. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1155. if (ret > 0)
  1156. return (ret);
  1157. saved_herrno = h_errno;
  1158. _state |= __TRIED_AS_IS;
  1159. }
  1160. /*
  1161. * We do at least one level of search if
  1162. * - there is no dot and RES_DEFNAME is set, or
  1163. * - there is at least one dot, there is no trailing dot,
  1164. * and RES_DNSRCH is set.
  1165. */
  1166. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1167. _res_options = _res.options;
  1168. _res_dnsrch = _res.dnsrch;
  1169. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1170. if ((!dots && (_res_options & RES_DEFNAMES)) ||
  1171. (dots && !(_state & __TRAILING_DOT) && (_res_options & RES_DNSRCH))) {
  1172. bool done = 0;
  1173. for (domain = (const char * const *)_res_dnsrch;
  1174. *domain && !done;
  1175. domain++) {
  1176. ret = res_querydomain(name, *domain, class, type,
  1177. answer, anslen);
  1178. if (ret > 0)
  1179. return (ret);
  1180. /*
  1181. * If no server present, give up.
  1182. * If name isn't found in this domain,
  1183. * keep trying higher domains in the search list
  1184. * (if that's enabled).
  1185. * On a NO_DATA error, keep trying, otherwise
  1186. * a wildcard entry of another type could keep us
  1187. * from finding this entry higher in the domain.
  1188. * If we get some other error (negative answer or
  1189. * server failure), then stop searching up,
  1190. * but try the input name below in case it's
  1191. * fully-qualified.
  1192. */
  1193. if (errno == ECONNREFUSED) {
  1194. h_errno = TRY_AGAIN;
  1195. return (-1);
  1196. }
  1197. switch (h_errno) {
  1198. case NO_DATA:
  1199. _state |= __GOT_NODATA;
  1200. /* FALLTHROUGH */
  1201. case HOST_NOT_FOUND:
  1202. /* keep trying */
  1203. break;
  1204. case TRY_AGAIN:
  1205. if (hp->rcode == SERVFAIL) {
  1206. /* try next search element, if any */
  1207. _state |= __GOT_SERVFAIL;
  1208. break;
  1209. }
  1210. /* FALLTHROUGH */
  1211. default:
  1212. /* anything else implies that we're done */
  1213. done = 1;
  1214. }
  1215. /*
  1216. * if we got here for some reason other than DNSRCH,
  1217. * we only wanted one iteration of the loop, so stop.
  1218. */
  1219. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1220. _res_options = _res.options;
  1221. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1222. if (!(_res_options & RES_DNSRCH))
  1223. done = 1;
  1224. }
  1225. }
  1226. /*
  1227. * if we have not already tried the name "as is", do that now.
  1228. * note that we do this regardless of how many dots were in the
  1229. * name or whether it ends with a dot.
  1230. */
  1231. if (!(_state & __TRIED_AS_IS)) {
  1232. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1233. if (ret > 0)
  1234. return (ret);
  1235. }
  1236. /*
  1237. * if we got here, we didn't satisfy the search.
  1238. * if we did an initial full query, return that query's h_errno
  1239. * (note that we wouldn't be here if that query had succeeded).
  1240. * else if we ever got a nodata, send that back as the reason.
  1241. * else send back meaningless h_errno, that being the one from
  1242. * the last DNSRCH we did.
  1243. */
  1244. if (saved_herrno != -1)
  1245. h_errno = saved_herrno;
  1246. else if (_state & __GOT_NODATA)
  1247. h_errno = NO_DATA;
  1248. else if (_state & __GOT_SERVFAIL)
  1249. h_errno = TRY_AGAIN;
  1250. return (-1);
  1251. }
  1252. #undef __TRAILING_DOT
  1253. #undef __GOT_NODATA
  1254. #undef __GOT_SERVFAIL
  1255. #undef __TRIED_AS_IS
  1256. /*
  1257. * Perform a call on res_query on the concatenation of name and domain,
  1258. * removing a trailing dot from name if domain is NULL.
  1259. */
  1260. int res_querydomain(name, domain, class, type, answer, anslen)
  1261. const char *name, *domain;
  1262. int class, type; /* class and type of query */
  1263. u_char *answer; /* buffer to put answer */
  1264. int anslen; /* size of answer */
  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. else if (ok == AF_INET) {
  1616. if (addrlen < sizeof (struct sockaddr_in))
  1617. goto BAD_FAM;
  1618. #ifdef __UCLIBC_HAS_IPV6__
  1619. } else if (ok == AF_INET6) {
  1620. if (addrlen < sizeof (struct sockaddr_in6))
  1621. goto BAD_FAM;
  1622. #endif /* __UCLIBC_HAS_IPV6__ */
  1623. } else
  1624. BAD_FAM:
  1625. return EAI_FAMILY;
  1626. ok = 0;
  1627. if (host != NULL && hostlen > 0)
  1628. switch (sa->sa_family) {
  1629. case AF_INET:
  1630. #ifdef __UCLIBC_HAS_IPV6__
  1631. case AF_INET6:
  1632. #endif /* __UCLIBC_HAS_IPV6__ */
  1633. if (!(flags & NI_NUMERICHOST)) {
  1634. #ifdef __UCLIBC_HAS_IPV6__
  1635. if (sa->sa_family == AF_INET6)
  1636. h = gethostbyaddr ((const void *)
  1637. &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1638. sizeof(struct in6_addr), AF_INET6);
  1639. else
  1640. #endif /* __UCLIBC_HAS_IPV6__ */
  1641. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1642. sizeof(struct in_addr), AF_INET);
  1643. if (h) {
  1644. char *c;
  1645. if ((flags & NI_NOFQDN)
  1646. && (getdomainname (domain, sizeof(domain)) == 0)
  1647. && (c = strstr (h->h_name, domain))
  1648. && (c != h->h_name) && (*(--c) == '.')) {
  1649. strncpy (host, h->h_name,
  1650. min(hostlen, (size_t) (c - h->h_name)));
  1651. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1652. ok = 1;
  1653. } else {
  1654. strncpy (host, h->h_name, hostlen);
  1655. ok = 1;
  1656. }
  1657. }
  1658. }
  1659. if (!ok) {
  1660. if (flags & NI_NAMEREQD) {
  1661. errno = serrno;
  1662. return EAI_NONAME;
  1663. } else {
  1664. const char *c;
  1665. #ifdef __UCLIBC_HAS_IPV6__
  1666. if (sa->sa_family == AF_INET6) {
  1667. const struct sockaddr_in6 *sin6p;
  1668. sin6p = (const struct sockaddr_in6 *) sa;
  1669. c = inet_ntop (AF_INET6,
  1670. (const void *) &sin6p->sin6_addr, host, hostlen);
  1671. #if 0
  1672. /* Does scope id need to be supported? */
  1673. uint32_t scopeid;
  1674. scopeid = sin6p->sin6_scope_id;
  1675. if (scopeid != 0) {
  1676. /* Buffer is >= IFNAMSIZ+1. */
  1677. char scopebuf[IFNAMSIZ + 1];
  1678. char *scopeptr;
  1679. int ni_numericscope = 0;
  1680. size_t real_hostlen = strnlen (host, hostlen);
  1681. size_t scopelen = 0;
  1682. scopebuf[0] = SCOPE_DELIMITER;
  1683. scopebuf[1] = '\0';
  1684. scopeptr = &scopebuf[1];
  1685. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1686. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1687. if (if_indextoname (scopeid, scopeptr) == NULL)
  1688. ++ni_numericscope;
  1689. else
  1690. scopelen = strlen (scopebuf);
  1691. } else {
  1692. ++ni_numericscope;
  1693. }
  1694. if (ni_numericscope)
  1695. scopelen = 1 + snprintf (scopeptr,
  1696. (scopebuf
  1697. + sizeof scopebuf
  1698. - scopeptr),
  1699. "%u", scopeid);
  1700. if (real_hostlen + scopelen + 1 > hostlen)
  1701. return EAI_SYSTEM;
  1702. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1703. }
  1704. #endif
  1705. } else
  1706. #endif /* __UCLIBC_HAS_IPV6__ */
  1707. c = inet_ntop (AF_INET, (const void *)
  1708. &(((const struct sockaddr_in *) sa)->sin_addr),
  1709. host, hostlen);
  1710. if (c == NULL) {
  1711. errno = serrno;
  1712. return EAI_SYSTEM;
  1713. }
  1714. }
  1715. ok = 1;
  1716. }
  1717. break;
  1718. case AF_LOCAL:
  1719. if (!(flags & NI_NUMERICHOST)) {
  1720. struct utsname utsname;
  1721. if (!uname (&utsname)) {
  1722. strncpy (host, utsname.nodename, hostlen);
  1723. break;
  1724. };
  1725. };
  1726. if (flags & NI_NAMEREQD) {
  1727. errno = serrno;
  1728. return EAI_NONAME;
  1729. }
  1730. strncpy (host, "localhost", hostlen);
  1731. break;
  1732. /*Already checked above default:
  1733. return EAI_FAMILY;
  1734. */
  1735. }
  1736. if (serv && (servlen > 0)) {
  1737. if (sa->sa_family == AF_LOCAL) {
  1738. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1739. } else { /* AF_INET || AF_INET6 */
  1740. if (!(flags & NI_NUMERICSERV)) {
  1741. struct servent *s;
  1742. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1743. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1744. if (s) {
  1745. strncpy (serv, s->s_name, servlen);
  1746. goto DONE;
  1747. }
  1748. }
  1749. snprintf (serv, servlen, "%d",
  1750. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1751. }
  1752. }
  1753. DONE:
  1754. if (host && (hostlen > 0))
  1755. host[hostlen-1] = 0;
  1756. if (serv && (servlen > 0))
  1757. serv[servlen-1] = 0;
  1758. errno = serrno;
  1759. return 0;
  1760. }
  1761. libc_hidden_def(getnameinfo)
  1762. #endif
  1763. #ifdef L_gethostbyname_r
  1764. int gethostbyname_r(const char * name,
  1765. struct hostent * result_buf,
  1766. char * buf, size_t buflen,
  1767. struct hostent ** result,
  1768. int * h_errnop)
  1769. {
  1770. struct in_addr *in;
  1771. struct in_addr **addr_list;
  1772. char **alias;
  1773. unsigned char *packet;
  1774. struct resolv_answer a;
  1775. int i;
  1776. int __nameserversXX;
  1777. char ** __nameserverXX;
  1778. __open_nameservers();
  1779. *result = NULL;
  1780. if (!name)
  1781. return EINVAL;
  1782. /* do /etc/hosts first */
  1783. {
  1784. int old_errno = errno; /* Save the old errno and reset errno */
  1785. __set_errno(0); /* to check for missing /etc/hosts. */
  1786. if ((i = __get_hosts_byname_r(name, AF_INET, result_buf,
  1787. buf, buflen, result, h_errnop)) == 0)
  1788. return i;
  1789. switch (*h_errnop) {
  1790. case HOST_NOT_FOUND:
  1791. case NO_ADDRESS:
  1792. break;
  1793. case NETDB_INTERNAL:
  1794. if (errno == ENOENT) {
  1795. break;
  1796. }
  1797. /* else fall through */
  1798. default:
  1799. return i;
  1800. }
  1801. __set_errno(old_errno);
  1802. }
  1803. DPRINTF("Nothing found in /etc/hosts\n");
  1804. /* make sure user char * is aligned */
  1805. i = ALIGN_BUFFER_OFFSET(buf);
  1806. if (unlikely(i)) {
  1807. if (buflen < i)
  1808. return ERANGE;
  1809. buf += i;
  1810. buflen -= i;
  1811. }
  1812. *h_errnop = NETDB_INTERNAL;
  1813. if (buflen < sizeof(*in))
  1814. return ERANGE;
  1815. in = (struct in_addr*)buf;
  1816. buf += sizeof(*in);
  1817. buflen -= sizeof(*in);
  1818. if (buflen < sizeof(*addr_list)*2)
  1819. return ERANGE;
  1820. addr_list = (struct in_addr**)buf;
  1821. buf += sizeof(*addr_list)*2;
  1822. buflen -= sizeof(*addr_list)*2;
  1823. addr_list[0] = in;
  1824. addr_list[1] = 0;
  1825. if (buflen < sizeof(char *)*(ALIAS_DIM))
  1826. return ERANGE;
  1827. alias = (char **)buf;
  1828. buf += sizeof(char **)*(ALIAS_DIM);
  1829. buflen -= sizeof(char **)*(ALIAS_DIM);
  1830. if (buflen < 256)
  1831. return ERANGE;
  1832. strncpy(buf, name, buflen);
  1833. alias[0] = buf;
  1834. alias[1] = NULL;
  1835. /* First check if this is already an address */
  1836. if (inet_aton(name, in)) {
  1837. result_buf->h_name = buf;
  1838. result_buf->h_addrtype = AF_INET;
  1839. result_buf->h_length = sizeof(*in);
  1840. result_buf->h_addr_list = (char **) addr_list;
  1841. result_buf->h_aliases = alias;
  1842. *result = result_buf;
  1843. *h_errnop = NETDB_SUCCESS;
  1844. return NETDB_SUCCESS;
  1845. }
  1846. for (;;) {
  1847. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1848. __nameserversXX = __nameservers;
  1849. __nameserverXX = __nameserver;
  1850. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1851. a.buf = buf;
  1852. a.buflen = buflen;
  1853. a.add_count = 0;
  1854. i = __dns_lookup(name, T_A, __nameserversXX, __nameserverXX, &packet, &a);
  1855. if (i < 0) {
  1856. *h_errnop = HOST_NOT_FOUND;
  1857. DPRINTF("__dns_lookup\n");
  1858. return TRY_AGAIN;
  1859. }
  1860. if ((a.rdlength + sizeof(struct in_addr*)) * a.add_count + 256 > buflen) {
  1861. free(a.dotted);
  1862. free(packet);
  1863. *h_errnop = NETDB_INTERNAL;
  1864. DPRINTF("buffer too small for all addresses\n");
  1865. return ERANGE;
  1866. } else if (a.add_count > 0) {
  1867. memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
  1868. addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
  1869. addr_list[0] = in;
  1870. for (i = a.add_count - 1; i >= 0; --i)
  1871. addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
  1872. addr_list[a.add_count + 1] = 0;
  1873. buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
  1874. buf = (char*)&addr_list[a.add_count + 2];
  1875. }
  1876. strncpy(buf, a.dotted, buflen);
  1877. free(a.dotted);
  1878. if (a.atype == T_A) { /* ADDRESS */
  1879. memcpy(in, a.rdata, sizeof(*in));
  1880. result_buf->h_name = buf;
  1881. result_buf->h_addrtype = AF_INET;
  1882. result_buf->h_length = sizeof(*in);
  1883. result_buf->h_addr_list = (char **) addr_list;
  1884. #ifdef __UCLIBC_MJN3_ONLY__
  1885. #warning TODO -- generate the full list
  1886. #endif
  1887. result_buf->h_aliases = alias; /* TODO: generate the full list */
  1888. free(packet);
  1889. break;
  1890. } else {
  1891. free(packet);
  1892. *h_errnop = HOST_NOT_FOUND;
  1893. return TRY_AGAIN;
  1894. }
  1895. }
  1896. *result = result_buf;
  1897. *h_errnop = NETDB_SUCCESS;
  1898. return NETDB_SUCCESS;
  1899. }
  1900. libc_hidden_def(gethostbyname_r)
  1901. #endif
  1902. #ifdef L_gethostbyname2_r
  1903. int gethostbyname2_r(const char *name, int family,
  1904. struct hostent * result_buf,
  1905. char * buf, size_t buflen,
  1906. struct hostent ** result,
  1907. int * h_errnop)
  1908. {
  1909. #ifndef __UCLIBC_HAS_IPV6__
  1910. return family == (AF_INET)? gethostbyname_r(name, result_buf,
  1911. buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1912. #else /* __UCLIBC_HAS_IPV6__ */
  1913. struct in6_addr *in;
  1914. struct in6_addr **addr_list;
  1915. unsigned char *packet;
  1916. struct resolv_answer a;
  1917. int i;
  1918. int nest = 0;
  1919. int __nameserversXX;
  1920. char ** __nameserverXX;
  1921. if (family == AF_INET)
  1922. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1923. if (family != AF_INET6)
  1924. return EINVAL;
  1925. __open_nameservers();
  1926. *result = NULL;
  1927. if (!name)
  1928. return EINVAL;
  1929. /* do /etc/hosts first */
  1930. {
  1931. int old_errno = errno; /* Save the old errno and reset errno */
  1932. __set_errno(0); /* to check for missing /etc/hosts. */
  1933. if ((i = __get_hosts_byname_r(name, family, result_buf,
  1934. buf, buflen, result, h_errnop)) == 0)
  1935. return i;
  1936. switch (*h_errnop) {
  1937. case HOST_NOT_FOUND:
  1938. case NO_ADDRESS:
  1939. break;
  1940. case NETDB_INTERNAL:
  1941. if (errno == ENOENT) {
  1942. break;
  1943. }
  1944. /* else fall through */
  1945. default:
  1946. return i;
  1947. }
  1948. __set_errno(old_errno);
  1949. }
  1950. DPRINTF("Nothing found in /etc/hosts\n");
  1951. *h_errnop = NETDB_INTERNAL;
  1952. if (buflen < sizeof(*in))
  1953. return ERANGE;
  1954. in = (struct in6_addr*)buf;
  1955. buf += sizeof(*in);
  1956. buflen -= sizeof(*in);
  1957. if (buflen < sizeof(*addr_list)*2)
  1958. return ERANGE;
  1959. addr_list = (struct in6_addr**)buf;
  1960. buf += sizeof(*addr_list)*2;
  1961. buflen -= sizeof(*addr_list)*2;
  1962. addr_list[0] = in;
  1963. addr_list[1] = 0;
  1964. if (buflen < 256)
  1965. return ERANGE;
  1966. strncpy(buf, name, buflen);
  1967. /* First check if this is already an address */
  1968. if (inet_pton(AF_INET6, name, in)) {
  1969. result_buf->h_name = buf;
  1970. result_buf->h_addrtype = AF_INET6;
  1971. result_buf->h_length = sizeof(*in);
  1972. result_buf->h_addr_list = (char **) addr_list;
  1973. *result = result_buf;
  1974. *h_errnop = NETDB_SUCCESS;
  1975. return NETDB_SUCCESS;
  1976. }
  1977. memset((char *) &a, '\0', sizeof(a));
  1978. for (;;) {
  1979. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  1980. __nameserversXX = __nameservers;
  1981. __nameserverXX = __nameserver;
  1982. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  1983. i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
  1984. if (i < 0) {
  1985. *h_errnop = HOST_NOT_FOUND;
  1986. return TRY_AGAIN;
  1987. }
  1988. strncpy(buf, a.dotted, buflen);
  1989. free(a.dotted);
  1990. if (a.atype == T_CNAME) { /* CNAME */
  1991. DPRINTF("Got a CNAME in gethostbyname()\n");
  1992. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1993. free(packet);
  1994. if (i < 0) {
  1995. *h_errnop = NO_RECOVERY;
  1996. return -1;
  1997. }
  1998. if (++nest > MAX_RECURSE) {
  1999. *h_errnop = NO_RECOVERY;
  2000. return -1;
  2001. }
  2002. continue;
  2003. } else if (a.atype == T_AAAA) { /* ADDRESS */
  2004. memcpy(in, a.rdata, sizeof(*in));
  2005. result_buf->h_name = buf;
  2006. result_buf->h_addrtype = AF_INET6;
  2007. result_buf->h_length = sizeof(*in);
  2008. result_buf->h_addr_list = (char **) addr_list;
  2009. free(packet);
  2010. break;
  2011. } else {
  2012. free(packet);
  2013. *h_errnop = HOST_NOT_FOUND;
  2014. return TRY_AGAIN;
  2015. }
  2016. }
  2017. *result = result_buf;
  2018. *h_errnop = NETDB_SUCCESS;
  2019. return NETDB_SUCCESS;
  2020. #endif /* __UCLIBC_HAS_IPV6__ */
  2021. }
  2022. libc_hidden_def(gethostbyname2_r)
  2023. #endif
  2024. #ifdef L_gethostbyaddr_r
  2025. int gethostbyaddr_r (const void *addr, socklen_t len, int type,
  2026. struct hostent * result_buf,
  2027. char * buf, size_t buflen,
  2028. struct hostent ** result,
  2029. int * h_errnop)
  2030. {
  2031. struct in_addr *in;
  2032. struct in_addr **addr_list;
  2033. #ifdef __UCLIBC_HAS_IPV6__
  2034. char *qp;
  2035. size_t plen;
  2036. struct in6_addr *in6;
  2037. struct in6_addr **addr_list6;
  2038. #endif /* __UCLIBC_HAS_IPV6__ */
  2039. char **alias;
  2040. unsigned char *packet;
  2041. struct resolv_answer a;
  2042. int i;
  2043. int nest = 0;
  2044. int __nameserversXX;
  2045. char ** __nameserverXX;
  2046. *result = NULL;
  2047. if (!addr)
  2048. return EINVAL;
  2049. memset((char *) &a, '\0', sizeof(a));
  2050. switch (type) {
  2051. case AF_INET:
  2052. if (len != sizeof(struct in_addr))
  2053. return EINVAL;
  2054. break;
  2055. #ifdef __UCLIBC_HAS_IPV6__
  2056. case AF_INET6:
  2057. if (len != sizeof(struct in6_addr))
  2058. return EINVAL;
  2059. break;
  2060. #endif /* __UCLIBC_HAS_IPV6__ */
  2061. default:
  2062. return EINVAL;
  2063. }
  2064. /* do /etc/hosts first */
  2065. if ((i = __get_hosts_byaddr_r(addr, len, type, result_buf,
  2066. buf, buflen, result, h_errnop)) == 0)
  2067. return i;
  2068. switch (*h_errnop) {
  2069. case HOST_NOT_FOUND:
  2070. case NO_ADDRESS:
  2071. break;
  2072. default:
  2073. return i;
  2074. }
  2075. __open_nameservers();
  2076. #ifdef __UCLIBC_HAS_IPV6__
  2077. qp = buf;
  2078. plen = buflen;
  2079. #endif /* __UCLIBC_HAS_IPV6__ */
  2080. *h_errnop = NETDB_INTERNAL;
  2081. if (buflen < sizeof(*in))
  2082. return ERANGE;
  2083. in = (struct in_addr*)buf;
  2084. buf += sizeof(*in);
  2085. buflen -= sizeof(*in);
  2086. if (buflen < sizeof(*addr_list)*2)
  2087. return ERANGE;
  2088. addr_list = (struct in_addr**)buf;
  2089. buf += sizeof(*addr_list)*2;
  2090. buflen -= sizeof(*addr_list)*2;
  2091. if (buflen < sizeof(char *)*(ALIAS_DIM))
  2092. return ERANGE;
  2093. alias = (char **)buf;
  2094. buf += sizeof(*alias)*(ALIAS_DIM);
  2095. buflen -= sizeof(*alias)*(ALIAS_DIM);
  2096. #ifdef __UCLIBC_HAS_IPV6__
  2097. if (plen < sizeof(*in6))
  2098. return ERANGE;
  2099. in6 = (struct in6_addr*)qp;
  2100. qp += sizeof(*in6);
  2101. plen -= sizeof(*in6);
  2102. if (plen < sizeof(*addr_list6)*2)
  2103. return ERANGE;
  2104. addr_list6 = (struct in6_addr**)qp;
  2105. qp += sizeof(*addr_list6)*2;
  2106. plen -= sizeof(*addr_list6)*2;
  2107. if (plen < buflen) {
  2108. buflen = plen;
  2109. buf = qp;
  2110. }
  2111. #endif /* __UCLIBC_HAS_IPV6__ */
  2112. if (buflen < 256)
  2113. return ERANGE;
  2114. if (type == AF_INET) {
  2115. unsigned char *tmp_addr = (unsigned char *)addr;
  2116. memcpy(&in->s_addr, addr, len);
  2117. addr_list[0] = in;
  2118. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  2119. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  2120. #ifdef __UCLIBC_HAS_IPV6__
  2121. } else {
  2122. memcpy(in6->s6_addr, addr, len);
  2123. addr_list6[0] = in6;
  2124. qp = buf;
  2125. for (i = len - 1; i >= 0; i--) {
  2126. qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
  2127. (in6->s6_addr[i] >> 4) & 0xf);
  2128. }
  2129. strcpy(qp, "ip6.int");
  2130. #endif /* __UCLIBC_HAS_IPV6__ */
  2131. }
  2132. addr_list[1] = 0;
  2133. alias[0] = buf;
  2134. alias[1] = 0;
  2135. for (;;) {
  2136. __UCLIBC_MUTEX_LOCK(__resolv_lock);
  2137. __nameserversXX = __nameservers;
  2138. __nameserverXX = __nameserver;
  2139. __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
  2140. i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
  2141. if (i < 0) {
  2142. *h_errnop = HOST_NOT_FOUND;
  2143. return TRY_AGAIN;
  2144. }
  2145. strncpy(buf, a.dotted, buflen);
  2146. free(a.dotted);
  2147. if (a.atype == T_CNAME) { /* CNAME */
  2148. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  2149. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  2150. free(packet);
  2151. if (i < 0) {
  2152. *h_errnop = NO_RECOVERY;
  2153. return -1;
  2154. }
  2155. if (++nest > MAX_RECURSE) {
  2156. *h_errnop = NO_RECOVERY;
  2157. return -1;
  2158. }
  2159. continue;
  2160. } else if (a.atype == T_PTR) { /* ADDRESS */
  2161. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  2162. free(packet);
  2163. result_buf->h_name = buf;
  2164. result_buf->h_addrtype = type;
  2165. if (type == AF_INET) {
  2166. result_buf->h_length = sizeof(*in);
  2167. #ifdef __UCLIBC_HAS_IPV6__
  2168. } else {
  2169. result_buf->h_length = sizeof(*in6);
  2170. #endif /* __UCLIBC_HAS_IPV6__ */
  2171. }
  2172. result_buf->h_addr_list = (char **) addr_list;
  2173. result_buf->h_aliases = alias;
  2174. break;
  2175. } else {
  2176. free(packet);
  2177. *h_errnop = NO_ADDRESS;
  2178. return TRY_AGAIN;
  2179. }
  2180. }
  2181. *result = result_buf;
  2182. *h_errnop = NETDB_SUCCESS;
  2183. return NETDB_SUCCESS;
  2184. }
  2185. libc_hidden_def(gethostbyaddr_r)
  2186. #endif
  2187. #ifdef L_res_comp
  2188. /*
  2189. * Expand compressed domain name 'comp_dn' to full domain name.
  2190. * 'msg' is a pointer to the begining of the message,
  2191. * 'eomorig' points to the first location after the message,
  2192. * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  2193. * Return size of compressed name or -1 if there was an error.
  2194. */
  2195. int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  2196. char *dst, int dstsiz)
  2197. {
  2198. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  2199. if (n > 0 && dst[0] == '.')
  2200. dst[0] = '\0';
  2201. return (n);
  2202. }
  2203. #endif /* L_res_comp */
  2204. #ifdef L_ns_name
  2205. /*
  2206. * printable(ch)
  2207. * Thinking in noninternationalized USASCII (per the DNS spec),
  2208. * is this character visible and not a space when printed ?
  2209. * return:
  2210. * boolean.
  2211. */
  2212. static int printable(int ch)
  2213. {
  2214. return (ch > 0x20 && ch < 0x7f);
  2215. }
  2216. /*
  2217. * special(ch)
  2218. * Thinking in noninternationalized USASCII (per the DNS spec),
  2219. * is this characted special ("in need of quoting") ?
  2220. * return:
  2221. * boolean.
  2222. */
  2223. static int special(int ch)
  2224. {
  2225. switch (ch) {
  2226. case 0x22: /* '"' */
  2227. case 0x2E: /* '.' */
  2228. case 0x3B: /* ';' */
  2229. case 0x5C: /* '\\' */
  2230. /* Special modifiers in zone files. */
  2231. case 0x40: /* '@' */
  2232. case 0x24: /* '$' */
  2233. return (1);
  2234. default:
  2235. return (0);
  2236. }
  2237. }
  2238. /*
  2239. * ns_name_uncompress(msg, eom, src, dst, dstsiz)
  2240. * Expand compressed domain name to presentation format.
  2241. * return:
  2242. * Number of bytes read out of `src', or -1 (with errno set).
  2243. * note:
  2244. * Root domain returns as "." not "".
  2245. */
  2246. int ns_name_uncompress(const u_char *msg, const u_char *eom,
  2247. const u_char *src, char *dst, size_t dstsiz)
  2248. {
  2249. u_char tmp[NS_MAXCDNAME];
  2250. int n;
  2251. if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
  2252. return (-1);
  2253. if (ns_name_ntop(tmp, dst, dstsiz) == -1)
  2254. return (-1);
  2255. return (n);
  2256. }
  2257. libc_hidden_def(ns_name_uncompress)
  2258. /*
  2259. * ns_name_ntop(src, dst, dstsiz)
  2260. * Convert an encoded domain name to printable ascii as per RFC1035.
  2261. * return:
  2262. * Number of bytes written to buffer, or -1 (with errno set)
  2263. * notes:
  2264. * The root is returned as "."
  2265. * All other domains are returned in non absolute form
  2266. */
  2267. int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
  2268. const u_char *cp;
  2269. char *dn, *eom;
  2270. u_char c;
  2271. u_int n;
  2272. const char digits[] = "0123456789";
  2273. cp = src;
  2274. dn = dst;
  2275. eom = dst + dstsiz;
  2276. while ((n = *cp++) != 0) {
  2277. if ((n & NS_CMPRSFLGS) != 0) {
  2278. /* Some kind of compression pointer. */
  2279. __set_errno (EMSGSIZE);
  2280. return (-1);
  2281. }
  2282. if (dn != dst) {
  2283. if (dn >= eom) {
  2284. __set_errno (EMSGSIZE);
  2285. return (-1);
  2286. }
  2287. *dn++ = '.';
  2288. }
  2289. if (dn + n >= eom) {
  2290. __set_errno (EMSGSIZE);
  2291. return (-1);
  2292. }
  2293. for ((void)NULL; n > 0; n--) {
  2294. c = *cp++;
  2295. if (special(c)) {
  2296. if (dn + 1 >= eom) {
  2297. __set_errno (EMSGSIZE);
  2298. return (-1);
  2299. }
  2300. *dn++ = '\\';
  2301. *dn++ = (char)c;
  2302. } else if (!printable(c)) {
  2303. if (dn + 3 >= eom) {
  2304. __set_errno (EMSGSIZE);
  2305. return (-1);
  2306. }
  2307. *dn++ = '\\';
  2308. *dn++ = digits[c / 100];
  2309. *dn++ = digits[(c % 100) / 10];
  2310. *dn++ = digits[c % 10];
  2311. } else {
  2312. if (dn >= eom) {
  2313. __set_errno (EMSGSIZE);
  2314. return (-1);
  2315. }
  2316. *dn++ = (char)c;
  2317. }
  2318. }
  2319. }
  2320. if (dn == dst) {
  2321. if (dn >= eom) {
  2322. __set_errno (EMSGSIZE);
  2323. return (-1);
  2324. }
  2325. *dn++ = '.';
  2326. }
  2327. if (dn >= eom) {
  2328. __set_errno (EMSGSIZE);
  2329. return (-1);
  2330. }
  2331. *dn++ = '\0';
  2332. return (dn - dst);
  2333. }
  2334. libc_hidden_def(ns_name_ntop)
  2335. /*
  2336. * ns_name_unpack(msg, eom, src, dst, dstsiz)
  2337. * Unpack a domain name from a message, source may be compressed.
  2338. * return:
  2339. * -1 if it fails, or consumed octets if it succeeds.
  2340. */
  2341. int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
  2342. u_char *dst, size_t dstsiz)
  2343. {
  2344. const u_char *srcp, *dstlim;
  2345. u_char *dstp;
  2346. int n, len, checked;
  2347. len = -1;
  2348. checked = 0;
  2349. dstp = dst;
  2350. srcp = src;
  2351. dstlim = dst + dstsiz;
  2352. if (srcp < msg || srcp >= eom) {
  2353. __set_errno (EMSGSIZE);
  2354. return (-1);
  2355. }
  2356. /* Fetch next label in domain name. */
  2357. while ((n = *srcp++) != 0) {
  2358. /* Check for indirection. */
  2359. switch (n & NS_CMPRSFLGS) {
  2360. case 0:
  2361. /* Limit checks. */
  2362. if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  2363. __set_errno (EMSGSIZE);
  2364. return (-1);
  2365. }
  2366. checked += n + 1;
  2367. *dstp++ = n;
  2368. memcpy(dstp, srcp, n);
  2369. dstp += n;
  2370. srcp += n;
  2371. break;
  2372. case NS_CMPRSFLGS:
  2373. if (srcp >= eom) {
  2374. __set_errno (EMSGSIZE);
  2375. return (-1);
  2376. }
  2377. if (len < 0)
  2378. len = srcp - src + 1;
  2379. srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  2380. if (srcp < msg || srcp >= eom) { /* Out of range. */
  2381. __set_errno (EMSGSIZE);
  2382. return (-1);
  2383. }
  2384. checked += 2;
  2385. /*
  2386. * Check for loops in the compressed name;
  2387. * if we've looked at the whole message,
  2388. * there must be a loop.
  2389. */
  2390. if (checked >= eom - msg) {
  2391. __set_errno (EMSGSIZE);
  2392. return (-1);
  2393. }
  2394. break;
  2395. default:
  2396. __set_errno (EMSGSIZE);
  2397. return (-1); /* flag error */
  2398. }
  2399. }
  2400. *dstp = '\0';
  2401. if (len < 0)
  2402. len = srcp - src;
  2403. return (len);
  2404. }
  2405. libc_hidden_def(ns_name_unpack)
  2406. #endif /* L_ns_name */