resolv.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303
  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. #endif
  967. #ifdef L_gethostbyaddr
  968. struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
  969. {
  970. static struct hostent h;
  971. static char buf[
  972. #ifndef __UCLIBC_HAS_IPV6__
  973. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  974. #else
  975. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  976. #endif /* __UCLIBC_HAS_IPV6__ */
  977. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  978. struct hostent *hp;
  979. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  980. return hp;
  981. }
  982. #endif
  983. #ifdef L_read_etc_hosts_r
  984. void __open_etc_hosts(FILE **fp)
  985. {
  986. if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
  987. *fp = fopen("/etc/config/hosts", "r");
  988. }
  989. return;
  990. }
  991. int __read_etc_hosts_r(FILE * fp, const char * name, int type,
  992. enum etc_hosts_action action,
  993. struct hostent * result_buf,
  994. char * buf, size_t buflen,
  995. struct hostent ** result,
  996. int * h_errnop)
  997. {
  998. struct in_addr *in=NULL;
  999. struct in_addr **addr_list=NULL;
  1000. #ifdef __UCLIBC_HAS_IPV6__
  1001. struct in6_addr *in6=NULL;
  1002. struct in6_addr **addr_list6=NULL;
  1003. #endif /* __UCLIBC_HAS_IPV6__ */
  1004. char *cp;
  1005. char **alias;
  1006. int aliases, i;
  1007. int ret=HOST_NOT_FOUND;
  1008. if (buflen < sizeof(char *)*(ALIAS_DIM))
  1009. return ERANGE;
  1010. alias=(char **)buf;
  1011. buf+=sizeof(char **)*(ALIAS_DIM);
  1012. buflen-=sizeof(char **)*(ALIAS_DIM);
  1013. if (action!=GETHOSTENT) {
  1014. #ifdef __UCLIBC_HAS_IPV6__
  1015. char *p=buf;
  1016. size_t len=buflen;
  1017. #endif /* __UCLIBC_HAS_IPV6__ */
  1018. *h_errnop=NETDB_INTERNAL;
  1019. if (buflen < sizeof(*in))
  1020. return ERANGE;
  1021. in=(struct in_addr*)buf;
  1022. buf+=sizeof(*in);
  1023. buflen-=sizeof(*in);
  1024. if (buflen < sizeof(*addr_list)*2)
  1025. return ERANGE;
  1026. addr_list=(struct in_addr **)buf;
  1027. buf+=sizeof(*addr_list)*2;
  1028. buflen-=sizeof(*addr_list)*2;
  1029. #ifdef __UCLIBC_HAS_IPV6__
  1030. if (len < sizeof(*in6))
  1031. return ERANGE;
  1032. in6=(struct in6_addr*)p;
  1033. p+=sizeof(*in6);
  1034. len-=sizeof(*in6);
  1035. if (len < sizeof(*addr_list6)*2)
  1036. return ERANGE;
  1037. addr_list6=(struct in6_addr**)p;
  1038. p+=sizeof(*addr_list6)*2;
  1039. len-=sizeof(*addr_list6)*2;
  1040. if (len < buflen) {
  1041. buflen=len;
  1042. buf=p;
  1043. }
  1044. #endif /* __UCLIBC_HAS_IPV6__ */
  1045. if (buflen < 80)
  1046. return ERANGE;
  1047. __open_etc_hosts(&fp);
  1048. if (fp == NULL) {
  1049. result=NULL;
  1050. return errno;
  1051. }
  1052. }
  1053. *h_errnop=HOST_NOT_FOUND;
  1054. while (fgets(buf, buflen, fp)) {
  1055. if ((cp = strchr(buf, '#')))
  1056. *cp = '\0';
  1057. DPRINTF("Looking at: %s\n", buf);
  1058. aliases = 0;
  1059. cp = buf;
  1060. while (*cp) {
  1061. while (*cp && isspace(*cp))
  1062. *cp++ = '\0';
  1063. if (!*cp)
  1064. continue;
  1065. if (aliases < (2+MAX_ALIASES))
  1066. alias[aliases++] = cp;
  1067. while (*cp && !isspace(*cp))
  1068. cp++;
  1069. }
  1070. alias[aliases] = 0;
  1071. if (aliases < 2)
  1072. continue; /* syntax error really */
  1073. if (action==GETHOSTENT) {
  1074. /* Return whatever the next entry happens to be. */
  1075. break;
  1076. } else if (action==GET_HOSTS_BYADDR) {
  1077. if (strcmp(name, alias[0]) != 0)
  1078. continue;
  1079. } else {
  1080. /* GET_HOSTS_BYNAME */
  1081. for (i = 1; i < aliases; i++)
  1082. if (strcasecmp(name, alias[i]) == 0)
  1083. break;
  1084. if (i >= aliases)
  1085. continue;
  1086. }
  1087. if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
  1088. DPRINTF("Found INET\n");
  1089. addr_list[0] = in;
  1090. addr_list[1] = 0;
  1091. result_buf->h_name = alias[1];
  1092. result_buf->h_addrtype = AF_INET;
  1093. result_buf->h_length = sizeof(*in);
  1094. result_buf->h_addr_list = (char**) addr_list;
  1095. result_buf->h_aliases = alias + 2;
  1096. *result=result_buf;
  1097. ret=NETDB_SUCCESS;
  1098. #ifdef __UCLIBC_HAS_IPV6__
  1099. } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
  1100. DPRINTF("Found INET6\n");
  1101. addr_list6[0] = in6;
  1102. addr_list6[1] = 0;
  1103. result_buf->h_name = alias[1];
  1104. result_buf->h_addrtype = AF_INET6;
  1105. result_buf->h_length = sizeof(*in6);
  1106. result_buf->h_addr_list = (char**) addr_list6;
  1107. result_buf->h_aliases = alias + 2;
  1108. *result=result_buf;
  1109. ret=NETDB_SUCCESS;
  1110. #endif /* __UCLIBC_HAS_IPV6__ */
  1111. } else {
  1112. DPRINTF("Error\n");
  1113. ret=TRY_AGAIN;
  1114. break; /* bad ip address */
  1115. }
  1116. if (action!=GETHOSTENT) {
  1117. fclose(fp);
  1118. }
  1119. return ret;
  1120. }
  1121. if (action!=GETHOSTENT) {
  1122. fclose(fp);
  1123. }
  1124. return ret;
  1125. }
  1126. #endif
  1127. #ifdef L_gethostent
  1128. #ifdef __UCLIBC_HAS_THREADS__
  1129. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  1130. # define LOCK __pthread_mutex_lock(&mylock)
  1131. # define UNLOCK __pthread_mutex_unlock(&mylock);
  1132. #else
  1133. # define LOCK
  1134. # define UNLOCK
  1135. #endif
  1136. static int __stay_open;
  1137. static FILE * __gethostent_fp;
  1138. void endhostent (void)
  1139. {
  1140. LOCK;
  1141. __stay_open = 0;
  1142. if (__gethostent_fp) {
  1143. fclose(__gethostent_fp);
  1144. }
  1145. UNLOCK;
  1146. }
  1147. void sethostent (int stay_open)
  1148. {
  1149. LOCK;
  1150. __stay_open = stay_open;
  1151. UNLOCK;
  1152. }
  1153. struct hostent *gethostent (void)
  1154. {
  1155. static struct hostent h;
  1156. static char buf[
  1157. #ifndef __UCLIBC_HAS_IPV6__
  1158. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1159. #else
  1160. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1161. #endif /* __UCLIBC_HAS_IPV6__ */
  1162. sizeof(char *)*(ALIAS_DIM) +
  1163. 80/*namebuffer*/ + 2/* margin */];
  1164. struct hostent *host;
  1165. LOCK;
  1166. if (__gethostent_fp == NULL) {
  1167. __open_etc_hosts(&__gethostent_fp);
  1168. if (__gethostent_fp == NULL) {
  1169. UNLOCK;
  1170. return((struct hostent *)NULL);
  1171. }
  1172. }
  1173. __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  1174. &h, buf, sizeof(buf), &host, &h_errno);
  1175. if (__stay_open==0) {
  1176. fclose(__gethostent_fp);
  1177. }
  1178. UNLOCK;
  1179. return(host);
  1180. }
  1181. #endif
  1182. #ifdef L_get_hosts_byname_r
  1183. int __get_hosts_byname_r(const char * name, int type,
  1184. struct hostent * result_buf,
  1185. char * buf, size_t buflen,
  1186. struct hostent ** result,
  1187. int * h_errnop)
  1188. {
  1189. return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, result_buf, buf, buflen, result, h_errnop));
  1190. }
  1191. #endif
  1192. #ifdef L_get_hosts_byaddr_r
  1193. int __get_hosts_byaddr_r(const char * addr, int len, int type,
  1194. struct hostent * result_buf,
  1195. char * buf, size_t buflen,
  1196. struct hostent ** result,
  1197. int * h_errnop)
  1198. {
  1199. #ifndef __UCLIBC_HAS_IPV6__
  1200. char ipaddr[INET_ADDRSTRLEN];
  1201. #else
  1202. char ipaddr[INET6_ADDRSTRLEN];
  1203. #endif /* __UCLIBC_HAS_IPV6__ */
  1204. switch (type) {
  1205. case AF_INET:
  1206. if (len != sizeof(struct in_addr))
  1207. return 0;
  1208. break;
  1209. #ifdef __UCLIBC_HAS_IPV6__
  1210. case AF_INET6:
  1211. if (len != sizeof(struct in6_addr))
  1212. return 0;
  1213. break;
  1214. #endif /* __UCLIBC_HAS_IPV6__ */
  1215. default:
  1216. return 0;
  1217. }
  1218. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1219. return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1220. result_buf, buf, buflen, result, h_errnop));
  1221. }
  1222. #endif
  1223. #ifdef L_getnameinfo
  1224. #ifndef min
  1225. # define min(x,y) (((x) > (y)) ? (y) : (x))
  1226. #endif /* min */
  1227. int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  1228. socklen_t hostlen, char *serv, socklen_t servlen,
  1229. unsigned int flags)
  1230. {
  1231. int serrno = errno;
  1232. int ok = 0;
  1233. struct hostent *h = NULL;
  1234. char domain[256];
  1235. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1236. return EAI_BADFLAGS;
  1237. if (sa == NULL || addrlen < sizeof (sa_family_t))
  1238. return EAI_FAMILY;
  1239. switch (sa->sa_family) {
  1240. case AF_LOCAL:
  1241. break;
  1242. case AF_INET:
  1243. if (addrlen < sizeof (struct sockaddr_in))
  1244. return EAI_FAMILY;
  1245. break;
  1246. #ifdef __UCLIBC_HAS_IPV6__
  1247. case AF_INET6:
  1248. if (addrlen < sizeof (struct sockaddr_in6))
  1249. return EAI_FAMILY;
  1250. break;
  1251. #endif /* __UCLIBC_HAS_IPV6__ */
  1252. default:
  1253. return EAI_FAMILY;
  1254. }
  1255. if (host != NULL && hostlen > 0)
  1256. switch (sa->sa_family) {
  1257. case AF_INET:
  1258. #ifdef __UCLIBC_HAS_IPV6__
  1259. case AF_INET6:
  1260. #endif /* __UCLIBC_HAS_IPV6__ */
  1261. if (!(flags & NI_NUMERICHOST)) {
  1262. #ifdef __UCLIBC_HAS_IPV6__
  1263. if (sa->sa_family == AF_INET6)
  1264. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1265. sizeof(struct in6_addr), AF_INET6);
  1266. else
  1267. #endif /* __UCLIBC_HAS_IPV6__ */
  1268. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1269. sizeof(struct in_addr), AF_INET);
  1270. if (h) {
  1271. char *c;
  1272. if ((flags & NI_NOFQDN)
  1273. && (getdomainname (domain, sizeof(domain)) == 0)
  1274. && (c = strstr (h->h_name, domain))
  1275. && (c != h->h_name) && (*(--c) == '.')) {
  1276. strncpy (host, h->h_name,
  1277. min(hostlen, (size_t) (c - h->h_name)));
  1278. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1279. ok = 1;
  1280. } else {
  1281. strncpy (host, h->h_name, hostlen);
  1282. ok = 1;
  1283. }
  1284. }
  1285. }
  1286. if (!ok) {
  1287. if (flags & NI_NAMEREQD) {
  1288. errno = serrno;
  1289. return EAI_NONAME;
  1290. } else {
  1291. const char *c;
  1292. #ifdef __UCLIBC_HAS_IPV6__
  1293. if (sa->sa_family == AF_INET6) {
  1294. const struct sockaddr_in6 *sin6p;
  1295. sin6p = (const struct sockaddr_in6 *) sa;
  1296. c = inet_ntop (AF_INET6,
  1297. (const void *) &sin6p->sin6_addr, host, hostlen);
  1298. #if 0
  1299. /* Does scope id need to be supported? */
  1300. uint32_t scopeid;
  1301. scopeid = sin6p->sin6_scope_id;
  1302. if (scopeid != 0) {
  1303. /* Buffer is >= IFNAMSIZ+1. */
  1304. char scopebuf[IFNAMSIZ + 1];
  1305. char *scopeptr;
  1306. int ni_numericscope = 0;
  1307. size_t real_hostlen = __strnlen (host, hostlen);
  1308. size_t scopelen = 0;
  1309. scopebuf[0] = SCOPE_DELIMITER;
  1310. scopebuf[1] = '\0';
  1311. scopeptr = &scopebuf[1];
  1312. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1313. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1314. if (if_indextoname (scopeid, scopeptr) == NULL)
  1315. ++ni_numericscope;
  1316. else
  1317. scopelen = strlen (scopebuf);
  1318. } else {
  1319. ++ni_numericscope;
  1320. }
  1321. if (ni_numericscope)
  1322. scopelen = 1 + snprintf (scopeptr,
  1323. (scopebuf
  1324. + sizeof scopebuf
  1325. - scopeptr),
  1326. "%u", scopeid);
  1327. if (real_hostlen + scopelen + 1 > hostlen)
  1328. return EAI_SYSTEM;
  1329. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1330. }
  1331. #endif
  1332. } else
  1333. #endif /* __UCLIBC_HAS_IPV6__ */
  1334. c = inet_ntop (AF_INET,
  1335. (const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
  1336. host, hostlen);
  1337. if (c == NULL) {
  1338. errno = serrno;
  1339. return EAI_SYSTEM;
  1340. }
  1341. }
  1342. ok = 1;
  1343. }
  1344. break;
  1345. case AF_LOCAL:
  1346. if (!(flags & NI_NUMERICHOST)) {
  1347. struct utsname utsname;
  1348. if (!uname (&utsname)) {
  1349. strncpy (host, utsname.nodename, hostlen);
  1350. break;
  1351. };
  1352. };
  1353. if (flags & NI_NAMEREQD) {
  1354. errno = serrno;
  1355. return EAI_NONAME;
  1356. }
  1357. strncpy (host, "localhost", hostlen);
  1358. break;
  1359. default:
  1360. return EAI_FAMILY;
  1361. }
  1362. if (serv && (servlen > 0)) {
  1363. switch (sa->sa_family) {
  1364. case AF_INET:
  1365. #ifdef __UCLIBC_HAS_IPV6__
  1366. case AF_INET6:
  1367. #endif /* __UCLIBC_HAS_IPV6__ */
  1368. if (!(flags & NI_NUMERICSERV)) {
  1369. struct servent *s;
  1370. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1371. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1372. if (s) {
  1373. strncpy (serv, s->s_name, servlen);
  1374. break;
  1375. }
  1376. }
  1377. snprintf (serv, servlen, "%d",
  1378. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1379. break;
  1380. case AF_LOCAL:
  1381. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1382. break;
  1383. }
  1384. }
  1385. if (host && (hostlen > 0))
  1386. host[hostlen-1] = 0;
  1387. if (serv && (servlen > 0))
  1388. serv[servlen-1] = 0;
  1389. errno = serrno;
  1390. return 0;
  1391. }
  1392. #endif
  1393. #ifdef L_gethostbyname_r
  1394. int gethostbyname_r(const char * name,
  1395. struct hostent * result_buf,
  1396. char * buf, size_t buflen,
  1397. struct hostent ** result,
  1398. int * h_errnop)
  1399. {
  1400. struct in_addr *in;
  1401. struct in_addr **addr_list;
  1402. unsigned char *packet;
  1403. struct resolv_answer a;
  1404. int i;
  1405. int nest = 0;
  1406. int __nameserversXX;
  1407. char ** __nameserverXX;
  1408. __open_nameservers();
  1409. *result=NULL;
  1410. if (!name)
  1411. return EINVAL;
  1412. /* do /etc/hosts first */
  1413. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1414. buf, buflen, result, h_errnop))==0)
  1415. return i;
  1416. switch (*h_errnop) {
  1417. case HOST_NOT_FOUND:
  1418. case NO_ADDRESS:
  1419. break;
  1420. case NETDB_INTERNAL:
  1421. if (errno == ENOENT) {
  1422. break;
  1423. }
  1424. /* else fall through */
  1425. default:
  1426. return i;
  1427. }
  1428. DPRINTF("Nothing found in /etc/hosts\n");
  1429. *h_errnop = NETDB_INTERNAL;
  1430. if (buflen < sizeof(*in))
  1431. return ERANGE;
  1432. in=(struct in_addr*)buf;
  1433. buf+=sizeof(*in);
  1434. buflen-=sizeof(*in);
  1435. if (buflen < sizeof(*addr_list)*2)
  1436. return ERANGE;
  1437. addr_list=(struct in_addr**)buf;
  1438. buf+=sizeof(*addr_list)*2;
  1439. buflen-=sizeof(*addr_list)*2;
  1440. addr_list[0] = in;
  1441. addr_list[1] = 0;
  1442. if (buflen<256)
  1443. return ERANGE;
  1444. strncpy(buf, name, buflen);
  1445. /* First check if this is already an address */
  1446. if (inet_aton(name, in)) {
  1447. result_buf->h_name = buf;
  1448. result_buf->h_addrtype = AF_INET;
  1449. result_buf->h_length = sizeof(*in);
  1450. result_buf->h_addr_list = (char **) addr_list;
  1451. *result=result_buf;
  1452. *h_errnop = NETDB_SUCCESS;
  1453. return NETDB_SUCCESS;
  1454. }
  1455. for (;;) {
  1456. BIGLOCK;
  1457. __nameserversXX=__nameservers;
  1458. __nameserverXX=__nameserver;
  1459. BIGUNLOCK;
  1460. i = __dns_lookup(buf, T_A, __nameserversXX, __nameserverXX, &packet, &a);
  1461. if (i < 0) {
  1462. *h_errnop = HOST_NOT_FOUND;
  1463. DPRINTF("__dns_lookup\n");
  1464. return TRY_AGAIN;
  1465. }
  1466. strncpy(buf, a.dotted, buflen);
  1467. free(a.dotted);
  1468. if (a.atype == T_CNAME) { /* CNAME */
  1469. DPRINTF("Got a CNAME in gethostbyname()\n");
  1470. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1471. free(packet);
  1472. if (i < 0) {
  1473. *h_errnop = NO_RECOVERY;
  1474. DPRINTF("__decode_dotted\n");
  1475. return -1;
  1476. }
  1477. if (++nest > MAX_RECURSE) {
  1478. *h_errnop = NO_RECOVERY;
  1479. DPRINTF("recursion\n");
  1480. return -1;
  1481. }
  1482. continue;
  1483. } else if (a.atype == T_A) { /* ADDRESS */
  1484. memcpy(in, a.rdata, sizeof(*in));
  1485. result_buf->h_name = buf;
  1486. result_buf->h_addrtype = AF_INET;
  1487. result_buf->h_length = sizeof(*in);
  1488. result_buf->h_addr_list = (char **) addr_list;
  1489. free(packet);
  1490. break;
  1491. } else {
  1492. free(packet);
  1493. *h_errnop=HOST_NOT_FOUND;
  1494. return TRY_AGAIN;
  1495. }
  1496. }
  1497. *result=result_buf;
  1498. *h_errnop = NETDB_SUCCESS;
  1499. return NETDB_SUCCESS;
  1500. }
  1501. #endif
  1502. #ifdef L_gethostbyname2_r
  1503. int gethostbyname2_r(const char *name, int family,
  1504. struct hostent * result_buf,
  1505. char * buf, size_t buflen,
  1506. struct hostent ** result,
  1507. int * h_errnop)
  1508. {
  1509. #ifndef __UCLIBC_HAS_IPV6__
  1510. return family == AF_INET ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1511. #else /* __UCLIBC_HAS_IPV6__ */
  1512. struct in6_addr *in;
  1513. struct in6_addr **addr_list;
  1514. unsigned char *packet;
  1515. struct resolv_answer a;
  1516. int i;
  1517. int nest = 0;
  1518. int __nameserversXX;
  1519. char ** __nameserverXX;
  1520. if (family == AF_INET)
  1521. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1522. if (family != AF_INET6)
  1523. return EINVAL;
  1524. __open_nameservers();
  1525. *result=NULL;
  1526. if (!name)
  1527. return EINVAL;
  1528. /* do /etc/hosts first */
  1529. if ((i=__get_hosts_byname_r(name, family, result_buf,
  1530. buf, buflen, result, h_errnop))==0)
  1531. return i;
  1532. switch (*h_errnop) {
  1533. case HOST_NOT_FOUND:
  1534. case NO_ADDRESS:
  1535. break;
  1536. default:
  1537. return i;
  1538. }
  1539. DPRINTF("Nothing found in /etc/hosts\n");
  1540. *h_errnop = NETDB_INTERNAL;
  1541. if (buflen < sizeof(*in))
  1542. return ERANGE;
  1543. in=(struct in6_addr*)buf;
  1544. buf+=sizeof(*in);
  1545. buflen-=sizeof(*in);
  1546. if (buflen < sizeof(*addr_list)*2)
  1547. return ERANGE;
  1548. addr_list=(struct in6_addr**)buf;
  1549. buf+=sizeof(*addr_list)*2;
  1550. buflen-=sizeof(*addr_list)*2;
  1551. addr_list[0] = in;
  1552. addr_list[1] = 0;
  1553. if (buflen<256)
  1554. return ERANGE;
  1555. strncpy(buf, name, buflen);
  1556. /* First check if this is already an address */
  1557. if (inet_pton(AF_INET6, name, in)) {
  1558. result_buf->h_name = buf;
  1559. result_buf->h_addrtype = AF_INET6;
  1560. result_buf->h_length = sizeof(*in);
  1561. result_buf->h_addr_list = (char **) addr_list;
  1562. *result=result_buf;
  1563. *h_errnop = NETDB_SUCCESS;
  1564. return NETDB_SUCCESS;
  1565. }
  1566. for (;;) {
  1567. BIGLOCK;
  1568. __nameserversXX=__nameservers;
  1569. __nameserverXX=__nameserver;
  1570. BIGUNLOCK;
  1571. i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
  1572. if (i < 0) {
  1573. *h_errnop = HOST_NOT_FOUND;
  1574. return TRY_AGAIN;
  1575. }
  1576. strncpy(buf, a.dotted, buflen);
  1577. free(a.dotted);
  1578. if (a.atype == T_CNAME) { /* CNAME */
  1579. DPRINTF("Got a CNAME in gethostbyname()\n");
  1580. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1581. free(packet);
  1582. if (i < 0) {
  1583. *h_errnop = NO_RECOVERY;
  1584. return -1;
  1585. }
  1586. if (++nest > MAX_RECURSE) {
  1587. *h_errnop = NO_RECOVERY;
  1588. return -1;
  1589. }
  1590. continue;
  1591. } else if (a.atype == T_AAAA) { /* ADDRESS */
  1592. memcpy(in, a.rdata, sizeof(*in));
  1593. result_buf->h_name = buf;
  1594. result_buf->h_addrtype = AF_INET6;
  1595. result_buf->h_length = sizeof(*in);
  1596. result_buf->h_addr_list = (char **) addr_list;
  1597. free(packet);
  1598. break;
  1599. } else {
  1600. free(packet);
  1601. *h_errnop=HOST_NOT_FOUND;
  1602. return TRY_AGAIN;
  1603. }
  1604. }
  1605. *result=result_buf;
  1606. *h_errnop = NETDB_SUCCESS;
  1607. return NETDB_SUCCESS;
  1608. #endif /* __UCLIBC_HAS_IPV6__ */
  1609. }
  1610. #endif
  1611. #ifdef L_gethostbyaddr_r
  1612. int gethostbyaddr_r (const void *addr, socklen_t len, int type,
  1613. struct hostent * result_buf,
  1614. char * buf, size_t buflen,
  1615. struct hostent ** result,
  1616. int * h_errnop)
  1617. {
  1618. struct in_addr *in;
  1619. struct in_addr **addr_list;
  1620. #ifdef __UCLIBC_HAS_IPV6__
  1621. char *qp;
  1622. size_t plen;
  1623. struct in6_addr *in6;
  1624. struct in6_addr **addr_list6;
  1625. #endif /* __UCLIBC_HAS_IPV6__ */
  1626. unsigned char *packet;
  1627. struct resolv_answer a;
  1628. int i;
  1629. int nest = 0;
  1630. int __nameserversXX;
  1631. char ** __nameserverXX;
  1632. *result=NULL;
  1633. if (!addr)
  1634. return EINVAL;
  1635. switch (type) {
  1636. case AF_INET:
  1637. if (len != sizeof(struct in_addr))
  1638. return EINVAL;
  1639. break;
  1640. #ifdef __UCLIBC_HAS_IPV6__
  1641. case AF_INET6:
  1642. if (len != sizeof(struct in6_addr))
  1643. return EINVAL;
  1644. break;
  1645. #endif /* __UCLIBC_HAS_IPV6__ */
  1646. default:
  1647. return EINVAL;
  1648. }
  1649. /* do /etc/hosts first */
  1650. if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
  1651. buf, buflen, result, h_errnop))==0)
  1652. return i;
  1653. switch (*h_errnop) {
  1654. case HOST_NOT_FOUND:
  1655. case NO_ADDRESS:
  1656. break;
  1657. default:
  1658. return i;
  1659. }
  1660. __open_nameservers();
  1661. #ifdef __UCLIBC_HAS_IPV6__
  1662. qp=buf;
  1663. plen=buflen;
  1664. #endif /* __UCLIBC_HAS_IPV6__ */
  1665. *h_errnop = NETDB_INTERNAL;
  1666. if (buflen < sizeof(*in))
  1667. return ERANGE;
  1668. in=(struct in_addr*)buf;
  1669. buf+=sizeof(*in);
  1670. buflen-=sizeof(*in);
  1671. if (buflen < sizeof(*addr_list)*2)
  1672. return ERANGE;
  1673. addr_list=(struct in_addr**)buf;
  1674. buf+=sizeof(*addr_list)*2;
  1675. buflen-=sizeof(*addr_list)*2;
  1676. #ifdef __UCLIBC_HAS_IPV6__
  1677. if (plen < sizeof(*in6))
  1678. return ERANGE;
  1679. in6=(struct in6_addr*)qp;
  1680. qp+=sizeof(*in6);
  1681. plen-=sizeof(*in6);
  1682. if (plen < sizeof(*addr_list6)*2)
  1683. return ERANGE;
  1684. addr_list6=(struct in6_addr**)qp;
  1685. qp+=sizeof(*addr_list6)*2;
  1686. plen-=sizeof(*addr_list6)*2;
  1687. if (plen < buflen) {
  1688. buflen=plen;
  1689. buf=qp;
  1690. }
  1691. #endif /* __UCLIBC_HAS_IPV6__ */
  1692. if (buflen<256)
  1693. return ERANGE;
  1694. if(type == AF_INET) {
  1695. unsigned char *tmp_addr = (unsigned char *)addr;
  1696. memcpy(&in->s_addr, addr, len);
  1697. addr_list[0] = in;
  1698. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  1699. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  1700. #ifdef __UCLIBC_HAS_IPV6__
  1701. } else {
  1702. memcpy(in6->s6_addr, addr, len);
  1703. addr_list6[0] = in6;
  1704. qp = buf;
  1705. for (i = len - 1; i >= 0; i--) {
  1706. qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
  1707. (in6->s6_addr[i] >> 4) & 0xf);
  1708. }
  1709. strcpy(qp, "ip6.int");
  1710. #endif /* __UCLIBC_HAS_IPV6__ */
  1711. }
  1712. addr_list[1] = 0;
  1713. for (;;) {
  1714. BIGLOCK;
  1715. __nameserversXX=__nameservers;
  1716. __nameserverXX=__nameserver;
  1717. BIGUNLOCK;
  1718. i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
  1719. if (i < 0) {
  1720. *h_errnop = HOST_NOT_FOUND;
  1721. return TRY_AGAIN;
  1722. }
  1723. strncpy(buf, a.dotted, buflen);
  1724. free(a.dotted);
  1725. if (a.atype == T_CNAME) { /* CNAME */
  1726. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  1727. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1728. free(packet);
  1729. if (i < 0) {
  1730. *h_errnop = NO_RECOVERY;
  1731. return -1;
  1732. }
  1733. if (++nest > MAX_RECURSE) {
  1734. *h_errnop = NO_RECOVERY;
  1735. return -1;
  1736. }
  1737. continue;
  1738. } else if (a.atype == T_PTR) { /* ADDRESS */
  1739. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1740. free(packet);
  1741. result_buf->h_name = buf;
  1742. result_buf->h_addrtype = type;
  1743. if(type == AF_INET) {
  1744. result_buf->h_length = sizeof(*in);
  1745. #ifdef __UCLIBC_HAS_IPV6__
  1746. } else {
  1747. result_buf->h_length = sizeof(*in6);
  1748. #endif /* __UCLIBC_HAS_IPV6__ */
  1749. }
  1750. result_buf->h_addr_list = (char **) addr_list;
  1751. break;
  1752. } else {
  1753. free(packet);
  1754. *h_errnop = NO_ADDRESS;
  1755. return TRY_AGAIN;
  1756. }
  1757. }
  1758. *result=result_buf;
  1759. *h_errnop = NETDB_SUCCESS;
  1760. return NETDB_SUCCESS;
  1761. }
  1762. #endif
  1763. #ifdef L_res_comp
  1764. /*
  1765. * Expand compressed domain name 'comp_dn' to full domain name.
  1766. * 'msg' is a pointer to the begining of the message,
  1767. * 'eomorig' points to the first location after the message,
  1768. * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  1769. * Return size of compressed name or -1 if there was an error.
  1770. */
  1771. int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  1772. char *dst, int dstsiz)
  1773. {
  1774. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  1775. if (n > 0 && dst[0] == '.')
  1776. dst[0] = '\0';
  1777. return (n);
  1778. }
  1779. #endif /* L_res_comp */
  1780. #ifdef L_ns_name
  1781. /*
  1782. * printable(ch)
  1783. * Thinking in noninternationalized USASCII (per the DNS spec),
  1784. * is this character visible and not a space when printed ?
  1785. * return:
  1786. * boolean.
  1787. */
  1788. static int printable(int ch)
  1789. {
  1790. return (ch > 0x20 && ch < 0x7f);
  1791. }
  1792. /*
  1793. * special(ch)
  1794. * Thinking in noninternationalized USASCII (per the DNS spec),
  1795. * is this characted special ("in need of quoting") ?
  1796. * return:
  1797. * boolean.
  1798. */
  1799. static int special(int ch)
  1800. {
  1801. switch (ch) {
  1802. case 0x22: /* '"' */
  1803. case 0x2E: /* '.' */
  1804. case 0x3B: /* ';' */
  1805. case 0x5C: /* '\\' */
  1806. /* Special modifiers in zone files. */
  1807. case 0x40: /* '@' */
  1808. case 0x24: /* '$' */
  1809. return (1);
  1810. default:
  1811. return (0);
  1812. }
  1813. }
  1814. /*
  1815. * ns_name_uncompress(msg, eom, src, dst, dstsiz)
  1816. * Expand compressed domain name to presentation format.
  1817. * return:
  1818. * Number of bytes read out of `src', or -1 (with errno set).
  1819. * note:
  1820. * Root domain returns as "." not "".
  1821. */
  1822. int __ns_name_uncompress(const u_char *msg, const u_char *eom,
  1823. const u_char *src, char *dst, size_t dstsiz)
  1824. {
  1825. u_char tmp[NS_MAXCDNAME];
  1826. int n;
  1827. if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
  1828. return (-1);
  1829. if (ns_name_ntop(tmp, dst, dstsiz) == -1)
  1830. return (-1);
  1831. return (n);
  1832. }
  1833. /*
  1834. * ns_name_ntop(src, dst, dstsiz)
  1835. * Convert an encoded domain name to printable ascii as per RFC1035.
  1836. * return:
  1837. * Number of bytes written to buffer, or -1 (with errno set)
  1838. * notes:
  1839. * The root is returned as "."
  1840. * All other domains are returned in non absolute form
  1841. */
  1842. int __ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
  1843. const u_char *cp;
  1844. char *dn, *eom;
  1845. u_char c;
  1846. u_int n;
  1847. const char digits[] = "0123456789";
  1848. cp = src;
  1849. dn = dst;
  1850. eom = dst + dstsiz;
  1851. while ((n = *cp++) != 0) {
  1852. if ((n & NS_CMPRSFLGS) != 0) {
  1853. /* Some kind of compression pointer. */
  1854. __set_errno (EMSGSIZE);
  1855. return (-1);
  1856. }
  1857. if (dn != dst) {
  1858. if (dn >= eom) {
  1859. __set_errno (EMSGSIZE);
  1860. return (-1);
  1861. }
  1862. *dn++ = '.';
  1863. }
  1864. if (dn + n >= eom) {
  1865. __set_errno (EMSGSIZE);
  1866. return (-1);
  1867. }
  1868. for ((void)NULL; n > 0; n--) {
  1869. c = *cp++;
  1870. if (special(c)) {
  1871. if (dn + 1 >= eom) {
  1872. __set_errno (EMSGSIZE);
  1873. return (-1);
  1874. }
  1875. *dn++ = '\\';
  1876. *dn++ = (char)c;
  1877. } else if (!printable(c)) {
  1878. if (dn + 3 >= eom) {
  1879. __set_errno (EMSGSIZE);
  1880. return (-1);
  1881. }
  1882. *dn++ = '\\';
  1883. *dn++ = digits[c / 100];
  1884. *dn++ = digits[(c % 100) / 10];
  1885. *dn++ = digits[c % 10];
  1886. } else {
  1887. if (dn >= eom) {
  1888. __set_errno (EMSGSIZE);
  1889. return (-1);
  1890. }
  1891. *dn++ = (char)c;
  1892. }
  1893. }
  1894. }
  1895. if (dn == dst) {
  1896. if (dn >= eom) {
  1897. __set_errno (EMSGSIZE);
  1898. return (-1);
  1899. }
  1900. *dn++ = '.';
  1901. }
  1902. if (dn >= eom) {
  1903. __set_errno (EMSGSIZE);
  1904. return (-1);
  1905. }
  1906. *dn++ = '\0';
  1907. return (dn - dst);
  1908. }
  1909. /*
  1910. * ns_name_unpack(msg, eom, src, dst, dstsiz)
  1911. * Unpack a domain name from a message, source may be compressed.
  1912. * return:
  1913. * -1 if it fails, or consumed octets if it succeeds.
  1914. */
  1915. int __ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
  1916. u_char *dst, size_t dstsiz)
  1917. {
  1918. const u_char *srcp, *dstlim;
  1919. u_char *dstp;
  1920. int n, len, checked;
  1921. len = -1;
  1922. checked = 0;
  1923. dstp = dst;
  1924. srcp = src;
  1925. dstlim = dst + dstsiz;
  1926. if (srcp < msg || srcp >= eom) {
  1927. __set_errno (EMSGSIZE);
  1928. return (-1);
  1929. }
  1930. /* Fetch next label in domain name. */
  1931. while ((n = *srcp++) != 0) {
  1932. /* Check for indirection. */
  1933. switch (n & NS_CMPRSFLGS) {
  1934. case 0:
  1935. /* Limit checks. */
  1936. if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  1937. __set_errno (EMSGSIZE);
  1938. return (-1);
  1939. }
  1940. checked += n + 1;
  1941. *dstp++ = n;
  1942. memcpy(dstp, srcp, n);
  1943. dstp += n;
  1944. srcp += n;
  1945. break;
  1946. case NS_CMPRSFLGS:
  1947. if (srcp >= eom) {
  1948. __set_errno (EMSGSIZE);
  1949. return (-1);
  1950. }
  1951. if (len < 0)
  1952. len = srcp - src + 1;
  1953. srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  1954. if (srcp < msg || srcp >= eom) { /* Out of range. */
  1955. __set_errno (EMSGSIZE);
  1956. return (-1);
  1957. }
  1958. checked += 2;
  1959. /*
  1960. * Check for loops in the compressed name;
  1961. * if we've looked at the whole message,
  1962. * there must be a loop.
  1963. */
  1964. if (checked >= eom - msg) {
  1965. __set_errno (EMSGSIZE);
  1966. return (-1);
  1967. }
  1968. break;
  1969. default:
  1970. __set_errno (EMSGSIZE);
  1971. return (-1); /* flag error */
  1972. }
  1973. }
  1974. *dstp = '\0';
  1975. if (len < 0)
  1976. len = srcp - src;
  1977. return (len);
  1978. }
  1979. #endif /* L_ns_name */