resolv.c 65 KB

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