resolv.c 43 KB

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