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