resolv.c 62 KB

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