resolv.c 61 KB

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