resolv.c 70 KB

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