resolv.c 63 KB

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