resolv.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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. struct resolv_header {
  80. int id;
  81. int qr,opcode,aa,tc,rd,ra,rcode;
  82. int qdcount;
  83. int ancount;
  84. int nscount;
  85. int arcount;
  86. };
  87. struct resolv_question {
  88. char * dotted;
  89. int qtype;
  90. int qclass;
  91. };
  92. struct resolv_answer {
  93. char * dotted;
  94. int atype;
  95. int aclass;
  96. int ttl;
  97. int rdlength;
  98. unsigned char * rdata;
  99. int rdoffset;
  100. };
  101. enum etc_hosts_action {
  102. GET_HOSTS_BYNAME = 0,
  103. GETHOSTENT,
  104. GET_HOSTS_BYADDR,
  105. };
  106. extern int nameservers;
  107. extern char * nameserver[MAX_SERVERS];
  108. extern int searchdomains;
  109. extern char * searchdomain[MAX_SEARCH];
  110. extern struct hostent * get_hosts_byname(const char * name, int type);
  111. extern struct hostent * get_hosts_byaddr(const char * addr, int len, int type);
  112. extern void __open_etc_hosts(FILE *fp);
  113. extern struct hostent * read_etc_hosts(FILE *fp, const char * name, int type, enum etc_hosts_action action);
  114. extern int resolve_address(const char * address, int nscount,
  115. char ** nsip, struct in_addr * in);
  116. extern int resolve_mailbox(const char * address, int nscount,
  117. char ** nsip, struct in_addr * in);
  118. extern int dns_lookup(const char * name, int type, int nscount,
  119. char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
  120. int encode_dotted(const char * dotted, unsigned char * dest, int maxlen);
  121. int decode_dotted(const unsigned char * message, int offset,
  122. char * dest, int maxlen);
  123. int length_dotted(const unsigned char * message, int offset);
  124. int encode_header(struct resolv_header * h, unsigned char * dest, int maxlen);
  125. int decode_header(unsigned char * data, struct resolv_header * h);
  126. int encode_question(struct resolv_question * q,
  127. unsigned char * dest, int maxlen);
  128. int decode_question(unsigned char * message, int offset,
  129. struct resolv_question * q);
  130. int encode_answer(struct resolv_answer * a,
  131. unsigned char * dest, int maxlen);
  132. int decode_answer(unsigned char * message, int offset,
  133. struct resolv_answer * a);
  134. int length_question(unsigned char * message, int offset);
  135. extern int open_nameservers(void);
  136. #ifdef L_encodeh
  137. int encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
  138. {
  139. if (maxlen < HFIXEDSZ)
  140. return -1;
  141. dest[0] = (h->id & 0xff00) >> 8;
  142. dest[1] = (h->id & 0x00ff) >> 0;
  143. dest[2] = (h->qr ? 0x80 : 0) |
  144. ((h->opcode & 0x0f) << 3) |
  145. (h->aa ? 0x04 : 0) |
  146. (h->tc ? 0x02 : 0) |
  147. (h->rd ? 0x01 : 0);
  148. dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
  149. dest[4] = (h->qdcount & 0xff00) >> 8;
  150. dest[5] = (h->qdcount & 0x00ff) >> 0;
  151. dest[6] = (h->ancount & 0xff00) >> 8;
  152. dest[7] = (h->ancount & 0x00ff) >> 0;
  153. dest[8] = (h->nscount & 0xff00) >> 8;
  154. dest[9] = (h->nscount & 0x00ff) >> 0;
  155. dest[10] = (h->arcount & 0xff00) >> 8;
  156. dest[11] = (h->arcount & 0x00ff) >> 0;
  157. return HFIXEDSZ;
  158. }
  159. #endif
  160. #ifdef L_decodeh
  161. int decode_header(unsigned char *data, struct resolv_header *h)
  162. {
  163. h->id = (data[0] << 8) | data[1];
  164. h->qr = (data[2] & 0x80) ? 1 : 0;
  165. h->opcode = (data[2] >> 3) & 0x0f;
  166. h->aa = (data[2] & 0x04) ? 1 : 0;
  167. h->tc = (data[2] & 0x02) ? 1 : 0;
  168. h->rd = (data[2] & 0x01) ? 1 : 0;
  169. h->ra = (data[3] & 0x80) ? 1 : 0;
  170. h->rcode = data[3] & 0x0f;
  171. h->qdcount = (data[4] << 8) | data[5];
  172. h->ancount = (data[6] << 8) | data[7];
  173. h->nscount = (data[8] << 8) | data[9];
  174. h->arcount = (data[10] << 8) | data[11];
  175. return HFIXEDSZ;
  176. }
  177. #endif
  178. #ifdef L_encoded
  179. /* Encode a dotted string into nameserver transport-level encoding.
  180. This routine is fairly dumb, and doesn't attempt to compress
  181. the data */
  182. int encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
  183. {
  184. int used = 0;
  185. while (dotted && *dotted) {
  186. char *c = strchr(dotted, '.');
  187. int l = c ? c - dotted : strlen(dotted);
  188. if (l >= (maxlen - used - 1))
  189. return -1;
  190. dest[used++] = l;
  191. memcpy(dest + used, dotted, l);
  192. used += l;
  193. if (c)
  194. dotted = c + 1;
  195. else
  196. break;
  197. }
  198. if (maxlen < 1)
  199. return -1;
  200. dest[used++] = 0;
  201. return used;
  202. }
  203. #endif
  204. #ifdef L_decoded
  205. /* Decode a dotted string from nameserver transport-level encoding.
  206. This routine understands compressed data. */
  207. int decode_dotted(const unsigned char *data, int offset,
  208. char *dest, int maxlen)
  209. {
  210. int l;
  211. int measure = 1;
  212. int total = 0;
  213. int used = 0;
  214. if (!data)
  215. return -1;
  216. while ((l=data[offset++])) {
  217. if (measure)
  218. total++;
  219. if ((l & 0xc0) == (0xc0)) {
  220. if (measure)
  221. total++;
  222. /* compressed item, redirect */
  223. offset = ((l & 0x3f) << 8) | data[offset];
  224. measure = 0;
  225. continue;
  226. }
  227. if ((used + l + 1) >= maxlen)
  228. return -1;
  229. memcpy(dest + used, data + offset, l);
  230. offset += l;
  231. used += l;
  232. if (measure)
  233. total += l;
  234. if (data[offset] != 0)
  235. dest[used++] = '.';
  236. else
  237. dest[used++] = '\0';
  238. }
  239. DPRINTF("Total decode len = %d\n", total);
  240. return total;
  241. }
  242. #endif
  243. #ifdef L_lengthd
  244. int length_dotted(const unsigned char *data, int offset)
  245. {
  246. int orig_offset = offset;
  247. int l;
  248. if (!data)
  249. return -1;
  250. while ((l = data[offset++])) {
  251. if ((l & 0xc0) == (0xc0)) {
  252. offset++;
  253. break;
  254. }
  255. offset += l;
  256. }
  257. return offset - orig_offset;
  258. }
  259. #endif
  260. #ifdef L_encodeq
  261. int encode_question(struct resolv_question *q,
  262. unsigned char *dest, int maxlen)
  263. {
  264. int i;
  265. i = encode_dotted(q->dotted, dest, maxlen);
  266. if (i < 0)
  267. return i;
  268. dest += i;
  269. maxlen -= i;
  270. if (maxlen < 4)
  271. return -1;
  272. dest[0] = (q->qtype & 0xff00) >> 8;
  273. dest[1] = (q->qtype & 0x00ff) >> 0;
  274. dest[2] = (q->qclass & 0xff00) >> 8;
  275. dest[3] = (q->qclass & 0x00ff) >> 0;
  276. return i + 4;
  277. }
  278. #endif
  279. #ifdef L_decodeq
  280. int decode_question(unsigned char *message, int offset,
  281. struct resolv_question *q)
  282. {
  283. char temp[256];
  284. int i;
  285. i = decode_dotted(message, offset, temp, sizeof(temp));
  286. if (i < 0)
  287. return i;
  288. offset += i;
  289. q->dotted = strdup(temp);
  290. q->qtype = (message[offset + 0] << 8) | message[offset + 1];
  291. q->qclass = (message[offset + 2] << 8) | message[offset + 3];
  292. return i + 4;
  293. }
  294. #endif
  295. #ifdef L_lengthq
  296. int length_question(unsigned char *message, int offset)
  297. {
  298. int i;
  299. i = length_dotted(message, offset);
  300. if (i < 0)
  301. return i;
  302. return i + 4;
  303. }
  304. #endif
  305. #ifdef L_encodea
  306. int encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
  307. {
  308. int i;
  309. i = encode_dotted(a->dotted, dest, maxlen);
  310. if (i < 0)
  311. return i;
  312. dest += i;
  313. maxlen -= i;
  314. if (maxlen < (RRFIXEDSZ+a->rdlength))
  315. return -1;
  316. *dest++ = (a->atype & 0xff00) >> 8;
  317. *dest++ = (a->atype & 0x00ff) >> 0;
  318. *dest++ = (a->aclass & 0xff00) >> 8;
  319. *dest++ = (a->aclass & 0x00ff) >> 0;
  320. *dest++ = (a->ttl & 0xff000000) >> 24;
  321. *dest++ = (a->ttl & 0x00ff0000) >> 16;
  322. *dest++ = (a->ttl & 0x0000ff00) >> 8;
  323. *dest++ = (a->ttl & 0x000000ff) >> 0;
  324. *dest++ = (a->rdlength & 0xff00) >> 8;
  325. *dest++ = (a->rdlength & 0x00ff) >> 0;
  326. memcpy(dest, a->rdata, a->rdlength);
  327. return i + RRFIXEDSZ + a->rdlength;
  328. }
  329. #endif
  330. #ifdef L_decodea
  331. int decode_answer(unsigned char *message, int offset,
  332. struct resolv_answer *a)
  333. {
  334. char temp[256];
  335. int i;
  336. i = decode_dotted(message, offset, temp, sizeof(temp));
  337. if (i < 0)
  338. return i;
  339. message += offset + i;
  340. a->dotted = strdup(temp);
  341. a->atype = (message[0] << 8) | message[1];
  342. message += 2;
  343. a->aclass = (message[0] << 8) | message[1];
  344. message += 2;
  345. a->ttl = (message[0] << 24) |
  346. (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
  347. message += 4;
  348. a->rdlength = (message[0] << 8) | message[1];
  349. message += 2;
  350. a->rdata = message;
  351. a->rdoffset = offset + i + RRFIXEDSZ;
  352. DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
  353. return i + RRFIXEDSZ + a->rdlength;
  354. }
  355. #endif
  356. #ifdef L_encodep
  357. int encode_packet(struct resolv_header *h,
  358. struct resolv_question **q,
  359. struct resolv_answer **an,
  360. struct resolv_answer **ns,
  361. struct resolv_answer **ar,
  362. unsigned char *dest, int maxlen)
  363. {
  364. int i, total = 0;
  365. int j;
  366. i = encode_header(h, dest, maxlen);
  367. if (i < 0)
  368. return i;
  369. dest += i;
  370. maxlen -= i;
  371. total += i;
  372. for (j = 0; j < h->qdcount; j++) {
  373. i = encode_question(q[j], dest, maxlen);
  374. if (i < 0)
  375. return i;
  376. dest += i;
  377. maxlen -= i;
  378. total += i;
  379. }
  380. for (j = 0; j < h->ancount; j++) {
  381. i = encode_answer(an[j], dest, maxlen);
  382. if (i < 0)
  383. return i;
  384. dest += i;
  385. maxlen -= i;
  386. total += i;
  387. }
  388. for (j = 0; j < h->nscount; j++) {
  389. i = encode_answer(ns[j], dest, maxlen);
  390. if (i < 0)
  391. return i;
  392. dest += i;
  393. maxlen -= i;
  394. total += i;
  395. }
  396. for (j = 0; j < h->arcount; j++) {
  397. i = encode_answer(ar[j], dest, maxlen);
  398. if (i < 0)
  399. return i;
  400. dest += i;
  401. maxlen -= i;
  402. total += i;
  403. }
  404. return total;
  405. }
  406. #endif
  407. #ifdef L_decodep
  408. int decode_packet(unsigned char *data, struct resolv_header *h)
  409. {
  410. return decode_header(data, h);
  411. }
  412. #endif
  413. #ifdef L_formquery
  414. int form_query(int id, const char *name, int type, unsigned char *packet,
  415. int maxlen)
  416. {
  417. struct resolv_header h;
  418. struct resolv_question q;
  419. int i, j;
  420. memset(&h, 0, sizeof(h));
  421. h.id = id;
  422. h.qdcount = 1;
  423. q.dotted = (char *) name;
  424. q.qtype = type;
  425. q.qclass = C_IN; /* CLASS_IN */
  426. i = encode_header(&h, packet, maxlen);
  427. if (i < 0)
  428. return i;
  429. j = encode_question(&q, packet + i, maxlen - i);
  430. if (j < 0)
  431. return j;
  432. return i + j;
  433. }
  434. #endif
  435. #ifdef L_dnslookup
  436. int dns_caught_signal = 0;
  437. void dns_catch_signal(int signo)
  438. {
  439. dns_caught_signal = 1;
  440. }
  441. int dns_lookup(const char *name, int type, int nscount, char **nsip,
  442. unsigned char **outpacket, struct resolv_answer *a)
  443. {
  444. static int id = 1;
  445. int i, j, len, fd, pos;
  446. static int ns = 0;
  447. struct sockaddr_in sa;
  448. #ifdef __UCLIBC_HAS_IPV6__
  449. struct sockaddr_in6 sa6;
  450. #endif /* __UCLIBC_HAS_IPV6__ */
  451. int oldalarm;
  452. __sighandler_t oldhandler;
  453. struct resolv_header h;
  454. struct resolv_question q;
  455. int retries = 0;
  456. unsigned char * packet = malloc(PACKETSZ);
  457. unsigned char * lookup = malloc(MAXDNAME);
  458. int variant = 0;
  459. #ifdef __UCLIBC_HAS_IPV6__
  460. int v6;
  461. #endif /* __UCLIBC_HAS_IPV6__ */
  462. fd = -1;
  463. if (!packet || !lookup || !nscount)
  464. goto fail;
  465. DPRINTF("Looking up type %d answer for '%s'\n", type, name);
  466. ns %= nscount;
  467. while (retries++ < MAX_RETRIES) {
  468. #ifdef __UCLIBC_HAS_IPV6__
  469. v6 = (inet_pton(AF_INET6, nsip[ns], &sa6.sin6_addr) > 0);
  470. #endif /* __UCLIBC_HAS_IPV6__ */
  471. if (fd != -1)
  472. close(fd);
  473. #ifndef __UCLIBC_HAS_IPV6__
  474. fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  475. #else /* __UCLIBC_HAS_IPV6__ */
  476. fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  477. #endif /* __UCLIBC_HAS_IPV6__ */
  478. if (fd == -1)
  479. goto fail;
  480. memset(packet, 0, PACKETSZ);
  481. memset(&h, 0, sizeof(h));
  482. h.id = ++id;
  483. h.qdcount = 1;
  484. h.rd = 1;
  485. DPRINTF("encoding header\n", h.rd);
  486. i = encode_header(&h, packet, PACKETSZ);
  487. if (i < 0)
  488. goto fail;
  489. strncpy(lookup,name,MAXDNAME);
  490. if (variant < searchdomains && strchr(lookup, '.') == NULL)
  491. {
  492. strncat(lookup,".", MAXDNAME);
  493. strncat(lookup,searchdomain[variant], MAXDNAME);
  494. }
  495. DPRINTF("lookup name: %s\n", lookup);
  496. q.dotted = (char *)lookup;
  497. q.qtype = type;
  498. q.qclass = C_IN; /* CLASS_IN */
  499. j = encode_question(&q, packet+i, PACKETSZ-i);
  500. if (j < 0)
  501. goto fail;
  502. len = i + j;
  503. DPRINTF("On try %d, sending query to port %d of machine %s\n",
  504. retries, NAMESERVER_PORT, nsip[ns]);
  505. #ifndef __UCLIBC_HAS_IPV6__
  506. sa.sin_family = AF_INET;
  507. sa.sin_port = htons(NAMESERVER_PORT);
  508. sa.sin_addr.s_addr = inet_addr(nsip[ns]);
  509. #else /* __UCLIBC_HAS_IPV6__ */
  510. if (v6) {
  511. sa6.sin6_family = AF_INET6;
  512. sa6.sin6_port = htons(NAMESERVER_PORT);
  513. /* sa6.sin6_addr is already here */
  514. } else {
  515. sa.sin_family = AF_INET;
  516. sa.sin_port = htons(NAMESERVER_PORT);
  517. sa.sin_addr.s_addr = inet_addr(nsip[ns]);
  518. }
  519. #endif /* __UCLIBC_HAS_IPV6__ */
  520. #ifndef __UCLIBC_HAS_IPV6__
  521. if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
  522. #else /* __UCLIBC_HAS_IPV6__ */
  523. if (connect(fd, (struct sockaddr *) (v6 ? &sa6 : &sa),
  524. v6 ? sizeof(sa6) : sizeof(sa)) == -1) {
  525. #endif /* __UCLIBC_HAS_IPV6__ */
  526. if (errno == ENETUNREACH) {
  527. /* routing error, presume not transient */
  528. goto tryall;
  529. } else
  530. /* retry */
  531. continue;
  532. }
  533. DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
  534. len, h.id, h.qr);
  535. send(fd, packet, len, 0);
  536. dns_caught_signal = 0;
  537. oldalarm = alarm(REPLY_TIMEOUT);
  538. oldhandler = signal(SIGALRM, dns_catch_signal);
  539. i = recv(fd, packet, PACKETSZ, 0);
  540. alarm(0);
  541. signal(SIGALRM, oldhandler);
  542. alarm(oldalarm);
  543. DPRINTF("Timeout=%d, len=%d\n", dns_caught_signal, i);
  544. if (dns_caught_signal)
  545. /* timed out, so retry send and receive,
  546. to next nameserver on queue */
  547. goto again;
  548. if (i < HFIXEDSZ)
  549. /* too short ! */
  550. goto again;
  551. decode_header(packet, &h);
  552. DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
  553. if ((h.id != id) || (!h.qr))
  554. /* unsolicited */
  555. goto again;
  556. DPRINTF("Got response %s\n", "(i think)!");
  557. DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
  558. h.qdcount, h.ancount, h.nscount, h.arcount);
  559. DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
  560. h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
  561. if ((h.rcode) || (h.ancount < 1)) {
  562. /* negative result, not present */
  563. goto again;
  564. }
  565. pos = HFIXEDSZ;
  566. for (j = 0; j < h.qdcount; j++) {
  567. DPRINTF("Skipping question %d at %d\n", j, pos);
  568. i = length_question(packet, pos);
  569. DPRINTF("Length of question %d is %d\n", j, i);
  570. if (i < 0)
  571. goto again;
  572. pos += i;
  573. }
  574. DPRINTF("Decoding answer at pos %d\n", pos);
  575. for (j=0;j<h.ancount;j++)
  576. {
  577. i = decode_answer(packet, pos, a);
  578. if (i<0) {
  579. DPRINTF("failed decode %d\n", i);
  580. goto again;
  581. }
  582. /* For all but T_SIG, accept first answer */
  583. if (a->atype != T_SIG)
  584. break;
  585. DPRINTF("skipping T_SIG %d\n", i);
  586. free(a->dotted);
  587. pos += i;
  588. }
  589. DPRINTF("Answer name = |%s|\n", a->dotted);
  590. DPRINTF("Answer type = |%d|\n", a->atype);
  591. close(fd);
  592. if (outpacket)
  593. *outpacket = packet;
  594. else
  595. free(packet);
  596. free(lookup);
  597. return (0); /* success! */
  598. tryall:
  599. /* if there are other nameservers, give them a go,
  600. otherwise return with error */
  601. variant = 0;
  602. if (retries >= nscount*(searchdomains+1))
  603. goto fail;
  604. again:
  605. /* if there are searchdomains, try them or fallback as passed */
  606. if (variant < searchdomains) {
  607. /* next search */
  608. variant++;
  609. } else {
  610. /* next server, first search */
  611. ns = (ns + 1) % nscount;
  612. variant = 0;
  613. }
  614. }
  615. fail:
  616. if (fd != -1)
  617. close(fd);
  618. if (lookup)
  619. free(lookup);
  620. if (packet)
  621. free(packet);
  622. return -1;
  623. }
  624. #endif
  625. #ifdef L_resolveaddress
  626. int resolve_address(const char *address, int nscount,
  627. char **nsip, struct in_addr *in)
  628. {
  629. unsigned char *packet;
  630. struct resolv_answer a;
  631. char temp[256];
  632. int i;
  633. int nest = 0;
  634. if (!address || !in)
  635. return -1;
  636. strncpy(temp, address, sizeof(temp));
  637. for (;;) {
  638. i = dns_lookup(temp, T_A, nscount, nsip, &packet, &a);
  639. if (i < 0)
  640. return -1;
  641. free(a.dotted);
  642. if (a.atype == T_CNAME) { /* CNAME */
  643. DPRINTF("Got a CNAME in resolve_address()\n");
  644. i = decode_dotted(packet, a.rdoffset, temp, sizeof(temp));
  645. free(packet);
  646. if (i < 0)
  647. return -1;
  648. if (++nest > MAX_RECURSE)
  649. return -1;
  650. continue;
  651. } else if (a.atype == T_A) { /* ADDRESS */
  652. free(packet);
  653. break;
  654. } else {
  655. free(packet);
  656. return -1;
  657. }
  658. }
  659. if (in)
  660. memcpy(in, a.rdata, INADDRSZ); /* IPv4 T_A */
  661. return 0;
  662. }
  663. #endif
  664. #ifdef L_resolvemailbox
  665. int resolve_mailbox(const char *address, int nscount,
  666. char **nsip, struct in_addr *in)
  667. {
  668. struct resolv_answer a;
  669. unsigned char *packet;
  670. char temp[256];
  671. int nest = 0;
  672. int i;
  673. if (!address || !in)
  674. return -1;
  675. /* look up mail exchange */
  676. i = dns_lookup(address, T_MX, nscount, nsip, &packet, &a);
  677. strncpy(temp, address, sizeof(temp));
  678. if (i >= 0) {
  679. i = decode_dotted(packet, a.rdoffset+2, temp, sizeof(temp));
  680. free(packet);
  681. }
  682. for (;;) {
  683. i = dns_lookup(temp, T_A, nscount, nsip, &packet, &a);
  684. if (i < 0)
  685. return -1;
  686. free(a.dotted);
  687. if (a.atype == T_CNAME) { /* CNAME */
  688. DPRINTF("Got a CNAME in resolve_mailbox()\n");
  689. i = decode_dotted(packet, a.rdoffset, temp, sizeof(temp));
  690. free(packet);
  691. if (i < 0)
  692. return i;
  693. if (++nest > MAX_RECURSE)
  694. return -1;
  695. continue;
  696. } else if (a.atype == T_A) { /* ADDRESS */
  697. free(packet);
  698. break;
  699. } else {
  700. free(packet);
  701. return -1;
  702. }
  703. }
  704. if (in)
  705. memcpy(in, a.rdata, INADDRSZ); /* IPv4 */
  706. return 0;
  707. }
  708. #endif
  709. #ifdef L_opennameservers
  710. int nameservers;
  711. char * nameserver[MAX_SERVERS];
  712. int searchdomains;
  713. char * searchdomain[MAX_SEARCH];
  714. /*
  715. * we currently read formats not quite the same as that on normal
  716. * unix systems, we can have a list of nameservers after the keyword.
  717. */
  718. int open_nameservers()
  719. {
  720. FILE *fp;
  721. int i;
  722. #define RESOLV_ARGS 5
  723. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  724. int argc;
  725. if (nameservers > 0)
  726. return 0;
  727. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  728. (fp = fopen("/etc/config/resolv.conf", "r"))) {
  729. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  730. for (p = szBuffer; *p && isspace(*p); p++)
  731. /* skip white space */;
  732. if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  733. continue;
  734. argc = 0;
  735. while (*p && argc < RESOLV_ARGS) {
  736. argv[argc++] = p;
  737. while (*p && !isspace(*p) && *p != '\n')
  738. p++;
  739. while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  740. *p++ = '\0';
  741. }
  742. if (strcmp(argv[0], "nameserver") == 0) {
  743. for (i = 1; i < argc && nameservers < MAX_SERVERS; i++) {
  744. nameserver[nameservers++] = strdup(argv[i]);
  745. DPRINTF("adding nameserver %s\n", argv[i]);
  746. }
  747. }
  748. /* domain and search are mutually exclusive, the last one wins */
  749. if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  750. while (searchdomains > 0) {
  751. free(searchdomain[--searchdomains]);
  752. searchdomain[searchdomains] = NULL;
  753. }
  754. for (i=1; i < argc && searchdomains < MAX_SEARCH; i++) {
  755. searchdomain[searchdomains++] = strdup(argv[i]);
  756. DPRINTF("adding search %s\n", argv[i]);
  757. }
  758. }
  759. }
  760. fclose(fp);
  761. } else {
  762. DPRINTF("failed to open %s\n", "resolv.conf");
  763. }
  764. DPRINTF("nameservers = %d\n", nameservers);
  765. return 0;
  766. }
  767. #endif
  768. #ifdef L_closenameservers
  769. void close_nameservers(void)
  770. {
  771. while (nameservers > 0) {
  772. free(nameserver[--nameservers]);
  773. nameserver[nameservers] = NULL;
  774. }
  775. while (searchdomains > 0) {
  776. free(searchdomain[--searchdomains]);
  777. searchdomain[searchdomains] = NULL;
  778. }
  779. }
  780. #endif
  781. #ifdef L_resolvename
  782. const char *resolve_name(const char *name, int mailbox)
  783. {
  784. struct in_addr in;
  785. int i;
  786. /* shortcut: is it a valid IP address to begin with? */
  787. if (inet_aton(name, &in))
  788. return name;
  789. open_nameservers();
  790. DPRINTF("looking up '%s', mailbox=%d, nameservers=%d\n",
  791. name, mailbox, nameservers);
  792. if (mailbox)
  793. i = resolve_mailbox(name, nameservers, nameserver, &in);
  794. else
  795. i = resolve_address(name, nameservers, nameserver, &in);
  796. if (i < 0)
  797. return 0;
  798. DPRINTF("success = '%s'\n", inet_ntoa(in));
  799. return inet_ntoa(in);
  800. }
  801. #endif
  802. #ifdef L_gethostbyname
  803. struct hostent *gethostbyname(const char *name)
  804. {
  805. static struct hostent h;
  806. static char namebuf[256];
  807. static struct in_addr in;
  808. static struct in_addr *addr_list[2];
  809. struct hostent *hp;
  810. unsigned char *packet;
  811. struct resolv_answer a;
  812. int i;
  813. int nest = 0;
  814. open_nameservers();
  815. if (!name)
  816. return 0;
  817. if ((hp = get_hosts_byname(name, AF_INET))) /* do /etc/hosts first */
  818. return(hp);
  819. memset(&h, 0, sizeof(h));
  820. addr_list[0] = &in;
  821. addr_list[1] = 0;
  822. strncpy(namebuf, name, sizeof(namebuf));
  823. /* First check if this is already an address */
  824. if (inet_aton(name, &in)) {
  825. h.h_name = namebuf;
  826. h.h_addrtype = AF_INET;
  827. h.h_length = sizeof(in);
  828. h.h_addr_list = (char **) addr_list;
  829. return &h;
  830. }
  831. for (;;) {
  832. i = dns_lookup(namebuf, 1, nameservers, nameserver, &packet, &a);
  833. if (i < 0)
  834. return 0;
  835. strncpy(namebuf, a.dotted, sizeof(namebuf));
  836. free(a.dotted);
  837. if (a.atype == T_CNAME) { /* CNAME */
  838. DPRINTF("Got a CNAME in gethostbyname()\n");
  839. i = decode_dotted(packet, a.rdoffset, namebuf, sizeof(namebuf));
  840. free(packet);
  841. if (i < 0)
  842. return 0;
  843. if (++nest > MAX_RECURSE)
  844. return 0;
  845. continue;
  846. } else if (a.atype == T_A) { /* ADDRESS */
  847. memcpy(&in, a.rdata, sizeof(in));
  848. h.h_name = namebuf;
  849. h.h_addrtype = AF_INET;
  850. h.h_length = sizeof(in);
  851. h.h_addr_list = (char **) addr_list;
  852. free(packet);
  853. break;
  854. } else {
  855. free(packet);
  856. return 0;
  857. }
  858. }
  859. return &h;
  860. }
  861. #endif
  862. #ifdef L_gethostbyname2
  863. #ifdef __UCLIBC_HAS_IPV6__
  864. /* TBD: Not the right place for defining these, I guess */
  865. const struct in6_addr in6addr_any =
  866. { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
  867. const struct in6_addr in6addr_loopback =
  868. { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } };
  869. #endif /* __UCLIBC_HAS_IPV6__ */
  870. struct hostent *gethostbyname2(const char *name, int family)
  871. {
  872. #ifndef __UCLIBC_HAS_IPV6__
  873. return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
  874. #else /* __UCLIBC_HAS_IPV6__ */
  875. static struct hostent h;
  876. static char namebuf[256];
  877. static struct in6_addr in;
  878. static struct in6_addr *addr_list[2];
  879. struct hostent *hp;
  880. unsigned char *packet;
  881. struct resolv_answer a;
  882. int i;
  883. int nest = 0;
  884. if (family == AF_INET)
  885. return gethostbyname(name);
  886. if (family != AF_INET6)
  887. return NULL;
  888. open_nameservers();
  889. if (!name)
  890. return 0;
  891. if ((hp = get_hosts_byname(name, family))) /* do /etc/hosts first */
  892. return(hp);
  893. memset(&h, 0, sizeof(h));
  894. addr_list[0] = &in;
  895. addr_list[1] = 0;
  896. strncpy(namebuf, name, sizeof(namebuf));
  897. /* First check if this is already an address */
  898. if (inet_pton(AF_INET6, name, &in)) {
  899. h.h_name = namebuf;
  900. h.h_addrtype = AF_INET6;
  901. h.h_length = sizeof(in);
  902. h.h_addr_list = (char **) addr_list;
  903. return &h;
  904. }
  905. for (;;) {
  906. i = dns_lookup(namebuf, T_AAAA, nameservers, nameserver, &packet, &a);
  907. if (i < 0)
  908. return 0;
  909. strncpy(namebuf, a.dotted, sizeof(namebuf));
  910. free(a.dotted);
  911. if (a.atype == T_CNAME) { /* CNAME */
  912. DPRINTF("Got a CNAME in gethostbyname()\n");
  913. i = decode_dotted(packet, a.rdoffset, namebuf, sizeof(namebuf));
  914. free(packet);
  915. if (i < 0)
  916. return 0;
  917. if (++nest > MAX_RECURSE)
  918. return 0;
  919. continue;
  920. } else if (a.atype == T_AAAA) { /* ADDRESS */
  921. memcpy(&in, a.rdata, sizeof(in));
  922. h.h_name = namebuf;
  923. h.h_addrtype = AF_INET6;
  924. h.h_length = sizeof(in);
  925. h.h_addr_list = (char **) addr_list;
  926. free(packet);
  927. break;
  928. } else {
  929. free(packet);
  930. return 0;
  931. }
  932. }
  933. return &h;
  934. #endif /* __UCLIBC_HAS_IPV6__ */
  935. }
  936. #endif
  937. #ifdef L_getnetbyname
  938. struct netent * getnetbyname(const char * name)
  939. {
  940. return NULL;
  941. }
  942. #endif
  943. #ifdef L_res_init
  944. int res_init(void)
  945. {
  946. return(0);
  947. }
  948. #endif
  949. #ifdef L_res_query
  950. #ifndef MIN
  951. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  952. #endif
  953. int res_query(const char *dname, int class, int type,
  954. unsigned char *answer, int anslen)
  955. {
  956. unsigned char * packet = 0;
  957. struct resolv_answer a;
  958. int i;
  959. open_nameservers();
  960. if (!dname || class != 1 /* CLASS_IN */)
  961. return(-1);
  962. memset((char *) &a, '\0', sizeof(a));
  963. i = dns_lookup(dname, type, nameservers, nameserver, &packet, &a);
  964. if (i < 0)
  965. return(-1);
  966. free(a.dotted);
  967. if (a.atype == type) { /* CNAME*/
  968. if (anslen && answer)
  969. memcpy(answer, a.rdata, MIN(anslen, a.rdlength));
  970. if (packet)
  971. free(packet);
  972. return(MIN(anslen, a.rdlength));
  973. }
  974. if (packet)
  975. free(packet);
  976. return 0;
  977. }
  978. #endif
  979. #ifdef L_gethostbyaddr
  980. struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
  981. {
  982. static struct hostent h;
  983. static char namebuf[256];
  984. static struct in_addr in;
  985. static struct in_addr *addr_list[2];
  986. #ifdef __UCLIBC_HAS_IPV6__
  987. char *qp;
  988. static struct in6_addr in6;
  989. static struct in6_addr *addr_list6[2];
  990. #endif /* __UCLIBC_HAS_IPV6__ */
  991. struct hostent *hp;
  992. unsigned char *packet;
  993. struct resolv_answer a;
  994. int i;
  995. int nest = 0;
  996. if (!addr)
  997. return 0;
  998. switch (type) {
  999. case AF_INET:
  1000. if (len != sizeof(struct in_addr))
  1001. return 0;
  1002. break;
  1003. #ifdef __UCLIBC_HAS_IPV6__
  1004. case AF_INET6:
  1005. if (len != sizeof(struct in6_addr))
  1006. return 0;
  1007. break;
  1008. #endif /* __UCLIBC_HAS_IPV6__ */
  1009. default:
  1010. return 0;
  1011. }
  1012. if ((hp = get_hosts_byaddr(addr, len, type))) /* do /etc/hosts first */
  1013. return(hp);
  1014. open_nameservers();
  1015. memset(&h, 0, sizeof(h));
  1016. if(type == AF_INET) {
  1017. unsigned char *tmp_addr = (unsigned char *)addr;
  1018. memcpy(&in.s_addr, addr, len);
  1019. addr_list[0] = &in;
  1020. sprintf(namebuf, "%u.%u.%u.%u.in-addr.arpa",
  1021. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  1022. #ifdef __UCLIBC_HAS_IPV6__
  1023. } else {
  1024. memcpy(&in6.s6_addr, addr, len);
  1025. addr_list6[0] = &in6;
  1026. qp = namebuf;
  1027. for (i = len - 1; i >= 0; i--) {
  1028. qp += sprintf(qp, "%x.%x.", in6.s6_addr[i] & 0xf,
  1029. (in6.s6_addr[i] >> 4) & 0xf);
  1030. }
  1031. strcpy(qp, "ip6.int");
  1032. #endif /* __UCLIBC_HAS_IPV6__ */
  1033. }
  1034. addr_list[1] = 0;
  1035. for (;;) {
  1036. i = dns_lookup(namebuf, T_PTR, nameservers, nameserver, &packet, &a);
  1037. if (i < 0)
  1038. return 0;
  1039. strncpy(namebuf, a.dotted, sizeof(namebuf));
  1040. free(a.dotted);
  1041. if (a.atype == T_CNAME) { /* CNAME */
  1042. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  1043. i = decode_dotted(packet, a.rdoffset, namebuf, sizeof(namebuf));
  1044. free(packet);
  1045. if (i < 0)
  1046. return 0;
  1047. if (++nest > MAX_RECURSE)
  1048. return 0;
  1049. continue;
  1050. } else if (a.atype == T_PTR) { /* ADDRESS */
  1051. i = decode_dotted(packet, a.rdoffset, namebuf, sizeof(namebuf));
  1052. free(packet);
  1053. h.h_name = namebuf;
  1054. h.h_addrtype = type;
  1055. if(type == AF_INET) {
  1056. h.h_length = sizeof(in);
  1057. #ifdef __UCLIBC_HAS_IPV6__
  1058. } else {
  1059. h.h_length = sizeof(in6);
  1060. #endif /* __UCLIBC_HAS_IPV6__ */
  1061. }
  1062. h.h_addr_list = (char **) addr_list;
  1063. break;
  1064. } else {
  1065. free(packet);
  1066. return 0;
  1067. }
  1068. }
  1069. return &h;
  1070. }
  1071. #endif
  1072. #ifdef L_read_etc_hosts
  1073. void __open_etc_hosts(FILE *fp)
  1074. {
  1075. if ((fp = fopen("/etc/hosts", "r")) == NULL) {
  1076. fp = fopen("/etc/config/hosts", "r");
  1077. }
  1078. return;
  1079. }
  1080. struct hostent * read_etc_hosts(FILE * fp, const char * name, int type, enum etc_hosts_action action)
  1081. {
  1082. static struct hostent h;
  1083. static struct in_addr in;
  1084. static struct in_addr *addr_list[2];
  1085. #ifdef __UCLIBC_HAS_IPV6__
  1086. static struct in6_addr in6;
  1087. static struct in6_addr *addr_list6[2];
  1088. #endif /* __UCLIBC_HAS_IPV6__ */
  1089. static char line[80];
  1090. char *cp;
  1091. #define MAX_ALIAS 5
  1092. char *alias[MAX_ALIAS];
  1093. int aliases, i;
  1094. if (action!=GETHOSTENT) {
  1095. __open_etc_hosts(fp);
  1096. if (fp == NULL) {
  1097. return((struct hostent *)NULL);
  1098. }
  1099. }
  1100. while (fgets(line, sizeof(line), fp)) {
  1101. if ((cp = strchr(line, '#')))
  1102. *cp = '\0';
  1103. aliases = 0;
  1104. cp = line;
  1105. while (*cp) {
  1106. while (*cp && isspace(*cp))
  1107. *cp++ = '\0';
  1108. if (!*cp)
  1109. continue;
  1110. if (aliases < MAX_ALIAS)
  1111. alias[aliases++] = cp;
  1112. while (*cp && !isspace(*cp))
  1113. cp++;
  1114. }
  1115. if (aliases < 2)
  1116. continue; /* syntax error really */
  1117. if (action==GETHOSTENT) {
  1118. /* Return whatever the next entry happens to be. */
  1119. break;
  1120. } else if (action==GET_HOSTS_BYADDR) {
  1121. if (strcmp(name, alias[0]) != 0)
  1122. continue;
  1123. } else {
  1124. /* GET_HOSTS_BYNAME */
  1125. for (i = 1; i < aliases; i++)
  1126. if (strcasecmp(name, alias[i]) == 0)
  1127. break;
  1128. if (i >= aliases)
  1129. continue;
  1130. }
  1131. if (type == AF_INET && inet_pton(AF_INET, alias[0], &in) > 0) {
  1132. addr_list[0] = &in;
  1133. addr_list[1] = 0;
  1134. h.h_name = alias[1];
  1135. h.h_addrtype = AF_INET;
  1136. h.h_length = sizeof(in);
  1137. h.h_addr_list = (char**) addr_list;
  1138. #ifdef __UCLIBC_HAS_IPV6__
  1139. } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], &in6) > 0) {
  1140. addr_list6[0] = &in6;
  1141. addr_list6[1] = 0;
  1142. h.h_name = alias[1];
  1143. h.h_addrtype = AF_INET6;
  1144. h.h_length = sizeof(in6);
  1145. h.h_addr_list = (char**) addr_list6;
  1146. #endif /* __UCLIBC_HAS_IPV6__ */
  1147. } else {
  1148. break; /* bad ip address */
  1149. }
  1150. if (action!=GETHOSTENT) {
  1151. fclose(fp);
  1152. }
  1153. return(&h);
  1154. }
  1155. if (action!=GETHOSTENT) {
  1156. fclose(fp);
  1157. }
  1158. return((struct hostent *) NULL);
  1159. }
  1160. #endif
  1161. #ifdef L_endhostent
  1162. extern int __stay_open;
  1163. extern FILE * __gethostent_fp;
  1164. void endhostent (void)
  1165. {
  1166. __stay_open = 0;
  1167. if (__gethostent_fp) {
  1168. fclose(__gethostent_fp);
  1169. }
  1170. }
  1171. #endif
  1172. #ifdef L_sethostent
  1173. extern int __stay_open;
  1174. void sethostent (int stay_open)
  1175. {
  1176. __stay_open = stay_open;
  1177. }
  1178. #endif
  1179. #ifdef L_gethostent
  1180. int __stay_open;
  1181. FILE * __gethostent_fp;
  1182. struct hostent *gethostent (void)
  1183. {
  1184. struct hostent *host;
  1185. if (__gethostent_fp == NULL) {
  1186. __open_etc_hosts(__gethostent_fp);
  1187. if (__gethostent_fp == NULL) {
  1188. return((struct hostent *)NULL);
  1189. }
  1190. }
  1191. host = read_etc_hosts(__gethostent_fp, NULL, AF_INET, GETHOSTENT);
  1192. if (__stay_open==0) {
  1193. fclose(__gethostent_fp);
  1194. }
  1195. return(host);
  1196. }
  1197. #endif
  1198. #ifdef L_get_hosts_byname
  1199. struct hostent * get_hosts_byname(const char * name, int type)
  1200. {
  1201. return(read_etc_hosts(NULL, name, type, GET_HOSTS_BYNAME));
  1202. }
  1203. #endif
  1204. #ifdef L_get_hosts_byaddr
  1205. struct hostent * get_hosts_byaddr(const char * addr, int len, int type)
  1206. {
  1207. #ifndef __UCLIBC_HAS_IPV6__
  1208. char ipaddr[INET_ADDRSTRLEN];
  1209. #else
  1210. char ipaddr[INET6_ADDRSTRLEN];
  1211. #endif /* __UCLIBC_HAS_IPV6__ */
  1212. switch (type) {
  1213. case AF_INET:
  1214. if (len != sizeof(struct in_addr))
  1215. return 0;
  1216. break;
  1217. #ifdef __UCLIBC_HAS_IPV6__
  1218. case AF_INET6:
  1219. if (len != sizeof(struct in6_addr))
  1220. return 0;
  1221. break;
  1222. #endif /* __UCLIBC_HAS_IPV6__ */
  1223. default:
  1224. return 0;
  1225. }
  1226. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1227. return(read_etc_hosts(NULL, ipaddr, type, GET_HOSTS_BYADDR));
  1228. }
  1229. #endif
  1230. #ifdef L_getnameinfo
  1231. #ifndef min
  1232. # define min(x,y) (((x) > (y)) ? (y) : (x))
  1233. #endif /* min */
  1234. int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  1235. socklen_t hostlen, char *serv, socklen_t servlen,
  1236. unsigned int flags)
  1237. {
  1238. int serrno = errno;
  1239. int ok = 0;
  1240. struct hostent *h = NULL;
  1241. char domain[256];
  1242. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1243. return EAI_BADFLAGS;
  1244. if (sa == NULL || addrlen < sizeof (sa_family_t))
  1245. return EAI_FAMILY;
  1246. switch (sa->sa_family) {
  1247. case AF_LOCAL:
  1248. break;
  1249. case AF_INET:
  1250. if (addrlen < sizeof (struct sockaddr_in))
  1251. return EAI_FAMILY;
  1252. break;
  1253. #ifdef __UCLIBC_HAS_IPV6__
  1254. case AF_INET6:
  1255. if (addrlen < sizeof (struct sockaddr_in6))
  1256. return EAI_FAMILY;
  1257. break;
  1258. #endif /* __UCLIBC_HAS_IPV6__ */
  1259. default:
  1260. return EAI_FAMILY;
  1261. }
  1262. if (host != NULL && hostlen > 0)
  1263. switch (sa->sa_family) {
  1264. case AF_INET:
  1265. #ifdef __UCLIBC_HAS_IPV6__
  1266. case AF_INET6:
  1267. #endif /* __UCLIBC_HAS_IPV6__ */
  1268. if (!(flags & NI_NUMERICHOST)) {
  1269. #ifdef __UCLIBC_HAS_IPV6__
  1270. if (sa->sa_family == AF_INET6)
  1271. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1272. sizeof(struct in6_addr), AF_INET6);
  1273. else
  1274. #endif /* __UCLIBC_HAS_IPV6__ */
  1275. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1276. sizeof(struct in_addr), AF_INET);
  1277. if (h) {
  1278. char *c;
  1279. if ((flags & NI_NOFQDN)
  1280. && (getdomainname (domain, sizeof(domain)) == 0)
  1281. && (c = strstr (h->h_name, domain))
  1282. && (c != h->h_name) && (*(--c) == '.')) {
  1283. strncpy (host, h->h_name,
  1284. min(hostlen, (size_t) (c - h->h_name)));
  1285. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1286. ok = 1;
  1287. } else {
  1288. strncpy (host, h->h_name, hostlen);
  1289. ok = 1;
  1290. }
  1291. }
  1292. }
  1293. if (!ok) {
  1294. if (flags & NI_NAMEREQD) {
  1295. errno = serrno;
  1296. return EAI_NONAME;
  1297. } else {
  1298. const char *c;
  1299. #ifdef __UCLIBC_HAS_IPV6__
  1300. if (sa->sa_family == AF_INET6) {
  1301. const struct sockaddr_in6 *sin6p;
  1302. sin6p = (const struct sockaddr_in6 *) sa;
  1303. c = inet_ntop (AF_INET6,
  1304. (const void *) &sin6p->sin6_addr, host, hostlen);
  1305. #if 0
  1306. /* Does scope id need to be supported? */
  1307. uint32_t scopeid;
  1308. scopeid = sin6p->sin6_scope_id;
  1309. if (scopeid != 0) {
  1310. /* Buffer is >= IFNAMSIZ+1. */
  1311. char scopebuf[IFNAMSIZ + 1];
  1312. char *scopeptr;
  1313. int ni_numericscope = 0;
  1314. size_t real_hostlen = __strnlen (host, hostlen);
  1315. size_t scopelen = 0;
  1316. scopebuf[0] = SCOPE_DELIMITER;
  1317. scopebuf[1] = '\0';
  1318. scopeptr = &scopebuf[1];
  1319. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1320. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1321. if (if_indextoname (scopeid, scopeptr) == NULL)
  1322. ++ni_numericscope;
  1323. else
  1324. scopelen = strlen (scopebuf);
  1325. } else {
  1326. ++ni_numericscope;
  1327. }
  1328. if (ni_numericscope)
  1329. scopelen = 1 + snprintf (scopeptr,
  1330. (scopebuf
  1331. + sizeof scopebuf
  1332. - scopeptr),
  1333. "%u", scopeid);
  1334. if (real_hostlen + scopelen + 1 > hostlen)
  1335. return EAI_SYSTEM;
  1336. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1337. }
  1338. #endif
  1339. } else
  1340. #endif /* __UCLIBC_HAS_IPV6__ */
  1341. c = inet_ntop (AF_INET,
  1342. (const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
  1343. host, hostlen);
  1344. if (c == NULL) {
  1345. errno = serrno;
  1346. return EAI_SYSTEM;
  1347. }
  1348. }
  1349. ok = 1;
  1350. }
  1351. break;
  1352. case AF_LOCAL:
  1353. if (!(flags & NI_NUMERICHOST)) {
  1354. struct utsname utsname;
  1355. if (!uname (&utsname)) {
  1356. strncpy (host, utsname.nodename, hostlen);
  1357. break;
  1358. };
  1359. };
  1360. if (flags & NI_NAMEREQD) {
  1361. errno = serrno;
  1362. return EAI_NONAME;
  1363. }
  1364. strncpy (host, "localhost", hostlen);
  1365. break;
  1366. default:
  1367. return EAI_FAMILY;
  1368. }
  1369. if (serv && (servlen > 0)) {
  1370. switch (sa->sa_family) {
  1371. case AF_INET:
  1372. #ifdef __UCLIBC_HAS_IPV6__
  1373. case AF_INET6:
  1374. #endif /* __UCLIBC_HAS_IPV6__ */
  1375. if (!(flags & NI_NUMERICSERV)) {
  1376. struct servent *s;
  1377. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1378. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1379. if (s) {
  1380. strncpy (serv, s->s_name, servlen);
  1381. break;
  1382. }
  1383. }
  1384. snprintf (serv, servlen, "%d",
  1385. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1386. break;
  1387. case AF_LOCAL:
  1388. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1389. break;
  1390. }
  1391. }
  1392. if (host && (hostlen > 0))
  1393. host[hostlen-1] = 0;
  1394. if (serv && (servlen > 0))
  1395. serv[servlen-1] = 0;
  1396. errno = serrno;
  1397. return 0;
  1398. }
  1399. #endif