resolv.c 63 KB

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