resolv.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664
  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. libc_hidden_proto(__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. libc_hidden_data_def(__resolv_lock)
  884. #endif
  885. /*
  886. * we currently read formats not quite the same as that on normal
  887. * unix systems, we can have a list of nameservers after the keyword.
  888. */
  889. int attribute_hidden __open_nameservers()
  890. {
  891. FILE *fp;
  892. int i;
  893. #define RESOLV_ARGS 5
  894. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  895. int argc;
  896. BIGLOCK;
  897. if (__nameservers > 0) {
  898. BIGUNLOCK;
  899. return 0;
  900. }
  901. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  902. (fp = fopen("/etc/config/resolv.conf", "r")))
  903. {
  904. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  905. for (p = szBuffer; *p && isspace(*p); p++)
  906. /* skip white space */;
  907. if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  908. continue;
  909. argc = 0;
  910. while (*p && argc < RESOLV_ARGS) {
  911. argv[argc++] = p;
  912. while (*p && !isspace(*p) && *p != '\n')
  913. p++;
  914. while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  915. *p++ = '\0';
  916. }
  917. if (strcmp(argv[0], "nameserver") == 0) {
  918. for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
  919. __nameserver[__nameservers++] = strdup(argv[i]);
  920. DPRINTF("adding nameserver %s\n", argv[i]);
  921. }
  922. }
  923. /* domain and search are mutually exclusive, the last one wins */
  924. if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  925. while (__searchdomains > 0) {
  926. free(__searchdomain[--__searchdomains]);
  927. __searchdomain[__searchdomains] = NULL;
  928. }
  929. for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
  930. __searchdomain[__searchdomains++] = strdup(argv[i]);
  931. DPRINTF("adding search %s\n", argv[i]);
  932. }
  933. }
  934. }
  935. fclose(fp);
  936. DPRINTF("nameservers = %d\n", __nameservers);
  937. BIGUNLOCK;
  938. return 0;
  939. }
  940. DPRINTF("failed to open %s\n", "resolv.conf");
  941. h_errno = NO_RECOVERY;
  942. BIGUNLOCK;
  943. return -1;
  944. }
  945. #endif
  946. #ifdef L_closenameservers
  947. void attribute_hidden __close_nameservers(void)
  948. {
  949. BIGLOCK;
  950. while (__nameservers > 0) {
  951. free(__nameserver[--__nameservers]);
  952. __nameserver[__nameservers] = NULL;
  953. }
  954. while (__searchdomains > 0) {
  955. free(__searchdomain[--__searchdomains]);
  956. __searchdomain[__searchdomains] = NULL;
  957. }
  958. BIGUNLOCK;
  959. }
  960. #endif
  961. #ifdef L_gethostbyname
  962. struct hostent *gethostbyname(const char *name)
  963. {
  964. static struct hostent h;
  965. static char buf[sizeof(struct in_addr) +
  966. sizeof(struct in_addr *)*2 +
  967. sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
  968. struct hostent *hp;
  969. gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
  970. return hp;
  971. }
  972. libc_hidden_def(gethostbyname)
  973. #endif
  974. #ifdef L_gethostbyname2
  975. struct hostent *gethostbyname2(const char *name, int family)
  976. {
  977. #ifndef __UCLIBC_HAS_IPV6__
  978. return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
  979. #else /* __UCLIBC_HAS_IPV6__ */
  980. static struct hostent h;
  981. static char buf[sizeof(struct in6_addr) +
  982. sizeof(struct in6_addr *)*2 +
  983. sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
  984. struct hostent *hp;
  985. gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
  986. return hp;
  987. #endif /* __UCLIBC_HAS_IPV6__ */
  988. }
  989. #endif
  990. #ifdef L_res_init
  991. struct __res_state _res;
  992. int res_init(void)
  993. {
  994. struct __res_state *rp = &(_res);
  995. __close_nameservers();
  996. __open_nameservers();
  997. rp->retrans = RES_TIMEOUT;
  998. rp->retry = 4;
  999. rp->options = RES_INIT;
  1000. rp->id = (u_int) random();
  1001. rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
  1002. rp->nsaddr.sin_family = AF_INET;
  1003. rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
  1004. rp->ndots = 1;
  1005. /** rp->pfcode = 0; **/
  1006. rp->_vcsock = -1;
  1007. /** rp->_flags = 0; **/
  1008. /** rp->qhook = NULL; **/
  1009. /** rp->rhook = NULL; **/
  1010. /** rp->_u._ext.nsinit = 0; **/
  1011. BIGLOCK;
  1012. if(__searchdomains) {
  1013. int i;
  1014. for(i=0; i<__searchdomains; i++) {
  1015. rp->dnsrch[i] = __searchdomain[i];
  1016. }
  1017. }
  1018. if(__nameservers) {
  1019. int i;
  1020. struct in_addr a;
  1021. for(i=0; i<__nameservers; i++) {
  1022. if (inet_aton(__nameserver[i], &a)) {
  1023. rp->nsaddr_list[i].sin_addr = a;
  1024. rp->nsaddr_list[i].sin_family = AF_INET;
  1025. rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
  1026. }
  1027. }
  1028. }
  1029. rp->nscount = __nameservers;
  1030. BIGUNLOCK;
  1031. return(0);
  1032. }
  1033. libc_hidden_def(res_init)
  1034. void res_close( void )
  1035. {
  1036. return;
  1037. }
  1038. #endif
  1039. #ifdef L_res_query
  1040. #ifndef MIN
  1041. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  1042. #endif
  1043. int res_query(const char *dname, int class, int type,
  1044. unsigned char *answer, int anslen)
  1045. {
  1046. int i;
  1047. unsigned char * packet = 0;
  1048. struct resolv_answer a;
  1049. int __nameserversXX;
  1050. char ** __nameserverXX;
  1051. __open_nameservers();
  1052. if (!dname || class != 1 /* CLASS_IN */) {
  1053. h_errno = NO_RECOVERY;
  1054. return(-1);
  1055. }
  1056. memset((char *) &a, '\0', sizeof(a));
  1057. BIGLOCK;
  1058. __nameserversXX=__nameservers;
  1059. __nameserverXX=__nameserver;
  1060. BIGUNLOCK;
  1061. i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
  1062. if (i < 0) {
  1063. h_errno = TRY_AGAIN;
  1064. return(-1);
  1065. }
  1066. free(a.dotted);
  1067. if (a.atype == type) { /* CNAME*/
  1068. int len = MIN(anslen, i);
  1069. memcpy(answer, packet, len);
  1070. if (packet)
  1071. free(packet);
  1072. return(len);
  1073. }
  1074. if (packet)
  1075. free(packet);
  1076. return i;
  1077. }
  1078. libc_hidden_def(res_query)
  1079. /*
  1080. * Formulate a normal query, send, and retrieve answer in supplied buffer.
  1081. * Return the size of the response on success, -1 on error.
  1082. * If enabled, implement search rules until answer or unrecoverable failure
  1083. * is detected. Error code, if any, is left in h_errno.
  1084. */
  1085. int res_search(name, class, type, answer, anslen)
  1086. const char *name; /* domain name */
  1087. int class, type; /* class and type of query */
  1088. u_char *answer; /* buffer to put answer */
  1089. int anslen; /* size of answer */
  1090. {
  1091. const char *cp, * const *domain;
  1092. HEADER *hp = (HEADER *)(void *)answer;
  1093. u_int dots;
  1094. int trailing_dot, ret, saved_herrno;
  1095. int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
  1096. if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) {
  1097. h_errno = NETDB_INTERNAL;
  1098. return (-1);
  1099. }
  1100. errno = 0;
  1101. h_errno = HOST_NOT_FOUND; /* default, if we never query */
  1102. dots = 0;
  1103. for (cp = name; *cp; cp++)
  1104. dots += (*cp == '.');
  1105. trailing_dot = 0;
  1106. if (cp > name && *--cp == '.')
  1107. trailing_dot++;
  1108. /*
  1109. * If there are dots in the name already, let's just give it a try
  1110. * 'as is'. The threshold can be set with the "ndots" option.
  1111. */
  1112. saved_herrno = -1;
  1113. if (dots >= _res.ndots) {
  1114. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1115. if (ret > 0)
  1116. return (ret);
  1117. saved_herrno = h_errno;
  1118. tried_as_is++;
  1119. }
  1120. /*
  1121. * We do at least one level of search if
  1122. * - there is no dot and RES_DEFNAME is set, or
  1123. * - there is at least one dot, there is no trailing dot,
  1124. * and RES_DNSRCH is set.
  1125. */
  1126. if ((!dots && (_res.options & RES_DEFNAMES)) ||
  1127. (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
  1128. int done = 0;
  1129. for (domain = (const char * const *)_res.dnsrch;
  1130. *domain && !done;
  1131. domain++) {
  1132. ret = res_querydomain(name, *domain, class, type,
  1133. answer, anslen);
  1134. if (ret > 0)
  1135. return (ret);
  1136. /*
  1137. * If no server present, give up.
  1138. * If name isn't found in this domain,
  1139. * keep trying higher domains in the search list
  1140. * (if that's enabled).
  1141. * On a NO_DATA error, keep trying, otherwise
  1142. * a wildcard entry of another type could keep us
  1143. * from finding this entry higher in the domain.
  1144. * If we get some other error (negative answer or
  1145. * server failure), then stop searching up,
  1146. * but try the input name below in case it's
  1147. * fully-qualified.
  1148. */
  1149. if (errno == ECONNREFUSED) {
  1150. h_errno = TRY_AGAIN;
  1151. return (-1);
  1152. }
  1153. switch (h_errno) {
  1154. case NO_DATA:
  1155. got_nodata++;
  1156. /* FALLTHROUGH */
  1157. case HOST_NOT_FOUND:
  1158. /* keep trying */
  1159. break;
  1160. case TRY_AGAIN:
  1161. if (hp->rcode == SERVFAIL) {
  1162. /* try next search element, if any */
  1163. got_servfail++;
  1164. break;
  1165. }
  1166. /* FALLTHROUGH */
  1167. default:
  1168. /* anything else implies that we're done */
  1169. done++;
  1170. }
  1171. /*
  1172. * if we got here for some reason other than DNSRCH,
  1173. * we only wanted one iteration of the loop, so stop.
  1174. */
  1175. if (!(_res.options & RES_DNSRCH))
  1176. done++;
  1177. }
  1178. }
  1179. /*
  1180. * if we have not already tried the name "as is", do that now.
  1181. * note that we do this regardless of how many dots were in the
  1182. * name or whether it ends with a dot.
  1183. */
  1184. if (!tried_as_is) {
  1185. ret = res_querydomain(name, NULL, class, type, answer, anslen);
  1186. if (ret > 0)
  1187. return (ret);
  1188. }
  1189. /*
  1190. * if we got here, we didn't satisfy the search.
  1191. * if we did an initial full query, return that query's h_errno
  1192. * (note that we wouldn't be here if that query had succeeded).
  1193. * else if we ever got a nodata, send that back as the reason.
  1194. * else send back meaningless h_errno, that being the one from
  1195. * the last DNSRCH we did.
  1196. */
  1197. if (saved_herrno != -1)
  1198. h_errno = saved_herrno;
  1199. else if (got_nodata)
  1200. h_errno = NO_DATA;
  1201. else if (got_servfail)
  1202. h_errno = TRY_AGAIN;
  1203. return (-1);
  1204. }
  1205. /*
  1206. * Perform a call on res_query on the concatenation of name and domain,
  1207. * removing a trailing dot from name if domain is NULL.
  1208. */
  1209. int res_querydomain(name, domain, class, type, answer, anslen)
  1210. const char *name, *domain;
  1211. int class, type; /* class and type of query */
  1212. u_char *answer; /* buffer to put answer */
  1213. int anslen; /* size of answer */
  1214. {
  1215. char nbuf[MAXDNAME];
  1216. const char *longname = nbuf;
  1217. size_t n, d;
  1218. if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) {
  1219. h_errno = NETDB_INTERNAL;
  1220. return (-1);
  1221. }
  1222. #ifdef DEBUG
  1223. if (_res.options & RES_DEBUG)
  1224. printf(";; res_querydomain(%s, %s, %d, %d)\n",
  1225. name, domain?domain:"<Nil>", class, type);
  1226. #endif
  1227. if (domain == NULL) {
  1228. /*
  1229. * Check for trailing '.';
  1230. * copy without '.' if present.
  1231. */
  1232. n = strlen(name);
  1233. if (n + 1 > sizeof(nbuf)) {
  1234. h_errno = NO_RECOVERY;
  1235. return (-1);
  1236. }
  1237. if (n > 0 && name[--n] == '.') {
  1238. strncpy(nbuf, name, n);
  1239. nbuf[n] = '\0';
  1240. } else
  1241. longname = name;
  1242. } else {
  1243. n = strlen(name);
  1244. d = strlen(domain);
  1245. if (n + 1 + d + 1 > sizeof(nbuf)) {
  1246. h_errno = NO_RECOVERY;
  1247. return (-1);
  1248. }
  1249. snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
  1250. }
  1251. return (res_query(longname, class, type, answer, anslen));
  1252. }
  1253. libc_hidden_def(res_querydomain)
  1254. /* res_mkquery */
  1255. /* res_send */
  1256. /* dn_comp */
  1257. /* dn_expand */
  1258. #endif
  1259. #ifdef L_gethostbyaddr
  1260. struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
  1261. {
  1262. static struct hostent h;
  1263. static char buf[
  1264. #ifndef __UCLIBC_HAS_IPV6__
  1265. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1266. #else
  1267. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1268. #endif /* __UCLIBC_HAS_IPV6__ */
  1269. sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
  1270. struct hostent *hp;
  1271. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  1272. return hp;
  1273. }
  1274. libc_hidden_def(gethostbyaddr)
  1275. #endif
  1276. #ifdef L_read_etc_hosts_r
  1277. void attribute_hidden __open_etc_hosts(FILE **fp)
  1278. {
  1279. if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
  1280. *fp = fopen("/etc/config/hosts", "r");
  1281. }
  1282. return;
  1283. }
  1284. int attribute_hidden __read_etc_hosts_r(FILE * fp, const char * name, int type,
  1285. enum etc_hosts_action action,
  1286. struct hostent * result_buf,
  1287. char * buf, size_t buflen,
  1288. struct hostent ** result,
  1289. int * h_errnop)
  1290. {
  1291. struct in_addr *in=NULL;
  1292. struct in_addr **addr_list=NULL;
  1293. #ifdef __UCLIBC_HAS_IPV6__
  1294. struct in6_addr *in6=NULL;
  1295. struct in6_addr **addr_list6=NULL;
  1296. #endif /* __UCLIBC_HAS_IPV6__ */
  1297. char *cp, **alias;
  1298. int aliases, i, ret=HOST_NOT_FOUND;
  1299. if (buflen < sizeof(char *)*(ALIAS_DIM))
  1300. return ERANGE;
  1301. alias=(char **)buf;
  1302. buf+=sizeof(char **)*(ALIAS_DIM);
  1303. buflen-=sizeof(char **)*(ALIAS_DIM);
  1304. if (action!=GETHOSTENT) {
  1305. #ifdef __UCLIBC_HAS_IPV6__
  1306. char *p=buf;
  1307. size_t len=buflen;
  1308. #endif /* __UCLIBC_HAS_IPV6__ */
  1309. *h_errnop=NETDB_INTERNAL;
  1310. if (buflen < sizeof(*in))
  1311. return ERANGE;
  1312. in=(struct in_addr*)buf;
  1313. buf+=sizeof(*in);
  1314. buflen-=sizeof(*in);
  1315. if (buflen < sizeof(*addr_list)*2)
  1316. return ERANGE;
  1317. addr_list=(struct in_addr **)buf;
  1318. buf+=sizeof(*addr_list)*2;
  1319. buflen-=sizeof(*addr_list)*2;
  1320. #ifdef __UCLIBC_HAS_IPV6__
  1321. if (len < sizeof(*in6))
  1322. return ERANGE;
  1323. in6=(struct in6_addr*)p;
  1324. p+=sizeof(*in6);
  1325. len-=sizeof(*in6);
  1326. if (len < sizeof(*addr_list6)*2)
  1327. return ERANGE;
  1328. addr_list6=(struct in6_addr**)p;
  1329. p+=sizeof(*addr_list6)*2;
  1330. len-=sizeof(*addr_list6)*2;
  1331. if (len < buflen) {
  1332. buflen=len;
  1333. buf=p;
  1334. }
  1335. #endif /* __UCLIBC_HAS_IPV6__ */
  1336. if (buflen < 80)
  1337. return ERANGE;
  1338. __open_etc_hosts(&fp);
  1339. if (fp == NULL) {
  1340. result=NULL;
  1341. return errno;
  1342. }
  1343. }
  1344. *h_errnop=HOST_NOT_FOUND;
  1345. while (fgets(buf, buflen, fp)) {
  1346. if ((cp = strchr(buf, '#')))
  1347. *cp = '\0';
  1348. DPRINTF("Looking at: %s\n", buf);
  1349. aliases = 0;
  1350. cp = buf;
  1351. while (*cp) {
  1352. while (*cp && isspace(*cp))
  1353. *cp++ = '\0';
  1354. if (!*cp)
  1355. continue;
  1356. if (aliases < (2+MAX_ALIASES))
  1357. alias[aliases++] = cp;
  1358. while (*cp && !isspace(*cp))
  1359. cp++;
  1360. }
  1361. alias[aliases] = 0;
  1362. if (aliases < 2)
  1363. continue; /* syntax error really */
  1364. if (action==GETHOSTENT) {
  1365. /* Return whatever the next entry happens to be. */
  1366. break;
  1367. } else if (action==GET_HOSTS_BYADDR) {
  1368. if (strcmp(name, alias[0]) != 0)
  1369. continue;
  1370. } else {
  1371. /* GET_HOSTS_BYNAME */
  1372. for (i = 1; i < aliases; i++)
  1373. if (strcasecmp(name, alias[i]) == 0)
  1374. break;
  1375. if (i >= aliases)
  1376. continue;
  1377. }
  1378. if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
  1379. DPRINTF("Found INET\n");
  1380. addr_list[0] = in;
  1381. addr_list[1] = 0;
  1382. result_buf->h_name = alias[1];
  1383. result_buf->h_addrtype = AF_INET;
  1384. result_buf->h_length = sizeof(*in);
  1385. result_buf->h_addr_list = (char**) addr_list;
  1386. result_buf->h_aliases = alias + 2;
  1387. *result=result_buf;
  1388. ret=NETDB_SUCCESS;
  1389. #ifdef __UCLIBC_HAS_IPV6__
  1390. } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
  1391. DPRINTF("Found INET6\n");
  1392. addr_list6[0] = in6;
  1393. addr_list6[1] = 0;
  1394. result_buf->h_name = alias[1];
  1395. result_buf->h_addrtype = AF_INET6;
  1396. result_buf->h_length = sizeof(*in6);
  1397. result_buf->h_addr_list = (char**) addr_list6;
  1398. result_buf->h_aliases = alias + 2;
  1399. *result=result_buf;
  1400. ret=NETDB_SUCCESS;
  1401. #endif /* __UCLIBC_HAS_IPV6__ */
  1402. } else {
  1403. DPRINTF("Error\n");
  1404. ret=TRY_AGAIN;
  1405. break; /* bad ip address */
  1406. }
  1407. if (action!=GETHOSTENT) {
  1408. fclose(fp);
  1409. }
  1410. return ret;
  1411. }
  1412. if (action!=GETHOSTENT) {
  1413. fclose(fp);
  1414. }
  1415. return ret;
  1416. }
  1417. #endif
  1418. #ifdef L_gethostent
  1419. static int __stay_open;
  1420. static FILE * __gethostent_fp;
  1421. void endhostent (void)
  1422. {
  1423. LOCK;
  1424. __stay_open = 0;
  1425. if (__gethostent_fp) {
  1426. fclose(__gethostent_fp);
  1427. }
  1428. UNLOCK;
  1429. }
  1430. void sethostent (int stay_open)
  1431. {
  1432. LOCK;
  1433. __stay_open = stay_open;
  1434. UNLOCK;
  1435. }
  1436. int gethostent_r(struct hostent *result_buf, char *buf, size_t buflen,
  1437. struct hostent **result, int *h_errnop)
  1438. {
  1439. int ret;
  1440. LOCK;
  1441. if (__gethostent_fp == NULL) {
  1442. __open_etc_hosts(&__gethostent_fp);
  1443. if (__gethostent_fp == NULL) {
  1444. UNLOCK;
  1445. *result=NULL;
  1446. return 0;
  1447. }
  1448. }
  1449. ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  1450. result_buf, buf, buflen, result, h_errnop);
  1451. if (__stay_open==0) {
  1452. fclose(__gethostent_fp);
  1453. }
  1454. UNLOCK;
  1455. return(ret);
  1456. }
  1457. libc_hidden_def(gethostent_r)
  1458. struct hostent *gethostent (void)
  1459. {
  1460. static struct hostent h;
  1461. static char buf[
  1462. #ifndef __UCLIBC_HAS_IPV6__
  1463. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1464. #else
  1465. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1466. #endif /* __UCLIBC_HAS_IPV6__ */
  1467. sizeof(char *)*(ALIAS_DIM) +
  1468. 80/*namebuffer*/ + 2/* margin */];
  1469. struct hostent *host;
  1470. LOCK;
  1471. gethostent_r(&h, buf, sizeof(buf), &host, &h_errno);
  1472. UNLOCK;
  1473. return(host);
  1474. }
  1475. #endif
  1476. #ifdef L_get_hosts_byname_r
  1477. int attribute_hidden __get_hosts_byname_r(const char * name, int type,
  1478. struct hostent * result_buf,
  1479. char * buf, size_t buflen,
  1480. struct hostent ** result,
  1481. int * h_errnop)
  1482. {
  1483. return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME,
  1484. result_buf, buf, buflen, result, h_errnop));
  1485. }
  1486. #endif
  1487. #ifdef L_get_hosts_byaddr_r
  1488. int attribute_hidden __get_hosts_byaddr_r(const char * addr, int len, int type,
  1489. struct hostent * result_buf,
  1490. char * buf, size_t buflen,
  1491. struct hostent ** result,
  1492. int * h_errnop)
  1493. {
  1494. #ifndef __UCLIBC_HAS_IPV6__
  1495. char ipaddr[INET_ADDRSTRLEN];
  1496. #else
  1497. char ipaddr[INET6_ADDRSTRLEN];
  1498. #endif /* __UCLIBC_HAS_IPV6__ */
  1499. switch (type) {
  1500. case AF_INET:
  1501. if (len != sizeof(struct in_addr))
  1502. return 0;
  1503. break;
  1504. #ifdef __UCLIBC_HAS_IPV6__
  1505. case AF_INET6:
  1506. if (len != sizeof(struct in6_addr))
  1507. return 0;
  1508. break;
  1509. #endif /* __UCLIBC_HAS_IPV6__ */
  1510. default:
  1511. return 0;
  1512. }
  1513. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1514. return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1515. result_buf, buf, buflen, result, h_errnop));
  1516. }
  1517. #endif
  1518. #ifdef L_getnameinfo
  1519. #ifndef min
  1520. # define min(x,y) (((x) > (y)) ? (y) : (x))
  1521. #endif /* min */
  1522. libc_hidden_proto(getnameinfo)
  1523. int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  1524. socklen_t hostlen, char *serv, socklen_t servlen,
  1525. unsigned int flags)
  1526. {
  1527. int serrno = errno;
  1528. int ok = 0;
  1529. struct hostent *h = NULL;
  1530. char domain[256];
  1531. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1532. return EAI_BADFLAGS;
  1533. if (sa == NULL || addrlen < sizeof (sa_family_t))
  1534. return EAI_FAMILY;
  1535. switch (sa->sa_family) {
  1536. case AF_LOCAL:
  1537. break;
  1538. case AF_INET:
  1539. if (addrlen < sizeof (struct sockaddr_in))
  1540. return EAI_FAMILY;
  1541. break;
  1542. #ifdef __UCLIBC_HAS_IPV6__
  1543. case AF_INET6:
  1544. if (addrlen < sizeof (struct sockaddr_in6))
  1545. return EAI_FAMILY;
  1546. break;
  1547. #endif /* __UCLIBC_HAS_IPV6__ */
  1548. default:
  1549. return EAI_FAMILY;
  1550. }
  1551. if (host != NULL && hostlen > 0)
  1552. switch (sa->sa_family) {
  1553. case AF_INET:
  1554. #ifdef __UCLIBC_HAS_IPV6__
  1555. case AF_INET6:
  1556. #endif /* __UCLIBC_HAS_IPV6__ */
  1557. if (!(flags & NI_NUMERICHOST)) {
  1558. #ifdef __UCLIBC_HAS_IPV6__
  1559. if (sa->sa_family == AF_INET6)
  1560. h = gethostbyaddr ((const void *)
  1561. &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1562. sizeof(struct in6_addr), AF_INET6);
  1563. else
  1564. #endif /* __UCLIBC_HAS_IPV6__ */
  1565. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1566. sizeof(struct in_addr), AF_INET);
  1567. if (h) {
  1568. char *c;
  1569. if ((flags & NI_NOFQDN)
  1570. && (getdomainname (domain, sizeof(domain)) == 0)
  1571. && (c = strstr (h->h_name, domain))
  1572. && (c != h->h_name) && (*(--c) == '.')) {
  1573. strncpy (host, h->h_name,
  1574. min(hostlen, (size_t) (c - h->h_name)));
  1575. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1576. ok = 1;
  1577. } else {
  1578. strncpy (host, h->h_name, hostlen);
  1579. ok = 1;
  1580. }
  1581. }
  1582. }
  1583. if (!ok) {
  1584. if (flags & NI_NAMEREQD) {
  1585. errno = serrno;
  1586. return EAI_NONAME;
  1587. } else {
  1588. const char *c;
  1589. #ifdef __UCLIBC_HAS_IPV6__
  1590. if (sa->sa_family == AF_INET6) {
  1591. const struct sockaddr_in6 *sin6p;
  1592. sin6p = (const struct sockaddr_in6 *) sa;
  1593. c = inet_ntop (AF_INET6,
  1594. (const void *) &sin6p->sin6_addr, host, hostlen);
  1595. #if 0
  1596. /* Does scope id need to be supported? */
  1597. uint32_t scopeid;
  1598. scopeid = sin6p->sin6_scope_id;
  1599. if (scopeid != 0) {
  1600. /* Buffer is >= IFNAMSIZ+1. */
  1601. char scopebuf[IFNAMSIZ + 1];
  1602. char *scopeptr;
  1603. int ni_numericscope = 0;
  1604. size_t real_hostlen = strnlen (host, hostlen);
  1605. size_t scopelen = 0;
  1606. scopebuf[0] = SCOPE_DELIMITER;
  1607. scopebuf[1] = '\0';
  1608. scopeptr = &scopebuf[1];
  1609. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1610. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1611. if (if_indextoname (scopeid, scopeptr) == NULL)
  1612. ++ni_numericscope;
  1613. else
  1614. scopelen = strlen (scopebuf);
  1615. } else {
  1616. ++ni_numericscope;
  1617. }
  1618. if (ni_numericscope)
  1619. scopelen = 1 + snprintf (scopeptr,
  1620. (scopebuf
  1621. + sizeof scopebuf
  1622. - scopeptr),
  1623. "%u", scopeid);
  1624. if (real_hostlen + scopelen + 1 > hostlen)
  1625. return EAI_SYSTEM;
  1626. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1627. }
  1628. #endif
  1629. } else
  1630. #endif /* __UCLIBC_HAS_IPV6__ */
  1631. c = inet_ntop (AF_INET, (const void *)
  1632. &(((const struct sockaddr_in *) sa)->sin_addr),
  1633. host, hostlen);
  1634. if (c == NULL) {
  1635. errno = serrno;
  1636. return EAI_SYSTEM;
  1637. }
  1638. }
  1639. ok = 1;
  1640. }
  1641. break;
  1642. case AF_LOCAL:
  1643. if (!(flags & NI_NUMERICHOST)) {
  1644. struct utsname utsname;
  1645. if (!uname (&utsname)) {
  1646. strncpy (host, utsname.nodename, hostlen);
  1647. break;
  1648. };
  1649. };
  1650. if (flags & NI_NAMEREQD) {
  1651. errno = serrno;
  1652. return EAI_NONAME;
  1653. }
  1654. strncpy (host, "localhost", hostlen);
  1655. break;
  1656. default:
  1657. return EAI_FAMILY;
  1658. }
  1659. if (serv && (servlen > 0)) {
  1660. switch (sa->sa_family) {
  1661. case AF_INET:
  1662. #ifdef __UCLIBC_HAS_IPV6__
  1663. case AF_INET6:
  1664. #endif /* __UCLIBC_HAS_IPV6__ */
  1665. if (!(flags & NI_NUMERICSERV)) {
  1666. struct servent *s;
  1667. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1668. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1669. if (s) {
  1670. strncpy (serv, s->s_name, servlen);
  1671. break;
  1672. }
  1673. }
  1674. snprintf (serv, servlen, "%d",
  1675. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1676. break;
  1677. case AF_LOCAL:
  1678. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1679. break;
  1680. }
  1681. }
  1682. if (host && (hostlen > 0))
  1683. host[hostlen-1] = 0;
  1684. if (serv && (servlen > 0))
  1685. serv[servlen-1] = 0;
  1686. errno = serrno;
  1687. return 0;
  1688. }
  1689. libc_hidden_def(getnameinfo)
  1690. #endif
  1691. #ifdef L_gethostbyname_r
  1692. int gethostbyname_r(const char * name,
  1693. struct hostent * result_buf,
  1694. char * buf, size_t buflen,
  1695. struct hostent ** result,
  1696. int * h_errnop)
  1697. {
  1698. struct in_addr *in;
  1699. struct in_addr **addr_list;
  1700. char **alias;
  1701. unsigned char *packet;
  1702. struct resolv_answer a;
  1703. int i;
  1704. int __nameserversXX;
  1705. char ** __nameserverXX;
  1706. __open_nameservers();
  1707. *result=NULL;
  1708. if (!name)
  1709. return EINVAL;
  1710. /* do /etc/hosts first */
  1711. {
  1712. int old_errno = errno; /* Save the old errno and reset errno */
  1713. __set_errno(0); /* to check for missing /etc/hosts. */
  1714. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1715. buf, buflen, result, h_errnop))==0)
  1716. return i;
  1717. switch (*h_errnop) {
  1718. case HOST_NOT_FOUND:
  1719. case NO_ADDRESS:
  1720. break;
  1721. case NETDB_INTERNAL:
  1722. if (errno == ENOENT) {
  1723. break;
  1724. }
  1725. /* else fall through */
  1726. default:
  1727. return i;
  1728. }
  1729. __set_errno(old_errno);
  1730. }
  1731. DPRINTF("Nothing found in /etc/hosts\n");
  1732. *h_errnop = NETDB_INTERNAL;
  1733. if (buflen < sizeof(*in))
  1734. return ERANGE;
  1735. in=(struct in_addr*)buf;
  1736. buf+=sizeof(*in);
  1737. buflen-=sizeof(*in);
  1738. if (buflen < sizeof(*addr_list)*2)
  1739. return ERANGE;
  1740. addr_list=(struct in_addr**)buf;
  1741. buf+=sizeof(*addr_list)*2;
  1742. buflen-=sizeof(*addr_list)*2;
  1743. addr_list[0] = in;
  1744. addr_list[1] = 0;
  1745. if (buflen < sizeof(char *)*(ALIAS_DIM))
  1746. return ERANGE;
  1747. alias=(char **)buf;
  1748. buf+=sizeof(char **)*(ALIAS_DIM);
  1749. buflen-=sizeof(char **)*(ALIAS_DIM);
  1750. if (buflen<256)
  1751. return ERANGE;
  1752. strncpy(buf, name, buflen);
  1753. alias[0] = buf;
  1754. alias[1] = NULL;
  1755. /* First check if this is already an address */
  1756. if (inet_aton(name, in)) {
  1757. result_buf->h_name = buf;
  1758. result_buf->h_addrtype = AF_INET;
  1759. result_buf->h_length = sizeof(*in);
  1760. result_buf->h_addr_list = (char **) addr_list;
  1761. result_buf->h_aliases = alias;
  1762. *result=result_buf;
  1763. *h_errnop = NETDB_SUCCESS;
  1764. return NETDB_SUCCESS;
  1765. }
  1766. for (;;) {
  1767. BIGLOCK;
  1768. __nameserversXX=__nameservers;
  1769. __nameserverXX=__nameserver;
  1770. BIGUNLOCK;
  1771. a.buf = buf;
  1772. a.buflen = buflen;
  1773. a.add_count = 0;
  1774. i = __dns_lookup(name, T_A, __nameserversXX, __nameserverXX, &packet, &a);
  1775. if (i < 0) {
  1776. *h_errnop = HOST_NOT_FOUND;
  1777. DPRINTF("__dns_lookup\n");
  1778. return TRY_AGAIN;
  1779. }
  1780. if ((a.rdlength + sizeof(struct in_addr*)) * a.add_count + 256 > buflen)
  1781. {
  1782. free(a.dotted);
  1783. free(packet);
  1784. *h_errnop = NETDB_INTERNAL;
  1785. DPRINTF("buffer too small for all addresses\n");
  1786. return ERANGE;
  1787. }
  1788. else if(a.add_count > 0)
  1789. {
  1790. memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
  1791. addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
  1792. addr_list[0] = in;
  1793. for (i = a.add_count-1; i>=0; --i)
  1794. addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
  1795. addr_list[a.add_count + 1] = 0;
  1796. buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
  1797. buf = (char*)&addr_list[a.add_count + 2];
  1798. }
  1799. strncpy(buf, a.dotted, buflen);
  1800. free(a.dotted);
  1801. if (a.atype == T_A) { /* ADDRESS */
  1802. memcpy(in, a.rdata, sizeof(*in));
  1803. result_buf->h_name = buf;
  1804. result_buf->h_addrtype = AF_INET;
  1805. result_buf->h_length = sizeof(*in);
  1806. result_buf->h_addr_list = (char **) addr_list;
  1807. #ifdef __UCLIBC_MJN3_ONLY__
  1808. #warning TODO -- generate the full list
  1809. #endif
  1810. result_buf->h_aliases = alias; /* TODO: generate the full list */
  1811. free(packet);
  1812. break;
  1813. } else {
  1814. free(packet);
  1815. *h_errnop=HOST_NOT_FOUND;
  1816. return TRY_AGAIN;
  1817. }
  1818. }
  1819. *result=result_buf;
  1820. *h_errnop = NETDB_SUCCESS;
  1821. return NETDB_SUCCESS;
  1822. }
  1823. libc_hidden_def(gethostbyname_r)
  1824. #endif
  1825. #ifdef L_gethostbyname2_r
  1826. int gethostbyname2_r(const char *name, int family,
  1827. struct hostent * result_buf,
  1828. char * buf, size_t buflen,
  1829. struct hostent ** result,
  1830. int * h_errnop)
  1831. {
  1832. #ifndef __UCLIBC_HAS_IPV6__
  1833. return family == (AF_INET)? gethostbyname_r(name, result_buf,
  1834. buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1835. #else /* __UCLIBC_HAS_IPV6__ */
  1836. struct in6_addr *in;
  1837. struct in6_addr **addr_list;
  1838. unsigned char *packet;
  1839. struct resolv_answer a;
  1840. int i;
  1841. int nest = 0;
  1842. int __nameserversXX;
  1843. char ** __nameserverXX;
  1844. if (family == AF_INET)
  1845. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1846. if (family != AF_INET6)
  1847. return EINVAL;
  1848. __open_nameservers();
  1849. *result=NULL;
  1850. if (!name)
  1851. return EINVAL;
  1852. /* do /etc/hosts first */
  1853. {
  1854. int old_errno = errno; /* Save the old errno and reset errno */
  1855. __set_errno(0); /* to check for missing /etc/hosts. */
  1856. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1857. buf, buflen, result, h_errnop))==0)
  1858. return i;
  1859. switch (*h_errnop) {
  1860. case HOST_NOT_FOUND:
  1861. case NO_ADDRESS:
  1862. break;
  1863. case NETDB_INTERNAL:
  1864. if (errno == ENOENT) {
  1865. break;
  1866. }
  1867. /* else fall through */
  1868. default:
  1869. return i;
  1870. }
  1871. __set_errno(old_errno);
  1872. }
  1873. DPRINTF("Nothing found in /etc/hosts\n");
  1874. *h_errnop = NETDB_INTERNAL;
  1875. if (buflen < sizeof(*in))
  1876. return ERANGE;
  1877. in=(struct in6_addr*)buf;
  1878. buf+=sizeof(*in);
  1879. buflen-=sizeof(*in);
  1880. if (buflen < sizeof(*addr_list)*2)
  1881. return ERANGE;
  1882. addr_list=(struct in6_addr**)buf;
  1883. buf+=sizeof(*addr_list)*2;
  1884. buflen-=sizeof(*addr_list)*2;
  1885. addr_list[0] = in;
  1886. addr_list[1] = 0;
  1887. if (buflen<256)
  1888. return ERANGE;
  1889. strncpy(buf, name, buflen);
  1890. /* First check if this is already an address */
  1891. if (inet_pton(AF_INET6, name, in)) {
  1892. result_buf->h_name = buf;
  1893. result_buf->h_addrtype = AF_INET6;
  1894. result_buf->h_length = sizeof(*in);
  1895. result_buf->h_addr_list = (char **) addr_list;
  1896. *result=result_buf;
  1897. *h_errnop = NETDB_SUCCESS;
  1898. return NETDB_SUCCESS;
  1899. }
  1900. memset((char *) &a, '\0', sizeof(a));
  1901. for (;;) {
  1902. BIGLOCK;
  1903. __nameserversXX=__nameservers;
  1904. __nameserverXX=__nameserver;
  1905. BIGUNLOCK;
  1906. i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
  1907. if (i < 0) {
  1908. *h_errnop = HOST_NOT_FOUND;
  1909. return TRY_AGAIN;
  1910. }
  1911. strncpy(buf, a.dotted, buflen);
  1912. free(a.dotted);
  1913. if (a.atype == T_CNAME) { /* CNAME */
  1914. DPRINTF("Got a CNAME in gethostbyname()\n");
  1915. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1916. free(packet);
  1917. if (i < 0) {
  1918. *h_errnop = NO_RECOVERY;
  1919. return -1;
  1920. }
  1921. if (++nest > MAX_RECURSE) {
  1922. *h_errnop = NO_RECOVERY;
  1923. return -1;
  1924. }
  1925. continue;
  1926. } else if (a.atype == T_AAAA) { /* ADDRESS */
  1927. memcpy(in, a.rdata, sizeof(*in));
  1928. result_buf->h_name = buf;
  1929. result_buf->h_addrtype = AF_INET6;
  1930. result_buf->h_length = sizeof(*in);
  1931. result_buf->h_addr_list = (char **) addr_list;
  1932. free(packet);
  1933. break;
  1934. } else {
  1935. free(packet);
  1936. *h_errnop=HOST_NOT_FOUND;
  1937. return TRY_AGAIN;
  1938. }
  1939. }
  1940. *result=result_buf;
  1941. *h_errnop = NETDB_SUCCESS;
  1942. return NETDB_SUCCESS;
  1943. #endif /* __UCLIBC_HAS_IPV6__ */
  1944. }
  1945. libc_hidden_def(gethostbyname2_r)
  1946. #endif
  1947. #ifdef L_gethostbyaddr_r
  1948. int gethostbyaddr_r (const void *addr, socklen_t len, int type,
  1949. struct hostent * result_buf,
  1950. char * buf, size_t buflen,
  1951. struct hostent ** result,
  1952. int * h_errnop)
  1953. {
  1954. struct in_addr *in;
  1955. struct in_addr **addr_list;
  1956. #ifdef __UCLIBC_HAS_IPV6__
  1957. char *qp;
  1958. size_t plen;
  1959. struct in6_addr *in6;
  1960. struct in6_addr **addr_list6;
  1961. #endif /* __UCLIBC_HAS_IPV6__ */
  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. #ifdef __UCLIBC_HAS_IPV6__
  2014. if (plen < sizeof(*in6))
  2015. return ERANGE;
  2016. in6=(struct in6_addr*)qp;
  2017. qp+=sizeof(*in6);
  2018. plen-=sizeof(*in6);
  2019. if (plen < sizeof(*addr_list6)*2)
  2020. return ERANGE;
  2021. addr_list6=(struct in6_addr**)qp;
  2022. qp+=sizeof(*addr_list6)*2;
  2023. plen-=sizeof(*addr_list6)*2;
  2024. if (plen < buflen) {
  2025. buflen=plen;
  2026. buf=qp;
  2027. }
  2028. #endif /* __UCLIBC_HAS_IPV6__ */
  2029. if (buflen<256)
  2030. return ERANGE;
  2031. if(type == AF_INET) {
  2032. unsigned char *tmp_addr = (unsigned char *)addr;
  2033. memcpy(&in->s_addr, addr, len);
  2034. addr_list[0] = in;
  2035. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  2036. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  2037. #ifdef __UCLIBC_HAS_IPV6__
  2038. } else {
  2039. memcpy(in6->s6_addr, addr, len);
  2040. addr_list6[0] = in6;
  2041. qp = buf;
  2042. for (i = len - 1; i >= 0; i--) {
  2043. qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
  2044. (in6->s6_addr[i] >> 4) & 0xf);
  2045. }
  2046. strcpy(qp, "ip6.int");
  2047. #endif /* __UCLIBC_HAS_IPV6__ */
  2048. }
  2049. addr_list[1] = 0;
  2050. for (;;) {
  2051. BIGLOCK;
  2052. __nameserversXX=__nameservers;
  2053. __nameserverXX=__nameserver;
  2054. BIGUNLOCK;
  2055. i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
  2056. if (i < 0) {
  2057. *h_errnop = HOST_NOT_FOUND;
  2058. return TRY_AGAIN;
  2059. }
  2060. strncpy(buf, a.dotted, buflen);
  2061. free(a.dotted);
  2062. if (a.atype == T_CNAME) { /* CNAME */
  2063. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  2064. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  2065. free(packet);
  2066. if (i < 0) {
  2067. *h_errnop = NO_RECOVERY;
  2068. return -1;
  2069. }
  2070. if (++nest > MAX_RECURSE) {
  2071. *h_errnop = NO_RECOVERY;
  2072. return -1;
  2073. }
  2074. continue;
  2075. } else if (a.atype == T_PTR) { /* ADDRESS */
  2076. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  2077. free(packet);
  2078. result_buf->h_name = buf;
  2079. result_buf->h_addrtype = type;
  2080. if(type == AF_INET) {
  2081. result_buf->h_length = sizeof(*in);
  2082. #ifdef __UCLIBC_HAS_IPV6__
  2083. } else {
  2084. result_buf->h_length = sizeof(*in6);
  2085. #endif /* __UCLIBC_HAS_IPV6__ */
  2086. }
  2087. result_buf->h_addr_list = (char **) addr_list;
  2088. break;
  2089. } else {
  2090. free(packet);
  2091. *h_errnop = NO_ADDRESS;
  2092. return TRY_AGAIN;
  2093. }
  2094. }
  2095. *result=result_buf;
  2096. *h_errnop = NETDB_SUCCESS;
  2097. return NETDB_SUCCESS;
  2098. }
  2099. libc_hidden_def(gethostbyaddr_r)
  2100. #endif
  2101. #ifdef L_res_comp
  2102. /*
  2103. * Expand compressed domain name 'comp_dn' to full domain name.
  2104. * 'msg' is a pointer to the begining of the message,
  2105. * 'eomorig' points to the first location after the message,
  2106. * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  2107. * Return size of compressed name or -1 if there was an error.
  2108. */
  2109. int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  2110. char *dst, int dstsiz)
  2111. {
  2112. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  2113. if (n > 0 && dst[0] == '.')
  2114. dst[0] = '\0';
  2115. return (n);
  2116. }
  2117. #endif /* L_res_comp */
  2118. #ifdef L_ns_name
  2119. /*
  2120. * printable(ch)
  2121. * Thinking in noninternationalized USASCII (per the DNS spec),
  2122. * is this character visible and not a space when printed ?
  2123. * return:
  2124. * boolean.
  2125. */
  2126. static int printable(int ch)
  2127. {
  2128. return (ch > 0x20 && ch < 0x7f);
  2129. }
  2130. /*
  2131. * special(ch)
  2132. * Thinking in noninternationalized USASCII (per the DNS spec),
  2133. * is this characted special ("in need of quoting") ?
  2134. * return:
  2135. * boolean.
  2136. */
  2137. static int special(int ch)
  2138. {
  2139. switch (ch) {
  2140. case 0x22: /* '"' */
  2141. case 0x2E: /* '.' */
  2142. case 0x3B: /* ';' */
  2143. case 0x5C: /* '\\' */
  2144. /* Special modifiers in zone files. */
  2145. case 0x40: /* '@' */
  2146. case 0x24: /* '$' */
  2147. return (1);
  2148. default:
  2149. return (0);
  2150. }
  2151. }
  2152. /*
  2153. * ns_name_uncompress(msg, eom, src, dst, dstsiz)
  2154. * Expand compressed domain name to presentation format.
  2155. * return:
  2156. * Number of bytes read out of `src', or -1 (with errno set).
  2157. * note:
  2158. * Root domain returns as "." not "".
  2159. */
  2160. int ns_name_uncompress(const u_char *msg, const u_char *eom,
  2161. const u_char *src, char *dst, size_t dstsiz)
  2162. {
  2163. u_char tmp[NS_MAXCDNAME];
  2164. int n;
  2165. if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
  2166. return (-1);
  2167. if (ns_name_ntop(tmp, dst, dstsiz) == -1)
  2168. return (-1);
  2169. return (n);
  2170. }
  2171. libc_hidden_def(ns_name_uncompress)
  2172. /*
  2173. * ns_name_ntop(src, dst, dstsiz)
  2174. * Convert an encoded domain name to printable ascii as per RFC1035.
  2175. * return:
  2176. * Number of bytes written to buffer, or -1 (with errno set)
  2177. * notes:
  2178. * The root is returned as "."
  2179. * All other domains are returned in non absolute form
  2180. */
  2181. int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
  2182. const u_char *cp;
  2183. char *dn, *eom;
  2184. u_char c;
  2185. u_int n;
  2186. const char digits[] = "0123456789";
  2187. cp = src;
  2188. dn = dst;
  2189. eom = dst + dstsiz;
  2190. while ((n = *cp++) != 0) {
  2191. if ((n & NS_CMPRSFLGS) != 0) {
  2192. /* Some kind of compression pointer. */
  2193. __set_errno (EMSGSIZE);
  2194. return (-1);
  2195. }
  2196. if (dn != dst) {
  2197. if (dn >= eom) {
  2198. __set_errno (EMSGSIZE);
  2199. return (-1);
  2200. }
  2201. *dn++ = '.';
  2202. }
  2203. if (dn + n >= eom) {
  2204. __set_errno (EMSGSIZE);
  2205. return (-1);
  2206. }
  2207. for ((void)NULL; n > 0; n--) {
  2208. c = *cp++;
  2209. if (special(c)) {
  2210. if (dn + 1 >= eom) {
  2211. __set_errno (EMSGSIZE);
  2212. return (-1);
  2213. }
  2214. *dn++ = '\\';
  2215. *dn++ = (char)c;
  2216. } else if (!printable(c)) {
  2217. if (dn + 3 >= eom) {
  2218. __set_errno (EMSGSIZE);
  2219. return (-1);
  2220. }
  2221. *dn++ = '\\';
  2222. *dn++ = digits[c / 100];
  2223. *dn++ = digits[(c % 100) / 10];
  2224. *dn++ = digits[c % 10];
  2225. } else {
  2226. if (dn >= eom) {
  2227. __set_errno (EMSGSIZE);
  2228. return (-1);
  2229. }
  2230. *dn++ = (char)c;
  2231. }
  2232. }
  2233. }
  2234. if (dn == dst) {
  2235. if (dn >= eom) {
  2236. __set_errno (EMSGSIZE);
  2237. return (-1);
  2238. }
  2239. *dn++ = '.';
  2240. }
  2241. if (dn >= eom) {
  2242. __set_errno (EMSGSIZE);
  2243. return (-1);
  2244. }
  2245. *dn++ = '\0';
  2246. return (dn - dst);
  2247. }
  2248. libc_hidden_def(ns_name_ntop)
  2249. /*
  2250. * ns_name_unpack(msg, eom, src, dst, dstsiz)
  2251. * Unpack a domain name from a message, source may be compressed.
  2252. * return:
  2253. * -1 if it fails, or consumed octets if it succeeds.
  2254. */
  2255. int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
  2256. u_char *dst, size_t dstsiz)
  2257. {
  2258. const u_char *srcp, *dstlim;
  2259. u_char *dstp;
  2260. int n, len, checked;
  2261. len = -1;
  2262. checked = 0;
  2263. dstp = dst;
  2264. srcp = src;
  2265. dstlim = dst + dstsiz;
  2266. if (srcp < msg || srcp >= eom) {
  2267. __set_errno (EMSGSIZE);
  2268. return (-1);
  2269. }
  2270. /* Fetch next label in domain name. */
  2271. while ((n = *srcp++) != 0) {
  2272. /* Check for indirection. */
  2273. switch (n & NS_CMPRSFLGS) {
  2274. case 0:
  2275. /* Limit checks. */
  2276. if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  2277. __set_errno (EMSGSIZE);
  2278. return (-1);
  2279. }
  2280. checked += n + 1;
  2281. *dstp++ = n;
  2282. memcpy(dstp, srcp, n);
  2283. dstp += n;
  2284. srcp += n;
  2285. break;
  2286. case NS_CMPRSFLGS:
  2287. if (srcp >= eom) {
  2288. __set_errno (EMSGSIZE);
  2289. return (-1);
  2290. }
  2291. if (len < 0)
  2292. len = srcp - src + 1;
  2293. srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  2294. if (srcp < msg || srcp >= eom) { /* Out of range. */
  2295. __set_errno (EMSGSIZE);
  2296. return (-1);
  2297. }
  2298. checked += 2;
  2299. /*
  2300. * Check for loops in the compressed name;
  2301. * if we've looked at the whole message,
  2302. * there must be a loop.
  2303. */
  2304. if (checked >= eom - msg) {
  2305. __set_errno (EMSGSIZE);
  2306. return (-1);
  2307. }
  2308. break;
  2309. default:
  2310. __set_errno (EMSGSIZE);
  2311. return (-1); /* flag error */
  2312. }
  2313. }
  2314. *dstp = '\0';
  2315. if (len < 0)
  2316. len = srcp - src;
  2317. return (len);
  2318. }
  2319. libc_hidden_def(ns_name_unpack)
  2320. #endif /* L_ns_name */