resolv.c 60 KB

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