resolv.c 63 KB

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