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