resolv.c 42 KB

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