resolv.c 78 KB

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