resolv.c 68 KB

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