resolv.c 59 KB

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