resolv.c 63 KB

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