resolv.c 43 KB

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