resolv.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  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 * 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. while (retries++ < MAX_RETRIES) {
  519. if (fd != -1)
  520. close(fd);
  521. memset(packet, 0, PACKETSZ);
  522. memset(&h, 0, sizeof(h));
  523. h.id = ++id;
  524. h.qdcount = 1;
  525. h.rd = 1;
  526. DPRINTF("encoding header\n", h.rd);
  527. i = __encode_header(&h, packet, PACKETSZ);
  528. if (i < 0)
  529. goto fail;
  530. strncpy(lookup,name,MAXDNAME);
  531. if (variant < __searchdomains && strchr(lookup, '.') == NULL)
  532. {
  533. strncat(lookup,".", MAXDNAME);
  534. strncat(lookup,__searchdomain[variant], MAXDNAME);
  535. }
  536. DPRINTF("lookup name: %s\n", lookup);
  537. q.dotted = (char *)lookup;
  538. q.qtype = type;
  539. q.qclass = C_IN; /* CLASS_IN */
  540. j = __encode_question(&q, packet+i, PACKETSZ-i);
  541. if (j < 0)
  542. goto fail;
  543. len = i + j;
  544. DPRINTF("On try %d, sending query to port %d of machine %s\n",
  545. retries, NAMESERVER_PORT, nsip[ns]);
  546. fd = __connect_dns(nsip[ns]);
  547. if (fd < 0) {
  548. if (errno == ENETUNREACH) {
  549. /* routing error, presume not transient */
  550. goto tryall;
  551. } else
  552. /* retry */
  553. continue;
  554. }
  555. DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
  556. len, h.id, h.qr);
  557. send(fd, packet, len, 0);
  558. FD_ZERO(&fds);
  559. FD_SET(fd, &fds);
  560. tv.tv_sec = REPLY_TIMEOUT;
  561. tv.tv_usec = 0;
  562. if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
  563. DPRINTF("Timeout\n");
  564. /* timed out, so retry send and receive,
  565. * to next nameserver on queue */
  566. goto again;
  567. }
  568. i = recv(fd, packet, 512, 0);
  569. if (i < HFIXEDSZ)
  570. /* too short ! */
  571. goto again;
  572. __decode_header(packet, &h);
  573. DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
  574. if ((h.id != id) || (!h.qr))
  575. /* unsolicited */
  576. goto again;
  577. DPRINTF("Got response %s\n", "(i think)!");
  578. DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
  579. h.qdcount, h.ancount, h.nscount, h.arcount);
  580. DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
  581. h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
  582. if ((h.rcode) || (h.ancount < 1)) {
  583. /* negative result, not present */
  584. goto again;
  585. }
  586. pos = HFIXEDSZ;
  587. for (j = 0; j < h.qdcount; j++) {
  588. DPRINTF("Skipping question %d at %d\n", j, pos);
  589. i = __length_question(packet, pos);
  590. DPRINTF("Length of question %d is %d\n", j, i);
  591. if (i < 0)
  592. goto again;
  593. pos += i;
  594. }
  595. DPRINTF("Decoding answer at pos %d\n", pos);
  596. for (j=0;j<h.ancount;j++)
  597. {
  598. i = __decode_answer(packet, pos, a);
  599. if (i<0) {
  600. DPRINTF("failed decode %d\n", i);
  601. goto again;
  602. }
  603. /* For all but T_SIG, accept first answer */
  604. if (a->atype != T_SIG)
  605. break;
  606. DPRINTF("skipping T_SIG %d\n", i);
  607. free(a->dotted);
  608. pos += i;
  609. }
  610. DPRINTF("Answer name = |%s|\n", a->dotted);
  611. DPRINTF("Answer type = |%d|\n", a->atype);
  612. close(fd);
  613. if (outpacket)
  614. *outpacket = packet;
  615. else
  616. free(packet);
  617. free(lookup);
  618. UNLOCK;
  619. return (0); /* success! */
  620. tryall:
  621. /* if there are other nameservers, give them a go,
  622. otherwise return with error */
  623. variant = 0;
  624. if (retries >= nscount*(__searchdomains+1))
  625. goto fail;
  626. again:
  627. /* if there are searchdomains, try them or fallback as passed */
  628. if (variant < __searchdomains) {
  629. /* next search */
  630. variant++;
  631. } else {
  632. /* next server, first search */
  633. ns = (ns + 1) % nscount;
  634. variant = 0;
  635. }
  636. }
  637. fail:
  638. UNLOCK;
  639. if (fd != -1)
  640. close(fd);
  641. if (lookup)
  642. free(lookup);
  643. if (packet)
  644. free(packet);
  645. return -1;
  646. }
  647. #endif
  648. #ifdef L_opennameservers
  649. int __nameservers;
  650. char * __nameserver[MAX_SERVERS];
  651. int __searchdomains;
  652. char * __searchdomain[MAX_SEARCH];
  653. /*
  654. * we currently read formats not quite the same as that on normal
  655. * unix systems, we can have a list of nameservers after the keyword.
  656. */
  657. int __open_nameservers()
  658. {
  659. FILE *fp;
  660. int i;
  661. #define RESOLV_ARGS 5
  662. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  663. int argc;
  664. if (__nameservers > 0)
  665. return 0;
  666. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  667. (fp = fopen("/etc/config/resolv.conf", "r"))) {
  668. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  669. for (p = szBuffer; *p && isspace(*p); p++)
  670. /* skip white space */;
  671. if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  672. continue;
  673. argc = 0;
  674. while (*p && argc < RESOLV_ARGS) {
  675. argv[argc++] = p;
  676. while (*p && !isspace(*p) && *p != '\n')
  677. p++;
  678. while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  679. *p++ = '\0';
  680. }
  681. if (strcmp(argv[0], "nameserver") == 0) {
  682. for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
  683. __nameserver[__nameservers++] = strdup(argv[i]);
  684. DPRINTF("adding nameserver %s\n", argv[i]);
  685. }
  686. }
  687. /* domain and search are mutually exclusive, the last one wins */
  688. if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  689. while (__searchdomains > 0) {
  690. free(__searchdomain[--__searchdomains]);
  691. __searchdomain[__searchdomains] = NULL;
  692. }
  693. for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
  694. __searchdomain[__searchdomains++] = strdup(argv[i]);
  695. DPRINTF("adding search %s\n", argv[i]);
  696. }
  697. }
  698. }
  699. fclose(fp);
  700. } else {
  701. DPRINTF("failed to open %s\n", "resolv.conf");
  702. }
  703. DPRINTF("nameservers = %d\n", __nameservers);
  704. return 0;
  705. }
  706. #endif
  707. #ifdef L_closenameservers
  708. void __close_nameservers(void)
  709. {
  710. while (__nameservers > 0) {
  711. free(__nameserver[--__nameservers]);
  712. __nameserver[__nameservers] = NULL;
  713. }
  714. while (__searchdomains > 0) {
  715. free(__searchdomain[--__searchdomains]);
  716. __searchdomain[__searchdomains] = NULL;
  717. }
  718. }
  719. #endif
  720. #ifdef L_gethostbyname
  721. struct hostent *gethostbyname(const char *name)
  722. {
  723. static struct hostent h;
  724. static char buf[sizeof(struct in_addr) +
  725. sizeof(struct in_addr *)*2 +
  726. 256/*namebuffer*/ + 32/* margin */];
  727. struct hostent *hp;
  728. gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
  729. return hp;
  730. }
  731. #endif
  732. #ifdef L_gethostbyname2
  733. #ifdef __UCLIBC_HAS_IPV6__
  734. /* TBD: Not the right place for defining these, I guess */
  735. const struct in6_addr in6addr_any =
  736. { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
  737. const struct in6_addr in6addr_loopback =
  738. { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } };
  739. #endif /* __UCLIBC_HAS_IPV6__ */
  740. struct hostent *gethostbyname2(const char *name, int family)
  741. {
  742. #ifndef __UCLIBC_HAS_IPV6__
  743. return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
  744. #else /* __UCLIBC_HAS_IPV6__ */
  745. static struct hostent h;
  746. static char buf[sizeof(struct in6_addr) +
  747. sizeof(struct in6_addr *)*2 +
  748. 256/*namebuffer*/ + 32/* margin */];
  749. struct hostent *hp;
  750. gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
  751. return hp;
  752. #endif /* __UCLIBC_HAS_IPV6__ */
  753. }
  754. #endif
  755. #ifdef L_getnetbyname
  756. struct netent * getnetbyname(const char * name)
  757. {
  758. return NULL;
  759. }
  760. #endif
  761. #ifdef L_res_init
  762. struct __res_state * __res;
  763. #ifndef _res
  764. #define _res (*__res_state())
  765. #endif
  766. int res_init(void)
  767. {
  768. struct __res_state *rp = __res;
  769. if(!__res) {
  770. rp = (struct __res_state *) malloc(sizeof(struct __res_state));
  771. memset(rp, 0, sizeof(struct __res_state));
  772. __res = rp;
  773. }
  774. __open_nameservers();
  775. rp->retrans = RES_TIMEOUT;
  776. rp->retry = 4;
  777. rp->options = RES_INIT;
  778. rp->id = (u_int) random();
  779. rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
  780. rp->nsaddr.sin_family = AF_INET;
  781. rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
  782. rp->ndots = 1;
  783. /** rp->pfcode = 0; **/
  784. rp->_vcsock = -1;
  785. /** rp->_flags = 0; **/
  786. /** rp->qhook = NULL; **/
  787. /** rp->rhook = NULL; **/
  788. /** rp->_u._ext.nsinit = 0; **/
  789. if(__searchdomains) {
  790. int i;
  791. for(i=0; i<__searchdomains; i++) {
  792. rp->dnsrch[i] = __searchdomain[i];
  793. }
  794. }
  795. if(__nameservers) {
  796. int i;
  797. struct in_addr a;
  798. for(i=0; i<__nameservers; i++) {
  799. if (inet_aton(__nameserver[i], &a)) {
  800. rp->nsaddr_list[i].sin_addr = a;
  801. rp->nsaddr_list[i].sin_family = AF_INET;
  802. rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
  803. }
  804. }
  805. }
  806. rp->nscount = __nameservers;
  807. return(0);
  808. }
  809. struct __res_state * __res_state (void)
  810. {
  811. if(!__res) {
  812. res_init();
  813. }
  814. return __res;
  815. }
  816. void res_close( void )
  817. {
  818. if(__res) {
  819. free(__res);
  820. __res = NULL;
  821. }
  822. return;
  823. }
  824. #endif
  825. #ifdef L_res_query
  826. #ifndef MIN
  827. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  828. #endif
  829. int res_query(const char *dname, int class, int type,
  830. unsigned char *answer, int anslen)
  831. {
  832. unsigned char * packet = 0;
  833. struct resolv_answer a;
  834. int i;
  835. __open_nameservers();
  836. if (!dname || class != 1 /* CLASS_IN */)
  837. return(-1);
  838. memset((char *) &a, '\0', sizeof(a));
  839. i = __dns_lookup(dname, type, __nameservers, __nameserver, &packet, &a);
  840. if (i < 0)
  841. return(-1);
  842. free(a.dotted);
  843. if (a.atype == type) { /* CNAME*/
  844. if (anslen && answer)
  845. memcpy(answer, a.rdata, MIN(anslen, a.rdlength));
  846. if (packet)
  847. free(packet);
  848. return(MIN(anslen, a.rdlength));
  849. }
  850. if (packet)
  851. free(packet);
  852. return 0;
  853. }
  854. #endif
  855. #ifdef L_gethostbyaddr
  856. struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
  857. {
  858. static struct hostent h;
  859. static char buf[
  860. #ifndef __UCLIBC_HAS_IPV6__
  861. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  862. #else
  863. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  864. #endif /* __UCLIBC_HAS_IPV6__ */
  865. 256/*namebuffer*/ + 32/* margin */];
  866. struct hostent *hp;
  867. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  868. return hp;
  869. }
  870. #endif
  871. #ifdef L___read_etc_hosts_r
  872. void __open_etc_hosts(FILE **fp)
  873. {
  874. if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
  875. *fp = fopen("/etc/config/hosts", "r");
  876. }
  877. return;
  878. }
  879. int __read_etc_hosts_r(FILE * fp, const char * name, int type,
  880. enum etc_hosts_action action,
  881. struct hostent * result_buf,
  882. char * buf, size_t buflen,
  883. struct hostent ** result,
  884. int * h_errnop)
  885. {
  886. struct in_addr *in=NULL;
  887. struct in_addr **addr_list=NULL;
  888. #ifdef __UCLIBC_HAS_IPV6__
  889. struct in6_addr *in6=NULL;
  890. struct in6_addr **addr_list6=NULL;
  891. #endif /* __UCLIBC_HAS_IPV6__ */
  892. char *cp;
  893. #define MAX_ALIAS 5
  894. char *alias[MAX_ALIAS];
  895. int aliases, i;
  896. int ret=HOST_NOT_FOUND;
  897. if (action!=GETHOSTENT) {
  898. #ifdef __UCLIBC_HAS_IPV6__
  899. char *p=buf;
  900. size_t len=buflen;
  901. #endif /* __UCLIBC_HAS_IPV6__ */
  902. *h_errnop=NETDB_INTERNAL;
  903. if (buflen < sizeof(*in))
  904. return ERANGE;
  905. in=(struct in_addr*)buf;
  906. buf+=sizeof(*in);
  907. buflen-=sizeof(*in);
  908. if (buflen < sizeof(*addr_list)*2)
  909. return ERANGE;
  910. addr_list=(struct in_addr **)buf;
  911. buf+=sizeof(*addr_list)*2;
  912. buflen-=sizeof(*addr_list)*2;
  913. #ifdef __UCLIBC_HAS_IPV6__
  914. if (len < sizeof(*in6))
  915. return ERANGE;
  916. in6=(struct in6_addr*)p;
  917. p+=sizeof(*in6);
  918. len-=sizeof(*in6);
  919. if (len < sizeof(*addr_list6)*2)
  920. return ERANGE;
  921. addr_list6=(struct in6_addr**)p;
  922. p+=sizeof(*addr_list6)*2;
  923. len-=sizeof(*addr_list6)*2;
  924. if (len < buflen) {
  925. buflen=len;
  926. buf=p;
  927. }
  928. #endif /* __UCLIBC_HAS_IPV6__ */
  929. if (buflen < 80)
  930. return ERANGE;
  931. __open_etc_hosts(&fp);
  932. if (fp == NULL) {
  933. result=NULL;
  934. return errno;
  935. }
  936. }
  937. *h_errnop=HOST_NOT_FOUND;
  938. while (fgets(buf, buflen, fp)) {
  939. if ((cp = strchr(buf, '#')))
  940. *cp = '\0';
  941. DPRINTF("Looking at: %s\n", buf);
  942. aliases = 0;
  943. cp = buf;
  944. while (*cp) {
  945. while (*cp && isspace(*cp))
  946. *cp++ = '\0';
  947. if (!*cp)
  948. continue;
  949. if (aliases < MAX_ALIAS)
  950. alias[aliases++] = cp;
  951. while (*cp && !isspace(*cp))
  952. cp++;
  953. }
  954. if (aliases < 2)
  955. continue; /* syntax error really */
  956. if (action==GETHOSTENT) {
  957. /* Return whatever the next entry happens to be. */
  958. break;
  959. } else if (action==GET_HOSTS_BYADDR) {
  960. if (strcmp(name, alias[0]) != 0)
  961. continue;
  962. } else {
  963. /* GET_HOSTS_BYNAME */
  964. for (i = 1; i < aliases; i++)
  965. if (strcasecmp(name, alias[i]) == 0)
  966. break;
  967. if (i >= aliases)
  968. continue;
  969. }
  970. if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
  971. DPRINTF("Found INET\n");
  972. addr_list[0] = in;
  973. addr_list[1] = 0;
  974. result_buf->h_name = alias[1];
  975. result_buf->h_addrtype = AF_INET;
  976. result_buf->h_length = sizeof(*in);
  977. result_buf->h_addr_list = (char**) addr_list;
  978. *result=result_buf;
  979. ret=NETDB_SUCCESS;
  980. #ifdef __UCLIBC_HAS_IPV6__
  981. } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
  982. DPRINTF("Found INET6\n");
  983. addr_list6[0] = in6;
  984. addr_list6[1] = 0;
  985. result_buf->h_name = alias[1];
  986. result_buf->h_addrtype = AF_INET6;
  987. result_buf->h_length = sizeof(*in6);
  988. result_buf->h_addr_list = (char**) addr_list6;
  989. *result=result_buf;
  990. ret=NETDB_SUCCESS;
  991. #endif /* __UCLIBC_HAS_IPV6__ */
  992. } else {
  993. DPRINTF("Error\n");
  994. ret=TRY_AGAIN;
  995. break; /* bad ip address */
  996. }
  997. if (action!=GETHOSTENT) {
  998. fclose(fp);
  999. }
  1000. return ret;
  1001. }
  1002. if (action!=GETHOSTENT) {
  1003. fclose(fp);
  1004. }
  1005. return ret;
  1006. }
  1007. #endif
  1008. #ifdef L_gethostent
  1009. #ifdef __UCLIBC_HAS_THREADS__
  1010. #include <pthread.h>
  1011. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  1012. # define LOCK pthread_mutex_lock(&mylock)
  1013. # define UNLOCK pthread_mutex_unlock(&mylock);
  1014. #else
  1015. # define LOCK
  1016. # define UNLOCK
  1017. #endif
  1018. static int __stay_open;
  1019. static FILE * __gethostent_fp;
  1020. void endhostent (void)
  1021. {
  1022. LOCK;
  1023. __stay_open = 0;
  1024. if (__gethostent_fp) {
  1025. fclose(__gethostent_fp);
  1026. }
  1027. UNLOCK;
  1028. }
  1029. void sethostent (int stay_open)
  1030. {
  1031. LOCK;
  1032. __stay_open = stay_open;
  1033. UNLOCK;
  1034. }
  1035. struct hostent *gethostent (void)
  1036. {
  1037. static struct hostent h;
  1038. static char buf[
  1039. #ifndef __UCLIBC_HAS_IPV6__
  1040. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1041. #else
  1042. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1043. #endif /* __UCLIBC_HAS_IPV6__ */
  1044. 80/*namebuffer*/ + 2/* margin */];
  1045. struct hostent *host;
  1046. LOCK;
  1047. if (__gethostent_fp == NULL) {
  1048. __open_etc_hosts(&__gethostent_fp);
  1049. if (__gethostent_fp == NULL) {
  1050. UNLOCK;
  1051. return((struct hostent *)NULL);
  1052. }
  1053. }
  1054. __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  1055. &h, buf, sizeof(buf), &host, &h_errno);
  1056. if (__stay_open==0) {
  1057. fclose(__gethostent_fp);
  1058. }
  1059. UNLOCK;
  1060. return(host);
  1061. }
  1062. #endif
  1063. #ifdef L___get_hosts_byname_r
  1064. int __get_hosts_byname_r(const char * name, int type,
  1065. struct hostent * result_buf,
  1066. char * buf, size_t buflen,
  1067. struct hostent ** result,
  1068. int * h_errnop)
  1069. {
  1070. return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, result_buf, buf, buflen, result, h_errnop));
  1071. }
  1072. #endif
  1073. #ifdef L___get_hosts_byaddr_r
  1074. int __get_hosts_byaddr_r(const char * addr, int len, int type,
  1075. struct hostent * result_buf,
  1076. char * buf, size_t buflen,
  1077. struct hostent ** result,
  1078. int * h_errnop)
  1079. {
  1080. #ifndef __UCLIBC_HAS_IPV6__
  1081. char ipaddr[INET_ADDRSTRLEN];
  1082. #else
  1083. char ipaddr[INET6_ADDRSTRLEN];
  1084. #endif /* __UCLIBC_HAS_IPV6__ */
  1085. switch (type) {
  1086. case AF_INET:
  1087. if (len != sizeof(struct in_addr))
  1088. return 0;
  1089. break;
  1090. #ifdef __UCLIBC_HAS_IPV6__
  1091. case AF_INET6:
  1092. if (len != sizeof(struct in6_addr))
  1093. return 0;
  1094. break;
  1095. #endif /* __UCLIBC_HAS_IPV6__ */
  1096. default:
  1097. return 0;
  1098. }
  1099. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1100. return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1101. result_buf, buf, buflen, result, h_errnop));
  1102. }
  1103. #endif
  1104. #ifdef L_getnameinfo
  1105. #ifndef min
  1106. # define min(x,y) (((x) > (y)) ? (y) : (x))
  1107. #endif /* min */
  1108. int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  1109. socklen_t hostlen, char *serv, socklen_t servlen,
  1110. unsigned int flags)
  1111. {
  1112. int serrno = errno;
  1113. int ok = 0;
  1114. struct hostent *h = NULL;
  1115. char domain[256];
  1116. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1117. return EAI_BADFLAGS;
  1118. if (sa == NULL || addrlen < sizeof (sa_family_t))
  1119. return EAI_FAMILY;
  1120. switch (sa->sa_family) {
  1121. case AF_LOCAL:
  1122. break;
  1123. case AF_INET:
  1124. if (addrlen < sizeof (struct sockaddr_in))
  1125. return EAI_FAMILY;
  1126. break;
  1127. #ifdef __UCLIBC_HAS_IPV6__
  1128. case AF_INET6:
  1129. if (addrlen < sizeof (struct sockaddr_in6))
  1130. return EAI_FAMILY;
  1131. break;
  1132. #endif /* __UCLIBC_HAS_IPV6__ */
  1133. default:
  1134. return EAI_FAMILY;
  1135. }
  1136. if (host != NULL && hostlen > 0)
  1137. switch (sa->sa_family) {
  1138. case AF_INET:
  1139. #ifdef __UCLIBC_HAS_IPV6__
  1140. case AF_INET6:
  1141. #endif /* __UCLIBC_HAS_IPV6__ */
  1142. if (!(flags & NI_NUMERICHOST)) {
  1143. #ifdef __UCLIBC_HAS_IPV6__
  1144. if (sa->sa_family == AF_INET6)
  1145. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1146. sizeof(struct in6_addr), AF_INET6);
  1147. else
  1148. #endif /* __UCLIBC_HAS_IPV6__ */
  1149. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1150. sizeof(struct in_addr), AF_INET);
  1151. if (h) {
  1152. char *c;
  1153. if ((flags & NI_NOFQDN)
  1154. && (getdomainname (domain, sizeof(domain)) == 0)
  1155. && (c = strstr (h->h_name, domain))
  1156. && (c != h->h_name) && (*(--c) == '.')) {
  1157. strncpy (host, h->h_name,
  1158. min(hostlen, (size_t) (c - h->h_name)));
  1159. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1160. ok = 1;
  1161. } else {
  1162. strncpy (host, h->h_name, hostlen);
  1163. ok = 1;
  1164. }
  1165. }
  1166. }
  1167. if (!ok) {
  1168. if (flags & NI_NAMEREQD) {
  1169. errno = serrno;
  1170. return EAI_NONAME;
  1171. } else {
  1172. const char *c;
  1173. #ifdef __UCLIBC_HAS_IPV6__
  1174. if (sa->sa_family == AF_INET6) {
  1175. const struct sockaddr_in6 *sin6p;
  1176. sin6p = (const struct sockaddr_in6 *) sa;
  1177. c = inet_ntop (AF_INET6,
  1178. (const void *) &sin6p->sin6_addr, host, hostlen);
  1179. #if 0
  1180. /* Does scope id need to be supported? */
  1181. uint32_t scopeid;
  1182. scopeid = sin6p->sin6_scope_id;
  1183. if (scopeid != 0) {
  1184. /* Buffer is >= IFNAMSIZ+1. */
  1185. char scopebuf[IFNAMSIZ + 1];
  1186. char *scopeptr;
  1187. int ni_numericscope = 0;
  1188. size_t real_hostlen = __strnlen (host, hostlen);
  1189. size_t scopelen = 0;
  1190. scopebuf[0] = SCOPE_DELIMITER;
  1191. scopebuf[1] = '\0';
  1192. scopeptr = &scopebuf[1];
  1193. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1194. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1195. if (if_indextoname (scopeid, scopeptr) == NULL)
  1196. ++ni_numericscope;
  1197. else
  1198. scopelen = strlen (scopebuf);
  1199. } else {
  1200. ++ni_numericscope;
  1201. }
  1202. if (ni_numericscope)
  1203. scopelen = 1 + snprintf (scopeptr,
  1204. (scopebuf
  1205. + sizeof scopebuf
  1206. - scopeptr),
  1207. "%u", scopeid);
  1208. if (real_hostlen + scopelen + 1 > hostlen)
  1209. return EAI_SYSTEM;
  1210. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1211. }
  1212. #endif
  1213. } else
  1214. #endif /* __UCLIBC_HAS_IPV6__ */
  1215. c = inet_ntop (AF_INET,
  1216. (const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
  1217. host, hostlen);
  1218. if (c == NULL) {
  1219. errno = serrno;
  1220. return EAI_SYSTEM;
  1221. }
  1222. }
  1223. ok = 1;
  1224. }
  1225. break;
  1226. case AF_LOCAL:
  1227. if (!(flags & NI_NUMERICHOST)) {
  1228. struct utsname utsname;
  1229. if (!uname (&utsname)) {
  1230. strncpy (host, utsname.nodename, hostlen);
  1231. break;
  1232. };
  1233. };
  1234. if (flags & NI_NAMEREQD) {
  1235. errno = serrno;
  1236. return EAI_NONAME;
  1237. }
  1238. strncpy (host, "localhost", hostlen);
  1239. break;
  1240. default:
  1241. return EAI_FAMILY;
  1242. }
  1243. if (serv && (servlen > 0)) {
  1244. switch (sa->sa_family) {
  1245. case AF_INET:
  1246. #ifdef __UCLIBC_HAS_IPV6__
  1247. case AF_INET6:
  1248. #endif /* __UCLIBC_HAS_IPV6__ */
  1249. if (!(flags & NI_NUMERICSERV)) {
  1250. struct servent *s;
  1251. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1252. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1253. if (s) {
  1254. strncpy (serv, s->s_name, servlen);
  1255. break;
  1256. }
  1257. }
  1258. snprintf (serv, servlen, "%d",
  1259. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1260. break;
  1261. case AF_LOCAL:
  1262. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1263. break;
  1264. }
  1265. }
  1266. if (host && (hostlen > 0))
  1267. host[hostlen-1] = 0;
  1268. if (serv && (servlen > 0))
  1269. serv[servlen-1] = 0;
  1270. errno = serrno;
  1271. return 0;
  1272. }
  1273. #endif
  1274. #ifdef L_gethostbyname_r
  1275. int gethostbyname_r(const char * name,
  1276. struct hostent * result_buf,
  1277. char * buf, size_t buflen,
  1278. struct hostent ** result,
  1279. int * h_errnop)
  1280. {
  1281. struct in_addr *in;
  1282. struct in_addr **addr_list;
  1283. unsigned char *packet;
  1284. struct resolv_answer a;
  1285. int i;
  1286. int nest = 0;
  1287. __open_nameservers();
  1288. *result=NULL;
  1289. if (!name)
  1290. return EINVAL;
  1291. /* do /etc/hosts first */
  1292. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1293. buf, buflen, result, h_errnop))==0)
  1294. return i;
  1295. switch (*h_errnop) {
  1296. case HOST_NOT_FOUND:
  1297. case NO_ADDRESS:
  1298. break;
  1299. case NETDB_INTERNAL:
  1300. if (errno == ENOENT) {
  1301. break;
  1302. }
  1303. /* else fall through */
  1304. default:
  1305. return i;
  1306. }
  1307. DPRINTF("Nothing found in /etc/hosts\n");
  1308. *h_errnop = NETDB_INTERNAL;
  1309. if (buflen < sizeof(*in))
  1310. return ERANGE;
  1311. in=(struct in_addr*)buf;
  1312. buf+=sizeof(*in);
  1313. buflen-=sizeof(*in);
  1314. if (buflen < sizeof(*addr_list)*2)
  1315. return ERANGE;
  1316. addr_list=(struct in_addr**)buf;
  1317. buf+=sizeof(*addr_list)*2;
  1318. buflen-=sizeof(*addr_list)*2;
  1319. addr_list[0] = in;
  1320. addr_list[1] = 0;
  1321. if (buflen<256)
  1322. return ERANGE;
  1323. strncpy(buf, name, buflen);
  1324. /* First check if this is already an address */
  1325. if (inet_aton(name, in)) {
  1326. result_buf->h_name = buf;
  1327. result_buf->h_addrtype = AF_INET;
  1328. result_buf->h_length = sizeof(*in);
  1329. result_buf->h_addr_list = (char **) addr_list;
  1330. *result=result_buf;
  1331. *h_errnop = NETDB_SUCCESS;
  1332. return NETDB_SUCCESS;
  1333. }
  1334. for (;;) {
  1335. i = __dns_lookup(buf, T_A, __nameservers, __nameserver, &packet, &a);
  1336. if (i < 0) {
  1337. *h_errnop = HOST_NOT_FOUND;
  1338. DPRINTF("__dns_lookup\n");
  1339. return TRY_AGAIN;
  1340. }
  1341. strncpy(buf, a.dotted, buflen);
  1342. free(a.dotted);
  1343. if (a.atype == T_CNAME) { /* CNAME */
  1344. DPRINTF("Got a CNAME in gethostbyname()\n");
  1345. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1346. free(packet);
  1347. if (i < 0) {
  1348. *h_errnop = NO_RECOVERY;
  1349. DPRINTF("__decode_dotted\n");
  1350. return -1;
  1351. }
  1352. if (++nest > MAX_RECURSE) {
  1353. *h_errnop = NO_RECOVERY;
  1354. DPRINTF("recursion\n");
  1355. return -1;
  1356. }
  1357. continue;
  1358. } else if (a.atype == T_A) { /* ADDRESS */
  1359. memcpy(in, a.rdata, sizeof(*in));
  1360. result_buf->h_name = buf;
  1361. result_buf->h_addrtype = AF_INET;
  1362. result_buf->h_length = sizeof(*in);
  1363. result_buf->h_addr_list = (char **) addr_list;
  1364. free(packet);
  1365. break;
  1366. } else {
  1367. free(packet);
  1368. *h_errnop=HOST_NOT_FOUND;
  1369. return TRY_AGAIN;
  1370. }
  1371. }
  1372. *result=result_buf;
  1373. return NETDB_SUCCESS;
  1374. }
  1375. #endif
  1376. #ifdef L_gethostbyname2_r
  1377. #ifdef __UCLIBC_HAS_IPV6__
  1378. /* TBD: Not the right place for defining these, I guess */
  1379. /*
  1380. const struct in6_addr in6addr_any =
  1381. { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
  1382. const struct in6_addr in6addr_loopback =
  1383. { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } };
  1384. */
  1385. #endif /* __UCLIBC_HAS_IPV6__ */
  1386. int gethostbyname2_r(const char *name, int family,
  1387. struct hostent * result_buf,
  1388. char * buf, size_t buflen,
  1389. struct hostent ** result,
  1390. int * h_errnop)
  1391. {
  1392. #ifndef __UCLIBC_HAS_IPV6__
  1393. return family == AF_INET ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1394. #else /* __UCLIBC_HAS_IPV6__ */
  1395. struct in6_addr *in;
  1396. struct in6_addr **addr_list;
  1397. unsigned char *packet;
  1398. struct resolv_answer a;
  1399. int i;
  1400. int nest = 0;
  1401. if (family == AF_INET)
  1402. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1403. if (family != AF_INET6)
  1404. return EINVAL;
  1405. __open_nameservers();
  1406. *result=NULL;
  1407. if (!name)
  1408. return EINVAL;
  1409. /* do /etc/hosts first */
  1410. if ((i=__get_hosts_byname_r(name, family, result_buf,
  1411. buf, buflen, result, h_errnop))==0)
  1412. return i;
  1413. switch (*h_errnop) {
  1414. case HOST_NOT_FOUND:
  1415. case NO_ADDRESS:
  1416. break;
  1417. default:
  1418. return i;
  1419. }
  1420. DPRINTF("Nothing found in /etc/hosts\n");
  1421. *h_errnop = NETDB_INTERNAL;
  1422. if (buflen < sizeof(*in))
  1423. return ERANGE;
  1424. in=(struct in6_addr*)buf;
  1425. buf+=sizeof(*in);
  1426. buflen-=sizeof(*in);
  1427. if (buflen < sizeof(*addr_list)*2)
  1428. return ERANGE;
  1429. addr_list=(struct in6_addr**)buf;
  1430. buf+=sizeof(*addr_list)*2;
  1431. buflen-=sizeof(*addr_list)*2;
  1432. addr_list[0] = in;
  1433. addr_list[1] = 0;
  1434. if (buflen<256)
  1435. return ERANGE;
  1436. strncpy(buf, name, buflen);
  1437. /* First check if this is already an address */
  1438. if (inet_pton(AF_INET6, name, in)) {
  1439. result_buf->h_name = buf;
  1440. result_buf->h_addrtype = AF_INET6;
  1441. result_buf->h_length = sizeof(*in);
  1442. result_buf->h_addr_list = (char **) addr_list;
  1443. *result=result_buf;
  1444. *h_errnop = NETDB_SUCCESS;
  1445. return NETDB_SUCCESS;
  1446. }
  1447. for (;;) {
  1448. i = __dns_lookup(buf, T_AAAA, __nameservers, __nameserver, &packet, &a);
  1449. if (i < 0) {
  1450. *h_errnop = HOST_NOT_FOUND;
  1451. return TRY_AGAIN;
  1452. }
  1453. strncpy(buf, a.dotted, buflen);
  1454. free(a.dotted);
  1455. if (a.atype == T_CNAME) { /* CNAME */
  1456. DPRINTF("Got a CNAME in gethostbyname()\n");
  1457. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1458. free(packet);
  1459. if (i < 0) {
  1460. *h_errnop = NO_RECOVERY;
  1461. return -1;
  1462. }
  1463. if (++nest > MAX_RECURSE) {
  1464. *h_errnop = NO_RECOVERY;
  1465. return -1;
  1466. }
  1467. continue;
  1468. } else if (a.atype == T_AAAA) { /* ADDRESS */
  1469. memcpy(in, a.rdata, sizeof(*in));
  1470. result_buf->h_name = buf;
  1471. result_buf->h_addrtype = AF_INET6;
  1472. result_buf->h_length = sizeof(*in);
  1473. result_buf->h_addr_list = (char **) addr_list;
  1474. free(packet);
  1475. break;
  1476. } else {
  1477. free(packet);
  1478. *h_errnop=HOST_NOT_FOUND;
  1479. return TRY_AGAIN;
  1480. }
  1481. }
  1482. *result=result_buf;
  1483. return NETDB_SUCCESS;
  1484. #endif /* __UCLIBC_HAS_IPV6__ */
  1485. }
  1486. #endif
  1487. #ifdef L_gethostbyaddr_r
  1488. int gethostbyaddr_r (const void *addr, socklen_t len, int type,
  1489. struct hostent * result_buf,
  1490. char * buf, size_t buflen,
  1491. struct hostent ** result,
  1492. int * h_errnop)
  1493. {
  1494. struct in_addr *in;
  1495. struct in_addr **addr_list;
  1496. #ifdef __UCLIBC_HAS_IPV6__
  1497. char *qp;
  1498. size_t plen;
  1499. struct in6_addr *in6;
  1500. struct in6_addr **addr_list6;
  1501. #endif /* __UCLIBC_HAS_IPV6__ */
  1502. unsigned char *packet;
  1503. struct resolv_answer a;
  1504. int i;
  1505. int nest = 0;
  1506. *result=NULL;
  1507. if (!addr)
  1508. return EINVAL;
  1509. switch (type) {
  1510. case AF_INET:
  1511. if (len != sizeof(struct in_addr))
  1512. return EINVAL;
  1513. break;
  1514. #ifdef __UCLIBC_HAS_IPV6__
  1515. case AF_INET6:
  1516. if (len != sizeof(struct in6_addr))
  1517. return EINVAL;
  1518. break;
  1519. #endif /* __UCLIBC_HAS_IPV6__ */
  1520. default:
  1521. return EINVAL;
  1522. }
  1523. /* do /etc/hosts first */
  1524. if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
  1525. buf, buflen, result, h_errnop))==0)
  1526. return i;
  1527. switch (*h_errnop) {
  1528. case HOST_NOT_FOUND:
  1529. case NO_ADDRESS:
  1530. break;
  1531. default:
  1532. return i;
  1533. }
  1534. __open_nameservers();
  1535. #ifdef __UCLIBC_HAS_IPV6__
  1536. qp=buf;
  1537. plen=buflen;
  1538. #endif /* __UCLIBC_HAS_IPV6__ */
  1539. *h_errnop = NETDB_INTERNAL;
  1540. if (buflen < sizeof(*in))
  1541. return ERANGE;
  1542. in=(struct in_addr*)buf;
  1543. buf+=sizeof(*in);
  1544. buflen-=sizeof(*in);
  1545. if (buflen < sizeof(*addr_list)*2)
  1546. return ERANGE;
  1547. addr_list=(struct in_addr**)buf;
  1548. buf+=sizeof(*addr_list)*2;
  1549. buflen-=sizeof(*addr_list)*2;
  1550. #ifdef __UCLIBC_HAS_IPV6__
  1551. if (plen < sizeof(*in6))
  1552. return ERANGE;
  1553. in6=(struct in6_addr*)qp;
  1554. qp+=sizeof(*in6);
  1555. plen-=sizeof(*in6);
  1556. if (plen < sizeof(*addr_list6)*2)
  1557. return ERANGE;
  1558. addr_list6=(struct in6_addr**)qp;
  1559. qp+=sizeof(*addr_list6)*2;
  1560. plen-=sizeof(*addr_list6)*2;
  1561. if (len < buflen) {
  1562. buflen=len;
  1563. buf=qp;
  1564. }
  1565. #endif /* __UCLIBC_HAS_IPV6__ */
  1566. if (buflen<256)
  1567. return ERANGE;
  1568. if(type == AF_INET) {
  1569. unsigned char *tmp_addr = (unsigned char *)addr;
  1570. memcpy(&in->s_addr, addr, len);
  1571. addr_list[0] = in;
  1572. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  1573. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  1574. #ifdef __UCLIBC_HAS_IPV6__
  1575. } else {
  1576. memcpy(in6->s6_addr, addr, len);
  1577. addr_list6[0] = in6;
  1578. qp = buf;
  1579. for (i = len - 1; i >= 0; i--) {
  1580. qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
  1581. (in6->s6_addr[i] >> 4) & 0xf);
  1582. }
  1583. strcpy(qp, "ip6.int");
  1584. #endif /* __UCLIBC_HAS_IPV6__ */
  1585. }
  1586. addr_list[1] = 0;
  1587. for (;;) {
  1588. i = __dns_lookup(buf, T_PTR, __nameservers, __nameserver, &packet, &a);
  1589. if (i < 0) {
  1590. *h_errnop = HOST_NOT_FOUND;
  1591. return TRY_AGAIN;
  1592. }
  1593. strncpy(buf, a.dotted, buflen);
  1594. free(a.dotted);
  1595. if (a.atype == T_CNAME) { /* CNAME */
  1596. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  1597. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1598. free(packet);
  1599. if (i < 0) {
  1600. *h_errnop = NO_RECOVERY;
  1601. return -1;
  1602. }
  1603. if (++nest > MAX_RECURSE) {
  1604. *h_errnop = NO_RECOVERY;
  1605. return -1;
  1606. }
  1607. continue;
  1608. } else if (a.atype == T_PTR) { /* ADDRESS */
  1609. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1610. free(packet);
  1611. result_buf->h_name = buf;
  1612. result_buf->h_addrtype = type;
  1613. if(type == AF_INET) {
  1614. result_buf->h_length = sizeof(*in);
  1615. #ifdef __UCLIBC_HAS_IPV6__
  1616. } else {
  1617. result_buf->h_length = sizeof(*in6);
  1618. #endif /* __UCLIBC_HAS_IPV6__ */
  1619. }
  1620. result_buf->h_addr_list = (char **) addr_list;
  1621. break;
  1622. } else {
  1623. free(packet);
  1624. *h_errnop = NO_ADDRESS;
  1625. return TRY_AGAIN;
  1626. }
  1627. }
  1628. *result=result_buf;
  1629. return NETDB_SUCCESS;
  1630. }
  1631. #endif