resolv.c 76 KB

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