resolv.c 61 KB

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