resolv.c 64 KB

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