resolv.c 63 KB

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