resolv.c 60 KB

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