resolv.c 62 KB

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