resolv.c 78 KB

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