resolv.c 68 KB

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