resolv.c 63 KB

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