resolv.c 44 KB

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