resolv.c 67 KB

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