resolv.c 58 KB

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