resolv.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  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 <andersee@debian.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. */
  128. #define __FORCE_GLIBC
  129. #include <features.h>
  130. #include <string.h>
  131. #include <stdio.h>
  132. #include <signal.h>
  133. #include <errno.h>
  134. #include <sys/socket.h>
  135. #include <sys/types.h>
  136. #include <sys/time.h>
  137. #include <netinet/in.h>
  138. #include <arpa/inet.h>
  139. #include <stdlib.h>
  140. #include <unistd.h>
  141. #include <resolv.h>
  142. #include <netdb.h>
  143. #include <ctype.h>
  144. #include <arpa/nameser.h>
  145. #include <sys/utsname.h>
  146. #include <sys/un.h>
  147. #define MAX_RECURSE 5
  148. #define REPLY_TIMEOUT 10
  149. #define MAX_RETRIES 3
  150. #define MAX_SERVERS 3
  151. #define MAX_SEARCH 4
  152. #define MAX_ALIASES 5
  153. /* 1:ip + 1:full + MAX_ALIASES:aliases + 1:NULL */
  154. #define ALIAS_DIM (2 + MAX_ALIASES + 1)
  155. #undef DEBUG
  156. /*#define DEBUG*/
  157. #ifdef DEBUG
  158. #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
  159. #else
  160. #define DPRINTF(X,args...)
  161. #endif /* DEBUG */
  162. /* Global stuff (stuff needing to be locked to be thread safe)... */
  163. extern int __nameservers;
  164. extern char * __nameserver[MAX_SERVERS];
  165. extern int __searchdomains;
  166. extern char * __searchdomain[MAX_SEARCH];
  167. #ifdef __UCLIBC_HAS_THREADS__
  168. #include <pthread.h>
  169. extern pthread_mutex_t __resolv_lock;
  170. # define BIGLOCK __pthread_mutex_lock(&__resolv_lock)
  171. # define BIGUNLOCK __pthread_mutex_unlock(&__resolv_lock);
  172. #else
  173. # define BIGLOCK
  174. # define BIGUNLOCK
  175. #endif
  176. /* Structs */
  177. struct resolv_header {
  178. int id;
  179. int qr,opcode,aa,tc,rd,ra,rcode;
  180. int qdcount;
  181. int ancount;
  182. int nscount;
  183. int arcount;
  184. };
  185. struct resolv_question {
  186. char * dotted;
  187. int qtype;
  188. int qclass;
  189. };
  190. struct resolv_answer {
  191. char * dotted;
  192. int atype;
  193. int aclass;
  194. int ttl;
  195. int rdlength;
  196. unsigned char * rdata;
  197. int rdoffset;
  198. };
  199. enum etc_hosts_action {
  200. GET_HOSTS_BYNAME = 0,
  201. GETHOSTENT,
  202. GET_HOSTS_BYADDR,
  203. };
  204. /* function prototypes */
  205. extern int __get_hosts_byname_r(const char * name, int type,
  206. struct hostent * result_buf,
  207. char * buf, size_t buflen,
  208. struct hostent ** result,
  209. int * h_errnop);
  210. extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
  211. struct hostent * result_buf,
  212. char * buf, size_t buflen,
  213. struct hostent ** result,
  214. int * h_errnop);
  215. extern void __open_etc_hosts(FILE **fp);
  216. extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
  217. enum etc_hosts_action action,
  218. struct hostent * result_buf,
  219. char * buf, size_t buflen,
  220. struct hostent ** result,
  221. int * h_errnop);
  222. extern int __dns_lookup(const char * name, int type, int nscount,
  223. char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
  224. extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen);
  225. extern int __decode_dotted(const unsigned char * message, int offset,
  226. char * dest, int maxlen);
  227. extern int __length_dotted(const unsigned char * message, int offset);
  228. extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen);
  229. extern int __decode_header(unsigned char * data, struct resolv_header * h);
  230. extern int __encode_question(struct resolv_question * q,
  231. unsigned char * dest, int maxlen);
  232. extern int __decode_question(unsigned char * message, int offset,
  233. struct resolv_question * q);
  234. extern int __encode_answer(struct resolv_answer * a,
  235. unsigned char * dest, int maxlen);
  236. extern int __decode_answer(unsigned char * message, int offset,
  237. struct resolv_answer * a);
  238. extern int __length_question(unsigned char * message, int offset);
  239. extern int __open_nameservers(void);
  240. extern void __close_nameservers(void);
  241. extern int __dn_expand(const u_char *, const u_char *, const u_char *,
  242. char *, int);
  243. extern int __ns_name_uncompress(const u_char *, const u_char *,
  244. const u_char *, char *, size_t);
  245. extern int __ns_name_ntop(const u_char *, char *, size_t);
  246. extern int __ns_name_unpack(const u_char *, const u_char *, const u_char *,
  247. u_char *, size_t);
  248. #ifdef L_encodeh
  249. int __encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
  250. {
  251. if (maxlen < HFIXEDSZ)
  252. return -1;
  253. dest[0] = (h->id & 0xff00) >> 8;
  254. dest[1] = (h->id & 0x00ff) >> 0;
  255. dest[2] = (h->qr ? 0x80 : 0) |
  256. ((h->opcode & 0x0f) << 3) |
  257. (h->aa ? 0x04 : 0) |
  258. (h->tc ? 0x02 : 0) |
  259. (h->rd ? 0x01 : 0);
  260. dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
  261. dest[4] = (h->qdcount & 0xff00) >> 8;
  262. dest[5] = (h->qdcount & 0x00ff) >> 0;
  263. dest[6] = (h->ancount & 0xff00) >> 8;
  264. dest[7] = (h->ancount & 0x00ff) >> 0;
  265. dest[8] = (h->nscount & 0xff00) >> 8;
  266. dest[9] = (h->nscount & 0x00ff) >> 0;
  267. dest[10] = (h->arcount & 0xff00) >> 8;
  268. dest[11] = (h->arcount & 0x00ff) >> 0;
  269. return HFIXEDSZ;
  270. }
  271. #endif
  272. #ifdef L_decodeh
  273. int __decode_header(unsigned char *data, struct resolv_header *h)
  274. {
  275. h->id = (data[0] << 8) | data[1];
  276. h->qr = (data[2] & 0x80) ? 1 : 0;
  277. h->opcode = (data[2] >> 3) & 0x0f;
  278. h->aa = (data[2] & 0x04) ? 1 : 0;
  279. h->tc = (data[2] & 0x02) ? 1 : 0;
  280. h->rd = (data[2] & 0x01) ? 1 : 0;
  281. h->ra = (data[3] & 0x80) ? 1 : 0;
  282. h->rcode = data[3] & 0x0f;
  283. h->qdcount = (data[4] << 8) | data[5];
  284. h->ancount = (data[6] << 8) | data[7];
  285. h->nscount = (data[8] << 8) | data[9];
  286. h->arcount = (data[10] << 8) | data[11];
  287. return HFIXEDSZ;
  288. }
  289. #endif
  290. #ifdef L_encoded
  291. /* Encode a dotted string into nameserver transport-level encoding.
  292. This routine is fairly dumb, and doesn't attempt to compress
  293. the data */
  294. int __encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
  295. {
  296. int used = 0;
  297. while (dotted && *dotted) {
  298. char *c = strchr(dotted, '.');
  299. int l = c ? c - dotted : strlen(dotted);
  300. if (l >= (maxlen - used - 1))
  301. return -1;
  302. dest[used++] = l;
  303. memcpy(dest + used, dotted, l);
  304. used += l;
  305. if (c)
  306. dotted = c + 1;
  307. else
  308. break;
  309. }
  310. if (maxlen < 1)
  311. return -1;
  312. dest[used++] = 0;
  313. return used;
  314. }
  315. #endif
  316. #ifdef L_decoded
  317. /* Decode a dotted string from nameserver transport-level encoding.
  318. This routine understands compressed data. */
  319. int __decode_dotted(const unsigned char *data, int offset,
  320. char *dest, int maxlen)
  321. {
  322. int l;
  323. int measure = 1;
  324. int total = 0;
  325. int used = 0;
  326. if (!data)
  327. return -1;
  328. while ((l=data[offset++])) {
  329. if (measure)
  330. total++;
  331. if ((l & 0xc0) == (0xc0)) {
  332. if (measure)
  333. total++;
  334. /* compressed item, redirect */
  335. offset = ((l & 0x3f) << 8) | data[offset];
  336. measure = 0;
  337. continue;
  338. }
  339. if ((used + l + 1) >= maxlen)
  340. return -1;
  341. memcpy(dest + used, data + offset, l);
  342. offset += l;
  343. used += l;
  344. if (measure)
  345. total += l;
  346. if (data[offset] != 0)
  347. dest[used++] = '.';
  348. else
  349. dest[used++] = '\0';
  350. }
  351. /* The null byte must be counted too */
  352. if (measure) {
  353. total++;
  354. }
  355. DPRINTF("Total decode len = %d\n", total);
  356. return total;
  357. }
  358. #endif
  359. #ifdef L_lengthd
  360. int __length_dotted(const unsigned char *data, int offset)
  361. {
  362. int orig_offset = offset;
  363. int l;
  364. if (!data)
  365. return -1;
  366. while ((l = data[offset++])) {
  367. if ((l & 0xc0) == (0xc0)) {
  368. offset++;
  369. break;
  370. }
  371. offset += l;
  372. }
  373. return offset - orig_offset;
  374. }
  375. #endif
  376. #ifdef L_encodeq
  377. int __encode_question(struct resolv_question *q,
  378. unsigned char *dest, int maxlen)
  379. {
  380. int i;
  381. i = __encode_dotted(q->dotted, dest, maxlen);
  382. if (i < 0)
  383. return i;
  384. dest += i;
  385. maxlen -= i;
  386. if (maxlen < 4)
  387. return -1;
  388. dest[0] = (q->qtype & 0xff00) >> 8;
  389. dest[1] = (q->qtype & 0x00ff) >> 0;
  390. dest[2] = (q->qclass & 0xff00) >> 8;
  391. dest[3] = (q->qclass & 0x00ff) >> 0;
  392. return i + 4;
  393. }
  394. #endif
  395. #ifdef L_decodeq
  396. int __decode_question(unsigned char *message, int offset,
  397. struct resolv_question *q)
  398. {
  399. char temp[256];
  400. int i;
  401. i = __decode_dotted(message, offset, temp, sizeof(temp));
  402. if (i < 0)
  403. return i;
  404. offset += i;
  405. q->dotted = strdup(temp);
  406. q->qtype = (message[offset + 0] << 8) | message[offset + 1];
  407. q->qclass = (message[offset + 2] << 8) | message[offset + 3];
  408. return i + 4;
  409. }
  410. #endif
  411. #ifdef L_lengthq
  412. int __length_question(unsigned char *message, int offset)
  413. {
  414. int i;
  415. i = __length_dotted(message, offset);
  416. if (i < 0)
  417. return i;
  418. return i + 4;
  419. }
  420. #endif
  421. #ifdef L_encodea
  422. int __encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
  423. {
  424. int i;
  425. i = __encode_dotted(a->dotted, dest, maxlen);
  426. if (i < 0)
  427. return i;
  428. dest += i;
  429. maxlen -= i;
  430. if (maxlen < (RRFIXEDSZ+a->rdlength))
  431. return -1;
  432. *dest++ = (a->atype & 0xff00) >> 8;
  433. *dest++ = (a->atype & 0x00ff) >> 0;
  434. *dest++ = (a->aclass & 0xff00) >> 8;
  435. *dest++ = (a->aclass & 0x00ff) >> 0;
  436. *dest++ = (a->ttl & 0xff000000) >> 24;
  437. *dest++ = (a->ttl & 0x00ff0000) >> 16;
  438. *dest++ = (a->ttl & 0x0000ff00) >> 8;
  439. *dest++ = (a->ttl & 0x000000ff) >> 0;
  440. *dest++ = (a->rdlength & 0xff00) >> 8;
  441. *dest++ = (a->rdlength & 0x00ff) >> 0;
  442. memcpy(dest, a->rdata, a->rdlength);
  443. return i + RRFIXEDSZ + a->rdlength;
  444. }
  445. #endif
  446. #ifdef L_decodea
  447. int __decode_answer(unsigned char *message, int offset,
  448. struct resolv_answer *a)
  449. {
  450. char temp[256];
  451. int i;
  452. i = __decode_dotted(message, offset, temp, sizeof(temp));
  453. if (i < 0)
  454. return i;
  455. message += offset + i;
  456. a->dotted = strdup(temp);
  457. a->atype = (message[0] << 8) | message[1];
  458. message += 2;
  459. a->aclass = (message[0] << 8) | message[1];
  460. message += 2;
  461. a->ttl = (message[0] << 24) |
  462. (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
  463. message += 4;
  464. a->rdlength = (message[0] << 8) | message[1];
  465. message += 2;
  466. a->rdata = message;
  467. a->rdoffset = offset + i + RRFIXEDSZ;
  468. DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
  469. return i + RRFIXEDSZ + a->rdlength;
  470. }
  471. #endif
  472. #ifdef L_encodep
  473. int __encode_packet(struct resolv_header *h,
  474. struct resolv_question **q,
  475. struct resolv_answer **an,
  476. struct resolv_answer **ns,
  477. struct resolv_answer **ar,
  478. unsigned char *dest, int maxlen)
  479. {
  480. int i, total = 0;
  481. int j;
  482. i = __encode_header(h, dest, maxlen);
  483. if (i < 0)
  484. return i;
  485. dest += i;
  486. maxlen -= i;
  487. total += i;
  488. for (j = 0; j < h->qdcount; j++) {
  489. i = __encode_question(q[j], dest, maxlen);
  490. if (i < 0)
  491. return i;
  492. dest += i;
  493. maxlen -= i;
  494. total += i;
  495. }
  496. for (j = 0; j < h->ancount; j++) {
  497. i = __encode_answer(an[j], dest, maxlen);
  498. if (i < 0)
  499. return i;
  500. dest += i;
  501. maxlen -= i;
  502. total += i;
  503. }
  504. for (j = 0; j < h->nscount; j++) {
  505. i = __encode_answer(ns[j], dest, maxlen);
  506. if (i < 0)
  507. return i;
  508. dest += i;
  509. maxlen -= i;
  510. total += i;
  511. }
  512. for (j = 0; j < h->arcount; j++) {
  513. i = __encode_answer(ar[j], dest, maxlen);
  514. if (i < 0)
  515. return i;
  516. dest += i;
  517. maxlen -= i;
  518. total += i;
  519. }
  520. return total;
  521. }
  522. #endif
  523. #ifdef L_decodep
  524. int __decode_packet(unsigned char *data, struct resolv_header *h)
  525. {
  526. return __decode_header(data, h);
  527. }
  528. #endif
  529. #ifdef L_formquery
  530. int __form_query(int id, const char *name, int type, unsigned char *packet,
  531. int maxlen)
  532. {
  533. struct resolv_header h;
  534. struct resolv_question q;
  535. int i, j;
  536. memset(&h, 0, sizeof(h));
  537. h.id = id;
  538. h.qdcount = 1;
  539. q.dotted = (char *) name;
  540. q.qtype = type;
  541. q.qclass = C_IN; /* CLASS_IN */
  542. i = __encode_header(&h, packet, maxlen);
  543. if (i < 0)
  544. return i;
  545. j = __encode_question(&q, packet + i, maxlen - i);
  546. if (j < 0)
  547. return j;
  548. return i + j;
  549. }
  550. #endif
  551. #ifdef L_dnslookup
  552. #ifdef __UCLIBC_HAS_THREADS__
  553. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  554. # define LOCK __pthread_mutex_lock(&mylock)
  555. # define UNLOCK __pthread_mutex_unlock(&mylock);
  556. #else
  557. # define LOCK
  558. # define UNLOCK
  559. #endif
  560. /* Just for the record, having to lock __dns_lookup() just for these two globals
  561. * is pretty lame. I think these two variables can probably be de-global-ized,
  562. * which should eliminate the need for doing locking here... Needs a closer
  563. * look anyways. */
  564. static int ns=0, id=1;
  565. int __dns_lookup(const char *name, int type, int nscount, char **nsip,
  566. unsigned char **outpacket, struct resolv_answer *a)
  567. {
  568. int i, j, len, fd, pos, rc;
  569. struct timeval tv;
  570. fd_set fds;
  571. struct resolv_header h;
  572. struct resolv_question q;
  573. int retries = 0;
  574. unsigned char * packet = malloc(PACKETSZ);
  575. char *dns, *lookup = malloc(MAXDNAME);
  576. int variant = 0;
  577. struct sockaddr_in sa;
  578. #ifdef __UCLIBC_HAS_IPV6__
  579. int v6;
  580. struct sockaddr_in6 sa6;
  581. #endif
  582. fd = -1;
  583. if (!packet || !lookup || !nscount)
  584. goto fail;
  585. DPRINTF("Looking up type %d answer for '%s'\n", type, name);
  586. LOCK;
  587. ns %= nscount;
  588. UNLOCK;
  589. while (retries < MAX_RETRIES) {
  590. if (fd != -1)
  591. close(fd);
  592. memset(packet, 0, PACKETSZ);
  593. memset(&h, 0, sizeof(h));
  594. /* Mess with globals while under lock */
  595. LOCK;
  596. ++id;
  597. id &= 0xffff;
  598. h.id = id;
  599. dns = nsip[ns];
  600. UNLOCK;
  601. h.qdcount = 1;
  602. h.rd = 1;
  603. DPRINTF("encoding header\n", h.rd);
  604. i = __encode_header(&h, packet, PACKETSZ);
  605. if (i < 0)
  606. goto fail;
  607. strncpy(lookup,name,MAXDNAME);
  608. BIGLOCK;
  609. if (variant < __searchdomains && strchr(lookup, '.') == NULL)
  610. {
  611. strncat(lookup,".", MAXDNAME);
  612. strncat(lookup,__searchdomain[variant], MAXDNAME);
  613. }
  614. BIGUNLOCK;
  615. DPRINTF("lookup name: %s\n", lookup);
  616. q.dotted = (char *)lookup;
  617. q.qtype = type;
  618. q.qclass = C_IN; /* CLASS_IN */
  619. j = __encode_question(&q, packet+i, PACKETSZ-i);
  620. if (j < 0)
  621. goto fail;
  622. len = i + j;
  623. DPRINTF("On try %d, sending query to port %d of machine %s\n",
  624. retries+1, NAMESERVER_PORT, dns);
  625. #ifdef __UCLIBC_HAS_IPV6__
  626. v6 = inet_pton(AF_INET6, dns, &sa6.sin6_addr) > 0;
  627. fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  628. #else
  629. fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  630. #endif
  631. if (fd < 0) {
  632. retries++;
  633. continue;
  634. }
  635. /* Connect to the UDP socket so that asyncronous errors are returned */
  636. #ifdef __UCLIBC_HAS_IPV6__
  637. if (v6) {
  638. sa6.sin6_family = AF_INET6;
  639. sa6.sin6_port = htons(NAMESERVER_PORT);
  640. /* sa6.sin6_addr is already here */
  641. rc = connect(fd, (struct sockaddr *) &sa6, sizeof(sa6));
  642. } else {
  643. #endif
  644. sa.sin_family = AF_INET;
  645. sa.sin_port = htons(NAMESERVER_PORT);
  646. sa.sin_addr.s_addr = inet_addr(dns);
  647. rc = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
  648. #ifdef __UCLIBC_HAS_IPV6__
  649. }
  650. #endif
  651. if (rc < 0) {
  652. if (errno == ENETUNREACH) {
  653. /* routing error, presume not transient */
  654. goto tryall;
  655. } else
  656. /* retry */
  657. retries++;
  658. continue;
  659. }
  660. DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
  661. len, h.id, h.qr);
  662. send(fd, packet, len, 0);
  663. FD_ZERO(&fds);
  664. FD_SET(fd, &fds);
  665. tv.tv_sec = REPLY_TIMEOUT;
  666. tv.tv_usec = 0;
  667. if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
  668. DPRINTF("Timeout\n");
  669. /* timed out, so retry send and receive,
  670. * to next nameserver on queue */
  671. goto tryall;
  672. }
  673. i = recv(fd, packet, 512, 0);
  674. if (i < HFIXEDSZ) {
  675. /* too short ! */
  676. goto again;
  677. }
  678. __decode_header(packet, &h);
  679. DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
  680. LOCK;
  681. if ((h.id != id) || (!h.qr)) {
  682. UNLOCK;
  683. /* unsolicited */
  684. goto again;
  685. }
  686. UNLOCK;
  687. DPRINTF("Got response %s\n", "(i think)!");
  688. DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
  689. h.qdcount, h.ancount, h.nscount, h.arcount);
  690. DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
  691. h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
  692. if ((h.rcode) || (h.ancount < 1)) {
  693. /* negative result, not present */
  694. goto again;
  695. }
  696. pos = HFIXEDSZ;
  697. for (j = 0; j < h.qdcount; j++) {
  698. DPRINTF("Skipping question %d at %d\n", j, pos);
  699. i = __length_question(packet, pos);
  700. DPRINTF("Length of question %d is %d\n", j, i);
  701. if (i < 0)
  702. goto again;
  703. pos += i;
  704. }
  705. DPRINTF("Decoding answer at pos %d\n", pos);
  706. for (j=0;j<h.ancount;j++)
  707. {
  708. i = __decode_answer(packet, pos, a);
  709. if (i<0) {
  710. DPRINTF("failed decode %d\n", i);
  711. goto again;
  712. }
  713. /* For all but T_SIG, accept first answer */
  714. if (a->atype != T_SIG)
  715. break;
  716. DPRINTF("skipping T_SIG %d\n", i);
  717. free(a->dotted);
  718. pos += i;
  719. }
  720. DPRINTF("Answer name = |%s|\n", a->dotted);
  721. DPRINTF("Answer type = |%d|\n", a->atype);
  722. close(fd);
  723. if (outpacket)
  724. *outpacket = packet;
  725. else
  726. free(packet);
  727. free(lookup);
  728. return (0); /* success! */
  729. tryall:
  730. /* if there are other nameservers, give them a go,
  731. otherwise return with error */
  732. {
  733. variant = 0;
  734. LOCK;
  735. ns = (ns + 1) % nscount;
  736. if (ns == 0)
  737. retries++;
  738. UNLOCK;
  739. continue;
  740. }
  741. again:
  742. /* if there are searchdomains, try them or fallback as passed */
  743. {
  744. int sdomains;
  745. BIGLOCK;
  746. sdomains=__searchdomains;
  747. BIGUNLOCK;
  748. if (variant < ((sdomains - 1) && strchr(lookup, '.') == NULL)) {
  749. /* next search */
  750. variant++;
  751. } else {
  752. /* next server, first search */
  753. LOCK;
  754. ns = (ns + 1) % nscount;
  755. if (ns == 0)
  756. retries++;
  757. UNLOCK;
  758. variant = 0;
  759. }
  760. }
  761. }
  762. fail:
  763. if (fd != -1)
  764. close(fd);
  765. if (lookup)
  766. free(lookup);
  767. if (packet)
  768. free(packet);
  769. return -1;
  770. }
  771. #endif
  772. #ifdef L_opennameservers
  773. int __nameservers;
  774. char * __nameserver[MAX_SERVERS];
  775. int __searchdomains;
  776. char * __searchdomain[MAX_SEARCH];
  777. #ifdef __UCLIBC_HAS_THREADS__
  778. pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER;
  779. #endif
  780. /*
  781. * we currently read formats not quite the same as that on normal
  782. * unix systems, we can have a list of nameservers after the keyword.
  783. */
  784. int __open_nameservers()
  785. {
  786. FILE *fp;
  787. int i;
  788. #define RESOLV_ARGS 5
  789. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  790. int argc;
  791. BIGLOCK;
  792. if (__nameservers > 0) {
  793. BIGUNLOCK;
  794. return 0;
  795. }
  796. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  797. (fp = fopen("/etc/config/resolv.conf", "r"))) {
  798. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  799. for (p = szBuffer; *p && isspace(*p); p++)
  800. /* skip white space */;
  801. if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  802. continue;
  803. argc = 0;
  804. while (*p && argc < RESOLV_ARGS) {
  805. argv[argc++] = p;
  806. while (*p && !isspace(*p) && *p != '\n')
  807. p++;
  808. while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  809. *p++ = '\0';
  810. }
  811. if (strcmp(argv[0], "nameserver") == 0) {
  812. for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
  813. __nameserver[__nameservers++] = strdup(argv[i]);
  814. DPRINTF("adding nameserver %s\n", argv[i]);
  815. }
  816. }
  817. /* domain and search are mutually exclusive, the last one wins */
  818. if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  819. while (__searchdomains > 0) {
  820. free(__searchdomain[--__searchdomains]);
  821. __searchdomain[__searchdomains] = NULL;
  822. }
  823. for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
  824. __searchdomain[__searchdomains++] = strdup(argv[i]);
  825. DPRINTF("adding search %s\n", argv[i]);
  826. }
  827. }
  828. }
  829. fclose(fp);
  830. } else {
  831. DPRINTF("failed to open %s\n", "resolv.conf");
  832. }
  833. DPRINTF("nameservers = %d\n", __nameservers);
  834. BIGUNLOCK;
  835. return 0;
  836. }
  837. #endif
  838. #ifdef L_closenameservers
  839. void __close_nameservers(void)
  840. {
  841. BIGLOCK;
  842. while (__nameservers > 0) {
  843. free(__nameserver[--__nameservers]);
  844. __nameserver[__nameservers] = NULL;
  845. }
  846. while (__searchdomains > 0) {
  847. free(__searchdomain[--__searchdomains]);
  848. __searchdomain[__searchdomains] = NULL;
  849. }
  850. BIGUNLOCK;
  851. }
  852. #endif
  853. #ifdef L_gethostbyname
  854. struct hostent *gethostbyname(const char *name)
  855. {
  856. static struct hostent h;
  857. static char buf[sizeof(struct in_addr) +
  858. sizeof(struct in_addr *)*2 +
  859. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  860. struct hostent *hp;
  861. gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
  862. return hp;
  863. }
  864. #endif
  865. #ifdef L_gethostbyname2
  866. struct hostent *gethostbyname2(const char *name, int family)
  867. {
  868. #ifndef __UCLIBC_HAS_IPV6__
  869. return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
  870. #else /* __UCLIBC_HAS_IPV6__ */
  871. static struct hostent h;
  872. static char buf[sizeof(struct in6_addr) +
  873. sizeof(struct in6_addr *)*2 +
  874. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  875. struct hostent *hp;
  876. gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
  877. return hp;
  878. #endif /* __UCLIBC_HAS_IPV6__ */
  879. }
  880. #endif
  881. #ifdef L_getnetbyname
  882. struct netent * getnetbyname(const char * name)
  883. {
  884. return NULL;
  885. }
  886. #endif
  887. #ifdef L_res_init
  888. struct __res_state _res;
  889. int res_init(void)
  890. {
  891. struct __res_state *rp = &(_res);
  892. __close_nameservers();
  893. __open_nameservers();
  894. rp->retrans = RES_TIMEOUT;
  895. rp->retry = 4;
  896. rp->options = RES_INIT;
  897. rp->id = (u_int) random();
  898. rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
  899. rp->nsaddr.sin_family = AF_INET;
  900. rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
  901. rp->ndots = 1;
  902. /** rp->pfcode = 0; **/
  903. rp->_vcsock = -1;
  904. /** rp->_flags = 0; **/
  905. /** rp->qhook = NULL; **/
  906. /** rp->rhook = NULL; **/
  907. /** rp->_u._ext.nsinit = 0; **/
  908. BIGLOCK;
  909. if(__searchdomains) {
  910. int i;
  911. for(i=0; i<__searchdomains; i++) {
  912. rp->dnsrch[i] = __searchdomain[i];
  913. }
  914. }
  915. if(__nameservers) {
  916. int i;
  917. struct in_addr a;
  918. for(i=0; i<__nameservers; i++) {
  919. if (inet_aton(__nameserver[i], &a)) {
  920. rp->nsaddr_list[i].sin_addr = a;
  921. rp->nsaddr_list[i].sin_family = AF_INET;
  922. rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
  923. }
  924. }
  925. }
  926. rp->nscount = __nameservers;
  927. BIGUNLOCK;
  928. return(0);
  929. }
  930. void res_close( void )
  931. {
  932. return;
  933. }
  934. #endif
  935. #ifdef L_res_query
  936. #ifndef MIN
  937. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  938. #endif
  939. int res_query(const char *dname, int class, int type,
  940. unsigned char *answer, int anslen)
  941. {
  942. int i;
  943. unsigned char * packet = 0;
  944. struct resolv_answer a;
  945. int __nameserversXX;
  946. char ** __nameserverXX;
  947. __open_nameservers();
  948. if (!dname || class != 1 /* CLASS_IN */)
  949. return(-1);
  950. memset((char *) &a, '\0', sizeof(a));
  951. BIGLOCK;
  952. __nameserversXX=__nameservers;
  953. __nameserverXX=__nameserver;
  954. BIGUNLOCK;
  955. i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
  956. if (i < 0)
  957. return(-1);
  958. free(a.dotted);
  959. if (a.atype == type) { /* CNAME*/
  960. if (anslen && answer)
  961. memcpy(answer, a.rdata, MIN(anslen, a.rdlength));
  962. if (packet)
  963. free(packet);
  964. return(MIN(anslen, a.rdlength));
  965. }
  966. if (packet)
  967. free(packet);
  968. return 0;
  969. }
  970. /*
  971. * Formulate a normal query, send, and retrieve answer in supplied buffer.
  972. * Return the size of the response on success, -1 on error.
  973. * If enabled, implement search rules until answer or unrecoverable failure
  974. * is detected. Error code, if any, is left in h_errno.
  975. */
  976. int res_search(name, class, type, answer, anslen)
  977. const char *name; /* domain name */
  978. int class, type; /* class and type of query */
  979. u_char *answer; /* buffer to put answer */
  980. int anslen; /* size of answer */
  981. {
  982. const char *cp, * const *domain;
  983. HEADER *hp = (HEADER *)(void *)answer;
  984. u_int dots;
  985. int trailing_dot, ret, saved_herrno;
  986. int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
  987. if (!name || !answer)
  988. return(-1);
  989. if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
  990. h_errno = NETDB_INTERNAL;
  991. return (-1);
  992. }
  993. errno = 0;
  994. h_errno = HOST_NOT_FOUND; /* default, if we never query */
  995. dots = 0;
  996. for (cp = name; *cp; cp++)
  997. dots += (*cp == '.');
  998. trailing_dot = 0;
  999. if (cp > name && *--cp == '.')
  1000. trailing_dot++;
  1001. /*
  1002. * If there are dots in the name already, let's just give it a try
  1003. * 'as is'. The threshold can be set with the "ndots" option.
  1004. */
  1005. saved_herrno = -1;
  1006. if (dots >= _res.ndots) {
  1007. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1008. if (ret > 0)
  1009. return (ret);
  1010. saved_herrno = h_errno;
  1011. tried_as_is++;
  1012. }
  1013. /*
  1014. * We do at least one level of search if
  1015. * - there is no dot and RES_DEFNAME is set, or
  1016. * - there is at least one dot, there is no trailing dot,
  1017. * and RES_DNSRCH is set.
  1018. */
  1019. if ((!dots && (_res.options & RES_DEFNAMES)) ||
  1020. (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
  1021. int done = 0;
  1022. for (domain = (const char * const *)_res.dnsrch;
  1023. *domain && !done;
  1024. domain++) {
  1025. ret = res_querydomain(name, *domain, class, type,
  1026. answer, anslen);
  1027. if (ret > 0)
  1028. return (ret);
  1029. /*
  1030. * If no server present, give up.
  1031. * If name isn't found in this domain,
  1032. * keep trying higher domains in the search list
  1033. * (if that's enabled).
  1034. * On a NO_DATA error, keep trying, otherwise
  1035. * a wildcard entry of another type could keep us
  1036. * from finding this entry higher in the domain.
  1037. * If we get some other error (negative answer or
  1038. * server failure), then stop searching up,
  1039. * but try the input name below in case it's
  1040. * fully-qualified.
  1041. */
  1042. if (errno == ECONNREFUSED) {
  1043. h_errno = TRY_AGAIN;
  1044. return (-1);
  1045. }
  1046. switch (h_errno) {
  1047. case NO_DATA:
  1048. got_nodata++;
  1049. /* FALLTHROUGH */
  1050. case HOST_NOT_FOUND:
  1051. /* keep trying */
  1052. break;
  1053. case TRY_AGAIN:
  1054. if (hp->rcode == SERVFAIL) {
  1055. /* try next search element, if any */
  1056. got_servfail++;
  1057. break;
  1058. }
  1059. /* FALLTHROUGH */
  1060. default:
  1061. /* anything else implies that we're done */
  1062. done++;
  1063. }
  1064. /*
  1065. * if we got here for some reason other than DNSRCH,
  1066. * we only wanted one iteration of the loop, so stop.
  1067. */
  1068. if (!(_res.options & RES_DNSRCH))
  1069. done++;
  1070. }
  1071. }
  1072. /*
  1073. * if we have not already tried the name "as is", do that now.
  1074. * note that we do this regardless of how many dots were in the
  1075. * name or whether it ends with a dot.
  1076. */
  1077. if (!tried_as_is) {
  1078. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1079. if (ret > 0)
  1080. return (ret);
  1081. }
  1082. /*
  1083. * if we got here, we didn't satisfy the search.
  1084. * if we did an initial full query, return that query's h_errno
  1085. * (note that we wouldn't be here if that query had succeeded).
  1086. * else if we ever got a nodata, send that back as the reason.
  1087. * else send back meaningless h_errno, that being the one from
  1088. * the last DNSRCH we did.
  1089. */
  1090. if (saved_herrno != -1)
  1091. h_errno = saved_herrno;
  1092. else if (got_nodata)
  1093. h_errno = NO_DATA;
  1094. else if (got_servfail)
  1095. h_errno = TRY_AGAIN;
  1096. return (-1);
  1097. }
  1098. /*
  1099. * Perform a call on res_query on the concatenation of name and domain,
  1100. * removing a trailing dot from name if domain is NULL.
  1101. */
  1102. int res_querydomain(name, domain, class, type, answer, anslen)
  1103. const char *name, *domain;
  1104. int class, type; /* class and type of query */
  1105. u_char *answer; /* buffer to put answer */
  1106. int anslen; /* size of answer */
  1107. {
  1108. char nbuf[MAXDNAME];
  1109. const char *longname = nbuf;
  1110. size_t n, d;
  1111. if (!name || !answer)
  1112. return(-1);
  1113. if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
  1114. h_errno = NETDB_INTERNAL;
  1115. return (-1);
  1116. }
  1117. #ifdef DEBUG
  1118. if (_res.options & RES_DEBUG)
  1119. printf(";; res_querydomain(%s, %s, %d, %d)\n",
  1120. name, domain?domain:"<Nil>", class, type);
  1121. #endif
  1122. if (domain == NULL) {
  1123. /*
  1124. * Check for trailing '.';
  1125. * copy without '.' if present.
  1126. */
  1127. n = strlen(name);
  1128. if (n + 1 > sizeof(nbuf)) {
  1129. h_errno = NO_RECOVERY;
  1130. return (-1);
  1131. }
  1132. if (n > 0 && name[--n] == '.') {
  1133. strncpy(nbuf, name, n);
  1134. nbuf[n] = '\0';
  1135. } else
  1136. longname = name;
  1137. } else {
  1138. n = strlen(name);
  1139. d = strlen(domain);
  1140. if (n + 1 + d + 1 > sizeof(nbuf)) {
  1141. h_errno = NO_RECOVERY;
  1142. return (-1);
  1143. }
  1144. snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
  1145. }
  1146. return (res_query(longname, class, type, answer, anslen));
  1147. }
  1148. /* res_mkquery */
  1149. /* res_send */
  1150. /* dn_comp */
  1151. /* dn_expand */
  1152. #endif
  1153. #ifdef L_gethostbyaddr
  1154. struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
  1155. {
  1156. static struct hostent h;
  1157. static char buf[
  1158. #ifndef __UCLIBC_HAS_IPV6__
  1159. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1160. #else
  1161. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1162. #endif /* __UCLIBC_HAS_IPV6__ */
  1163. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  1164. struct hostent *hp;
  1165. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  1166. return hp;
  1167. }
  1168. #endif
  1169. #ifdef L_read_etc_hosts_r
  1170. void __open_etc_hosts(FILE **fp)
  1171. {
  1172. if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
  1173. *fp = fopen("/etc/config/hosts", "r");
  1174. }
  1175. return;
  1176. }
  1177. int __read_etc_hosts_r(FILE * fp, const char * name, int type,
  1178. enum etc_hosts_action action,
  1179. struct hostent * result_buf,
  1180. char * buf, size_t buflen,
  1181. struct hostent ** result,
  1182. int * h_errnop)
  1183. {
  1184. struct in_addr *in=NULL;
  1185. struct in_addr **addr_list=NULL;
  1186. #ifdef __UCLIBC_HAS_IPV6__
  1187. struct in6_addr *in6=NULL;
  1188. struct in6_addr **addr_list6=NULL;
  1189. #endif /* __UCLIBC_HAS_IPV6__ */
  1190. char *cp;
  1191. char **alias;
  1192. int aliases, i;
  1193. int ret=HOST_NOT_FOUND;
  1194. if (buflen < sizeof(char *)*(ALIAS_DIM))
  1195. return ERANGE;
  1196. alias=(char **)buf;
  1197. buf+=sizeof(char **)*(ALIAS_DIM);
  1198. buflen-=sizeof(char **)*(ALIAS_DIM);
  1199. if (action!=GETHOSTENT) {
  1200. #ifdef __UCLIBC_HAS_IPV6__
  1201. char *p=buf;
  1202. size_t len=buflen;
  1203. #endif /* __UCLIBC_HAS_IPV6__ */
  1204. *h_errnop=NETDB_INTERNAL;
  1205. if (buflen < sizeof(*in))
  1206. return ERANGE;
  1207. in=(struct in_addr*)buf;
  1208. buf+=sizeof(*in);
  1209. buflen-=sizeof(*in);
  1210. if (buflen < sizeof(*addr_list)*2)
  1211. return ERANGE;
  1212. addr_list=(struct in_addr **)buf;
  1213. buf+=sizeof(*addr_list)*2;
  1214. buflen-=sizeof(*addr_list)*2;
  1215. #ifdef __UCLIBC_HAS_IPV6__
  1216. if (len < sizeof(*in6))
  1217. return ERANGE;
  1218. in6=(struct in6_addr*)p;
  1219. p+=sizeof(*in6);
  1220. len-=sizeof(*in6);
  1221. if (len < sizeof(*addr_list6)*2)
  1222. return ERANGE;
  1223. addr_list6=(struct in6_addr**)p;
  1224. p+=sizeof(*addr_list6)*2;
  1225. len-=sizeof(*addr_list6)*2;
  1226. if (len < buflen) {
  1227. buflen=len;
  1228. buf=p;
  1229. }
  1230. #endif /* __UCLIBC_HAS_IPV6__ */
  1231. if (buflen < 80)
  1232. return ERANGE;
  1233. __open_etc_hosts(&fp);
  1234. if (fp == NULL) {
  1235. result=NULL;
  1236. return errno;
  1237. }
  1238. }
  1239. *h_errnop=HOST_NOT_FOUND;
  1240. while (fgets(buf, buflen, fp)) {
  1241. if ((cp = strchr(buf, '#')))
  1242. *cp = '\0';
  1243. DPRINTF("Looking at: %s\n", buf);
  1244. aliases = 0;
  1245. cp = buf;
  1246. while (*cp) {
  1247. while (*cp && isspace(*cp))
  1248. *cp++ = '\0';
  1249. if (!*cp)
  1250. continue;
  1251. if (aliases < (2+MAX_ALIASES))
  1252. alias[aliases++] = cp;
  1253. while (*cp && !isspace(*cp))
  1254. cp++;
  1255. }
  1256. alias[aliases] = 0;
  1257. if (aliases < 2)
  1258. continue; /* syntax error really */
  1259. if (action==GETHOSTENT) {
  1260. /* Return whatever the next entry happens to be. */
  1261. break;
  1262. } else if (action==GET_HOSTS_BYADDR) {
  1263. if (strcmp(name, alias[0]) != 0)
  1264. continue;
  1265. } else {
  1266. /* GET_HOSTS_BYNAME */
  1267. for (i = 1; i < aliases; i++)
  1268. if (strcasecmp(name, alias[i]) == 0)
  1269. break;
  1270. if (i >= aliases)
  1271. continue;
  1272. }
  1273. if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
  1274. DPRINTF("Found INET\n");
  1275. addr_list[0] = in;
  1276. addr_list[1] = 0;
  1277. result_buf->h_name = alias[1];
  1278. result_buf->h_addrtype = AF_INET;
  1279. result_buf->h_length = sizeof(*in);
  1280. result_buf->h_addr_list = (char**) addr_list;
  1281. result_buf->h_aliases = alias + 2;
  1282. *result=result_buf;
  1283. ret=NETDB_SUCCESS;
  1284. #ifdef __UCLIBC_HAS_IPV6__
  1285. } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
  1286. DPRINTF("Found INET6\n");
  1287. addr_list6[0] = in6;
  1288. addr_list6[1] = 0;
  1289. result_buf->h_name = alias[1];
  1290. result_buf->h_addrtype = AF_INET6;
  1291. result_buf->h_length = sizeof(*in6);
  1292. result_buf->h_addr_list = (char**) addr_list6;
  1293. result_buf->h_aliases = alias + 2;
  1294. *result=result_buf;
  1295. ret=NETDB_SUCCESS;
  1296. #endif /* __UCLIBC_HAS_IPV6__ */
  1297. } else {
  1298. DPRINTF("Error\n");
  1299. ret=TRY_AGAIN;
  1300. break; /* bad ip address */
  1301. }
  1302. if (action!=GETHOSTENT) {
  1303. fclose(fp);
  1304. }
  1305. return ret;
  1306. }
  1307. if (action!=GETHOSTENT) {
  1308. fclose(fp);
  1309. }
  1310. return ret;
  1311. }
  1312. #endif
  1313. #ifdef L_gethostent
  1314. #ifdef __UCLIBC_HAS_THREADS__
  1315. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  1316. # define LOCK __pthread_mutex_lock(&mylock)
  1317. # define UNLOCK __pthread_mutex_unlock(&mylock);
  1318. #else
  1319. # define LOCK
  1320. # define UNLOCK
  1321. #endif
  1322. static int __stay_open;
  1323. static FILE * __gethostent_fp;
  1324. void endhostent (void)
  1325. {
  1326. LOCK;
  1327. __stay_open = 0;
  1328. if (__gethostent_fp) {
  1329. fclose(__gethostent_fp);
  1330. }
  1331. UNLOCK;
  1332. }
  1333. void sethostent (int stay_open)
  1334. {
  1335. LOCK;
  1336. __stay_open = stay_open;
  1337. UNLOCK;
  1338. }
  1339. struct hostent *gethostent (void)
  1340. {
  1341. static struct hostent h;
  1342. static char buf[
  1343. #ifndef __UCLIBC_HAS_IPV6__
  1344. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1345. #else
  1346. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1347. #endif /* __UCLIBC_HAS_IPV6__ */
  1348. sizeof(char *)*(ALIAS_DIM) +
  1349. 80/*namebuffer*/ + 2/* margin */];
  1350. struct hostent *host;
  1351. LOCK;
  1352. if (__gethostent_fp == NULL) {
  1353. __open_etc_hosts(&__gethostent_fp);
  1354. if (__gethostent_fp == NULL) {
  1355. UNLOCK;
  1356. return((struct hostent *)NULL);
  1357. }
  1358. }
  1359. __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  1360. &h, buf, sizeof(buf), &host, &h_errno);
  1361. if (__stay_open==0) {
  1362. fclose(__gethostent_fp);
  1363. }
  1364. UNLOCK;
  1365. return(host);
  1366. }
  1367. #endif
  1368. #ifdef L_get_hosts_byname_r
  1369. int __get_hosts_byname_r(const char * name, int type,
  1370. struct hostent * result_buf,
  1371. char * buf, size_t buflen,
  1372. struct hostent ** result,
  1373. int * h_errnop)
  1374. {
  1375. return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, result_buf, buf, buflen, result, h_errnop));
  1376. }
  1377. #endif
  1378. #ifdef L_get_hosts_byaddr_r
  1379. int __get_hosts_byaddr_r(const char * addr, int len, int type,
  1380. struct hostent * result_buf,
  1381. char * buf, size_t buflen,
  1382. struct hostent ** result,
  1383. int * h_errnop)
  1384. {
  1385. #ifndef __UCLIBC_HAS_IPV6__
  1386. char ipaddr[INET_ADDRSTRLEN];
  1387. #else
  1388. char ipaddr[INET6_ADDRSTRLEN];
  1389. #endif /* __UCLIBC_HAS_IPV6__ */
  1390. switch (type) {
  1391. case AF_INET:
  1392. if (len != sizeof(struct in_addr))
  1393. return 0;
  1394. break;
  1395. #ifdef __UCLIBC_HAS_IPV6__
  1396. case AF_INET6:
  1397. if (len != sizeof(struct in6_addr))
  1398. return 0;
  1399. break;
  1400. #endif /* __UCLIBC_HAS_IPV6__ */
  1401. default:
  1402. return 0;
  1403. }
  1404. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1405. return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1406. result_buf, buf, buflen, result, h_errnop));
  1407. }
  1408. #endif
  1409. #ifdef L_getnameinfo
  1410. #ifndef min
  1411. # define min(x,y) (((x) > (y)) ? (y) : (x))
  1412. #endif /* min */
  1413. int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  1414. socklen_t hostlen, char *serv, socklen_t servlen,
  1415. unsigned int flags)
  1416. {
  1417. int serrno = errno;
  1418. int ok = 0;
  1419. struct hostent *h = NULL;
  1420. char domain[256];
  1421. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1422. return EAI_BADFLAGS;
  1423. if (sa == NULL || addrlen < sizeof (sa_family_t))
  1424. return EAI_FAMILY;
  1425. switch (sa->sa_family) {
  1426. case AF_LOCAL:
  1427. break;
  1428. case AF_INET:
  1429. if (addrlen < sizeof (struct sockaddr_in))
  1430. return EAI_FAMILY;
  1431. break;
  1432. #ifdef __UCLIBC_HAS_IPV6__
  1433. case AF_INET6:
  1434. if (addrlen < sizeof (struct sockaddr_in6))
  1435. return EAI_FAMILY;
  1436. break;
  1437. #endif /* __UCLIBC_HAS_IPV6__ */
  1438. default:
  1439. return EAI_FAMILY;
  1440. }
  1441. if (host != NULL && hostlen > 0)
  1442. switch (sa->sa_family) {
  1443. case AF_INET:
  1444. #ifdef __UCLIBC_HAS_IPV6__
  1445. case AF_INET6:
  1446. #endif /* __UCLIBC_HAS_IPV6__ */
  1447. if (!(flags & NI_NUMERICHOST)) {
  1448. #ifdef __UCLIBC_HAS_IPV6__
  1449. if (sa->sa_family == AF_INET6)
  1450. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1451. sizeof(struct in6_addr), AF_INET6);
  1452. else
  1453. #endif /* __UCLIBC_HAS_IPV6__ */
  1454. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1455. sizeof(struct in_addr), AF_INET);
  1456. if (h) {
  1457. char *c;
  1458. if ((flags & NI_NOFQDN)
  1459. && (getdomainname (domain, sizeof(domain)) == 0)
  1460. && (c = strstr (h->h_name, domain))
  1461. && (c != h->h_name) && (*(--c) == '.')) {
  1462. strncpy (host, h->h_name,
  1463. min(hostlen, (size_t) (c - h->h_name)));
  1464. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1465. ok = 1;
  1466. } else {
  1467. strncpy (host, h->h_name, hostlen);
  1468. ok = 1;
  1469. }
  1470. }
  1471. }
  1472. if (!ok) {
  1473. if (flags & NI_NAMEREQD) {
  1474. errno = serrno;
  1475. return EAI_NONAME;
  1476. } else {
  1477. const char *c;
  1478. #ifdef __UCLIBC_HAS_IPV6__
  1479. if (sa->sa_family == AF_INET6) {
  1480. const struct sockaddr_in6 *sin6p;
  1481. sin6p = (const struct sockaddr_in6 *) sa;
  1482. c = inet_ntop (AF_INET6,
  1483. (const void *) &sin6p->sin6_addr, host, hostlen);
  1484. #if 0
  1485. /* Does scope id need to be supported? */
  1486. uint32_t scopeid;
  1487. scopeid = sin6p->sin6_scope_id;
  1488. if (scopeid != 0) {
  1489. /* Buffer is >= IFNAMSIZ+1. */
  1490. char scopebuf[IFNAMSIZ + 1];
  1491. char *scopeptr;
  1492. int ni_numericscope = 0;
  1493. size_t real_hostlen = __strnlen (host, hostlen);
  1494. size_t scopelen = 0;
  1495. scopebuf[0] = SCOPE_DELIMITER;
  1496. scopebuf[1] = '\0';
  1497. scopeptr = &scopebuf[1];
  1498. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1499. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1500. if (if_indextoname (scopeid, scopeptr) == NULL)
  1501. ++ni_numericscope;
  1502. else
  1503. scopelen = strlen (scopebuf);
  1504. } else {
  1505. ++ni_numericscope;
  1506. }
  1507. if (ni_numericscope)
  1508. scopelen = 1 + snprintf (scopeptr,
  1509. (scopebuf
  1510. + sizeof scopebuf
  1511. - scopeptr),
  1512. "%u", scopeid);
  1513. if (real_hostlen + scopelen + 1 > hostlen)
  1514. return EAI_SYSTEM;
  1515. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1516. }
  1517. #endif
  1518. } else
  1519. #endif /* __UCLIBC_HAS_IPV6__ */
  1520. c = inet_ntop (AF_INET,
  1521. (const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
  1522. host, hostlen);
  1523. if (c == NULL) {
  1524. errno = serrno;
  1525. return EAI_SYSTEM;
  1526. }
  1527. }
  1528. ok = 1;
  1529. }
  1530. break;
  1531. case AF_LOCAL:
  1532. if (!(flags & NI_NUMERICHOST)) {
  1533. struct utsname utsname;
  1534. if (!uname (&utsname)) {
  1535. strncpy (host, utsname.nodename, hostlen);
  1536. break;
  1537. };
  1538. };
  1539. if (flags & NI_NAMEREQD) {
  1540. errno = serrno;
  1541. return EAI_NONAME;
  1542. }
  1543. strncpy (host, "localhost", hostlen);
  1544. break;
  1545. default:
  1546. return EAI_FAMILY;
  1547. }
  1548. if (serv && (servlen > 0)) {
  1549. switch (sa->sa_family) {
  1550. case AF_INET:
  1551. #ifdef __UCLIBC_HAS_IPV6__
  1552. case AF_INET6:
  1553. #endif /* __UCLIBC_HAS_IPV6__ */
  1554. if (!(flags & NI_NUMERICSERV)) {
  1555. struct servent *s;
  1556. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1557. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1558. if (s) {
  1559. strncpy (serv, s->s_name, servlen);
  1560. break;
  1561. }
  1562. }
  1563. snprintf (serv, servlen, "%d",
  1564. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1565. break;
  1566. case AF_LOCAL:
  1567. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1568. break;
  1569. }
  1570. }
  1571. if (host && (hostlen > 0))
  1572. host[hostlen-1] = 0;
  1573. if (serv && (servlen > 0))
  1574. serv[servlen-1] = 0;
  1575. errno = serrno;
  1576. return 0;
  1577. }
  1578. #endif
  1579. #ifdef L_gethostbyname_r
  1580. int gethostbyname_r(const char * name,
  1581. struct hostent * result_buf,
  1582. char * buf, size_t buflen,
  1583. struct hostent ** result,
  1584. int * h_errnop)
  1585. {
  1586. struct in_addr *in;
  1587. struct in_addr **addr_list;
  1588. unsigned char *packet;
  1589. struct resolv_answer a;
  1590. int i;
  1591. int nest = 0;
  1592. int __nameserversXX;
  1593. char ** __nameserverXX;
  1594. __open_nameservers();
  1595. *result=NULL;
  1596. if (!name)
  1597. return EINVAL;
  1598. /* do /etc/hosts first */
  1599. {
  1600. int old_errno = errno; /* Save the old errno and reset errno */
  1601. __set_errno(0); /* to check for missing /etc/hosts. */
  1602. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1603. buf, buflen, result, h_errnop))==0)
  1604. return i;
  1605. switch (*h_errnop) {
  1606. case HOST_NOT_FOUND:
  1607. case NO_ADDRESS:
  1608. break;
  1609. case NETDB_INTERNAL:
  1610. if (errno == ENOENT) {
  1611. break;
  1612. }
  1613. /* else fall through */
  1614. default:
  1615. return i;
  1616. }
  1617. __set_errno(old_errno);
  1618. }
  1619. DPRINTF("Nothing found in /etc/hosts\n");
  1620. *h_errnop = NETDB_INTERNAL;
  1621. if (buflen < sizeof(*in))
  1622. return ERANGE;
  1623. in=(struct in_addr*)buf;
  1624. buf+=sizeof(*in);
  1625. buflen-=sizeof(*in);
  1626. if (buflen < sizeof(*addr_list)*2)
  1627. return ERANGE;
  1628. addr_list=(struct in_addr**)buf;
  1629. buf+=sizeof(*addr_list)*2;
  1630. buflen-=sizeof(*addr_list)*2;
  1631. addr_list[0] = in;
  1632. addr_list[1] = 0;
  1633. if (buflen<256)
  1634. return ERANGE;
  1635. strncpy(buf, name, buflen);
  1636. /* First check if this is already an address */
  1637. if (inet_aton(name, in)) {
  1638. result_buf->h_name = buf;
  1639. result_buf->h_addrtype = AF_INET;
  1640. result_buf->h_length = sizeof(*in);
  1641. result_buf->h_addr_list = (char **) addr_list;
  1642. *result=result_buf;
  1643. *h_errnop = NETDB_SUCCESS;
  1644. return NETDB_SUCCESS;
  1645. }
  1646. for (;;) {
  1647. BIGLOCK;
  1648. __nameserversXX=__nameservers;
  1649. __nameserverXX=__nameserver;
  1650. BIGUNLOCK;
  1651. i = __dns_lookup(buf, T_A, __nameserversXX, __nameserverXX, &packet, &a);
  1652. if (i < 0) {
  1653. *h_errnop = HOST_NOT_FOUND;
  1654. DPRINTF("__dns_lookup\n");
  1655. return TRY_AGAIN;
  1656. }
  1657. strncpy(buf, a.dotted, buflen);
  1658. free(a.dotted);
  1659. if (a.atype == T_CNAME) { /* CNAME */
  1660. DPRINTF("Got a CNAME in gethostbyname()\n");
  1661. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1662. free(packet);
  1663. if (i < 0) {
  1664. *h_errnop = NO_RECOVERY;
  1665. DPRINTF("__decode_dotted\n");
  1666. return -1;
  1667. }
  1668. if (++nest > MAX_RECURSE) {
  1669. *h_errnop = NO_RECOVERY;
  1670. DPRINTF("recursion\n");
  1671. return -1;
  1672. }
  1673. continue;
  1674. } else if (a.atype == T_A) { /* ADDRESS */
  1675. memcpy(in, a.rdata, sizeof(*in));
  1676. result_buf->h_name = buf;
  1677. result_buf->h_addrtype = AF_INET;
  1678. result_buf->h_length = sizeof(*in);
  1679. result_buf->h_addr_list = (char **) addr_list;
  1680. free(packet);
  1681. break;
  1682. } else {
  1683. free(packet);
  1684. *h_errnop=HOST_NOT_FOUND;
  1685. return TRY_AGAIN;
  1686. }
  1687. }
  1688. *result=result_buf;
  1689. *h_errnop = NETDB_SUCCESS;
  1690. return NETDB_SUCCESS;
  1691. }
  1692. #endif
  1693. #ifdef L_gethostbyname2_r
  1694. int gethostbyname2_r(const char *name, int family,
  1695. struct hostent * result_buf,
  1696. char * buf, size_t buflen,
  1697. struct hostent ** result,
  1698. int * h_errnop)
  1699. {
  1700. #ifndef __UCLIBC_HAS_IPV6__
  1701. return family == AF_INET ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1702. #else /* __UCLIBC_HAS_IPV6__ */
  1703. struct in6_addr *in;
  1704. struct in6_addr **addr_list;
  1705. unsigned char *packet;
  1706. struct resolv_answer a;
  1707. int i;
  1708. int nest = 0;
  1709. int __nameserversXX;
  1710. char ** __nameserverXX;
  1711. if (family == AF_INET)
  1712. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1713. if (family != AF_INET6)
  1714. return EINVAL;
  1715. __open_nameservers();
  1716. *result=NULL;
  1717. if (!name)
  1718. return EINVAL;
  1719. /* do /etc/hosts first */
  1720. {
  1721. int old_errno = errno; /* Save the old errno and reset errno */
  1722. __set_errno(0); /* to check for missing /etc/hosts. */
  1723. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1724. buf, buflen, result, h_errnop))==0)
  1725. return i;
  1726. switch (*h_errnop) {
  1727. case HOST_NOT_FOUND:
  1728. case NO_ADDRESS:
  1729. break;
  1730. case NETDB_INTERNAL:
  1731. if (errno == ENOENT) {
  1732. break;
  1733. }
  1734. /* else fall through */
  1735. default:
  1736. return i;
  1737. }
  1738. __set_errno(old_errno);
  1739. }
  1740. DPRINTF("Nothing found in /etc/hosts\n");
  1741. *h_errnop = NETDB_INTERNAL;
  1742. if (buflen < sizeof(*in))
  1743. return ERANGE;
  1744. in=(struct in6_addr*)buf;
  1745. buf+=sizeof(*in);
  1746. buflen-=sizeof(*in);
  1747. if (buflen < sizeof(*addr_list)*2)
  1748. return ERANGE;
  1749. addr_list=(struct in6_addr**)buf;
  1750. buf+=sizeof(*addr_list)*2;
  1751. buflen-=sizeof(*addr_list)*2;
  1752. addr_list[0] = in;
  1753. addr_list[1] = 0;
  1754. if (buflen<256)
  1755. return ERANGE;
  1756. strncpy(buf, name, buflen);
  1757. /* First check if this is already an address */
  1758. if (inet_pton(AF_INET6, name, in)) {
  1759. result_buf->h_name = buf;
  1760. result_buf->h_addrtype = AF_INET6;
  1761. result_buf->h_length = sizeof(*in);
  1762. result_buf->h_addr_list = (char **) addr_list;
  1763. *result=result_buf;
  1764. *h_errnop = NETDB_SUCCESS;
  1765. return NETDB_SUCCESS;
  1766. }
  1767. for (;;) {
  1768. BIGLOCK;
  1769. __nameserversXX=__nameservers;
  1770. __nameserverXX=__nameserver;
  1771. BIGUNLOCK;
  1772. i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
  1773. if (i < 0) {
  1774. *h_errnop = HOST_NOT_FOUND;
  1775. return TRY_AGAIN;
  1776. }
  1777. strncpy(buf, a.dotted, buflen);
  1778. free(a.dotted);
  1779. if (a.atype == T_CNAME) { /* CNAME */
  1780. DPRINTF("Got a CNAME in gethostbyname()\n");
  1781. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1782. free(packet);
  1783. if (i < 0) {
  1784. *h_errnop = NO_RECOVERY;
  1785. return -1;
  1786. }
  1787. if (++nest > MAX_RECURSE) {
  1788. *h_errnop = NO_RECOVERY;
  1789. return -1;
  1790. }
  1791. continue;
  1792. } else if (a.atype == T_AAAA) { /* ADDRESS */
  1793. memcpy(in, a.rdata, sizeof(*in));
  1794. result_buf->h_name = buf;
  1795. result_buf->h_addrtype = AF_INET6;
  1796. result_buf->h_length = sizeof(*in);
  1797. result_buf->h_addr_list = (char **) addr_list;
  1798. free(packet);
  1799. break;
  1800. } else {
  1801. free(packet);
  1802. *h_errnop=HOST_NOT_FOUND;
  1803. return TRY_AGAIN;
  1804. }
  1805. }
  1806. *result=result_buf;
  1807. *h_errnop = NETDB_SUCCESS;
  1808. return NETDB_SUCCESS;
  1809. #endif /* __UCLIBC_HAS_IPV6__ */
  1810. }
  1811. #endif
  1812. #ifdef L_gethostbyaddr_r
  1813. int gethostbyaddr_r (const void *addr, socklen_t len, int type,
  1814. struct hostent * result_buf,
  1815. char * buf, size_t buflen,
  1816. struct hostent ** result,
  1817. int * h_errnop)
  1818. {
  1819. struct in_addr *in;
  1820. struct in_addr **addr_list;
  1821. #ifdef __UCLIBC_HAS_IPV6__
  1822. char *qp;
  1823. size_t plen;
  1824. struct in6_addr *in6;
  1825. struct in6_addr **addr_list6;
  1826. #endif /* __UCLIBC_HAS_IPV6__ */
  1827. unsigned char *packet;
  1828. struct resolv_answer a;
  1829. int i;
  1830. int nest = 0;
  1831. int __nameserversXX;
  1832. char ** __nameserverXX;
  1833. *result=NULL;
  1834. if (!addr)
  1835. return EINVAL;
  1836. switch (type) {
  1837. case AF_INET:
  1838. if (len != sizeof(struct in_addr))
  1839. return EINVAL;
  1840. break;
  1841. #ifdef __UCLIBC_HAS_IPV6__
  1842. case AF_INET6:
  1843. if (len != sizeof(struct in6_addr))
  1844. return EINVAL;
  1845. break;
  1846. #endif /* __UCLIBC_HAS_IPV6__ */
  1847. default:
  1848. return EINVAL;
  1849. }
  1850. /* do /etc/hosts first */
  1851. if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
  1852. buf, buflen, result, h_errnop))==0)
  1853. return i;
  1854. switch (*h_errnop) {
  1855. case HOST_NOT_FOUND:
  1856. case NO_ADDRESS:
  1857. break;
  1858. default:
  1859. return i;
  1860. }
  1861. __open_nameservers();
  1862. #ifdef __UCLIBC_HAS_IPV6__
  1863. qp=buf;
  1864. plen=buflen;
  1865. #endif /* __UCLIBC_HAS_IPV6__ */
  1866. *h_errnop = NETDB_INTERNAL;
  1867. if (buflen < sizeof(*in))
  1868. return ERANGE;
  1869. in=(struct in_addr*)buf;
  1870. buf+=sizeof(*in);
  1871. buflen-=sizeof(*in);
  1872. if (buflen < sizeof(*addr_list)*2)
  1873. return ERANGE;
  1874. addr_list=(struct in_addr**)buf;
  1875. buf+=sizeof(*addr_list)*2;
  1876. buflen-=sizeof(*addr_list)*2;
  1877. #ifdef __UCLIBC_HAS_IPV6__
  1878. if (plen < sizeof(*in6))
  1879. return ERANGE;
  1880. in6=(struct in6_addr*)qp;
  1881. qp+=sizeof(*in6);
  1882. plen-=sizeof(*in6);
  1883. if (plen < sizeof(*addr_list6)*2)
  1884. return ERANGE;
  1885. addr_list6=(struct in6_addr**)qp;
  1886. qp+=sizeof(*addr_list6)*2;
  1887. plen-=sizeof(*addr_list6)*2;
  1888. if (plen < buflen) {
  1889. buflen=plen;
  1890. buf=qp;
  1891. }
  1892. #endif /* __UCLIBC_HAS_IPV6__ */
  1893. if (buflen<256)
  1894. return ERANGE;
  1895. if(type == AF_INET) {
  1896. unsigned char *tmp_addr = (unsigned char *)addr;
  1897. memcpy(&in->s_addr, addr, len);
  1898. addr_list[0] = in;
  1899. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  1900. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  1901. #ifdef __UCLIBC_HAS_IPV6__
  1902. } else {
  1903. memcpy(in6->s6_addr, addr, len);
  1904. addr_list6[0] = in6;
  1905. qp = buf;
  1906. for (i = len - 1; i >= 0; i--) {
  1907. qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
  1908. (in6->s6_addr[i] >> 4) & 0xf);
  1909. }
  1910. strcpy(qp, "ip6.int");
  1911. #endif /* __UCLIBC_HAS_IPV6__ */
  1912. }
  1913. addr_list[1] = 0;
  1914. for (;;) {
  1915. BIGLOCK;
  1916. __nameserversXX=__nameservers;
  1917. __nameserverXX=__nameserver;
  1918. BIGUNLOCK;
  1919. i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
  1920. if (i < 0) {
  1921. *h_errnop = HOST_NOT_FOUND;
  1922. return TRY_AGAIN;
  1923. }
  1924. strncpy(buf, a.dotted, buflen);
  1925. free(a.dotted);
  1926. if (a.atype == T_CNAME) { /* CNAME */
  1927. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  1928. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1929. free(packet);
  1930. if (i < 0) {
  1931. *h_errnop = NO_RECOVERY;
  1932. return -1;
  1933. }
  1934. if (++nest > MAX_RECURSE) {
  1935. *h_errnop = NO_RECOVERY;
  1936. return -1;
  1937. }
  1938. continue;
  1939. } else if (a.atype == T_PTR) { /* ADDRESS */
  1940. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1941. free(packet);
  1942. result_buf->h_name = buf;
  1943. result_buf->h_addrtype = type;
  1944. if(type == AF_INET) {
  1945. result_buf->h_length = sizeof(*in);
  1946. #ifdef __UCLIBC_HAS_IPV6__
  1947. } else {
  1948. result_buf->h_length = sizeof(*in6);
  1949. #endif /* __UCLIBC_HAS_IPV6__ */
  1950. }
  1951. result_buf->h_addr_list = (char **) addr_list;
  1952. break;
  1953. } else {
  1954. free(packet);
  1955. *h_errnop = NO_ADDRESS;
  1956. return TRY_AGAIN;
  1957. }
  1958. }
  1959. *result=result_buf;
  1960. *h_errnop = NETDB_SUCCESS;
  1961. return NETDB_SUCCESS;
  1962. }
  1963. #endif
  1964. #ifdef L_res_comp
  1965. /*
  1966. * Expand compressed domain name 'comp_dn' to full domain name.
  1967. * 'msg' is a pointer to the begining of the message,
  1968. * 'eomorig' points to the first location after the message,
  1969. * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  1970. * Return size of compressed name or -1 if there was an error.
  1971. */
  1972. int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  1973. char *dst, int dstsiz)
  1974. {
  1975. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  1976. if (n > 0 && dst[0] == '.')
  1977. dst[0] = '\0';
  1978. return (n);
  1979. }
  1980. #endif /* L_res_comp */
  1981. #ifdef L_ns_name
  1982. /*
  1983. * printable(ch)
  1984. * Thinking in noninternationalized USASCII (per the DNS spec),
  1985. * is this character visible and not a space when printed ?
  1986. * return:
  1987. * boolean.
  1988. */
  1989. static int printable(int ch)
  1990. {
  1991. return (ch > 0x20 && ch < 0x7f);
  1992. }
  1993. /*
  1994. * special(ch)
  1995. * Thinking in noninternationalized USASCII (per the DNS spec),
  1996. * is this characted special ("in need of quoting") ?
  1997. * return:
  1998. * boolean.
  1999. */
  2000. static int special(int ch)
  2001. {
  2002. switch (ch) {
  2003. case 0x22: /* '"' */
  2004. case 0x2E: /* '.' */
  2005. case 0x3B: /* ';' */
  2006. case 0x5C: /* '\\' */
  2007. /* Special modifiers in zone files. */
  2008. case 0x40: /* '@' */
  2009. case 0x24: /* '$' */
  2010. return (1);
  2011. default:
  2012. return (0);
  2013. }
  2014. }
  2015. /*
  2016. * ns_name_uncompress(msg, eom, src, dst, dstsiz)
  2017. * Expand compressed domain name to presentation format.
  2018. * return:
  2019. * Number of bytes read out of `src', or -1 (with errno set).
  2020. * note:
  2021. * Root domain returns as "." not "".
  2022. */
  2023. int __ns_name_uncompress(const u_char *msg, const u_char *eom,
  2024. const u_char *src, char *dst, size_t dstsiz)
  2025. {
  2026. u_char tmp[NS_MAXCDNAME];
  2027. int n;
  2028. if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
  2029. return (-1);
  2030. if (ns_name_ntop(tmp, dst, dstsiz) == -1)
  2031. return (-1);
  2032. return (n);
  2033. }
  2034. /*
  2035. * ns_name_ntop(src, dst, dstsiz)
  2036. * Convert an encoded domain name to printable ascii as per RFC1035.
  2037. * return:
  2038. * Number of bytes written to buffer, or -1 (with errno set)
  2039. * notes:
  2040. * The root is returned as "."
  2041. * All other domains are returned in non absolute form
  2042. */
  2043. int __ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
  2044. const u_char *cp;
  2045. char *dn, *eom;
  2046. u_char c;
  2047. u_int n;
  2048. const char digits[] = "0123456789";
  2049. cp = src;
  2050. dn = dst;
  2051. eom = dst + dstsiz;
  2052. while ((n = *cp++) != 0) {
  2053. if ((n & NS_CMPRSFLGS) != 0) {
  2054. /* Some kind of compression pointer. */
  2055. __set_errno (EMSGSIZE);
  2056. return (-1);
  2057. }
  2058. if (dn != dst) {
  2059. if (dn >= eom) {
  2060. __set_errno (EMSGSIZE);
  2061. return (-1);
  2062. }
  2063. *dn++ = '.';
  2064. }
  2065. if (dn + n >= eom) {
  2066. __set_errno (EMSGSIZE);
  2067. return (-1);
  2068. }
  2069. for ((void)NULL; n > 0; n--) {
  2070. c = *cp++;
  2071. if (special(c)) {
  2072. if (dn + 1 >= eom) {
  2073. __set_errno (EMSGSIZE);
  2074. return (-1);
  2075. }
  2076. *dn++ = '\\';
  2077. *dn++ = (char)c;
  2078. } else if (!printable(c)) {
  2079. if (dn + 3 >= eom) {
  2080. __set_errno (EMSGSIZE);
  2081. return (-1);
  2082. }
  2083. *dn++ = '\\';
  2084. *dn++ = digits[c / 100];
  2085. *dn++ = digits[(c % 100) / 10];
  2086. *dn++ = digits[c % 10];
  2087. } else {
  2088. if (dn >= eom) {
  2089. __set_errno (EMSGSIZE);
  2090. return (-1);
  2091. }
  2092. *dn++ = (char)c;
  2093. }
  2094. }
  2095. }
  2096. if (dn == dst) {
  2097. if (dn >= eom) {
  2098. __set_errno (EMSGSIZE);
  2099. return (-1);
  2100. }
  2101. *dn++ = '.';
  2102. }
  2103. if (dn >= eom) {
  2104. __set_errno (EMSGSIZE);
  2105. return (-1);
  2106. }
  2107. *dn++ = '\0';
  2108. return (dn - dst);
  2109. }
  2110. /*
  2111. * ns_name_unpack(msg, eom, src, dst, dstsiz)
  2112. * Unpack a domain name from a message, source may be compressed.
  2113. * return:
  2114. * -1 if it fails, or consumed octets if it succeeds.
  2115. */
  2116. int __ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
  2117. u_char *dst, size_t dstsiz)
  2118. {
  2119. const u_char *srcp, *dstlim;
  2120. u_char *dstp;
  2121. int n, len, checked;
  2122. len = -1;
  2123. checked = 0;
  2124. dstp = dst;
  2125. srcp = src;
  2126. dstlim = dst + dstsiz;
  2127. if (srcp < msg || srcp >= eom) {
  2128. __set_errno (EMSGSIZE);
  2129. return (-1);
  2130. }
  2131. /* Fetch next label in domain name. */
  2132. while ((n = *srcp++) != 0) {
  2133. /* Check for indirection. */
  2134. switch (n & NS_CMPRSFLGS) {
  2135. case 0:
  2136. /* Limit checks. */
  2137. if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  2138. __set_errno (EMSGSIZE);
  2139. return (-1);
  2140. }
  2141. checked += n + 1;
  2142. *dstp++ = n;
  2143. memcpy(dstp, srcp, n);
  2144. dstp += n;
  2145. srcp += n;
  2146. break;
  2147. case NS_CMPRSFLGS:
  2148. if (srcp >= eom) {
  2149. __set_errno (EMSGSIZE);
  2150. return (-1);
  2151. }
  2152. if (len < 0)
  2153. len = srcp - src + 1;
  2154. srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  2155. if (srcp < msg || srcp >= eom) { /* Out of range. */
  2156. __set_errno (EMSGSIZE);
  2157. return (-1);
  2158. }
  2159. checked += 2;
  2160. /*
  2161. * Check for loops in the compressed name;
  2162. * if we've looked at the whole message,
  2163. * there must be a loop.
  2164. */
  2165. if (checked >= eom - msg) {
  2166. __set_errno (EMSGSIZE);
  2167. return (-1);
  2168. }
  2169. break;
  2170. default:
  2171. __set_errno (EMSGSIZE);
  2172. return (-1); /* flag error */
  2173. }
  2174. }
  2175. *dstp = '\0';
  2176. if (len < 0)
  2177. len = srcp - src;
  2178. return (len);
  2179. }
  2180. #endif /* L_ns_name */