resolv.c 67 KB

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