resolv.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  1. /* resolv.c: DNS Resolver
  2. *
  3. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
  4. * The Silver Hammer Group, Ltd.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. */
  11. /*
  12. * Portions Copyright (c) 1985, 1993
  13. * The Regents of the University of California. All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 4. Neither the name of the University nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37. * SUCH DAMAGE.
  38. */
  39. /*
  40. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  41. *
  42. * Permission to use, copy, modify, and distribute this software for any
  43. * purpose with or without fee is hereby granted, provided that the above
  44. * copyright notice and this permission notice appear in all copies, and that
  45. * the name of Digital Equipment Corporation not be used in advertising or
  46. * publicity pertaining to distribution of the document or software without
  47. * specific, written prior permission.
  48. *
  49. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  50. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  51. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  52. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  53. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  54. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  55. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  56. * SOFTWARE.
  57. */
  58. /*
  59. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  60. *
  61. * Permission to use, copy, modify, and distribute this software for any
  62. * purpose with or without fee is hereby granted, provided that the above
  63. * copyright notice and this permission notice appear in all copies.
  64. *
  65. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  66. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  67. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  68. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  69. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  70. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  71. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  72. * SOFTWARE.
  73. */
  74. /*
  75. *
  76. * 5-Oct-2000 W. Greathouse wgreathouse@smva.com
  77. * Fix memory leak and memory corruption.
  78. * -- Every name resolution resulted in
  79. * a new parse of resolv.conf and new
  80. * copy of nameservers allocated by
  81. * strdup.
  82. * -- Every name resolution resulted in
  83. * a new read of resolv.conf without
  84. * resetting index from prior read...
  85. * resulting in exceeding array bounds.
  86. *
  87. * Limit nameservers read from resolv.conf
  88. *
  89. * Add "search" domains from resolv.conf
  90. *
  91. * Some systems will return a security
  92. * signature along with query answer for
  93. * dynamic DNS entries.
  94. * -- skip/ignore this answer
  95. *
  96. * Include arpa/nameser.h for defines.
  97. *
  98. * General cleanup
  99. *
  100. * 20-Jun-2001 Michal Moskal <malekith@pld.org.pl>
  101. * partial IPv6 support (i.e. gethostbyname2() and resolve_address2()
  102. * functions added), IPv6 nameservers are also supported.
  103. *
  104. * 6-Oct-2001 Jari Korva <jari.korva@iki.fi>
  105. * more IPv6 support (IPv6 support for gethostbyaddr();
  106. * address family parameter and improved IPv6 support for get_hosts_byname
  107. * and read_etc_hosts; getnameinfo() port from glibc; defined
  108. * defined ip6addr_any and in6addr_loopback)
  109. *
  110. * 2-Feb-2002 Erik Andersen <andersen@codepoet.org>
  111. * Added gethostent(), sethostent(), and endhostent()
  112. *
  113. * 17-Aug-2002 Manuel Novoa III <mjn3@codepoet.org>
  114. * Fixed __read_etc_hosts_r to return alias list, and modified buffer
  115. * allocation accordingly. See MAX_ALIASES and ALIAS_DIM below.
  116. * This fixes the segfault in the Python 2.2.1 socket test.
  117. *
  118. * 04-Jan-2003 Jay Kulpinski <jskulpin@berkshire.rr.com>
  119. * Fixed __decode_dotted to count the terminating null character
  120. * in a host name.
  121. *
  122. * 02-Oct-2003 Tony J. White <tjw@tjw.org>
  123. * Lifted dn_expand() and dependent ns_name_uncompress(), ns_name_unpack(),
  124. * and ns_name_ntop() from glibc 2.3.2 for compatibility with ipsec-tools
  125. * and openldap.
  126. *
  127. * 7-Sep-2004 Erik Andersen <andersen@codepoet.org>
  128. * Added gethostent_r()
  129. *
  130. */
  131. #define __FORCE_GLIBC
  132. #include <features.h>
  133. #include <string.h>
  134. #include <stdio.h>
  135. #include <signal.h>
  136. #include <errno.h>
  137. #include <sys/socket.h>
  138. #include <sys/types.h>
  139. #include <sys/time.h>
  140. #include <netinet/in.h>
  141. #include <arpa/inet.h>
  142. #include <stdlib.h>
  143. #include <unistd.h>
  144. #include <resolv.h>
  145. #include <netdb.h>
  146. #include <ctype.h>
  147. #include <arpa/nameser.h>
  148. #include <sys/utsname.h>
  149. #include <sys/un.h>
  150. #define MAX_RECURSE 5
  151. #define REPLY_TIMEOUT 10
  152. #define MAX_RETRIES 3
  153. #define MAX_SERVERS 3
  154. #define MAX_SEARCH 4
  155. #define MAX_ALIASES 5
  156. /* 1:ip + 1:full + MAX_ALIASES:aliases + 1:NULL */
  157. #define ALIAS_DIM (2 + MAX_ALIASES + 1)
  158. #undef DEBUG
  159. /* #define DEBUG */
  160. #ifdef DEBUG
  161. #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
  162. #else
  163. #define DPRINTF(X,args...)
  164. #endif /* DEBUG */
  165. /* Global stuff (stuff needing to be locked to be thread safe)... */
  166. extern int __nameservers;
  167. extern char * __nameserver[MAX_SERVERS];
  168. extern int __searchdomains;
  169. extern char * __searchdomain[MAX_SEARCH];
  170. #ifdef __UCLIBC_HAS_THREADS__
  171. #include <pthread.h>
  172. extern pthread_mutex_t __resolv_lock;
  173. # define BIGLOCK __pthread_mutex_lock(&__resolv_lock)
  174. # define BIGUNLOCK __pthread_mutex_unlock(&__resolv_lock);
  175. #else
  176. # define BIGLOCK
  177. # define BIGUNLOCK
  178. #endif
  179. /* Structs */
  180. struct resolv_header {
  181. int id;
  182. int qr,opcode,aa,tc,rd,ra,rcode;
  183. int qdcount;
  184. int ancount;
  185. int nscount;
  186. int arcount;
  187. };
  188. struct resolv_question {
  189. char * dotted;
  190. int qtype;
  191. int qclass;
  192. };
  193. struct resolv_answer {
  194. char * dotted;
  195. int atype;
  196. int aclass;
  197. int ttl;
  198. int rdlength;
  199. unsigned char * rdata;
  200. int rdoffset;
  201. };
  202. enum etc_hosts_action {
  203. GET_HOSTS_BYNAME = 0,
  204. GETHOSTENT,
  205. GET_HOSTS_BYADDR,
  206. };
  207. /* function prototypes */
  208. extern int __get_hosts_byname_r(const char * name, int type,
  209. struct hostent * result_buf,
  210. char * buf, size_t buflen,
  211. struct hostent ** result,
  212. int * h_errnop);
  213. extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
  214. struct hostent * result_buf,
  215. char * buf, size_t buflen,
  216. struct hostent ** result,
  217. int * h_errnop);
  218. extern void __open_etc_hosts(FILE **fp);
  219. extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
  220. enum etc_hosts_action action,
  221. struct hostent * result_buf,
  222. char * buf, size_t buflen,
  223. struct hostent ** result,
  224. int * h_errnop);
  225. extern int __dns_lookup(const char * name, int type, int nscount,
  226. char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
  227. extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen);
  228. extern int __decode_dotted(const unsigned char * message, int offset,
  229. char * dest, int maxlen);
  230. extern int __length_dotted(const unsigned char * message, int offset);
  231. extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen);
  232. extern int __decode_header(unsigned char * data, struct resolv_header * h);
  233. extern int __encode_question(struct resolv_question * q,
  234. unsigned char * dest, int maxlen);
  235. extern int __decode_question(unsigned char * message, int offset,
  236. struct resolv_question * q);
  237. extern int __encode_answer(struct resolv_answer * a,
  238. unsigned char * dest, int maxlen);
  239. extern int __decode_answer(unsigned char * message, int offset,
  240. struct resolv_answer * a);
  241. extern int __length_question(unsigned char * message, int offset);
  242. extern int __open_nameservers(void);
  243. extern void __close_nameservers(void);
  244. extern int __dn_expand(const u_char *, const u_char *, const u_char *,
  245. char *, int);
  246. extern int __ns_name_uncompress(const u_char *, const u_char *,
  247. const u_char *, char *, size_t);
  248. extern int __ns_name_ntop(const u_char *, char *, size_t);
  249. extern int __ns_name_unpack(const u_char *, const u_char *, const u_char *,
  250. u_char *, size_t);
  251. #ifdef L_encodeh
  252. int __encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
  253. {
  254. if (maxlen < HFIXEDSZ)
  255. return -1;
  256. dest[0] = (h->id & 0xff00) >> 8;
  257. dest[1] = (h->id & 0x00ff) >> 0;
  258. dest[2] = (h->qr ? 0x80 : 0) |
  259. ((h->opcode & 0x0f) << 3) |
  260. (h->aa ? 0x04 : 0) |
  261. (h->tc ? 0x02 : 0) |
  262. (h->rd ? 0x01 : 0);
  263. dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
  264. dest[4] = (h->qdcount & 0xff00) >> 8;
  265. dest[5] = (h->qdcount & 0x00ff) >> 0;
  266. dest[6] = (h->ancount & 0xff00) >> 8;
  267. dest[7] = (h->ancount & 0x00ff) >> 0;
  268. dest[8] = (h->nscount & 0xff00) >> 8;
  269. dest[9] = (h->nscount & 0x00ff) >> 0;
  270. dest[10] = (h->arcount & 0xff00) >> 8;
  271. dest[11] = (h->arcount & 0x00ff) >> 0;
  272. return HFIXEDSZ;
  273. }
  274. #endif
  275. #ifdef L_decodeh
  276. int __decode_header(unsigned char *data, struct resolv_header *h)
  277. {
  278. h->id = (data[0] << 8) | data[1];
  279. h->qr = (data[2] & 0x80) ? 1 : 0;
  280. h->opcode = (data[2] >> 3) & 0x0f;
  281. h->aa = (data[2] & 0x04) ? 1 : 0;
  282. h->tc = (data[2] & 0x02) ? 1 : 0;
  283. h->rd = (data[2] & 0x01) ? 1 : 0;
  284. h->ra = (data[3] & 0x80) ? 1 : 0;
  285. h->rcode = data[3] & 0x0f;
  286. h->qdcount = (data[4] << 8) | data[5];
  287. h->ancount = (data[6] << 8) | data[7];
  288. h->nscount = (data[8] << 8) | data[9];
  289. h->arcount = (data[10] << 8) | data[11];
  290. return HFIXEDSZ;
  291. }
  292. #endif
  293. #ifdef L_encoded
  294. /* Encode a dotted string into nameserver transport-level encoding.
  295. This routine is fairly dumb, and doesn't attempt to compress
  296. the data */
  297. int __encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
  298. {
  299. int used = 0;
  300. while (dotted && *dotted) {
  301. char *c = strchr(dotted, '.');
  302. int l = c ? c - dotted : strlen(dotted);
  303. if (l >= (maxlen - used - 1))
  304. return -1;
  305. dest[used++] = l;
  306. memcpy(dest + used, dotted, l);
  307. used += l;
  308. if (c)
  309. dotted = c + 1;
  310. else
  311. break;
  312. }
  313. if (maxlen < 1)
  314. return -1;
  315. dest[used++] = 0;
  316. return used;
  317. }
  318. #endif
  319. #ifdef L_decoded
  320. /* Decode a dotted string from nameserver transport-level encoding.
  321. This routine understands compressed data. */
  322. int __decode_dotted(const unsigned char *data, int offset,
  323. char *dest, int maxlen)
  324. {
  325. int l;
  326. int measure = 1;
  327. int total = 0;
  328. int used = 0;
  329. if (!data)
  330. return -1;
  331. while ((l=data[offset++])) {
  332. if (measure)
  333. total++;
  334. if ((l & 0xc0) == (0xc0)) {
  335. if (measure)
  336. total++;
  337. /* compressed item, redirect */
  338. offset = ((l & 0x3f) << 8) | data[offset];
  339. measure = 0;
  340. continue;
  341. }
  342. if ((used + l + 1) >= maxlen)
  343. return -1;
  344. memcpy(dest + used, data + offset, l);
  345. offset += l;
  346. used += l;
  347. if (measure)
  348. total += l;
  349. if (data[offset] != 0)
  350. dest[used++] = '.';
  351. else
  352. dest[used++] = '\0';
  353. }
  354. /* The null byte must be counted too */
  355. if (measure) {
  356. total++;
  357. }
  358. DPRINTF("Total decode len = %d\n", total);
  359. return total;
  360. }
  361. #endif
  362. #ifdef L_lengthd
  363. int __length_dotted(const unsigned char *data, int offset)
  364. {
  365. int orig_offset = offset;
  366. int l;
  367. if (!data)
  368. return -1;
  369. while ((l = data[offset++])) {
  370. if ((l & 0xc0) == (0xc0)) {
  371. offset++;
  372. break;
  373. }
  374. offset += l;
  375. }
  376. return offset - orig_offset;
  377. }
  378. #endif
  379. #ifdef L_encodeq
  380. int __encode_question(struct resolv_question *q,
  381. unsigned char *dest, int maxlen)
  382. {
  383. int i;
  384. i = __encode_dotted(q->dotted, dest, maxlen);
  385. if (i < 0)
  386. return i;
  387. dest += i;
  388. maxlen -= i;
  389. if (maxlen < 4)
  390. return -1;
  391. dest[0] = (q->qtype & 0xff00) >> 8;
  392. dest[1] = (q->qtype & 0x00ff) >> 0;
  393. dest[2] = (q->qclass & 0xff00) >> 8;
  394. dest[3] = (q->qclass & 0x00ff) >> 0;
  395. return i + 4;
  396. }
  397. #endif
  398. #ifdef L_decodeq
  399. int __decode_question(unsigned char *message, int offset,
  400. struct resolv_question *q)
  401. {
  402. char temp[256];
  403. int i;
  404. i = __decode_dotted(message, offset, temp, sizeof(temp));
  405. if (i < 0)
  406. return i;
  407. offset += i;
  408. q->dotted = strdup(temp);
  409. q->qtype = (message[offset + 0] << 8) | message[offset + 1];
  410. q->qclass = (message[offset + 2] << 8) | message[offset + 3];
  411. return i + 4;
  412. }
  413. #endif
  414. #ifdef L_lengthq
  415. int __length_question(unsigned char *message, int offset)
  416. {
  417. int i;
  418. i = __length_dotted(message, offset);
  419. if (i < 0)
  420. return i;
  421. return i + 4;
  422. }
  423. #endif
  424. #ifdef L_encodea
  425. int __encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
  426. {
  427. int i;
  428. i = __encode_dotted(a->dotted, dest, maxlen);
  429. if (i < 0)
  430. return i;
  431. dest += i;
  432. maxlen -= i;
  433. if (maxlen < (RRFIXEDSZ+a->rdlength))
  434. return -1;
  435. *dest++ = (a->atype & 0xff00) >> 8;
  436. *dest++ = (a->atype & 0x00ff) >> 0;
  437. *dest++ = (a->aclass & 0xff00) >> 8;
  438. *dest++ = (a->aclass & 0x00ff) >> 0;
  439. *dest++ = (a->ttl & 0xff000000) >> 24;
  440. *dest++ = (a->ttl & 0x00ff0000) >> 16;
  441. *dest++ = (a->ttl & 0x0000ff00) >> 8;
  442. *dest++ = (a->ttl & 0x000000ff) >> 0;
  443. *dest++ = (a->rdlength & 0xff00) >> 8;
  444. *dest++ = (a->rdlength & 0x00ff) >> 0;
  445. memcpy(dest, a->rdata, a->rdlength);
  446. return i + RRFIXEDSZ + a->rdlength;
  447. }
  448. #endif
  449. #ifdef L_decodea
  450. int __decode_answer(unsigned char *message, int offset,
  451. struct resolv_answer *a)
  452. {
  453. char temp[256];
  454. int i;
  455. i = __decode_dotted(message, offset, temp, sizeof(temp));
  456. if (i < 0)
  457. return i;
  458. message += offset + i;
  459. a->dotted = strdup(temp);
  460. a->atype = (message[0] << 8) | message[1];
  461. message += 2;
  462. a->aclass = (message[0] << 8) | message[1];
  463. message += 2;
  464. a->ttl = (message[0] << 24) |
  465. (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
  466. message += 4;
  467. a->rdlength = (message[0] << 8) | message[1];
  468. message += 2;
  469. a->rdata = message;
  470. a->rdoffset = offset + i + RRFIXEDSZ;
  471. DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
  472. return i + RRFIXEDSZ + a->rdlength;
  473. }
  474. #endif
  475. #ifdef L_encodep
  476. int __encode_packet(struct resolv_header *h,
  477. struct resolv_question **q,
  478. struct resolv_answer **an,
  479. struct resolv_answer **ns,
  480. struct resolv_answer **ar,
  481. unsigned char *dest, int maxlen)
  482. {
  483. int i, total = 0;
  484. int j;
  485. i = __encode_header(h, dest, maxlen);
  486. if (i < 0)
  487. return i;
  488. dest += i;
  489. maxlen -= i;
  490. total += i;
  491. for (j = 0; j < h->qdcount; j++) {
  492. i = __encode_question(q[j], dest, maxlen);
  493. if (i < 0)
  494. return i;
  495. dest += i;
  496. maxlen -= i;
  497. total += i;
  498. }
  499. for (j = 0; j < h->ancount; j++) {
  500. i = __encode_answer(an[j], dest, maxlen);
  501. if (i < 0)
  502. return i;
  503. dest += i;
  504. maxlen -= i;
  505. total += i;
  506. }
  507. for (j = 0; j < h->nscount; j++) {
  508. i = __encode_answer(ns[j], dest, maxlen);
  509. if (i < 0)
  510. return i;
  511. dest += i;
  512. maxlen -= i;
  513. total += i;
  514. }
  515. for (j = 0; j < h->arcount; j++) {
  516. i = __encode_answer(ar[j], dest, maxlen);
  517. if (i < 0)
  518. return i;
  519. dest += i;
  520. maxlen -= i;
  521. total += i;
  522. }
  523. return total;
  524. }
  525. #endif
  526. #ifdef L_decodep
  527. int __decode_packet(unsigned char *data, struct resolv_header *h)
  528. {
  529. return __decode_header(data, h);
  530. }
  531. #endif
  532. #ifdef L_formquery
  533. int __form_query(int id, const char *name, int type, unsigned char *packet,
  534. int maxlen)
  535. {
  536. struct resolv_header h;
  537. struct resolv_question q;
  538. int i, j;
  539. memset(&h, 0, sizeof(h));
  540. h.id = id;
  541. h.qdcount = 1;
  542. q.dotted = (char *) name;
  543. q.qtype = type;
  544. q.qclass = C_IN; /* CLASS_IN */
  545. i = __encode_header(&h, packet, maxlen);
  546. if (i < 0)
  547. return i;
  548. j = __encode_question(&q, packet + i, maxlen - i);
  549. if (j < 0)
  550. return j;
  551. return i + j;
  552. }
  553. #endif
  554. #ifdef L_dnslookup
  555. #ifdef __UCLIBC_HAS_THREADS__
  556. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  557. # define LOCK __pthread_mutex_lock(&mylock)
  558. # define UNLOCK __pthread_mutex_unlock(&mylock);
  559. #else
  560. # define LOCK
  561. # define UNLOCK
  562. #endif
  563. /* Just for the record, having to lock __dns_lookup() just for these two globals
  564. * is pretty lame. I think these two variables can probably be de-global-ized,
  565. * which should eliminate the need for doing locking here... Needs a closer
  566. * look anyways. */
  567. static int ns=0, id=1;
  568. int __dns_lookup(const char *name, int type, int nscount, char **nsip,
  569. unsigned char **outpacket, struct resolv_answer *a)
  570. {
  571. int i, j, len, fd, pos, rc;
  572. struct timeval tv;
  573. fd_set fds;
  574. struct resolv_header h;
  575. struct resolv_question q;
  576. int retries = 0;
  577. unsigned char * packet = malloc(PACKETSZ);
  578. char *dns, *lookup = malloc(MAXDNAME);
  579. int variant = 0;
  580. struct sockaddr_in sa;
  581. #ifdef __UCLIBC_HAS_IPV6__
  582. int v6;
  583. struct sockaddr_in6 sa6;
  584. #endif
  585. fd = -1;
  586. if (!packet || !lookup || !nscount)
  587. goto fail;
  588. DPRINTF("Looking up type %d answer for '%s'\n", type, name);
  589. LOCK;
  590. ns %= nscount;
  591. UNLOCK;
  592. while (retries < MAX_RETRIES) {
  593. if (fd != -1)
  594. close(fd);
  595. memset(packet, 0, PACKETSZ);
  596. memset(&h, 0, sizeof(h));
  597. /* Mess with globals while under lock */
  598. LOCK;
  599. ++id;
  600. id &= 0xffff;
  601. h.id = id;
  602. dns = nsip[ns];
  603. UNLOCK;
  604. h.qdcount = 1;
  605. h.rd = 1;
  606. DPRINTF("encoding header\n", h.rd);
  607. i = __encode_header(&h, packet, PACKETSZ);
  608. if (i < 0)
  609. goto fail;
  610. strncpy(lookup,name,MAXDNAME);
  611. BIGLOCK;
  612. if (variant < __searchdomains && strchr(lookup, '.') == NULL)
  613. {
  614. strncat(lookup,".", MAXDNAME);
  615. strncat(lookup,__searchdomain[variant], MAXDNAME);
  616. }
  617. BIGUNLOCK;
  618. DPRINTF("lookup name: %s\n", lookup);
  619. q.dotted = (char *)lookup;
  620. q.qtype = type;
  621. q.qclass = C_IN; /* CLASS_IN */
  622. j = __encode_question(&q, packet+i, PACKETSZ-i);
  623. if (j < 0)
  624. goto fail;
  625. len = i + j;
  626. DPRINTF("On try %d, sending query to port %d of machine %s\n",
  627. retries+1, NAMESERVER_PORT, dns);
  628. #ifdef __UCLIBC_HAS_IPV6__
  629. v6 = inet_pton(AF_INET6, dns, &sa6.sin6_addr) > 0;
  630. fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  631. #else
  632. fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  633. #endif
  634. if (fd < 0) {
  635. retries++;
  636. continue;
  637. }
  638. /* Connect to the UDP socket so that asyncronous errors are returned */
  639. #ifdef __UCLIBC_HAS_IPV6__
  640. if (v6) {
  641. sa6.sin6_family = AF_INET6;
  642. sa6.sin6_port = htons(NAMESERVER_PORT);
  643. /* sa6.sin6_addr is already here */
  644. rc = connect(fd, (struct sockaddr *) &sa6, sizeof(sa6));
  645. } else {
  646. #endif
  647. sa.sin_family = AF_INET;
  648. sa.sin_port = htons(NAMESERVER_PORT);
  649. sa.sin_addr.s_addr = inet_addr(dns);
  650. rc = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
  651. #ifdef __UCLIBC_HAS_IPV6__
  652. }
  653. #endif
  654. if (rc < 0) {
  655. if (errno == ENETUNREACH) {
  656. /* routing error, presume not transient */
  657. goto tryall;
  658. } else
  659. /* retry */
  660. retries++;
  661. continue;
  662. }
  663. DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
  664. len, h.id, h.qr);
  665. send(fd, packet, len, 0);
  666. FD_ZERO(&fds);
  667. FD_SET(fd, &fds);
  668. tv.tv_sec = REPLY_TIMEOUT;
  669. tv.tv_usec = 0;
  670. if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
  671. DPRINTF("Timeout\n");
  672. /* timed out, so retry send and receive,
  673. * to next nameserver on queue */
  674. goto tryall;
  675. }
  676. len = recv(fd, packet, 512, 0);
  677. if (len < HFIXEDSZ) {
  678. /* too short ! */
  679. goto again;
  680. }
  681. __decode_header(packet, &h);
  682. DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
  683. LOCK;
  684. if ((h.id != id) || (!h.qr)) {
  685. UNLOCK;
  686. /* unsolicited */
  687. goto again;
  688. }
  689. UNLOCK;
  690. DPRINTF("Got response %s\n", "(i think)!");
  691. DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
  692. h.qdcount, h.ancount, h.nscount, h.arcount);
  693. DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
  694. h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
  695. if ((h.rcode) || (h.ancount < 1)) {
  696. /* negative result, not present */
  697. goto again;
  698. }
  699. pos = HFIXEDSZ;
  700. for (j = 0; j < h.qdcount; j++) {
  701. DPRINTF("Skipping question %d at %d\n", j, pos);
  702. i = __length_question(packet, pos);
  703. DPRINTF("Length of question %d is %d\n", j, i);
  704. if (i < 0)
  705. goto again;
  706. pos += i;
  707. }
  708. DPRINTF("Decoding answer at pos %d\n", pos);
  709. for (j=0;j<h.ancount;j++)
  710. {
  711. i = __decode_answer(packet, pos, a);
  712. if (i<0) {
  713. DPRINTF("failed decode %d\n", i);
  714. goto again;
  715. }
  716. /* For all but T_SIG, accept first answer */
  717. if (a->atype != T_SIG)
  718. break;
  719. DPRINTF("skipping T_SIG %d\n", i);
  720. free(a->dotted);
  721. pos += i;
  722. }
  723. DPRINTF("Answer name = |%s|\n", a->dotted);
  724. DPRINTF("Answer type = |%d|\n", a->atype);
  725. close(fd);
  726. if (outpacket)
  727. *outpacket = packet;
  728. else
  729. free(packet);
  730. free(lookup);
  731. return (len); /* success! */
  732. tryall:
  733. /* if there are other nameservers, give them a go,
  734. otherwise return with error */
  735. {
  736. variant = 0;
  737. LOCK;
  738. ns = (ns + 1) % nscount;
  739. if (ns == 0)
  740. retries++;
  741. UNLOCK;
  742. continue;
  743. }
  744. again:
  745. /* if there are searchdomains, try them or fallback as passed */
  746. {
  747. int sdomains;
  748. BIGLOCK;
  749. sdomains=__searchdomains;
  750. BIGUNLOCK;
  751. if (variant < ((sdomains - 1) && strchr(lookup, '.') == NULL)) {
  752. /* next search */
  753. variant++;
  754. } else {
  755. /* next server, first search */
  756. LOCK;
  757. ns = (ns + 1) % nscount;
  758. if (ns == 0)
  759. retries++;
  760. UNLOCK;
  761. variant = 0;
  762. }
  763. }
  764. }
  765. fail:
  766. if (fd != -1)
  767. close(fd);
  768. if (lookup)
  769. free(lookup);
  770. if (packet)
  771. free(packet);
  772. h_errno = NETDB_INTERNAL;
  773. return -1;
  774. }
  775. #endif
  776. #ifdef L_opennameservers
  777. int __nameservers;
  778. char * __nameserver[MAX_SERVERS];
  779. int __searchdomains;
  780. char * __searchdomain[MAX_SEARCH];
  781. #ifdef __UCLIBC_HAS_THREADS__
  782. pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER;
  783. #endif
  784. /*
  785. * we currently read formats not quite the same as that on normal
  786. * unix systems, we can have a list of nameservers after the keyword.
  787. */
  788. int __open_nameservers()
  789. {
  790. FILE *fp;
  791. int i;
  792. #define RESOLV_ARGS 5
  793. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  794. int argc;
  795. BIGLOCK;
  796. if (__nameservers > 0) {
  797. BIGUNLOCK;
  798. return 0;
  799. }
  800. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  801. (fp = fopen("/etc/config/resolv.conf", "r")))
  802. {
  803. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  804. for (p = szBuffer; *p && isspace(*p); p++)
  805. /* skip white space */;
  806. if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  807. continue;
  808. argc = 0;
  809. while (*p && argc < RESOLV_ARGS) {
  810. argv[argc++] = p;
  811. while (*p && !isspace(*p) && *p != '\n')
  812. p++;
  813. while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  814. *p++ = '\0';
  815. }
  816. if (strcmp(argv[0], "nameserver") == 0) {
  817. for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
  818. __nameserver[__nameservers++] = strdup(argv[i]);
  819. DPRINTF("adding nameserver %s\n", argv[i]);
  820. }
  821. }
  822. /* domain and search are mutually exclusive, the last one wins */
  823. if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  824. while (__searchdomains > 0) {
  825. free(__searchdomain[--__searchdomains]);
  826. __searchdomain[__searchdomains] = NULL;
  827. }
  828. for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
  829. __searchdomain[__searchdomains++] = strdup(argv[i]);
  830. DPRINTF("adding search %s\n", argv[i]);
  831. }
  832. }
  833. }
  834. fclose(fp);
  835. DPRINTF("nameservers = %d\n", __nameservers);
  836. BIGUNLOCK;
  837. return 0;
  838. }
  839. DPRINTF("failed to open %s\n", "resolv.conf");
  840. h_errno = NO_RECOVERY;
  841. BIGUNLOCK;
  842. return -1;
  843. }
  844. #endif
  845. #ifdef L_closenameservers
  846. void __close_nameservers(void)
  847. {
  848. BIGLOCK;
  849. while (__nameservers > 0) {
  850. free(__nameserver[--__nameservers]);
  851. __nameserver[__nameservers] = NULL;
  852. }
  853. while (__searchdomains > 0) {
  854. free(__searchdomain[--__searchdomains]);
  855. __searchdomain[__searchdomains] = NULL;
  856. }
  857. BIGUNLOCK;
  858. }
  859. #endif
  860. #ifdef L_gethostbyname
  861. struct hostent *gethostbyname(const char *name)
  862. {
  863. static struct hostent h;
  864. static char buf[sizeof(struct in_addr) +
  865. sizeof(struct in_addr *)*2 +
  866. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  867. struct hostent *hp;
  868. gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
  869. return hp;
  870. }
  871. #endif
  872. #ifdef L_gethostbyname2
  873. struct hostent *gethostbyname2(const char *name, int family)
  874. {
  875. #ifndef __UCLIBC_HAS_IPV6__
  876. return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
  877. #else /* __UCLIBC_HAS_IPV6__ */
  878. static struct hostent h;
  879. static char buf[sizeof(struct in6_addr) +
  880. sizeof(struct in6_addr *)*2 +
  881. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  882. struct hostent *hp;
  883. gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
  884. return hp;
  885. #endif /* __UCLIBC_HAS_IPV6__ */
  886. }
  887. #endif
  888. #ifdef L_res_init
  889. struct __res_state _res;
  890. int res_init(void)
  891. {
  892. struct __res_state *rp = &(_res);
  893. __close_nameservers();
  894. __open_nameservers();
  895. rp->retrans = RES_TIMEOUT;
  896. rp->retry = 4;
  897. rp->options = RES_INIT;
  898. rp->id = (u_int) random();
  899. rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
  900. rp->nsaddr.sin_family = AF_INET;
  901. rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
  902. rp->ndots = 1;
  903. /** rp->pfcode = 0; **/
  904. rp->_vcsock = -1;
  905. /** rp->_flags = 0; **/
  906. /** rp->qhook = NULL; **/
  907. /** rp->rhook = NULL; **/
  908. /** rp->_u._ext.nsinit = 0; **/
  909. BIGLOCK;
  910. if(__searchdomains) {
  911. int i;
  912. for(i=0; i<__searchdomains; i++) {
  913. rp->dnsrch[i] = __searchdomain[i];
  914. }
  915. }
  916. if(__nameservers) {
  917. int i;
  918. struct in_addr a;
  919. for(i=0; i<__nameservers; i++) {
  920. if (inet_aton(__nameserver[i], &a)) {
  921. rp->nsaddr_list[i].sin_addr = a;
  922. rp->nsaddr_list[i].sin_family = AF_INET;
  923. rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
  924. }
  925. }
  926. }
  927. rp->nscount = __nameservers;
  928. BIGUNLOCK;
  929. return(0);
  930. }
  931. void res_close( void )
  932. {
  933. return;
  934. }
  935. #endif
  936. #ifdef L_res_query
  937. #ifndef MIN
  938. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  939. #endif
  940. int res_query(const char *dname, int class, int type,
  941. unsigned char *answer, int anslen)
  942. {
  943. int i;
  944. unsigned char * packet = 0;
  945. struct resolv_answer a;
  946. int __nameserversXX;
  947. char ** __nameserverXX;
  948. __open_nameservers();
  949. if (!dname || class != 1 /* CLASS_IN */) {
  950. h_errno = NO_RECOVERY;
  951. return(-1);
  952. }
  953. memset((char *) &a, '\0', sizeof(a));
  954. BIGLOCK;
  955. __nameserversXX=__nameservers;
  956. __nameserverXX=__nameserver;
  957. BIGUNLOCK;
  958. i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
  959. if (i < 0) {
  960. h_errno = TRY_AGAIN;
  961. return(-1);
  962. }
  963. free(a.dotted);
  964. if (a.atype == type) { /* CNAME*/
  965. int len = MIN(anslen, i);
  966. memcpy(answer, packet, len);
  967. if (packet)
  968. free(packet);
  969. return(len);
  970. }
  971. if (packet)
  972. free(packet);
  973. return i;
  974. }
  975. /*
  976. * Formulate a normal query, send, and retrieve answer in supplied buffer.
  977. * Return the size of the response on success, -1 on error.
  978. * If enabled, implement search rules until answer or unrecoverable failure
  979. * is detected. Error code, if any, is left in h_errno.
  980. */
  981. int res_search(name, class, type, answer, anslen)
  982. const char *name; /* domain name */
  983. int class, type; /* class and type of query */
  984. u_char *answer; /* buffer to put answer */
  985. int anslen; /* size of answer */
  986. {
  987. const char *cp, * const *domain;
  988. HEADER *hp = (HEADER *)(void *)answer;
  989. u_int dots;
  990. int trailing_dot, ret, saved_herrno;
  991. int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
  992. if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) {
  993. h_errno = NETDB_INTERNAL;
  994. return (-1);
  995. }
  996. errno = 0;
  997. h_errno = HOST_NOT_FOUND; /* default, if we never query */
  998. dots = 0;
  999. for (cp = name; *cp; cp++)
  1000. dots += (*cp == '.');
  1001. trailing_dot = 0;
  1002. if (cp > name && *--cp == '.')
  1003. trailing_dot++;
  1004. /*
  1005. * If there are dots in the name already, let's just give it a try
  1006. * 'as is'. The threshold can be set with the "ndots" option.
  1007. */
  1008. saved_herrno = -1;
  1009. if (dots >= _res.ndots) {
  1010. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1011. if (ret > 0)
  1012. return (ret);
  1013. saved_herrno = h_errno;
  1014. tried_as_is++;
  1015. }
  1016. /*
  1017. * We do at least one level of search if
  1018. * - there is no dot and RES_DEFNAME is set, or
  1019. * - there is at least one dot, there is no trailing dot,
  1020. * and RES_DNSRCH is set.
  1021. */
  1022. if ((!dots && (_res.options & RES_DEFNAMES)) ||
  1023. (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
  1024. int done = 0;
  1025. for (domain = (const char * const *)_res.dnsrch;
  1026. *domain && !done;
  1027. domain++) {
  1028. ret = res_querydomain(name, *domain, class, type,
  1029. answer, anslen);
  1030. if (ret > 0)
  1031. return (ret);
  1032. /*
  1033. * If no server present, give up.
  1034. * If name isn't found in this domain,
  1035. * keep trying higher domains in the search list
  1036. * (if that's enabled).
  1037. * On a NO_DATA error, keep trying, otherwise
  1038. * a wildcard entry of another type could keep us
  1039. * from finding this entry higher in the domain.
  1040. * If we get some other error (negative answer or
  1041. * server failure), then stop searching up,
  1042. * but try the input name below in case it's
  1043. * fully-qualified.
  1044. */
  1045. if (errno == ECONNREFUSED) {
  1046. h_errno = TRY_AGAIN;
  1047. return (-1);
  1048. }
  1049. switch (h_errno) {
  1050. case NO_DATA:
  1051. got_nodata++;
  1052. /* FALLTHROUGH */
  1053. case HOST_NOT_FOUND:
  1054. /* keep trying */
  1055. break;
  1056. case TRY_AGAIN:
  1057. if (hp->rcode == SERVFAIL) {
  1058. /* try next search element, if any */
  1059. got_servfail++;
  1060. break;
  1061. }
  1062. /* FALLTHROUGH */
  1063. default:
  1064. /* anything else implies that we're done */
  1065. done++;
  1066. }
  1067. /*
  1068. * if we got here for some reason other than DNSRCH,
  1069. * we only wanted one iteration of the loop, so stop.
  1070. */
  1071. if (!(_res.options & RES_DNSRCH))
  1072. done++;
  1073. }
  1074. }
  1075. /*
  1076. * if we have not already tried the name "as is", do that now.
  1077. * note that we do this regardless of how many dots were in the
  1078. * name or whether it ends with a dot.
  1079. */
  1080. if (!tried_as_is) {
  1081. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1082. if (ret > 0)
  1083. return (ret);
  1084. }
  1085. /*
  1086. * if we got here, we didn't satisfy the search.
  1087. * if we did an initial full query, return that query's h_errno
  1088. * (note that we wouldn't be here if that query had succeeded).
  1089. * else if we ever got a nodata, send that back as the reason.
  1090. * else send back meaningless h_errno, that being the one from
  1091. * the last DNSRCH we did.
  1092. */
  1093. if (saved_herrno != -1)
  1094. h_errno = saved_herrno;
  1095. else if (got_nodata)
  1096. h_errno = NO_DATA;
  1097. else if (got_servfail)
  1098. h_errno = TRY_AGAIN;
  1099. return (-1);
  1100. }
  1101. /*
  1102. * Perform a call on res_query on the concatenation of name and domain,
  1103. * removing a trailing dot from name if domain is NULL.
  1104. */
  1105. int res_querydomain(name, domain, class, type, answer, anslen)
  1106. const char *name, *domain;
  1107. int class, type; /* class and type of query */
  1108. u_char *answer; /* buffer to put answer */
  1109. int anslen; /* size of answer */
  1110. {
  1111. char nbuf[MAXDNAME];
  1112. const char *longname = nbuf;
  1113. size_t n, d;
  1114. if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) {
  1115. h_errno = NETDB_INTERNAL;
  1116. return (-1);
  1117. }
  1118. #ifdef DEBUG
  1119. if (_res.options & RES_DEBUG)
  1120. printf(";; res_querydomain(%s, %s, %d, %d)\n",
  1121. name, domain?domain:"<Nil>", class, type);
  1122. #endif
  1123. if (domain == NULL) {
  1124. /*
  1125. * Check for trailing '.';
  1126. * copy without '.' if present.
  1127. */
  1128. n = strlen(name);
  1129. if (n + 1 > sizeof(nbuf)) {
  1130. h_errno = NO_RECOVERY;
  1131. return (-1);
  1132. }
  1133. if (n > 0 && name[--n] == '.') {
  1134. strncpy(nbuf, name, n);
  1135. nbuf[n] = '\0';
  1136. } else
  1137. longname = name;
  1138. } else {
  1139. n = strlen(name);
  1140. d = strlen(domain);
  1141. if (n + 1 + d + 1 > sizeof(nbuf)) {
  1142. h_errno = NO_RECOVERY;
  1143. return (-1);
  1144. }
  1145. snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
  1146. }
  1147. return (res_query(longname, class, type, answer, anslen));
  1148. }
  1149. /* res_mkquery */
  1150. /* res_send */
  1151. /* dn_comp */
  1152. /* dn_expand */
  1153. #endif
  1154. #ifdef L_gethostbyaddr
  1155. struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
  1156. {
  1157. static struct hostent h;
  1158. static char buf[
  1159. #ifndef __UCLIBC_HAS_IPV6__
  1160. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1161. #else
  1162. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1163. #endif /* __UCLIBC_HAS_IPV6__ */
  1164. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  1165. struct hostent *hp;
  1166. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  1167. return hp;
  1168. }
  1169. #endif
  1170. #ifdef L_read_etc_hosts_r
  1171. void __open_etc_hosts(FILE **fp)
  1172. {
  1173. if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
  1174. *fp = fopen("/etc/config/hosts", "r");
  1175. }
  1176. return;
  1177. }
  1178. int __read_etc_hosts_r(FILE * fp, const char * name, int type,
  1179. enum etc_hosts_action action,
  1180. struct hostent * result_buf,
  1181. char * buf, size_t buflen,
  1182. struct hostent ** result,
  1183. int * h_errnop)
  1184. {
  1185. struct in_addr *in=NULL;
  1186. struct in_addr **addr_list=NULL;
  1187. #ifdef __UCLIBC_HAS_IPV6__
  1188. struct in6_addr *in6=NULL;
  1189. struct in6_addr **addr_list6=NULL;
  1190. #endif /* __UCLIBC_HAS_IPV6__ */
  1191. char *cp;
  1192. char **alias;
  1193. int aliases, i;
  1194. int ret=HOST_NOT_FOUND;
  1195. if (buflen < sizeof(char *)*(ALIAS_DIM))
  1196. return ERANGE;
  1197. alias=(char **)buf;
  1198. buf+=sizeof(char **)*(ALIAS_DIM);
  1199. buflen-=sizeof(char **)*(ALIAS_DIM);
  1200. if (action!=GETHOSTENT) {
  1201. #ifdef __UCLIBC_HAS_IPV6__
  1202. char *p=buf;
  1203. size_t len=buflen;
  1204. #endif /* __UCLIBC_HAS_IPV6__ */
  1205. *h_errnop=NETDB_INTERNAL;
  1206. if (buflen < sizeof(*in))
  1207. return ERANGE;
  1208. in=(struct in_addr*)buf;
  1209. buf+=sizeof(*in);
  1210. buflen-=sizeof(*in);
  1211. if (buflen < sizeof(*addr_list)*2)
  1212. return ERANGE;
  1213. addr_list=(struct in_addr **)buf;
  1214. buf+=sizeof(*addr_list)*2;
  1215. buflen-=sizeof(*addr_list)*2;
  1216. #ifdef __UCLIBC_HAS_IPV6__
  1217. if (len < sizeof(*in6))
  1218. return ERANGE;
  1219. in6=(struct in6_addr*)p;
  1220. p+=sizeof(*in6);
  1221. len-=sizeof(*in6);
  1222. if (len < sizeof(*addr_list6)*2)
  1223. return ERANGE;
  1224. addr_list6=(struct in6_addr**)p;
  1225. p+=sizeof(*addr_list6)*2;
  1226. len-=sizeof(*addr_list6)*2;
  1227. if (len < buflen) {
  1228. buflen=len;
  1229. buf=p;
  1230. }
  1231. #endif /* __UCLIBC_HAS_IPV6__ */
  1232. if (buflen < 80)
  1233. return ERANGE;
  1234. __open_etc_hosts(&fp);
  1235. if (fp == NULL) {
  1236. result=NULL;
  1237. return errno;
  1238. }
  1239. }
  1240. *h_errnop=HOST_NOT_FOUND;
  1241. while (fgets(buf, buflen, fp)) {
  1242. if ((cp = strchr(buf, '#')))
  1243. *cp = '\0';
  1244. DPRINTF("Looking at: %s\n", buf);
  1245. aliases = 0;
  1246. cp = buf;
  1247. while (*cp) {
  1248. while (*cp && isspace(*cp))
  1249. *cp++ = '\0';
  1250. if (!*cp)
  1251. continue;
  1252. if (aliases < (2+MAX_ALIASES))
  1253. alias[aliases++] = cp;
  1254. while (*cp && !isspace(*cp))
  1255. cp++;
  1256. }
  1257. alias[aliases] = 0;
  1258. if (aliases < 2)
  1259. continue; /* syntax error really */
  1260. if (action==GETHOSTENT) {
  1261. /* Return whatever the next entry happens to be. */
  1262. break;
  1263. } else if (action==GET_HOSTS_BYADDR) {
  1264. if (strcmp(name, alias[0]) != 0)
  1265. continue;
  1266. } else {
  1267. /* GET_HOSTS_BYNAME */
  1268. for (i = 1; i < aliases; i++)
  1269. if (strcasecmp(name, alias[i]) == 0)
  1270. break;
  1271. if (i >= aliases)
  1272. continue;
  1273. }
  1274. if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
  1275. DPRINTF("Found INET\n");
  1276. addr_list[0] = in;
  1277. addr_list[1] = 0;
  1278. result_buf->h_name = alias[1];
  1279. result_buf->h_addrtype = AF_INET;
  1280. result_buf->h_length = sizeof(*in);
  1281. result_buf->h_addr_list = (char**) addr_list;
  1282. result_buf->h_aliases = alias + 2;
  1283. *result=result_buf;
  1284. ret=NETDB_SUCCESS;
  1285. #ifdef __UCLIBC_HAS_IPV6__
  1286. } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
  1287. DPRINTF("Found INET6\n");
  1288. addr_list6[0] = in6;
  1289. addr_list6[1] = 0;
  1290. result_buf->h_name = alias[1];
  1291. result_buf->h_addrtype = AF_INET6;
  1292. result_buf->h_length = sizeof(*in6);
  1293. result_buf->h_addr_list = (char**) addr_list6;
  1294. result_buf->h_aliases = alias + 2;
  1295. *result=result_buf;
  1296. ret=NETDB_SUCCESS;
  1297. #endif /* __UCLIBC_HAS_IPV6__ */
  1298. } else {
  1299. DPRINTF("Error\n");
  1300. ret=TRY_AGAIN;
  1301. break; /* bad ip address */
  1302. }
  1303. if (action!=GETHOSTENT) {
  1304. fclose(fp);
  1305. }
  1306. return ret;
  1307. }
  1308. if (action!=GETHOSTENT) {
  1309. fclose(fp);
  1310. }
  1311. return ret;
  1312. }
  1313. #endif
  1314. #ifdef L_gethostent
  1315. #ifdef __UCLIBC_HAS_THREADS__
  1316. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  1317. # define LOCK __pthread_mutex_lock(&mylock)
  1318. # define UNLOCK __pthread_mutex_unlock(&mylock);
  1319. #else
  1320. # define LOCK
  1321. # define UNLOCK
  1322. #endif
  1323. static int __stay_open;
  1324. static FILE * __gethostent_fp;
  1325. void endhostent (void)
  1326. {
  1327. LOCK;
  1328. __stay_open = 0;
  1329. if (__gethostent_fp) {
  1330. fclose(__gethostent_fp);
  1331. }
  1332. UNLOCK;
  1333. }
  1334. void sethostent (int stay_open)
  1335. {
  1336. LOCK;
  1337. __stay_open = stay_open;
  1338. UNLOCK;
  1339. }
  1340. int gethostent_r(struct hostent *result_buf, char *buf, size_t buflen,
  1341. struct hostent **result, int *h_errnop)
  1342. {
  1343. int ret;
  1344. LOCK;
  1345. if (__gethostent_fp == NULL) {
  1346. __open_etc_hosts(&__gethostent_fp);
  1347. if (__gethostent_fp == NULL) {
  1348. UNLOCK;
  1349. *result=NULL;
  1350. return 0;
  1351. }
  1352. }
  1353. ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  1354. result_buf, buf, buflen, result, h_errnop);
  1355. if (__stay_open==0) {
  1356. fclose(__gethostent_fp);
  1357. }
  1358. UNLOCK;
  1359. return(ret);
  1360. }
  1361. struct hostent *gethostent (void)
  1362. {
  1363. static struct hostent h;
  1364. static char buf[
  1365. #ifndef __UCLIBC_HAS_IPV6__
  1366. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1367. #else
  1368. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1369. #endif /* __UCLIBC_HAS_IPV6__ */
  1370. sizeof(char *)*(ALIAS_DIM) +
  1371. 80/*namebuffer*/ + 2/* margin */];
  1372. struct hostent *host;
  1373. LOCK;
  1374. gethostent_r(&h, buf, sizeof(buf), &host, &h_errno);
  1375. UNLOCK;
  1376. return(host);
  1377. }
  1378. #endif
  1379. #ifdef L_get_hosts_byname_r
  1380. int __get_hosts_byname_r(const char * name, int type,
  1381. struct hostent * result_buf,
  1382. char * buf, size_t buflen,
  1383. struct hostent ** result,
  1384. int * h_errnop)
  1385. {
  1386. return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, result_buf, buf, buflen, result, h_errnop));
  1387. }
  1388. #endif
  1389. #ifdef L_get_hosts_byaddr_r
  1390. int __get_hosts_byaddr_r(const char * addr, int len, int type,
  1391. struct hostent * result_buf,
  1392. char * buf, size_t buflen,
  1393. struct hostent ** result,
  1394. int * h_errnop)
  1395. {
  1396. #ifndef __UCLIBC_HAS_IPV6__
  1397. char ipaddr[INET_ADDRSTRLEN];
  1398. #else
  1399. char ipaddr[INET6_ADDRSTRLEN];
  1400. #endif /* __UCLIBC_HAS_IPV6__ */
  1401. switch (type) {
  1402. case AF_INET:
  1403. if (len != sizeof(struct in_addr))
  1404. return 0;
  1405. break;
  1406. #ifdef __UCLIBC_HAS_IPV6__
  1407. case AF_INET6:
  1408. if (len != sizeof(struct in6_addr))
  1409. return 0;
  1410. break;
  1411. #endif /* __UCLIBC_HAS_IPV6__ */
  1412. default:
  1413. return 0;
  1414. }
  1415. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1416. return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1417. result_buf, buf, buflen, result, h_errnop));
  1418. }
  1419. #endif
  1420. #ifdef L_getnameinfo
  1421. #ifndef min
  1422. # define min(x,y) (((x) > (y)) ? (y) : (x))
  1423. #endif /* min */
  1424. int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  1425. socklen_t hostlen, char *serv, socklen_t servlen,
  1426. unsigned int flags)
  1427. {
  1428. int serrno = errno;
  1429. int ok = 0;
  1430. struct hostent *h = NULL;
  1431. char domain[256];
  1432. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1433. return EAI_BADFLAGS;
  1434. if (sa == NULL || addrlen < sizeof (sa_family_t))
  1435. return EAI_FAMILY;
  1436. switch (sa->sa_family) {
  1437. case AF_LOCAL:
  1438. break;
  1439. case AF_INET:
  1440. if (addrlen < sizeof (struct sockaddr_in))
  1441. return EAI_FAMILY;
  1442. break;
  1443. #ifdef __UCLIBC_HAS_IPV6__
  1444. case AF_INET6:
  1445. if (addrlen < sizeof (struct sockaddr_in6))
  1446. return EAI_FAMILY;
  1447. break;
  1448. #endif /* __UCLIBC_HAS_IPV6__ */
  1449. default:
  1450. return EAI_FAMILY;
  1451. }
  1452. if (host != NULL && hostlen > 0)
  1453. switch (sa->sa_family) {
  1454. case AF_INET:
  1455. #ifdef __UCLIBC_HAS_IPV6__
  1456. case AF_INET6:
  1457. #endif /* __UCLIBC_HAS_IPV6__ */
  1458. if (!(flags & NI_NUMERICHOST)) {
  1459. #ifdef __UCLIBC_HAS_IPV6__
  1460. if (sa->sa_family == AF_INET6)
  1461. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1462. sizeof(struct in6_addr), AF_INET6);
  1463. else
  1464. #endif /* __UCLIBC_HAS_IPV6__ */
  1465. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1466. sizeof(struct in_addr), AF_INET);
  1467. if (h) {
  1468. char *c;
  1469. if ((flags & NI_NOFQDN)
  1470. && (getdomainname (domain, sizeof(domain)) == 0)
  1471. && (c = strstr (h->h_name, domain))
  1472. && (c != h->h_name) && (*(--c) == '.')) {
  1473. strncpy (host, h->h_name,
  1474. min(hostlen, (size_t) (c - h->h_name)));
  1475. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1476. ok = 1;
  1477. } else {
  1478. strncpy (host, h->h_name, hostlen);
  1479. ok = 1;
  1480. }
  1481. }
  1482. }
  1483. if (!ok) {
  1484. if (flags & NI_NAMEREQD) {
  1485. errno = serrno;
  1486. return EAI_NONAME;
  1487. } else {
  1488. const char *c;
  1489. #ifdef __UCLIBC_HAS_IPV6__
  1490. if (sa->sa_family == AF_INET6) {
  1491. const struct sockaddr_in6 *sin6p;
  1492. sin6p = (const struct sockaddr_in6 *) sa;
  1493. c = inet_ntop (AF_INET6,
  1494. (const void *) &sin6p->sin6_addr, host, hostlen);
  1495. #if 0
  1496. /* Does scope id need to be supported? */
  1497. uint32_t scopeid;
  1498. scopeid = sin6p->sin6_scope_id;
  1499. if (scopeid != 0) {
  1500. /* Buffer is >= IFNAMSIZ+1. */
  1501. char scopebuf[IFNAMSIZ + 1];
  1502. char *scopeptr;
  1503. int ni_numericscope = 0;
  1504. size_t real_hostlen = __strnlen (host, hostlen);
  1505. size_t scopelen = 0;
  1506. scopebuf[0] = SCOPE_DELIMITER;
  1507. scopebuf[1] = '\0';
  1508. scopeptr = &scopebuf[1];
  1509. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1510. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1511. if (if_indextoname (scopeid, scopeptr) == NULL)
  1512. ++ni_numericscope;
  1513. else
  1514. scopelen = strlen (scopebuf);
  1515. } else {
  1516. ++ni_numericscope;
  1517. }
  1518. if (ni_numericscope)
  1519. scopelen = 1 + snprintf (scopeptr,
  1520. (scopebuf
  1521. + sizeof scopebuf
  1522. - scopeptr),
  1523. "%u", scopeid);
  1524. if (real_hostlen + scopelen + 1 > hostlen)
  1525. return EAI_SYSTEM;
  1526. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1527. }
  1528. #endif
  1529. } else
  1530. #endif /* __UCLIBC_HAS_IPV6__ */
  1531. c = inet_ntop (AF_INET,
  1532. (const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
  1533. host, hostlen);
  1534. if (c == NULL) {
  1535. errno = serrno;
  1536. return EAI_SYSTEM;
  1537. }
  1538. }
  1539. ok = 1;
  1540. }
  1541. break;
  1542. case AF_LOCAL:
  1543. if (!(flags & NI_NUMERICHOST)) {
  1544. struct utsname utsname;
  1545. if (!uname (&utsname)) {
  1546. strncpy (host, utsname.nodename, hostlen);
  1547. break;
  1548. };
  1549. };
  1550. if (flags & NI_NAMEREQD) {
  1551. errno = serrno;
  1552. return EAI_NONAME;
  1553. }
  1554. strncpy (host, "localhost", hostlen);
  1555. break;
  1556. default:
  1557. return EAI_FAMILY;
  1558. }
  1559. if (serv && (servlen > 0)) {
  1560. switch (sa->sa_family) {
  1561. case AF_INET:
  1562. #ifdef __UCLIBC_HAS_IPV6__
  1563. case AF_INET6:
  1564. #endif /* __UCLIBC_HAS_IPV6__ */
  1565. if (!(flags & NI_NUMERICSERV)) {
  1566. struct servent *s;
  1567. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1568. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1569. if (s) {
  1570. strncpy (serv, s->s_name, servlen);
  1571. break;
  1572. }
  1573. }
  1574. snprintf (serv, servlen, "%d",
  1575. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1576. break;
  1577. case AF_LOCAL:
  1578. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1579. break;
  1580. }
  1581. }
  1582. if (host && (hostlen > 0))
  1583. host[hostlen-1] = 0;
  1584. if (serv && (servlen > 0))
  1585. serv[servlen-1] = 0;
  1586. errno = serrno;
  1587. return 0;
  1588. }
  1589. #endif
  1590. #ifdef L_gethostbyname_r
  1591. int gethostbyname_r(const char * name,
  1592. struct hostent * result_buf,
  1593. char * buf, size_t buflen,
  1594. struct hostent ** result,
  1595. int * h_errnop)
  1596. {
  1597. struct in_addr *in;
  1598. struct in_addr **addr_list;
  1599. char **alias;
  1600. unsigned char *packet;
  1601. struct resolv_answer a;
  1602. int i;
  1603. int nest = 0;
  1604. int __nameserversXX;
  1605. char ** __nameserverXX;
  1606. __open_nameservers();
  1607. *result=NULL;
  1608. if (!name)
  1609. return EINVAL;
  1610. /* do /etc/hosts first */
  1611. {
  1612. int old_errno = errno; /* Save the old errno and reset errno */
  1613. __set_errno(0); /* to check for missing /etc/hosts. */
  1614. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1615. buf, buflen, result, h_errnop))==0)
  1616. return i;
  1617. switch (*h_errnop) {
  1618. case HOST_NOT_FOUND:
  1619. case NO_ADDRESS:
  1620. break;
  1621. case NETDB_INTERNAL:
  1622. if (errno == ENOENT) {
  1623. break;
  1624. }
  1625. /* else fall through */
  1626. default:
  1627. return i;
  1628. }
  1629. __set_errno(old_errno);
  1630. }
  1631. DPRINTF("Nothing found in /etc/hosts\n");
  1632. *h_errnop = NETDB_INTERNAL;
  1633. if (buflen < sizeof(*in))
  1634. return ERANGE;
  1635. in=(struct in_addr*)buf;
  1636. buf+=sizeof(*in);
  1637. buflen-=sizeof(*in);
  1638. if (buflen < sizeof(*addr_list)*2)
  1639. return ERANGE;
  1640. addr_list=(struct in_addr**)buf;
  1641. buf+=sizeof(*addr_list)*2;
  1642. buflen-=sizeof(*addr_list)*2;
  1643. addr_list[0] = in;
  1644. addr_list[1] = 0;
  1645. if (buflen < sizeof(char *)*(ALIAS_DIM))
  1646. return ERANGE;
  1647. alias=(char **)buf;
  1648. buf+=sizeof(char **)*(ALIAS_DIM);
  1649. buflen-=sizeof(char **)*(ALIAS_DIM);
  1650. if (buflen<256)
  1651. return ERANGE;
  1652. strncpy(buf, name, buflen);
  1653. alias[0] = buf;
  1654. alias[1] = NULL;
  1655. /* First check if this is already an address */
  1656. if (inet_aton(name, in)) {
  1657. result_buf->h_name = buf;
  1658. result_buf->h_addrtype = AF_INET;
  1659. result_buf->h_length = sizeof(*in);
  1660. result_buf->h_addr_list = (char **) addr_list;
  1661. result_buf->h_aliases = alias;
  1662. *result=result_buf;
  1663. *h_errnop = NETDB_SUCCESS;
  1664. return NETDB_SUCCESS;
  1665. }
  1666. for (;;) {
  1667. BIGLOCK;
  1668. __nameserversXX=__nameservers;
  1669. __nameserverXX=__nameserver;
  1670. BIGUNLOCK;
  1671. i = __dns_lookup(buf, T_A, __nameserversXX, __nameserverXX, &packet, &a);
  1672. if (i < 0) {
  1673. *h_errnop = HOST_NOT_FOUND;
  1674. DPRINTF("__dns_lookup\n");
  1675. return TRY_AGAIN;
  1676. }
  1677. strncpy(buf, a.dotted, buflen);
  1678. free(a.dotted);
  1679. if (a.atype == T_CNAME) { /* CNAME */
  1680. DPRINTF("Got a CNAME in gethostbyname()\n");
  1681. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1682. free(packet);
  1683. if (i < 0) {
  1684. *h_errnop = NO_RECOVERY;
  1685. DPRINTF("__decode_dotted\n");
  1686. return -1;
  1687. }
  1688. if (++nest > MAX_RECURSE) {
  1689. *h_errnop = NO_RECOVERY;
  1690. DPRINTF("recursion\n");
  1691. return -1;
  1692. }
  1693. continue;
  1694. } else if (a.atype == T_A) { /* ADDRESS */
  1695. memcpy(in, a.rdata, sizeof(*in));
  1696. result_buf->h_name = buf;
  1697. result_buf->h_addrtype = AF_INET;
  1698. result_buf->h_length = sizeof(*in);
  1699. result_buf->h_addr_list = (char **) addr_list;
  1700. #ifdef __UCLIBC_MJN3_ONLY__
  1701. #warning TODO -- generate the full list
  1702. #endif
  1703. result_buf->h_aliases = alias; /* TODO: generate the full list */
  1704. free(packet);
  1705. break;
  1706. } else {
  1707. free(packet);
  1708. *h_errnop=HOST_NOT_FOUND;
  1709. return TRY_AGAIN;
  1710. }
  1711. }
  1712. *result=result_buf;
  1713. *h_errnop = NETDB_SUCCESS;
  1714. return NETDB_SUCCESS;
  1715. }
  1716. #endif
  1717. #ifdef L_gethostbyname2_r
  1718. int gethostbyname2_r(const char *name, int family,
  1719. struct hostent * result_buf,
  1720. char * buf, size_t buflen,
  1721. struct hostent ** result,
  1722. int * h_errnop)
  1723. {
  1724. #ifndef __UCLIBC_HAS_IPV6__
  1725. return family == AF_INET ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1726. #else /* __UCLIBC_HAS_IPV6__ */
  1727. struct in6_addr *in;
  1728. struct in6_addr **addr_list;
  1729. unsigned char *packet;
  1730. struct resolv_answer a;
  1731. int i;
  1732. int nest = 0;
  1733. int __nameserversXX;
  1734. char ** __nameserverXX;
  1735. if (family == AF_INET)
  1736. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1737. if (family != AF_INET6)
  1738. return EINVAL;
  1739. __open_nameservers();
  1740. *result=NULL;
  1741. if (!name)
  1742. return EINVAL;
  1743. /* do /etc/hosts first */
  1744. {
  1745. int old_errno = errno; /* Save the old errno and reset errno */
  1746. __set_errno(0); /* to check for missing /etc/hosts. */
  1747. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1748. buf, buflen, result, h_errnop))==0)
  1749. return i;
  1750. switch (*h_errnop) {
  1751. case HOST_NOT_FOUND:
  1752. case NO_ADDRESS:
  1753. break;
  1754. case NETDB_INTERNAL:
  1755. if (errno == ENOENT) {
  1756. break;
  1757. }
  1758. /* else fall through */
  1759. default:
  1760. return i;
  1761. }
  1762. __set_errno(old_errno);
  1763. }
  1764. DPRINTF("Nothing found in /etc/hosts\n");
  1765. *h_errnop = NETDB_INTERNAL;
  1766. if (buflen < sizeof(*in))
  1767. return ERANGE;
  1768. in=(struct in6_addr*)buf;
  1769. buf+=sizeof(*in);
  1770. buflen-=sizeof(*in);
  1771. if (buflen < sizeof(*addr_list)*2)
  1772. return ERANGE;
  1773. addr_list=(struct in6_addr**)buf;
  1774. buf+=sizeof(*addr_list)*2;
  1775. buflen-=sizeof(*addr_list)*2;
  1776. addr_list[0] = in;
  1777. addr_list[1] = 0;
  1778. if (buflen<256)
  1779. return ERANGE;
  1780. strncpy(buf, name, buflen);
  1781. /* First check if this is already an address */
  1782. if (inet_pton(AF_INET6, name, in)) {
  1783. result_buf->h_name = buf;
  1784. result_buf->h_addrtype = AF_INET6;
  1785. result_buf->h_length = sizeof(*in);
  1786. result_buf->h_addr_list = (char **) addr_list;
  1787. *result=result_buf;
  1788. *h_errnop = NETDB_SUCCESS;
  1789. return NETDB_SUCCESS;
  1790. }
  1791. for (;;) {
  1792. BIGLOCK;
  1793. __nameserversXX=__nameservers;
  1794. __nameserverXX=__nameserver;
  1795. BIGUNLOCK;
  1796. i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
  1797. if (i < 0) {
  1798. *h_errnop = HOST_NOT_FOUND;
  1799. return TRY_AGAIN;
  1800. }
  1801. strncpy(buf, a.dotted, buflen);
  1802. free(a.dotted);
  1803. if (a.atype == T_CNAME) { /* CNAME */
  1804. DPRINTF("Got a CNAME in gethostbyname()\n");
  1805. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1806. free(packet);
  1807. if (i < 0) {
  1808. *h_errnop = NO_RECOVERY;
  1809. return -1;
  1810. }
  1811. if (++nest > MAX_RECURSE) {
  1812. *h_errnop = NO_RECOVERY;
  1813. return -1;
  1814. }
  1815. continue;
  1816. } else if (a.atype == T_AAAA) { /* ADDRESS */
  1817. memcpy(in, a.rdata, sizeof(*in));
  1818. result_buf->h_name = buf;
  1819. result_buf->h_addrtype = AF_INET6;
  1820. result_buf->h_length = sizeof(*in);
  1821. result_buf->h_addr_list = (char **) addr_list;
  1822. free(packet);
  1823. break;
  1824. } else {
  1825. free(packet);
  1826. *h_errnop=HOST_NOT_FOUND;
  1827. return TRY_AGAIN;
  1828. }
  1829. }
  1830. *result=result_buf;
  1831. *h_errnop = NETDB_SUCCESS;
  1832. return NETDB_SUCCESS;
  1833. #endif /* __UCLIBC_HAS_IPV6__ */
  1834. }
  1835. #endif
  1836. #ifdef L_gethostbyaddr_r
  1837. int gethostbyaddr_r (const void *addr, socklen_t len, int type,
  1838. struct hostent * result_buf,
  1839. char * buf, size_t buflen,
  1840. struct hostent ** result,
  1841. int * h_errnop)
  1842. {
  1843. struct in_addr *in;
  1844. struct in_addr **addr_list;
  1845. #ifdef __UCLIBC_HAS_IPV6__
  1846. char *qp;
  1847. size_t plen;
  1848. struct in6_addr *in6;
  1849. struct in6_addr **addr_list6;
  1850. #endif /* __UCLIBC_HAS_IPV6__ */
  1851. unsigned char *packet;
  1852. struct resolv_answer a;
  1853. int i;
  1854. int nest = 0;
  1855. int __nameserversXX;
  1856. char ** __nameserverXX;
  1857. *result=NULL;
  1858. if (!addr)
  1859. return EINVAL;
  1860. switch (type) {
  1861. case AF_INET:
  1862. if (len != sizeof(struct in_addr))
  1863. return EINVAL;
  1864. break;
  1865. #ifdef __UCLIBC_HAS_IPV6__
  1866. case AF_INET6:
  1867. if (len != sizeof(struct in6_addr))
  1868. return EINVAL;
  1869. break;
  1870. #endif /* __UCLIBC_HAS_IPV6__ */
  1871. default:
  1872. return EINVAL;
  1873. }
  1874. /* do /etc/hosts first */
  1875. if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
  1876. buf, buflen, result, h_errnop))==0)
  1877. return i;
  1878. switch (*h_errnop) {
  1879. case HOST_NOT_FOUND:
  1880. case NO_ADDRESS:
  1881. break;
  1882. default:
  1883. return i;
  1884. }
  1885. __open_nameservers();
  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 */