resolv.c 42 KB

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