resolv.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  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. * 5-Oct-2000 W. Greathouse wgreathouse@smva.com
  12. * Fix memory leak and memory corruption.
  13. * -- Every name resolution resulted in
  14. * a new parse of resolv.conf and new
  15. * copy of nameservers allocated by
  16. * strdup.
  17. * -- Every name resolution resulted in
  18. * a new read of resolv.conf without
  19. * resetting index from prior read...
  20. * resulting in exceeding array bounds.
  21. *
  22. * Limit nameservers read from resolv.conf
  23. *
  24. * Add "search" domains from resolv.conf
  25. *
  26. * Some systems will return a security
  27. * signature along with query answer for
  28. * dynamic DNS entries.
  29. * -- skip/ignore this answer
  30. *
  31. * Include arpa/nameser.h for defines.
  32. *
  33. * General cleanup
  34. *
  35. * 20-Jun-2001 Michal Moskal <malekith@pld.org.pl>
  36. * partial IPv6 support (i.e. gethostbyname2() and resolve_address2()
  37. * functions added), IPv6 nameservers are also supported.
  38. *
  39. * 6-Oct-2001 Jari Korva <jari.korva@iki.fi>
  40. * more IPv6 support (IPv6 support for gethostbyaddr();
  41. * address family parameter and improved IPv6 support for get_hosts_byname
  42. * and read_etc_hosts; getnameinfo() port from glibc; defined
  43. * defined ip6addr_any and in6addr_loopback)
  44. *
  45. * 2-Feb-2002 Erik Andersen <andersee@debian.org>
  46. * Added gethostent(), sethostent(), and endhostent()
  47. *
  48. */
  49. #define __FORCE_GLIBC
  50. #include <features.h>
  51. #include <string.h>
  52. #include <stdio.h>
  53. #include <signal.h>
  54. #include <errno.h>
  55. #include <sys/socket.h>
  56. #include <sys/types.h>
  57. #include <netinet/in.h>
  58. #include <arpa/inet.h>
  59. #include <stdlib.h>
  60. #include <unistd.h>
  61. #include <resolv.h>
  62. #include <netdb.h>
  63. #include <ctype.h>
  64. #include <arpa/nameser.h>
  65. #include <sys/utsname.h>
  66. #include <sys/un.h>
  67. #define MAX_RECURSE 5
  68. #define REPLY_TIMEOUT 10
  69. #define MAX_RETRIES 15
  70. #define MAX_SERVERS 3
  71. #define MAX_SEARCH 4
  72. #undef DEBUG
  73. /*#define DEBUG*/
  74. #ifdef DEBUG
  75. #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
  76. #else
  77. #define DPRINTF(X,args...)
  78. #endif /* DEBUG */
  79. /* Global stuff... */
  80. extern int nameservers;
  81. extern char * nameserver[MAX_SERVERS];
  82. extern int searchdomains;
  83. extern char * searchdomain[MAX_SEARCH];
  84. /* Structs */
  85. struct resolv_header {
  86. int id;
  87. int qr,opcode,aa,tc,rd,ra,rcode;
  88. int qdcount;
  89. int ancount;
  90. int nscount;
  91. int arcount;
  92. };
  93. struct resolv_question {
  94. char * dotted;
  95. int qtype;
  96. int qclass;
  97. };
  98. struct resolv_answer {
  99. char * dotted;
  100. int atype;
  101. int aclass;
  102. int ttl;
  103. int rdlength;
  104. unsigned char * rdata;
  105. int rdoffset;
  106. };
  107. enum etc_hosts_action {
  108. GET_HOSTS_BYNAME = 0,
  109. GETHOSTENT,
  110. GET_HOSTS_BYADDR,
  111. };
  112. /* function prototypes */
  113. extern int get_hosts_byname_r(const char * name, int type,
  114. struct hostent * result_buf,
  115. char * buf, size_t buflen,
  116. struct hostent ** result,
  117. int * h_errnop);
  118. extern int get_hosts_byaddr_r(const char * addr, int len, int type,
  119. struct hostent * result_buf,
  120. char * buf, size_t buflen,
  121. struct hostent ** result,
  122. int * h_errnop);
  123. extern void __open_etc_hosts(FILE **fp);
  124. extern int read_etc_hosts_r(FILE *fp, const char * name, int type,
  125. enum etc_hosts_action action,
  126. struct hostent * result_buf,
  127. char * buf, size_t buflen,
  128. struct hostent ** result,
  129. int * h_errnop);
  130. extern int dns_lookup(const char * name, int type, int nscount,
  131. char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
  132. extern int encode_dotted(const char * dotted, unsigned char * dest, int maxlen);
  133. extern int decode_dotted(const unsigned char * message, int offset,
  134. char * dest, int maxlen);
  135. extern int length_dotted(const unsigned char * message, int offset);
  136. extern int encode_header(struct resolv_header * h, unsigned char * dest, int maxlen);
  137. extern int decode_header(unsigned char * data, struct resolv_header * h);
  138. extern int encode_question(struct resolv_question * q,
  139. unsigned char * dest, int maxlen);
  140. extern int decode_question(unsigned char * message, int offset,
  141. struct resolv_question * q);
  142. extern int encode_answer(struct resolv_answer * a,
  143. unsigned char * dest, int maxlen);
  144. extern int decode_answer(unsigned char * message, int offset,
  145. struct resolv_answer * a);
  146. extern int length_question(unsigned char * message, int offset);
  147. extern int open_nameservers(void);
  148. #ifdef L_encodeh
  149. int encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
  150. {
  151. if (maxlen < HFIXEDSZ)
  152. return -1;
  153. dest[0] = (h->id & 0xff00) >> 8;
  154. dest[1] = (h->id & 0x00ff) >> 0;
  155. dest[2] = (h->qr ? 0x80 : 0) |
  156. ((h->opcode & 0x0f) << 3) |
  157. (h->aa ? 0x04 : 0) |
  158. (h->tc ? 0x02 : 0) |
  159. (h->rd ? 0x01 : 0);
  160. dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
  161. dest[4] = (h->qdcount & 0xff00) >> 8;
  162. dest[5] = (h->qdcount & 0x00ff) >> 0;
  163. dest[6] = (h->ancount & 0xff00) >> 8;
  164. dest[7] = (h->ancount & 0x00ff) >> 0;
  165. dest[8] = (h->nscount & 0xff00) >> 8;
  166. dest[9] = (h->nscount & 0x00ff) >> 0;
  167. dest[10] = (h->arcount & 0xff00) >> 8;
  168. dest[11] = (h->arcount & 0x00ff) >> 0;
  169. return HFIXEDSZ;
  170. }
  171. #endif
  172. #ifdef L_decodeh
  173. int decode_header(unsigned char *data, struct resolv_header *h)
  174. {
  175. h->id = (data[0] << 8) | data[1];
  176. h->qr = (data[2] & 0x80) ? 1 : 0;
  177. h->opcode = (data[2] >> 3) & 0x0f;
  178. h->aa = (data[2] & 0x04) ? 1 : 0;
  179. h->tc = (data[2] & 0x02) ? 1 : 0;
  180. h->rd = (data[2] & 0x01) ? 1 : 0;
  181. h->ra = (data[3] & 0x80) ? 1 : 0;
  182. h->rcode = data[3] & 0x0f;
  183. h->qdcount = (data[4] << 8) | data[5];
  184. h->ancount = (data[6] << 8) | data[7];
  185. h->nscount = (data[8] << 8) | data[9];
  186. h->arcount = (data[10] << 8) | data[11];
  187. return HFIXEDSZ;
  188. }
  189. #endif
  190. #ifdef L_encoded
  191. /* Encode a dotted string into nameserver transport-level encoding.
  192. This routine is fairly dumb, and doesn't attempt to compress
  193. the data */
  194. int encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
  195. {
  196. int used = 0;
  197. while (dotted && *dotted) {
  198. char *c = strchr(dotted, '.');
  199. int l = c ? c - dotted : strlen(dotted);
  200. if (l >= (maxlen - used - 1))
  201. return -1;
  202. dest[used++] = l;
  203. memcpy(dest + used, dotted, l);
  204. used += l;
  205. if (c)
  206. dotted = c + 1;
  207. else
  208. break;
  209. }
  210. if (maxlen < 1)
  211. return -1;
  212. dest[used++] = 0;
  213. return used;
  214. }
  215. #endif
  216. #ifdef L_decoded
  217. /* Decode a dotted string from nameserver transport-level encoding.
  218. This routine understands compressed data. */
  219. int decode_dotted(const unsigned char *data, int offset,
  220. char *dest, int maxlen)
  221. {
  222. int l;
  223. int measure = 1;
  224. int total = 0;
  225. int used = 0;
  226. if (!data)
  227. return -1;
  228. while ((l=data[offset++])) {
  229. if (measure)
  230. total++;
  231. if ((l & 0xc0) == (0xc0)) {
  232. if (measure)
  233. total++;
  234. /* compressed item, redirect */
  235. offset = ((l & 0x3f) << 8) | data[offset];
  236. measure = 0;
  237. continue;
  238. }
  239. if ((used + l + 1) >= maxlen)
  240. return -1;
  241. memcpy(dest + used, data + offset, l);
  242. offset += l;
  243. used += l;
  244. if (measure)
  245. total += l;
  246. if (data[offset] != 0)
  247. dest[used++] = '.';
  248. else
  249. dest[used++] = '\0';
  250. }
  251. DPRINTF("Total decode len = %d\n", total);
  252. return total;
  253. }
  254. #endif
  255. #ifdef L_lengthd
  256. int length_dotted(const unsigned char *data, int offset)
  257. {
  258. int orig_offset = offset;
  259. int l;
  260. if (!data)
  261. return -1;
  262. while ((l = data[offset++])) {
  263. if ((l & 0xc0) == (0xc0)) {
  264. offset++;
  265. break;
  266. }
  267. offset += l;
  268. }
  269. return offset - orig_offset;
  270. }
  271. #endif
  272. #ifdef L_encodeq
  273. int encode_question(struct resolv_question *q,
  274. unsigned char *dest, int maxlen)
  275. {
  276. int i;
  277. i = encode_dotted(q->dotted, dest, maxlen);
  278. if (i < 0)
  279. return i;
  280. dest += i;
  281. maxlen -= i;
  282. if (maxlen < 4)
  283. return -1;
  284. dest[0] = (q->qtype & 0xff00) >> 8;
  285. dest[1] = (q->qtype & 0x00ff) >> 0;
  286. dest[2] = (q->qclass & 0xff00) >> 8;
  287. dest[3] = (q->qclass & 0x00ff) >> 0;
  288. return i + 4;
  289. }
  290. #endif
  291. #ifdef L_decodeq
  292. int decode_question(unsigned char *message, int offset,
  293. struct resolv_question *q)
  294. {
  295. char temp[BUFSIZ];
  296. int i;
  297. i = decode_dotted(message, offset, temp, sizeof(temp));
  298. if (i < 0)
  299. return i;
  300. offset += i;
  301. q->dotted = strdup(temp);
  302. q->qtype = (message[offset + 0] << 8) | message[offset + 1];
  303. q->qclass = (message[offset + 2] << 8) | message[offset + 3];
  304. return i + 4;
  305. }
  306. #endif
  307. #ifdef L_lengthq
  308. int length_question(unsigned char *message, int offset)
  309. {
  310. int i;
  311. i = length_dotted(message, offset);
  312. if (i < 0)
  313. return i;
  314. return i + 4;
  315. }
  316. #endif
  317. #ifdef L_encodea
  318. int encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
  319. {
  320. int i;
  321. i = encode_dotted(a->dotted, dest, maxlen);
  322. if (i < 0)
  323. return i;
  324. dest += i;
  325. maxlen -= i;
  326. if (maxlen < (RRFIXEDSZ+a->rdlength))
  327. return -1;
  328. *dest++ = (a->atype & 0xff00) >> 8;
  329. *dest++ = (a->atype & 0x00ff) >> 0;
  330. *dest++ = (a->aclass & 0xff00) >> 8;
  331. *dest++ = (a->aclass & 0x00ff) >> 0;
  332. *dest++ = (a->ttl & 0xff000000) >> 24;
  333. *dest++ = (a->ttl & 0x00ff0000) >> 16;
  334. *dest++ = (a->ttl & 0x0000ff00) >> 8;
  335. *dest++ = (a->ttl & 0x000000ff) >> 0;
  336. *dest++ = (a->rdlength & 0xff00) >> 8;
  337. *dest++ = (a->rdlength & 0x00ff) >> 0;
  338. memcpy(dest, a->rdata, a->rdlength);
  339. return i + RRFIXEDSZ + a->rdlength;
  340. }
  341. #endif
  342. #ifdef L_decodea
  343. int decode_answer(unsigned char *message, int offset,
  344. struct resolv_answer *a)
  345. {
  346. char temp[BUFSIZ];
  347. int i;
  348. i = decode_dotted(message, offset, temp, sizeof(temp));
  349. if (i < 0)
  350. return i;
  351. message += offset + i;
  352. a->dotted = strdup(temp);
  353. a->atype = (message[0] << 8) | message[1];
  354. message += 2;
  355. a->aclass = (message[0] << 8) | message[1];
  356. message += 2;
  357. a->ttl = (message[0] << 24) |
  358. (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
  359. message += 4;
  360. a->rdlength = (message[0] << 8) | message[1];
  361. message += 2;
  362. a->rdata = message;
  363. a->rdoffset = offset + i + RRFIXEDSZ;
  364. DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
  365. return i + RRFIXEDSZ + a->rdlength;
  366. }
  367. #endif
  368. #ifdef L_encodep
  369. int encode_packet(struct resolv_header *h,
  370. struct resolv_question **q,
  371. struct resolv_answer **an,
  372. struct resolv_answer **ns,
  373. struct resolv_answer **ar,
  374. unsigned char *dest, int maxlen)
  375. {
  376. int i, total = 0;
  377. int j;
  378. i = encode_header(h, dest, maxlen);
  379. if (i < 0)
  380. return i;
  381. dest += i;
  382. maxlen -= i;
  383. total += i;
  384. for (j = 0; j < h->qdcount; j++) {
  385. i = encode_question(q[j], dest, maxlen);
  386. if (i < 0)
  387. return i;
  388. dest += i;
  389. maxlen -= i;
  390. total += i;
  391. }
  392. for (j = 0; j < h->ancount; j++) {
  393. i = encode_answer(an[j], dest, maxlen);
  394. if (i < 0)
  395. return i;
  396. dest += i;
  397. maxlen -= i;
  398. total += i;
  399. }
  400. for (j = 0; j < h->nscount; j++) {
  401. i = encode_answer(ns[j], dest, maxlen);
  402. if (i < 0)
  403. return i;
  404. dest += i;
  405. maxlen -= i;
  406. total += i;
  407. }
  408. for (j = 0; j < h->arcount; j++) {
  409. i = encode_answer(ar[j], dest, maxlen);
  410. if (i < 0)
  411. return i;
  412. dest += i;
  413. maxlen -= i;
  414. total += i;
  415. }
  416. return total;
  417. }
  418. #endif
  419. #ifdef L_decodep
  420. int decode_packet(unsigned char *data, struct resolv_header *h)
  421. {
  422. return decode_header(data, h);
  423. }
  424. #endif
  425. #ifdef L_formquery
  426. int form_query(int id, const char *name, int type, unsigned char *packet,
  427. int maxlen)
  428. {
  429. struct resolv_header h;
  430. struct resolv_question q;
  431. int i, j;
  432. memset(&h, 0, sizeof(h));
  433. h.id = id;
  434. h.qdcount = 1;
  435. q.dotted = (char *) name;
  436. q.qtype = type;
  437. q.qclass = C_IN; /* CLASS_IN */
  438. i = encode_header(&h, packet, maxlen);
  439. if (i < 0)
  440. return i;
  441. j = encode_question(&q, packet + i, maxlen - i);
  442. if (j < 0)
  443. return j;
  444. return i + j;
  445. }
  446. #endif
  447. #ifdef L_dnslookup
  448. int dns_lookup(const char *name, int type, int nscount, char **nsip,
  449. unsigned char **outpacket, struct resolv_answer *a)
  450. {
  451. int i, j, len, fd, pos, id, ns;
  452. struct sockaddr_in sa;
  453. #ifdef __UCLIBC_HAS_IPV6__
  454. struct sockaddr_in6 sa6;
  455. #endif /* __UCLIBC_HAS_IPV6__ */
  456. struct timeval tv;
  457. fd_set fds;
  458. struct resolv_header h;
  459. struct resolv_question q;
  460. int retries = 0;
  461. unsigned char * packet = malloc(PACKETSZ);
  462. char * lookup = malloc(MAXDNAME);
  463. int variant = 0;
  464. #ifdef __UCLIBC_HAS_IPV6__
  465. int v6;
  466. #endif /* __UCLIBC_HAS_IPV6__ */
  467. fd = -1;
  468. if (!packet || !lookup || !nscount)
  469. goto fail;
  470. DPRINTF("Looking up type %d answer for '%s'\n", type, name);
  471. ns %= nscount;
  472. while (retries++ < MAX_RETRIES) {
  473. #ifdef __UCLIBC_HAS_IPV6__
  474. v6 = (inet_pton(AF_INET6, nsip[ns], &sa6.sin6_addr) > 0);
  475. #endif /* __UCLIBC_HAS_IPV6__ */
  476. if (fd != -1)
  477. close(fd);
  478. #ifndef __UCLIBC_HAS_IPV6__
  479. fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  480. #else /* __UCLIBC_HAS_IPV6__ */
  481. fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  482. #endif /* __UCLIBC_HAS_IPV6__ */
  483. if (fd == -1)
  484. goto fail;
  485. memset(packet, 0, PACKETSZ);
  486. memset(&h, 0, sizeof(h));
  487. id = (int) random();
  488. h.id = id;
  489. h.qdcount = 1;
  490. h.rd = 1;
  491. DPRINTF("encoding header\n", h.rd);
  492. i = encode_header(&h, packet, PACKETSZ);
  493. if (i < 0)
  494. goto fail;
  495. strncpy(lookup,name,MAXDNAME);
  496. if (variant < searchdomains && strchr(lookup, '.') == NULL)
  497. {
  498. strncat(lookup,".", MAXDNAME);
  499. strncat(lookup,searchdomain[variant], MAXDNAME);
  500. }
  501. DPRINTF("lookup name: %s\n", lookup);
  502. q.dotted = (char *)lookup;
  503. q.qtype = type;
  504. q.qclass = C_IN; /* CLASS_IN */
  505. j = encode_question(&q, packet+i, PACKETSZ-i);
  506. if (j < 0)
  507. goto fail;
  508. len = i + j;
  509. DPRINTF("On try %d, sending query to port %d of machine %s\n",
  510. retries, NAMESERVER_PORT, nsip[ns]);
  511. #ifndef __UCLIBC_HAS_IPV6__
  512. sa.sin_family = AF_INET;
  513. sa.sin_port = htons(NAMESERVER_PORT);
  514. sa.sin_addr.s_addr = inet_addr(nsip[ns]);
  515. #else /* __UCLIBC_HAS_IPV6__ */
  516. if (v6) {
  517. sa6.sin6_family = AF_INET6;
  518. sa6.sin6_port = htons(NAMESERVER_PORT);
  519. /* sa6.sin6_addr is already here */
  520. } else {
  521. sa.sin_family = AF_INET;
  522. sa.sin_port = htons(NAMESERVER_PORT);
  523. sa.sin_addr.s_addr = inet_addr(nsip[ns]);
  524. }
  525. #endif /* __UCLIBC_HAS_IPV6__ */
  526. #ifndef __UCLIBC_HAS_IPV6__
  527. if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
  528. #else /* __UCLIBC_HAS_IPV6__ */
  529. if (connect(fd, (struct sockaddr *) (v6 ? &sa6 : &sa),
  530. v6 ? sizeof(sa6) : sizeof(sa)) == -1) {
  531. #endif /* __UCLIBC_HAS_IPV6__ */
  532. if (errno == ENETUNREACH) {
  533. /* routing error, presume not transient */
  534. goto tryall;
  535. } else
  536. /* retry */
  537. continue;
  538. }
  539. DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
  540. len, h.id, h.qr);
  541. send(fd, packet, len, 0);
  542. FD_ZERO(&fds);
  543. FD_SET(fd, &fds);
  544. tv.tv_sec = REPLY_TIMEOUT;
  545. tv.tv_usec = 0;
  546. if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
  547. DPRINTF("Timeout\n");
  548. /* timed out, so retry send and receive,
  549. * to next nameserver on queue */
  550. goto again;
  551. }
  552. i = recv(fd, packet, 512, 0);
  553. if (i < HFIXEDSZ)
  554. /* too short ! */
  555. goto again;
  556. decode_header(packet, &h);
  557. DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
  558. if ((h.id != id) || (!h.qr))
  559. /* unsolicited */
  560. goto again;
  561. DPRINTF("Got response %s\n", "(i think)!");
  562. DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
  563. h.qdcount, h.ancount, h.nscount, h.arcount);
  564. DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
  565. h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
  566. if ((h.rcode) || (h.ancount < 1)) {
  567. /* negative result, not present */
  568. goto again;
  569. }
  570. pos = HFIXEDSZ;
  571. for (j = 0; j < h.qdcount; j++) {
  572. DPRINTF("Skipping question %d at %d\n", j, pos);
  573. i = length_question(packet, pos);
  574. DPRINTF("Length of question %d is %d\n", j, i);
  575. if (i < 0)
  576. goto again;
  577. pos += i;
  578. }
  579. DPRINTF("Decoding answer at pos %d\n", pos);
  580. for (j=0;j<h.ancount;j++)
  581. {
  582. i = decode_answer(packet, pos, a);
  583. if (i<0) {
  584. DPRINTF("failed decode %d\n", i);
  585. goto again;
  586. }
  587. /* For all but T_SIG, accept first answer */
  588. if (a->atype != T_SIG)
  589. break;
  590. DPRINTF("skipping T_SIG %d\n", i);
  591. free(a->dotted);
  592. pos += i;
  593. }
  594. DPRINTF("Answer name = |%s|\n", a->dotted);
  595. DPRINTF("Answer type = |%d|\n", a->atype);
  596. close(fd);
  597. if (outpacket)
  598. *outpacket = packet;
  599. else
  600. free(packet);
  601. free(lookup);
  602. return (0); /* success! */
  603. tryall:
  604. /* if there are other nameservers, give them a go,
  605. otherwise return with error */
  606. variant = 0;
  607. if (retries >= nscount*(searchdomains+1))
  608. goto fail;
  609. again:
  610. /* if there are searchdomains, try them or fallback as passed */
  611. if (variant < searchdomains) {
  612. /* next search */
  613. variant++;
  614. } else {
  615. /* next server, first search */
  616. ns = (ns + 1) % nscount;
  617. variant = 0;
  618. }
  619. }
  620. fail:
  621. if (fd != -1)
  622. close(fd);
  623. if (lookup)
  624. free(lookup);
  625. if (packet)
  626. free(packet);
  627. return -1;
  628. }
  629. #endif
  630. #ifdef L_opennameservers
  631. int nameservers;
  632. char * nameserver[MAX_SERVERS];
  633. int searchdomains;
  634. char * searchdomain[MAX_SEARCH];
  635. /*
  636. * we currently read formats not quite the same as that on normal
  637. * unix systems, we can have a list of nameservers after the keyword.
  638. */
  639. int open_nameservers()
  640. {
  641. FILE *fp;
  642. int i;
  643. #define RESOLV_ARGS 5
  644. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  645. int argc;
  646. if (nameservers > 0)
  647. return 0;
  648. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  649. (fp = fopen("/etc/config/resolv.conf", "r"))) {
  650. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  651. for (p = szBuffer; *p && isspace(*p); p++)
  652. /* skip white space */;
  653. if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  654. continue;
  655. argc = 0;
  656. while (*p && argc < RESOLV_ARGS) {
  657. argv[argc++] = p;
  658. while (*p && !isspace(*p) && *p != '\n')
  659. p++;
  660. while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  661. *p++ = '\0';
  662. }
  663. if (strcmp(argv[0], "nameserver") == 0) {
  664. for (i = 1; i < argc && nameservers < MAX_SERVERS; i++) {
  665. nameserver[nameservers++] = strdup(argv[i]);
  666. DPRINTF("adding nameserver %s\n", argv[i]);
  667. }
  668. }
  669. /* domain and search are mutually exclusive, the last one wins */
  670. if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  671. while (searchdomains > 0) {
  672. free(searchdomain[--searchdomains]);
  673. searchdomain[searchdomains] = NULL;
  674. }
  675. for (i=1; i < argc && searchdomains < MAX_SEARCH; i++) {
  676. searchdomain[searchdomains++] = strdup(argv[i]);
  677. DPRINTF("adding search %s\n", argv[i]);
  678. }
  679. }
  680. }
  681. fclose(fp);
  682. } else {
  683. DPRINTF("failed to open %s\n", "resolv.conf");
  684. }
  685. DPRINTF("nameservers = %d\n", nameservers);
  686. return 0;
  687. }
  688. #endif
  689. #ifdef L_closenameservers
  690. void close_nameservers(void)
  691. {
  692. while (nameservers > 0) {
  693. free(nameserver[--nameservers]);
  694. nameserver[nameservers] = NULL;
  695. }
  696. while (searchdomains > 0) {
  697. free(searchdomain[--searchdomains]);
  698. searchdomain[searchdomains] = NULL;
  699. }
  700. }
  701. #endif
  702. #ifdef L_gethostbyname
  703. struct hostent *gethostbyname(const char *name)
  704. {
  705. static struct hostent h;
  706. static char buf[sizeof(struct in_addr) +
  707. sizeof(struct in_addr *)*2 +
  708. 256/*namebuffer*/ + 32/* margin */];
  709. struct hostent *hp;
  710. gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
  711. return hp;
  712. }
  713. #endif
  714. #ifdef L_gethostbyname2
  715. #ifdef __UCLIBC_HAS_IPV6__
  716. /* TBD: Not the right place for defining these, I guess */
  717. const struct in6_addr in6addr_any =
  718. { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
  719. const struct in6_addr in6addr_loopback =
  720. { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } };
  721. #endif /* __UCLIBC_HAS_IPV6__ */
  722. struct hostent *gethostbyname2(const char *name, int family)
  723. {
  724. #ifndef __UCLIBC_HAS_IPV6__
  725. return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
  726. #else /* __UCLIBC_HAS_IPV6__ */
  727. static struct hostent h;
  728. static char buf[sizeof(struct in6_addr) +
  729. sizeof(struct in6_addr *)*2 +
  730. 256/*namebuffer*/ + 32/* margin */];
  731. struct hostent *hp;
  732. gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
  733. return hp;
  734. #endif /* __UCLIBC_HAS_IPV6__ */
  735. }
  736. #endif
  737. #ifdef L_getnetbyname
  738. struct netent * getnetbyname(const char * name)
  739. {
  740. return NULL;
  741. }
  742. #endif
  743. #ifdef L_res_init
  744. struct __res_state * __res;
  745. #ifndef _res
  746. #define _res (*__res_state())
  747. #endif
  748. int res_init(void)
  749. {
  750. struct __res_state *rp = __res;
  751. if(!__res) {
  752. rp = (struct __res_state *) malloc(sizeof(struct __res_state));
  753. memset(rp, 0, sizeof(struct __res_state));
  754. __res = rp;
  755. }
  756. (void) open_nameservers();
  757. rp->retrans = RES_TIMEOUT;
  758. rp->retry = 4;
  759. rp->options = RES_INIT;
  760. rp->id = (u_int) random();
  761. rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
  762. rp->nsaddr.sin_family = AF_INET;
  763. rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
  764. rp->ndots = 1;
  765. /** rp->pfcode = 0; **/
  766. rp->_vcsock = -1;
  767. /** rp->_flags = 0; **/
  768. /** rp->qhook = NULL; **/
  769. /** rp->rhook = NULL; **/
  770. /** rp->_u._ext.nsinit = 0; **/
  771. if(searchdomains) {
  772. int i;
  773. for(i=0; i<searchdomains; i++) {
  774. rp->dnsrch[i] = searchdomain[i];
  775. }
  776. }
  777. if(nameservers) {
  778. int i;
  779. struct in_addr a;
  780. for(i=0; i<nameservers; i++) {
  781. if (inet_aton(nameserver[i], &a)) {
  782. rp->nsaddr_list[i].sin_addr = a;
  783. rp->nsaddr_list[i].sin_family = AF_INET;
  784. rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
  785. }
  786. }
  787. }
  788. rp->nscount = nameservers;
  789. return(0);
  790. }
  791. struct __res_state * __res_state (void)
  792. {
  793. if(!__res) {
  794. res_init();
  795. }
  796. return __res;
  797. }
  798. void res_close( void )
  799. {
  800. if(__res) {
  801. free(__res);
  802. __res = NULL;
  803. }
  804. return;
  805. }
  806. #endif
  807. #ifdef L_res_query
  808. #ifndef MIN
  809. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  810. #endif
  811. int res_query(const char *dname, int class, int type,
  812. unsigned char *answer, int anslen)
  813. {
  814. unsigned char * packet = 0;
  815. struct resolv_answer a;
  816. int i;
  817. open_nameservers();
  818. if (!dname || class != 1 /* CLASS_IN */)
  819. return(-1);
  820. memset((char *) &a, '\0', sizeof(a));
  821. i = dns_lookup(dname, type, nameservers, nameserver, &packet, &a);
  822. if (i < 0)
  823. return(-1);
  824. free(a.dotted);
  825. if (a.atype == type) { /* CNAME*/
  826. if (anslen && answer)
  827. memcpy(answer, a.rdata, MIN(anslen, a.rdlength));
  828. if (packet)
  829. free(packet);
  830. return(MIN(anslen, a.rdlength));
  831. }
  832. if (packet)
  833. free(packet);
  834. return 0;
  835. }
  836. #endif
  837. #ifdef L_gethostbyaddr
  838. struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
  839. {
  840. static struct hostent h;
  841. static char buf[
  842. #ifndef __UCLIBC_HAS_IPV6__
  843. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  844. #else
  845. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  846. #endif /* __UCLIBC_HAS_IPV6__ */
  847. 256/*namebuffer*/ + 32/* margin */];
  848. struct hostent *hp;
  849. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  850. return hp;
  851. }
  852. #endif
  853. #ifdef L_read_etc_hosts_r
  854. void __open_etc_hosts(FILE **fp)
  855. {
  856. if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
  857. *fp = fopen("/etc/config/hosts", "r");
  858. }
  859. return;
  860. }
  861. int read_etc_hosts_r(FILE * fp, const char * name, int type,
  862. enum etc_hosts_action action,
  863. struct hostent * result_buf,
  864. char * buf, size_t buflen,
  865. struct hostent ** result,
  866. int * h_errnop)
  867. {
  868. struct in_addr *in=NULL;
  869. struct in_addr **addr_list=NULL;
  870. #ifdef __UCLIBC_HAS_IPV6__
  871. struct in6_addr *in6=NULL;
  872. struct in6_addr **addr_list6=NULL;
  873. #endif /* __UCLIBC_HAS_IPV6__ */
  874. char *cp;
  875. #define MAX_ALIAS 5
  876. char *alias[MAX_ALIAS];
  877. int aliases, i;
  878. int ret=HOST_NOT_FOUND;
  879. if (action!=GETHOSTENT) {
  880. #ifdef __UCLIBC_HAS_IPV6__
  881. char *p=buf;
  882. size_t len=buflen;
  883. #endif /* __UCLIBC_HAS_IPV6__ */
  884. *h_errnop=NETDB_INTERNAL;
  885. if (buflen < sizeof(*in))
  886. return ERANGE;
  887. in=(struct in_addr*)buf;
  888. buf+=sizeof(*in);
  889. buflen-=sizeof(*in);
  890. if (buflen < sizeof(*addr_list)*2)
  891. return ERANGE;
  892. addr_list=(struct in_addr **)buf;
  893. buf+=sizeof(*addr_list)*2;
  894. buflen-=sizeof(*addr_list)*2;
  895. #ifdef __UCLIBC_HAS_IPV6__
  896. if (len < sizeof(*in6))
  897. return ERANGE;
  898. in6=(struct in6_addr*)p;
  899. p+=sizeof(*in6);
  900. len-=sizeof(*in6);
  901. if (len < sizeof(*addr_list6)*2)
  902. return ERANGE;
  903. addr_list6=(struct in6_addr**)p;
  904. p+=sizeof(*addr_list6)*2;
  905. len-=sizeof(*addr_list6)*2;
  906. if (len < buflen) {
  907. buflen=len;
  908. buf=p;
  909. }
  910. #endif /* __UCLIBC_HAS_IPV6__ */
  911. if (buflen < 80)
  912. return ERANGE;
  913. __open_etc_hosts(&fp);
  914. if (fp == NULL) {
  915. result=NULL;
  916. return errno;
  917. }
  918. }
  919. *h_errnop=HOST_NOT_FOUND;
  920. while (fgets(buf, buflen, fp)) {
  921. if ((cp = strchr(buf, '#')))
  922. *cp = '\0';
  923. DPRINTF("Looking at: %s\n", buf);
  924. aliases = 0;
  925. cp = buf;
  926. while (*cp) {
  927. while (*cp && isspace(*cp))
  928. *cp++ = '\0';
  929. if (!*cp)
  930. continue;
  931. if (aliases < MAX_ALIAS)
  932. alias[aliases++] = cp;
  933. while (*cp && !isspace(*cp))
  934. cp++;
  935. }
  936. if (aliases < 2)
  937. continue; /* syntax error really */
  938. if (action==GETHOSTENT) {
  939. /* Return whatever the next entry happens to be. */
  940. break;
  941. } else if (action==GET_HOSTS_BYADDR) {
  942. if (strcmp(name, alias[0]) != 0)
  943. continue;
  944. } else {
  945. /* GET_HOSTS_BYNAME */
  946. for (i = 1; i < aliases; i++)
  947. if (strcasecmp(name, alias[i]) == 0)
  948. break;
  949. if (i >= aliases)
  950. continue;
  951. }
  952. if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
  953. DPRINTF("Found INET\n");
  954. addr_list[0] = in;
  955. addr_list[1] = 0;
  956. result_buf->h_name = alias[1];
  957. result_buf->h_addrtype = AF_INET;
  958. result_buf->h_length = sizeof(*in);
  959. result_buf->h_addr_list = (char**) addr_list;
  960. *result=result_buf;
  961. ret=NETDB_SUCCESS;
  962. #ifdef __UCLIBC_HAS_IPV6__
  963. } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
  964. DPRINTF("Found INET6\n");
  965. addr_list6[0] = in6;
  966. addr_list6[1] = 0;
  967. result_buf->h_name = alias[1];
  968. result_buf->h_addrtype = AF_INET6;
  969. result_buf->h_length = sizeof(*in6);
  970. result_buf->h_addr_list = (char**) addr_list6;
  971. *result=result_buf;
  972. ret=NETDB_SUCCESS;
  973. #endif /* __UCLIBC_HAS_IPV6__ */
  974. } else {
  975. DPRINTF("Error\n");
  976. ret=TRY_AGAIN;
  977. break; /* bad ip address */
  978. }
  979. if (action!=GETHOSTENT) {
  980. fclose(fp);
  981. }
  982. return ret;
  983. }
  984. if (action!=GETHOSTENT) {
  985. fclose(fp);
  986. }
  987. return ret;
  988. }
  989. #endif
  990. #ifdef L_gethostent
  991. #ifdef __UCLIBC_HAS_THREADS__
  992. #include <pthread.h>
  993. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  994. # define LOCK pthread_mutex_lock(&mylock)
  995. # define UNLOCK pthread_mutex_unlock(&mylock);
  996. #else
  997. # define LOCK
  998. # define UNLOCK
  999. #endif
  1000. static int __stay_open;
  1001. static FILE * __gethostent_fp;
  1002. void endhostent (void)
  1003. {
  1004. LOCK;
  1005. __stay_open = 0;
  1006. if (__gethostent_fp) {
  1007. fclose(__gethostent_fp);
  1008. }
  1009. UNLOCK;
  1010. }
  1011. void sethostent (int stay_open)
  1012. {
  1013. LOCK;
  1014. __stay_open = stay_open;
  1015. UNLOCK;
  1016. }
  1017. struct hostent *gethostent (void)
  1018. {
  1019. static struct hostent h;
  1020. static char buf[
  1021. #ifndef __UCLIBC_HAS_IPV6__
  1022. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1023. #else
  1024. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1025. #endif /* __UCLIBC_HAS_IPV6__ */
  1026. 80/*namebuffer*/ + 2/* margin */];
  1027. struct hostent *host;
  1028. LOCK;
  1029. if (__gethostent_fp == NULL) {
  1030. __open_etc_hosts(&__gethostent_fp);
  1031. if (__gethostent_fp == NULL) {
  1032. UNLOCK;
  1033. return((struct hostent *)NULL);
  1034. }
  1035. }
  1036. read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  1037. &h, buf, sizeof(buf), &host, &h_errno);
  1038. if (__stay_open==0) {
  1039. fclose(__gethostent_fp);
  1040. }
  1041. UNLOCK;
  1042. return(host);
  1043. }
  1044. #endif
  1045. #ifdef L_get_hosts_byname_r
  1046. int get_hosts_byname_r(const char * name, int type,
  1047. struct hostent * result_buf,
  1048. char * buf, size_t buflen,
  1049. struct hostent ** result,
  1050. int * h_errnop)
  1051. {
  1052. return(read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, result_buf, buf, buflen, result, h_errnop));
  1053. }
  1054. #endif
  1055. #ifdef L_get_hosts_byaddr_r
  1056. int get_hosts_byaddr_r(const char * addr, int len, int type,
  1057. struct hostent * result_buf,
  1058. char * buf, size_t buflen,
  1059. struct hostent ** result,
  1060. int * h_errnop)
  1061. {
  1062. #ifndef __UCLIBC_HAS_IPV6__
  1063. char ipaddr[INET_ADDRSTRLEN];
  1064. #else
  1065. char ipaddr[INET6_ADDRSTRLEN];
  1066. #endif /* __UCLIBC_HAS_IPV6__ */
  1067. switch (type) {
  1068. case AF_INET:
  1069. if (len != sizeof(struct in_addr))
  1070. return 0;
  1071. break;
  1072. #ifdef __UCLIBC_HAS_IPV6__
  1073. case AF_INET6:
  1074. if (len != sizeof(struct in6_addr))
  1075. return 0;
  1076. break;
  1077. #endif /* __UCLIBC_HAS_IPV6__ */
  1078. default:
  1079. return 0;
  1080. }
  1081. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1082. return(read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR, result_buf, buf, buflen, result, h_errnop));
  1083. }
  1084. #endif
  1085. #ifdef L_getnameinfo
  1086. #ifndef min
  1087. # define min(x,y) (((x) > (y)) ? (y) : (x))
  1088. #endif /* min */
  1089. int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  1090. socklen_t hostlen, char *serv, socklen_t servlen,
  1091. unsigned int flags)
  1092. {
  1093. int serrno = errno;
  1094. int ok = 0;
  1095. struct hostent *h = NULL;
  1096. char domain[256];
  1097. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1098. return EAI_BADFLAGS;
  1099. if (sa == NULL || addrlen < sizeof (sa_family_t))
  1100. return EAI_FAMILY;
  1101. switch (sa->sa_family) {
  1102. case AF_LOCAL:
  1103. break;
  1104. case AF_INET:
  1105. if (addrlen < sizeof (struct sockaddr_in))
  1106. return EAI_FAMILY;
  1107. break;
  1108. #ifdef __UCLIBC_HAS_IPV6__
  1109. case AF_INET6:
  1110. if (addrlen < sizeof (struct sockaddr_in6))
  1111. return EAI_FAMILY;
  1112. break;
  1113. #endif /* __UCLIBC_HAS_IPV6__ */
  1114. default:
  1115. return EAI_FAMILY;
  1116. }
  1117. if (host != NULL && hostlen > 0)
  1118. switch (sa->sa_family) {
  1119. case AF_INET:
  1120. #ifdef __UCLIBC_HAS_IPV6__
  1121. case AF_INET6:
  1122. #endif /* __UCLIBC_HAS_IPV6__ */
  1123. if (!(flags & NI_NUMERICHOST)) {
  1124. #ifdef __UCLIBC_HAS_IPV6__
  1125. if (sa->sa_family == AF_INET6)
  1126. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1127. sizeof(struct in6_addr), AF_INET6);
  1128. else
  1129. #endif /* __UCLIBC_HAS_IPV6__ */
  1130. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1131. sizeof(struct in_addr), AF_INET);
  1132. if (h) {
  1133. char *c;
  1134. if ((flags & NI_NOFQDN)
  1135. && (getdomainname (domain, sizeof(domain)) == 0)
  1136. && (c = strstr (h->h_name, domain))
  1137. && (c != h->h_name) && (*(--c) == '.')) {
  1138. strncpy (host, h->h_name,
  1139. min(hostlen, (size_t) (c - h->h_name)));
  1140. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1141. ok = 1;
  1142. } else {
  1143. strncpy (host, h->h_name, hostlen);
  1144. ok = 1;
  1145. }
  1146. }
  1147. }
  1148. if (!ok) {
  1149. if (flags & NI_NAMEREQD) {
  1150. errno = serrno;
  1151. return EAI_NONAME;
  1152. } else {
  1153. const char *c;
  1154. #ifdef __UCLIBC_HAS_IPV6__
  1155. if (sa->sa_family == AF_INET6) {
  1156. const struct sockaddr_in6 *sin6p;
  1157. sin6p = (const struct sockaddr_in6 *) sa;
  1158. c = inet_ntop (AF_INET6,
  1159. (const void *) &sin6p->sin6_addr, host, hostlen);
  1160. #if 0
  1161. /* Does scope id need to be supported? */
  1162. uint32_t scopeid;
  1163. scopeid = sin6p->sin6_scope_id;
  1164. if (scopeid != 0) {
  1165. /* Buffer is >= IFNAMSIZ+1. */
  1166. char scopebuf[IFNAMSIZ + 1];
  1167. char *scopeptr;
  1168. int ni_numericscope = 0;
  1169. size_t real_hostlen = __strnlen (host, hostlen);
  1170. size_t scopelen = 0;
  1171. scopebuf[0] = SCOPE_DELIMITER;
  1172. scopebuf[1] = '\0';
  1173. scopeptr = &scopebuf[1];
  1174. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1175. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1176. if (if_indextoname (scopeid, scopeptr) == NULL)
  1177. ++ni_numericscope;
  1178. else
  1179. scopelen = strlen (scopebuf);
  1180. } else {
  1181. ++ni_numericscope;
  1182. }
  1183. if (ni_numericscope)
  1184. scopelen = 1 + snprintf (scopeptr,
  1185. (scopebuf
  1186. + sizeof scopebuf
  1187. - scopeptr),
  1188. "%u", scopeid);
  1189. if (real_hostlen + scopelen + 1 > hostlen)
  1190. return EAI_SYSTEM;
  1191. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1192. }
  1193. #endif
  1194. } else
  1195. #endif /* __UCLIBC_HAS_IPV6__ */
  1196. c = inet_ntop (AF_INET,
  1197. (const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
  1198. host, hostlen);
  1199. if (c == NULL) {
  1200. errno = serrno;
  1201. return EAI_SYSTEM;
  1202. }
  1203. }
  1204. ok = 1;
  1205. }
  1206. break;
  1207. case AF_LOCAL:
  1208. if (!(flags & NI_NUMERICHOST)) {
  1209. struct utsname utsname;
  1210. if (!uname (&utsname)) {
  1211. strncpy (host, utsname.nodename, hostlen);
  1212. break;
  1213. };
  1214. };
  1215. if (flags & NI_NAMEREQD) {
  1216. errno = serrno;
  1217. return EAI_NONAME;
  1218. }
  1219. strncpy (host, "localhost", hostlen);
  1220. break;
  1221. default:
  1222. return EAI_FAMILY;
  1223. }
  1224. if (serv && (servlen > 0)) {
  1225. switch (sa->sa_family) {
  1226. case AF_INET:
  1227. #ifdef __UCLIBC_HAS_IPV6__
  1228. case AF_INET6:
  1229. #endif /* __UCLIBC_HAS_IPV6__ */
  1230. if (!(flags & NI_NUMERICSERV)) {
  1231. struct servent *s;
  1232. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1233. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1234. if (s) {
  1235. strncpy (serv, s->s_name, servlen);
  1236. break;
  1237. }
  1238. }
  1239. snprintf (serv, servlen, "%d",
  1240. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1241. break;
  1242. case AF_LOCAL:
  1243. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1244. break;
  1245. }
  1246. }
  1247. if (host && (hostlen > 0))
  1248. host[hostlen-1] = 0;
  1249. if (serv && (servlen > 0))
  1250. serv[servlen-1] = 0;
  1251. errno = serrno;
  1252. return 0;
  1253. }
  1254. #endif
  1255. #ifdef L_gethostbyname_r
  1256. int gethostbyname_r(const char * name,
  1257. struct hostent * result_buf,
  1258. char * buf, size_t buflen,
  1259. struct hostent ** result,
  1260. int * h_errnop)
  1261. {
  1262. struct in_addr *in;
  1263. struct in_addr **addr_list;
  1264. unsigned char *packet;
  1265. struct resolv_answer a;
  1266. int i;
  1267. int nest = 0;
  1268. open_nameservers();
  1269. *result=NULL;
  1270. if (!name)
  1271. return EINVAL;
  1272. /* do /etc/hosts first */
  1273. if ((i=get_hosts_byname_r(name, AF_INET, result_buf,
  1274. buf, buflen, result, h_errnop))==0)
  1275. return i;
  1276. switch (*h_errnop) {
  1277. case HOST_NOT_FOUND:
  1278. case NO_ADDRESS:
  1279. break;
  1280. case NETDB_INTERNAL:
  1281. if (errno == ENOENT) {
  1282. break;
  1283. }
  1284. /* else fall through */
  1285. default:
  1286. return i;
  1287. }
  1288. DPRINTF("Nothing found in /etc/hosts\n");
  1289. *h_errnop = NETDB_INTERNAL;
  1290. if (buflen < sizeof(*in))
  1291. return ERANGE;
  1292. in=(struct in_addr*)buf;
  1293. buf+=sizeof(*in);
  1294. buflen-=sizeof(*in);
  1295. if (buflen < sizeof(*addr_list)*2)
  1296. return ERANGE;
  1297. addr_list=(struct in_addr**)buf;
  1298. buf+=sizeof(*addr_list)*2;
  1299. buflen-=sizeof(*addr_list)*2;
  1300. addr_list[0] = in;
  1301. addr_list[1] = 0;
  1302. if (buflen<256)
  1303. return ERANGE;
  1304. strncpy(buf, name, buflen);
  1305. /* First check if this is already an address */
  1306. if (inet_aton(name, in)) {
  1307. result_buf->h_name = buf;
  1308. result_buf->h_addrtype = AF_INET;
  1309. result_buf->h_length = sizeof(*in);
  1310. result_buf->h_addr_list = (char **) addr_list;
  1311. *result=result_buf;
  1312. *h_errnop = NETDB_SUCCESS;
  1313. return NETDB_SUCCESS;
  1314. }
  1315. for (;;) {
  1316. i = dns_lookup(buf, T_A, nameservers, nameserver, &packet, &a);
  1317. if (i < 0) {
  1318. *h_errnop = HOST_NOT_FOUND;
  1319. DPRINTF("dns_lookup\n");
  1320. return TRY_AGAIN;
  1321. }
  1322. strncpy(buf, a.dotted, buflen);
  1323. free(a.dotted);
  1324. if (a.atype == T_CNAME) { /* CNAME */
  1325. DPRINTF("Got a CNAME in gethostbyname()\n");
  1326. i = decode_dotted(packet, a.rdoffset, buf, buflen);
  1327. free(packet);
  1328. if (i < 0) {
  1329. *h_errnop = NO_RECOVERY;
  1330. DPRINTF("decode_dotted\n");
  1331. return -1;
  1332. }
  1333. if (++nest > MAX_RECURSE) {
  1334. *h_errnop = NO_RECOVERY;
  1335. DPRINTF("recursion\n");
  1336. return -1;
  1337. }
  1338. continue;
  1339. } else if (a.atype == T_A) { /* ADDRESS */
  1340. memcpy(in, a.rdata, sizeof(*in));
  1341. result_buf->h_name = buf;
  1342. result_buf->h_addrtype = AF_INET;
  1343. result_buf->h_length = sizeof(*in);
  1344. result_buf->h_addr_list = (char **) addr_list;
  1345. free(packet);
  1346. break;
  1347. } else {
  1348. free(packet);
  1349. *h_errnop=HOST_NOT_FOUND;
  1350. return TRY_AGAIN;
  1351. }
  1352. }
  1353. *result=result_buf;
  1354. return NETDB_SUCCESS;
  1355. }
  1356. #endif
  1357. #ifdef L_gethostbyname2_r
  1358. #ifdef __UCLIBC_HAS_IPV6__
  1359. /* TBD: Not the right place for defining these, I guess */
  1360. /*
  1361. const struct in6_addr in6addr_any =
  1362. { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
  1363. const struct in6_addr in6addr_loopback =
  1364. { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } };
  1365. */
  1366. #endif /* __UCLIBC_HAS_IPV6__ */
  1367. int gethostbyname2_r(const char *name, int family,
  1368. struct hostent * result_buf,
  1369. char * buf, size_t buflen,
  1370. struct hostent ** result,
  1371. int * h_errnop)
  1372. {
  1373. #ifndef __UCLIBC_HAS_IPV6__
  1374. return family == AF_INET ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1375. #else /* __UCLIBC_HAS_IPV6__ */
  1376. struct in6_addr *in;
  1377. struct in6_addr **addr_list;
  1378. unsigned char *packet;
  1379. struct resolv_answer a;
  1380. int i;
  1381. int nest = 0;
  1382. if (family == AF_INET)
  1383. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1384. if (family != AF_INET6)
  1385. return EINVAL;
  1386. open_nameservers();
  1387. *result=NULL;
  1388. if (!name)
  1389. return EINVAL;
  1390. /* do /etc/hosts first */
  1391. if ((i=get_hosts_byname_r(name, family, result_buf,
  1392. buf, buflen, result, h_errnop))==0)
  1393. return i;
  1394. switch (*h_errnop) {
  1395. case HOST_NOT_FOUND:
  1396. case NO_ADDRESS:
  1397. break;
  1398. default:
  1399. return i;
  1400. }
  1401. DPRINTF("Nothing found in /etc/hosts\n");
  1402. *h_errnop = NETDB_INTERNAL;
  1403. if (buflen < sizeof(*in))
  1404. return ERANGE;
  1405. in=(struct in6_addr*)buf;
  1406. buf+=sizeof(*in);
  1407. buflen-=sizeof(*in);
  1408. if (buflen < sizeof(*addr_list)*2)
  1409. return ERANGE;
  1410. addr_list=(struct in6_addr**)buf;
  1411. buf+=sizeof(*addr_list)*2;
  1412. buflen-=sizeof(*addr_list)*2;
  1413. addr_list[0] = in;
  1414. addr_list[1] = 0;
  1415. if (buflen<256)
  1416. return ERANGE;
  1417. strncpy(buf, name, buflen);
  1418. /* First check if this is already an address */
  1419. if (inet_pton(AF_INET6, name, in)) {
  1420. result_buf->h_name = buf;
  1421. result_buf->h_addrtype = AF_INET6;
  1422. result_buf->h_length = sizeof(*in);
  1423. result_buf->h_addr_list = (char **) addr_list;
  1424. *result=result_buf;
  1425. *h_errnop = NETDB_SUCCESS;
  1426. return NETDB_SUCCESS;
  1427. }
  1428. for (;;) {
  1429. i = dns_lookup(buf, T_AAAA, nameservers, nameserver, &packet, &a);
  1430. if (i < 0) {
  1431. *h_errnop = HOST_NOT_FOUND;
  1432. return TRY_AGAIN;
  1433. }
  1434. strncpy(buf, a.dotted, buflen);
  1435. free(a.dotted);
  1436. if (a.atype == T_CNAME) { /* CNAME */
  1437. DPRINTF("Got a CNAME in gethostbyname()\n");
  1438. i = decode_dotted(packet, a.rdoffset, buf, buflen);
  1439. free(packet);
  1440. if (i < 0) {
  1441. *h_errnop = NO_RECOVERY;
  1442. return -1;
  1443. }
  1444. if (++nest > MAX_RECURSE) {
  1445. *h_errnop = NO_RECOVERY;
  1446. return -1;
  1447. }
  1448. continue;
  1449. } else if (a.atype == T_AAAA) { /* ADDRESS */
  1450. memcpy(in, a.rdata, sizeof(*in));
  1451. result_buf->h_name = buf;
  1452. result_buf->h_addrtype = AF_INET6;
  1453. result_buf->h_length = sizeof(*in);
  1454. result_buf->h_addr_list = (char **) addr_list;
  1455. free(packet);
  1456. break;
  1457. } else {
  1458. free(packet);
  1459. *h_errnop=HOST_NOT_FOUND;
  1460. return TRY_AGAIN;
  1461. }
  1462. }
  1463. *result=result_buf;
  1464. return NETDB_SUCCESS;
  1465. #endif /* __UCLIBC_HAS_IPV6__ */
  1466. }
  1467. #endif
  1468. #ifdef L_gethostbyaddr_r
  1469. int gethostbyaddr_r (const void *addr, socklen_t len, int type,
  1470. struct hostent * result_buf,
  1471. char * buf, size_t buflen,
  1472. struct hostent ** result,
  1473. int * h_errnop)
  1474. {
  1475. struct in_addr *in;
  1476. struct in_addr **addr_list;
  1477. #ifdef __UCLIBC_HAS_IPV6__
  1478. char *qp;
  1479. size_t plen;
  1480. struct in6_addr *in6;
  1481. struct in6_addr **addr_list6;
  1482. #endif /* __UCLIBC_HAS_IPV6__ */
  1483. unsigned char *packet;
  1484. struct resolv_answer a;
  1485. int i;
  1486. int nest = 0;
  1487. *result=NULL;
  1488. if (!addr)
  1489. return EINVAL;
  1490. switch (type) {
  1491. case AF_INET:
  1492. if (len != sizeof(struct in_addr))
  1493. return EINVAL;
  1494. break;
  1495. #ifdef __UCLIBC_HAS_IPV6__
  1496. case AF_INET6:
  1497. if (len != sizeof(struct in6_addr))
  1498. return EINVAL;
  1499. break;
  1500. #endif /* __UCLIBC_HAS_IPV6__ */
  1501. default:
  1502. return EINVAL;
  1503. }
  1504. /* do /etc/hosts first */
  1505. if ((i=get_hosts_byaddr_r(addr, len, type, result_buf,
  1506. buf, buflen, result, h_errnop))==0)
  1507. return i;
  1508. switch (*h_errnop) {
  1509. case HOST_NOT_FOUND:
  1510. case NO_ADDRESS:
  1511. break;
  1512. default:
  1513. return i;
  1514. }
  1515. open_nameservers();
  1516. #ifdef __UCLIBC_HAS_IPV6__
  1517. qp=buf;
  1518. plen=buflen;
  1519. #endif /* __UCLIBC_HAS_IPV6__ */
  1520. *h_errnop = NETDB_INTERNAL;
  1521. if (buflen < sizeof(*in))
  1522. return ERANGE;
  1523. in=(struct in_addr*)buf;
  1524. buf+=sizeof(*in);
  1525. buflen-=sizeof(*in);
  1526. if (buflen < sizeof(*addr_list)*2)
  1527. return ERANGE;
  1528. addr_list=(struct in_addr**)buf;
  1529. buf+=sizeof(*addr_list)*2;
  1530. buflen-=sizeof(*addr_list)*2;
  1531. #ifdef __UCLIBC_HAS_IPV6__
  1532. if (plen < sizeof(*in6))
  1533. return ERANGE;
  1534. in6=(struct in6_addr*)qp;
  1535. qp+=sizeof(*in6);
  1536. plen-=sizeof(*in6);
  1537. if (plen < sizeof(*addr_list6)*2)
  1538. return ERANGE;
  1539. addr_list6=(struct in6_addr**)qp;
  1540. qp+=sizeof(*addr_list6)*2;
  1541. plen-=sizeof(*addr_list6)*2;
  1542. if (len < buflen) {
  1543. buflen=len;
  1544. buf=qp;
  1545. }
  1546. #endif /* __UCLIBC_HAS_IPV6__ */
  1547. if (buflen<256)
  1548. return ERANGE;
  1549. if(type == AF_INET) {
  1550. unsigned char *tmp_addr = (unsigned char *)addr;
  1551. memcpy(&in->s_addr, addr, len);
  1552. addr_list[0] = in;
  1553. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  1554. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  1555. #ifdef __UCLIBC_HAS_IPV6__
  1556. } else {
  1557. memcpy(in6->s6_addr, addr, len);
  1558. addr_list6[0] = in6;
  1559. qp = buf;
  1560. for (i = len - 1; i >= 0; i--) {
  1561. qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
  1562. (in6->s6_addr[i] >> 4) & 0xf);
  1563. }
  1564. strcpy(qp, "ip6.int");
  1565. #endif /* __UCLIBC_HAS_IPV6__ */
  1566. }
  1567. addr_list[1] = 0;
  1568. for (;;) {
  1569. i = dns_lookup(buf, T_PTR, nameservers, nameserver, &packet, &a);
  1570. if (i < 0) {
  1571. *h_errnop = HOST_NOT_FOUND;
  1572. return TRY_AGAIN;
  1573. }
  1574. strncpy(buf, a.dotted, buflen);
  1575. free(a.dotted);
  1576. if (a.atype == T_CNAME) { /* CNAME */
  1577. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  1578. i = decode_dotted(packet, a.rdoffset, buf, buflen);
  1579. free(packet);
  1580. if (i < 0) {
  1581. *h_errnop = NO_RECOVERY;
  1582. return -1;
  1583. }
  1584. if (++nest > MAX_RECURSE) {
  1585. *h_errnop = NO_RECOVERY;
  1586. return -1;
  1587. }
  1588. continue;
  1589. } else if (a.atype == T_PTR) { /* ADDRESS */
  1590. i = decode_dotted(packet, a.rdoffset, buf, buflen);
  1591. free(packet);
  1592. result_buf->h_name = buf;
  1593. result_buf->h_addrtype = type;
  1594. if(type == AF_INET) {
  1595. result_buf->h_length = sizeof(*in);
  1596. #ifdef __UCLIBC_HAS_IPV6__
  1597. } else {
  1598. result_buf->h_length = sizeof(*in6);
  1599. #endif /* __UCLIBC_HAS_IPV6__ */
  1600. }
  1601. result_buf->h_addr_list = (char **) addr_list;
  1602. break;
  1603. } else {
  1604. free(packet);
  1605. *h_errnop = NO_ADDRESS;
  1606. return TRY_AGAIN;
  1607. }
  1608. }
  1609. *result=result_buf;
  1610. return NETDB_SUCCESS;
  1611. }
  1612. #endif