resolv.c 43 KB

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