resolv.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547
  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. len = recv(fd, packet, 512, 0);
  674. if (len < 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 (len); /* 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. h_errno = NETDB_INTERNAL;
  770. return -1;
  771. }
  772. #endif
  773. #ifdef L_opennameservers
  774. int __nameservers;
  775. char * __nameserver[MAX_SERVERS];
  776. int __searchdomains;
  777. char * __searchdomain[MAX_SEARCH];
  778. #ifdef __UCLIBC_HAS_THREADS__
  779. pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER;
  780. #endif
  781. /*
  782. * we currently read formats not quite the same as that on normal
  783. * unix systems, we can have a list of nameservers after the keyword.
  784. */
  785. int __open_nameservers()
  786. {
  787. FILE *fp;
  788. int i;
  789. #define RESOLV_ARGS 5
  790. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  791. int argc;
  792. BIGLOCK;
  793. if (__nameservers > 0) {
  794. BIGUNLOCK;
  795. return 0;
  796. }
  797. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  798. (fp = fopen("/etc/config/resolv.conf", "r")))
  799. {
  800. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  801. for (p = szBuffer; *p && isspace(*p); p++)
  802. /* skip white space */;
  803. if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  804. continue;
  805. argc = 0;
  806. while (*p && argc < RESOLV_ARGS) {
  807. argv[argc++] = p;
  808. while (*p && !isspace(*p) && *p != '\n')
  809. p++;
  810. while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  811. *p++ = '\0';
  812. }
  813. if (strcmp(argv[0], "nameserver") == 0) {
  814. for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
  815. __nameserver[__nameservers++] = strdup(argv[i]);
  816. DPRINTF("adding nameserver %s\n", argv[i]);
  817. }
  818. }
  819. /* domain and search are mutually exclusive, the last one wins */
  820. if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  821. while (__searchdomains > 0) {
  822. free(__searchdomain[--__searchdomains]);
  823. __searchdomain[__searchdomains] = NULL;
  824. }
  825. for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
  826. __searchdomain[__searchdomains++] = strdup(argv[i]);
  827. DPRINTF("adding search %s\n", argv[i]);
  828. }
  829. }
  830. }
  831. fclose(fp);
  832. DPRINTF("nameservers = %d\n", __nameservers);
  833. BIGUNLOCK;
  834. return 0;
  835. }
  836. DPRINTF("failed to open %s\n", "resolv.conf");
  837. h_errno = NO_RECOVERY;
  838. BIGUNLOCK;
  839. return -1;
  840. }
  841. #endif
  842. #ifdef L_closenameservers
  843. void __close_nameservers(void)
  844. {
  845. BIGLOCK;
  846. while (__nameservers > 0) {
  847. free(__nameserver[--__nameservers]);
  848. __nameserver[__nameservers] = NULL;
  849. }
  850. while (__searchdomains > 0) {
  851. free(__searchdomain[--__searchdomains]);
  852. __searchdomain[__searchdomains] = NULL;
  853. }
  854. BIGUNLOCK;
  855. }
  856. #endif
  857. #ifdef L_gethostbyname
  858. struct hostent *gethostbyname(const char *name)
  859. {
  860. static struct hostent h;
  861. static char buf[sizeof(struct in_addr) +
  862. sizeof(struct in_addr *)*2 +
  863. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  864. struct hostent *hp;
  865. gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
  866. return hp;
  867. }
  868. #endif
  869. #ifdef L_gethostbyname2
  870. struct hostent *gethostbyname2(const char *name, int family)
  871. {
  872. #ifndef __UCLIBC_HAS_IPV6__
  873. return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
  874. #else /* __UCLIBC_HAS_IPV6__ */
  875. static struct hostent h;
  876. static char buf[sizeof(struct in6_addr) +
  877. sizeof(struct in6_addr *)*2 +
  878. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  879. struct hostent *hp;
  880. gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
  881. return hp;
  882. #endif /* __UCLIBC_HAS_IPV6__ */
  883. }
  884. #endif
  885. #ifdef L_getnetbyname
  886. struct netent * getnetbyname(const char * name)
  887. {
  888. return NULL;
  889. }
  890. #endif
  891. #ifdef L_res_init
  892. struct __res_state _res;
  893. int res_init(void)
  894. {
  895. struct __res_state *rp = &(_res);
  896. __close_nameservers();
  897. if (__open_nameservers()) {
  898. return(-1);
  899. }
  900. rp->retrans = RES_TIMEOUT;
  901. rp->retry = 4;
  902. rp->options = RES_INIT;
  903. rp->id = (u_int) random();
  904. rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
  905. rp->nsaddr.sin_family = AF_INET;
  906. rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
  907. rp->ndots = 1;
  908. /** rp->pfcode = 0; **/
  909. rp->_vcsock = -1;
  910. /** rp->_flags = 0; **/
  911. /** rp->qhook = NULL; **/
  912. /** rp->rhook = NULL; **/
  913. /** rp->_u._ext.nsinit = 0; **/
  914. BIGLOCK;
  915. if(__searchdomains) {
  916. int i;
  917. for(i=0; i<__searchdomains; i++) {
  918. rp->dnsrch[i] = __searchdomain[i];
  919. }
  920. }
  921. if(__nameservers) {
  922. int i;
  923. struct in_addr a;
  924. for(i=0; i<__nameservers; i++) {
  925. if (inet_aton(__nameserver[i], &a)) {
  926. rp->nsaddr_list[i].sin_addr = a;
  927. rp->nsaddr_list[i].sin_family = AF_INET;
  928. rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
  929. }
  930. }
  931. }
  932. rp->nscount = __nameservers;
  933. BIGUNLOCK;
  934. return(0);
  935. }
  936. void res_close( void )
  937. {
  938. return;
  939. }
  940. #endif
  941. #ifdef L_res_query
  942. #ifndef MIN
  943. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  944. #endif
  945. int res_query(const char *dname, int class, int type,
  946. unsigned char *answer, int anslen)
  947. {
  948. int i;
  949. unsigned char * packet = 0;
  950. struct resolv_answer a;
  951. int __nameserversXX;
  952. char ** __nameserverXX;
  953. if (__open_nameservers() || !dname || class != 1 /* CLASS_IN */) {
  954. h_errno = NO_RECOVERY;
  955. return(-1);
  956. }
  957. memset((char *) &a, '\0', sizeof(a));
  958. BIGLOCK;
  959. __nameserversXX=__nameservers;
  960. __nameserverXX=__nameserver;
  961. BIGUNLOCK;
  962. i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
  963. if (i < 0) {
  964. h_errno = TRY_AGAIN;
  965. return(-1);
  966. }
  967. free(a.dotted);
  968. if (a.atype == type) { /* CNAME*/
  969. int len = MIN(anslen, i);
  970. memcpy(answer, packet, len);
  971. if (packet)
  972. free(packet);
  973. return(len);
  974. }
  975. if (packet)
  976. free(packet);
  977. return i;
  978. }
  979. /*
  980. * Formulate a normal query, send, and retrieve answer in supplied buffer.
  981. * Return the size of the response on success, -1 on error.
  982. * If enabled, implement search rules until answer or unrecoverable failure
  983. * is detected. Error code, if any, is left in h_errno.
  984. */
  985. int res_search(name, class, type, answer, anslen)
  986. const char *name; /* domain name */
  987. int class, type; /* class and type of query */
  988. u_char *answer; /* buffer to put answer */
  989. int anslen; /* size of answer */
  990. {
  991. const char *cp, * const *domain;
  992. HEADER *hp = (HEADER *)(void *)answer;
  993. u_int dots;
  994. int trailing_dot, ret, saved_herrno;
  995. int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
  996. if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) {
  997. h_errno = NETDB_INTERNAL;
  998. return (-1);
  999. }
  1000. errno = 0;
  1001. h_errno = HOST_NOT_FOUND; /* default, if we never query */
  1002. dots = 0;
  1003. for (cp = name; *cp; cp++)
  1004. dots += (*cp == '.');
  1005. trailing_dot = 0;
  1006. if (cp > name && *--cp == '.')
  1007. trailing_dot++;
  1008. /*
  1009. * If there are dots in the name already, let's just give it a try
  1010. * 'as is'. The threshold can be set with the "ndots" option.
  1011. */
  1012. saved_herrno = -1;
  1013. if (dots >= _res.ndots) {
  1014. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1015. if (ret > 0)
  1016. return (ret);
  1017. saved_herrno = h_errno;
  1018. tried_as_is++;
  1019. }
  1020. /*
  1021. * We do at least one level of search if
  1022. * - there is no dot and RES_DEFNAME is set, or
  1023. * - there is at least one dot, there is no trailing dot,
  1024. * and RES_DNSRCH is set.
  1025. */
  1026. if ((!dots && (_res.options & RES_DEFNAMES)) ||
  1027. (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
  1028. int done = 0;
  1029. for (domain = (const char * const *)_res.dnsrch;
  1030. *domain && !done;
  1031. domain++) {
  1032. ret = res_querydomain(name, *domain, class, type,
  1033. answer, anslen);
  1034. if (ret > 0)
  1035. return (ret);
  1036. /*
  1037. * If no server present, give up.
  1038. * If name isn't found in this domain,
  1039. * keep trying higher domains in the search list
  1040. * (if that's enabled).
  1041. * On a NO_DATA error, keep trying, otherwise
  1042. * a wildcard entry of another type could keep us
  1043. * from finding this entry higher in the domain.
  1044. * If we get some other error (negative answer or
  1045. * server failure), then stop searching up,
  1046. * but try the input name below in case it's
  1047. * fully-qualified.
  1048. */
  1049. if (errno == ECONNREFUSED) {
  1050. h_errno = TRY_AGAIN;
  1051. return (-1);
  1052. }
  1053. switch (h_errno) {
  1054. case NO_DATA:
  1055. got_nodata++;
  1056. /* FALLTHROUGH */
  1057. case HOST_NOT_FOUND:
  1058. /* keep trying */
  1059. break;
  1060. case TRY_AGAIN:
  1061. if (hp->rcode == SERVFAIL) {
  1062. /* try next search element, if any */
  1063. got_servfail++;
  1064. break;
  1065. }
  1066. /* FALLTHROUGH */
  1067. default:
  1068. /* anything else implies that we're done */
  1069. done++;
  1070. }
  1071. /*
  1072. * if we got here for some reason other than DNSRCH,
  1073. * we only wanted one iteration of the loop, so stop.
  1074. */
  1075. if (!(_res.options & RES_DNSRCH))
  1076. done++;
  1077. }
  1078. }
  1079. /*
  1080. * if we have not already tried the name "as is", do that now.
  1081. * note that we do this regardless of how many dots were in the
  1082. * name or whether it ends with a dot.
  1083. */
  1084. if (!tried_as_is) {
  1085. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1086. if (ret > 0)
  1087. return (ret);
  1088. }
  1089. /*
  1090. * if we got here, we didn't satisfy the search.
  1091. * if we did an initial full query, return that query's h_errno
  1092. * (note that we wouldn't be here if that query had succeeded).
  1093. * else if we ever got a nodata, send that back as the reason.
  1094. * else send back meaningless h_errno, that being the one from
  1095. * the last DNSRCH we did.
  1096. */
  1097. if (saved_herrno != -1)
  1098. h_errno = saved_herrno;
  1099. else if (got_nodata)
  1100. h_errno = NO_DATA;
  1101. else if (got_servfail)
  1102. h_errno = TRY_AGAIN;
  1103. return (-1);
  1104. }
  1105. /*
  1106. * Perform a call on res_query on the concatenation of name and domain,
  1107. * removing a trailing dot from name if domain is NULL.
  1108. */
  1109. int res_querydomain(name, domain, class, type, answer, anslen)
  1110. const char *name, *domain;
  1111. int class, type; /* class and type of query */
  1112. u_char *answer; /* buffer to put answer */
  1113. int anslen; /* size of answer */
  1114. {
  1115. char nbuf[MAXDNAME];
  1116. const char *longname = nbuf;
  1117. size_t n, d;
  1118. if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) {
  1119. h_errno = NETDB_INTERNAL;
  1120. return (-1);
  1121. }
  1122. #ifdef DEBUG
  1123. if (_res.options & RES_DEBUG)
  1124. printf(";; res_querydomain(%s, %s, %d, %d)\n",
  1125. name, domain?domain:"<Nil>", class, type);
  1126. #endif
  1127. if (domain == NULL) {
  1128. /*
  1129. * Check for trailing '.';
  1130. * copy without '.' if present.
  1131. */
  1132. n = strlen(name);
  1133. if (n + 1 > sizeof(nbuf)) {
  1134. h_errno = NO_RECOVERY;
  1135. return (-1);
  1136. }
  1137. if (n > 0 && name[--n] == '.') {
  1138. strncpy(nbuf, name, n);
  1139. nbuf[n] = '\0';
  1140. } else
  1141. longname = name;
  1142. } else {
  1143. n = strlen(name);
  1144. d = strlen(domain);
  1145. if (n + 1 + d + 1 > sizeof(nbuf)) {
  1146. h_errno = NO_RECOVERY;
  1147. return (-1);
  1148. }
  1149. snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
  1150. }
  1151. return (res_query(longname, class, type, answer, anslen));
  1152. }
  1153. /* res_mkquery */
  1154. /* res_send */
  1155. /* dn_comp */
  1156. /* dn_expand */
  1157. #endif
  1158. #ifdef L_gethostbyaddr
  1159. struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
  1160. {
  1161. static struct hostent h;
  1162. static char buf[
  1163. #ifndef __UCLIBC_HAS_IPV6__
  1164. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1165. #else
  1166. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1167. #endif /* __UCLIBC_HAS_IPV6__ */
  1168. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  1169. struct hostent *hp;
  1170. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  1171. return hp;
  1172. }
  1173. #endif
  1174. #ifdef L_read_etc_hosts_r
  1175. void __open_etc_hosts(FILE **fp)
  1176. {
  1177. if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
  1178. *fp = fopen("/etc/config/hosts", "r");
  1179. }
  1180. return;
  1181. }
  1182. int __read_etc_hosts_r(FILE * fp, const char * name, int type,
  1183. enum etc_hosts_action action,
  1184. struct hostent * result_buf,
  1185. char * buf, size_t buflen,
  1186. struct hostent ** result,
  1187. int * h_errnop)
  1188. {
  1189. struct in_addr *in=NULL;
  1190. struct in_addr **addr_list=NULL;
  1191. #ifdef __UCLIBC_HAS_IPV6__
  1192. struct in6_addr *in6=NULL;
  1193. struct in6_addr **addr_list6=NULL;
  1194. #endif /* __UCLIBC_HAS_IPV6__ */
  1195. char *cp;
  1196. char **alias;
  1197. int aliases, i;
  1198. int ret=HOST_NOT_FOUND;
  1199. if (buflen < sizeof(char *)*(ALIAS_DIM))
  1200. return ERANGE;
  1201. alias=(char **)buf;
  1202. buf+=sizeof(char **)*(ALIAS_DIM);
  1203. buflen-=sizeof(char **)*(ALIAS_DIM);
  1204. if (action!=GETHOSTENT) {
  1205. #ifdef __UCLIBC_HAS_IPV6__
  1206. char *p=buf;
  1207. size_t len=buflen;
  1208. #endif /* __UCLIBC_HAS_IPV6__ */
  1209. *h_errnop=NETDB_INTERNAL;
  1210. if (buflen < sizeof(*in))
  1211. return ERANGE;
  1212. in=(struct in_addr*)buf;
  1213. buf+=sizeof(*in);
  1214. buflen-=sizeof(*in);
  1215. if (buflen < sizeof(*addr_list)*2)
  1216. return ERANGE;
  1217. addr_list=(struct in_addr **)buf;
  1218. buf+=sizeof(*addr_list)*2;
  1219. buflen-=sizeof(*addr_list)*2;
  1220. #ifdef __UCLIBC_HAS_IPV6__
  1221. if (len < sizeof(*in6))
  1222. return ERANGE;
  1223. in6=(struct in6_addr*)p;
  1224. p+=sizeof(*in6);
  1225. len-=sizeof(*in6);
  1226. if (len < sizeof(*addr_list6)*2)
  1227. return ERANGE;
  1228. addr_list6=(struct in6_addr**)p;
  1229. p+=sizeof(*addr_list6)*2;
  1230. len-=sizeof(*addr_list6)*2;
  1231. if (len < buflen) {
  1232. buflen=len;
  1233. buf=p;
  1234. }
  1235. #endif /* __UCLIBC_HAS_IPV6__ */
  1236. if (buflen < 80)
  1237. return ERANGE;
  1238. __open_etc_hosts(&fp);
  1239. if (fp == NULL) {
  1240. result=NULL;
  1241. return errno;
  1242. }
  1243. }
  1244. *h_errnop=HOST_NOT_FOUND;
  1245. while (fgets(buf, buflen, fp)) {
  1246. if ((cp = strchr(buf, '#')))
  1247. *cp = '\0';
  1248. DPRINTF("Looking at: %s\n", buf);
  1249. aliases = 0;
  1250. cp = buf;
  1251. while (*cp) {
  1252. while (*cp && isspace(*cp))
  1253. *cp++ = '\0';
  1254. if (!*cp)
  1255. continue;
  1256. if (aliases < (2+MAX_ALIASES))
  1257. alias[aliases++] = cp;
  1258. while (*cp && !isspace(*cp))
  1259. cp++;
  1260. }
  1261. alias[aliases] = 0;
  1262. if (aliases < 2)
  1263. continue; /* syntax error really */
  1264. if (action==GETHOSTENT) {
  1265. /* Return whatever the next entry happens to be. */
  1266. break;
  1267. } else if (action==GET_HOSTS_BYADDR) {
  1268. if (strcmp(name, alias[0]) != 0)
  1269. continue;
  1270. } else {
  1271. /* GET_HOSTS_BYNAME */
  1272. for (i = 1; i < aliases; i++)
  1273. if (strcasecmp(name, alias[i]) == 0)
  1274. break;
  1275. if (i >= aliases)
  1276. continue;
  1277. }
  1278. if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
  1279. DPRINTF("Found INET\n");
  1280. addr_list[0] = in;
  1281. addr_list[1] = 0;
  1282. result_buf->h_name = alias[1];
  1283. result_buf->h_addrtype = AF_INET;
  1284. result_buf->h_length = sizeof(*in);
  1285. result_buf->h_addr_list = (char**) addr_list;
  1286. result_buf->h_aliases = alias + 2;
  1287. *result=result_buf;
  1288. ret=NETDB_SUCCESS;
  1289. #ifdef __UCLIBC_HAS_IPV6__
  1290. } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
  1291. DPRINTF("Found INET6\n");
  1292. addr_list6[0] = in6;
  1293. addr_list6[1] = 0;
  1294. result_buf->h_name = alias[1];
  1295. result_buf->h_addrtype = AF_INET6;
  1296. result_buf->h_length = sizeof(*in6);
  1297. result_buf->h_addr_list = (char**) addr_list6;
  1298. result_buf->h_aliases = alias + 2;
  1299. *result=result_buf;
  1300. ret=NETDB_SUCCESS;
  1301. #endif /* __UCLIBC_HAS_IPV6__ */
  1302. } else {
  1303. DPRINTF("Error\n");
  1304. ret=TRY_AGAIN;
  1305. break; /* bad ip address */
  1306. }
  1307. if (action!=GETHOSTENT) {
  1308. fclose(fp);
  1309. }
  1310. return ret;
  1311. }
  1312. if (action!=GETHOSTENT) {
  1313. fclose(fp);
  1314. }
  1315. return ret;
  1316. }
  1317. #endif
  1318. #ifdef L_gethostent
  1319. #ifdef __UCLIBC_HAS_THREADS__
  1320. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  1321. # define LOCK __pthread_mutex_lock(&mylock)
  1322. # define UNLOCK __pthread_mutex_unlock(&mylock);
  1323. #else
  1324. # define LOCK
  1325. # define UNLOCK
  1326. #endif
  1327. static int __stay_open;
  1328. static FILE * __gethostent_fp;
  1329. void endhostent (void)
  1330. {
  1331. LOCK;
  1332. __stay_open = 0;
  1333. if (__gethostent_fp) {
  1334. fclose(__gethostent_fp);
  1335. }
  1336. UNLOCK;
  1337. }
  1338. void sethostent (int stay_open)
  1339. {
  1340. LOCK;
  1341. __stay_open = stay_open;
  1342. UNLOCK;
  1343. }
  1344. struct hostent *gethostent (void)
  1345. {
  1346. static struct hostent h;
  1347. static char buf[
  1348. #ifndef __UCLIBC_HAS_IPV6__
  1349. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1350. #else
  1351. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1352. #endif /* __UCLIBC_HAS_IPV6__ */
  1353. sizeof(char *)*(ALIAS_DIM) +
  1354. 80/*namebuffer*/ + 2/* margin */];
  1355. struct hostent *host;
  1356. LOCK;
  1357. if (__gethostent_fp == NULL) {
  1358. __open_etc_hosts(&__gethostent_fp);
  1359. if (__gethostent_fp == NULL) {
  1360. UNLOCK;
  1361. return((struct hostent *)NULL);
  1362. }
  1363. }
  1364. __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  1365. &h, buf, sizeof(buf), &host, &h_errno);
  1366. if (__stay_open==0) {
  1367. fclose(__gethostent_fp);
  1368. }
  1369. UNLOCK;
  1370. return(host);
  1371. }
  1372. #endif
  1373. #ifdef L_get_hosts_byname_r
  1374. int __get_hosts_byname_r(const char * name, int type,
  1375. struct hostent * result_buf,
  1376. char * buf, size_t buflen,
  1377. struct hostent ** result,
  1378. int * h_errnop)
  1379. {
  1380. return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, result_buf, buf, buflen, result, h_errnop));
  1381. }
  1382. #endif
  1383. #ifdef L_get_hosts_byaddr_r
  1384. int __get_hosts_byaddr_r(const char * addr, int len, int type,
  1385. struct hostent * result_buf,
  1386. char * buf, size_t buflen,
  1387. struct hostent ** result,
  1388. int * h_errnop)
  1389. {
  1390. #ifndef __UCLIBC_HAS_IPV6__
  1391. char ipaddr[INET_ADDRSTRLEN];
  1392. #else
  1393. char ipaddr[INET6_ADDRSTRLEN];
  1394. #endif /* __UCLIBC_HAS_IPV6__ */
  1395. switch (type) {
  1396. case AF_INET:
  1397. if (len != sizeof(struct in_addr))
  1398. return 0;
  1399. break;
  1400. #ifdef __UCLIBC_HAS_IPV6__
  1401. case AF_INET6:
  1402. if (len != sizeof(struct in6_addr))
  1403. return 0;
  1404. break;
  1405. #endif /* __UCLIBC_HAS_IPV6__ */
  1406. default:
  1407. return 0;
  1408. }
  1409. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1410. return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1411. result_buf, buf, buflen, result, h_errnop));
  1412. }
  1413. #endif
  1414. #ifdef L_getnameinfo
  1415. #ifndef min
  1416. # define min(x,y) (((x) > (y)) ? (y) : (x))
  1417. #endif /* min */
  1418. int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  1419. socklen_t hostlen, char *serv, socklen_t servlen,
  1420. unsigned int flags)
  1421. {
  1422. int serrno = errno;
  1423. int ok = 0;
  1424. struct hostent *h = NULL;
  1425. char domain[256];
  1426. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1427. return EAI_BADFLAGS;
  1428. if (sa == NULL || addrlen < sizeof (sa_family_t))
  1429. return EAI_FAMILY;
  1430. switch (sa->sa_family) {
  1431. case AF_LOCAL:
  1432. break;
  1433. case AF_INET:
  1434. if (addrlen < sizeof (struct sockaddr_in))
  1435. return EAI_FAMILY;
  1436. break;
  1437. #ifdef __UCLIBC_HAS_IPV6__
  1438. case AF_INET6:
  1439. if (addrlen < sizeof (struct sockaddr_in6))
  1440. return EAI_FAMILY;
  1441. break;
  1442. #endif /* __UCLIBC_HAS_IPV6__ */
  1443. default:
  1444. return EAI_FAMILY;
  1445. }
  1446. if (host != NULL && hostlen > 0)
  1447. switch (sa->sa_family) {
  1448. case AF_INET:
  1449. #ifdef __UCLIBC_HAS_IPV6__
  1450. case AF_INET6:
  1451. #endif /* __UCLIBC_HAS_IPV6__ */
  1452. if (!(flags & NI_NUMERICHOST)) {
  1453. #ifdef __UCLIBC_HAS_IPV6__
  1454. if (sa->sa_family == AF_INET6)
  1455. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1456. sizeof(struct in6_addr), AF_INET6);
  1457. else
  1458. #endif /* __UCLIBC_HAS_IPV6__ */
  1459. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1460. sizeof(struct in_addr), AF_INET);
  1461. if (h) {
  1462. char *c;
  1463. if ((flags & NI_NOFQDN)
  1464. && (getdomainname (domain, sizeof(domain)) == 0)
  1465. && (c = strstr (h->h_name, domain))
  1466. && (c != h->h_name) && (*(--c) == '.')) {
  1467. strncpy (host, h->h_name,
  1468. min(hostlen, (size_t) (c - h->h_name)));
  1469. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1470. ok = 1;
  1471. } else {
  1472. strncpy (host, h->h_name, hostlen);
  1473. ok = 1;
  1474. }
  1475. }
  1476. }
  1477. if (!ok) {
  1478. if (flags & NI_NAMEREQD) {
  1479. errno = serrno;
  1480. return EAI_NONAME;
  1481. } else {
  1482. const char *c;
  1483. #ifdef __UCLIBC_HAS_IPV6__
  1484. if (sa->sa_family == AF_INET6) {
  1485. const struct sockaddr_in6 *sin6p;
  1486. sin6p = (const struct sockaddr_in6 *) sa;
  1487. c = inet_ntop (AF_INET6,
  1488. (const void *) &sin6p->sin6_addr, host, hostlen);
  1489. #if 0
  1490. /* Does scope id need to be supported? */
  1491. uint32_t scopeid;
  1492. scopeid = sin6p->sin6_scope_id;
  1493. if (scopeid != 0) {
  1494. /* Buffer is >= IFNAMSIZ+1. */
  1495. char scopebuf[IFNAMSIZ + 1];
  1496. char *scopeptr;
  1497. int ni_numericscope = 0;
  1498. size_t real_hostlen = __strnlen (host, hostlen);
  1499. size_t scopelen = 0;
  1500. scopebuf[0] = SCOPE_DELIMITER;
  1501. scopebuf[1] = '\0';
  1502. scopeptr = &scopebuf[1];
  1503. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1504. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1505. if (if_indextoname (scopeid, scopeptr) == NULL)
  1506. ++ni_numericscope;
  1507. else
  1508. scopelen = strlen (scopebuf);
  1509. } else {
  1510. ++ni_numericscope;
  1511. }
  1512. if (ni_numericscope)
  1513. scopelen = 1 + snprintf (scopeptr,
  1514. (scopebuf
  1515. + sizeof scopebuf
  1516. - scopeptr),
  1517. "%u", scopeid);
  1518. if (real_hostlen + scopelen + 1 > hostlen)
  1519. return EAI_SYSTEM;
  1520. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1521. }
  1522. #endif
  1523. } else
  1524. #endif /* __UCLIBC_HAS_IPV6__ */
  1525. c = inet_ntop (AF_INET,
  1526. (const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
  1527. host, hostlen);
  1528. if (c == NULL) {
  1529. errno = serrno;
  1530. return EAI_SYSTEM;
  1531. }
  1532. }
  1533. ok = 1;
  1534. }
  1535. break;
  1536. case AF_LOCAL:
  1537. if (!(flags & NI_NUMERICHOST)) {
  1538. struct utsname utsname;
  1539. if (!uname (&utsname)) {
  1540. strncpy (host, utsname.nodename, hostlen);
  1541. break;
  1542. };
  1543. };
  1544. if (flags & NI_NAMEREQD) {
  1545. errno = serrno;
  1546. return EAI_NONAME;
  1547. }
  1548. strncpy (host, "localhost", hostlen);
  1549. break;
  1550. default:
  1551. return EAI_FAMILY;
  1552. }
  1553. if (serv && (servlen > 0)) {
  1554. switch (sa->sa_family) {
  1555. case AF_INET:
  1556. #ifdef __UCLIBC_HAS_IPV6__
  1557. case AF_INET6:
  1558. #endif /* __UCLIBC_HAS_IPV6__ */
  1559. if (!(flags & NI_NUMERICSERV)) {
  1560. struct servent *s;
  1561. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1562. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1563. if (s) {
  1564. strncpy (serv, s->s_name, servlen);
  1565. break;
  1566. }
  1567. }
  1568. snprintf (serv, servlen, "%d",
  1569. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1570. break;
  1571. case AF_LOCAL:
  1572. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1573. break;
  1574. }
  1575. }
  1576. if (host && (hostlen > 0))
  1577. host[hostlen-1] = 0;
  1578. if (serv && (servlen > 0))
  1579. serv[servlen-1] = 0;
  1580. errno = serrno;
  1581. return 0;
  1582. }
  1583. #endif
  1584. #ifdef L_gethostbyname_r
  1585. int gethostbyname_r(const char * name,
  1586. struct hostent * result_buf,
  1587. char * buf, size_t buflen,
  1588. struct hostent ** result,
  1589. int * h_errnop)
  1590. {
  1591. struct in_addr *in;
  1592. struct in_addr **addr_list;
  1593. char **alias;
  1594. unsigned char *packet;
  1595. struct resolv_answer a;
  1596. int i;
  1597. int nest = 0;
  1598. int __nameserversXX;
  1599. char ** __nameserverXX;
  1600. if (__open_nameservers()) {
  1601. return(-1);
  1602. }
  1603. *result=NULL;
  1604. if (!name)
  1605. return EINVAL;
  1606. /* do /etc/hosts first */
  1607. {
  1608. int old_errno = errno; /* Save the old errno and reset errno */
  1609. __set_errno(0); /* to check for missing /etc/hosts. */
  1610. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1611. buf, buflen, result, h_errnop))==0)
  1612. return i;
  1613. switch (*h_errnop) {
  1614. case HOST_NOT_FOUND:
  1615. case NO_ADDRESS:
  1616. break;
  1617. case NETDB_INTERNAL:
  1618. if (errno == ENOENT) {
  1619. break;
  1620. }
  1621. /* else fall through */
  1622. default:
  1623. return i;
  1624. }
  1625. __set_errno(old_errno);
  1626. }
  1627. DPRINTF("Nothing found in /etc/hosts\n");
  1628. *h_errnop = NETDB_INTERNAL;
  1629. if (buflen < sizeof(*in))
  1630. return ERANGE;
  1631. in=(struct in_addr*)buf;
  1632. buf+=sizeof(*in);
  1633. buflen-=sizeof(*in);
  1634. if (buflen < sizeof(*addr_list)*2)
  1635. return ERANGE;
  1636. addr_list=(struct in_addr**)buf;
  1637. buf+=sizeof(*addr_list)*2;
  1638. buflen-=sizeof(*addr_list)*2;
  1639. addr_list[0] = in;
  1640. addr_list[1] = 0;
  1641. if (buflen < sizeof(char *)*(ALIAS_DIM))
  1642. return ERANGE;
  1643. alias=(char **)buf;
  1644. buf+=sizeof(char **)*(ALIAS_DIM);
  1645. buflen-=sizeof(char **)*(ALIAS_DIM);
  1646. if (buflen<256)
  1647. return ERANGE;
  1648. strncpy(buf, name, buflen);
  1649. alias[0] = buf;
  1650. alias[1] = NULL;
  1651. /* First check if this is already an address */
  1652. if (inet_aton(name, in)) {
  1653. result_buf->h_name = buf;
  1654. result_buf->h_addrtype = AF_INET;
  1655. result_buf->h_length = sizeof(*in);
  1656. result_buf->h_addr_list = (char **) addr_list;
  1657. result_buf->h_aliases = alias;
  1658. *result=result_buf;
  1659. *h_errnop = NETDB_SUCCESS;
  1660. return NETDB_SUCCESS;
  1661. }
  1662. for (;;) {
  1663. BIGLOCK;
  1664. __nameserversXX=__nameservers;
  1665. __nameserverXX=__nameserver;
  1666. BIGUNLOCK;
  1667. i = __dns_lookup(buf, T_A, __nameserversXX, __nameserverXX, &packet, &a);
  1668. if (i < 0) {
  1669. *h_errnop = HOST_NOT_FOUND;
  1670. DPRINTF("__dns_lookup\n");
  1671. return TRY_AGAIN;
  1672. }
  1673. strncpy(buf, a.dotted, buflen);
  1674. free(a.dotted);
  1675. if (a.atype == T_CNAME) { /* CNAME */
  1676. DPRINTF("Got a CNAME in gethostbyname()\n");
  1677. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1678. free(packet);
  1679. if (i < 0) {
  1680. *h_errnop = NO_RECOVERY;
  1681. DPRINTF("__decode_dotted\n");
  1682. return -1;
  1683. }
  1684. if (++nest > MAX_RECURSE) {
  1685. *h_errnop = NO_RECOVERY;
  1686. DPRINTF("recursion\n");
  1687. return -1;
  1688. }
  1689. continue;
  1690. } else if (a.atype == T_A) { /* ADDRESS */
  1691. memcpy(in, a.rdata, sizeof(*in));
  1692. result_buf->h_name = buf;
  1693. result_buf->h_addrtype = AF_INET;
  1694. result_buf->h_length = sizeof(*in);
  1695. result_buf->h_addr_list = (char **) addr_list;
  1696. #ifdef __UCLIBC_MJN3_ONLY__
  1697. #warning TODO -- generate the full list
  1698. #endif
  1699. result_buf->h_aliases = alias; /* TODO: generate the full list */
  1700. free(packet);
  1701. break;
  1702. } else {
  1703. free(packet);
  1704. *h_errnop=HOST_NOT_FOUND;
  1705. return TRY_AGAIN;
  1706. }
  1707. }
  1708. *result=result_buf;
  1709. *h_errnop = NETDB_SUCCESS;
  1710. return NETDB_SUCCESS;
  1711. }
  1712. #endif
  1713. #ifdef L_gethostbyname2_r
  1714. int gethostbyname2_r(const char *name, int family,
  1715. struct hostent * result_buf,
  1716. char * buf, size_t buflen,
  1717. struct hostent ** result,
  1718. int * h_errnop)
  1719. {
  1720. #ifndef __UCLIBC_HAS_IPV6__
  1721. return family == AF_INET ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1722. #else /* __UCLIBC_HAS_IPV6__ */
  1723. struct in6_addr *in;
  1724. struct in6_addr **addr_list;
  1725. unsigned char *packet;
  1726. struct resolv_answer a;
  1727. int i;
  1728. int nest = 0;
  1729. int __nameserversXX;
  1730. char ** __nameserverXX;
  1731. if (family == AF_INET)
  1732. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1733. if (family != AF_INET6)
  1734. return EINVAL;
  1735. if (__open_nameservers()) {
  1736. return(-1);
  1737. }
  1738. *result=NULL;
  1739. if (!name)
  1740. return EINVAL;
  1741. /* do /etc/hosts first */
  1742. {
  1743. int old_errno = errno; /* Save the old errno and reset errno */
  1744. __set_errno(0); /* to check for missing /etc/hosts. */
  1745. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1746. buf, buflen, result, h_errnop))==0)
  1747. return i;
  1748. switch (*h_errnop) {
  1749. case HOST_NOT_FOUND:
  1750. case NO_ADDRESS:
  1751. break;
  1752. case NETDB_INTERNAL:
  1753. if (errno == ENOENT) {
  1754. break;
  1755. }
  1756. /* else fall through */
  1757. default:
  1758. return i;
  1759. }
  1760. __set_errno(old_errno);
  1761. }
  1762. DPRINTF("Nothing found in /etc/hosts\n");
  1763. *h_errnop = NETDB_INTERNAL;
  1764. if (buflen < sizeof(*in))
  1765. return ERANGE;
  1766. in=(struct in6_addr*)buf;
  1767. buf+=sizeof(*in);
  1768. buflen-=sizeof(*in);
  1769. if (buflen < sizeof(*addr_list)*2)
  1770. return ERANGE;
  1771. addr_list=(struct in6_addr**)buf;
  1772. buf+=sizeof(*addr_list)*2;
  1773. buflen-=sizeof(*addr_list)*2;
  1774. addr_list[0] = in;
  1775. addr_list[1] = 0;
  1776. if (buflen<256)
  1777. return ERANGE;
  1778. strncpy(buf, name, buflen);
  1779. /* First check if this is already an address */
  1780. if (inet_pton(AF_INET6, name, in)) {
  1781. result_buf->h_name = buf;
  1782. result_buf->h_addrtype = AF_INET6;
  1783. result_buf->h_length = sizeof(*in);
  1784. result_buf->h_addr_list = (char **) addr_list;
  1785. *result=result_buf;
  1786. *h_errnop = NETDB_SUCCESS;
  1787. return NETDB_SUCCESS;
  1788. }
  1789. for (;;) {
  1790. BIGLOCK;
  1791. __nameserversXX=__nameservers;
  1792. __nameserverXX=__nameserver;
  1793. BIGUNLOCK;
  1794. i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
  1795. if (i < 0) {
  1796. *h_errnop = HOST_NOT_FOUND;
  1797. return TRY_AGAIN;
  1798. }
  1799. strncpy(buf, a.dotted, buflen);
  1800. free(a.dotted);
  1801. if (a.atype == T_CNAME) { /* CNAME */
  1802. DPRINTF("Got a CNAME in gethostbyname()\n");
  1803. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1804. free(packet);
  1805. if (i < 0) {
  1806. *h_errnop = NO_RECOVERY;
  1807. return -1;
  1808. }
  1809. if (++nest > MAX_RECURSE) {
  1810. *h_errnop = NO_RECOVERY;
  1811. return -1;
  1812. }
  1813. continue;
  1814. } else if (a.atype == T_AAAA) { /* ADDRESS */
  1815. memcpy(in, a.rdata, sizeof(*in));
  1816. result_buf->h_name = buf;
  1817. result_buf->h_addrtype = AF_INET6;
  1818. result_buf->h_length = sizeof(*in);
  1819. result_buf->h_addr_list = (char **) addr_list;
  1820. free(packet);
  1821. break;
  1822. } else {
  1823. free(packet);
  1824. *h_errnop=HOST_NOT_FOUND;
  1825. return TRY_AGAIN;
  1826. }
  1827. }
  1828. *result=result_buf;
  1829. *h_errnop = NETDB_SUCCESS;
  1830. return NETDB_SUCCESS;
  1831. #endif /* __UCLIBC_HAS_IPV6__ */
  1832. }
  1833. #endif
  1834. #ifdef L_gethostbyaddr_r
  1835. int gethostbyaddr_r (const void *addr, socklen_t len, int type,
  1836. struct hostent * result_buf,
  1837. char * buf, size_t buflen,
  1838. struct hostent ** result,
  1839. int * h_errnop)
  1840. {
  1841. struct in_addr *in;
  1842. struct in_addr **addr_list;
  1843. #ifdef __UCLIBC_HAS_IPV6__
  1844. char *qp;
  1845. size_t plen;
  1846. struct in6_addr *in6;
  1847. struct in6_addr **addr_list6;
  1848. #endif /* __UCLIBC_HAS_IPV6__ */
  1849. unsigned char *packet;
  1850. struct resolv_answer a;
  1851. int i;
  1852. int nest = 0;
  1853. int __nameserversXX;
  1854. char ** __nameserverXX;
  1855. *result=NULL;
  1856. if (!addr)
  1857. return EINVAL;
  1858. switch (type) {
  1859. case AF_INET:
  1860. if (len != sizeof(struct in_addr))
  1861. return EINVAL;
  1862. break;
  1863. #ifdef __UCLIBC_HAS_IPV6__
  1864. case AF_INET6:
  1865. if (len != sizeof(struct in6_addr))
  1866. return EINVAL;
  1867. break;
  1868. #endif /* __UCLIBC_HAS_IPV6__ */
  1869. default:
  1870. return EINVAL;
  1871. }
  1872. /* do /etc/hosts first */
  1873. if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
  1874. buf, buflen, result, h_errnop))==0)
  1875. return i;
  1876. switch (*h_errnop) {
  1877. case HOST_NOT_FOUND:
  1878. case NO_ADDRESS:
  1879. break;
  1880. default:
  1881. return i;
  1882. }
  1883. if (__open_nameservers()) {
  1884. return(-1);
  1885. }
  1886. #ifdef __UCLIBC_HAS_IPV6__
  1887. qp=buf;
  1888. plen=buflen;
  1889. #endif /* __UCLIBC_HAS_IPV6__ */
  1890. *h_errnop = NETDB_INTERNAL;
  1891. if (buflen < sizeof(*in))
  1892. return ERANGE;
  1893. in=(struct in_addr*)buf;
  1894. buf+=sizeof(*in);
  1895. buflen-=sizeof(*in);
  1896. if (buflen < sizeof(*addr_list)*2)
  1897. return ERANGE;
  1898. addr_list=(struct in_addr**)buf;
  1899. buf+=sizeof(*addr_list)*2;
  1900. buflen-=sizeof(*addr_list)*2;
  1901. #ifdef __UCLIBC_HAS_IPV6__
  1902. if (plen < sizeof(*in6))
  1903. return ERANGE;
  1904. in6=(struct in6_addr*)qp;
  1905. qp+=sizeof(*in6);
  1906. plen-=sizeof(*in6);
  1907. if (plen < sizeof(*addr_list6)*2)
  1908. return ERANGE;
  1909. addr_list6=(struct in6_addr**)qp;
  1910. qp+=sizeof(*addr_list6)*2;
  1911. plen-=sizeof(*addr_list6)*2;
  1912. if (plen < buflen) {
  1913. buflen=plen;
  1914. buf=qp;
  1915. }
  1916. #endif /* __UCLIBC_HAS_IPV6__ */
  1917. if (buflen<256)
  1918. return ERANGE;
  1919. if(type == AF_INET) {
  1920. unsigned char *tmp_addr = (unsigned char *)addr;
  1921. memcpy(&in->s_addr, addr, len);
  1922. addr_list[0] = in;
  1923. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  1924. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  1925. #ifdef __UCLIBC_HAS_IPV6__
  1926. } else {
  1927. memcpy(in6->s6_addr, addr, len);
  1928. addr_list6[0] = in6;
  1929. qp = buf;
  1930. for (i = len - 1; i >= 0; i--) {
  1931. qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
  1932. (in6->s6_addr[i] >> 4) & 0xf);
  1933. }
  1934. strcpy(qp, "ip6.int");
  1935. #endif /* __UCLIBC_HAS_IPV6__ */
  1936. }
  1937. addr_list[1] = 0;
  1938. for (;;) {
  1939. BIGLOCK;
  1940. __nameserversXX=__nameservers;
  1941. __nameserverXX=__nameserver;
  1942. BIGUNLOCK;
  1943. i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
  1944. if (i < 0) {
  1945. *h_errnop = HOST_NOT_FOUND;
  1946. return TRY_AGAIN;
  1947. }
  1948. strncpy(buf, a.dotted, buflen);
  1949. free(a.dotted);
  1950. if (a.atype == T_CNAME) { /* CNAME */
  1951. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  1952. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1953. free(packet);
  1954. if (i < 0) {
  1955. *h_errnop = NO_RECOVERY;
  1956. return -1;
  1957. }
  1958. if (++nest > MAX_RECURSE) {
  1959. *h_errnop = NO_RECOVERY;
  1960. return -1;
  1961. }
  1962. continue;
  1963. } else if (a.atype == T_PTR) { /* ADDRESS */
  1964. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1965. free(packet);
  1966. result_buf->h_name = buf;
  1967. result_buf->h_addrtype = type;
  1968. if(type == AF_INET) {
  1969. result_buf->h_length = sizeof(*in);
  1970. #ifdef __UCLIBC_HAS_IPV6__
  1971. } else {
  1972. result_buf->h_length = sizeof(*in6);
  1973. #endif /* __UCLIBC_HAS_IPV6__ */
  1974. }
  1975. result_buf->h_addr_list = (char **) addr_list;
  1976. break;
  1977. } else {
  1978. free(packet);
  1979. *h_errnop = NO_ADDRESS;
  1980. return TRY_AGAIN;
  1981. }
  1982. }
  1983. *result=result_buf;
  1984. *h_errnop = NETDB_SUCCESS;
  1985. return NETDB_SUCCESS;
  1986. }
  1987. #endif
  1988. #ifdef L_res_comp
  1989. /*
  1990. * Expand compressed domain name 'comp_dn' to full domain name.
  1991. * 'msg' is a pointer to the begining of the message,
  1992. * 'eomorig' points to the first location after the message,
  1993. * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  1994. * Return size of compressed name or -1 if there was an error.
  1995. */
  1996. int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  1997. char *dst, int dstsiz)
  1998. {
  1999. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  2000. if (n > 0 && dst[0] == '.')
  2001. dst[0] = '\0';
  2002. return (n);
  2003. }
  2004. #endif /* L_res_comp */
  2005. #ifdef L_ns_name
  2006. /*
  2007. * printable(ch)
  2008. * Thinking in noninternationalized USASCII (per the DNS spec),
  2009. * is this character visible and not a space when printed ?
  2010. * return:
  2011. * boolean.
  2012. */
  2013. static int printable(int ch)
  2014. {
  2015. return (ch > 0x20 && ch < 0x7f);
  2016. }
  2017. /*
  2018. * special(ch)
  2019. * Thinking in noninternationalized USASCII (per the DNS spec),
  2020. * is this characted special ("in need of quoting") ?
  2021. * return:
  2022. * boolean.
  2023. */
  2024. static int special(int ch)
  2025. {
  2026. switch (ch) {
  2027. case 0x22: /* '"' */
  2028. case 0x2E: /* '.' */
  2029. case 0x3B: /* ';' */
  2030. case 0x5C: /* '\\' */
  2031. /* Special modifiers in zone files. */
  2032. case 0x40: /* '@' */
  2033. case 0x24: /* '$' */
  2034. return (1);
  2035. default:
  2036. return (0);
  2037. }
  2038. }
  2039. /*
  2040. * ns_name_uncompress(msg, eom, src, dst, dstsiz)
  2041. * Expand compressed domain name to presentation format.
  2042. * return:
  2043. * Number of bytes read out of `src', or -1 (with errno set).
  2044. * note:
  2045. * Root domain returns as "." not "".
  2046. */
  2047. int __ns_name_uncompress(const u_char *msg, const u_char *eom,
  2048. const u_char *src, char *dst, size_t dstsiz)
  2049. {
  2050. u_char tmp[NS_MAXCDNAME];
  2051. int n;
  2052. if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
  2053. return (-1);
  2054. if (ns_name_ntop(tmp, dst, dstsiz) == -1)
  2055. return (-1);
  2056. return (n);
  2057. }
  2058. /*
  2059. * ns_name_ntop(src, dst, dstsiz)
  2060. * Convert an encoded domain name to printable ascii as per RFC1035.
  2061. * return:
  2062. * Number of bytes written to buffer, or -1 (with errno set)
  2063. * notes:
  2064. * The root is returned as "."
  2065. * All other domains are returned in non absolute form
  2066. */
  2067. int __ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
  2068. const u_char *cp;
  2069. char *dn, *eom;
  2070. u_char c;
  2071. u_int n;
  2072. const char digits[] = "0123456789";
  2073. cp = src;
  2074. dn = dst;
  2075. eom = dst + dstsiz;
  2076. while ((n = *cp++) != 0) {
  2077. if ((n & NS_CMPRSFLGS) != 0) {
  2078. /* Some kind of compression pointer. */
  2079. __set_errno (EMSGSIZE);
  2080. return (-1);
  2081. }
  2082. if (dn != dst) {
  2083. if (dn >= eom) {
  2084. __set_errno (EMSGSIZE);
  2085. return (-1);
  2086. }
  2087. *dn++ = '.';
  2088. }
  2089. if (dn + n >= eom) {
  2090. __set_errno (EMSGSIZE);
  2091. return (-1);
  2092. }
  2093. for ((void)NULL; n > 0; n--) {
  2094. c = *cp++;
  2095. if (special(c)) {
  2096. if (dn + 1 >= eom) {
  2097. __set_errno (EMSGSIZE);
  2098. return (-1);
  2099. }
  2100. *dn++ = '\\';
  2101. *dn++ = (char)c;
  2102. } else if (!printable(c)) {
  2103. if (dn + 3 >= eom) {
  2104. __set_errno (EMSGSIZE);
  2105. return (-1);
  2106. }
  2107. *dn++ = '\\';
  2108. *dn++ = digits[c / 100];
  2109. *dn++ = digits[(c % 100) / 10];
  2110. *dn++ = digits[c % 10];
  2111. } else {
  2112. if (dn >= eom) {
  2113. __set_errno (EMSGSIZE);
  2114. return (-1);
  2115. }
  2116. *dn++ = (char)c;
  2117. }
  2118. }
  2119. }
  2120. if (dn == dst) {
  2121. if (dn >= eom) {
  2122. __set_errno (EMSGSIZE);
  2123. return (-1);
  2124. }
  2125. *dn++ = '.';
  2126. }
  2127. if (dn >= eom) {
  2128. __set_errno (EMSGSIZE);
  2129. return (-1);
  2130. }
  2131. *dn++ = '\0';
  2132. return (dn - dst);
  2133. }
  2134. /*
  2135. * ns_name_unpack(msg, eom, src, dst, dstsiz)
  2136. * Unpack a domain name from a message, source may be compressed.
  2137. * return:
  2138. * -1 if it fails, or consumed octets if it succeeds.
  2139. */
  2140. int __ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
  2141. u_char *dst, size_t dstsiz)
  2142. {
  2143. const u_char *srcp, *dstlim;
  2144. u_char *dstp;
  2145. int n, len, checked;
  2146. len = -1;
  2147. checked = 0;
  2148. dstp = dst;
  2149. srcp = src;
  2150. dstlim = dst + dstsiz;
  2151. if (srcp < msg || srcp >= eom) {
  2152. __set_errno (EMSGSIZE);
  2153. return (-1);
  2154. }
  2155. /* Fetch next label in domain name. */
  2156. while ((n = *srcp++) != 0) {
  2157. /* Check for indirection. */
  2158. switch (n & NS_CMPRSFLGS) {
  2159. case 0:
  2160. /* Limit checks. */
  2161. if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  2162. __set_errno (EMSGSIZE);
  2163. return (-1);
  2164. }
  2165. checked += n + 1;
  2166. *dstp++ = n;
  2167. memcpy(dstp, srcp, n);
  2168. dstp += n;
  2169. srcp += n;
  2170. break;
  2171. case NS_CMPRSFLGS:
  2172. if (srcp >= eom) {
  2173. __set_errno (EMSGSIZE);
  2174. return (-1);
  2175. }
  2176. if (len < 0)
  2177. len = srcp - src + 1;
  2178. srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  2179. if (srcp < msg || srcp >= eom) { /* Out of range. */
  2180. __set_errno (EMSGSIZE);
  2181. return (-1);
  2182. }
  2183. checked += 2;
  2184. /*
  2185. * Check for loops in the compressed name;
  2186. * if we've looked at the whole message,
  2187. * there must be a loop.
  2188. */
  2189. if (checked >= eom - msg) {
  2190. __set_errno (EMSGSIZE);
  2191. return (-1);
  2192. }
  2193. break;
  2194. default:
  2195. __set_errno (EMSGSIZE);
  2196. return (-1); /* flag error */
  2197. }
  2198. }
  2199. *dstp = '\0';
  2200. if (len < 0)
  2201. len = srcp - src;
  2202. return (len);
  2203. }
  2204. #endif /* L_ns_name */