resolv.c 61 KB

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