resolv.c 65 KB

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