resolv.c 63 KB

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