resolv.c 66 KB

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