resolv.c 57 KB

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