resolv.c 63 KB

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