resolv.c 72 KB

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